views:

31

answers:

1

Hi folks, let me first off noting that I have absolutely no idea what I'm doing with objective-c and mac development (though I'm fine with c). I made a wonderfully simple graphics utility on leopard with the Quartz-2d binding for python:

http://developer.apple.com/graphicsimaging/pythonandquartz.html

that basically inputs a text file and writes a nice png file (it's a command line utility). I was happy as a pig in mud until I moved the utility to our snow leopard servers and discovered that there were all sorts of issues with CoreGraphics and 32 bit python on snow leopard. Some of these issues are soluble, and some not. So, I'm attempting to port this simple utility script to objective-c (really C I suppose) and running into a few issues. Does anyone else know if there's a nice example almost exactly like the one given in python and quartz, but all in native code?

My major issue is writing the graphics context to a file

myBitmapContext = MyCreateBitmapContext (400, 300);

CGContextSetRGBFillColor (myBitmapContext, 1, 0, 0, 1);
CGContextFillRect (myBitmapContext, CGRectMake (0, 0, 200, 100 ));
CGContextSetRGBFillColor (myBitmapContext, 0, 0, 1, .5);
CGContextFillRect (myBitmapContext, CGRectMake (0, 0, 100, 200 ));
CGImageRef myImage = CGBitmapContextCreateImage (myBitmapContext);// 5

CGContextDrawImage(myBitmapContext, myBoundingBox, myImage);// 6
char *bitmapData = CGBitmapContextGetData(myBitmapContext); // 7

// I'd like to write to a file here!

CGContextRelease (myBitmapContext);// 8
if (bitmapData) free(bitmapData); // 9
CGImageRelease(myImage);

MyCreateBitmapContext is a simple function from apple's guide on quartz 2d.

TL;DR Does anyone have a C port of the python demo given in the above link?

+1  A: 
CGImageRef myImage = CGBitmapContextCreateImage (myBitmapContext);// 5

CGContextDrawImage(myBitmapContext, myBoundingBox, myImage);// 6

What? Why would you capture the contents of the context as an image, and then draw that image back into the context you got it from?

// I'd like to write to a file here!

Do step 5, then feed that image to a CGImageDestination.

You may find the Core Graphics Reference Collection handy. There's a document like that for each of most of the frameworks.

Peter Hosey
It was a stupid demo in Core grahpics. I think I found what I was looking for in image i/o... http://developer.apple.com/mac/library/documentation/GraphicsImaging/Conceptual/ImageIOGuide/ikpg_dest/ikpg_dest.html. Thanks for noticing that though!
Ralphleon
Yes, CGImageDestination and CGImageSource are the two classes of the ImageIO framework, though they also count as part of Core Graphics.
Peter Hosey