Re: How to render 8-bit bitmap image representation?
Re: How to render 8-bit bitmap image representation?
- Subject: Re: How to render 8-bit bitmap image representation?
- From: "Ken Ferry" <email@hidden>
- Date: Mon, 25 Jun 2007 14:42:57 -0700
Instead of lockFocus on the image, which does have the behavior you
describe (think of it as drawing the image to a view and then drawing
on the view), you can draw directly to the bitmap image rep.
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:[NSGraphicsContext
graphicsContextWithBitmapImageRep:bitmapImageRep]];
// draw
[NSGraphicsContext restoreGraphicsState];
Ken Ferry
Cocoa Frameworks
On 6/24/07, Tron Thomas <email@hidden> wrote:
I created an 8-bit grayscale bitmap image using code like the following:
NSSize size = { 128.0f, 128.0f };
image = [[NSImage alloc] initWithSize:size];
NSBitmapImageRep* bitmap =
[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
pixelsWide:size.width pixelsHigh:size.height
bitsPerSample:8
samplesPerPixel:1 hasAlpha:NO isPlanar:NO
colorSpaceName:NSCalibratedWhiteColorSpace bytesPerRow:0
bitsPerPixel:8];
[image addRepresentation:bitmap];
Later on I did something like:
[image lockFocus];
// Do some drawing…
[image unlockFocus];
When the rendering to the image was done, I then wanted to access the
bitmap data. So, I tried doing something such as:
NSBitmapImageRep* bitmap =
[[image representations] objectAtIndex:0];
unsigned char* data = [bitmap bitmapData];
This didn't work because, it turns out that locking the image for
rendering causes the image to throw away its current representation
and create an NSCachedImageRep instead. NSCachedImageRep doesn't
support calling bitmapData to access the bits in the image.
I found I could get a bitmap representation of the image by doing:
NSBitmapImageRep* bitmap =
[NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]];
However, when I do this the image representation is 24-bit instead of
the 8-bit format I wanted originally.
How can I get a rendering for a bitmap image representation that has
the 8-bit pixel format I want?
_______________________________________________
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
_______________________________________________
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