Copying between NSBitmapImageRep objects
As someone who's fairly well versed in the ways of Classic, I'm still trying to wrap my head around Cocoa and Objective-C. Anyway, I was reading up on NSBitmapImageRep and I think I finally found a class where I can manipulate image data on a per-pixel basis. Now I wonder... is there a way to copy the image from an NSImage, NSImageRep, or another NSBitmapImageRep to my NSBitmapImageRep ala CopyBits?
I may get hit with a lot of "just use OpenGL" responses but I'm trying to iron out the basics before I dive into that. My project is a 2D tile-based game so OpenGL seems like trying to kill a fly with a sledgehammer. Thanks in advance! Apologies if this is a dumb question
I may get hit with a lot of "just use OpenGL" responses but I'm trying to iron out the basics before I dive into that. My project is a 2D tile-based game so OpenGL seems like trying to kill a fly with a sledgehammer. Thanks in advance! Apologies if this is a dumb question
If you really want to copy from the image memory from one to another, you might as well write the blitter yourself.
If you just want to draw an NSImage into another NSImage, you can call lockFocus and unlockFocus to draw into said image.
If you just want to draw an NSImage into another NSImage, you can call lockFocus and unlockFocus to draw into said image.
Virus Out
Big Bang Board Games
Adventures on Pirate Isle
Solace - Strategy, War, and Glory!
The Belt
Here's a snippet of code that might not do precisely what you're looking for, but should get you pointed in the right direction:
Code:
NSBitmapImageRep * NSImageRepToNSBitmapImageRepCopy(NSImageRep * imageRep, int targetWidth, int targetHeight) {
NSBitmapImageRep * bitmap;
NSGraphicsContext * context;
bitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
pixelsWide: targetWidth
pixelsHigh: targetHeight
bitsPerSample: 8
samplesPerPixel: 4
hasAlpha: YES
isPlanar: NO
colorSpaceName: NSDeviceRGBColorSpace
bitmapFormat: NSAlphaNonpremultipliedBitmapFormat
bytesPerRow: (targetWidth * 4)
bitsPerPixel: 32];
context = [NSGraphicsContext graphicsContextWithBitmapImageRep: bitmap];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext: context];
[imageRep drawInRect: NSMakeRect(0, 0, targetWidth, targetHeight)];
[NSGraphicsContext restoreGraphicsState];
return bitmap;
}
That looks like just the thing I need. I never would have known to dig into the NSGraphicsContext class for this. Thanks! I'll try it out when I get home from work.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Strange OpenGl or NSBitmapImageRep error | magnusrw | 4 | 2,832 |
Apr 24, 2009 11:13 AM Last Post: magnusrw |
|
| NSBitmapImageRep/CIImage questions | Caveman | 6 | 5,130 |
Sep 6, 2008 04:37 PM Last Post: ThemsAllTook |
|
| NSBitmapImageRep ICC profile stripping? | kelvin | 0 | 1,868 |
Mar 9, 2006 05:42 PM Last Post: kelvin |
|
| How to get resolution of an NSImage or NSBitmapImageRep? | aegidian | 4 | 4,725 |
Oct 18, 2005 02:39 AM Last Post: aegidian |
|
| "Inconsistent set of values to create NSBitmapImageRep" Help! | Joseph Duchesne | 2 | 5,039 |
Sep 30, 2005 05:32 PM Last Post: Joseph Duchesne |
|

