1

I'm working in Quartz/Core-graphics. I'm trying to create a black and white, 1b per pixel graphics context.

I currently have a CGImageRef with a grayscale image (which is really black and white). I want to draw it into a black and white BitmapContext so I can get the bitmap out and compress it with CCITT-group 4. (For some reason Quartz won't let you save in any TIFF format other than LZW).

So, I need the 1bit per pixel data. I figure that drawing into a 1bpp context would do that. However, it won't let me create the context with:

    context = CGBitmapContextCreate (data,
                pixelsWide,
                pixelsHigh,
                1,
                pixelsWide/8,
                CGColorSpaceCreateDeviceGray(),
                kCGImageAlphaNone
                                 );

Is there a colorspace smaller than gray?

jjnguy
  • 136,852
  • 53
  • 295
  • 323
Brian Postow
  • 11,709
  • 17
  • 81
  • 125

1 Answers1

2

Even if 1-bit bitmaps were supported, if pixelsWide is not a multiple of 8, then the number of bytes per row is not an integer: for example, if your image is 12 pixels wide, then the number of bytes per row is one and a half. Your division expression will truncate this to one byte per row, which is wrong.

But that's if 1-bit bitmaps were supported, which they aren't.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • Well, in reality I do (pixelsWide +7)/8 to avoid that very problem. Integer arithmetic and all... However, unfortunate link fail. Can you edit? – Brian Postow Apr 16 '09 at 21:20
  • I think you wanted to link to http://developer.apple.com/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_context/dq_context.html#//apple_ref/doc/uid/TP30001066-CH203-BCIBHHBB – Brian Postow Apr 16 '09 at 21:28