关闭

模仿Flickr 使用GPUImage的实时相机滤镜(iOS源代码)

916人阅读 评论(0) 收藏 举报
分类:

from: http://code1.okbase.net/codefile/GPUImagePoissonBlendFilter.m_2016011433675_220.htm


  1. #import "GPUImagePoissonBlendFilter.h"

  2. #if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
  3. NSString *const kGPUImagePoissonBlendFragmentShaderString = SHADER_STRING
  4. (
  5. precision mediump float;

  6. varying vec2 textureCoordinate;
  7. varying vec2 leftTextureCoordinate;
  8. varying vec2 rightTextureCoordinate;
  9. varying vec2 topTextureCoordinate;
  10. varying vec2 bottomTextureCoordinate;

  11. varying vec2 textureCoordinate2;
  12. varying vec2 leftTextureCoordinate2;
  13. varying vec2 rightTextureCoordinate2;
  14. varying vec2 topTextureCoordinate2;
  15. varying vec2 bottomTextureCoordinate2;

  16. uniform sampler2D inputImageTexture;
  17. uniform sampler2D inputImageTexture2;

  18. uniform lowp float mixturePercent;

  19. void main()
  20. {
  21. vec4 centerColor = texture2D(inputImageTexture, textureCoordinate);
  22. vec3 bottomColor = texture2D(inputImageTexture, bottomTextureCoordinate).rgb;
  23. vec3 leftColor = texture2D(inputImageTexture, leftTextureCoordinate).rgb;
  24. vec3 rightColor = texture2D(inputImageTexture, rightTextureCoordinate).rgb;
  25. vec3 topColor = texture2D(inputImageTexture, topTextureCoordinate).rgb;

  26. vec4 centerColor2 = texture2D(inputImageTexture2, textureCoordinate2);
  27. vec3 bottomColor2 = texture2D(inputImageTexture2, bottomTextureCoordinate2).rgb;
  28. vec3 leftColor2 = texture2D(inputImageTexture2, leftTextureCoordinate2).rgb;
  29. vec3 rightColor2 = texture2D(inputImageTexture2, rightTextureCoordinate2).rgb;
  30. vec3 topColor2 = texture2D(inputImageTexture2, topTextureCoordinate2).rgb;

  31. vec3 meanColor = (bottomColor + leftColor + rightColor + topColor) / 4.0;
  32. vec3 diffColor = centerColor.rgb - meanColor;

  33. vec3 meanColor2 = (bottomColor2 + leftColor2 + rightColor2 + topColor2) / 4.0;
  34. vec3 diffColor2 = centerColor2.rgb - meanColor2;

  35. vec3 gradColor = (meanColor + diffColor2);

  36. gl_FragColor = vec4(mix(centerColor.rgb, gradColor, centerColor2.a * mixturePercent), centerColor.a);
  37. }
  38. );
  39. #else
  40. NSString *const kGPUImagePoissonBlendFragmentShaderString = SHADER_STRING
  41. (
  42. varying vec2 textureCoordinate;
  43. varying vec2 leftTextureCoordinate;
  44. varying vec2 rightTextureCoordinate;
  45. varying vec2 topTextureCoordinate;
  46. varying vec2 bottomTextureCoordinate;

  47. varying vec2 textureCoordinate2;
  48. varying vec2 leftTextureCoordinate2;
  49. varying vec2 rightTextureCoordinate2;
  50. varying vec2 topTextureCoordinate2;
  51. varying vec2 bottomTextureCoordinate2;

  52. uniform sampler2D inputImageTexture;
  53. uniform sampler2D inputImageTexture2;

  54. uniform float mixturePercent;

  55. void main()
  56. {
  57. vec4 centerColor = texture2D(inputImageTexture, textureCoordinate);
  58. vec3 bottomColor = texture2D(inputImageTexture, bottomTextureCoordinate).rgb;
  59. vec3 leftColor = texture2D(inputImageTexture, leftTextureCoordinate).rgb;
  60. vec3 rightColor = texture2D(inputImageTexture, rightTextureCoordinate).rgb;
  61. vec3 topColor = texture2D(inputImageTexture, topTextureCoordinate).rgb;

  62. vec4 centerColor2 = texture2D(inputImageTexture2, textureCoordinate2);
  63. vec3 bottomColor2 = texture2D(inputImageTexture2, bottomTextureCoordinate2).rgb;
  64. vec3 leftColor2 = texture2D(inputImageTexture2, leftTextureCoordinate2).rgb;
  65. vec3 rightColor2 = texture2D(inputImageTexture2, rightTextureCoordinate2).rgb;
  66. vec3 topColor2 = texture2D(inputImageTexture2, topTextureCoordinate2).rgb;

  67. vec3 meanColor = (bottomColor + leftColor + rightColor + topColor) / 4.0;
  68. vec3 diffColor = centerColor.rgb - meanColor;

  69. vec3 meanColor2 = (bottomColor2 + leftColor2 + rightColor2 + topColor2) / 4.0;
  70. vec3 diffColor2 = centerColor2.rgb - meanColor2;

  71. vec3 gradColor = (meanColor + diffColor2);

  72. gl_FragColor = vec4(mix(centerColor.rgb, gradColor, centerColor2.a * mixturePercent), centerColor.a);
  73. }
  74. );
  75. #endif

  76. @implementation GPUImagePoissonBlendFilter

  77. @synthesize mix = _mix;
  78. @synthesize numIterations = _numIterations;

  79. - (id)init;
  80. {
  81. if (!(self = [super initWithFragmentShaderFromString:kGPUImagePoissonBlendFragmentShaderString]))
  82. {
  83. return nil;
  84. }

  85. mixUniform = [filterProgram uniformIndex:@"mixturePercent"];
  86. self.mix = 0.5;

  87. self.numIterations = 10;

  88. return self;
  89. }

  90. - (void)setMix:(CGFloat)newValue;
  91. {
  92. _mix = newValue;

  93. [self setFloat:_mix forUniform:mixUniform program:filterProgram];
  94. }

  95. //- (void)setOutputFBO;
  96. //{
  97. // if (self.numIterations % 2 == 1) {
  98. // [self setSecondFilterFBO];
  99. // } else {
  100. // [self setFilterFBO];
  101. // }
  102. //}

  103. - (void)renderToTextureWithVertices:(const GLfloat *)vertices textureCoordinates:(const GLfloat *)textureCoordinates;
  104. {
  105. // Run the first stage of the two-pass filter
  106. [GPUImageContext setActiveShaderProgram:filterProgram];

  107. [super renderToTextureWithVertices:vertices textureCoordinates:textureCoordinates];

  108. for (int pass = 1; pass < self.numIterations; pass++) {

  109. if (pass % 2 == 0) {

  110. [GPUImageContext setActiveShaderProgram:filterProgram];

  111. // TODO: This will over-unlock the incoming framebuffer
  112. [super renderToTextureWithVertices:vertices textureCoordinates:[[self class] textureCoordinatesForRotation:kGPUImageNoRotation]];
  113. } else {
  114. // Run the second stage of the two-pass filter
  115. secondOutputFramebuffer = [[GPUImageContext sharedFramebufferCache] fetchFramebufferForSize:[self sizeOfFBO] textureOptions:self.outputTextureOptions onlyTexture:NO];
  116. [secondOutputFramebuffer activateFramebuffer];

  117. [GPUImageContext setActiveShaderProgram:filterProgram];

  118. glClearColor(backgroundColorRed, backgroundColorGreen, backgroundColorBlue, backgroundColorAlpha);
  119. glClear(GL_COLOR_BUFFER_BIT);

  120. glActiveTexture(GL_TEXTURE2);
  121. glBindTexture(GL_TEXTURE_2D, [outputFramebuffer texture]);
  122. glUniform1i(filterInputTextureUniform, 2);

  123. glActiveTexture(GL_TEXTURE3);
  124. glBindTexture(GL_TEXTURE_2D, [secondInputFramebuffer texture]);
  125. glUniform1i(filterInputTextureUniform2, 3);

  126. glVertexAttribPointer(filterPositionAttribute, 2, GL_FLOAT, 0, 0, vertices);
  127. glVertexAttribPointer(filterTextureCoordinateAttribute, 2, GL_FLOAT, 0, 0, [[self class] textureCoordinatesForRotation:kGPUImageNoRotation]);
  128. glVertexAttribPointer(filterSecondTextureCoordinateAttribute, 2, GL_FLOAT, 0, 0, [[self class] textureCoordinatesForRotation:inputRotation2]);

  129. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  130. }
  131. }
  132. }

  133. @end// 鐗堟潈灞炰簬鍘熶綔鑰?// http://code4app.com (cn) http://code4app.net (en)
  134. // 鍙戝竷浠g爜浜庢渶涓撲笟鐨勬簮鐮佸垎浜綉绔? Code4App.com
