如何为glReadPixels选择UIImage?

时间:2010-12-10 13:08:18

标签: iphone opengl-es uiimage

我有50张图片的视图。图像可能重叠。我想(例如)选择图像编号33并找到像素颜色。我怎样才能做到这一点 ? PS我使用glReadPixels。

1 个答案:

答案 0 :(得分:1)

除非您想要先消除将图像应用为纹理的时间,否则不会使用glReadPixels。您可以直接从UIImage执行此操作:

void pixelExamine( UIImage *image )
    {
    CGImageRef colorImage = image.CGImage;
    int width = CGImageGetWidth(colorImage);
    int height = CGImageGetHeight(colorImage);

    uint32_t *pixels = (uint32_t *) malloc(width * height * sizeof(uint32_t));

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), colorImage);

    int x;
    int y;
    uint8_t *rgbaPixel;

    for( y = 0; y < height; y++)
        {
        rgbaPixel = (uint8_t *) &pixels[y * width];

        for( x = 0; x < width; x++, rgbaPixel+=4)
            {
            //  rgbaPixel[0] = ALPHA 0..255
            //  rgbaPixel[3] = RED 0..255
            //  rgbaPixel[2] = GREEN  0..255
            //  rgbaPixel[1] = BLUE 0..255
            }
        }

    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    free(pixels);
    }