GPUImageHistogramGenerator.m 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #import "GPUImageHistogramGenerator.h"
  2. NSString *const kGPUImageHistogramGeneratorVertexShaderString = SHADER_STRING
  3. (
  4. attribute vec4 position;
  5. attribute vec4 inputTextureCoordinate;
  6. varying vec2 textureCoordinate;
  7. varying float height;
  8. void main()
  9. {
  10. gl_Position = position;
  11. textureCoordinate = vec2(inputTextureCoordinate.x, 0.5);
  12. height = 1.0 - inputTextureCoordinate.y;
  13. }
  14. );
  15. #if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
  16. NSString *const kGPUImageHistogramGeneratorFragmentShaderString = SHADER_STRING
  17. (
  18. varying highp vec2 textureCoordinate;
  19. varying highp float height;
  20. uniform sampler2D inputImageTexture;
  21. uniform lowp vec4 backgroundColor;
  22. void main()
  23. {
  24. lowp vec3 colorChannels = texture2D(inputImageTexture, textureCoordinate).rgb;
  25. lowp vec4 heightTest = vec4(step(height, colorChannels), 1.0);
  26. gl_FragColor = mix(backgroundColor, heightTest, heightTest.r + heightTest.g + heightTest.b);
  27. }
  28. );
  29. #else
  30. NSString *const kGPUImageHistogramGeneratorFragmentShaderString = SHADER_STRING
  31. (
  32. varying vec2 textureCoordinate;
  33. varying float height;
  34. uniform sampler2D inputImageTexture;
  35. uniform vec4 backgroundColor;
  36. void main()
  37. {
  38. vec3 colorChannels = texture2D(inputImageTexture, textureCoordinate).rgb;
  39. vec4 heightTest = vec4(step(height, colorChannels), 1.0);
  40. gl_FragColor = mix(backgroundColor, heightTest, heightTest.r + heightTest.g + heightTest.b);
  41. }
  42. );
  43. #endif
  44. @implementation GPUImageHistogramGenerator
  45. #pragma mark -
  46. #pragma mark Initialization and teardown
  47. - (id)init;
  48. {
  49. if (!(self = [super initWithVertexShaderFromString:kGPUImageHistogramGeneratorVertexShaderString fragmentShaderFromString:kGPUImageHistogramGeneratorFragmentShaderString]))
  50. {
  51. return nil;
  52. }
  53. backgroundColorUniform = [filterProgram uniformIndex:@"backgroundColor"];
  54. [self setBackgroundColorRed:0.0 green:0.0 blue:0.0 alpha:0.0];
  55. return self;
  56. }
  57. #pragma mark -
  58. #pragma mark Accessors
  59. - (void)setBackgroundColorRed:(GLfloat)redComponent green:(GLfloat)greenComponent blue:(GLfloat)blueComponent alpha:(GLfloat)alphaComponent;
  60. {
  61. // GLfloat backgroundColor[4];
  62. // backgroundColor[0] = redComponent;
  63. // backgroundColor[1] = greenComponent;
  64. // backgroundColor[2] = blueComponent;
  65. // backgroundColor[3] = alphaComponent;
  66. GPUVector4 backgroundColor = {redComponent, greenComponent, blueComponent, alphaComponent};
  67. [self setVec4:backgroundColor forUniform:backgroundColorUniform program:filterProgram];
  68. }
  69. @end