Image scaling
Image scaling
- Subject: Image scaling
- From: Adam Knight <email@hidden>
- Date: Fri, 4 Jan 2008 16:25:53 -0600
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
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden