Converting a CIImage to a Bitmap
Sunday, 04 March 2007
Someone on Apple's OpenGL mailing list asked about converting a CIImage into a pixel bitmap. This is harder than one might think, and there are a bunch of ways of doing it. Here's one. I'm posting it here because the mailing list has a lame 18Kb size limit and this code was too big for the list. Another way of doing this is with glReadPixels. I don't think that's a good way because it means that the CIImage needs to be rendered into an OpenGL context (moving it to the GPU and possibly doing a data transfer on the GPU) and then copied back into system memory. I think the technique below stays on system memory and never hits the GPU. If you need to do this conversion often, you should cache the CIContext and the CGContext and regenerate them only if the image becomes larger - creating them is very expensive in my experience. I'll be glad to post code that caches if you'd like, I just pulled the caching out for what is below. Please leave a comment if you'd like me to add that — I pulled that out of my code to post this simpler version. If you plan to modify the pixels, you'll probably want to unpremultiply them first. You can use vImage to do this. And if the image is intended for something other than the screen, you should change the colorspace. And don't forget to Clickie for The Code Update: I've posted the version with caching here |
SearchLatest Musings
|