Getting a real bitmap of a compressed JPEG file
Getting a real bitmap of a compressed JPEG file
- Subject: Getting a real bitmap of a compressed JPEG file
- From: Ken Tozier <email@hidden>
- Date: Sat, 27 Mar 2004 17:11:01 -0500
I'm trying to write a general purpose image editing class for things
like resizing, rgb to grayscale conversion etc and am completely
stumped on how to get access to the bitmap data in such a way that I
can write to it without destroying it's "jpeg-ness".
The solution I came up with is rediculously wasteful , generating
several interim image representations/mutable data objects with a side
trip into the Quartz api's (to get the alpha type). Isn't there a way
to read a jpeg file in so that it's bitmap data contains the correct
number of uncompressed pixels, bitsPerPixel, bytesPerRow etc?
Here's the hideous thing I came up with...
// read the file into an NSData
NSData *tempData = [NSData dataWithContentsOfURL: inURL];
// now call the Quartz api's to get the pixel/alpha structure
// NOTE: Couldn't find any NSImage methods that reveal whether pixels
are
// structured ARGB or RGBA thus the need for the Quartz calls
CGDataProvider dp = CGDataProviderCreateWithData (NULL, [tempData
bytes], [tempData length], NULL);
CGImageRef ir = CGImageCreateWithJPEGDataProvider (dp, NULL, YES,
kCGRenderingIntentPerceptual);
// get properties that myClass is interested in
mAlphaInfo = CGImageGetAlphaInfo(ir);
mBitsPerComponent = CGImageGetBitsPerComponent(ir);
mBitsPerPixel = CGImageGetBitsPerPixel(ir);
mBytesPerRow = CGImageGetBytesPerRow(ir);
mHeight = CGImageGetHeight(ir);
mWidth = CGImageGetWidth(ir);
// create an image rep of the data
NSBitmapImageRep *tempImage = [NSBitmapImageRep imageRepWithData:
tempData];
// get an uncompressed representation of the image
NSDictionary *props = [NSDictionary dictionaryWithObject:[NSNumber
numberWithFloat:0.0]
forKey:NSImageCompressionFactor];
tempData = ;[tempImage representationUsingType:NSJPEGFileType
properties:props];
mImageData = [[tempImage representationUsingType:NSJPEGFileType
properties:props] mutableCopy];
Butt ugly and even after all that, writing to [mImageData mutableBytes]
destroys the jpeg anyway
There has to be a better way...,
Ken
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.