developer
2023-05-20 e12c7b4c22df631ebdcd16b2f98fbef8f738f92f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//
//  GPUImageHistogramEqualizationFilter.m
//  FilterShowcase
//
//  Created by Adam Marcus on 19/08/2014.
//  Copyright (c) 2014 Sunset Lake Software LLC. All rights reserved.
//
 
#import "GPUImageHistogramEqualizationFilter.h"
 
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
NSString *const kGPUImageRedHistogramEqualizationFragmentShaderString = SHADER_STRING
(
 varying highp vec2 textureCoordinate;
 uniform sampler2D inputImageTexture;
 uniform sampler2D inputImageTexture2;
 
 void main()
 {
     lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
     lowp float redCurveValue = texture2D(inputImageTexture2, vec2(textureColor.r, 0.0)).r;
     
     gl_FragColor = vec4(redCurveValue, textureColor.g, textureColor.b, textureColor.a);
 }
 );
#else
NSString *const kGPUImageRedHistogramEqualizationFragmentShaderString = SHADER_STRING
(
 varying vec2 textureCoordinate;
 uniform sampler2D inputImageTexture;
 uniform sampler2D inputImageTexture2;
 
 void main()
 {
     vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
     float redCurveValue = texture2D(inputImageTexture2, vec2(textureColor.r, 0.0)).r;
     
     gl_FragColor = vec4(redCurveValue, textureColor.g, textureColor.b, textureColor.a);
 }
 );
#endif
 
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
NSString *const kGPUImageGreenHistogramEqualizationFragmentShaderString = SHADER_STRING
(
 varying highp vec2 textureCoordinate;
 uniform sampler2D inputImageTexture;
 uniform sampler2D inputImageTexture2;
 
 void main()
 {
     lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
     lowp float greenCurveValue = texture2D(inputImageTexture2, vec2(textureColor.g, 0.0)).g;
     
     gl_FragColor = vec4(textureColor.r, greenCurveValue, textureColor.b, textureColor.a);
 }
 );
#else
NSString *const kGPUImageGreenHistogramEqualizationFragmentShaderString = SHADER_STRING
(
 varying vec2 textureCoordinate;
 uniform sampler2D inputImageTexture;
 uniform sampler2D inputImageTexture2;
 
 void main()
 {
     vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
     float greenCurveValue = texture2D(inputImageTexture2, vec2(textureColor.g, 0.0)).g;
     
     gl_FragColor = vec4(textureColor.r, greenCurveValue, textureColor.b, textureColor.a);
 }
 );
#endif
 
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
NSString *const kGPUImageBlueHistogramEqualizationFragmentShaderString = SHADER_STRING
(
 varying highp vec2 textureCoordinate;
 uniform sampler2D inputImageTexture;
 uniform sampler2D inputImageTexture2;
 
 void main()
 {
     lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
     lowp float blueCurveValue = texture2D(inputImageTexture2, vec2(textureColor.b, 0.0)).b;
     
     gl_FragColor = vec4(textureColor.r, textureColor.g, blueCurveValue, textureColor.a);
 }
 );
#else
NSString *const kGPUImageBlueHistogramEqualizationFragmentShaderString = SHADER_STRING
(
 varying vec2 textureCoordinate;
 uniform sampler2D inputImageTexture;
 uniform sampler2D inputImageTexture2;
 
 void main()
 {
     vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
     float blueCurveValue = texture2D(inputImageTexture2, vec2(textureColor.b, 0.0)).b;
     
     gl_FragColor = vec4(textureColor.r, textureColor.g, blueCurveValue, textureColor.a);
 }
 );
