Skip navigation.
 
mlImage scaling
FROM : Adam Knight
DATE : Fri Jan 04 23:25:53 2008

I think I'm missing something here.

I need to take an image and scale it (very cleanly) down while keeping 
the metadata.  The source is a JPEG file on-disk (typically from a 
digital camera).  The destination is an NSData object in memory that 
is JPEG-encoded with the same metadata.

I tried this using CIImage and a CILanczosScaleTransform filter then 
using NSBitmapImageRep's initWithCIImage method.  This worked very 
well and made a great image, but it looks like Apple's initWithCIImage 
code leaks a whole copy of the image outside of my reach.  I'm working 
with too many images for this to be good.  Also, this method drops the 
file metadata entirely (EXIF, etc.) and doesn't appear to give me the 
chance to add it back in.

So then I tried using a CGImageRef with 
CGImageSourceCreateThumbnailAtIndex and copying the metadata with 
CGImageSourceCopyPropertiesAtIndex.  This works well except that the 
scaled image is blurry and not at all professional.  I tried fiddling 
with CGContextSetInterpolationQuality to no avail.  Almost there.

It appears that NSImage does not scale the backing image, but only the 
display one.  Tricks involving drawing onto a new context are not 
thread-safe.  Thus, I cannot use that.


The only solution that's coming to mind is one I really dread:

* Get the JPEG data with a CGImageSourceRef
* Store the metadata
* Make a CIImage (from the original file or the CGImage)
* Do the transform that works
* Make the CIImage a CGImage without using NSBitmapImageRep (Can I 
even do this? I haven't found this path.)
* Copy the metadata to a CGImageDestinationRef item along with the 
CGImage
* Generate the JPEG data object using CGImageDestinationCreateWithData

This seems overly complicated just to scale an image properly. 
Everything appears highly optimized for drawing on-screen rather than 
to a data structure in a threaded app.

Can anyone tell me how I can easily scale an image with a good/
excellent quality in a thread-safe manner in Mac OS X?


Adam Knight
Co-Founder; CTO
Barton Springs Software

Related mailsAuthorDate
mlImage scaling Adam Knight Jan 4, 23:25
mlRe: Image scaling Adam Knight Jan 5, 00:45
mlRe: Image scaling John Stiles Jan 5, 00:51
mlRe: Image scaling Chris Williams Jan 5, 01:19
mlRe: Image scaling Scott Ribe Jan 5, 01:30
mlRe: Image scaling Scott Ribe Jan 5, 01:34
mlRe: Image scaling Shawn Erickson Jan 5, 01:56
mlRe: Image scaling Adam Knight Jan 7, 18:41