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
|
/*============================================================================
libpamcolor.c
==============================================================================
These are the library functions, which belong in the libnetpbm library,
that deal with colors in the PAM image format.
This file was originally written by Bryan Henderson and is contributed
to the public domain by him and subsequent authors.
=============================================================================*/
/* See pmfileio.c for the complicated explanation of this 32/64 bit file
offset stuff.
*/
#define _FILE_OFFSET_BITS 64
#define _LARGE_FILES
#include <string.h>
#include <limits.h>
#include <math.h>
#include "netpbm/pm_c_util.h"
#include "netpbm/mallocvar.h"
#include "netpbm/nstring.h"
#include "netpbm/colorname.h"
#include "netpbm/pam.h"
#include "netpbm/ppm.h"
static unsigned int
hexDigitValue(char const digit) {
switch (digit) {
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case 'a': case 'A': return 10;
case 'b': case 'B': return 11;
case 'c': case 'C': return 12;
case 'd': case 'D': return 13;
case 'e': case 'E': return 14;
case 'f': case 'F': return 15;
default:
pm_error("Invalid hex digit '%c'", digit);
return 0; /* Defeat compiler warning */
}
}
static void
parseHexDigits(const char * const string,
char const delim,
samplen * const nP,
unsigned int * const digitCtP) {
unsigned int digitCt;
unsigned long n;
unsigned long range;
/* 16 for one hex digit, 256 for two hex digits, etc. */
for (digitCt = 0, n = 0, range = 1; string[digitCt] != delim; ) {
char const digit = string[digitCt];
if (digit == '\0')
pm_error("rgb: color spec '%s' ends prematurely", string);
else {
n = n * 16 + hexDigitValue(digit);
range *= 16;
++digitCt;
}
}
if (range <= 1)
pm_error("No digits where hexadecimal number expected in rgb: "
"color spec '%s'", string);
*nP = (samplen) n / (range-1);
*digitCtP = digitCt;
}
static void
parseNewHexX11(char const colorname[],
tuplen const color) {
/*----------------------------------------------------------------------------
Determine what color colorname[] specifies in the new style hex
color specification format (e.g. rgb:55/40/55).
Return that color as *colorP.
Assume colorname[] starts with "rgb:", but otherwise it might be
gibberish.
-----------------------------------------------------------------------------*/
const char * cp;
unsigned int digitCt;
cp = &colorname[4];
parseHexDigits(cp, '/', &color[PAM_RED_PLANE], &digitCt);
cp += digitCt;
++cp; /* Skip the slash */
parseHexDigits(cp, '/', &color[PAM_GRN_PLANE], &digitCt);
cp += digitCt;
++cp; /* Skip the slash */
parseHexDigits(cp, '\0', &color[PAM_BLU_PLANE], &digitCt);
}
static bool
isNormal(samplen const arg) {
return arg >= 0.0 && arg <= 1.0;
}
static void
parseNewDecX11(const char * const colorname,
tuplen const color) {
int rc;
rc = sscanf(colorname, "rgbi:%f/%f/%f",
&color[PAM_RED_PLANE],
&color[PAM_GRN_PLANE],
&color[PAM_BLU_PLANE]);
if (rc != 3)
pm_error("invalid color specifier '%s'", colorname);
if (!(isNormal(color[PAM_RED_PLANE]) &&
isNormal(color[PAM_GRN_PLANE]) &&
isNormal(color[PAM_BLU_PLANE]))) {
pm_error("invalid color specifier '%s' - "
"values must be between 0.0 and 1.0", colorname);
}
}
static void
parseInteger(const char * const colorname,
tuplen const color) {
unsigned int maxval;
unsigned int r, g, b;
int rc;
rc = sscanf(colorname, "rgb-%u:%u/%u/%u", &maxval, &r, &g, &b);
if (rc != 4)
pm_error("invalid color specifier '%s'. "
"If it starts with \"rgb-\", then it must have the format "
"rgb-<MAXVAL>:<RED>:<GRN>:<BLU>, "
"where <MAXVAL>, <RED>, <GRN>, and <BLU> are "
"unsigned integers",
colorname);
if (maxval < 1 || maxval > PNM_OVERALLMAXVAL)
pm_error("Maxval in color specification '%s' is %u, "
"which is invalid because it is not between "
"1 and %u, inclusive",
colorname, maxval, PNM_OVERALLMAXVAL);
if (r > maxval)
pm_error("Red value in color specification '%s' is %u, "
"which is invalid because the specified maxval is %u",
colorname, r, maxval);
if (g > maxval)
pm_error("Green value in color specification '%s' is %u, "
"which is invalid because the specified maxval is %u",
colorname, g, maxval);
if (b > maxval)
pm_error("Blue value in color specification '%s' is %u, "
"which is invalid because the specified maxval is %u",
colorname, b, maxval);
color[PAM_RED_PLANE] = (float)r/maxval;
color[PAM_GRN_PLANE] = (float)g/maxval;
color[PAM_BLU_PLANE] = (float)b/maxval;
}
static void
parseOldX11(const char * const colorname,
tuplen const color) {
/*----------------------------------------------------------------------------
Return as *colorP the color specified by the old X11 style color
specififier colorname[] (e.g. #554055).
-----------------------------------------------------------------------------*/
if (!pm_strishex(&colorname[1]))
pm_error("Non-hexadecimal characters in #-type color specification");
switch (strlen(colorname) - 1 /* (Number of hex digits) */) {
case 3:
color[PAM_RED_PLANE] = (samplen)hexDigitValue(colorname[1])/15;
color[PAM_GRN_PLANE] = (samplen)hexDigitValue(colorname[2])/15;
color[PAM_BLU_PLANE] = (samplen)hexDigitValue(colorname[3])/15;
break;
case 6:
color[PAM_RED_PLANE] =
((samplen)(hexDigitValue(colorname[1]) << 4) +
(samplen)(hexDigitValue(colorname[2]) << 0))
/ 255;
color[PAM_GRN_PLANE] =
((samplen)(hexDigitValue(colorname[3]) << 4) +
(samplen)(hexDigitValue(colorname[4]) << 0))
/ 255;
color[PAM_BLU_PLANE] =
((samplen)(hexDigitValue(colorname[5]) << 4) +
(samplen)(hexDigitValue(colorname[6]) << 0))
/ 255;
break;
case 9:
color[PAM_RED_PLANE] =
((samplen)(hexDigitValue(colorname[1]) << 8) +
(samplen)(hexDigitValue(colorname[2]) << 4) +
(samplen)(hexDigitValue(colorname[3]) << 0))
/ 4095;
color[PAM_GRN_PLANE] =
((samplen)(hexDigitValue(colorname[4]) << 8) +
(samplen)(hexDigitValue(colorname[5]) << 4) +
(samplen)(hexDigitValue(colorname[6]) << 0))
/ 4095;
color[PAM_BLU_PLANE] =
((samplen)(hexDigitValue(colorname[7]) << 8) +
(samplen)(hexDigitValue(colorname[8]) << 4) +
(samplen)(hexDigitValue(colorname[9]) << 0))
/ 4095;
break;
case 12:
color[PAM_RED_PLANE] =
((samplen)(hexDigitValue(colorname[1]) << 12) +
(samplen)(hexDigitValue(colorname[2]) << 8) +
(samplen)(hexDigitValue(colorname[3]) << 4) +
(samplen)(hexDigitValue(colorname[4]) << 0))
/ 65535;
color[PAM_GRN_PLANE] =
((samplen)(hexDigitValue(colorname[5]) << 12) +
(samplen)(hexDigitValue(colorname[6]) << 8) +
(samplen)(hexDigitValue(colorname[7]) << 4) +
(samplen)(hexDigitValue(colorname[8]) << 0))
/ 65535;
color[PAM_BLU_PLANE] =
((samplen)(hexDigitValue(colorname[ 9]) << 12) +
(samplen)(hexDigitValue(colorname[10]) << 8) +
(samplen)(hexDigitValue(colorname[11]) << 4) +
(samplen)(hexDigitValue(colorname[12]) << 0))
/ 65535;
break;
default:
pm_error("invalid color specifier '%s'", colorname);
}
}
static void
parseOldX11Dec(const char* const colorname,
tuplen const color) {
int rc;
rc = sscanf(colorname, "%f,%f,%f",
&color[PAM_RED_PLANE],
&color[PAM_GRN_PLANE],
&color[PAM_BLU_PLANE]);
if (rc != 3)
pm_error("invalid color specifier '%s'", colorname);
if (!(isNormal(color[PAM_RED_PLANE]) &&
isNormal(color[PAM_GRN_PLANE]) &&
isNormal(color[PAM_BLU_PLANE]))) {
pm_error("invalid color specifier '%s' - "
"values must be between 0.0 and 1.0", colorname);
}
}
tuplen
pnm_parsecolorn(const char * const colorname) {
tuplen retval;
MALLOCARRAY_NOFAIL(retval, 3);
if (strneq(colorname, "rgb:", 4))
/* It's a new-X11-style hexadecimal rgb specifier. */
parseNewHexX11(colorname, retval);
else if (strneq(colorname, "rgbi:", 5))
/* It's a new-X11-style decimal/float rgb specifier. */
parseNewDecX11(colorname, retval);
else if (strneq(colorname, "rgb-", 4))
/* It's a Netpbm-native decimal integer rgb specifier */
parseInteger(colorname, retval);
else if (colorname[0] == '#')
/* It's an old-X11-style hexadecimal rgb specifier. */
parseOldX11(colorname, retval);
else if ((colorname[0] >= '0' && colorname[0] <= '9') ||
colorname[0] == '.')
/* It's an old-style decimal/float rgb specifier. */
parseOldX11Dec(colorname, retval);
else
/* Must be a name from the X-style rgb file. */
pm_parse_dictionary_namen(colorname, retval);
return retval;
}
static void
warnIfNotExact(const char * const colorname,
tuple const rounded,
tuplen const exact,
sample const maxval,
unsigned int const plane) {
float const epsilon = 1.0/65536.0;
if (fabs((float)(rounded[plane] / maxval) - exact[plane]) > epsilon) {
pm_message("WARNING: Component %u of color '%s' is %f, "
"which cannot be represented precisely with maxval %lu. "
"Approximating as %lu.",
plane, colorname, exact[plane], maxval, rounded[plane]);
}
}
tuple
pnm_parsecolor2(const char * const colorname,
sample const maxval,
int const closeOk) {
tuple retval;
tuplen color;
struct pam pam;
pam.len = PAM_STRUCT_SIZE(bytes_per_sample);
pam.depth = 3;
pam.maxval = maxval;
pam.bytes_per_sample = pnm_bytespersample(maxval);
retval = pnm_allocpamtuple(&pam);
color = pnm_parsecolorn(colorname);
pnm_unnormalizetuple(&pam, color, retval);
if (!closeOk) {
warnIfNotExact(colorname, retval, color, maxval, PAM_RED_PLANE);
warnIfNotExact(colorname, retval, color, maxval, PAM_GRN_PLANE);
warnIfNotExact(colorname, retval, color, maxval, PAM_BLU_PLANE);
}
free(color);
return retval;
}
tuple
pnm_parsecolor(const char * const colorname,
sample const maxval) {
return pnm_parsecolor2(colorname, maxval, true);
}
const char *
pnm_colorname(struct pam * const pamP,
tuple const color,
int const hexok) {
const char * retval;
pixel colorp;
char * colorname;
if (pamP->depth < 3)
PPM_ASSIGN(colorp, color[0], color[0], color[0]);
else
PPM_ASSIGN(colorp,
color[PAM_RED_PLANE],
color[PAM_GRN_PLANE],
color[PAM_BLU_PLANE]);
colorname = ppm_colorname(&colorp, pamP->maxval, hexok);
retval = pm_strdup(colorname);
if (retval == pm_strsol)
pm_error("Couldn't get memory for color name string");
return retval;
}
static tuple
scaledRgb(struct pam * const pamP,
tuple const color,
sample const maxval) {
tuple scaledColor;
struct pam pam;
pam.size = sizeof(pam);
pam.len = PAM_STRUCT_SIZE(allocation_depth);
pam.maxval = pamP->maxval;
pam.depth = pamP->depth;
pam.allocation_depth = 3;
scaledColor = pnm_allocpamtuple(&pam);
pnm_scaletuple(&pam, scaledColor, color, maxval);
pnm_maketuplergb(&pam, scaledColor);
return scaledColor;
}
const char *
pnm_colorspec_rgb_integer(struct pam * const pamP,
tuple const color,
sample const maxval) {
const char * retval;
tuple scaledColor = scaledRgb(pamP, color, maxval);
pm_asprintf(&retval, "rgb-%lu:%lu/%lu/%lu",
maxval,
scaledColor[PAM_RED_PLANE],
scaledColor[PAM_GRN_PLANE],
scaledColor[PAM_BLU_PLANE]
);
pnm_freepamtuple(scaledColor);
return retval;
}
const char *
pnm_colorspec_rgb_norm(struct pam * const pamP,
tuple const color,
unsigned int const digitCt) {
const char * retval;
tuple rgbColor;
tuplen normColor;
struct pam rgbPam;
rgbPam.size = sizeof(rgbPam);
rgbPam.len = PAM_STRUCT_SIZE(allocation_depth);
rgbPam.maxval = pamP->maxval;
rgbPam.depth = pamP->depth;
rgbPam.allocation_depth = 3;
rgbColor = pnm_allocpamtuple(&rgbPam);
pnm_assigntuple(&rgbPam, rgbColor, color); /* initial value */
pnm_maketuplergb(&rgbPam, rgbColor);
normColor = pnm_allocpamtuplen(&rgbPam);
rgbPam.depth = 3;
pnm_normalizetuple(&rgbPam, rgbColor, normColor);
{
const char * format;
pm_asprintf(&format, "rgbi:%%.%uf/%%.%uf/%%.%uf",
digitCt, digitCt, digitCt);
pm_asprintf(&retval, format,
normColor[PAM_RED_PLANE],
normColor[PAM_GRN_PLANE],
normColor[PAM_BLU_PLANE]
);
pm_strfree(format);
}
pnm_freepamtuplen(normColor);
pnm_freepamtuple(rgbColor);
return retval;
}
const char *
pnm_colorspec_rgb_x11(struct pam * const pamP,
tuple const color,
unsigned int const hexDigitCt) {
const char * retval;
sample maxval;
const char * format;
switch(hexDigitCt) {
case 1:
maxval = 15;
format = "rgb:%01x:%01x:%01x";
break;
case 2:
maxval = 255;
format = "rgb:%02x:%02x:%02x";
break;
case 3:
maxval = 4095;
format = "rgb:%03x:%03x:%03x";
break;
case 4:
maxval = 65535;
format = "rgb:%04x:%04x:%04x";
break;
default:
pm_error("Invalid number of hex digits "
"for X11 color specification: %u. "
"Must be 1, 2, 3, or 4", hexDigitCt);
}
{
tuple const scaledColor = scaledRgb(pamP, color, maxval);
pm_asprintf(&retval, format,
scaledColor[PAM_RED_PLANE],
scaledColor[PAM_GRN_PLANE],
scaledColor[PAM_BLU_PLANE]
);
pnm_freepamtuple(scaledColor);
}
return retval;
}
const char *
pnm_colorspec_dict(struct pam * const pamP,
tuple const color) {
/*----------------------------------------------------------------------------
Return the name from the color dictionary of color 'color'.
Return it in newly allocated pm_strdrup storage.
If the color is not in the dictionary, or the dictionary doesn't even
exist (file not found in any of the possible places), return NULL.
The color dictionary uses maxval 255, so we match to that precision.
E.g. if a component of 'color' is 1000 out of maxval 65535 (which would be
3.9 out of maxval 255), we consider it a match to a component value of 4
in the color dictionary.
-----------------------------------------------------------------------------*/
tuple scaledColor = scaledRgb(pamP, color, PAM_COLORFILE_MAXVAL);
FILE * dictFileP;
const char * colorname;
dictFileP = pm_openColornameFile(NULL, false);
if (dictFileP) {
bool eof;
for (colorname = NULL, eof = false; !colorname && !eof; ) {
struct colorfile_entry const ce = pm_colorget(dictFileP);
if (ce.colorname) {
if (scaledColor[PAM_RED_PLANE] == (sample)ce.r &&
scaledColor[PAM_GRN_PLANE] == (sample)ce.g &&
scaledColor[PAM_BLU_PLANE] == (sample)ce.b) {
colorname = pm_strdup(ce.colorname);
}
} else
eof = TRUE;
}
fclose(dictFileP);
} else
colorname = NULL;
pnm_freepamtuple(scaledColor);
return colorname;
}
const char *
pnm_colorspec_dict_close(struct pam * const pamP,
tuple const color) {
/*----------------------------------------------------------------------------
Return the name from the color dictionary of the color closst to 'color'.
Return it in newly allocated pm_strdrup storage.
If the color dictionary is empty, or the dictionary doesn't even exist
(file not found in any of the possible places), return a null string.
This is the only case in which we would return a null string, as the
color dictionary cannot define a null string color name.
-----------------------------------------------------------------------------*/
tuple scaledColor = scaledRgb(pamP, color, PAM_COLORFILE_MAXVAL);
FILE * dictFileP;
static char colorname[200];
dictFileP = pm_openColornameFile(NULL, false);
if (dictFileP) {
unsigned int bestDiff;
bool eof;
for (bestDiff = 32767, eof = FALSE; !eof && bestDiff > 0; ) {
struct colorfile_entry const ce = pm_colorget(dictFileP);
if (ce.colorname) {
unsigned int const thisDiff =
abs((int)scaledColor[PAM_RED_PLANE] - (int)ce.r) +
abs((int)scaledColor[PAM_GRN_PLANE] - (int)ce.g) +
abs((int)scaledColor[PAM_BLU_PLANE] - (int)ce.b);
if (thisDiff < bestDiff) {
bestDiff = thisDiff;
STRSCPY(colorname, ce.colorname);
}
} else
eof = TRUE;
}
fclose(dictFileP);
if (bestDiff == 32767) {
/* Color file contain no entries, so we can't even pick a close
one
*/
STRSCPY(colorname, "");
}
} else
STRSCPY(colorname, "");
pnm_freepamtuple(scaledColor);
return pm_strdup(colorname);
}
double pnm_lumin_factor[3] = {PPM_LUMINR, PPM_LUMING, PPM_LUMINB};
void
pnm_YCbCrtuple(tuple const tuple,
double * const YP,
double * const CbP,
double * const CrP) {
/*----------------------------------------------------------------------------
Assuming that the tuple 'tuple' is of tupletype RGB, return the
Y/Cb/Cr representation of the color represented by the tuple.
-----------------------------------------------------------------------------*/
int const red = (int)tuple[PAM_RED_PLANE];
int const grn = (int)tuple[PAM_GRN_PLANE];
int const blu = (int)tuple[PAM_BLU_PLANE];
*YP = (+ PPM_LUMINR * red + PPM_LUMING * grn + PPM_LUMINB * blu);
*CbP = (- 0.16874 * red - 0.33126 * grn + 0.50000 * blu);
*CrP = (+ 0.50000 * red - 0.41869 * grn - 0.08131 * blu);
}
void
pnm_YCbCr_to_rgbtuple(const struct pam * const pamP,
tuple const tuple,
double const Y,
double const Cb,
double const Cr,
int * const overflowP) {
double rgb[3];
unsigned int plane;
bool overflow;
rgb[PAM_RED_PLANE] = Y + 1.4022 * Cr + .5;
rgb[PAM_GRN_PLANE] = Y - 0.7145 * Cr - 0.3456 * Cb + .5;
rgb[PAM_BLU_PLANE] = Y + 1.7710 * Cb + .5;
overflow = FALSE; /* initial assumption */
for (plane = 0; plane < 3; ++plane) {
if (rgb[plane] > pamP->maxval) {
overflow = TRUE;
tuple[plane] = pamP->maxval;
} else if (rgb[plane] < 0.0) {
overflow = TRUE;
tuple[plane] = 0;
} else
tuple[plane] = (sample)rgb[plane];
}
if (overflowP)
*overflowP = overflow;
}
|