【发布时间】:2014-01-06 21:22:10
【问题描述】:
我试图从游戏场景的某些像素中捕捉 R、G 和 B。为此,我创建了一个黑白位图图像。
此图像首先在 Init() 上加载,然后检查每个精灵移动是否确实是可用点。
问题是我在 R、G 和 B 处获得了意想不到的数据。我尝试了两个位图图像(8 位和 24 位)。它们都只有黑白像素。但是 r、g 和 b 一直告诉我这些像素是任何其他颜色。我认为“no_of_channels”应该是 3,因为我没有使用 alpha 通道,对吧?有任何想法吗?
应用程序.h
// background mask
UIImage* bgmask;
CGImageRef aCGImageRef;
CFDataRef rawData;
UInt8 * bgmaskbuf;
初始化():
// BG Mask
bgmask = [UIImage imageNamed:@"mask.bmp"];
aCGImageRef = bgmask.CGImage;
rawData = CGDataProviderCopyData(CGImageGetDataProvider(aCGImageRef));
bgmaskbuf = (UInt8 *) CFDataGetBytePtr(rawData);
检查Pixel数据的方法:
-(BOOL) checkPixel: (CGFloat)x : (CGFloat)y{
BOOL result = FALSE;
//int length = CFDataGetLength(rawData);
//for(int i=0; i<length; i+=3)
//{
// int r = bgmaskbuf[i];
// int g = bgmaskbuf[i+1];
// int b = bgmaskbuf[i+2];
// NSLog(@"Ptr: %d, R: %d, G: %d, B: %d", i, r, g, b);
//}
int no_of_channels = 3;
int image_width = SCREEN_WIDTH();
unsigned long row_stride = image_width * no_of_channels; // 960 bytes in this case
unsigned long x_offset = x * no_of_channels;
/* assuming RGB byte order (as opposed to BGR) */
row_stride * (int)y + x_offset
int r = bgmaskbuf[next_pixel];
int g = bgmaskbuf[next_pixel + 1];
int b = bgmaskbuf[next_pixel + 2];
NSLog(@"Ptr: %d, R: %d, G: %d, B: %d",next_pixel r, g, b);
if((r==0)&&(g==0)&&(b==0)){
result = TRUE;
}
return result;
}
如何解决这个问题? 谢谢。
以下问题:
这是我试图解决这个问题的方法:
在像素检查时,我尝试运行内部的每个像素:
int length = CFDataGetLength(rawData);
for(int i=0; i<length; i+=3)
{
int r = bgmaskbuf[i];
int g = bgmaskbuf[i+1];
int b = bgmaskbuf[i+2];
NSLog(@"Ptr: %d, R: %d, G: %d, B: %d", i, r, g, b);
}
长度为 786432,这是有道理的(1024 * 768 像素)。我可以看到/读取所有像素,总共 2359296 字节(R + G + B)。
现在,奇怪的是,在处理用户的触摸和移动时,数据缓冲区索引(例如 793941)在地址 0x13200555 处为我提供 EXC_BAD_ACCESS。
当我尝试像这样阅读它时会发生这种情况:
row_stride * (int)y + x_offset
int r = bgmaskbuf[next_pixel];
int g = bgmaskbuf[next_pixel + 1];
int b = bgmaskbuf[next_pixel + 2];
bgmaskbuf 从 0x13240000 开始。
因此,从 0x13240000 到 0x13480000 的地址范围应该是可读的。
但我刚刚读到了同样的地址!
【问题讨论】:
-
您看到的实际频道值是多少?
-
几个样本:2014-01-06 07:25:26.190 color[3022:70b] R: 160, G: 182, B: 0 2014-01-06 07:25:26.244 color [3022:70b] R: 80, G: 111, B: 105 2014-01-06 07:25:26.247 颜色[3022:70b] R: 67, G: 84, B: 114 2014-01-06 07: 25:26.256 颜色[3022:70b] R: 32, G: 109, B: 97 2014-01-06 07:25:26.298 颜色[3022:70b] R: 116, G: 104, B: 0 2014-01 -06 07:25:26.300 颜色[3022:70b] R:116,G:67,B:111
-
嗨乔希,请检查我的问题。我要求其他几个测试.. tks!
标签: ios objective-c bitmap core-foundation bytebuffer