开发者

Getting 0 values for RGB when trying to change pixels in an CGImageRef data

Hi guys I am getting 0 for RGB values in this code. Any idea why? The inImage was created with this CGContext.

-(CGContextRef)createBitmapContextWide:(int) pixelsWide Height:(int) pixelsHigh{

CGContextRef    context = NULL;
CGColorSpaceRef colorSpace;
void *          bitmapData;
int             bitmapByteCount;
int             bitmapBytesPerRow;

bitmapBytesPerRow   = (pixelsWide * 4);
bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);

colorSpace = CGColorSpaceCreateDeviceRGB();
bitmapData = malloc( bitmapByteCount );
if (bitmapData == NULL)
{
    fprintf (stderr, "Memory not allocated!");
    return NULL;
}
context = CGBitmapContextCreate (bitmapData,
                                 pixelsWide,
                                 pixelsHigh,
                                 8,      // bits per component
                                 bitmapBytesPerRow,
                                 colorSpace,
                                 kCGImageAlphaPremultipliedLast);
if (context== NULL)
{
    free (bitmapData);
    fprintf (stderr, "Context not created!");
    return NULL;
}
CGColorSpaceRelease( colorSpace );

return context;
开发者_开发知识库

}

Code that changes pixel colors (or it should change)

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

}


Ok, so if you have problems with pixel manipultion on a bitmap you may want to take into considiration your bitmaps format (32bit ? 16bit?) ARGB or RGBA or CMYK

Tip: If you want to check what format is your bitmap in try putting 1 for first byte, 0, for the second byte, and 0 for the third byte.

m_PixelBuf[i] = 1;
m_PixelBuf[i+1] = 0;  
m_PixelBuf[i+2] = 0;  

This way if you get all pixels red you know your format is RGBA. Play with it and in few minutes you will figure it out. There is probably easier way but I am a noob :-)

In my case my image was RGBA, but all the pixels where stored in the alpha bit since the CGImageRef (inImage) was transparent with grayscale shadows.

Conclusion
Code is right but my knowledge of the input bitmap format was wrong.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