iPhone上的色彩平衡 [英] color balance on the iPhone

查看:95
本文介绍了iPhone上的色彩平衡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在拍摄图像,通过屏幕上下文加载它,并逐像素更改它。我对图像应用了许多不同的滤镜,但是我要做的最后一件事是改变色彩平衡(类似于Photoshop)以使红色更青色。

I am taking an image, loading it via the screen context, and changing it pixel by pixel. I have a number of different filters that I am applying to the images, but the last thing I need to do is shift the color balance (similar to Photoshop) to make the red more cyan.

下面的代码显示了我如何拍摄图像,获取数据以及如何逐像素遍历r / g / b值:

The code below shows how I am taking the image, getting the data, and going through the r/g/b values pixel by pixel:

CGImageRef sourceImage = theImage.image.CGImage;

CFDataRef theData;
theData = CGDataProviderCopyData(CGImageGetDataProvider(sourceImage));

UInt8 *pixelData = (UInt8 *) CFDataGetBytePtr(theData);

int dataLength = CFDataGetLength(theData);


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


for (int index = 0; index < dataLength; index += 4) {

    int r = pixelData[index + red];
    int g = pixelData[index + green];
    int b = pixelData[index + blue];

    // the color balancing would go here...

    if (r < 0) r = 0;
    if (g < 0) g = 0;
    if (b < 0) b = 0;

    if (r > 255) r = 255;
    if (g > 255) g = 255;
    if (b > 255) b = 255;

    pixelData[index + red] = r;
    pixelData[index + green] = g;
    pixelData[index + blue] = b;

}



CGContextRef context;
context = CGBitmapContextCreate(pixelData,
                                CGImageGetWidth(sourceImage),
                                CGImageGetHeight(sourceImage),
                                8,
                                CGImageGetBytesPerRow(sourceImage),
                                CGImageGetColorSpace(sourceImage),
                                kCGImageAlphaPremultipliedLast);

CGImageRef newCGImage = CGBitmapContextCreateImage(context);
UIImage *newImage = [UIImage imageWithCGImage:newCGImage];

CGContextRelease(context);
CFRelease(theData);
CGImageRelease(newCGImage);

theImage.image = newImage;

我正在对像素数据进行其他一些操作(设置电平,降低饱和度),但是

I am doing a number of other things to the pixel data (setting the levels, desaturating) but I need to shift the red value toward cyan.

在Photoshop中,我可以通过以下方式实现此操作:

In Photoshop, I am able to do it via:

图像:调整:颜色平衡
并将其设置为-30 0 0

Image: Adjustment: Color Balance and setting it to -30 0 0

我无法找到算法或示例来说明这种颜色偏移被执行。我尝试从每个红色值中减去30,或将最大最大值设置为255-30(225),但是这些值似乎限制了颜色并且不偏移值...现在,我只是在上面乱砍,但是指向引用的指针会有所帮助。

I have not been able to find an algorithm or an example explaining how this color shift is performed. I have tried to subtract 30 from every red value, or setting a hard maximum at 255 - 30 (225), but those seem to clip the colors and not shift the values... Right now, I'm just hacking around on it, but pointers to a reference would help out a lot.

(注意:我无法使用OpenGL解决方案,因为我必须采用相同的算法并将其转换到PHP / gd以获得此应用程序的Web服务器版本)

(NB: I am not able to use an OpenGL solution because I have to take the same algorithm and convert it to PHP/gd for a web server version of this application)

推荐答案

您需要做的是减去红色然后更正无论您选择哪种亮度,都可以使用亮度来匹配旧颜色。这样的事情应该起作用:

What you need to do is subtract the red and then correct the "lightness" to match the old color, for whatever measure of lightness you choose. Something like this should work:

// First, calculate the current lightness.
float oldL = r * 0.30 + g * 0.59 + b * 0.11;

// Adjust the color components. This changes lightness.
r = r - 30;
if (r < 0) r = 0;

// Now correct the color back to the old lightness.
float newL = r * 0.30 + g * 0.59 + b * 0.11;
if (newL > 0) {
    r = r * oldL / newL;
    g = g * oldL / newL;
    b = b * oldL / newL;
}

请注意,当给出纯红色时,此行为有些奇怪(它将保持不变) 。 OTOH, GIMP (版本2.6.11)在其颜色平衡工具中也做同样的事情,所以我不太喜欢对此感到担心。

Note this behaves somewhat oddly when given pure red (it will leave it unchanged). OTOH, GIMP (version 2.6.11) does the same thing in its color balance tool so I'm not too worried about it.

这篇关于iPhone上的色彩平衡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