Name:
Anonymous
2009-07-28 13:59
Welcome to the first annual /prog/ Code off !
The language will be C.
Compeptitors include (in no particular order):
Mr. Ribs
Xarn
FV
Anonymous
W. T. Snacks
Rumor has it, that there may be a special guest appearance from either The Sussman or
Leah Culver.
What's the point of this, you ask?
It's to determine who is an EXPERT PROGRAMMER.
Marking criteria:
Optimization
Elegance
Bonus marks:
indentation
Good luck!
Name:
Anonymous
2009-07-29 4:44
#include <Carbon/Carbon.h>
CGImageRef LoadJPEGImageAtFileSystemPath(const char *path)
{
CFURLRef url;
CGDataProviderRef dataProvider;
CGImageRef image = NULL;
url = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault,
(const UInt8 *)path, strlen(path), false);
if (dataProvider = CGDataProviderCreateWithURL(url))
{
image = CGImageCreateWithJPEGDataProvider(dataProvider, NULL,
true, kCGRenderingIntentPerceptual);
CGDataProviderRelease(dataProvider);
}
CFRelease(url);
return image;
}
void InitializeDisplaySystem()
{
ProcessSerialNumber psn = {0, kCurrentProcess};
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
SetFrontProcess(&psn);
}
WindowRef CreateWindowForImage(CGImageRef image)
{
Rect bounds;
size_t width, height;
WindowRef window;
GetAvailableWindowPositioningBounds(NULL, &bounds);
width = CGImageGetWidth(image);
height = CGImageGetHeight(image);
if (bounds.right - bounds.left > width)
{
bounds.left = (bounds.left + bounds.right - width) / 2;
bounds.right = bounds.left + width;
}
if (bounds.bottom - bounds.top > height)
{
bounds.top = (bounds.top + bounds.bottom - height) / 2;
bounds.bottom = bounds.top + height;
}
CreateNewWindow(kPlainWindowClass, kWindowNoAttributes, &bounds,
&window);
return window;
}
void RenderTwoImagesBlendedTogetherIntoWindow(WindowRef window,
CGImageRef firstImage, CGImageRef secondImage)
{
CGContextRef context;
CGRect drawArea = CGRectMake(0, 0, CGImageGetWidth(firstImage),
CGImageGetHeight(firstImage));
QDBeginCGContext(GetWindowPort(window), &context);
CGContextSaveGState(context);
CGContextDrawImage(context, drawArea, firstImage);
CGContextSetAlpha(context, 0.5);
CGContextDrawImage(context, drawArea, secondImage);
CGContextRestoreGState(context);
QDEndCGContext(GetWindowPort(window), &context);
}
OSStatus HandleExitEvent(EventHandlerCallRef inHandlerCallRef,
EventRef inEvent, void *inUserData)
{
QuitApplicationEventLoop();
return noErr;
}
void DoEventLoop(WindowRef window)
{
EventTypeSpec exitEvents[2] = {
{kEventClassKeyboard, kEventRawKeyDown},
{kEventClassMouse, kEventMouseDown}
};
InstallWindowEventHandler(window, HandleExitEvent, 2, exitEvents,
NULL, NULL);
RunApplicationEventLoop();
}
int main(int argc, char *argv[])
{
CGImageRef image1, image2;
WindowRef window;
if (argc != 3) errx(1, "U MENA ARGS?");
image1 = LoadJPEGImageAtFileSystemPath(argv[1]);
image2 = LoadJPEGImageAtFileSystemPath(argv[2]);
if (!image1 || !image2) errx(2, "images no load");
InitializeDisplaySystem();
window = CreateWindowForImage(image1);
ShowWindow(window);
RenderTwoImagesBlendedTogetherIntoWindow(window, image1, image2);
DoEventLoop(window);
return 0;
}