0
0
查看评论

iOS GPUImage研究三:视频采集并添加实时滤镜

在GPUImage中使用实时滤镜最简单的方法就是使用GPUImageVideoCamera这个类。通过添加滤镜来实时显示音视频。 前面http://blog.csdn.net/Xoxo_x/article/details/57079331我们知道: 首先我们要了解,下面的内容:GPUImageO...
  • Xoxo_x
  • Xoxo_x
  • 2017-02-28 00:38
  • 3275

iOS 实时滤镜 AVCapture Filter

转自:http://altitudelabs.com/blog/real-time-filter/ Introduction In this tutorial, we will create an iOS app with Objective-C which apply vignette eff...
  • zhangqipu000
  • zhangqipu000
  • 2016-07-27 00:56
  • 3630

iOS --- 使用GPUImage实现的简单滤镜效果

GPUImage 是一个基于 GPU 图像和视频处理的开源 iOS 框架。由于使用 GPU 来处理图像和视频,所以速度非常快. 除了速度上的优势,GPUImage 还提供了很多很棒的图像处理滤镜,但有时候这些基本功能仍然无法满足实际开发中的需求,GPUImage 还支持自定义滤镜.
  • icetime17
  • icetime17
  • 2015-08-31 21:29
  • 3502

GPUImage滤镜实战

