我試圖從遊戲場景中的某些像素上捕捉R,G和B.爲此,我創建了一個位於黑色&白色的位圖圖像。從原始圖像中獲取意想不到的像素
該圖像首先在Init()上加載,之後,每個精靈運動都會被檢查,因爲它確實是一個可用的點。
事情是,我在R,G和B得到意外的數據。我嘗試了兩個位圖圖像(8位和24位)。他們都只有黑色和白色的像素。但r,g和b一直告訴我這些像素是其他顏色。我認爲「no_of_channels」應該是3,因爲我沒有使用alpha通道,對嗎?有任何想法嗎?
App.h
// background mask
UIImage* bgmask;
CGImageRef aCGImageRef;
CFDataRef rawData;
UInt8 * bgmaskbuf;
的init():
// BG Mask
bgmask = [UIImage imageNamed:@"mask.bmp"];
aCGImageRef = bgmask.CGImage;
rawData = CGDataProviderCopyData(CGImageGetDataProvider(aCGImageRef));
bgmaskbuf = (UInt8 *) CFDataGetBytePtr(rawData);
方法來檢查像素的數據:
-(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)給了我EXC_BAD_ACCESS,地址爲0x13200555。
發生這種情況時,我試着去閱讀它喜歡:
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顏色[3022:70b] R:160,G:182,B:0 2014-01-06 07:25:26.244顏色[3022:70b] R :80,G:111,B:105 2014-01-06 07:25:26.247顏色[3022:70b] R:67,G:84,B:114顏色:顏色: [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 – RickON
嗨喬希,請檢查我的問題。我mande其他幾個測試.. tks! – RickON