How to create an indexed color PNG file
How to create an indexed color PNG file
- Subject: How to create an indexed color PNG file
- From: Martin Simoneau <email@hidden>
- Date: Fri, 14 Oct 2005 19:16:44 -0400
Hi,
I'm new to Cocoa programming. I need to draw some text inside an
image and save it as an indexed color PNG file with only 2 colors.
I create and save the image with the following code:
- (NSImage *) mask2Saved {
NSRect destRect = NSMakeRect(0.0,0.0,[image size].width,[image
size].height-60);
NSRect currentRect = NSMakeRect(0.0,0.0,[image size].width,[image
size].height);
NSImage *destinationImage = [[NSImage alloc]
initWithSize:destRect.size];
[destinationImage lockFocus];
if ( menuText) {
NSFont *font = [NSFont fontWithName:@"Helvetica" size:36.0];
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
[attrs setObject:font forKey:NSFontAttributeName];
[attrs setObject:[NSColor yellowColor]
forKey:NSForegroundColorAttributeName];
NSMutableAttributedString *str = [[NSMutableAttributedString
alloc]initWithString:[self menuText] attributes:attrs];
NSImage *textImage = [[NSImage alloc] initWithSize:
currentRect.size];
[[NSColor greenColor] set];
[textImage lockFocus];
[str drawAtPoint:NSMakePoint(20, currentRect.size.height -
(56.0))];
[textImage unlockFocus];
// Now scale down the text.
[textImage drawInRect: destRect fromRect:currentRect
operation:NSCompositeSourceOver fraction:1.0];
}
[destinationImage unlockFocus];
return [destinationImage autorelease];
}
...
NSBitmapImageRep *myBitmapImageRep = [[NSBitmapImageRep alloc]
initWithData:[[theMenuView mask2Saved] TIFFRepresentation]];
[[myBitmapImageRep representationUsingType: NSPNGFileType
properties: nil] writeToFile: [NSString stringWithFormat:@"%@.%@",
[sheet filename], @"png"] atomically: YES];
...
The saved PNG file has the following characteristics:
Color Model: RGB
Depth: 8
Has Alpha: 1
Pixel Height: 480
Pixel Width: 720
Profile Name: 787LE
Is there a way to force an image to use an indexed color model and
used a special color table? Can you suggest me some approaches to do it.
Thanks in advance for your help!
Martin
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden