c++cmacoscore-graphicscgbitmapcontext

Black and White graphics context


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?


Solution

  • 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.