Jordan Medlock
Jordan Medlock

Reputation: 817

Image processing in Obj-C

I want to do some scientific image processing on iOS in Obj-C or of course C, all I require to do this is to get a 3D array of the bit values of all the pixels' RGBA. UIImage doesn't seem to have a built in function. Do any of you know how to get the pixel values or more preferably a predefined library with those functions in there? Thanks in advance, JEM

Upvotes: 1

Views: 329

Answers (1)

Tommy
Tommy

Reputation: 100652

You'd normally either create a CGBitmapContext, telling it to use some memory you'd allocated yourself (so, you know where it is and how to access it) or let Core Graphics figure that out for you and call CGBitmapContextGetData if you're targeting only iOS 4/OS X 10.6 and later, then draw whatever you want to inspect to it.

E.g. (error checking and a few setup steps deliberately omitted for brevity; look for variables I use despite not defining and check the function documentation)

CGBitmapInfo bitmapInfo =
              kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host;

context =
       CGBitmapContextCreate(
                NULL,
                width, height,
                8,
                width * 4,
                rgbColourSpace,
                bitmapInfo);

CGContextDrawImage(
        context,
        CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height),
        [image CGImage]);

uint8_t *pixelPointer = CGBitmapContextGetData(context);

for(size_t y = 0; y < height; y++)
{
    for(size_t x = 0u; x < width; x++)
    {
        if((bitmapInfo & kCGBitmapByteOrder32Little))
        {
            NSLog(@"rgba: %02x %02x %02x %02x",
                 pixelPointer[2], pixelPointer[1],
                 pixelPointer[0], pixelPointer[3]);
        }
        else
        {
            NSLog(@"rgba: %02x %02x %02x %02x",
                 pixelPointer[1], pixelPointer[2],
                 pixelPointer[3], pixelPointer[0]);
        }

        pixelPointer += 4;
    }
}

Upvotes: 1

Related Questions