• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Keeping grayscale image grayscale
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Keeping grayscale image grayscale


  • Subject: Re: Keeping grayscale image grayscale
  • From: Heinrich Giesen <email@hidden>
  • Date: Wed, 12 Oct 2011 17:42:03 +0200

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 (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

  • Follow-Ups:
    • Re: Keeping grayscale image grayscale
      • From: Jonathan Taylor <email@hidden>
    • Re: Keeping grayscale image grayscale
      • From: Ken Ferry <email@hidden>
  • Prev by Date: Re: AVFoundation and kYUVSPixelFormat from AVPlayer
  • Next by Date: Re: AVFoundation and kYUVSPixelFormat from AVPlayer
  • Previous by thread: Re: Keeping grayscale image grayscale
  • Next by thread: Re: Keeping grayscale image grayscale
  • Index(es):
    • Date
    • Thread