#endif
 
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
NSString *const kGPUImageRGBHistogramEqualizationFragmentShaderString = SHADER_STRING
(
 varying highp vec2 textureCoordinate; 
 uniform sampler2D inputImageTexture;
 uniform sampler2D inputImageTexture2;
 
 void main()
 {
     lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
     lowp float redCurveValue = texture2D(inputImageTexture2, vec2(textureColor.r, 0.0)).r;
     lowp float greenCurveValue = texture2D(inputImageTexture2, vec2(textureColor.g, 0.0)).g;
     lowp float blueCurveValue = texture2D(inputImageTexture2, vec2(textureColor.b, 0.0)).b;
     
     gl_FragColor = vec4(redCurveValue, greenCurveValue, blueCurveValue, textureColor.a);
 }
 );
#else
NSString *const kGPUImageRGBHistogramEqualizationFragmentShaderString = SHADER_STRING
(
 varying vec2 textureCoordinate;
 uniform sampler2D inputImageTexture;
 uniform sampler2D inputImageTexture2;
 
 void main()
 {
     vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
     float redCurveValue = texture2D(inputImageTexture2, vec2(textureColor.r, 0.0)).r;
     float greenCurveValue = texture2D(inputImageTexture2, vec2(textureColor.g, 0.0)).g;
     float blueCurveValue = texture2D(inputImageTexture2, vec2(textureColor.b, 0.0)).b;
     
     gl_FragColor = vec4(redCurveValue, greenCurveValue, blueCurveValue, textureColor.a);
 }
 );
#endif
 
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
NSString *const kGPUImageLuminanceHistogramEqualizationFragmentShaderString = SHADER_STRING
(
 varying highp vec2 textureCoordinate;
 uniform sampler2D inputImageTexture;
 uniform sampler2D inputImageTexture2;
 
 const lowp vec3 W = vec3(0.2125, 0.7154, 0.0721);
 
 void main()
 {
     lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
     lowp float luminance = dot(textureColor.rgb, W);
     lowp float newLuminance = texture2D(inputImageTexture2, vec2(luminance, 0.0)).r;
     lowp float deltaLuminance = newLuminance - luminance;
     
     lowp float red   = clamp(textureColor.r + deltaLuminance, 0.0, 1.0);
     lowp float green = clamp(textureColor.g + deltaLuminance, 0.0, 1.0);
     lowp float blue  = clamp(textureColor.b + deltaLuminance, 0.0, 1.0);
 
     gl_FragColor = vec4(red, green, blue, textureColor.a);
 }
 );
#else
NSString *const kGPUImageLuminanceHistogramEqualizationFragmentShaderString = SHADER_STRING
(
 varying vec2 textureCoordinate;
 uniform sampler2D inputImageTexture;
 uniform sampler2D inputImageTexture2;
 
 const vec3 W = vec3(0.2125, 0.7154, 0.0721);
 
 void main()
 {
     vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
     float luminance = dot(textureColor.rgb, W);
     float newLuminance = texture2D(inputImageTexture2, vec2(luminance, 0.0)).r;
     float deltaLuminance = newLuminance - luminance;
     
     float red   = clamp(textureColor.r + deltaLuminance, 0.0, 1.0);
     float green = clamp(textureColor.g + deltaLuminance, 0.0, 1.0);
     float blue  = clamp(textureColor.b + deltaLuminance, 0.0, 1.0);
     
     gl_FragColor = vec4(red, green, blue, textureColor.a);
 }
 );
#endif
 
@implementation GPUImageHistogramEqualizationFilter
 
@synthesize downsamplingFactor = _downsamplingFactor;
 
#pragma mark -
#pragma mark Initialization
 
- (id)init;
{
    if (!(self = [self initWithHistogramType:kGPUImageHistogramRGB]))
    {
        return nil;
    }
    
    return self;
}
 
