-2

我看过饱和的实现代码,我觉得很不错。但我也想以这种方式实现色调。

饱和代码——

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)));  
   }
4

1 回答 1

1

这是重要的代码,取自 Alex Stone 的回答

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

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

于 2013-07-05T12:24:41.923 回答