滤镜中对图片的饱和度处理

static inline uint8_t clamp(uint8_t a, uint8_t b) {
  int16_t newval = (int32_t) a - b;
  if (newval > 255)
    return 255;
  if (newval < 0)
    return 0;
  return newval;
}

void saturation(uint8_t *data, int width, int height, float value) {
  uint16_t v = value ;
  for (int i = 0; i < width * height * 4; i += 4) {
    uint8_t r = data[i + 0];
    uint8_t g = data[i + 1];
    uint8_t b = data[i + 2];
    uint8_t avg = (int16_t) (r + g + b) / 3;

    uint8_t rr = clamp(r, avg);
    uint8_t gg = clamp(g, avg);
    uint8_t bb = clamp(b, avg);


    data[i + 0] = rr * v + avg;
    data[i + 1] = gg * v + avg;
    data[i + 2] = bb * v + avg;

  }
}

exmple:

NSInteger theWidth = (NSInteger)srcImg.size.width;
// apply blending effect
unsigned char *rawData = malloc(theWidth * theWidth * 4);

CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
CGRect theFrame = CGRectMake(0.0, 0.0, srcImg.size.width, srcImg.size.height);

CGContextRef context = CGBitmapContextCreate(rawData, theWidth, theWidth, 8, theWidth * 4, cs, kCGImageAlphaNoneSkipLast);
CGColorSpaceRelease(cs);

UIImage *sat_image = srcImg;
// draw image first
CGContextDrawImage(context, theFrame, sat_image.CGImage);

saturation(rawData, theWidth, theWidth, 0.16);// save final image
CGImageRef finalImage = CGBitmapContextCreateImage(context);
// release resouces
UIImage *result = [UIImage imageWithCGImage:finalImage];
CGContextRelease(context);
CGImageRelease(finalImage);
//CGImageRelease(firstStageImage);
free(rawData);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值