2011-02-08 51 views
1

我試圖實現一個簡單的框模糊,但我有問題。也就是說,不是模糊圖像,而是將每個像素轉換爲:紅色,綠色,藍色或黑色。不確定到底發生了什麼。任何幫助,將不勝感激。框模糊CGImageRef

請注意,這段代碼僅僅是第一次讓它工作,我並不擔心速度......但是。

- (CGImageRef)blur:(CGImageRef)base radius:(int)radius { 
CGContextRef ctx; 
CGImageRef imageRef = base; 
NSUInteger width = CGImageGetWidth(imageRef); 
NSUInteger height = CGImageGetHeight(imageRef); 
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
unsigned char *rawData = malloc(height * width *4); 
NSUInteger bytesPerPixel = 4; 
NSUInteger bytesPerRow = bytesPerPixel * width; 
NSUInteger bitsPerComponent = 8; 
CGContextRef context = CGBitmapContextCreate(rawData, width, height, 
              bitsPerComponent, bytesPerRow, colorSpace, 
              kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 
CGColorSpaceRelease(colorSpace); 

CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); 
CGContextRelease(context); 

char red = 0; 
char green = 0; 
char blue = 0; 
for (int widthIndex = radius; widthIndex < width - radius; widthIndex++) { 
    for (int heightIndex = radius; heightIndex < height - radius; heightIndex++) { 
     red = 0; 
     green = 0; 
     blue = 0; 
     for (int radiusY = -radius; radiusY <= radius; ++radiusY) { 
      for (int radiusX = -radius; radiusX <= radius; ++radiusX) { 

       int xIndex = widthIndex + radiusX; 
       int yIndex = heightIndex + radiusY; 

       int index = ((yIndex * width) + xIndex) * 4; 
       red += rawData[index]; 
       green += rawData[index + 1]; 
       blue += rawData[index + 2]; 
      } 
     } 

     int currentIndex = ((heightIndex * width) + widthIndex) * 4; 

     int divisor = (radius * 2) + 1; 
     divisor *= divisor; 

     int finalRed = red/divisor; 
     int finalGreen = green/divisor; 
     int finalBlue = blue/divisor; 

     rawData[currentIndex] = (char)finalRed; 
     rawData[currentIndex + 1] = (char)finalGreen; 
     rawData[currentIndex + 2] = (char)finalBlue; 
    } 
} 


ctx = CGBitmapContextCreate(rawData, 
          CGImageGetWidth(imageRef), 
          CGImageGetHeight(imageRef), 
          8, 
          CGImageGetBytesPerRow(imageRef), 
          CGImageGetColorSpace(imageRef), 
          kCGImageAlphaPremultipliedLast); 

imageRef = CGBitmapContextCreateImage (ctx); 

CGContextRelease(ctx); 

free(rawData); 
[(id)imageRef autorelease]; 
return imageRef; 
} 
+0

AH!當我發佈這個時發現它。我宣佈紅色,綠色和藍色爲字符。隨着chars快速包裝,應該是int。第一個回答這個問題的人,我會接受他們的。 :) – MarkPowell 2011-02-08 15:18:24

回答

1

char的顏色應該聲明爲int。

int red = 0; 
int green = 0; 
int blue = 0; 
1

我只是一個評論。我使用了這個腳本,它運行得很好,速度很快。唯一的問題是它會以不模糊的半徑寬度離開框架。所以我對它進行了一些修改,以便在邊緣附近使用鏡像技術模糊整個區域。

對於這一個需要把下面幾行:

// Mirroring the part between edge and blur raduis 
if (xIndex<0) xIndex=-xIndex-1; else if (xIndex>width-1) xIndex=2*width-xIndex-1; 
if (yIndex<0) yIndex=-yIndex-1; else if (yIndex>height-1) yIndex=2*height-yIndex-1; 

這些行後:

int xIndex = widthIndex + radiusX; 
int yIndex = heightIndex + radiusY; 

,然後更換的頭的循環:

for (int widthIndex = radius; widthIndex < width - radius; widthIndex++) { 
    for (int heightIndex = radius; heightIndex < height - radius; heightIndex++) { 

這些標題:

for (int widthIndex = 0; widthIndex < width; widthIndex++) { 
    for (int heightIndex = 0; heightIndex < height; heightIndex++) {