• 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: Ken Ferry <email@hidden>
  • Date: Wed, 12 Oct 2011 21:58:37 -0700

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 <
email@hidden> 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 (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
>
_______________________________________________

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

References: 
 >Re: Keeping grayscale image grayscale (From: Heinrich Giesen <email@hidden>)

  • Prev by Date: [Moderator] Re: Can we talk iOS5 yet?
  • Next by Date: Simple PDF generation code not working as intended
  • Previous by thread: Re: Keeping grayscale image grayscale
  • Next by thread: Re: Keeping grayscale image grayscale
  • Index(es):
    • Date
    • Thread