[SOLUTION] exporting bmp data from NSImage / NSBitmapImageRep
[SOLUTION] exporting bmp data from NSImage / NSBitmapImageRep
- Subject: [SOLUTION] exporting bmp data from NSImage / NSBitmapImageRep
- From: Florent Pillet <email@hidden>
- Date: Tue, 30 Sep 2003 17:33:19 +0200
After much browsing, it seems that NSBitmapImageRep won't properly
generate bmp data in some cases. To alleviate the problem, I wrote a
NSImage category selector which uses QuickTime to export the data to
bmp. It's a bit tedious since I have to go through an intermediate
format first (PNG works fine for that purpose).
Here is the code in the hope it helps others. Note that the
myCreateHandleDataRef function is the exact one listed in TN 1195,
therefore I didn't bother reproducing it below.
Florent.
#include <QuickTime/QuickTime.h>
@implementation NSImage (bmp)
- (NSData *)BMPData
{
NSRect r = [self rect];
[self lockFocus];
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc]
initWithFocusedViewRect:r];
[self unlockFocus];
NSData *bmpData = [rep representationUsingType:NSBMPFileType
properties:nil];
if (bmpData == nil)
{
// no joy. there's a bug in the OS which doesn't generate BMP data
(all
// other types are supported, though). Use QuickTime to generate the
bmp data.
NSData *pngData = [rep representationUsingType:NSPNGFileType
properties:nil];
if ([pngData length] == 0)
{
NSLog(@"Can't export bitmap data to BMP");
[rep release];
return nil;
}
// create a data reference handle for quicktime (see TN 1195 for
myCreateHandleDataRef source)
Handle pngDataH = NULL;
PtrToHand([pngData bytes], &pngDataH, [pngData length]);
Handle dataRef = myCreateHandleDataRef(pngDataH, "\pdummy.png",
kQTFileTypePNG, nil, nil, 0);
// create a Graphics Importer component that will read from the PNG
data
ComponentInstance importComponent=0, exportComponent=0;
OSErr err = GetGraphicsImporterForDataRef(dataRef,
HandleDataHandlerSubType, &importComponent);
DisposeHandle(dataRef);
if (err == noErr)
{
// create a Graphics Exporter component that will write BMP data
err = OpenADefaultComponent(GraphicsExporterComponentType,
kQTFileTypeBMP, &exportComponent);
if (err == noErr)
{
// set export parameters
Handle bmpDataH = NewHandle(0);
GraphicsExportSetInputGraphicsImporter(exportComponent,
importComponent);
GraphicsExportSetOutputHandle(exportComponent, bmpDataH);
// export data to BMP into handle
unsigned long actualSizeWritten = 0;
err = GraphicsExportDoExport(exportComponent, &actualSizeWritten);
if (err == noErr)
{
// export done: create the NSData that will be returned
HLock(bmpDataH);
bmpData = [NSData dataWithBytes:*bmpDataH
length:GetHandleSize(bmpDataH)];
HUnlock(bmpDataH);
}
DisposeHandle(bmpDataH);
CloseComponent(exportComponent);
}
CloseComponent(importComponent);
}
DisposeHandle(pngDataH);
}
[rep release];
return bmpData;
}
@end
--
Florent Pillet, Code Segment email@hidden
Developer tools and end-user products for Palm OS & Mac OS X
ICQ: 117292463
http://perso.wanadoo.fr/fpillet
_______________________________________________
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.