Baffling NSImage crash
Baffling NSImage crash
- Subject: Baffling NSImage crash
- From: Shamyl Zakariya <email@hidden>
- Date: Tue, 26 Jun 2007 12:34:20 -0400
I'm writing an app to generate normal maps from greyscale bump maps.
Right now, I'm just bootstrapping, and have written a test ( a
command-line app ) which opens the bump map, creates an RGB buffer of
the same dimensions, swizzles a few pixels, and then writes a PNG to
disk. I just want to make certain I'm getting my bumpmap data and
writing files correctly, before I worry about normal map calculation.
Presumably, once the test app works, I'll be writing a GUI.
Now, before I go any further: I've seen threads on this list about
using NSImage from a command-line app, so I made certain to call
NSApplicationLoad(). But it doesn't help me -- this is a different
crash.
#0 0x903a5d4f in CGSConvertBGR888toRGBA8888
#1 0x9033dc31 in argb32_image
#2 0x9430c855 in ripd_Mark
#3 0x9430fbb7 in ripl_BltImage
#4 0x9430f5f5 in ripc_RenderImage
#5 0x9430d48d in ripc_DrawImage
#6 0x90336de1 in CGContextDrawImage
#7 0x9338b04a in DrawImageRefInRect
#8 0x9338af04 in _NSDrawBitmapWithFlip2
#9 0x9338adb4 in -[NSBitmapImageRep draw]
#10 0x933b3f2f in -[NSImageRep drawAtPoint:]
#11 0x9338aaa9 in -[NSImage drawRepresentation:inRect:]
#12 0x9338a846 in -[NSImage _drawRepresentation:]
#13 0x932d9a10 in -[NSImage _cacheRepresentation:stayFocused:]
#14 0x932d989d in -[NSImage _lockFocusOnRep:]
#15 0x932d3d68 in -[NSImage lockFocus]
#16 0x00002671 in -[NSImage(Additions)
saveToFile:withFormat:andProperties:] at NSImage-Additions.m:16
#17 0x000027ef in -[NSImage(Additions) saveAsPNGToFile:] at NSImage-
Additions.m:28
#18 0x0000390c in main at bump2normal.m:28
What I've discovered, poking around, is that if I set even a single
byte of the image data to anything other than zero, I get a crash on
save. That being said, I can memset the buffer to zero and I get the
expected black image output. I know my dimensions are valid, and at
least I'm pretty certain I'm not memory smashing ( in my code ). I'm
just making a buffer and handing it to NSBitmapImageRep, and then
wrapping that in an NSImage.
Here's how I create the image
Note:
1) _normalmapData is a member of the class NormalMappr and is of type
unsigned char *
2) _outputSize is a struct like NSSize but using integer width/height.
_normalmapData = malloc( _outputSize.height * _outputSize.width * 3 *
sizeof( unsigned char ));
// If I set even a single byte of _normalmapData to anything other
// than zero, I get the crash on [NSImage lockFocus]
// This should just give me a red pixel, top-left
_normalmapData[0] = 255;
// create an RGB bitmap rep and wrap it in an NSImage
NSBitmapImageRep *nmRep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes: (void*)_normalmapData
pixelsWide: (float)_outputSize.width
pixelsHigh: (float)_outputSize.height
bitsPerSample: 8
samplesPerPixel: 3
hasAlpha: NO
isPlanar: NO
colorSpaceName: NSDeviceRGBColorSpace
bitmapFormat: 0
bytesPerRow: _outputSize.width * 3
bitsPerPixel: 24 ];
[_normalmapDisplayImage release];
_normalmapDisplayImage = [[NSImage alloc] initWithSize: NSMakeSize
( _outputSize.width, _outputSize.height )];
[_normalmapDisplayImage addRepresentation: nmRep];
The saving to disk is done by a category on NSImage ( that I've used
in other projects successfully )
- (void) saveToFile: (NSString *) file withFormat:
( NSBitmapImageFileType ) format andProperties: (NSDictionary *) props
{
// BOOM!
[self lockFocus];
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc]
initWithFocusedViewRect: NSMakeRect( 0,0, [self size].width, [self
size].height )];
[self unlockFocus];
NSData *data = [rep representationUsingType: format properties: props];
[data writeToFile: file atomically: NO];
}
- (void) saveAsPNGToFile: (NSString *) file
{
[self saveToFile: file withFormat: NSPNGFileType andProperties: nil];
}
My main() function looks like so -- it crashes on saveAsPNGToFile:
int main( int agc, const char **argv )
{
NSApplicationLoad(); // since this is a command-line app
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *BumpMapFile = @"../../TestBump.png";
NSString *OutputFile = @"../../TestNormalMap.png";
NormalMappr *nmg = [[NormalMappr alloc] initWithBumpmap:
BumpMapFile ];
if ( nmg )
{
NSImage *normalmap = [nmg normalmap];
if ( normalmap )
{
// BOOM!
[normalmap saveAsPNGToFile: OutputFile ];
[[NSWorkspace sharedWorkspace] openFile: OutputFile];
}
}
else
{
NSLog( @"Unable to open %@", BumpMapFile );
}
[nmg release];
[pool release];
return 0;
}
Shamyl Zakariya
- hobo-raconteur
_______________________________________________
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