有时候,单独对一张图像进行处理是很难或者根本达不到我们想要的效果的。一个好的滤镜效果的诞生,往往要经过很多复杂步骤、细致微调、图片应用效果观察以及很多图层叠加。

我在JSWidget上发现了一些常用混合算法,对应着一些常用混合模式,通过这些blend modes,我们可以指定两张图像如何混合。

不过在此之前,我们需要纯颜色图像和渐变图像来做辅助:

  1. + (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
  2. {
  3. // http://stackoverflow.com/questions/1213790/how-to-get-a-color-image-in-iphone-sdk
  4. //Create a context of the appropriate size
  5. UIGraphicsBeginImageContext(size);
  6. CGContextRef currentContext = UIGraphicsGetCurrentContext();
  7. //Build a rect of appropriate size at origin 0,0
  8. CGRect fillRect = CGRectMake(0, 0, size.width, size.height);
  9. //Set the fill color
  10. CGContextSetFillColorWithColor(currentContext, color.CGColor);
  11. //Fill the color
  12. CGContextFillRect(currentContext, fillRect);
  13. //Snap the picture and close the context
  14. UIImage *colorImage = UIGraphicsGetImageFromCurrentImageContext();
  15. UIGraphicsEndImageContext();
  16. return colorImage;
  17. }
  1. + (UIImage *)imageWithGradient:(UIImage *)image startColor:(UIColor *)startColor endColor:(UIColor *)endColor
  2. {
  3. UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
  4. CGContextRef context = UIGraphicsGetCurrentContext();
  5. CGContextTranslateCTM(context, 0, image.size.height);
  6. CGContextScaleCTM(context, 1.0, -1.0);
  7. CGContextSetBlendMode(context, kCGBlendModeNormal);
  8. CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
  9. CGContextDrawImage(context, rect, image.CGImage);
  10. // Create gradient
  11. NSArray *colors = [NSArray arrayWithObjects:(id)endColor.CGColor, (id)startColor.CGColor, nil];
  12. CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
  13. CGGradientRef gradient = CGGradientCreateWithColors(space, (CFArrayRef)colors, NULL);
  14. // Apply gradient
  15. CGContextClipToMask(context, rect, image.CGImage);
  16. CGContextDrawLinearGradient(context, gradient, CGPointMake(0,0), CGPointMake(0, image.size.height), 0);
  17. UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
  18. UIGraphicsEndImageContext();
  19. CGGradientRelease(gradient);
  20. CGColorSpaceRelease(space);
  21. return gradientImage;
  22. }

而且在第一篇文章中提到的透明度滤镜(作用域像素的alpha值上)是没效果的,可以通过Quartz 2D来实现:

  1. - (UIImage *)setAlpha:(CGFloat)alpha
  2. {
  3. // http://stackoverflow.com/questions/5084845/how-to-set-the-opacity-alpha-of-a-uiimage
  4. UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
  5. CGContextRef ctx = UIGraphicsGetCurrentContext();
  6. CGRect area = CGRectMake(0, 0, self.size.width, self.size.height);
  7. CGContextScaleCTM(ctx, 1, -1);
  8. CGContextTranslateCTM(ctx, 0, -area.size.height);
  9. CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
  10. CGContextSetAlpha(ctx, alpha);
  11. CGContextDrawImage(ctx, area, self.CGImage);
  12. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  13. UIGraphicsEndImageContext();
  14. return newImage;
  15. }

在此基础上,通过下面四行代码,可以分别得到四种不同效果:

  1. return [[UIImage imageWithColor:[UIColor purpleColor] size:originImage.size] changeOpacityByFactor:0.5];;
  1. return [UIImage imageWithGradient:originImage startColor:[UIColor whiteColor] endColor:[UIColor yellowColor]];
  1. return [[originImage tintWithMaxRGBA:(RGBA){190, 190, 230} minRGBA:(RGBA){50, 35, 10}] overlayWithImage:[[UIImage imageWithColor:[UIColor purpleColor] size:originImage.size] changeOpacityByFactor:0.3]];
  1. return [originImage softlightWithImage:[[UIImage imageWithColor:[UIColor yellowColor] size:originImage.size] changeOpacityByFactor:0.8]];

 

 

其中,overlay算法如下:

  1. double calcOverlay(float b, float t)
  2. {
  3. return (b > 128.0f) ? 255.0f - 2.0f * (255.0f - t) * (255.0f - b) / 255.0f: (b * t * 2.0f) / 255.0f;
  4. }
  5. void filterOverlay(UInt8 *pixelBuf, UInt8 *pixedBlendBuf, UInt32 offset, void *context)
  6. {
  7. int r = offset;
  8. int g = offset+1;
  9. int b = offset+2;
  10. int red = pixelBuf[r];
  11. int green = pixelBuf[g];
  12. int blue = pixelBuf[b];
  13. int blendRed = pixedBlendBuf[r];
  14. int blendGreen = pixedBlendBuf[g];
  15. int blendBlue = pixedBlendBuf[b];
  16. pixelBuf[r] = SAFECOLOR(calcOverlay(red, blendRed));
  17. pixelBuf[g] = SAFECOLOR(calcOverlay(green, blendGreen));
  18. pixelBuf[b] = SAFECOLOR(calcOverlay(blue, blendBlue));
  19. }

版权声明:本文为博主原创文章,未经博主允许不得转载。

iOS中的图像处理(三)——混合运算的更多相关文章

  1. iOS中的图像处理(二)——卷积运算

    关于图像处理中的卷积运算,这里有两份简明扼要的介绍:文一,文二. 其中,可能的一种卷积运算代码如下: - (UIImage*)applyConvolution:(NSArray*)kernel { C ...

  2. iOS中正则表达式的三种使用方式

    1.利用NSPredicate(谓词)匹配 例如匹配有效邮箱: NSString *email = @“nijino_saki@163.com”: NSString *regex = @"[ ...

  3. iOS中的图像处理(一)——基础滤镜

    最近在稍微做一些整理,翻起这部分的代码,发现是两个多月前的了. 这里讨论的是基于RGBA模型下的图像处理,即将变换作用在每个像素上. 代码是以UIImage的category形式存在的: typede ...

  4. iOS中几种常用的数据存储方式

    自己稍微总结了一下下,方便大家查看 1.write直接写入文件的方法 永久保存在磁盘中,可以存储的对象有NSString.NSArray.NSDictionary.NSData.NSNumber,数据 ...

  5. [OpenCV-Python] OpenCV 中的图像处理 部分 IV (三)

    部分 IVOpenCV 中的图像处理 OpenCV-Python 中文教程(搬运)目录 19 Canny 边缘检测 目标 • 了解 Canny 边缘检测的概念 • 学习函数 cv2.Canny() 1 ...

  6. Delphi中的三目运算函数有哪些?(XE10.2+WIN764)

    相关资料:https://www.cnblogs.com/rogge7/p/6078903.html 问题现象:在做一个判断时突然想到了C++的三目运算,就在想Delphi中一共有几个? 问题处理: ...

  7. python中实现三目运算

    python中没有其他语言中的三元表达式,不过有类似的实现方法 如: a = 1 b =2 k = 3 if a>b else 4 上面的代码就是python中实现三目运算的一个小demo, 如 ...

  8. 玩转iOS开发:iOS中的GCD开发(三)

    上一章, 我们了解到了GCD里的一些队列和任务的知识, 也实践了一下, 同时我们也对主队列的一些小情况了解了一下, 比如上一章讲到的卡线程的问题, 如果没有看的朋友可以去看看玩转iOS开发:iOS中的 ...

  9. iOS中的几种锁的总结,三种开启多线程的方式(GCD、NSOperation、NSThread)

    学习内容 欢迎关注我的iOS学习总结--每天学一点iOS:https://github.com/practiceqian/one-day-one-iOS-summary OC中的几种锁 为什么要引入锁 ...

随机推荐

  1. window.parent与window.opener的区别与使用

    window.parent 是iframe页面调用父页面对象 举例: a.html 如果我们需要在b.html中要对a.html中的username文本框赋值(就如很多上传功能,上传功能页在ifrma ...

  2. MysqlHelp

    using System.Configuration;using MySql.Data: public class MySqlHelp { //链接字符串 private static string ...

  3. PHP的一些函数

    //进制转换类 base_convert //字符转十六进制 binhex

  4. How to configure connectingstrings in Web.config

    先来看一下默认的连接SQL Server数据库配置<connectionStrings>   <add name="LocalSqlServer" connect ...

  5. 国内BI工具/报表工具厂商简介

    v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...

  6. zookeeper集群搭建设置

    zookeeper 官网:http://zookeeper.apache.org/ 现在最新版本是 3.4.6 ,但是这个版本我没有运行起来,可能是那配置出现问题了,现在我用的是3.4.5 http: ...

  7. Oracle中sign函数和decode函数的使用

    Oracle中sign函数和decode函数的使用 1.比较大小函数SIGN sign(x)或者Sign(x)叫做 符号函数,其功能是取某个数的符号(正或负): 当x>0,sign(x)=1; ...

  8. HDU 1108 最小公倍数

    #include <cstdio> int gcd(int a,int b) { ) return a; else return gcd(b,a%b); } int main() { in ...

  9. HDU 3486 Interviewe

    题目大意:给定n个数的序列,让我们找前面k个区间的最大值之和,每个区间长度为n/k,如果有剩余的区间长度不足n/k则无视之.现在让我们找最小的k使得和严格大于m. 题解:二分k,然后求RMQ检验. S ...

  10. Candy Sharing Game(模拟搜索)

    Candy Sharing Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...