-
Notifications
You must be signed in to change notification settings - Fork 1
/
Filter.h
322 lines (272 loc) · 14 KB
/
Filter.h
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
namespace Filter {
enum Color {
RGB = 3,
RGBA = 4
};
enum Type {
None = 0,
Sub = 1,
Up = 2,
Average = 3,
Paeth = 4,
Adaptive = 5
};
const static int MAX_THREAD = 8;
const static int THREAD = 4;
inline unsigned char predictor(unsigned char a, unsigned char b, unsigned char c) {
int p = a+b-c;
int pa = abs(p-a);
int pb = abs(p-b);
int pc = abs(p-c);
return (pa<=pb&&pa<=pc)?a:((pb<=pc)?b:c);
}
class Base {
protected:
dispatch_group_t _group = dispatch_group_create();
dispatch_queue_t _queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0);
unsigned int _width;
unsigned int _height;
unsigned int _bpp;
unsigned char *_bytes = nullptr;
unsigned int _length = 0;
unsigned char _blank[4] = {0,0,0,0};
public:
unsigned int width() { return this->_width; }
unsigned int height() { return this->_height; }
unsigned int bpp() { return this->_bpp; }
unsigned char *bytes() { return this->_bytes; }
unsigned int length() { return this->_length; }
Base(unsigned int w, unsigned int h, unsigned int bpp) {
this->_width = w;
this->_height = h;
this->_bpp = bpp;
this->_length = this->_height*(this->_width+1)*this->_bpp;
this->_bytes = new unsigned char[this->_length];
}
~Base() {
delete[] this->_bytes;
}
};
class Encoder : public Base {
protected:
unsigned char *_filters[5] = {nullptr,nullptr,nullptr,nullptr,nullptr};
public:
Encoder(unsigned int w, unsigned int h, Filter::Color bpp) : Base(w,h,bpp) {
for(int f=1; f<5; f++) this->_filters[f] = new unsigned char[this->_width*this->_bpp];
}
~Encoder() {
for(int f=1; f<5; f++) {
delete[] this->_filters[f];
}
}
void encode(unsigned char *buffer, Filter::Color ch, Filter::Type filter, unsigned int begin, unsigned int end) {
unsigned int src_row = this->_width*ch;
unsigned int dst_row = this->_width*this->_bpp;
bool eq = (ch==this->_bpp)?true:false;
int num = (this->_bpp>ch)?ch:this->_bpp;
for(int i=begin; i<end; i++) {
unsigned char *src = buffer+i*src_row;
unsigned char *dst = this->_bytes+i*(dst_row+1)+1;
unsigned int type = filter;
if(type==Filter::Type::Sub) {
for(int j=0; j<this->_width; j++) {
unsigned char *W = (j==0)?this->_blank:buffer+i*src_row+(j-1)*ch;
for(int n=0; n<num; n++) *dst++ = ((*src++)-(*W++))&0xFF;
if(!eq) {
if(this->_bpp<ch) src++;
else if(this->_bpp>ch) *dst++ = 0xFF;
}
}
}
else if(type==Filter::Type::Up) {
for(int j=0; j<this->_width; j++) {
unsigned char *N = (i==0)?this->_blank:buffer+(i-1)*src_row+j*ch;
for(int n=0; n<num; n++) *dst++ = ((*src++)-(*N++))&0xFF;
if(!eq) {
if(this->_bpp<ch) src++;
else if(this->_bpp>ch) *dst++ = 0xFF;
}
}
}
else if(type==Filter::Type::Average) {
for(int j=0; j<this->_width; j++) {
unsigned char *W = (j==0)?this->_blank:buffer+i*src_row+(j-1)*ch;
unsigned char *N = (i==0)?this->_blank:buffer+(i-1)*src_row+j*ch;
for(int n=0; n<num; n++) *dst++ = ((*src++)-(((*W++)+(*N++))>>1))&0xFF;
if(!eq) {
if(this->_bpp<ch) src++;
else if(this->_bpp>ch) *dst++ = 0xFF;
}
}
}
else if(type==Filter::Type::Paeth) {
for(int j=0; j<this->_width; j++) {
unsigned char *W = (j==0)?this->_blank:buffer+i*src_row+(j-1)*ch;
unsigned char *N = (i==0)?this->_blank:buffer+(i-1)*src_row+j*ch;
unsigned char *NW = (i==0||j==0)?this->_blank:buffer+(i-1)*src_row+(j-1)*ch;
for(int n=0; n<num; n++) *dst++ = ((*src++)-predictor((*W++),(*N++),(*NW++)))&0xFF;
if(!eq) {
if(this->_bpp<ch) src++;
else if(this->_bpp>ch) *dst++ = 0xFF;
}
}
}
else if(type==Filter::Type::Adaptive) {
for(int j=0; j<this->_width; j++) {
unsigned char *W = (j==0)?this->_blank:buffer+i*src_row+(j-1)*ch;
unsigned char *N = (i==0)?this->_blank:buffer+(i-1)*src_row+j*ch;
unsigned char *NW = (i==0||j==0)?this->_blank:buffer+(i-1)*src_row+(j-1)*ch;
for(int n=0; n<num; n++) {
this->_filters[1][j*this->_bpp+n] = (*src-*W)&0xFF;
this->_filters[2][j*this->_bpp+n] = (*src-*N)&0xFF;
this->_filters[3][j*this->_bpp+n] = (*src-((*W+*N)>>1))&0xFF;
this->_filters[4][j*this->_bpp+n] = (*src-predictor(*W,*N,*NW))&0xFF;
src++;
W++;
N++;
NW++;
}
if(!eq) {
if(this->_bpp<ch) src++;
else if(this->_bpp>ch) *dst++ = 0xFF;
}
}
const int INDEX = 0, VALUE = 1;
int best_filter[2] = { 1, 0x7FFFFFFF };
for(int f=1; f<5; f++) {
for(int n=0; n<num; n++) {
int est = 0;
for(int j=0; j<this->_width; j++) {
est+=abs((char)this->_filters[f][j*this->_bpp+n]);
}
if(est<best_filter[VALUE]) {
best_filter[INDEX] = f;
best_filter[VALUE] = est;
}
}
}
type = best_filter[INDEX];
for(int j=0; j<this->_width; j++) {
for(int n=0; n<num; n++) {
*dst++ = this->_filters[type][j*this->_bpp+n];
}
if(!eq) {
if(this->_bpp<ch) src++;
else if(this->_bpp>ch) *dst++ = 0xFF;
}
}
}
else { // Filter::Type::None
for(int j=0; j<this->_width; j++) {
for(int n=0; n<num; n++) *dst++ = *src++;
if(!eq) {
if(this->_bpp<ch) src++;
else if(this->_bpp>ch) *dst++ = 0xFF;
}
}
}
this->_bytes[i*(this->_width*this->_bpp+1)+0] = (unsigned char)type;
}
}
void encode(unsigned char *buffer,Filter::Color ch,Filter::Type filter) {
this->_length = this->_height*(this->_width+1)*this->_bpp;
if(Filter::THREAD==0) {
this->encode(buffer,ch,filter,0,this->_height);
}
else {
unsigned char thread = Filter::THREAD>=Filter::MAX_THREAD?Filter::MAX_THREAD:Filter::THREAD;
int step = this->_height/thread;
for(int n=0; n<thread; n++) {
unsigned int begin = step*n;
unsigned int end = (n<thread-1)?step*(n+1):this->_height;
dispatch_group_async(this->_group,_queue,^{
this->encode(buffer,ch,filter,begin,end);
});
}
dispatch_group_wait(this->_group,DISPATCH_TIME_FOREVER);
}
}
};
class Decoder : public Base {
public:
Decoder(unsigned int w, unsigned int h, Filter::Color bpp) : Base(w,h,bpp) {
}
~Decoder() {
}
void decode(unsigned char *buffer, Filter::Color ch, unsigned int begin, unsigned int end) {
int len = this->_height*(this->_width+1)*this->_bpp;
for(int k=0; k<len; k++) this->_bytes[k] = 0;
unsigned int src_row = this->_width*ch;
unsigned int dst_row = this->_width*this->_bpp;
int num = (this->_bpp>ch)?ch:this->_bpp;
for(int i=0; i<this->_height; i++) {
unsigned char *src = buffer+i*(src_row+1);
unsigned char *dst = this->_bytes+i*dst_row;
unsigned int filter = *src++;
if(filter>=Filter::Type::Adaptive+1) {
return;
}
if(filter==Filter::Type::Sub) {
for(int j=0; j<this->_width; j++) {
unsigned char *W = (j==0)?this->_blank:this->_bytes+i*dst_row+(j-1)*this->_bpp;
for(int n=0; n<num; n++) *dst++ = ((*src++)+(*W++))&0xFF;
if(this->_bpp<ch) src++;
else if(this->_bpp>ch) *dst++ = 0xFF;
}
}
else if(filter==Filter::Type::Up) {
for(int j=0; j<this->_width; j++) {
unsigned char *N = (i==0)?this->_blank:this->_bytes+(i-1)*dst_row+j*this->_bpp;
for(int n=0; n<num; n++) *dst++ = ((*src++)+(*N++))&0xFF;
if(this->_bpp<ch) src++;
else if(this->_bpp>ch) *dst++ = 0xFF;
}
}
else if(filter==Filter::Type::Average) {
for(int j=0; j<this->_width; j++) {
unsigned char *W = (j==0)?this->_blank:this->_bytes+i*dst_row+(j-1)*this->_bpp;
unsigned char *N = (i==0)?this->_blank:this->_bytes+(i-1)*dst_row+j*this->_bpp;
for(int n=0; n<num; n++) *dst++ = ((*src++)+(((*W++)+(*N++))>>1))&0xFF;
if(this->_bpp<ch) src++;
else if(this->_bpp>ch) *dst++ = 0xFF;
}
}
else if(filter==Filter::Type::Paeth) {
for(int j=0; j<this->_width; j++) {
unsigned char *W = (j==0)?this->_blank:this->_bytes+i*dst_row+(j-1)*this->_bpp;
unsigned char *N = (i==0)?this->_blank:this->_bytes+(i-1)*dst_row+j*this->_bpp;
unsigned char *NW = (i==0||j==0)?this->_blank:this->_bytes+(i-1)*dst_row+(j-1)*this->_bpp;
for(int n=0; n<num; n++) *dst++ = ((*src++)+predictor((*W++),(*N++),(*NW++)))&0xFF;
if(this->_bpp<ch) src++;
else if(this->_bpp>ch) *dst++ = 0xFF;
}
}
else { // Filter::Type::None
for(int j=0; j<this->_width; j++) {
for(int n=0; n<num; n++) *dst++ = *src++;
if(this->_bpp<ch) src++;
else if(this->_bpp>ch) *dst++ = 0xFF;
}
}
}
}
void decode(unsigned char *buffer, Filter::Color ch) {
this->_length = this->_width*this->_height*this->_bpp;
if(Filter::THREAD==0) {
this->decode(buffer,ch,0,this->_height);
}
else {
unsigned char thread = Filter::THREAD>=Filter::MAX_THREAD?Filter::MAX_THREAD:Filter::THREAD;
int step = this->_height/thread;
for(int n=0; n<thread; n++) {
unsigned int begin = step*n;
unsigned int end = (n<thread-1)?step*(n+1):this->_height;
dispatch_group_async(this->_group,_queue,^{
this->decode(buffer,ch,begin,end);
});
}
dispatch_group_wait(this->_group,DISPATCH_TIME_FOREVER);
}
}
};
};