-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpng2bmp_debug.cpp
More file actions
1313 lines (1073 loc) · 33.9 KB
/
Copy pathpng2bmp_debug.cpp
File metadata and controls
1313 lines (1073 loc) · 33.9 KB
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// png2bmp
// uses "Miniz" as Zlib library.
// see https://github.com/richgel999/miniz
#include "stdafx.h"
#include <stdlib.h>
//#include "crc32.hpp"
#include <Windows.h>
#include <vector>
#include "miniz.h"
#pragma warning(disable : 4996)
//Macro
#define SFPS(pos) SetFilePointer(f, pos, 0, FILE_BEGIN)
#define SFPC(pos) SetFilePointer(f, pos, 0, FILE_CURRENT)
#define READ(v) ReadFile(f, &(v), sizeof(v), &a, NULL)
#define READBE4(v) ReadFile(f, &(v), sizeof(v), &a, NULL); \
v = SwapFourBytes((v))
#define READP(p, n) ReadFile(f, p, n, &a, NULL)
#define WRITE(v) WriteFile(f2, &(v), sizeof(v), &a, NULL)
#define WRITEP(p, n) WriteFile(f2, p, n, &a, NULL)
#define SwapFourBytes(data) \
( (((data) >> 24) & 0x000000FF) | (((data) >> 8) & 0x0000FF00) | \
(((data) << 8) & 0x00FF0000) | (((data) << 24) & 0xFF000000) )
//typedef unsigned int UINT;
typedef struct {
UINT len;
UINT type;
UINT start_at;
UINT crc;
} png_chunk_t;
enum PNG_COLOR_TYPE
{
PNG_COLOR_GRAYSCALE,
PNG_COLOR_RGB = 2,
PNG_COLOR_PALETTE,
PNG_COLOR_GRAYSCALE_ALPHA,
PNG_COLOR_RGBA = 6
};
enum PNG_INTERLACE_METHOD
{
PNG_INTERLACE_NO,
PNG_INTERLACE_ADAM7
};
enum PNG_FILTER_METHOD
{
PNG_FILTER_NONE,
PNG_FILTER_SUB,
PNG_FILTER_UP,
PNG_FILTER_AVG,
PNG_FILTER_PAETH
};
typedef struct {
UINT height;
UINT width;
BYTE bit_depth;
BYTE color_type;
BYTE comp_method; // only deflate
BYTE filter_method; // only one
BYTE interlace_method;
} png_image_hdr_t;
typedef struct {
BYTE r;
BYTE g;
BYTE b;
} rgb_vec_t;
BYTE* g_pallete = nullptr;
BYTE* g_decompressed_IDAT_bytestream = nullptr;
BYTE* g_compressed_IDAT_bytestream = nullptr;
BYTE* g_raw_image = nullptr;
BYTE* g_bmp24_raw_image = nullptr;
bool g_debug_filters = false;
std::vector<PNG_FILTER_METHOD> g_vec_filters;
void Pad_lines(BYTE* lines_new, BYTE* lines_old, UINT width, UINT height)
{
UINT line_num = 0;
UINT new_width = (3 * width + 3) & ~3;
while (line_num < height)
{
memcpy(lines_new + new_width * line_num, lines_old + 3 * width * line_num, 3 * width);
line_num++;
}
}
bool BMP_24_create_from_bytes(char* file_name, png_image_hdr_t* png_hdr, BYTE* bmp_data)
{
DWORD a = 0;
HANDLE f2 = CreateFileA(file_name, GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (GetLastError() && (GetLastError() != ERROR_ALREADY_EXISTS))
{
printf("Error create bmp file: %d \n", GetLastError());
return false;
}
// 14b BITMAPFILEHEADER
// 40b BITMAPINFOHEADER
// Data ?
UINT bmp_size = 54 + 3 * png_hdr->width * png_hdr->height;
BITMAPFILEHEADER bmp_hdr;
bmp_hdr.bfType = 0x4D42; // BM
bmp_hdr.bfSize = bmp_size;
bmp_hdr.bfReserved1 = 0;
bmp_hdr.bfReserved2 = 0;
bmp_hdr.bfOffBits = 54;
BITMAPINFOHEADER bmp_info_hdr;
ZeroMemory(&bmp_info_hdr, sizeof(BITMAPINFOHEADER));
bmp_info_hdr.biSize = 40;
bmp_info_hdr.biWidth = png_hdr->width;
bmp_info_hdr.biHeight = -(int)png_hdr->height;
bmp_info_hdr.biPlanes = 1;
bmp_info_hdr.biBitCount = 24;
bmp_info_hdr.biCompression = 0;
bmp_info_hdr.biSizeImage = bmp_size - 54;
WRITE(bmp_hdr);
WRITE(bmp_info_hdr);
// probably most bad code here
// REPLACE IT !!! IT IS BAD FOR PERFORMANCE
//reverseBytes(bmp_data, bmp_size - 54);
//reverse_lines_rgb((rgb_vec_t*)bmp_data, png_hdr->width, png_hdr->height);
if (png_hdr->width % 4 != 0)
{
BYTE* bmp_padded_data = (BYTE*)malloc(((3 * png_hdr->width + 3) & ~3) * png_hdr->height);
Pad_lines(bmp_padded_data, bmp_data, png_hdr->width, png_hdr->height);
UINT pad_bytes_size = (((3 * png_hdr->width + 3) & ~3) - 3 * png_hdr->width) * png_hdr->height;
WRITEP(bmp_padded_data, bmp_size - 54 + pad_bytes_size);
free(bmp_padded_data);
return true;
}
WRITEP(bmp_data, bmp_size - 54);
return true;
}
void BMP_raw_grayscale_to_raw_bgr(BYTE* raw_in, BYTE* raw_out, UINT bitdepth, UINT width, UINT height)
{
BYTE bit2_to_8bit_color[] = {0x00, 0x55, 0xAA, 0xFF};
BYTE bit4_to_8bit_color[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};
UINT pix_amt = width * height;
BYTE* raw_in_row_p = raw_in;
switch (bitdepth)
{
case 1:
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
BYTE pix_val = (raw_in_row_p[j / 8] & (0x80 >> (j % 8))) != 0 ? 0xFF : 0x00;
raw_out[3 * (i*width + j) + 2] = pix_val;
raw_out[3 * (i*width + j) + 1] = pix_val;
raw_out[3 * (i*width + j) + 0] = pix_val;
}
raw_in_row_p = raw_in + ((width + 7) >> 3) * i;
}
return;
case 2:
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
BYTE val = raw_in_row_p[j / 4] & (0xC0 >> (2 * (j % 4)));
BYTE pix_val = bit2_to_8bit_color[val >> (6 - 2 * (j % 4))];
raw_out[3 * (i*width + j) + 2] = pix_val;
raw_out[3 * (i*width + j) + 1] = pix_val;
raw_out[3 * (i*width + j) + 0] = pix_val;
}
raw_in_row_p = raw_in + ((width + 3) >> 2) * i;
}
return;
case 4:
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
BYTE val = raw_in_row_p[j / 2] & (0xF0 >> (4 * (j % 2)));
BYTE pix_val = bit4_to_8bit_color[val >> (4 - 4 * (j % 2))];
raw_out[3 * (i*width + j) + 2] = pix_val;
raw_out[3 * (i*width + j) + 1] = pix_val;
raw_out[3 * (i*width + j) + 0] = pix_val;
}
raw_in_row_p = raw_in + ((width + 1) >> 1) * i;
}
return;
case 8:
for (int i = 0; i < pix_amt; i++)
{
BYTE pix_val = raw_in[i];
raw_out[3 * i + 0] = pix_val;
raw_out[3 * i + 1] = pix_val;
raw_out[3 * i + 2] = pix_val;
}
return;
case 16:
for (int i = 0; i < pix_amt; i++)
{
BYTE pix_val = (BYTE)((float)((WORD*)raw_in)[i] / 65535.0f * 255.0f);
raw_out[3 * i + 0] = pix_val;
raw_out[3 * i + 1] = pix_val;
raw_out[3 * i + 2] = pix_val;
}
return;
default:
printf("Error bitdepth = %d \n", bitdepth);
return;
}
}
void BMP_raw_palette_to_raw_bgr(BYTE* raw_in, BYTE* raw_out, UINT width, UINT height, UINT bit_depth, BYTE* palette)
{
UINT pix_amt = width * height;
BYTE* raw_in_row_p = raw_in;
if (!palette) return;
switch (bit_depth)
{
case 1:
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
BYTE idx_val = (raw_in_row_p[j / 8] & (0x80 >> (j % 8))) != 0 ? 0xFF : 0x00;
raw_out[3 * (i*width + j) + 2] = palette[idx_val * 3 + 0];
raw_out[3 * (i*width + j) + 1] = palette[idx_val * 3 + 1];
raw_out[3 * (i*width + j) + 0] = palette[idx_val * 3 + 2];
}
raw_in_row_p = raw_in + ((width + 7) >> 3) * i;
}
return;
case 2:
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
BYTE val = raw_in_row_p[j / 4] & (0xC0 >> (2 * (j % 4)));
BYTE idx_val = val >> (6 - 2 * (j % 4));
raw_out[3 * (i*width + j) + 2] = palette[idx_val * 3 + 0];
raw_out[3 * (i*width + j) + 1] = palette[idx_val * 3 + 1];
raw_out[3 * (i*width + j) + 0] = palette[idx_val * 3 + 2];
}
raw_in_row_p = raw_in + ((width + 3) >> 2) * i;
}
return;
case 4:
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
BYTE val = raw_in_row_p[j / 2] & (0xF0 >> (4 * (j % 2)));
BYTE idx_val = val >> (4 - 4 * (j % 2));
raw_out[3 * (i*width + j) + 2] = palette[idx_val * 3 + 0];
raw_out[3 * (i*width + j) + 1] = palette[idx_val * 3 + 1];
raw_out[3 * (i*width + j) + 0] = palette[idx_val * 3 + 2];
}
raw_in_row_p = raw_in + ((width + 1) >> 1) * i;
}
return;
case 8:
for (int i = 0; i < pix_amt; i++)
{
BYTE idx_val = raw_in[i];
raw_out[3 * i + 2] = palette[idx_val * 3 + 0];
raw_out[3 * i + 1] = palette[idx_val * 3 + 1];
raw_out[3 * i + 0] = palette[idx_val * 3 + 2];
}
return;
case 16:
default:
printf("Error bitdepth = %d \n", bit_depth);
return;
}
}
void Debug_fill_filter_colors(BYTE* raw_in, png_image_hdr_t* png_hdr)
{
UINT row_size = ((png_hdr->bit_depth + 7) >> 3) * png_hdr->width * 3;
UINT line_n = 0;
UINT line_size = 10;
if (g_vec_filters.size() < png_hdr->height)
{
printf("g_vec_filters.size() < png_hdr->height\n");
return;
}
while (line_n < png_hdr->height)
{
PNG_FILTER_METHOD mtd = g_vec_filters[line_n];
BYTE* line_p = raw_in + row_size * line_n;
switch (mtd)
{
case PNG_FILTER_NONE:
memset(line_p, 0, line_size*3);
break;
case PNG_FILTER_SUB:
for (int i = 0; i < line_size * 3; i++) { line_p[i] = !(2 - i % 3) ? 0xFF : 0x00; } // red
break;
case PNG_FILTER_UP:
for (int i = 0; i < line_size * 3; i++) { line_p[i] = !(i % 3) ? 0xFF : 0x00; } // blue
break;
case PNG_FILTER_AVG:
for (int i = 0; i < line_size * 3; i++) { line_p[i] = !(1 - i % 3) ? 0xFF : 0x00; } // green
break;
case PNG_FILTER_PAETH:
for (int i = 0; i < line_size * 3; i++) { line_p[i] = !(1 - i % 3) ? 0x00 : 0xFF; } // purple
break;
default:
break;
}
line_n++;
}
}
bool BMP_decode_raw_png_to_raw_bgr_bmp(BYTE* raw_in, BYTE* raw_out, png_image_hdr_t* png_hdr, rgb_vec_t background_clr = { 255, 255, 255 }, BYTE* pallete = nullptr)
{
BYTE ct = png_hdr->color_type;
//UINT raw_in_size = bpp * png_hdr->width * png_hdr->height;
switch (ct)
{
case PNG_COLOR_GRAYSCALE:
BMP_raw_grayscale_to_raw_bgr(raw_in, raw_out, png_hdr->bit_depth, png_hdr->width, png_hdr->height);
break;
case PNG_COLOR_RGB:
if (png_hdr->bit_depth == 8)
{
//memcpy(raw_out, raw_in, 3 * png_hdr->width * png_hdr->height);
for (int i = 0; i < png_hdr->width * png_hdr->height; i++)
{
rgb_vec_t pix = ((rgb_vec_t*)raw_in)[i];
((rgb_vec_t*)raw_out)[i] = { pix.b, pix.g, pix.r };
}
break;
}
if (png_hdr->bit_depth == 16)
{
for (int i = 0; i < png_hdr->width * png_hdr->height; i++)
{
//raw_out[i] = (BYTE)((float)((WORD*)raw_in)[i] / 65535.0f * 255.0f);
BYTE pix_r = (BYTE)((float)((WORD*)raw_in)[3 * i + 0] / 255.0f);
BYTE pix_g = (BYTE)((float)((WORD*)raw_in)[3 * i + 1] / 255.0f);
BYTE pix_b = (BYTE)((float)((WORD*)raw_in)[3 * i + 2] / 255.0f);
((rgb_vec_t*)raw_out)[i] = { pix_b, pix_g, pix_r };
}
break;
}
printf("error PNG_COLOR_RGB bit depth = %d", png_hdr->bit_depth);
return false;
case PNG_COLOR_PALETTE:
BMP_raw_palette_to_raw_bgr(raw_in, raw_out, png_hdr->width, png_hdr->height, png_hdr->bit_depth, pallete);
break;
case PNG_COLOR_GRAYSCALE_ALPHA:
if (png_hdr->bit_depth == 8)
{
for (int i = 0; i < png_hdr->width * png_hdr->height; i++)
{
// output = alpha * foreground + (1-alpha) * background
float alpha = (float)raw_in[2 * i + 1] / 255.0f;
float foreground = (float)raw_in[2 * i + 0] / 255.0f;
BYTE clr_r = (BYTE)((alpha * foreground + (1 - alpha) * (float)background_clr.r / 255.0f) * 255.0f);
BYTE clr_g = (BYTE)((alpha * foreground + (1 - alpha) * (float)background_clr.g / 255.0f) * 255.0f);
BYTE clr_b = (BYTE)((alpha * foreground + (1 - alpha) * (float)background_clr.b / 255.0f) * 255.0f);
raw_out[3 * i + 2] = clr_r;
raw_out[3 * i + 1] = clr_g;
raw_out[3 * i + 0] = clr_b;
}
break;
}
if (png_hdr->bit_depth == 16)
{
for (int i = 0; i < png_hdr->width * png_hdr->height; i++)
{
// output = alpha * foreground + (1-alpha) * background
float alpha = (float)((WORD*)raw_in)[2 * i + 1] / 65535.0f;
float foreground = (float)((WORD*)raw_in)[2 * i + 0] / 65535.0f;
BYTE clr_r = (BYTE)((alpha * foreground + (1 - alpha) * (float)background_clr.r / 255.0f) * 255.0f);
BYTE clr_g = (BYTE)((alpha * foreground + (1 - alpha) * (float)background_clr.g / 255.0f) * 255.0f);
BYTE clr_b = (BYTE)((alpha * foreground + (1 - alpha) * (float)background_clr.b / 255.0f) * 255.0f);
raw_out[3 * i + 2] = clr_r;
raw_out[3 * i + 1] = clr_g;
raw_out[3 * i + 0] = clr_b;
}
break;
}
printf("error PNG_COLOR_GRAYSCALE_ALPHA bit depth = %d", png_hdr->bit_depth);
return false;
case PNG_COLOR_RGBA:
if (png_hdr->bit_depth == 8)
{
for (int i = 0; i < png_hdr->width * png_hdr->height; i++)
{
// output = alpha * foreground + (1-alpha) * background
float alpha = (float)raw_in[4 * i + 3] / 255.0f;
float foreground_r = (float)raw_in[4 * i + 0] / 255.0f;
float foreground_g = (float)raw_in[4 * i + 1] / 255.0f;
float foreground_b = (float)raw_in[4 * i + 2] / 255.0f;
BYTE clr_r = (BYTE)((alpha * foreground_r + (1 - alpha) * (float)background_clr.r / 255.0f) * 255.0f);
BYTE clr_g = (BYTE)((alpha * foreground_g + (1 - alpha) * (float)background_clr.g / 255.0f) * 255.0f);
BYTE clr_b = (BYTE)((alpha * foreground_b + (1 - alpha) * (float)background_clr.b / 255.0f) * 255.0f);
raw_out[3 * i + 2] = clr_r;
raw_out[3 * i + 1] = clr_g;
raw_out[3 * i + 0] = clr_b;
}
break;
}
if (png_hdr->bit_depth == 16)
{
for (int i = 0; i < png_hdr->width * png_hdr->height; i++)
{
// output = alpha * foreground + (1-alpha) * background
float alpha = (float)((WORD*)raw_in)[4 * i + 3] / 65535.0f;
float foreground_r = (float)((WORD*)raw_in)[4 * i + 0] / 65535.0f;
float foreground_g = (float)((WORD*)raw_in)[4 * i + 1] / 65535.0f;
float foreground_b = (float)((WORD*)raw_in)[4 * i + 2] / 65535.0f;
BYTE clr_r = (BYTE)((alpha * foreground_r + (1 - alpha) * (float)background_clr.r / 255.0f) * 255.0f);
BYTE clr_g = (BYTE)((alpha * foreground_g + (1 - alpha) * (float)background_clr.g / 255.0f) * 255.0f);
BYTE clr_b = (BYTE)((alpha * foreground_b + (1 - alpha) * (float)background_clr.b / 255.0f) * 255.0f);
raw_out[3 * i + 2] = clr_r;
raw_out[3 * i + 1] = clr_g;
raw_out[3 * i + 0] = clr_b;
}
break;
}
printf("error PNG_COLOR_RGBA bit depth = %d", png_hdr->bit_depth);
return false;
default:
printf("unknown (%d)\n", ct);
return false;
}
if (g_debug_filters)
Debug_fill_filter_colors(raw_out, png_hdr);
return true;
}
/* report a zlib or i/o error */
void zerr(int ret)
{
//fputs("zpipe: ", stderr);
switch (ret) {
case Z_ERRNO:
if (ferror(stdin))
fputs("error reading stdin\n", stdout);
if (ferror(stdout))
fputs("error writing stdout\n", stdout);
break;
case Z_STREAM_ERROR:
fputs("invalid compression level\n", stdout);
break;
case Z_DATA_ERROR:
fputs("invalid or incomplete deflate data\n", stdout);
break;
case Z_MEM_ERROR:
fputs("out of memory\n", stdout);
break;
case Z_VERSION_ERROR:
fputs("zlib version mismatch!\n", stdout);
}
}
int zlib_decompress_IDAT(BYTE* in_data, int size_in, BYTE* out_data, int size_out)
{
if (!in_data) return Z_ERRNO;
int ret;
z_stream strm;
/* allocate inflate state */
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = size_in;
strm.next_in = in_data;
ret = inflateInit(&strm);
if (ret != Z_OK)
return ret;
strm.avail_out = size_out;
strm.next_out = out_data;
ret = inflate(&strm, Z_NO_FLUSH);
//printf("inflate ret %d \n", ret);
zerr(ret);
/* clean up and return */
(void)inflateEnd(&strm);
//return Z_OK;
return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
}
void Paeth_filter_func(BYTE* row, BYTE* prev_row, UINT bpp, UINT width)
{
UINT row_bytes_amt = width * bpp;
BYTE* rp_end = row + bpp;
while (row < rp_end)
{
BYTE a = *row + *prev_row++;
*row++ = a;
}
rp_end += row_bytes_amt - bpp;
while (row < rp_end)
{
int a, b, c, pa, pb, pc, p;
c = *(prev_row - bpp);
a = *(row - bpp);
b = *prev_row++;
p = b - c;
pc = a - c;
pa = abs(p);
pb = abs(pc);
pc = abs(p + pc);
if (pb < pa)
{
pa = pb; a = b;
}
if (pc < pa) a = c;
a += *row;
*row++ = a;
}
}
void Unfilter_data(BYTE* data_in, BYTE* data_out, UINT width, UINT height, UINT bpp, UINT bitdepth)
{
BYTE* curr_ptr_in = data_in;
BYTE* curr_ptr_out = data_out;
//UINT bpp = (png_hdr->bit_depth + 7) >> 3;
UINT row_bytes_amt = width * bpp;
if (bitdepth == 4) row_bytes_amt = (row_bytes_amt + 1) / 2;
else if (bitdepth == 2) row_bytes_amt = (row_bytes_amt + 3) / 4;
else if (bitdepth == 1) row_bytes_amt = (row_bytes_amt + 7) / 8;
UINT in_data_size = (row_bytes_amt + 1) * height;
BYTE* row_ptr_out;
BYTE* tmp_prev_row_p;
BYTE* prev_row = (BYTE*)malloc(width * bpp);
ZeroMemory(prev_row, width);
while (curr_ptr_in < data_in + in_data_size)
{
if (g_debug_filters)
{
g_vec_filters.push_back((PNG_FILTER_METHOD)*curr_ptr_in);
}
switch (*curr_ptr_in)
{
case PNG_FILTER_NONE:
//printf("Filter (None, %08x, %08x) \n", curr_ptr_in - data_in, curr_ptr_out - data_out);
memcpy(curr_ptr_out, curr_ptr_in + 1, row_bytes_amt);
memcpy(prev_row, curr_ptr_out, row_bytes_amt);
curr_ptr_out += row_bytes_amt;
curr_ptr_in += row_bytes_amt + 1;
break;
case PNG_FILTER_SUB:
//printf("Filter (Sub, %08x, %08x) \n", curr_ptr_in - data_in, curr_ptr_out - data_out);
memcpy(curr_ptr_out, curr_ptr_in + 1, row_bytes_amt);
curr_ptr_in += row_bytes_amt + 1;
row_ptr_out = curr_ptr_out;
curr_ptr_out += bpp;
for (int i = bpp; i < row_bytes_amt; i++)
{
*curr_ptr_out = (*curr_ptr_out + *(curr_ptr_out - bpp)) & 0xff;
curr_ptr_out++;
}
memcpy(prev_row, row_ptr_out, row_bytes_amt);
break;
case PNG_FILTER_UP:
//printf("Filter (Up, %08x, %08x) \n", curr_ptr_in - data_in, curr_ptr_out - data_out);
memcpy(curr_ptr_out, curr_ptr_in + 1, row_bytes_amt);
curr_ptr_in += row_bytes_amt + 1;
row_ptr_out = curr_ptr_out;
tmp_prev_row_p = prev_row;
for (int i = 0; i < row_bytes_amt; i++)
{
*curr_ptr_out = (*curr_ptr_out + (*tmp_prev_row_p++)) & 0xff;
curr_ptr_out++;
}
memcpy(prev_row, row_ptr_out, row_bytes_amt);
break;
case PNG_FILTER_AVG:
//printf("Filter (Avg, %08x, %08x) \n", curr_ptr_in - data_in, curr_ptr_out - data_out);
memcpy(curr_ptr_out, curr_ptr_in + 1, row_bytes_amt);
curr_ptr_in += row_bytes_amt + 1;
row_ptr_out = curr_ptr_out;
tmp_prev_row_p = prev_row;
for (int i = 0; i < bpp; i++)
{
*curr_ptr_out = (*curr_ptr_out + (*tmp_prev_row_p++) / 2) & 0xff;
curr_ptr_out++;
}
for (int i = 0; i < row_bytes_amt - bpp; i++)
{
*curr_ptr_out = (*curr_ptr_out +
(*tmp_prev_row_p++ + *(curr_ptr_out - bpp)) / 2) & 0xff;
curr_ptr_out++;
}
memcpy(prev_row, row_ptr_out, row_bytes_amt);
break;
case PNG_FILTER_PAETH:
//printf("Filter (Paeth, %08x, %08x) \n", curr_ptr_in - data_in, curr_ptr_out - data_out);
memcpy(curr_ptr_out, curr_ptr_in + 1, row_bytes_amt);
curr_ptr_in += row_bytes_amt + 1;
row_ptr_out = curr_ptr_out;
Paeth_filter_func(curr_ptr_out, prev_row, bpp, width);
curr_ptr_out += row_bytes_amt;
memcpy(prev_row, row_ptr_out, row_bytes_amt);
break;
default:
printf("Error not existing filter method %d \n", *curr_ptr_in);
return;
}
}
free(prev_row);
}
void Unfilter_data_interlaced(BYTE* data_in, BYTE* data_out, UINT width, UINT height, UINT bpp, UINT bitdepth)
{
UINT pass_cols[7] = { 0 };
UINT pass_rows[7] = { 0 };
UINT start_row[7] = { 0, 0, 4, 0, 2, 0, 1 };
UINT start_col[7] = { 0, 4, 0, 2, 0, 1, 0 };
UINT row_inc[7] = { 8, 8, 8, 4, 4, 2, 2 };
UINT col_inc[7] = { 8, 8, 4, 4, 2, 2, 1 };
pass_cols[0] = ((width + 7) / 8); pass_rows[0] = (height + 7) / 8;
pass_cols[1] = ((width + 3) / 8); pass_rows[1] = (height + 7) / 8;
pass_cols[2] = ((width + 3) / 4); pass_rows[2] = (height + 3) / 8;
pass_cols[3] = ((width + 1) / 4); pass_rows[3] = (height + 3) / 4;
pass_cols[4] = ((width + 1) / 2); pass_rows[4] = (height + 1) / 4;
pass_cols[5] = (width / 2); pass_rows[5] = (height + 1) / 2;
pass_cols[6] = (width); pass_rows[6] = (height) / 2;
UINT stop_row[7] = {};
UINT stop_col[7] = {};
UINT fpass_size[7] = {};
UINT pass_size[7] = {};
for (int i = 0; i < 7; i++) stop_row[i] = start_row[i] + row_inc[i] * pass_rows[i];
for (int i = 0; i < 7; i++) stop_col[i] = start_col[i] + col_inc[i] * pass_cols[i];
for (int i = 0; i < 7; i++) fpass_size[i] = (pass_cols[i] * bpp + 1) * pass_rows[i];
for (int i = 0; i < 7; i++) pass_size[i] = (pass_cols[i] * bpp) * pass_rows[i];
BYTE* curr_ptr_in = data_in;
BYTE* curr_ptr_out = data_out;
UINT curr_col = start_col[0];
UINT curr_row = start_row[0];
UINT pass = 0;
while (pass < 7)
{
BYTE* filtered_pass = (BYTE*)malloc(fpass_size[pass]);
BYTE* unfiltered_pass = (BYTE*)malloc(pass_size[pass]);
memcpy(filtered_pass, curr_ptr_in, fpass_size[pass]);
curr_ptr_in += fpass_size[pass];
Unfilter_data(filtered_pass, unfiltered_pass, pass_cols[pass], pass_rows[pass], bpp, bitdepth);
BYTE* unfiltered_curr_ptr = unfiltered_pass;
curr_row = start_row[pass];
while (curr_row < stop_row[pass])
{
curr_ptr_out = data_out + bpp * start_col[pass] + curr_row * width * bpp;
curr_col = start_col[pass];
while (curr_col < stop_col[pass])
{
for (int i = 0; i < bpp; i++)
{
*curr_ptr_out = *unfiltered_curr_ptr;
curr_ptr_out++;
unfiltered_curr_ptr++;
}
curr_ptr_out += bpp * (col_inc[pass] - 1);
curr_col += col_inc[pass];
}
curr_row += row_inc[pass];
}
free(filtered_pass);
free(unfiltered_pass);
pass++;
}
}
bool is_hdr_valid(png_image_hdr_t* hdr_p)
{
// big size
if (hdr_p->width > 8192 || hdr_p->height > 8192)
return false;
if (hdr_p->width == 0 || hdr_p->height == 0)
return false;
BYTE bd = hdr_p->bit_depth;
// checking all fields limits
if (!(bd == 1 || bd == 2 || bd == 4 || bd == 8 || bd == 16))
return false;
BYTE ct = hdr_p->color_type;
if (!(ct == PNG_COLOR_GRAYSCALE ||
ct == PNG_COLOR_RGB ||
ct == PNG_COLOR_PALETTE ||
ct == PNG_COLOR_GRAYSCALE_ALPHA ||
ct == PNG_COLOR_RGBA))
return false;
if (hdr_p->comp_method != 0 || hdr_p->filter_method != 0)
return false;
if (!(hdr_p->interlace_method == 0 || hdr_p->interlace_method == 1))
return false;
// cheking combination of color type and bit depth
switch (ct)
{
case PNG_COLOR_GRAYSCALE:
break;
case PNG_COLOR_RGB:
if (bd == 1 || bd == 2 || bd == 4)
return false;
break;
case PNG_COLOR_PALETTE:
if (bd == 16)
return false;
break;
case PNG_COLOR_GRAYSCALE_ALPHA:
if (bd == 1 || bd == 2 || bd == 4)
return false;
break;
case PNG_COLOR_RGBA:
if (bd == 1 || bd == 2 || bd == 4)
return false;
break;
default:
printf("error! ct is not in range somehow ? ct %d", ct);
break;
}
// valid then
return true;
}
void hdr_sweet_print(png_image_hdr_t* hdr_p)
{
printf("Width x Height: %dx%d\n", hdr_p->width, hdr_p->height);
printf("Bitdepth: %d bits\n", hdr_p->bit_depth);
BYTE ct = hdr_p->color_type;
printf("Color scheme: ");
switch (ct)
{
case PNG_COLOR_GRAYSCALE:
printf("grayscale \n");
break;
case PNG_COLOR_RGB:
printf("RGB \n");
break;
case PNG_COLOR_PALETTE:
printf("palette \n");
break;
case PNG_COLOR_GRAYSCALE_ALPHA:
printf("grayscale + alpha \n");
break;
case PNG_COLOR_RGBA:
printf("RGA + alpha \n");
break;
default:
printf("unknown (%d)\n", ct);
break;
}
printf("Compression method: %d\n", hdr_p->comp_method);
printf("Filter method: %d\n", hdr_p->filter_method);
if (hdr_p->interlace_method == 0)
printf("Interlace method: None \n");
else if (hdr_p->interlace_method == 1)
printf("Interlace method: Adam7\n");
else
printf("Interlace method: %d \n", hdr_p->interlace_method);
return;
}
int PNG_interlaced_image_size(UINT width, UINT height)
{
// 1 6 4 6 2 6 4 6
// 7 7 7 7 7 7 7 7
// 5 6 5 6 5 6 5 6
// 7 7 7 7 7 7 7 7
// 3 6 4 6 3 6 4 6
// 7 7 7 7 7 7 7 7
// 5 6 5 6 5 6 5 6
// 7 7 7 7 7 7 7 7
UINT pass_1, pass_2, pass_3, pass_4, pass_5, pass_6, pass_7;
pass_1 = ((width + 7) / 8 + 1) * (height + 7) / 8;
pass_2 = ((width + 3) / 8 + 1) * (height + 7) / 8;
pass_3 = ((width + 3) / 4 + 1) * (height + 3) / 8;
pass_4 = ((width + 1) / 4 + 1) * (height + 3) / 4;
pass_5 = ((width + 1) / 2 + 1) * (height + 1) / 4;
pass_6 = (width / 2 + 1) * (height + 1) / 2;
pass_7 = (width + 1) * (height) / 2;
return pass_1 + pass_2 + pass_3 + pass_4 + pass_5 + pass_6 + pass_7;
}
bool ReadPng(char* file_name)
{
DWORD a = 0;
HANDLE f = CreateFileA(file_name, GENERIC_READ, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
UINT png_file_size = GetFileSize(f, 0);
if (GetLastError())
{
printf("Error open file: %d \n", GetLastError());
return false;
}
char magic_buff[9];
READP(magic_buff, 8);
magic_buff[8] = '\0';
if (strcmp(magic_buff, "\x89PNG\x0D\x0A\x1A\x0A"))
{
printf("This is not PNG file\n");
return false;
}
//Reading chunks
std::vector<png_chunk_t> png_chunks;
while (SFPC(0) <= png_file_size - 12)
{
UINT len_chunk;
READBE4(len_chunk);
UINT type_chunk;
READBE4(type_chunk);
UINT start_at = SFPC(0);
SFPC(len_chunk);
UINT crc_chunk;
READBE4(crc_chunk);
png_chunk_t png_chunk;
png_chunk.crc = crc_chunk;
png_chunk.len = len_chunk;
png_chunk.start_at = start_at;
png_chunk.type = type_chunk;
png_chunks.push_back(png_chunk);
}
bool crit_chunks[] = { 0, 0, 0 };
for (png_chunk_t chunk : png_chunks)
{
switch (chunk.type)
{
case 'IHDR': crit_chunks[0] = true; break;
case 'IDAT': crit_chunks[1] = true; break;
case 'IEND': crit_chunks[2] = true; break;
default:
break;
}
}
if (crit_chunks[0] + crit_chunks[1] + crit_chunks[2] != 3)
{
if (!crit_chunks[0])
printf("PNG file dont contain IHDR\n");
if (!crit_chunks[1])
printf("PNG file dont contain IDAT\n");
if (!crit_chunks[2])
printf("PNG file dont contain IEND\n");
return false;
}
if (png_chunks[0].type != 'IHDR')
{
printf("IHDR must be first chunk \n");