objective-c - Core Graphics 点彩化 CGImage 效果

标签 objective-c ios c image-processing core-graphics

所以我最近一直在编写大量仅使用核心图形的图像处理代码,并且我制作了相当多的工作滤镜来操纵颜色、应用混合、模糊和类似的东西。但是我在编写滤镜以将点画效果应用于这样的图像时遇到了问题:

pointillize

我想做的是获取一个像素的颜色并用该颜色填充一个椭圆,遍历图像并每隔几个像素执行一次此操作,代码如下:

编辑:这是我这次的新代码,它只是在图像底部画了几个小圆圈,我是不是像你说的那样做对了?

-(UIImage*)applyFilterWithAmount:(double)amount {

CGImageRef inImage = self.CGImage;
CFDataRef m_dataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage));
UInt8* m_pixelBuf = (UInt8*)CFDataGetBytePtr(m_dataRef);

int length = CFDataGetLength(m_dataRef);

CGContextRef ctx = CGBitmapContextCreate(m_pixelBuf,
                                    CGImageGetWidth(inImage),
                                    CGImageGetHeight(inImage),
                                    CGImageGetBitsPerComponent(inImage),
                                    CGImageGetBytesPerRow(inImage),
                                    CGImageGetColorSpace(inImage),
                                    CGImageGetBitmapInfo(inImage));

int row = 0;
int imageWidth = self.size.width;

if ((row%imageWidth)==0) {
    row++;
}

int col = row%imageWidth;

for (int i = 0; i<length; i+=4) {
    //filterPointillize(m_pixelBuf, i, context);

    int r = i;
    int g = i+1;
    int b = i+2;

    int red = m_pixelBuf[r];
    int green = m_pixelBuf[g];
    int blue = m_pixelBuf[b];

    CGContextSetRGBFillColor(ctx, red/255, green/255, blue/255, 1.0);
    CGContextFillEllipseInRect(ctx, CGRectMake(col, row, amount, amount));
}

CGImageRef imageRef = CGBitmapContextCreateImage(ctx);
CGContextRelease(ctx);
UIImage* finalImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CFRelease(m_dataRef);
return finalImage;

最佳答案

我马上看到的一个问题是您正在为 X 和 Y 原点使用光栅像元编号。此配置中的光栅只是一条尺寸线。您可以根据光栅图像的宽度计算第二个维度。这可以解释为什么你有一条线。

另一件事:似乎您正在读取图像的每个像素。您不想跳过您尝试绘制的椭圆宽度的像素吗?

接下来看起来可疑的是,我认为您应该在 绘图之前创建您正在绘图的上下文。此外,您不应调用:

CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSaveGState(contextRef);

CGContextRestoreGState(contextRef);

在循环内。

编辑:

进一步观察:您读取的 RGB 值是 0-255,CGContextSetRGBFillColor 函数期望值介于 0.f - 1.f 之间。这可以解释为什么你变白了。所以你需要除以 255:

CGContextSetRGBFillColor(contextRef, red / 255, green / 255, blue / 255, 1.0);

如果您还有任何问题,请随时提问!

编辑 2:

要计算行,首先在循环外声明一个行计数器:

int row = 0; //declare before the loop

int imageWidth = self.size.width; //get the image width

if ((i % imageWidth) == 0) { //we divide the cell number and if the remainder is 0
                             //then we want to increment the row counter
    row++;
}

我们也可以使用mod来计算当前列:

int col = i % imageWidth; //divide i by the image width. the remainder is the col num

编辑 3: 你必须把它放在 for 循环中:

if ((row%imageWidth)==0) {
    row++;
}

int col = row%imageWidth;

此外,我之前忘了提到,要使列和行基于 0(这是您想要的),您需要从图像大小中减去 1:

 int imageWidth = self.size.width - 1;

关于objective-c - Core Graphics 点彩化 CGImage 效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11771505/

相关文章:

ios - 我需要了解为什么 Objective-C 中的委托(delegate)如此重要,是什么让它如此特别?

ios - 选中/取消选中时 Objective-C 突出显示 UIButton

objective-c - 错误 : Mutating method sent to immutable object for NSMutableArray from JSON file

python - 使用 NSStreams 在 iOS 上复制文件的问题

ios - 取消归档编码数据返回 nil 和不正确的格式

ios - 不搜索第一个字母

c - pthread_create : cannot allocate memory

c - sleep() 以 float 作为参数不起作用

iphone - Objective-C : NSMutableArray does not retain objects

c - 数组一致性 - 基础 C 编程