如何在运行时过滤颜色? - 苹果手机

时间:2011-05-03 14:59:59

标签: iphone opengl-es filter textures

我有一个图像,其中每个像素都有三种基色之一(RGBA)。 我必须通过改变另一个颜色通道(运行时过滤器)在运行时更改此图像 我有.PVR和相应的glTexture2D,但是如何在运行时过滤/更改颜色? 我不能使用OpenGL ES 2.0 但我可以使用Cocos2D和OpenGL ES 1.x. :(

2 个答案:

答案 0 :(得分:0)

这不是微不足道的,但它可能对你有用:

// Convierte la imagen a blanco y negro
+ (UIImage *)convertImageToGrayScale:(UIImage *)i {

    CGSize size = [i size];
    int width = size.width;
    int height = size.height;

    // the pixels will be painted to this array
    uint32_t *pixels = (uint32_t *) malloc(width * height * sizeof(uint32_t));

    // clear the pixels so any transparency is preserved
    memset(pixels, 0, width * height * sizeof(uint32_t));

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    // create a context with RGBA pixels
    CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace, 
                                                 kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);

    // paint the bitmap to our context which will fill in the pixels array
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), [i CGImage]);

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

            // convert to grayscale using recommended method: http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale
            uint32_t gray = 0.3 * rgbaPixel[RED] + 0.59 * rgbaPixel[GREEN] + 0.11 * rgbaPixel[BLUE];

            // set the pixels to gray
            rgbaPixel[RED] = gray;
            rgbaPixel[GREEN] = gray;
            rgbaPixel[BLUE] = gray;
        }
    }

    // create a new CGImageRef from our context with the modified pixels
    CGImageRef image = CGBitmapContextCreateImage(context);

    // we're done with the context, color space, and pixels
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    free(pixels);

    // make a new UIImage to return
    UIImage *resultUIImage = [UIImage imageWithCGImage:image];

    // we're done with image now too
    CGImageRelease(image);

    return resultUIImage;
}

这会将收到的UIimage转换为灰度图像

答案 1 :(得分:0)

这是我用来操作RGBA图片的代码

-(UIImage*)convertGrayScaleImageRedImage:(CGImageRef)inImage{

CFDataRef m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage));  
UInt8 * m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef);  

int length = CFDataGetLength(m_DataRef);

for (int i=0; i<length; i+=4){

    int r = i;
    int g = i+1;
    int b = i+2;
    //int a = i+3;//alpha

    NSLog(@"r=%i, g=%i, b=%i",m_PixelBuf[r], m_PixelBuf[g], m_PixelBuf[b]);



    m_PixelBuf[r] = 1;
    m_PixelBuf[g] = 0;  
    m_PixelBuf[b] = 0;  
    //m_PixelBuf[a] = m_PixelBuf[a];//alpha
}  

CGContextRef ctx = CGBitmapContextCreate(m_PixelBuf,  
                                         CGImageGetWidth(inImage),  
                                         CGImageGetHeight(inImage),  
                                         CGImageGetBitsPerComponent(inImage),
                                         CGImageGetBytesPerRow(inImage),  
                                         CGImageGetColorSpace(inImage),  
                                         CGImageGetBitmapInfo(inImage) 
                                         ); 

CGImageRef imageRef = CGBitmapContextCreateImage(ctx);  
CGContextRelease(ctx);
UIImage *redImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

return redImage;
}