• 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
Rasterizing an NSString to a bitmap
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Rasterizing an NSString to a bitmap


  • Subject: Rasterizing an NSString to a bitmap
  • From: John Stiles <email@hidden>
  • Date: Mon, 7 Jun 2004 11:26:53 -0700

I need to draw an NSString into a block of my own memory so I can do effects on it. I just want one grayscale channel. I thought this would be easy but I am stumped! :|
Here's what I've tried to do.

1:
- Make an NSImage
- Make an NSBitmapImageRep with the parameters I want (grayscale 1byte per pixel)
- Add this bitmap rep to the NSImage
- Lock focus on the bitmap rep
- Draw the string
Unfortunately my malloc'ed block still contained just zeroes.

NSImage *image = [[[NSImage alloc] initWithSize:size] autorelease];
NSBitmapImageRep *bitmap = [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:&myMallocBlock
pixelsWide:size.width
pixelsHigh:size.height
bitsPerSample:8
samplesPerPixel:1
hasAlpha:NO
isPlanar:NO
colorSpaceName:NSCalibratedBlackColorSpace
bytesPerRow:size.width
bitsPerPixel:8] autorelease];

[image lockFocus];
[nsString drawAtPoint:NSMakePoint(0,0) withAttributes:attribs];
[image unlockFocus];

2:
- Make an NSImage
- Draw the string into the NSImage (lockFocus/drawAtPoint/unlockFocus)
- Convert the NSImage to an NSBitmapImageRep via TIFFRepresentation
- Make another NSBitmapImageRep with the parameters I want (1 byte per pixel, grayscale)
- Make another image with the second NSBitmapImageRep as its representation
- Draw the first NSBitmapImageRep into the second NSImage
This made things more convoluted, but my block of memory was still all zeroes!

// Make an image.
NSImage *funkyImage = [[[NSImage alloc] initWithSize:size] autorelease];
[funkyImage lockFocus];
[nsString drawAtPoint:NSMakePoint(0,0) withAttributes:attribs];
[funkyImage unlockFocus];

// Convert it to a bitmap using a TIFF representation.
NSBitmapImageRep *funkyBitmap = [[[NSBitmapImageRep alloc] initWithData:[funkyImage TIFFRepresentation]] autorelease];

// Copy this bitmap of unknown depth/planarity/etc into our alpha buffer.
NSBitmapImageRep *bitmap = [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:&myMallocBlock
pixelsWide:size.width
pixelsHigh:size.height
bitsPerSample:8
samplesPerPixel:1
hasAlpha:NO
isPlanar:NO
colorSpaceName:NSCalibratedBlackColorSpace
bytesPerRow:size.x
bitsPerPixel:8] autorelease];

NSImage *image = [[[NSImage alloc] initWithSize:size] autorelease];
[image addRepresentation:bitmap];
[image lockFocusOnRepresentation:bitmap];
[funkyBitmap drawAtPoint:NSMakePoint(0,0)];
[image unlockFocus];

I've tried changing the color space, setting planar to YES (with one plane, it doesn't matter which you choose), allowing NSBitmapImageRep to create the block of memory itself, and lots of other crazy things. I'm at my wit's end--what's wrong?

Thanks to all who contribute...
_______________________________________________
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.


  • Follow-Ups:
    • Re: Rasterizing an NSString to a bitmap
      • From: Steve Sims <email@hidden>
    • Re: Rasterizing an NSString to a bitmap
      • From: Marcel Weiher <email@hidden>
  • Prev by Date: Re: itunes style expanding text field
  • Next by Date: Re: NSDictionary design bug (was: Re: Ugly bug in Foundation, beware!)
  • Previous by thread: Re: splitting a text view
  • Next by thread: Re: Rasterizing an NSString to a bitmap
  • Index(es):
    • Date
    • Thread