1

As a special case of this question:

What do you do if the format of your UIImage is unsupported? In my case, I have a monochrome UIImage object that I read in from a file. Each pixel has 8 bits of color(white) data followed by 8 bits of alpha data. The format is unsupported, but the image displays just fine on the iPhone screen.

But getting pixel data as described in the referenced question won't work as the format is unsupported. So what can I do?

Community
  • 1
  • 1
William Jockusch
  • 26,513
  • 49
  • 182
  • 323

2 Answers2

0

You have to render into a known/supported pixels format. Just setup a bitmap render target that is either grayscale or 24BPP and then draw the image into your pixel buffer at a 1:1 aspect ratio, so there is no resize. Then inspect the known format results as bytes or word sized pixels. You only need to worry about sRGB colorspace on iOS, so that keeps things simple and you can simply read the pixel values directly from the pixel buffer.

MoDJ
  • 4,309
  • 2
  • 30
  • 65
0

Will something like this work, essentially converting an image to a monochrome bitmap to manipulate the pixel data.

   CGImageRef cgImage = [image CGImage];
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapContext = CGBitmapContextCreate(NULL, CGImageGetWidth(cgImage), CGImageGetHeight(cgImage), 8, CGImageGetWidth(cgImage)*2, colorSpace, kCGImageAlphaLast);
    CGColorSpaceRelease(colorSpace);
    CGContextDrawImage(bitmapContext, CGRectMake(0, 0, CGBitmapContextGetWidth(bitmapContext), CGBitmapContextGetHeight(bitmapContext)), cgImage);

    UInt8 *data = CGBitmapContextGetData(bitmapContext);
    int numComponents = 2;
    int bytesInContext = CGBitmapContextGetHeight(bitmapContext) * CGBitmapContextGetBytesPerRow(bitmapContext);

    int white;
    int alpha;


    for (int i=0; i < bytesInContext; i+= numComponents) {

        white = data[i];
        alpha = data[i+1];
    }
chilitechno.com
  • 1,575
  • 10
  • 12
  • bitmapContext comes back as nil as it is unsupported. I suppose I could try lying to it and claiming I have RGB with 16 bits per pixel, 5 bits per component, and no alpha, which is supported, and grabbing the data from there. Might work; sure wouldn't by satisfying. – William Jockusch May 08 '11 at 19:55