2011-05-03 62 views
0

我有一個圖像,其中每個像素有三種主要顏色(RGBA)之一。 我必須通過改變每個顏色通道的運行時間來改變這個圖像(運行時過濾器) 我有.PVR和相應的glTexture2D,但是如何在運行時過濾/改變顏色? 我不能使用OpenGL ES 2.0的 但我可以使用的Cocos2D和OpenGL ES 1.x的 :(如何在運行時過濾顏色? - iphone

回答

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收到成灰度圖像

+0

我有一個.PVR和UIImage的不讀取i中的代碼噸。這個解決方案的另一個問題是我不能在每個繪製週期中完成它,它太昂貴了。謝謝, :) – 2011-05-03 16:15:33

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; 
}