1 /* makesRGB.c -- build sRGB-to-linear and linear-to-sRGB conversion tables
3 * COPYRIGHT: Written by John Cunningham Bowler, 2013.
4 * To the extent possible under law, the author has waived all copyright and
5 * related or neighboring rights to this work. This work is published from:
8 * Make a table to convert 8-bit sRGB encoding values into the closest 16-bit
11 * Make two tables to take a linear value scaled to 255*65535 and return an
12 * approximation to the 8-bit sRGB encoded value. Calculate the error in these
13 * tables and display it.
21 /* pngpriv.h includes the definition of 'PNG_sRGB_FROM_LINEAR' which is required
22 * to verify the actual code.
24 #include "../../pngpriv.h"
28 /* The tables are declared 'const' in pngpriv.h, so this redefines the tables to
31 #define png_sRGB_table sRGB_table
32 #define png_sRGB_base sRGB_base
33 #define png_sRGB_delta sRGB_delta
35 static png_uint_16 png_sRGB_table[256];
36 static png_uint_16 png_sRGB_base[512];
37 static png_byte png_sRGB_delta[512];
39 static const unsigned int max_input = 255*65535;
44 return sRGB_from_linear(l/max_input);
54 finvsRGB(unsigned int i)
56 return 65535 * linear_from_sRGB(i/255.);
60 invsRGB(unsigned int i)
62 unsigned int x = nearbyint(finvsRGB(i));
66 fprintf(stderr, "invsRGB(%u) overflows to %u\n", i, x);
70 return (png_uint_16)x;
74 main(int argc, char **argv)
76 unsigned int i, i16, ibase;
79 double min_error16 = 0;
80 double max_error16 = 0;
82 double adjust_lo = 0.4, adjust_hi = 0.6, adjust_mid = 0.5;
83 unsigned int ec_lo = 0, ec_hi = 0, ec_mid = 0;
84 unsigned int error_count = 0;
85 unsigned int error_count16 = 0;
89 test_only = strcmp("--test", argv[1]) == 0;
91 /* Initialize the encoding table first. */
94 png_sRGB_table[i] = invsRGB(i);
97 /* Now work out the decoding tables (this is where the error comes in because
98 * there are 512 set points and 512 straight lines between them.)
108 else if (ec_mid == 0)
111 else if (ec_mid < ec_hi)
112 adjust = (adjust_mid + adjust_hi)/2;
114 else if (ec_mid < ec_lo)
115 adjust = (adjust_mid + adjust_lo)/2;
119 fprintf(stderr, "not reached: %u .. %u .. %u\n", ec_lo, ec_mid, ec_hi);
123 /* Calculate the table using the current 'adjust' */
124 for (i=0; i<=511; ++i)
126 double lo = 255 * sRGB(i << 15);
127 double hi = 255 * sRGB((i+1) << 15);
130 calc = nearbyint((lo+adjust) * 256);
133 fprintf(stderr, "table[%d][0]: overflow %08x (%d)\n", i, calc,
137 png_sRGB_base[i] = calc;
139 calc = nearbyint((hi-lo) * 32);
142 fprintf(stderr, "table[%d][1]: overflow %08x (%d)\n", i, calc,
146 png_sRGB_delta[i] = calc;
149 /* Check the 16-bit linear values alone: */
151 for (i16=0; i16 <= 65535; ++i16)
153 unsigned int i = 255*i16;
154 unsigned int iexact = nearbyint(255*sRGB(i));
155 unsigned int icalc = PNG_sRGB_FROM_LINEAR(i);
161 /* Now try changing the adjustment. */
163 ec_lo = error_count16;
166 ec_hi = error_count16;
168 else if (ec_mid == 0)
170 ec_mid = error_count16;
171 printf("/* initial error counts: %u .. %u .. %u */\n", ec_lo, ec_mid,
175 else if (error_count16 < ec_mid)
177 printf("/* adjust (mid ): %f: %u -> %u */\n", adjust, ec_mid,
179 ec_mid = error_count16;
183 else if (adjust < adjust_mid && error_count16 < ec_lo)
185 printf("/* adjust (low ): %f: %u -> %u */\n", adjust, ec_lo,
187 ec_lo = error_count16;
191 else if (adjust > adjust_mid && error_count16 < ec_hi)
193 printf("/* adjust (high): %f: %u -> %u */\n", adjust, ec_hi,
195 ec_hi = error_count16;
202 printf("/* adjust: %f: %u */\n", adjust, ec_mid);
207 /* For each entry in the table try to adjust it to minimize the error count
208 * in that entry. Each entry corresponds to 128 input values.
210 for (ibase=0; ibase<65536; ibase+=128)
212 png_uint_16 base = png_sRGB_base[ibase >> 7], trybase = base, ob=base;
213 png_byte delta = png_sRGB_delta[ibase >> 7], trydelta = delta, od=delta;
214 unsigned int ecbase = 0, eco;
218 png_sRGB_base[ibase >> 7] = trybase;
219 png_sRGB_delta[ibase >> 7] = trydelta;
221 /* Check the 16-bit linear values alone: */
223 for (i16=ibase; i16 < ibase+128; ++i16)
225 unsigned int i = 255*i16;
226 unsigned int iexact = nearbyint(255*sRGB(i));
227 unsigned int icalc = PNG_sRGB_FROM_LINEAR(i);
233 if (error_count16 == 0)
238 eco = ecbase = error_count16;
239 ++trybase; /* First test */
242 else if (error_count16 < ecbase)
249 else if (trybase < base)
254 else if (trydelta > delta)
259 else if (trydelta < delta)
266 fprintf(stderr, "makesRGB: impossible\n");
269 ecbase = error_count16;
276 else if (trybase < base)
281 else if (trydelta > delta)
283 else if (trydelta < delta)
284 break; /* end of tests */
288 png_sRGB_base[ibase >> 7] = base;
289 png_sRGB_delta[ibase >> 7] = delta;
290 if (base != ob || delta != od)
292 printf("/* table[%u]={%u,%u} -> {%u,%u} %u -> %u errors */\n",
293 ibase>>7, ob, od, base, delta, eco, ecbase);
296 printf("/* table[%u]={%u,%u} %u errors */\n", ibase>>7, ob, od,
300 /* Only do the full (slow) test at the end: */
305 for (i=0; i <= max_input; ++i)
307 unsigned int iexact = nearbyint(255*sRGB(i));
308 unsigned int icalc = PNG_sRGB_FROM_LINEAR(i);
312 double err = 255*sRGB(i) - icalc;
314 if (err > (max_error+.001) || err < (min_error-.001))
317 "/* 0x%08x: exact: %3d, got: %3d [tables: %08x, %08x] (%f) */\n",
318 i, iexact, icalc, png_sRGB_base[i>>15],
319 png_sRGB_delta[i>>15], err);
325 else if (err < min_error)
330 /* Re-check the 16-bit cases too, including the warning if there is an error
336 for (i16=0; i16 <= 65535; ++i16)
338 unsigned int i = 255*i16;
339 unsigned int iexact = nearbyint(255*sRGB(i));
340 unsigned int icalc = PNG_sRGB_FROM_LINEAR(i);
344 double err = 255*sRGB(i) - icalc;
347 if (err > max_error16)
349 else if (err < min_error16)
352 if (abs(icalc - iexact) > 1)
354 "/* 0x%04x: exact: %3d, got: %3d [tables: %08x, %08x] (%f) */\n",
355 i16, iexact, icalc, png_sRGB_base[i>>15],
356 png_sRGB_delta[i>>15], err);
360 /* Check the round trip for each 8-bit sRGB value. */
361 for (i16=0; i16 <= 255; ++i16)
363 unsigned int i = 255 * png_sRGB_table[i16];
364 unsigned int iexact = nearbyint(255*sRGB(i));
365 unsigned int icalc = PNG_sRGB_FROM_LINEAR(i);
369 fprintf(stderr, "8-bit rounding error: %d -> %d\n", i16, iexact);
375 double finv = finvsRGB(i16);
377 printf("/* 8-bit roundtrip error: %d -> %f -> %d(%f) */\n",
378 i16, finv, icalc, fsRGB(255*finv));
383 printf("/* error: %g - %g, %u (%g%%) of readings inexact */\n",
384 min_error, max_error, error_count, (100.*error_count)/max_input);
385 printf("/* 16-bit error: %g - %g, %u (%g%%) of readings inexact */\n",
386 min_error16, max_error16, error_count16, (100.*error_count16)/65535);
390 printf("const png_uint_16 png_sRGB_table[256] =\n{\n ");
395 printf("%d,", png_sRGB_table[i++]);
397 while ((i & 0x7) != 0 && i<255);
398 if (i<255) printf("\n ");
400 printf("%d\n};\n\n", png_sRGB_table[i]);
403 printf("const png_uint_16 png_sRGB_base[512] =\n{\n ");
408 printf("%d,", png_sRGB_base[i++]);
410 while ((i & 0x7) != 0 && i<511);
411 if (i<511) printf("\n ");
413 printf("%d\n};\n\n", png_sRGB_base[i]);
415 printf("const png_byte png_sRGB_delta[512] =\n{\n ");
420 printf("%d,", png_sRGB_delta[i++]);
422 while ((i & 0xf) != 0 && i<511);
423 if (i<511) printf("\n ");
425 printf("%d\n};\n\n", png_sRGB_delta[i]);