2

大家好,我在这段代码中得到 0 的 RGB 值。知道为什么吗?inImage 是用这个 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;

}

改变像素颜色的代码(或者应该改变)

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

}

4

1 回答 1

1

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.

于 2010-11-25T21:24:33.577 回答