Thanks Heinrich. Your code looks correct to me! See also <http://cocoadev.com/index.pl?NSBitmapImageRep>. I wrote most of that.
-Ken Cocoa Frameworks On Wed, Oct 12, 2011 at 8:42 AM, Heinrich Giesen < heinrich.gie...@t-online.de> wrote: > > Jonathan Taylor wrote: > > I'm working with 16-bit grayscale images, and for the most part I'm just > manipulating bits within NSBitmapImageRep objects. However for convenience > it would be nice to do some stuff with NSImages, particularly when rescaling > and suchlike. The problem is that whenever I draw into such an NSImage it > gets converted to 3x8 bit RGB. Is there a simple way I can force things to > stay grayscale, or am I fighting a losing battle here. > > You are not fighting a losing battle, you use the wrong methods to create > an NSBitmapImageRep with properties you want. > If you want to create an NSImage/NSImageRep with special properties: there > is a rule of thumb: don't use lockFocus. > --> Re: bad behavior from unlockFocus on 10.6 by Ken Ferry. Another rule of thumb is: if you need -[NSImage TIFFRepresentation] you do > something wrong. > (There are of course counterexamples). > > The following code does (I hope so) what you want: > > > NSImage *srcImage = [[NSImage alloc] initWithContentsOfFile:thePath]; > > // create a new empty NSBitmapImageRep > NSBitmapImageRep *newRep = [[NSBitmapImageRep alloc] > initWithBitmapDataPlanes:NULL > pixelsWide: (NSInteger)newWidth > pixelsHigh: (NSInteger)newHeight > bitsPerSample: 16 > samplesPerPixel: 1 > hasAlpha: NO // YES is not supported > isPlanar: NO > colorSpaceName: NSCalibratedWhiteColorSpace > bytesPerRow: 0 > bitsPerPixel: 0]; > > //Now save the current NSGraphicsContext and set a new one > [NSGraphicsContext saveGraphicsState]; > NSGraphicsContext *newContext = [NSGraphicsContext > graphicsContextWithBitmapImageRep: newRep]; > [NSGraphicsContext setCurrentContext: newContext]; > > [srcImage drawInRect:NSMakeRect( 0, 0, [newRep pixelsWide], [newRep > pixelsHigh] ) > fromRect:NSZeroRect > operation:NSCompositeCopy > fraction:1.0]; > > // Restore the previous graphics context and state. > [NSGraphicsContext restoreGraphicsState]; > > and see what the newRep looks like: > NSLog( @"newRep is:%@", newRep ); > > > This code creates a 16 bit grayscale image whatever the source image is. > > Good luck > Heinrich > > > > > > _______________________________________________ > > Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) > > 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/kenferry%40gmail.com > > This email sent to kenfe...@gmail.com > _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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/archive%40mail-archive.com This email sent to arch...@mail-archive.com