GPUImage是现在做滤镜最主流的开源框架,没有之一。作者BradLarson基于openGL对图片处理单元进行封装,提供出GPUImageFilter基类,配合shader,常用滤镜都拿下不是问题。   下面大致讲解下GPUImage里的一些基本概念,为了表达方便。已经知道请跳过 G...
  • jcp312097937
  • jcp312097937
  • 2015-05-19 20:20
  • 15366

Android平台Camera实时滤镜实现方法探讨(二)--Android-GPUImage探讨

上一章讨论了将处理后的数据填充到事先创建的Bitmap中,使用SurfaceView绘制该Bitmap来实现预览数据实时处理。本章介绍android-gpuimage实现方式
  • oShunz
  • oShunz
  • 2015-11-25 16:29
  • 12553

GPUImage滤镜中的shader代码分析,及自定义滤镜

http://m.blog.csdn.net/blog/vegerjiangsir_11109/27172143# GPUImage由于使用GPU,顾其在滤镜染色的时候真正使用的是Open GL的shader语言。下面我们就GPUImagePinchDistortionFi...
  • u011270282
  • u011270282
  • 2015-10-30 11:03
  • 1342

iOS 中的 GpuImage 及相关滤镜介绍

GPUImage是Brad Larson在github托管的一个开源项目,项目实现了图片滤镜、摄像头实时滤镜,该项目的优点不但在于滤镜很多,而且处理效果是基于GPU的,比使用CPU性能更高。 下载地址是:https://github.com/BradLarson/GPUImage //已有的一些...
  • u010092035
  • u010092035
  • 2016-06-26 15:17
  • 778

iOS GPUImage之GPUImageFilterGroup组合滤镜(5)

【提醒】 添加滤镜的顺序不同,效果也不同!示例:使用GPUImageFilterGroup为GPUImagePicture添加组合滤镜代码: // // ViewController.m // GPUImageTest // // Created by 黄健 on 16/7/1. ...
  • MerryGOOT
  • MerryGOOT
  • 2016-07-02 00:08
  • 3962

iOS实时滤镜实现--基于GPUImage。

 1. 背景 前段时间由于项目需求,做了一个基于GPUImage的实时美颜滤镜。现在各种各样的直播、视频App层出不穷,美颜滤镜的需求也越来越多。为了回馈开源,现在我把它放到了GitHub上面,感兴趣的朋友可以去下载。下面将主要介绍实现美颜滤镜的原理和思路。 2. GPUImage GP...
  • args_
  • args_
  • 2016-05-16 15:46
  • 2126

用GPUImage做自己的滤镜

这篇博客主要说一下怎么用GPUImage这个库来撸一个自制滤镜。 GPUImage中滤镜主要使用OpenGL ES来对图片和视频进行渲染,当然,要自制一个滤镜并不需要你对OpenGL ES有很深入的了解,但最好还是简单了解一下。这里给出一个参考资料; 手撸一个滤镜需要算法(像:Overlay(B,A...
  • u1031
  • u1031
  • 2016-03-03 15:52
  • 2567
    个人资料
    • 访问:1926644次
    • 积分:22084
    • 等级:
    • 排名:第394名
    • 原创:102篇
    • 转载:1382篇
    • 译文:6篇
    • 评论:261条
    联系方式
    个人邮箱: xuxiduo@zju.edu.cn
    QQ群:
    1)OpenCV俱乐部
        186168905

    2) 视频/音频/图像/算法/ML
        群1:148111910

        群2:157103105

    备注:加群需要回答问题,避免广告党。
    如果你是博客看到后加的,请注明“博客”并回答问题,只注明”博客“不回答问题的恕不加入。答案为和群相关的任何技术名词,不能出现1)和2)中的任何字眼
    博客专栏
    文章分类
    最新评论