views:

215

answers:

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?

A: 

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
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
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
That's what I *did* link to. Stack Overflow's Markdown parser butchered my link.
Peter Hosey