Crash in NSBitmapImageRep representationUsingType: NSPNGFileType for 8-bit grayscale
Crash in NSBitmapImageRep representationUsingType: NSPNGFileType for 8-bit grayscale
- Subject: Crash in NSBitmapImageRep representationUsingType: NSPNGFileType for 8-bit grayscale
- From: John Horigan <email@hidden>
- Date: Mon, 02 Apr 2012 00:07:27 -0700
I have code that produces a PNG file from a cropped version of a bitmap that can be 8-bit grayscale, 16-bit grayscale, 24-bit color, or 32-bit color+alpha. It basically looks like this:
- (NSData *) getPNGCropped: (NSRect)cropRect
{
unsigned char* planes[5];
int offset = (int)cropRect.origin.y * _bytesPerRow +
(int)cropRect.origin.x * (_bitsPerPixel >> 3);
for (unsigned int i = 0; i < 5; ++i)
planes[i] = _imagePlanes[i] ? (_imagePlanes[i] + offset) : NULL;
int width = (int)cropRect.size.width;
int height = (int)cropRect.size.height;
NSBitmapImageRep* bits =
[[[NSBitmapImageRep alloc]
initWithBitmapDataPlanes: planes
pixelsWide: width
pixelsHigh: height
bitsPerSample: _bitsPerSample
samplesPerPixel: _samplesPerPixel
hasAlpha: _hasAlpha
isPlanar: _isPlanar
colorSpaceName: _colorSpace
bytesPerRow: _bytesPerRow
bitsPerPixel: _bitsPerPixel
] autorelease];
return [bits representationUsingType: NSPNGFileType properties: nil];
}
_planes[0] points to the mutableBytes of an NSMutableData and _planes[1 .. 4] point to nil
The NSMutableData contains a bitmap that my code is continuously modifying. Periodically the code makes an NSBitmapImageRep from this bitmap and displays it or creates a PNG file. There is a companion method that creates an uncropped NSBitmapImageRep, this method never crashes.
This cropping method crashes with SIGABRT in the representationUsingType method call if the source bitmap is 8-bit grayscale and planes[0] is not aligned on an 8-byte boundary. For 16-bit grayscale or 24- or 32- bit color there does not seem to be any alignment requirement. If the bitmap is 8-bit grayscale and planes[0] is on an 8-byte boundary then it works fine.
This seems to be a specific problem with creating a PNG file. If I take the same NSBitmapImageRep and drop it into a NSImage I can draw with it just fine. If I change the file type to NSJPEGFileType it does not crash.
-- john
_______________________________________________
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