首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >框模糊CGImageRef

框模糊CGImageRef
EN

Stack Overflow用户
提问于 2011-02-08 23:04:41
回答 2查看 657关注 0票数 1

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

请注意,这段代码只是让它工作的第一步,我并不担心速度……目前还没有。

代码语言:javascript
复制
- (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;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-02-09 04:07:48

字符颜色应该声明为int。

代码语言:javascript
复制
int red = 0;
int green = 0;
int blue = 0;
票数 1
EN

Stack Overflow用户

发布于 2012-03-27 20:40:32

我只有一个评论。我使用了这个脚本,它真的工作得又好又快。唯一的事情是,它离开的框架与半径的宽度是不模糊的。所以我对它做了一些修改,以便它在边缘附近使用镜像技术模糊整个区域。

对于这个,需要放入以下几行:

代码语言:javascript
复制
// 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;

在这些行之后:

代码语言:javascript
复制
int xIndex = widthIndex + radiusX;
int yIndex = heightIndex + radiusY;

然后替换for循环的头部:

代码语言:javascript
复制
for (int widthIndex = radius; widthIndex < width - radius; widthIndex++) {
    for (int heightIndex = radius; heightIndex < height - radius; heightIndex++) {

使用这些标头:

代码语言:javascript
复制
for (int widthIndex = 0; widthIndex < width; widthIndex++) {
    for (int heightIndex = 0; heightIndex < height; heightIndex++) {
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4934687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档