尝试更改 CGImageRef 数据中的像素时获取 RGB 0 值

发布于 2024-10-04 07:52:27 字数 2481 浏览 4 评论 0原文

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

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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空心空情空意 2024-10-11 07:52:27

好的,如果您在位图上的像素操作方面遇到问题,您可能需要考虑您的位图格式(32 位?16 位?)ARGB 或 RGBA 或 CMYK

提示:如果您想检查位图的格式,请尝试输入 1第一个字节为 0,第二个字节为 0,第三个字节为 0。

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

这样,如果所有像素都变成红色,则表明您的格式是 RGBA。尝试一下,几分钟之内你就会明白了。可能有更简单的方法,但我是个菜鸟:-)

在我的例子中,我的图像是 RGBA,但所有像素都存储在 alpha 位中,因为 CGImageRef (inImage) 是透明的,带有灰度阴影。

结论
代码是正确的,但我对输入位图格式的了解是错误的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文