Box Blur CGImageRef

时间:2011-02-08 15:04:41

标签: iphone core-graphics blur

我正在尝试实现一个简单的Box Blur,但是我遇到了问题。即,不是模糊图像,而是将每个像素转换为红色,绿色,蓝色或黑色。不确定到底发生了什么。任何帮助将不胜感激。

请注意,这段代码只是让它运转的第一步,我不担心速度......但是。

- (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;
}

2 个答案:

答案 0 :(得分:1)

char颜色应声明为int。

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

答案 1 :(得分: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循环的标题:

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++) {