-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsngc.c
1990 lines (1764 loc) · 50.4 KB
/
sngc.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
sngc.c -- compile SNG to PNG.
*****************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#define PNG_INTERNAL
#include "png.h"
#include "sng.h"
extern int verbose;
typedef int bool;
#define FALSE 0
#define TRUE 1
#define NONE -2
typedef struct {
char *name; /* name of chunk type */
bool multiple_ok; /* OK to have more than one? */
int count; /* how many have we seen? */
} chunkprops;
#ifndef PNG_KEYWORD_MAX_LENGTH
#define PNG_KEYWORD_MAX_LENGTH 79
#endif /* PNG_KEYWORD_MAX_LENGTH */
#define MEMORY_QUANTUM 1024
#define MAX_PARAMS 16
#define PNG_MAX_LONG 2147483647L /* 2^31 */
/* chunk types */
static chunkprops properties[] =
{
/*
* The PNG 1.0 chunks, listed in order of the summary table in section 4.3.
* IEND is not listed here because it doesn't have to appear in the file.
*/
#define IHDR 0
{"IHDR", FALSE, 0},
#define PLTE 1
{"PLTE", FALSE, 0},
#define IDAT 2
{"IDAT", TRUE, 0},
#define cHRM 3
{"cHRM", FALSE, 0},
#define gAMA 4
{"gAMA", FALSE, 0},
#define iCCP 5
{"iCCP", FALSE, 0},
#define sBIT 6
{"sBIT", FALSE, 0},
#define sRGB 7
{"sRGB", FALSE, 0},
#define bKGD 8
{"bKGD", FALSE, 0},
#define hIST 9
{"hIST", FALSE, 0},
#define tRNS 10
{"tRNS", FALSE, 0},
#define pHYs 11
{"pHYs", FALSE, 0},
#define sPLT 12
{"sPLT", TRUE, 0},
#define tIME 13
{"tIME", FALSE, 0},
#define iTXt 14
{"iTXt", TRUE, 0},
#define tEXt 15
{"tEXt", TRUE, 0},
#define zTXt 16
{"zTXt", TRUE, 0},
/*
* Special-purpose chunks in PNG Extensions 1.2.0 specification.
*/
#define oFFs 17
{"oFFs", FALSE, 0},
#define pCAL 18
{"pCAL", FALSE, 0},
#define sCAL 19
{"sCAL", FALSE, 0},
#define gIFg 20
{"gIFg", FALSE, 0},
#define gIFt 21
{"gIFt", FALSE, 0},
#define gIFx 22
{"gIFx", FALSE, 0},
#define fRAc 23
{"fRAc", FALSE, 0},
/*
* Image pseudo-chunk
*/
#define IMAGE 24
{"IMAGE", FALSE, 0},
/*
* Private chunks
*/
#define PRIVATE 25
{"private", TRUE, 0},
};
static png_color palette[256];
/*************************************************************************
*
* Color-name handling
*
************************************************************************/
static color_item *cname_hashbuckets[COLOR_HASH_MODULUS];
static int cname_initialized;
static int write_transform_options;
static int hash_by_cname(color_item *cp)
/* hash by color's RGB value */
{
unsigned int h = 0;
char *p;
for (p = cp->name; *p; p++)
h = COLOR_HASH_MODULUS * h + *p;
return(h % COLOR_HASH_MODULUS);
}
static color_item *find_by_cname(char *name)
/* find a character name */
{
color_item sc, *hp;
sc.name = name;
for (hp = cname_hashbuckets[hash_by_cname(&sc)]; hp; hp = hp->next)
if (strcmp(hp->name, name) == 0)
return(hp);
return((color_item *)NULL);
}
/*************************************************************************
*
* Token-parsing code
*
************************************************************************/
static char token_buffer[BUFSIZ];
static int token_class;
#define STRING_TOKEN 1
#define PUNCT_TOKEN 2
#define WORD_TOKEN 3
static bool pushed;
static void escapes(cp, tp)
/* process standard C-style escape sequences in a string */
const char *cp; /* source string with escapes */
char *tp; /* target buffer for digested string */
{
while (*cp)
{
int cval = 0;
if (*cp == '\\' && strchr("0123456789xX", cp[1]))
{
char *dp;
const char *hex = "00112233445566778899aAbBcCdDeEfF";
int dcount = 0;
if (*++cp == 'x' || *cp == 'X')
for (++cp; (dp = strchr(hex, *cp)) && (dcount++ < 2); cp++)
cval = (cval * 16) + (dp - hex) / 2;
else if (*cp == '0')
while (strchr("01234567",*cp) != (char*)NULL && (dcount++ < 3))
cval = (cval * 8) + (*cp++ - '0');
else
while ((strchr("0123456789",*cp)!=(char*)NULL)&&(dcount++ < 3))
cval = (cval * 10) + (*cp++ - '0');
}
else if (*cp == '\\') /* C-style character escapes */
{
switch (*++cp)
{
case '\\': cval = '\\'; break;
case 'n': cval = '\n'; break;
case 't': cval = '\t'; break;
case 'b': cval = '\b'; break;
case 'r': cval = '\r'; break;
default: cval = *cp;
}
cp++;
}
else
cval = *cp++;
*tp++ = cval;
}
*tp = '\0';
}
static int get_token(void)
/* grab a token from yyin */
{
char w, c, *tp = token_buffer;
if (pushed)
{
pushed = FALSE;
if (verbose > 1)
fprintf(stderr, "saved token: %s\n", token_buffer);
return(TRUE);
}
/*
* Skip leading whitespace.
*
* Treat commas, colons, and semicolons as whitespace -- this is
* a mildly sleazy way to ensure that the compiler has
* *really* tolerant syntax...in case we ever decide to change
* this, there are comments reading "comma" at every point
* where we might actually want same.
*/
for (;;)
{
w = fgetc(yyin);
if (w == '\n')
linenum++;
if (feof(yyin))
return(FALSE);
else if (isspace(w) || w == ',' || w == ';' || w == ':')
continue;
else if (w == '#') /* comment */
{
for (;;)
{
w = fgetc(yyin);
if (feof(yyin))
return(FALSE);
if (w == '\n')
{
ungetc(w, yyin);
break;
}
}
}
else /* non-space character */
{
*tp++ = w;
break;
}
}
/* accumulate token */
if (w == '\'' || w == '"')
{
int literal = FALSE;
tp = token_buffer;
for (;;)
{
c = fgetc(yyin);
if (feof(yyin))
return(FALSE);
else if (c == '\\' && !literal)
{
literal = TRUE;
continue;
}
else if (c == w && !literal)
break;
else if (c == '\n' && !literal)
fatal("runaway string");
else if (tp >= token_buffer + sizeof(token_buffer) - 1)
fatal("string token too long");
else
{
if (literal)
{
*tp++ = '\\';
literal = FALSE;
}
*tp++ = c;
}
}
*tp = '\0';
escapes(token_buffer, token_buffer);
token_class = STRING_TOKEN;
}
else if (ispunct(w))
{
*tp = '\0';
token_class = PUNCT_TOKEN;
}
else
{
for (;;)
{
c = fgetc(yyin);
if (feof(yyin))
return(FALSE);
else if (isspace(c))
{
if (c == '\n')
linenum++;
break;
}
else if (ispunct(c) && c != '.')
{
ungetc(c, yyin);
break;
}
else if (tp >= token_buffer + sizeof(token_buffer))
fatal("token too long");
else
*tp++ = c;
}
*tp = '\0';
token_class = WORD_TOKEN;
}
if (verbose > 1)
fprintf(stderr, "token: %s\n", token_buffer);
return(TRUE);
}
static int token_equals(const char *str)
/* does the currently fetched token equal a specified string? */
{
return !strcmp(str, token_buffer);
}
static int get_inner_token(void)
/* get a token within a chunk specification */
{
if (!get_token())
fatal("unexpected EOF");
return(!token_equals("}")); /* do we see end delimiter? */
}
static void push_token(void)
/* push back a token; must always be followed immediately by get_token */
{
if (verbose > 1)
fprintf(stderr, "pushing token: %s\n", token_buffer);
pushed = TRUE;
}
static void require_or_die(const char *str)
/* croak if the next token doesn't match what we expect */
{
if (!get_token())
fatal("unexpected EOF");
else if (!token_equals(str))
fatal("unexpected token `%s' while waiting for %s", token_buffer, str);
}
static png_uint_32 long_numeric(bool token_ok)
/* validate current token as a PNG long (range 0..2^31-1) */
{
unsigned long result;
char *vp;
if (!token_ok)
fatal("EOF while expecting long-integer constant");
result = strtoul(token_buffer, &vp, 0);
if (*vp || result >= PNG_MAX_LONG)
fatal("invalid or out of range long constant `%s'", token_buffer);
return(result);
}
static png_int_32 slong_numeric(bool token_ok)
/* validate current token as a signed PNG long (range 0..2^31-1) */
{
long result;
char *vp;
if (!token_ok)
fatal("EOF while expecting signed long-integer constant");
result = strtol(token_buffer, &vp, 0);
if (*vp || result >= PNG_MAX_LONG || result <= -PNG_MAX_LONG)
fatal("invalid or out of range long constant `%s'", token_buffer);
return(result);
}
static png_uint_16 short_numeric(bool token_ok)
/* validate current token as a PNG long (range 0..2^16-1) */
{
unsigned long result;
char *vp;
if (!token_ok)
fatal("EOF while expecting short-integer constant");
result = strtoul(token_buffer, &vp, 0);
if (*vp || result == 65536)
fatal("invalid or out of range short constant `%s'", token_buffer);
return(result);
}
static png_byte byte_numeric(bool token_ok)
/* validate current token as a byte */
{
unsigned long result;
char *vp;
if (!token_ok)
fatal("EOF while expecting byte constant");
result = strtoul(token_buffer, &vp, 0);
if (*vp || result > 255)
fatal("invalid or out of range byte constant `%s'", token_buffer);
return(result);
}
static double double_numeric(bool token_ok)
/* validate current token as a double-precision value */
{
double result;
char *vp;
if (!token_ok)
fatal("EOF while expecting double-precision constant");
result = strtod(token_buffer, &vp);
if (*vp || result < 0)
fatal("invalid or out of range double-precision constant `%s'", token_buffer);
return(result);
}
static int string_validate(bool token_ok, char *stash)
/* validate current token as a string */
{
if (!token_ok)
fatal("EOF while expecting string constant");
/* else */
{
int len = strlen(token_buffer);
if (len > PNG_STRING_MAX_LENGTH)
fatal("string token is too long");
strncpy(stash, token_buffer, PNG_STRING_MAX_LENGTH);
return(len);
}
}
static int keyword_validate(bool token_ok, char *stash)
/* validate current token as a PNG keyword */
{
if (!token_ok)
fatal("EOF while expecting PNG keyword");
/* else */
{
int len = strlen(token_buffer);
unsigned char *cp;
if (len > PNG_KEYWORD_MAX_LENGTH)
fatal("keyword token is too long");
strncpy(stash, token_buffer, PNG_KEYWORD_MAX_LENGTH);
if (isspace(stash[0]) || isspace(stash[len-1]))
fatal("keywords may not contain leading or trailing spaces");
for (cp = (unsigned char *)stash; *cp; cp++)
if (*cp < 32 || (*cp > 126 && *cp < 161))
fatal("keywords must contain Latin-1 characters only");
else if (isspace(cp[0]) && isspace(cp[1]))
fatal("keywords may not contain consecutive spaces");
return(len);
}
}
static void collect_data(int *pnbytes, char **pbytes)
/* collect data in either bitmap format */
{
/*
* A data segment consists of a byte stream.
* There are presently three formats:
*
* string:
* An ASCII string, doublequote-delimited.
*
* base64:
* One character per byte; values are as per RFC2045 base64:
* 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/
*
* hex:
* Two hex digits per byte.
*
* P1:
* pbm format P1 (see pbm(5)).
*
* P3:
* ppm format P3 (see ppm(5)).
*
* In either format, whitespace is ignored.
*/
char *bytes = xalloc(MEMORY_QUANTUM);
int quanta = 1;
int nbytes = 0;
int ocount = 0;
int c, maxval = 0;
#define BASE64_FMT 0
#define HEX_FMT 1
#define P1_FMT 2
#define P3_FMT 3
int fmt = 0;
if (!get_inner_token())
fatal("missing format type in data segment");
else if (token_class == STRING_TOKEN)
{
*pnbytes = 0;
*pbytes = NULL;
do {
int seglen = strlen(token_buffer);
*pbytes = xrealloc(*pbytes, *pnbytes + seglen);
memcpy(*pbytes + *pnbytes, token_buffer, seglen);
*pnbytes += seglen;
} while
(get_inner_token() && token_class == STRING_TOKEN);
push_token();
return;
}
else if (token_equals("base64"))
fmt = BASE64_FMT;
else if (token_equals("hex"))
fmt = HEX_FMT;
else if (token_equals("P1"))
{
int width = short_numeric(get_token());
int height = short_numeric(get_token());
if (width != info_ptr->width && height != info_ptr->height)
fatal("pbm image dimensions don't mastch IHDR");
fmt = P1_FMT;
}
else if (token_equals("P3"))
{
int width = short_numeric(get_token());
int height = short_numeric(get_token());
maxval = short_numeric(get_token());
if (width != info_ptr->width && height != info_ptr->height)
fatal("ppm image dimensions don't match IHDR");
fmt = P3_FMT;
}
else
fatal("unknown data format");
while ((c = fgetc(yyin)))
{
if (feof(yyin))
fatal("unexpected EOF in data segment");
else if (c == ';')
break;
else if (c == '}')
{
ungetc('}', yyin);
break;
}
else if (c == '#') /* handle comments */
{
while ((c = fgetc(yyin)))
if (feof(yyin) || c == '\n')
break;
if (c == '\n')
linenum++;
continue;
}
else if (isspace(c)) /* skip whitespace */
{
if (c == '\n')
linenum++;
continue;
}
else
{
unsigned char value = 0;
if (nbytes >= quanta * MEMORY_QUANTUM)
bytes = xrealloc(bytes, MEMORY_QUANTUM * ++quanta);
switch(fmt)
{
case BASE64_FMT:
if (!isalpha(c) && !isdigit(c))
fatal("bad character %02x in data block", c);
else if (isdigit(c))
value = c - '0';
else if (isupper(c))
value = (c - 'A') + 10;
else if (islower(c))
value = (c - 'a') + 36;
else if (c == '+')
value = 62;
else /* if (c == '/') */
value = 63;
bytes[nbytes++] = value;
break;
case HEX_FMT:
if (!isxdigit(c))
fatal("bad hex character %02x in data block", c);
else if (isdigit(c))
value = c - '0';
else if (isupper(c))
value = (c - 'A') + 10;
else
value = (c - 'a') + 10;
if (ocount++ % 2)
bytes[nbytes++] |= value;
else
bytes[nbytes] = value * 16;
break;
case P1_FMT:
if (c == '0')
bytes[nbytes++] = 0;
else if (c == '1')
bytes[nbytes++] = 1;
else
fatal("bad pbm character %02x in data block", c);
break;
case P3_FMT:
c = short_numeric(get_token());
if (c > maxval)
fatal("channel value out of range in pbm block");
/*
* Channel order in PBM is R, then G, then B, same as PNG;
* so a straight copy in the order we see them will work.
*/
bytes[nbytes++] = c;
break;
}
}
}
#undef BASE64_FMT
#undef HEX_FMT
#undef P1_FMT
#undef P3_FMT
*pnbytes = nbytes;
*pbytes = bytes;
}
/*************************************************************************
*
* The compiler itself
*
************************************************************************/
static void compile_IHDR(void)
/* parse IHDR specification, set corresponding bits in info_ptr */
{
int d = 0;
/* read IHDR data */
info_ptr->bit_depth = 8;
info_ptr->color_type = PNG_COLOR_TYPE_GRAY;
info_ptr->interlace_type = PNG_INTERLACE_NONE;
while (get_inner_token())
if (token_equals("height"))
info_ptr->height = long_numeric(get_token());
else if (token_equals("width"))
info_ptr->width = long_numeric(get_token());
else if (token_equals("bitdepth"))
d = byte_numeric(get_token());
else if (token_equals("using"))
continue; /* `uses' is just syntactic sugar */
else if (token_equals("grayscale"))
continue; /* so is grayscale */
else if (token_equals("palette"))
info_ptr->color_type |= PNG_COLOR_MASK_PALETTE;
else if (token_equals("color"))
info_ptr->color_type |= PNG_COLOR_MASK_COLOR;
else if (token_equals("alpha"))
info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
else if (token_equals("with"))
continue; /* `with' is just syntactic sugar */
else if (token_equals("interlace"))
info_ptr->interlace_type = PNG_INTERLACE_ADAM7;
else
fatal("bad token `%s' in IHDR specification", token_buffer);
/* IHDR sanity checks */
if (!info_ptr->height)
fatal("image height is zero or nonexistent");
else if (!info_ptr->width)
fatal("image width is zero or nonexistent");
else if (d != 1 && d != 2 && d != 4 && d != 8 && d != 16)
fatal("illegal bit depth %d in IHDR", d);
else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
{
if (d > 8)
fatal("bit depth of paletted images must be 1, 2, 4, or 8");
}
else if ((info_ptr->color_type != PNG_COLOR_TYPE_GRAY) && d!=8 && d!=16)
fatal("bit depth of RGB- and alpha-using images must be 8 or 16");
info_ptr->bit_depth = d;
}
static void compile_PLTE(void)
/* parse PLTE specification, set corresponding bits in info_ptr */
{
int ncolors;
initialize_hash(hash_by_cname, cname_hashbuckets, &cname_initialized);
memset(palette, '\0', sizeof(palette));
ncolors = 0;
while (get_inner_token())
{
if (ncolors >= 256)
fatal("too many palette entries in PLTE specification");
if (token_class == STRING_TOKEN)
{
color_item *cp = find_by_cname(token_buffer);
if (!cp)
fatal("unknown color name `%s' in PLTE", token_buffer);
else
{
palette[ncolors].red = cp->r;
palette[ncolors].green = cp->g;
palette[ncolors].blue = cp->b;
ncolors++;
}
}
else if (token_equals("("))
{
palette[ncolors].red = byte_numeric(get_token());
/* comma */
palette[ncolors].green = byte_numeric(get_token());
/* comma */
palette[ncolors].blue = byte_numeric(get_token());
require_or_die(")");
ncolors++;
}
else
fatal("bad token %s in PLTE", token_buffer);
}
/* register the accumulated palette entries into the info structure */
png_set_PLTE(png_ptr, info_ptr, palette, ncolors);
}
static void compile_IDAT(void)
/* parse IDAT specification and emit corresponding bits */
{
int nbits;
char *bits;
#ifdef PNG_INFO_IMAGE_SUPPORTED
png_unknown_chunk chunk;
#endif /* PNG_INFO_IMAGE_SUPPORTED */
/*
* Collect raw hex data and write it out as a chunk.
*/
collect_data(&nbits, &bits);
require_or_die("}");
#ifndef PNG_INFO_IMAGE_SUPPORTED
png_write_chunk(png_ptr, "IDAT", bits, nbits);
free(bits);
#else
strcpy(chunk.name, "IDAT");
chunk.data = bits;
chunk.size = nbits;
png_set_unknown_chunks(png_ptr, info_ptr, &chunk, 1);
png_free(png_ptr, bits);
#endif /* PNG_INFO_IMAGE_SUPPORTED */
}
static void compile_cHRM(void)
/* parse cHRM specification, set corresponding bits in info_ptr */
{
char cmask = 0;
float wx = 0, wy = 0, rx = 0, ry = 0, gx = 0, gy = 0, bx = 0, by = 0;
while (get_inner_token())
{
if (token_equals("white"))
{
require_or_die("(");
wx = double_numeric(get_inner_token());
/* comma */
wy = double_numeric(get_inner_token());
require_or_die(")");
cmask |= 0x01;
}
else if (token_equals("red"))
{
require_or_die("(");
rx = double_numeric(get_inner_token());
/* comma */
ry = double_numeric(get_inner_token());
require_or_die(")");
cmask |= 0x02;
}
else if (token_equals("green"))
{
require_or_die("(");
gx = double_numeric(get_inner_token());
/* comma */
gy = double_numeric(get_inner_token());
require_or_die(")");
cmask |= 0x04;
}
else if (token_equals("blue"))
{
require_or_die("(");
bx = double_numeric(get_inner_token());
/* comma */
by = double_numeric(get_inner_token());
require_or_die(")");
cmask |= 0x08;
}
else
fatal("invalid color `%s' name in cHRM specification",token_buffer);
}
if (cmask != 0x0f)
fatal("cHRM specification is not complete");
else
{
#ifdef PNG_FLOATING_POINT_SUPPORTED
png_set_cHRM(png_ptr, info_ptr,
wx, wy, rx, ry, gx, gy, bx, by);
#else
#ifdef PNG_FIXED_POINT_SUPPORTED
png_set_cHRM_fixed(png_ptr, info_ptr,
FLOAT_TO_FIXED(wx),
FLOAT_TO_FIXED(wy),
FLOAT_TO_FIXED(rx),
FLOAT_TO_FIXED(ry),
FLOAT_TO_FIXED(gx),
FLOAT_TO_FIXED(gy),
FLOAT_TO_FIXED(bx),
FLOAT_TO_FIXED(by));
#endif
#endif
}
}
static void compile_gAMA(void)
/* compile and emit an gAMA chunk */
{
double gamma = double_numeric(get_token());
#ifdef PNG_FLOATING_POINT_SUPPORTED
png_set_gAMA(png_ptr, info_ptr, gamma);
#endif
#ifdef PNG_FIXED_POINT_SUPPORTED
png_set_gAMA_fixed(png_ptr, info_ptr, FLOAT_TO_FIXED(gamma));
#endif
if (!get_token() || !token_equals("}"))
fatal("bad token `%s' in gAMA specification", token_buffer);
}
static void compile_iCCP(void)
/* compile and emit an iCCP chunk */
{
int nname = 0, data_len = 0;
char name[PNG_KEYWORD_MAX_LENGTH+1], *data;
while (get_inner_token())
if (token_equals("name"))
nname = keyword_validate(get_token(), name);
else if (token_equals("profile"))
collect_data(&data_len, &data);
if (!nname || !data_len)
fatal("incomplete iCCP specification");
png_set_iCCP(png_ptr, info_ptr, name, PNG_TEXT_COMPRESSION_NONE,
data, data_len);
free(data);
}
static void compile_sBIT(void)
/* compile an sBIT chunk, set corresponding bits in info_ptr */
{
png_color_8 sigbits;
bool color = (info_ptr->color_type & (PNG_COLOR_MASK_PALETTE | PNG_COLOR_MASK_COLOR));
int sample_depth = ((info_ptr->color_type & PNG_COLOR_MASK_PALETTE) ? 8 : info_ptr->bit_depth);
while (get_inner_token())
if (token_equals("red"))
{
if (!color)
fatal("No color channels in this image type");
sigbits.red = byte_numeric(get_token());
if (sigbits.red > sample_depth)
fatal("red sample depth out of range");
}
else if (token_equals("green"))
{
if (!color)
fatal("No color channels in this image type");
sigbits.green = byte_numeric(get_token());
if (sigbits.green > sample_depth)
fatal("red sample depth out of range");
}
else if (token_equals("blue"))
{
if (!color)
fatal("No color channels in this image type");
sigbits.blue = byte_numeric(get_token());
if (sigbits.blue > sample_depth)
fatal("red sample depth out of range");
}
else if (token_equals("gray"))
{
if (color)
fatal("No gray channel in this image type");
sigbits.gray = byte_numeric(get_token());
if (sigbits.gray > sample_depth)
fatal("gray sample depth out of range");
}
else if (token_equals("alpha"))
{
if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
fatal("No alpha channel in this image type");
sigbits.alpha = byte_numeric(get_token());
if (sigbits.alpha > sample_depth)
fatal("alpha sample depth out of range");
}
else
fatal("invalid channel name `%s' in sBIT specification",
token_buffer);
png_set_sBIT(png_ptr, info_ptr, &sigbits);
}
static void compile_bKGD(void)
/* compile a bKGD chunk, put data in info structure */
{
png_color_16 bkgbits;
while (get_inner_token())
if (token_equals("red"))
{
if (!(info_ptr->color_type & PNG_COLOR_MASK_COLOR))
fatal("Can't use color background with this image type");
bkgbits.red = short_numeric(get_token());
}
else if (token_equals("green"))
{
if (!(info_ptr->color_type & PNG_COLOR_MASK_COLOR))
fatal("Can't use color background with this image type");
bkgbits.green = short_numeric(get_token());
}
else if (token_equals("blue"))
{
if (!(info_ptr->color_type & PNG_COLOR_MASK_COLOR))
fatal("Can't use color background with this image type");
bkgbits.blue = short_numeric(get_token());
}
else if (token_equals("gray"))
{
if (info_ptr->color_type & (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE))
fatal("Can't use color background with this image type");
bkgbits.gray = short_numeric(get_token());
}
else if (token_equals("index"))
{
if (!(info_ptr->color_type & PNG_COLOR_MASK_PALETTE))
fatal("Can't use index background with a non-palette image");
bkgbits.index = byte_numeric(get_token());
}
else
fatal("invalid channel `%s' name in bKGD specification",
token_buffer);
png_set_bKGD(png_ptr, info_ptr, &bkgbits);
}
static void compile_hIST(void)
/* compile a hIST chunk, put data in info structure */
{
png_uint_16 hist[256];
int nhist = 0;
while (get_inner_token())
/* comma */
hist[nhist++] = short_numeric(TRUE);
#ifndef MNG_INTERFACE
if (nhist != info_ptr->num_palette)
fatal("number of hIST values (%d) for palette doesn't match palette size (%d)", nhist, info_ptr->num_palette);
#else
if (nhist != info_ptr->palette.size)
fatal("number of hIST values (%d) for palette doesn't match palette size (%d)", nhist, info_ptr->palette.size);
#endif /* GUG */