Re: Create NSImage from array of integers
Re: Create NSImage from array of integers
- Subject: Re: Create NSImage from array of integers
- From: Troy Stephens <email@hidden>
- Date: Thu, 1 Nov 2007 13:01:42 -0700
On Nov 1, 2007, at 12:38 PM, Jason Horn wrote:
Thank you Troy for the head start - I'm new to Mac development. One
thing I don't understand: when you call [bitmap bitmapData], what
is the pointer that it returns pointing to? How do I fill it with
an array of ints?
The pointer returned from -bitmapData points to the raw pixel data
(a.k.a. sample data). This may be a buffer that the NSBitmapImageRep
allocated itself (if you passed NULL as the first argument to -
initWithBitmapDataPlanes:...), or the buffer you provided to -
initWithBitmapDataPlanes:...
The data is arranged as [bitmapImageRep pixelsHigh] rows of
[bitmapImageRep bytesPerRow] bytes each. Each row may contain padding
on the end for alignment (so bytesPerRow may be slightly greater than
pixelsWide * bytesPerPixel). The [bitmapImageRep bitmapFormat] flags
specify the order and interpretation of the sample values for each
pixel (position of alpha value, and alpha premultiplied or not, if
alpha is present, as well as float or integer for the sample values),
and each row contains data for [bitmapImageRep pixelsWide] pixels,
packed together left to right.
This is assuming a nonplanar bitmap by the way ([bitmapImageRep
isPlanar] == NO). When dealing with planar bitmap data, you'll need
to use -getBitmapDataPlanes: instead of -bitmapData to retrieve the
pointers to the separate planes.
In your case, you're dealing with 16-bit-per-sample grayscale values,
so you'd find (or store) the value for a pixel at a given (x,y)
position like so:
unsigned char *bitmapData = [bitmapImageRep bitmapData];
uint16_t *pixel = bitmapData + y * [bitmapImageRep bytesPerRow] +
2 * x; // (2 bytes per pixel)
*pixel = 65535; // set pixel to desired value
If your data is already prepared in a buffer of exactly the same
layout, you can simply do:
memcpy(bitmapData, yourBuffer, [bitmapImageRep bytesPerRow] *
[bitmapImageRep pixelsHigh]);
If the bitmapImageRep uses different row padding than your buffer,
however, (i.e. bytesPerRow is different from that for your source
buffer), you'll need to memcpy the rows one at a time, finding the
start of each destination row as:
unsigned char *bitmapData = [bitmapImageRep bitmapData];
uint16_t *destRow = bitmapData + y * [bitmapImageRep bytesPerRow];
Troy
_______________________________________________
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