2012-07-03 84 views
0

我試圖以逐像素使用核芯顯卡(破CGImage到無符號整數使用CFData)核芯顯卡的RGB數據問題

當我嘗試創建一個具有處理數據的成像圖像過濾器然而, ,由此產生的圖像具有明顯不同的顏色。

我註釋了整個循環,我實際上改變了像素的rgb值,也沒有任何變化。

當我初始化我在過濾器中使用的UIImage;我使用drawInRect和UIGraphicsBeginContext()來調整大小。在從相機拍攝的圖像上。

當我刪除調整大小的步驟,並直接從相機設置我的形象;過濾器似乎工作得很好。這裏就是我初始化我用圖像(從內didFinishPickingImage)

self.editingImage是一個UIImageView和self.editingUIImage代碼是一個UIImage

-(void)imagePickerController:(UIImagePickerController *)picker 
    didFinishPickingImage : (UIImage *)image 
      editingInfo:(NSDictionary *)editingInfo 
{ 

self.didAskForImage = YES; 

UIGraphicsBeginImageContext(self.editingImage.frame.size); 

float prop = image.size.width/image.size.height; 

float left, top, width, height; 

if(prop < 1){ 

    height = self.editingImage.frame.size.height; 

    width = (height/image.size.height) * image.size.width; 

    left = (self.editingImage.frame.size.width - width)/2; 

    top = 0; 

}else{ 

    width = self.editingImage.frame.size.width; 

    height = (width/image.size.width) * image.size.height; 

    top = (self.editingImage.frame.size.height - height)/2; 

    left = 0; 

} 

[image drawInRect:CGRectMake(left, top, width, height)]; 

self.editingUIImage = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 



self.editingImage.image = self.editingUIImage; 

[self.contrastSlider addTarget:self action:@selector(doImageFilter:) forControlEvents:UIControlEventValueChanged]; 

[self.brightnessSlider addTarget:self action:@selector(doImageFilter:) forControlEvents:UIControlEventValueChanged]; 

[picker dismissModalViewControllerAnimated:YES]; 
picker = nil; 
} 

的調整圖像大小隻是我需要儘可能位置的方式;

這裏是圖像過濾功能,我已經把實際的循環內容取出來了,因爲它們是不相關的。

- (void) doImageFilter:(id)sender{ 


CGImageRef src = self.editingUIImage.CGImage; 

CFDataRef dta; 
dta = CGDataProviderCopyData(CGImageGetDataProvider(src)); 

UInt8 *pixData = (UInt8 *) CFDataGetBytePtr(dta); 

int dtaLen = CFDataGetLength(dta); 

for (int i = 0; i < dtaLen; i += 3) { 
    //the loop 
} 

CGContextRef ctx; 

ctx = CGBitmapContextCreate(pixData, CGImageGetWidth(src), CGImageGetHeight(src), 8, CGImageGetBytesPerRow(src), CGImageGetColorSpace(src), kCGImageAlphaPremultipliedLast); 

CGImageRef newCG = CGBitmapContextCreateImage(ctx); 
UIImage *new = [UIImage imageWithCGImage:newCG]; 

CGContextRelease(ctx); 
CFRelease(dta); 
CGImageRelease(newCG); 

self.editingImage.image = new; 

} 

圖像看上去像這樣在第一

http://picsilk.com/media/before.png

,然後做doImageFilter後...

http://picsilk.com/media/before.png

正如前面提到的,當我用這只是發生上面顯示的調整大小方法。

真的難住這個,一直在研究它......任何幫助非常感謝!

乾杯

更新:我已經檢查的所有圖像對象的色彩空間和他們都kCGColorSpaceDeviceRGB。漂亮的難倒了這一個傢伙,我很漂亮,當我把圖像分解成無符號整數時有些事情出錯了,但我不確定是什麼..任何人?

回答

1

你的問題是,在最後一行:

ctx = CGBitmapContextCreate(pixData, 
          CGImageGetWidth(src), 
          CGImageGetHeight(src), 
          8, 
          CGImageGetBytesPerRow(src), 
          CGImageGetColorSpace(src), 
          kCGImageAlphaPremultipliedLast); 

你讓有關α和源圖像,這顯然是不正確的數據的分量排序的假設。您應該通過CGImageGetBitmapInfo(src)從源圖像中獲取該圖像。

爲了避免像這樣的問題,如果您從任意CGImage開始,並且想要直接操作位圖的字節,最好使用您指定的格式製作CGBitmapContext(不是直接採用來自源圖像)。然後,將源圖像繪製到位圖上下文中;如有必要,CG會將圖像的數據轉換爲位圖上下文的格式。然後從位圖上下文獲取數據並對其進行處理。

+0

令人驚歎的是,這個伎倆。我明白你對alpha組件的看法,這實際上就是我目前正在調試的地方,但我沒有意識到你可以從原始圖像中獲取它。感謝您的回覆,幫助我理解了這些東西。乾杯! – Doug