Re: Bulletproof way to create a new CGBitmapContext from an existing image?
Hi Steve and David, Thank you both for your comments; they were very helpful. I had thought that I had to match the context to the image I was going to use, but that's clearly wrong. I had missed that link to the supported pixel formats - that's very informative. Steve's comment also suggested to me (if this will help) another trick. I created the context with the *cropped* image size. When I draw the image in the context, I specify a rect positioned such that the context region contains the bit I want cropped, i.e. specify a rect that is larger than the bounds of the context. CG scales it as appropriate, but I only get the bit that I want. Again, thanks very much for the help! Cheers, Demitri ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Bulletproof way to create a new CGBitmapContext from an existing image?
On Nov 1, 2009, at 8:45 AM, thatsanicehatyouh...@mac.com wrote: I'm trying to write code to avoid messages such as this: Error: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 16 bits/pixel; 1-component colorspace; kCGImageAlphaPremultipliedFirst; 352 bytes/row. I only rarely get these now, but I want to make that never. I'm pasting my code below that I have so far - can anyone suggest something better? (I'm working on an iPhone if it makes any difference.) Do you need to match the source image so closely? In most situations it is perfectly acceptable to just flatten all images down to a single canonical format for your application rather than trying to base the final image format on the source image format. About the only thing I may consider doing is differentiating Grayscale/RGB/CMYK source images, but I would want specific reasons for going beyond that. -- David Duncan Apple DTS Animation and Printing ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Bulletproof way to create a new CGBitmapContext from an existing image?
Why not alway create a CGBitmapContext of the desired size and in a supported pixel format (see http://developer.apple.com/mac/library/qa/qa2001/qa1037.html ), then call CGContextDrawImage to draw the CGImage into the context? Then you're always controlling the parameters. CGRect bounds = CGRectMake(0, 0, scaleFactorX * image.size.width, scaleFactorY * image.size.height); context = CGBitmapContextCreate(NULL, bounds.size.width, bounds.size.height, 8, 4 * bounds.size.width, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedFirst); CGContextDrawImage(context, bounds, image.CGImage); On Nov 1, 2009, at 8:45 AM, thatsanicehatyouh...@mac.com wrote: I have some code where I'm downloading images from the internet. They're probably all going to be jpegs, but I'm not making that assumption in my code. I have no control over the images. I'm creating a CGBitmapContext to manipulate them. Further, I'm scaling the image (could be up or down depending on the original size) by creating the context with the size I want and drawing the original image into that. I'm trying to write code to avoid messages such as this: Error: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 16 bits/pixel; 1-component colorspace; kCGImageAlphaPremultipliedFirst; 352 bytes/row. I only rarely get these now, but I want to make that never. I'm pasting my code below that I have so far - can anyone suggest something better? (I'm working on an iPhone if it makes any difference.) Cheers, Demitri size_t bitsPerComponent = CGImageGetBitsPerComponent(image.CGImage); CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage); // The value returned by CGImageGetBytesPerRow might be smaller than the final // image if we are scaling to a larger size. float contextWidth = (float)ceil((double)scaleX*image.size.width) * 4.0; int bytesPerRow = (CGImageGetBytesPerRow(image.CGImage) contextWidth) ? CGImageGetBytesPerRow(image.CGImage) : contextWidth; context = CGBitmapContextCreate(NULL, // let the OS manage the memory for me // image dimensions (desired width, desired height) scaleFactorX*image.size.width, scaleFactorY*image.size.height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedFirst); ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Bulletproof way to create a new CGBitmapContext from an existing image?
Hi, I have some code where I'm downloading images from the internet. They're probably all going to be jpegs, but I'm not making that assumption in my code. I have no control over the images. I'm creating a CGBitmapContext to manipulate them. Further, I'm scaling the image (could be up or down depending on the original size) by creating the context with the size I want and drawing the original image into that. I'm trying to write code to avoid messages such as this: Error: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 16 bits/pixel; 1-component colorspace; kCGImageAlphaPremultipliedFirst; 352 bytes/row. I only rarely get these now, but I want to make that never. I'm pasting my code below that I have so far - can anyone suggest something better? (I'm working on an iPhone if it makes any difference.) Cheers, Demitri size_t bitsPerComponent = CGImageGetBitsPerComponent(image.CGImage); CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage); // The value returned by CGImageGetBytesPerRow might be smaller than the final // image if we are scaling to a larger size. float contextWidth = (float)ceil((double)scaleX*image.size.width) * 4.0; int bytesPerRow = (CGImageGetBytesPerRow(image.CGImage) contextWidth) ? CGImageGetBytesPerRow(image.CGImage) : contextWidth; context = CGBitmapContextCreate(NULL, // let the OS manage the memory for me // image dimensions (desired width, desired height) scaleFactorX*image.size.width, scaleFactorY*image.size.height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedFirst); ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com