如何使用像素缓冲区在UIImage上应用色调?

时间:2013-07-05 11:47:20

标签: ios objective-c uiimage core-image

我已经看到了饱和度的实现代码,我发现它非常好。但我想以这种方式实现色调。

饱和度代码 -

void filterSaturate(UInt8 *pixelBuf, UInt32 offset, void *context)
{
double t = *((double*)context);



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

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

int avg = ( red + green + blue ) / 3;

pixelBuf[r] = SAFECOLOR((avg + t * (red - avg)));
pixelBuf[g] = SAFECOLOR((avg + t * (green - avg)));
pixelBuf[b] = SAFECOLOR((avg + t * (blue - avg)));  
   }

1 个答案:

答案 0 :(得分:1)

以下是重要的代码,取自Alex Stone的回答here

-(UIImage*)doHueAdjustFilterWithBaseImageName:(NSString*)baseImageName hueAdjust:(CGFloat)hueAdjust
{

    CIImage *inputImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:baseImageName]];
    CIFilter * controlsFilter = [CIFilter filterWithName:@"CIHueAdjust"];
    [controlsFilter setValue:inputImage forKey:kCIInputImageKey];

    [controlsFilter setValue:[NSNumber numberWithFloat:hueAdjust] forKey:@"inputAngle"];

    NSLog(@"%@",controlsFilter.attributes);
    CIImage *displayImage = controlsFilter.outputImage;
    UIImage *finalImage = [UIImage imageWithCIImage:displayImage];

    CIContext *context = [CIContext contextWithOptions:nil];
    if (displayImage == nil || finalImage == nil) {
        // We did not get output image. Let's display the original image itself.
       return  [UIImage imageNamed:baseImageName];
    }else {
        // We got output image. Display it.
        return  [UIImage imageWithCGImage:[context createCGImage:displayImage fromRect:displayImage.extent]];
    }    
}

非常整洁,很容易看到正在发生的事情,完成工作。