• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
How to create an indexed color PNG file
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: How to create an indexed color PNG file
      • From: Scott Thompson <email@hidden>
  • Prev by Date: Re: Multiple NSSortDescriptors on NSTableColumn header click
  • Next by Date: Re: Bindings do not work in threads?
  • Previous by thread: Re: Window Controllers Not Linking
  • Next by thread: Re: How to create an indexed color PNG file
  • Index(es):
    • Date
    • Thread