Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Create NSImage from array of integers



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?

Thanks,

- Jason



On Oct 31, 2007, at 7:29 PM, Troy Stephens wrote:

Yes, NSBitmapImageRep supports this pixel data format as well, so one could create an NSBitmapImageRep directly:

unsigned char *planes[1];
planes[0] = (unsigned char *)bitmapData;
NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:planes pixelsWide:pixelsWide pixelsHigh:pixelsHigh bitsPerSample:16 samplesPerPixel:1 hasAlpha:NO isPlanar:NO colorSpaceName:NSCalibratedWhiteColorSpace bitmapFormat: 0 bytesPerRow:0 bitsPerPixel:0];


Note that this creates an NSBitmapImageRep instance that references the "bitmapData" you've pointed it to, but has no way to insure that the data sticks around. So you must either insure that the bitmapData pointer remains valid for the lifetime of the NSBitmapImageRep (i.e. don't free or reuse the bitmapData buffer), or, if that's inconvenient, you can pass NULL for the "planes" parameter to the NSBItmapImageRep initializer to let the NSBitmapImageRep allocate its own buffer, then you can get a pointer to that buffer using the "-bitmapData" method and copy your pixel data into it.

In any case, if you then need to wrap the NSBitmapImageRep in an NSImage for handoff to other API, that's simple too:

          image = [[NSImage alloc] initWithSize:[bitmap size]];
          [image addRepresentation:bitmap];

Troy


-- Troy Stephens Cocoa Frameworks Apple, Inc.

On Oct 31, 2007, at 4:16 PM, David Spooner wrote:


Wouldn't a more direct approach be to create an NSBitmapImageRep from the bitmapData using -initWithBitmapDataPlanes:... and then use NSImage -initWithSize: and -addRepresentation:?


dave


On 31-Oct-07, at 4:14 PM, Bill Dudney wrote:

Hi Jason,

This is a job for Quartz i believe.

If you know the exact pixel layout and such I'd think you could create a bitmap context and then get the image from that;

void* bitmapData = load your array of ints;
CGContextRef context = CGBitmapContextCreate(bitmapData, size.width, size.height, bitsPerComponent, bytesPerRow, CGColorSpaceCreateWithName(kCGColorSpaceGenericGray), kCGImageAlphaNone);
image = CGBitmapContextCreateImage(context);


I think this will work. You might have to tweak some of the args to the context create to get it to draw properly.

Good Luck,

-bd-
http://bill.dudney.net/roller/objc

On Wednesday, October 31, 2007, at 03:13PM, "Jason Horn" <email@hidden > wrote:
I need some help creating an NSBitmapImageRep or an NSImage from a
proprietary file. I have figured out how to load the data from the
file into an array of integers. I'm not sure what to do from there.
The image is a greyscale image with 16 bit integers (unsigned int).
Separately, I have the dimensions of the image (640 x 480).


Any thoughts?

Thanks,

- Jason

_______________________________________________

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:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

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:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

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:
http://lists.apple.com/mailman/options/cocoa-dev/tstephens %40apple.com


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:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.