Copying between NSBitmapImageRep objects

Posts: 12
Joined: 2007.02
Post: #1
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 Wacko
Quote this message in a reply
Posts: 204
Joined: 2002.09
Post: #2
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.
Quote this message in a reply
Posts: 1,563
Joined: 2003.10
Post: #3
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:
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;
Quote this message in a reply
Posts: 12
Joined: 2007.02
Post: #4
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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Strange OpenGl or NSBitmapImageRep error magnusrw 4 5,143 Apr 24, 2009 11:13 AM
Last Post: magnusrw
  NSBitmapImageRep/CIImage questions Caveman 6 9,523 Sep 6, 2008 04:37 PM
Last Post: ThemsAllTook
  NSBitmapImageRep ICC profile stripping? kelvin 0 2,999 Mar 9, 2006 05:42 PM
Last Post: kelvin
  How to get resolution of an NSImage or NSBitmapImageRep? aegidian 4 8,675 Oct 18, 2005 02:39 AM
Last Post: aegidian
  "Inconsistent set of values to create NSBitmapImageRep" Help! Joseph Duchesne 2 8,046 Sep 30, 2005 05:32 PM
Last Post: Joseph Duchesne