-
Notifications
You must be signed in to change notification settings - Fork 0
/
sngd.c
1079 lines (937 loc) · 27.5 KB
/
sngd.c
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
/*****************************************************************************
NAME
sngd.c -- decompile PNG to SNG.
*****************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include "config.h" /* for RGBTXT */
#include "png.h"
#include "sng.h"
static char *image_type[] = {
"grayscale",
"undefined type",
"RGB",
"colormap",
"grayscale+alpha",
"undefined type",
"RGB+alpha"
};
static char *rendering_intent[] = {
"perceptual",
"relative colorimetric",
"saturation-preserving",
"absolute colorimetric"
};
static char *current_file;
/* Error status for the file being processed; reset to 0 at the top of sngd() */
static int sng_error;
/*****************************************************************************
*
* Interface to RGB database
*
*****************************************************************************/
#define COLOR_HASH(r, g, b) (((r<<16)|(g<<8)|(b))%COLOR_HASH_MODULUS)
static color_item *rgb_hashbuckets[COLOR_HASH_MODULUS];
static int rgb_initialized;
static int hash_by_rgb(color_item *cp)
/* hash by color's RGB value */
{
return(COLOR_HASH(cp->r, cp->g, cp->b));
}
static char *find_by_rgb(int r, int g, int b)
{
color_item sc, *hp;
sc.r = r; sc.g = g; sc.b = b;
for (hp = rgb_hashbuckets[hash_by_rgb(&sc)]; hp; hp = hp->next)
if (hp->r == r && hp->g == g && hp->b == b)
return(hp->name);
return((char *)NULL);
}
/*****************************************************************************
*
* Low-level helper code
*
*****************************************************************************/
char *safeprint(const char *buf)
/* visibilize a given string -- inverse of sngc.c:escapes() */
{
static char vbuf[PNG_STRING_MAX_LENGTH*4+1];
char *tp = vbuf;
while (*buf)
{
if (*buf == '"')
{
*tp++ = '\\'; *tp++ = '"';
buf++;
}
else if (*buf == '\\')
{
*tp++ = '\\'; *tp++ = '\\';
buf++;
}
else if (isprint(*buf) || *buf == ' ')
*tp++ = *buf++;
else if (*buf == '\n')
{
*tp++ = '\\'; *tp++ = 'n';
buf++;
}
else if (*buf == '\r')
{
*tp++ = '\\'; *tp++ = 'r';
buf++;
}
else if (*buf == '\b')
{
*tp++ = '\\'; *tp++ = 'b';
buf++;
}
else if ((unsigned char) *buf < ' ')
{
*tp++ = '\\'; *tp++ = '^'; *tp++ = '@' + *buf;
buf++;
}
else
{
(void) sprintf(tp, "\\x%02x", (unsigned char) *buf++);
tp += strlen(tp);
}
}
*tp++ = '\0';
return(vbuf);
}
static void multi_dump(FILE *fpout, char *leader,
int width, int height,
unsigned char *data[])
/* dump data in a recompilable form */
{
unsigned char *cp;
int i, all_printable = 1, base64 = 1;
png_byte bit_depth = png_get_bit_depth(png_ptr, info_ptr);
png_byte channels = png_get_channels(png_ptr, info_ptr);
#define SHORT_DATA 50
for (i = 0; i < height; i++)
for (cp = data[i]; cp < data[i] + width; cp++)
{
if (!isprint(*cp) && !isspace(*cp))
all_printable = 0;
if (*cp >= 64)
base64 = 0;
}
for (i = 0; i < height; i++)
{
if (all_printable)
{
unsigned char *cp;
if (i == 0)
{
fprintf(fpout, "%s ", leader);
if (height == 1 && width < SHORT_DATA)
fprintf(fpout, " ");
else
fprintf(fpout, "\n");
}
fputc('"', fpout);
for (cp = data[i]; cp < data[i] + width; cp++)
{
char cbuf[2];
cbuf[0] = *cp;
cbuf[1] = '\0';
fputs(safeprint(cbuf), fpout);
if (*cp == '\n' && cp < data[i] + width - 1)
fprintf(fpout, "\"\n\"");
}
fprintf(fpout, "\"%c\n", height == 1 ? ';' : ' ');
}
else if (base64)
{
if (i == 0)
{
fprintf(fpout, "%sbase64", leader);
if (height == 1 && width < SHORT_DATA)
fprintf(fpout, " ");
else
fprintf(fpout, "\n");
}
for (cp = data[i]; cp < data[i] + width; cp++) {
if (*cp >= 64)
fatal("invalid base64 data (%d)", *cp);
fputc(BASE64[*cp], fpout);
}
if (height == 1)
fprintf(fpout, ";\n");
else
fprintf(fpout, "\n");
}
else
{
if (i == 0)
{
fprintf(fpout, "%shex", leader);
if (height == 1 && width < SHORT_DATA)
fprintf(fpout, " ");
else
fprintf(fpout, "\n");
}
for (cp = data[i]; cp < data[i] + width; cp++)
{
fprintf(fpout, "%02x", *cp & 0xff);
/* only insert spacers for 8-bit images if > 1 channel */
if (bit_depth == 8 && channels > 1)
{
if (((cp - data[i]) % channels) == channels - 1)
fputc(' ', fpout);
}
else if (bit_depth == 16)
if (((cp - data[i]) % (channels*2)) == channels*2-1)
fputc(' ', fpout);
}
if (height == 1)
fprintf(fpout, ";\n");
else
fprintf(fpout, "\n");
}
}
}
static void dump_data(FILE *fpout, char *leader, int size, unsigned char *data)
{
unsigned char *dope[1];
dope[0] = data;
multi_dump(fpout, leader, size, 1, dope);
}
static void printerr(int err, const char *fmt, ... )
/* throw an error distinguishable from PNG library errors */
{
char buf[BUFSIZ];
va_list ap;
sprintf(buf, "sng: in %s, ", current_file);
va_start(ap, fmt);
vsprintf(buf + strlen(buf), fmt, ap);
va_end(ap);
strcat(buf, "\n");
fputs(buf, stderr);
sng_error = err;
}
/*****************************************************************************
*
* Chunk handlers
*
*****************************************************************************/
static void dump_IHDR(FILE *fpout)
{
png_uint_32 width;
png_uint_32 height;
int bit_depth;
int ityp;
int interlace_type;
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &ityp, &interlace_type, 0, 0);
if (width == 0 || height == 0) {
printerr(1, "invalid IHDR image dimensions (%lux%lu)",
width, height);
}
if (ityp > sizeof(image_type)/sizeof(char*)) {
ityp = 1; /* avoid out of range array index */
}
switch (bit_depth) {
case 1:
case 2:
case 4:
if (ityp == 2 || ityp == 4 || ityp == 6) {/* RGB or GA or RGBA */
printerr(1, "invalid IHDR bit depth (%u) for %s image",
bit_depth, image_type[ityp]);
}
break;
case 8:
break;
case 16:
if (ityp == 3) { /* palette */
printerr(1, "invalid IHDR bit depth (%u) for %s image",
bit_depth, image_type[ityp]);
}
break;
default:
printerr(1, "invalid IHDR bit depth (%u)", bit_depth);
break;
}
fprintf(fpout, "IHDR {\n");
fprintf(fpout, " width: %u; height: %u; bitdepth: %u;\n",
(unsigned int)width, (unsigned int)height, bit_depth);
fprintf(fpout, " using");
if (ityp & PNG_COLOR_MASK_COLOR)
fprintf(fpout, " color");
else
fprintf(fpout, " grayscale");
if (ityp & PNG_COLOR_MASK_PALETTE)
fprintf(fpout, " palette");
if (ityp & PNG_COLOR_MASK_ALPHA)
fprintf(fpout, " alpha");
fprintf(fpout, ";\n");
if (interlace_type)
fprintf(fpout, " with interlace; # type adam7 assumed\n");
fprintf(fpout, "}\n");
}
static void dump_PLTE(FILE *fpout)
{
int i;
png_byte color_type = png_get_color_type(png_ptr, info_ptr);
png_colorp palette;
int num_palette;
png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
initialize_hash(hash_by_rgb, rgb_hashbuckets, &rgb_initialized);
if (color_type & PNG_COLOR_MASK_PALETTE)
{
fprintf(fpout, "PLTE {\n");
for (i = 0; i < num_palette; i++)
{
char *name = NULL;
fprintf(fpout,
" (%3u,%3u,%3u) # rgb = (0x%02x,0x%02x,0x%02x)",
palette[i].red,
palette[i].green,
palette[i].blue,
palette[i].red,
palette[i].green,
palette[i].blue);
if (rgb_initialized)
name = find_by_rgb(palette[i].red,
palette[i].green,
palette[i].blue);
if (name)
fprintf(fpout, " %s", name);
fputc('\n', fpout);
}
fprintf(fpout, "}\n");
}
}
static void dump_image(png_bytepp rows, FILE *fpout)
{
if (idat)
{
int i;
for (i = 0; i < idat; i++)
{
fprintf(fpout, "IDAT {\n");
fprintf(fpout, "}\n");
}
}
else
{
fprintf(fpout, "IMAGE {\n");
multi_dump(fpout, " pixels ",
png_get_rowbytes(png_ptr, info_ptr), png_get_image_height(png_ptr, info_ptr),
rows);
fprintf(fpout, "}\n");
}
}
static void dump_bKGD(FILE *fpout)
{
png_color_16p background;
if (png_get_bKGD(png_ptr, info_ptr, &background))
{
png_byte color_type = png_get_color_type(png_ptr, info_ptr);
fprintf(fpout, "bKGD {");
switch (color_type) {
case PNG_COLOR_TYPE_GRAY:
case PNG_COLOR_TYPE_GRAY_ALPHA:
fprintf(fpout, "gray: %u;", background->gray);
break;
case PNG_COLOR_TYPE_RGB:
case PNG_COLOR_TYPE_RGB_ALPHA:
fprintf(fpout, "red: %u; green: %u; blue: %u;",
background->red,
background->green,
background->blue);
break;
case PNG_COLOR_TYPE_PALETTE:
fprintf(fpout, "index: %u", background->index);
break;
default:
printerr(1, "unknown image type");
}
fprintf(fpout, "}\n");
}
}
static void dump_cHRM(FILE *fpout)
{
double wx, wy, rx, ry, gx, gy, bx, by;
if (!png_get_valid(png_ptr, info_ptr, PNG_INFO_cHRM))
return;
#ifdef PNG_FLOATING_POINT_SUPPORTED
png_get_cHRM(png_ptr, info_ptr, &wx, &wy, &rx, &ry, &gx, &gy, &bx, &by);
#else
#ifdef PNG_FIXED_POINT_SUPPORTED
png_fixed_point wx_f, wy_f, rx_f, ry_f, gx_f, gy_f, bx_f, by_f;
png_get_cHRM_fixed(png_ptr, info_ptr, &wx_f, &wy_f, &rx_f, &ry_f, &gx_f, &gy_f, &bx_f, &by_f);
wx = FIXED_TO_FLOAT(wx_f);
wy = FIXED_TO_FLOAT(wy_f);
rx = FIXED_TO_FLOAT(rx_f);
ry = FIXED_TO_FLOAT(ry_f);
gx = FIXED_TO_FLOAT(gx_f);
gy = FIXED_TO_FLOAT(gy_f);
bx = FIXED_TO_FLOAT(bx_f);
by = FIXED_TO_FLOAT(by_f);
#endif
#endif
if (wx < 0 || wx > 0.8 || wy < 0 || wy > 0.8 || wx + wy > 1.0) {
printerr(1, "invalid cHRM white point %0g %0g", wx, wy);
} else if (rx < 0 || rx > 0.8 || ry < 0 || ry > 0.8 || rx + ry > 1.0) {
printerr(1, "invalid cHRM red point %0g %0g", rx, ry);
} else if (gx < 0 || gx > 0.8 || gy < 0 || gy > 0.8 || gx + gy > 1.0) {
printerr(1, "invalid cHRM green point %0g %0g", gx, gy);
} else if (bx < 0 || bx > 0.8 || by < 0 || by > 0.8 || bx + by > 1.0) {
printerr(1, "invalid cHRM blue point %0g %0g", bx, by);
}
fprintf(fpout, "cHRM {\n");
fprintf(fpout, " white: (%0g, %0g);\n", wx, wy);
fprintf(fpout, " red: (%0g, %0g);\n", rx, ry);
fprintf(fpout, " green: (%0g, %0g);\n", gx, gy);
fprintf(fpout, " blue: (%0g, %0g);\n", bx, by);
fprintf(fpout, "}\n");
}
static void dump_gAMA(FILE *fpout)
{
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_gAMA)) {
#ifdef PNG_FLOATING_POINT_SUPPORTED
double gamma;
png_get_gAMA(png_ptr, info_ptr, &gamma);
fprintf(fpout, "gAMA {%#0.5g}\n", gamma);
#else
#ifdef PNG_FIXED_POINT_SUPPORTED
png_fixed_point int_gamma;
png_get_gAMA_fixed(png_ptr, info_ptr, &int_gamma);
fprintf(fpout, "gAMA {%#0.5g}\n", FIXED_TO_FLOAT(int_gamma));
#endif
#endif
}
}
static void dump_hIST(FILE *fpout)
{
png_uint_16p hist;
if (png_get_hIST(png_ptr, info_ptr, &hist)) {
int j;
png_colorp palette;
int num_palette;
png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
fprintf(fpout, "hIST {\n");
fprintf(fpout, " ");
for (j = 0; j < num_palette; j++)
fprintf(fpout, " %3u", hist[j]);
fprintf(fpout, ";\n}\n");
}
}
static void dump_iCCP(FILE *fpout)
{
png_charp name;
int compression_type;
#if PNG_LIBPNG_VER_MAJOR < 1 || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR <= 4)
png_charp profile;
#else
png_bytep profile;
#endif
png_uint_32 proflen;
if (png_get_iCCP(png_ptr, info_ptr, &name, &compression_type, &profile, &proflen)) {
fprintf(fpout, "iCCP {\n");
fprintf(fpout, " name: \"%s\"\n", safeprint(name));
dump_data(fpout, " profile: ", proflen, profile);
fprintf(fpout, "}\n");
}
}
static void dump_oFFs(FILE *fpout)
{
png_int_32 offset_x;
png_int_32 offset_y;
int unit_type;
if (png_get_oFFs(png_ptr, info_ptr, &offset_x, &offset_y, &unit_type)) {
fprintf(fpout, "oFFs {xoffset: %d; yoffset: %d;",
(int)offset_x, (int)offset_y);
if (unit_type == PNG_OFFSET_PIXEL)
fprintf(fpout, " unit: pixels");
else if (unit_type == PNG_OFFSET_MICROMETER)
fprintf(fpout, " unit: micrometers");
fprintf(fpout, ";}\n");
}
}
static void dump_pHYs(FILE *fpout)
{
png_uint_32 res_x;
png_uint_32 res_y;
int unit_type;
if (png_get_pHYs(png_ptr, info_ptr, &res_x, &res_y, &unit_type)) {
if (unit_type > 1)
printerr(1, "invalid pHYs unit");
else {
fprintf(fpout, "pHYs {xpixels: %u; ypixels: %u;",
(unsigned int)res_x, (unsigned int)res_y);
if (unit_type == PNG_RESOLUTION_METER)
fprintf(fpout, " per: meter;");
fprintf(fpout, "}");
if (unit_type == 1 && res_x == res_y)
fprintf(fpout, " # (%lu dpi)\n", (long)(res_x*0.0254 + 0.5));
else
fputc('\n', fpout);
}
}
}
static void dump_sBIT(FILE *fpout)
{
png_byte color_type = png_get_color_type(png_ptr, info_ptr);
png_byte bit_depth = png_get_bit_depth(png_ptr, info_ptr);
png_color_8p sig_bit;
int maxbits = (color_type == 3)? 8 : bit_depth;
if (png_get_sBIT(png_ptr, info_ptr, &sig_bit)) {
fprintf(fpout, "sBIT {\n");
switch (color_type) {
case PNG_COLOR_TYPE_GRAY:
if (sig_bit->gray == 0 || sig_bit->gray > maxbits) {
printerr(1, "%u sBIT gray bits not valid for %ubit/sample image",
sig_bit->gray, maxbits);
} else {
fprintf(fpout, " gray: %u;\n", sig_bit->gray);
}
break;
case PNG_COLOR_TYPE_RGB:
case PNG_COLOR_TYPE_PALETTE:
if (sig_bit->red == 0 || sig_bit->red > maxbits) {
printerr(1, "%u sBIT red bits not valid for %ubit/sample image",
sig_bit->red, maxbits);
} else if (sig_bit->green == 0 || sig_bit->green > maxbits) {
printerr(1, "%u sBIT green bits not valid for %ubit/sample image",
sig_bit->green, maxbits);
} else if (sig_bit->blue == 0 || sig_bit->blue > maxbits) {
printerr(1, "%u sBIT blue bits not valid for %ubit/sample image",
sig_bit->blue, maxbits);
} else {
fprintf(fpout, " red: %u; green: %u; blue: %u;\n",
sig_bit->red, sig_bit->green, sig_bit->blue);
}
break;
case PNG_COLOR_TYPE_GRAY_ALPHA:
if (sig_bit->gray == 0 || sig_bit->gray > maxbits) {
printerr(2, "%u sBIT gray bits not valid for %ubit/sample image\n",
sig_bit->gray, maxbits);
} else if (sig_bit->alpha == 0 || sig_bit->alpha > maxbits) {
printerr(2, "%u sBIT alpha bits(tm) not valid for %ubit/sample image\n",
sig_bit->alpha, maxbits);
} else {
fprintf(fpout, " gray: %u; alpha: %u\n", sig_bit->gray, sig_bit->alpha);
}
break;
case PNG_COLOR_TYPE_RGB_ALPHA:
if (sig_bit->gray == 0 || sig_bit->gray > maxbits) {
printerr(1, "%u sBIT red bits not valid for %ubit/sample image",
sig_bit->gray, maxbits);
} else if (sig_bit->green == 0 || sig_bit->green > maxbits) {
printerr(1, "%u sBIT green bits not valid for %ubit/sample image",
sig_bit->green, maxbits);
} else if (sig_bit->blue == 0 || sig_bit->blue > maxbits) {
printerr(1, "%u sBIT blue bits not valid for %ubit/sample image",
sig_bit->blue, maxbits);
} else if (sig_bit->alpha == 0 || sig_bit->alpha > maxbits) {
printerr(1, "%u sBIT alpha bits not valid for %ubit/sample image",
sig_bit->alpha, maxbits);
} else {
fprintf(fpout, " red: %u; green: %u; blue: %u; alpha: %u;\n",
sig_bit->red,
sig_bit->green,
sig_bit->blue,
sig_bit->alpha);
}
break;
}
fprintf(fpout, "}\n");
}
}
static void dump_pCAL(FILE *fpout)
{
static char *mapping_type[] = {
"linear", "euler", "exponential", "hyperbolic"
};
png_charp purpose;
png_int_32 X0;
png_int_32 X1;
int type;
int nparams;
png_charp units;
png_charpp params;
if (png_get_pCAL(png_ptr, info_ptr, &purpose, &X0, &X1, &type, &nparams, &units, ¶ms)) {
if (type >= PNG_EQUATION_LAST)
printerr(1, "invalid equation type in pCAL");
else {
int i;
fprintf(fpout, "pCAL {\n");
fprintf(fpout, " name: \"%s\";\n", safeprint(purpose));
fprintf(fpout, " x0: %d;\n", (int)X0);
fprintf(fpout, " x1: %d;\n", (int)X1);
fprintf(fpout, " mapping: %s; # equation type %u\n",
mapping_type[type], type);
fprintf(fpout, " unit: \"%s\"\n", safeprint(units));
if (nparams) {
fprintf(fpout, " parameters:");
for (i = 0; i < nparams; i++)
fprintf(fpout, " %s", safeprint(params[i]));
fprintf(fpout, ";\n");
}
fprintf(fpout, "}\n");
}
}
}
static void dump_sCAL(FILE *fpout)
{
#ifdef PNG_FLOATING_POINT_SUPPORTED
int unit;
double width;
double height;
if (png_get_sCAL(png_ptr, info_ptr, &unit, &width, &height)) {
#else
#ifdef PNG_FIXED_POINT_SUPPORTED
int unit;
png_charp swidth;
png_charp sheight;
if (png_get_sCAL_s(png_ptr, info_ptr, &unit, &swidth, &sheight)) {
#endif
#endif
fprintf(fpout, "sCAL {\n");
switch (unit)
{
case PNG_SCALE_METER:
fprintf(fpout, " unit: meter\n");
break;
case PNG_SCALE_RADIAN:
fprintf(fpout, " unit: radian\n");
break;
default:
fprintf(fpout, " unit: unknown\n");
break;
}
#ifdef PNG_FLOATING_POINT_SUPPORTED
fprintf(fpout, " width: %g\n", width);
fprintf(fpout, " height: %g\n", height);
#else
#ifdef PNG_FIXED_POINT_SUPPORTED
fprintf(fpout, " width: %s\n", swidth);
fprintf(fpout, " height: %s\n", sheight);
#endif
#endif
fprintf(fpout, "}\n");
}
}
static void dump_sPLT(FILE *fpout)
{
/*
for (i = 0; i < info_ptr->splt_palettes_num; i++)
dump_sPLT(info_ptr->splt_palettes + i, fpout);
*/
int i, j;
int num_spalettes;
png_sPLT_tp entries;
num_spalettes = png_get_sPLT(png_ptr, info_ptr, &entries);
for (j = 0; j < num_spalettes; j++)
{
png_sPLT_tp ep = entries + j;
initialize_hash(hash_by_rgb, rgb_hashbuckets, &rgb_initialized);
fprintf(fpout, "sPLT {\n");
fprintf(fpout, " name: \"%s\";\n", safeprint(ep->name));
fprintf(fpout, " depth: %u;\n", ep->depth);
for (i = 0; i < ep->nentries; i++)
{
char *name = 0;
fprintf(fpout, " (%3u,%3u,%3u), %3u, %3u "
" # rgba = [0x%02x,0x%02x,0x%02x,0x%02x]",
ep->entries[i].red,
ep->entries[i].green,
ep->entries[i].blue,
ep->entries[i].alpha,
ep->entries[i].frequency,
ep->entries[i].red,
ep->entries[i].green,
ep->entries[i].blue,
ep->entries[i].alpha);
if (rgb_initialized)
name = find_by_rgb(ep->entries[i].red,
ep->entries[i].green,
ep->entries[i].blue);
if (name)
fprintf(fpout, ", name = %s", name);
fprintf(fpout, ", freq = %u\n", ep->entries[i].frequency);
}
fprintf(fpout, "}\n");
}
}
static void dump_tRNS(FILE *fpout)
{
png_byte color_type = png_get_color_type(png_ptr, info_ptr);
png_bytep trans;
int num_trans;
png_color_16p trans_values;
if (png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, &trans_values)) {
int i;
fprintf(fpout, "tRNS {\n");
switch (color_type) {
case PNG_COLOR_TYPE_GRAY:
fprintf(fpout, " gray: %u;\n", trans_values->gray);
break;
case PNG_COLOR_TYPE_RGB:
fprintf(fpout, " red: %u; green: %u; blue: %u;\n",
trans_values->red,
trans_values->green,
trans_values->blue);
break;
case PNG_COLOR_TYPE_PALETTE:
for (i = 0; i < num_trans; i++)
fprintf(fpout, " %u", trans[i]);
break;
case PNG_COLOR_TYPE_GRAY_ALPHA:
case PNG_COLOR_TYPE_RGB_ALPHA:
printerr(1, "tRNS chunk illegal with this image type");
break;
}
fprintf(fpout, "}\n");
}
}
static void dump_sRGB(FILE *fpout)
{
int intent;
if (png_get_sRGB(png_ptr, info_ptr, &intent)) {
if (intent < 0 || intent > 3) {
printerr(1, "sRGB invalid rendering intent %d", intent);
} else {
fprintf(fpout, "sRGB {%u;} # %s\n",
intent,
rendering_intent[intent]);
}
}
}
static void dump_tIME(FILE *fpout)
{
png_timep mod_time;
if (png_get_tIME(png_ptr, info_ptr, &mod_time)) {
static char *months[] =
{"(undefined)", "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
char *month;
if (mod_time->month < 1 || mod_time->month > 12)
month = months[0];
else
month = months[mod_time->month];
fprintf(fpout, "tIME {\n");
fprintf(fpout, " # %2d %s %4d %02d:%02d:%02d GMT\n",
mod_time->day,
month,
mod_time->year,
mod_time->hour,
mod_time->minute,
mod_time->second);
fprintf(fpout, " year: %u\n", mod_time->year);
fprintf(fpout, " month: %u\n", mod_time->month);
fprintf(fpout, " day: %u\n", mod_time->day);
fprintf(fpout, " hour: %u\n", mod_time->hour);
fprintf(fpout, " minute: %u\n", mod_time->minute);
fprintf(fpout, " second: %u\n", mod_time->second);
fprintf(fpout, "}\n");
}
}
static void dump_text(FILE *fpout)
{
png_textp text_ptr;
int num_text;
if (png_get_text(png_ptr, info_ptr, &text_ptr, &num_text)) {
int i;
for (i = 0; i < num_text; i++)
{
switch (text_ptr[i].compression)
{
case PNG_TEXT_COMPRESSION_NONE:
fprintf(fpout, "tEXt {\n");
fprintf(fpout, " keyword: \"%s\";\n",
safeprint(text_ptr[i].key));
break;
case PNG_TEXT_COMPRESSION_zTXt:
fprintf(fpout, "zTXt {\n");
fprintf(fpout, " keyword: \"%s\";\n",
safeprint(text_ptr[i].key));
break;
case PNG_ITXT_COMPRESSION_NONE:
case PNG_ITXT_COMPRESSION_zTXt:
fprintf(fpout, "iTXt {\n");
#ifdef PNG_iTXt_SUPPORTED
fprintf(fpout, " language: \"%s\";\n",
safeprint(text_ptr[i].lang));
#endif /* PNG_iTXt_SUPPORTED */
fprintf(fpout, " keyword: \"%s\";\n",
safeprint(text_ptr[i].key));
#ifdef PNG_iTXt_SUPPORTED
fprintf(fpout, " translated: \"%s\";\n",
safeprint(text_ptr[i].lang_key));
#endif /* PNG_iTXt_SUPPORTED */
break;
}
fprintf(fpout, " text: \"%s\";\n",
safeprint(text_ptr[i].text));
fprintf(fpout, "}\n");
}
}
}
static void dump_unknown_chunks(int after_idat, FILE *fpout)
{
png_unknown_chunkp entries;
int num_unknown_chunks;
int i;
num_unknown_chunks = png_get_unknown_chunks(png_ptr, info_ptr, &entries);
for (i = 0; i < num_unknown_chunks; i++)
{
png_unknown_chunk *up = entries + i;
/* are we before or after the IDAT part? */
if (after_idat != !!(up->location & PNG_AFTER_IDAT))
continue;
/* macros to extract big-endian short and long ints */
#define SH(p) ((unsigned short)((p)[1]) | (((p)[0]) << 8))
#define LG(p) ((unsigned long)(SH((p)+2)) | ((ulg)(SH(p)) << 16))
if (!memcmp(up->name, "gIFg", 5))
{
fprintf(fpout, "gIFg {\n");
fprintf(fpout, " disposal: %d; input: %d; delay %f;\n",
up->data[0], up->data[1], (float)(.01 * SH(up->data+2)));
}
else if (!memcmp(up->name, "gIFx", 5))
{
fprintf(fpout, "gIFx {\n");
fprintf(fpout, " identifier: \"%.*s\"; code: \"%c%c%c\"\n",
8, up->data, up->data[8], up->data[9], up->data[10]);
dump_data(fpout, " data: ", up->size - 11, up->data + 11);
}
else
{
fprintf(fpout, "private %s {\n", up->name);
dump_data(fpout, " ", up->size, up->data);
}
fprintf(fpout, "}\n");
#undef SH
#undef LG
}
}
/*****************************************************************************
*
* Compiler main sequence
*
*****************************************************************************/
void sngdump(png_byte *row_pointers[], FILE *fpout)
/* dump a canonicalized SNG form of a PNG file */
{
fprintf(fpout, "#SNG: from %s\n", current_file);
dump_IHDR(fpout); /* first critical chunk */
dump_cHRM(fpout);
dump_gAMA(fpout);
dump_iCCP(fpout);
dump_sBIT(fpout);
dump_sRGB(fpout);
dump_PLTE(fpout); /* second critical chunk */
dump_bKGD(fpout);
dump_hIST(fpout);
dump_tRNS(fpout);
dump_pHYs(fpout);
dump_sPLT(fpout);
dump_oFFs(fpout);
dump_pCAL(fpout);
dump_sCAL(fpout);
dump_unknown_chunks(FALSE, fpout);
/*
* This is the earliest point at which we could write the image data;
* the ancillary chunks after this point have no order contraints.
* We choose to write the image last so that viewers/editors can get
* a look at all the ancillary information.
*/
dump_tIME(fpout);
dump_text(fpout);
dump_image(row_pointers, fpout); /* third critical chunk */
dump_unknown_chunks(TRUE, fpout);
}
int sngd(FILE *fp, char *name, FILE *fpout)
/* read and decompile an SNG image presented on stdin */
{
#ifndef PNG_INFO_IMAGE_SUPPORTED
png_bytepp row_pointers;
png_uint_32 row;
png_uint_32 height;
#endif
current_file = name;
sng_error = 0;
/* Create and initialize the png_struct with the desired error handler
* functions. If you want to use the default stderr and longjump method,
* you can supply NULL for the last three parameters. We also supply the
* the compiler header file version, so that we know if the application
* was compiled with a compatible version of the library. REQUIRED
*/
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL)
{
fclose(fp);
return(1);
}
/* Allocate/initialize the memory for image information. REQUIRED. */
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
{
fclose(fp);
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);