Home  |  software  |  Musings  |  Pictorial  |  Academics  |  Links

Physics, Spring 2001  |  NSF Report  |  Transport in Nanostructures

Utilitage For Your Drawage

Due to popular request, below are my image conversion and utility routines, expanding on this earlier post. The one that people ask for regularly is the caching CIContext stuff used to speed conversion of a CIImage to an NSImage or CGImageRef. There's nothing particularly tricky about it, basically, you just make the context on the fly as needed and if it needs to be bigger, you create a new one, but you never make it smaller. Easy-sleezy, McFreezy!

So there's code here to go from CIImage to a premultiplied bitmap buffer, CIImage to an unpremultiplied bitmap buffer, CIImage to a CGImage. There's code to simplify creating a bitmap CGContextRef you can use for rendering something that's destined to go to the screen (it just wraps the CoreGraphics stuff and uses settings suitable for to-screen drawing). There're RGB <-> HSV conversions, which are rather skanky and not at all optimized.

And finally, there's a bunch of stuff to help with resolution independence. If you want to use PNGs instead of multi-image TIFFs, you can just name your PNGs with "MyImage.png" (for 1x), "MyImage_1.25.png" (for 1.25x), "MyImage_2.png" for (2x) and so on, and grab the most suitable one for a given scale factor with two calls.

There's also a selector that gives you the "best" CGImageRef for the image at a given filesystem path, given a known scale factor for that image and the scale factor you want to display the image with. This does a bunch of computation and heuristics based on the image's pixel dimension and DPI metadata and scales it if necessary, but tries not to.

Enjoy!

Clickie for

Search

Syndication

Login

Username

Password

Remember me
Forgotten your password?
© 2005 — All Rights Reserved Site credits >