-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathafterfilter.txt
395 lines (354 loc) · 7.33 KB
/
afterfilter.txt
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include "return_codes.h"
#include <malloc.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define ZLIB
#if defined (ZLIB)
#include <zlib.h>
#elif defined (LIBDEFLATE)
#include <libdeflate.h>
#elif defined (ISAL)
#include <include/igzip_lib.h>
#else
#error "Did't choose any right lib"
#endif
#define START_BUFF_LEN 32
typedef union
{
unsigned int converted;
unsigned char mini[4];
} ConverterCharsToInt;
typedef struct
{
unsigned int height;
unsigned int width;
unsigned char bitdepth;
unsigned char typeofcolor;
} MetaOfAllPNG;
typedef struct
{
size_t size;
size_t realsize;
} MetaOfBuf;
bool skipUselessInf(FILE *f, unsigned int bytestoskip)
{
unsigned char tmp[1];
for (unsigned int i = 0; i < bytestoskip; i++)
{
if (fread(tmp, sizeof(unsigned char), 1, f) != 1)
{
return false;
}
}
return true;
}
bool readBufOfChars(FILE *f, unsigned char *buf, size_t size)
{
return fread(buf, sizeof(unsigned char), size, f) == size;
}
bool oneByteToShort(FILE *f, unsigned char *future)
{
unsigned char a[1];
if (!readBufOfChars(f, a, 1))
{
return false;
}
*future = a[0];
return true;
}
bool fourBytesToInt(FILE *f, unsigned int *futureint)
{
ConverterCharsToInt m;
unsigned char tmp;
if (!readBufOfChars(f, m.mini, 4))
{
return false;
}
for (int i = 0; i < 2; i++)
{
tmp = m.mini[i];
m.mini[i] = m.mini[3 - i];
m.mini[3 - i] = tmp;
}
*futureint = m.converted;
return true;
}
bool nameOfChank(FILE *f, unsigned char *futurename)
{
return readBufOfChars(f, futurename, 4);
}
bool resize(unsigned char **buf, MetaOfBuf *met, unsigned int tolen)
{
while (met->size - met->realsize < tolen + 2)
{
*buf = realloc(*buf, sizeof(unsigned char) * met->size * 2);
if (*buf == NULL)
{
return false;
}
met->size *= 2;
}
return true;
}
int defillteringOneHe(unsigned char *buf, size_t *currpos, MetaOfAllPNG meta)
{
int lenofpixel = (int) (meta.typeofcolor + 1);
size_t i = 1;
size_t placetowork = meta.width * lenofpixel + 1;
if (buf[*currpos] == 0x00) //None
{
*currpos += placetowork;
return 0;
} else if (buf[*currpos] == 0x01) //sub
{
while (i < placetowork)
{
if (i >= lenofpixel + 1)
{
buf[i + *currpos] += buf[i + *currpos - lenofpixel];
}
i++;
}
*currpos += placetowork;
return 0;
} else if (buf[*currpos] == 0x02) //Up
{
while (i < placetowork)
{
if (*currpos - placetowork >= 0)
{
buf[i + *currpos] += buf[i + *currpos - (placetowork)];
} else {
break;
}
i++;
}
*currpos += placetowork;
return 0;
} else if (buf[*currpos] == 0x03) //Average
{
int tmp;
while (i < placetowork)
{
tmp = 0;
if (*currpos - placetowork >= 0)
{
tmp += buf[i + *currpos - placetowork];
}
if (i >= lenofpixel + 1)
{
tmp += buf[i + *currpos - lenofpixel];
}
buf[i + *currpos] += (unsigned char) floor(tmp / 2);
i++;
}
*currpos += placetowork;
return 0;
} else if (buf[*currpos] == 0x04) //Paeth
{
int a, b, c, pa, pc, pb;
while (i < placetowork)
{
a = b = c = 0;
if (i >= lenofpixel + 1)
{
a = buf[i + *currpos - lenofpixel];
}
if (*currpos - placetowork >= 0)
{
b = buf[i + *currpos - placetowork];
}
if ((*currpos - placetowork >= 0) && (i >= lenofpixel + 1))
{
c = buf[i + *currpos - placetowork - lenofpixel];
} else {
buf[i + *currpos] += a + b + c;
i++;
continue;
}
pa = abs(b - c);
pb = abs(a - c);
pc = abs(a + b - 2 * c);
if (pa <= pb && pa <= pc)
{
buf[i + *currpos] += a;
} else if (pb <= pc)
{
buf[i + *currpos] += b;
} else
{
buf[i + *currpos] += c;
}
i++;
}
*currpos += placetowork;
return 0;
}
return -1;
}
// -1 - bad news
// 0 - continue
// 1 - the logical end
int readAllChunk(FILE *f, unsigned char **buf, MetaOfAllPNG *meta, MetaOfBuf *mbuf)
{
unsigned char name[5];
unsigned int currlen = 0;
if (!(fourBytesToInt(f, &currlen) && nameOfChank(f, name)))
{
return -1;
}
if (strcmp((char *)name, "IDAT\0") == 0)
{
if (!resize(buf, mbuf, currlen))
{
return -1;
}
if ( !readBufOfChars(f, *buf + mbuf->realsize, currlen)) {
return -1;
}
mbuf->realsize += currlen;
}
else if (strcmp((char *)name, "IEND\0") == 0)
{
return 1;
}
else if (strcmp((char *)name, "IHDR\0") == 0)
{
bool res = true;
res &= fourBytesToInt(f, &meta->width);
res &= fourBytesToInt(f, &meta->height);
res &= oneByteToShort(f, &meta->bitdepth);
res &= oneByteToShort(f, &meta->typeofcolor);
res &= skipUselessInf(f, 3);
if (!res)
{
return -1;
}
}
else
{
if (!skipUselessInf(f, currlen))
{
return -1;
}
}
// skip control sum bytes
if (!skipUselessInf(f, 4))
{
return -1;
}
return 0;
}
bool checkPNGFormat(FILE *f)
{
unsigned char mini[5];
unsigned char correctpngheader[5] = { 137, 'P', 'N', 'G', 0 };
if (!readBufOfChars(f, mini, 4) || !skipUselessInf(f, 4))
{
return false;
}
return strcmp((char *)mini, (char *)correctpngheader) == 0;
}
int main(int argc, char *argv[])
{
if (argc != 3)
{
perror("Too small params in input!");
return ERROR_INVALID_PARAMETER;
}
FILE *image = fopen(*(argv + 1), "rb");
if (image == NULL)
{
perror("Couldn't open file!");
return ERROR_FILE_NOT_FOUND;
}
if (!checkPNGFormat(image))
{
perror("Not PNG file!");
return ERROR_INVALID_DATA;
}
MetaOfAllPNG metapng = { .height = 0, .width = 0, .bitdepth = 0, .typeofcolor = 0 };
MetaOfBuf metaBuf = { .size = START_BUFF_LEN, .realsize = 0 };
unsigned char *buffer = malloc(sizeof(char) * START_BUFF_LEN);
if (buffer == NULL)
{
perror("Problems with memory!");
fclose(image);
return ERROR_MEMORY;
}
int error;
do
{
error = readAllChunk(image, &buffer, &metapng, &metaBuf);
} while (error == 0);
if (error == -1)
{
perror("This file is broken or was not enought memory to use!");
free(buffer);
fclose(image);
return ERROR_INVALID_DATA;
}
uLongf uncompesedlen = (uLongf)(metapng.width * metapng.height * (metapng.typeofcolor + 1) + metapng.height);
unsigned char *allundata = malloc(sizeof(unsigned char) * uncompesedlen);
if (allundata == NULL)
{
perror("Memory error!");
free(buffer);
fclose(image);
return ERROR_NOT_ENOUGH_MEMORY;
}
//decompress
if (uncompress(allundata, &uncompesedlen, buffer, (uLong) metaBuf.realsize) != Z_OK)
{
perror("This file is broken!\n");
free(buffer);
free(allundata);
fclose(image);
return ERROR_INVALID_DATA;
}
free(buffer);
fclose(image);
//defiltering
size_t pos = 0;
do {
error = defillteringOneHe(allundata, &pos, metapng);
} while (error == 0 && pos < uncompesedlen);
if (error == -1) {
perror("Bag in filter!");
free(allundata);
return ERROR_UNKNOWN;
}
// pnm writing
image = NULL;
image = fopen(argv[2], "wb");
if (image == NULL)
{
perror("Problems with output file");
free(allundata);
return ERROR_UNKNOWN;
}
if (metapng.typeofcolor == 0)
{
fprintf(image, "P5\n");
}
else
{
fprintf(image, "P6\n");
}
fprintf(image, "%i %i\n255\n", metapng.width, metapng.height);
for (uLongf i = 1; i < uncompesedlen; i += metapng.width * (metapng.typeofcolor + 1) + 1)
{
if (fwrite(&allundata[i], 1, metapng.width * (metapng.typeofcolor + 1), image) != metapng.width * (metapng.typeofcolor + 1))
{
perror("Cannot write some bytes");
fclose(image);
free(allundata);
return ERROR_MEMORY;
}
}
fclose(image);
free(allundata);
return ERROR_SUCCESS;
}