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
//
//  GPUImageMosaicFilter.m
 
 
#import "GPUImageMosaicFilter.h"
#import "GPUImagePicture.h"
 
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
NSString *const kGPUImageMosaicFragmentShaderString = SHADER_STRING
(
 precision highp float;
 
 varying vec2 textureCoordinate;
 
 uniform sampler2D inputImageTexture;
 uniform sampler2D inputImageTexture2;
 
 uniform vec2 inputTileSize;
 uniform vec2 displayTileSize;
 uniform float numTiles;
 uniform int colorOn;
 
 void main()
 {
     vec2 xy = textureCoordinate;
     xy = xy - mod(xy, displayTileSize);
     
     vec4 lumcoeff = vec4(0.299,0.587,0.114,0.0);
     
     vec4 inputColor = texture2D(inputImageTexture2, xy);
     float lum = dot(inputColor,lumcoeff);
     lum = 1.0 - lum;
     
     float stepsize = 1.0 / numTiles;
     float lumStep = (lum - mod(lum, stepsize)) / stepsize; 
  
     float rowStep = 1.0 / inputTileSize.x;
     float x = mod(lumStep, rowStep);
     float y = floor(lumStep / rowStep);
     
     vec2 startCoord = vec2(float(x) *  inputTileSize.x, float(y) * inputTileSize.y);
     vec2 finalCoord = startCoord + ((textureCoordinate - xy) * (inputTileSize / displayTileSize));
     
     vec4 color = texture2D(inputImageTexture, finalCoord);   
     if (colorOn == 1) {
         color = color * inputColor;
     }
     gl_FragColor = color; 
     
 }  
);
#else
NSString *const kGPUImageMosaicFragmentShaderString = SHADER_STRING
(
 varying vec2 textureCoordinate;
 
 uniform sampler2D inputImageTexture;
 uniform sampler2D inputImageTexture2;
 
 uniform vec2 inputTileSize;
 uniform vec2 displayTileSize;
 uniform float numTiles;
 uniform int colorOn;
 
 void main()
 {
     vec2 xy = textureCoordinate;
     xy = xy - mod(xy, displayTileSize);
     
     vec4 lumcoeff = vec4(0.299,0.587,0.114,0.0);
     
     vec4 inputColor = texture2D(inputImageTexture2, xy);
     float lum = dot(inputColor,lumcoeff);
     lum = 1.0 - lum;
     
     float stepsize = 1.0 / numTiles;
     float lumStep = (lum - mod(lum, stepsize)) / stepsize;
     
     float rowStep = 1.0 / inputTileSize.x;
     float x = mod(lumStep, rowStep);
     float y = floor(lumStep / rowStep);
     
     vec2 startCoord = vec2(float(x) *  inputTileSize.x, float(y) * inputTileSize.y);
     vec2 finalCoord = startCoord + ((textureCoordinate - xy) * (inputTileSize / displayTileSize));
     
     vec4 color = texture2D(inputImageTexture, finalCoord);
     if (colorOn == 1) {
         color = color * inputColor;
     }
     gl_FragColor = color;
 }
);
#endif
 
@implementation GPUImageMosaicFilter
 
@synthesize inputTileSize = _inputTileSize, numTiles = _numTiles, displayTileSize = _displayTileSize, colorOn = _colorOn;
@synthesize tileSet = _tileSet;
 
- (id)init;
{
    if (!(self = [super initWithFragmentShaderFromString:kGPUImageMosaicFragmentShaderString]))
    {
        return nil;
    }
    
    inputTileSizeUniform = [filterProgram uniformIndex:@"inputTileSize"];
    displayTileSizeUniform = [filterProgram uniformIndex:@"displayTileSize"];
    numTilesUniform = [filterProgram uniformIndex:@"numTiles"];
    colorOnUniform = [filterProgram uniformIndex:@"colorOn"];
    
    CGSize its = CGSizeMake(0.125, 0.125);
    CGSize dts = CGSizeMake(0.025, 0.025);
    [self setDisplayTileSize:dts];
    [self setInputTileSize:its];
    [self setNumTiles:64.0];
    [self setColorOn:YES];
    //[self setTileSet:@"squares.png"];
    return self;
}
 
- (void)setColorOn:(BOOL)yes
{
    glUniform1i(colorOnUniform, yes);
}
 
- (void)setNumTiles:(float)numTiles
{
 
    _numTiles = numTiles;
    [self setFloat:_numTiles forUniformName:@"numTiles"];
}
 
- (void)setInputTileSize:(CGSize)inputTileSize
{
    if (inputTileSize.width > 1.0) {
        _inputTileSize.width = 1.0;
    } 
    if (inputTileSize.height > 1.0) {
        _inputTileSize.height = 1.0;
    }
    if (inputTileSize.width < 0.0) {
        _inputTileSize.width = 0.0;
    }
    if (inputTileSize.height < 0.0) {
        _inputTileSize.height = 0.0;
    }
    
    
    _inputTileSize = inputTileSize;
    
    [self setSize:_inputTileSize forUniform:inputTileSizeUniform program:filterProgram];    
}
 
-(void)setDisplayTileSize:(CGSize)displayTileSize
{
    if (displayTileSize.width > 1.0) {
        _displayTileSize.width = 1.0;
    } 
    if (displayTileSize.height > 1.0) {
        _displayTileSize.height = 1.0;
    }
    if (displayTileSize.width < 0.0) {
        _displayTileSize.width = 0.0;
    }
    if (displayTileSize.height < 0.0) {
        _displayTileSize.height = 0.0;
    }
    
    
    _displayTileSize = displayTileSize;
    
    [self setSize:_displayTileSize forUniform:displayTileSizeUniform program:filterProgram];
}
 
-(void)setTileSet:(NSString *)tileSet
{
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
    UIImage *img = [UIImage imageNamed:tileSet];
#else
    NSImage *img = [NSImage imageNamed:tileSet];
#endif
    pic = [[GPUImagePicture alloc] initWithImage:img smoothlyScaleOutput:YES];
    [pic addTarget:self];
    [pic processImage];
}
 
@end