- (id)initWithHistogramType:(GPUImageHistogramType)newHistogramType
{
    if (!(self = [super init]))
    {
        return nil;
    }
    
    histogramFilter = [[GPUImageHistogramFilter alloc] initWithHistogramType:newHistogramType];
    [self addFilter:histogramFilter];
    
    GLubyte dummyInput[4 * 256]; // NB: No way to initialise GPUImageRawDataInput without providing bytes
    rawDataInputFilter = [[GPUImageRawDataInput alloc] initWithBytes:dummyInput size:CGSizeMake(256.0, 1.0) pixelFormat:GPUPixelFormatBGRA type:GPUPixelTypeUByte];
    rawDataOutputFilter = [[GPUImageRawDataOutput alloc] initWithImageSize:CGSizeMake(256.0, 3.0) resultsInBGRAFormat:YES];
    
    __unsafe_unretained GPUImageRawDataOutput *_rawDataOutputFilter = rawDataOutputFilter;
    __unsafe_unretained GPUImageRawDataInput *_rawDataInputFilter = rawDataInputFilter;
    [rawDataOutputFilter setNewFrameAvailableBlock:^{
        
        unsigned int histogramBins[3][256];
        
        [_rawDataOutputFilter lockFramebufferForReading];
        
        GLubyte *data  = [_rawDataOutputFilter rawBytesForImage];
        data += [_rawDataOutputFilter bytesPerRowInOutput];
 
        histogramBins[0][0] = *data++;
        histogramBins[1][0] = *data++;
        histogramBins[2][0] = *data++;
        data++;
        
        for (unsigned int x = 1; x < 256; x++) {
            histogramBins[0][x] = histogramBins[0][x-1] + *data++;
            histogramBins[1][x] = histogramBins[1][x-1] + *data++;
            histogramBins[2][x] = histogramBins[2][x-1] + *data++;
            data++;
        }
        
        [_rawDataOutputFilter unlockFramebufferAfterReading];
 
        GLubyte colorMapping[4 * 256];
        GLubyte *_colorMapping = colorMapping;
        
        for (unsigned int x = 0; x < 256; x++) {
            *_colorMapping++ = (GLubyte) (((histogramBins[0][x] - histogramBins[0][0]) * 255) / histogramBins[0][255]);
            *_colorMapping++ = (GLubyte) (((histogramBins[1][x] - histogramBins[1][0]) * 255) / histogramBins[1][255]);
            *_colorMapping++ = (GLubyte) (((histogramBins[2][x] - histogramBins[2][0]) * 255) / histogramBins[2][255]);
            *_colorMapping++ = 255;
        }
        
        _colorMapping = colorMapping;
        [_rawDataInputFilter updateDataFromBytes:_colorMapping size:CGSizeMake(256.0, 1.0)];
        [_rawDataInputFilter processData];
    }];
    [histogramFilter addTarget:rawDataOutputFilter];
    
    NSString *fragmentShader = nil;
    switch (newHistogramType) {
        case kGPUImageHistogramRed:
            fragmentShader = kGPUImageRedHistogramEqualizationFragmentShaderString;
            break;
        case kGPUImageHistogramGreen:
            fragmentShader = kGPUImageGreenHistogramEqualizationFragmentShaderString;
            break;
        case kGPUImageHistogramBlue:
            fragmentShader = kGPUImageBlueHistogramEqualizationFragmentShaderString;
            break;
        default:
        case kGPUImageHistogramRGB:
            fragmentShader = kGPUImageRGBHistogramEqualizationFragmentShaderString;
            break;
        case kGPUImageHistogramLuminance:
            fragmentShader = kGPUImageLuminanceHistogramEqualizationFragmentShaderString;
            break;
    }
    GPUImageFilter *equalizationFilter = [[GPUImageTwoInputFilter alloc] initWithFragmentShaderFromString:fragmentShader];
    [rawDataInputFilter addTarget:equalizationFilter atTextureLocation:1];
    
    [self addFilter:equalizationFilter];
    
    self.initialFilters = [NSArray arrayWithObjects:histogramFilter, equalizationFilter, nil];
    self.terminalFilter = equalizationFilter;
    
    self.downsamplingFactor = 16;
    
    return self;
}
 
#pragma mark -
#pragma mark Accessors
 
- (void)setDownsamplingFactor:(NSUInteger)newValue;
{
    if (_downsamplingFactor != newValue)
    {
        _downsamplingFactor = newValue;
        histogramFilter.downsamplingFactor = newValue;
    }
}
 
@end