fukasawa e60969
/* makesRGB.c -- build sRGB-to-linear and linear-to-sRGB conversion tables
fukasawa e60969
 *
fukasawa e60969
 * Last changed in libpng 1.6.0 [February 14, 2013]
fukasawa e60969
 *
fukasawa e60969
 * COPYRIGHT: Written by John Cunningham Bowler, 2013.
fukasawa e60969
 * To the extent possible under law, the author has waived all copyright and
fukasawa e60969
 * related or neighboring rights to this work.  This work is published from:
fukasawa e60969
 * United States.
fukasawa e60969
 *
fukasawa e60969
 * Make a table to convert 8-bit sRGB encoding values into the closest 16-bit
fukasawa e60969
 * linear value.
fukasawa e60969
 *
fukasawa e60969
 * Make two tables to take a linear value scaled to 255*65535 and return an
fukasawa e60969
 * approximation to the 8-bit sRGB encoded value.  Calculate the error in these
fukasawa e60969
 * tables and display it.
fukasawa e60969
 */
fukasawa e60969
#define _C99_SOURCE 1
fukasawa e60969
#include <stdio.h></stdio.h>
fukasawa e60969
#include <math.h></math.h>
fukasawa e60969
#include <stdlib.h></stdlib.h>
fukasawa e60969
fukasawa e60969
/* pngpriv.h includes the definition of 'PNG_sRGB_FROM_LINEAR' which is required
fukasawa e60969
 * to verify the actual code.
fukasawa e60969
 */
fukasawa e60969
#include "../../pngpriv.h"
fukasawa e60969
fukasawa e60969
#include "sRGB.h"
fukasawa e60969
fukasawa e60969
/* The tables are declared 'const' in pngpriv.h, so this redefines the tables to
fukasawa e60969
 * be used.
fukasawa e60969
 */
fukasawa e60969
#define png_sRGB_table sRGB_table
fukasawa e60969
#define png_sRGB_base sRGB_base
fukasawa e60969
#define png_sRGB_delta sRGB_delta
fukasawa e60969
fukasawa e60969
static png_uint_16 png_sRGB_table[256];
fukasawa e60969
static png_uint_16 png_sRGB_base[512];
fukasawa e60969
static png_byte png_sRGB_delta[512];
fukasawa e60969
fukasawa e60969
static const unsigned int max_input = 255*65535;
fukasawa e60969
fukasawa e60969
double
fukasawa e60969
fsRGB(double l)
fukasawa e60969
{
fukasawa e60969
   return sRGB_from_linear(l/max_input);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
double
fukasawa e60969
sRGB(unsigned int i)
fukasawa e60969
{
fukasawa e60969
   return fsRGB(i);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
double
fukasawa e60969
finvsRGB(unsigned int i)
fukasawa e60969
{
fukasawa e60969
   return 65535 * linear_from_sRGB(i/255.);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
png_uint_16
fukasawa e60969
invsRGB(unsigned int i)
fukasawa e60969
{
fukasawa e60969
   unsigned int x = nearbyint(finvsRGB(i));
fukasawa e60969
fukasawa e60969
   if (x > 65535)
fukasawa e60969
   {
fukasawa e60969
      fprintf(stderr, "invsRGB(%u) overflows to %u\n", i, x);
fukasawa e60969
      exit(1);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return (png_uint_16)x;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
int
fukasawa e60969
main(int argc, char **argv)
fukasawa e60969
{
fukasawa e60969
   unsigned int i, i16, ibase;
fukasawa e60969
   double min_error = 0;
fukasawa e60969
   double max_error = 0;
fukasawa e60969
   double min_error16 = 0;
fukasawa e60969
   double max_error16 = 0;
fukasawa e60969
   double adjust;
fukasawa e60969
   double adjust_lo = 0.4, adjust_hi = 0.6, adjust_mid = 0.5;
fukasawa e60969
   unsigned int ec_lo = 0, ec_hi = 0, ec_mid = 0;
fukasawa e60969
   unsigned int error_count = 0;
fukasawa e60969
   unsigned int error_count16 = 0;
fukasawa e60969
   int test_only = 0;
fukasawa e60969
fukasawa e60969
   if (argc > 1)
fukasawa e60969
      test_only = strcmp("--test", argv[1]) == 0;
fukasawa e60969
fukasawa e60969
   /* Initialize the encoding table first. */
fukasawa e60969
   for (i=0; i<256; ++i)
fukasawa e60969
   {
fukasawa e60969
      png_sRGB_table[i] = invsRGB(i);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Now work out the decoding tables (this is where the error comes in because
fukasawa e60969
    * there are 512 set points and 512 straight lines between them.)
fukasawa e60969
    */
fukasawa e60969
   for (;;)
fukasawa e60969
   {
fukasawa e60969
      if (ec_lo == 0)
fukasawa e60969
         adjust = adjust_lo;
fukasawa e60969
fukasawa e60969
      else if (ec_hi == 0)
fukasawa e60969
         adjust = adjust_hi;
fukasawa e60969
fukasawa e60969
      else if (ec_mid == 0)
fukasawa e60969
         adjust = adjust_mid;
fukasawa e60969
fukasawa e60969
      else if (ec_mid < ec_hi)
fukasawa e60969
         adjust = (adjust_mid + adjust_hi)/2;
fukasawa e60969
fukasawa e60969
      else if (ec_mid < ec_lo)
fukasawa e60969
         adjust = (adjust_mid + adjust_lo)/2;
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr, "not reached: %u .. %u .. %u\n", ec_lo, ec_mid, ec_hi);
fukasawa e60969
         exit(1);
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* Calculate the table using the current 'adjust' */
fukasawa e60969
      for (i=0; i<=511; ++i)
fukasawa e60969
      {
fukasawa e60969
         double lo = 255 * sRGB(i << 15);
fukasawa e60969
         double hi = 255 * sRGB((i+1) << 15);
fukasawa e60969
         unsigned int calc;
fukasawa e60969
fukasawa e60969
         calc = nearbyint((lo+adjust) * 256);
fukasawa e60969
         if (calc > 65535)
fukasawa e60969
         {
fukasawa e60969
            fprintf(stderr, "table[%d][0]: overflow %08x (%d)\n", i, calc,
fukasawa e60969
               calc);
fukasawa e60969
            exit(1);
fukasawa e60969
         }
fukasawa e60969
         png_sRGB_base[i] = calc;
fukasawa e60969
fukasawa e60969
         calc = nearbyint((hi-lo) * 32);
fukasawa e60969
         if (calc > 255)
fukasawa e60969
         {
fukasawa e60969
            fprintf(stderr, "table[%d][1]: overflow %08x (%d)\n", i, calc,
fukasawa e60969
               calc);
fukasawa e60969
            exit(1);
fukasawa e60969
         }
fukasawa e60969
         png_sRGB_delta[i] = calc;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* Check the 16-bit linear values alone: */
fukasawa e60969
      error_count16 = 0;
fukasawa e60969
      for (i16=0; i16 <= 65535; ++i16)
fukasawa e60969
      {
fukasawa e60969
         unsigned int i = 255*i16;
fukasawa e60969
         unsigned int iexact = nearbyint(255*sRGB(i));
fukasawa e60969
         unsigned int icalc = PNG_sRGB_FROM_LINEAR(i);
fukasawa e60969
fukasawa e60969
         if (icalc != iexact)
fukasawa e60969
            ++error_count16;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* Now try changing the adjustment. */
fukasawa e60969
      if (ec_lo == 0)
fukasawa e60969
         ec_lo = error_count16;
fukasawa e60969
fukasawa e60969
      else if (ec_hi == 0)
fukasawa e60969
         ec_hi = error_count16;
fukasawa e60969
fukasawa e60969
      else if (ec_mid == 0)
fukasawa e60969
      {
fukasawa e60969
         ec_mid = error_count16;
fukasawa e60969
         printf("/* initial error counts: %u .. %u .. %u */\n", ec_lo, ec_mid,
fukasawa e60969
            ec_hi);
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else if (error_count16 < ec_mid)
fukasawa e60969
      {
fukasawa e60969
         printf("/* adjust (mid ): %f: %u -> %u */\n", adjust, ec_mid,
fukasawa e60969
            error_count16);
fukasawa e60969
         ec_mid = error_count16;
fukasawa e60969
         adjust_mid = adjust;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else if (adjust < adjust_mid && error_count16 < ec_lo)
fukasawa e60969
      {
fukasawa e60969
         printf("/* adjust (low ): %f: %u -> %u */\n", adjust, ec_lo,
fukasawa e60969
            error_count16);
fukasawa e60969
         ec_lo = error_count16;
fukasawa e60969
         adjust_lo = adjust;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else if (adjust > adjust_mid && error_count16 < ec_hi)
fukasawa e60969
      {
fukasawa e60969
         printf("/* adjust (high): %f: %u -> %u */\n", adjust, ec_hi,
fukasawa e60969
            error_count16);
fukasawa e60969
         ec_hi = error_count16;
fukasawa e60969
         adjust_hi = adjust;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
      {
fukasawa e60969
         adjust = adjust_mid;
fukasawa e60969
         printf("/* adjust: %f: %u */\n", adjust, ec_mid);
fukasawa e60969
         break;
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* For each entry in the table try to adjust it to minimize the error count
fukasawa e60969
    * in that entry.  Each entry corresponds to 128 input values.
fukasawa e60969
    */
fukasawa e60969
   for (ibase=0; ibase<65536; ibase+=128)
fukasawa e60969
   {
fukasawa e60969
      png_uint_16 base = png_sRGB_base[ibase >> 7], trybase = base, ob=base;
fukasawa e60969
      png_byte delta = png_sRGB_delta[ibase >> 7], trydelta = delta, od=delta;
fukasawa e60969
      unsigned int ecbase = 0, eco;
fukasawa e60969
fukasawa e60969
      for (;;)
fukasawa e60969
      {
fukasawa e60969
         png_sRGB_base[ibase >> 7] = trybase;
fukasawa e60969
         png_sRGB_delta[ibase >> 7] = trydelta;
fukasawa e60969
fukasawa e60969
         /* Check the 16-bit linear values alone: */
fukasawa e60969
         error_count16 = 0;
fukasawa e60969
         for (i16=ibase; i16 < ibase+128; ++i16)
fukasawa e60969
         {
fukasawa e60969
            unsigned int i = 255*i16;
fukasawa e60969
            unsigned int iexact = nearbyint(255*sRGB(i));
fukasawa e60969
            unsigned int icalc = PNG_sRGB_FROM_LINEAR(i);
fukasawa e60969
fukasawa e60969
            if (icalc != iexact)
fukasawa e60969
               ++error_count16;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         if (error_count16 == 0)
fukasawa e60969
            break;
fukasawa e60969
fukasawa e60969
         if (ecbase == 0)
fukasawa e60969
         {
fukasawa e60969
            eco = ecbase = error_count16;
fukasawa e60969
            ++trybase; /* First test */
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         else if (error_count16 < ecbase)
fukasawa e60969
         {
fukasawa e60969
            if (trybase > base)
fukasawa e60969
            {
fukasawa e60969
               base = trybase;
fukasawa e60969
               ++trybase;
fukasawa e60969
            }
fukasawa e60969
            else if (trybase < base)
fukasawa e60969
            {
fukasawa e60969
               base = trybase;
fukasawa e60969
               --trybase;
fukasawa e60969
            }
fukasawa e60969
            else if (trydelta > delta)
fukasawa e60969
            {
fukasawa e60969
               delta = trydelta;
fukasawa e60969
               ++trydelta;
fukasawa e60969
            }
fukasawa e60969
            else if (trydelta < delta)
fukasawa e60969
            {
fukasawa e60969
               delta = trydelta;
fukasawa e60969
               --trydelta;
fukasawa e60969
            }
fukasawa e60969
            else
fukasawa e60969
            {
fukasawa e60969
               fprintf(stderr, "makesRGB: impossible\n");
fukasawa e60969
               exit(1);
fukasawa e60969
            }
fukasawa e60969
            ecbase = error_count16;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
         {
fukasawa e60969
            if (trybase > base)
fukasawa e60969
               trybase = base-1;
fukasawa e60969
            else if (trybase < base)
fukasawa e60969
            {
fukasawa e60969
               trybase = base;
fukasawa e60969
               ++trydelta;
fukasawa e60969
            }
fukasawa e60969
            else if (trydelta > delta)
fukasawa e60969
               trydelta = delta-1;
fukasawa e60969
            else if (trydelta < delta)
fukasawa e60969
               break; /* end of tests */
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      png_sRGB_base[ibase >> 7] = base;
fukasawa e60969
      png_sRGB_delta[ibase >> 7] = delta;
fukasawa e60969
      if (base != ob || delta != od)
fukasawa e60969
      {
fukasawa e60969
         printf("/* table[%u]={%u,%u} -> {%u,%u} %u -> %u errors */\n",
fukasawa e60969
            ibase>>7, ob, od, base, delta, eco, ecbase);
fukasawa e60969
      }
fukasawa e60969
      else if (0)
fukasawa e60969
         printf("/* table[%u]={%u,%u} %u errors */\n", ibase>>7, ob, od,
fukasawa e60969
            ecbase);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Only do the full (slow) test at the end: */
fukasawa e60969
   min_error = -.4999;
fukasawa e60969
   max_error = .4999;
fukasawa e60969
   error_count = 0;
fukasawa e60969
fukasawa e60969
   for (i=0; i <= max_input; ++i)
fukasawa e60969
   {
fukasawa e60969
      unsigned int iexact = nearbyint(255*sRGB(i));
fukasawa e60969
      unsigned int icalc = PNG_sRGB_FROM_LINEAR(i);
fukasawa e60969
fukasawa e60969
      if (icalc != iexact)
fukasawa e60969
      {
fukasawa e60969
         double err = 255*sRGB(i) - icalc;
fukasawa e60969
fukasawa e60969
         if (err > (max_error+.001) || err < (min_error-.001))
fukasawa e60969
         {
fukasawa e60969
            printf(
fukasawa e60969
               "/* 0x%08x: exact: %3d, got: %3d [tables: %08x, %08x] (%f) */\n",
fukasawa e60969
               i, iexact, icalc, png_sRGB_base[i>>15],
fukasawa e60969
               png_sRGB_delta[i>>15], err);
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         ++error_count;
fukasawa e60969
         if (err > max_error)
fukasawa e60969
            max_error = err;
fukasawa e60969
         else if (err < min_error)
fukasawa e60969
            min_error = err;
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Re-check the 16-bit cases too, including the warning if there is an error
fukasawa e60969
    * bigger than 1.
fukasawa e60969
    */
fukasawa e60969
   error_count16 = 0;
fukasawa e60969
   max_error16 = 0;
fukasawa e60969
   min_error16 = 0;
fukasawa e60969
   for (i16=0; i16 <= 65535; ++i16)
fukasawa e60969
   {
fukasawa e60969
      unsigned int i = 255*i16;
fukasawa e60969
      unsigned int iexact = nearbyint(255*sRGB(i));
fukasawa e60969
      unsigned int icalc = PNG_sRGB_FROM_LINEAR(i);
fukasawa e60969
fukasawa e60969
      if (icalc != iexact)
fukasawa e60969
      {
fukasawa e60969
         double err = 255*sRGB(i) - icalc;
fukasawa e60969
fukasawa e60969
         ++error_count16;
fukasawa e60969
         if (err > max_error16)
fukasawa e60969
            max_error16 = err;
fukasawa e60969
         else if (err < min_error16)
fukasawa e60969
            min_error16 = err;
fukasawa e60969
fukasawa e60969
         if (abs(icalc - iexact) > 1)
fukasawa e60969
            printf(
fukasawa e60969
               "/* 0x%04x: exact: %3d, got: %3d [tables: %08x, %08x] (%f) */\n",
fukasawa e60969
               i16, iexact, icalc, png_sRGB_base[i>>15],
fukasawa e60969
               png_sRGB_delta[i>>15], err);
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Check the round trip for each 8-bit sRGB value. */
fukasawa e60969
   for (i16=0; i16 <= 255; ++i16)
fukasawa e60969
   {
fukasawa e60969
      unsigned int i = 255 * png_sRGB_table[i16];
fukasawa e60969
      unsigned int iexact = nearbyint(255*sRGB(i));
fukasawa e60969
      unsigned int icalc = PNG_sRGB_FROM_LINEAR(i);
fukasawa e60969
fukasawa e60969
      if (i16 != iexact)
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr, "8-bit rounding error: %d -> %d\n", i16, iexact);
fukasawa e60969
         exit(1);
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (icalc != i16)
fukasawa e60969
      {
fukasawa e60969
         double finv = finvsRGB(i16);
fukasawa e60969
fukasawa e60969
         printf("/* 8-bit roundtrip error: %d -> %f -> %d(%f) */\n",
fukasawa e60969
            i16, finv, icalc, fsRGB(255*finv));
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
fukasawa e60969
   printf("/* error: %g - %g, %u (%g%%) of readings inexact */\n",
fukasawa e60969
      min_error, max_error, error_count, (100.*error_count)/max_input);
fukasawa e60969
   printf("/* 16-bit error: %g - %g, %u (%g%%) of readings inexact */\n",
fukasawa e60969
      min_error16, max_error16, error_count16, (100.*error_count16)/65535);
fukasawa e60969
fukasawa e60969
   if (!test_only)
fukasawa e60969
   {
fukasawa e60969
      printf("PNG_CONST png_uint_16 png_sRGB_table[256] =\n{\n   ");
fukasawa e60969
      for (i=0; i<255; )
fukasawa e60969
      {
fukasawa e60969
         do
fukasawa e60969
         {
fukasawa e60969
            printf("%d,", png_sRGB_table[i++]);
fukasawa e60969
         }
fukasawa e60969
         while ((i & 0x7) != 0 && i<255);
fukasawa e60969
         if (i<255) printf("\n   ");
fukasawa e60969
      }
fukasawa e60969
      printf("%d\n};\n\n", png_sRGB_table[i]);
fukasawa e60969
fukasawa e60969
fukasawa e60969
      printf("PNG_CONST png_uint_16 png_sRGB_base[512] =\n{\n   ");
fukasawa e60969
      for (i=0; i<511; )
fukasawa e60969
      {
fukasawa e60969
         do
fukasawa e60969
         {
fukasawa e60969
            printf("%d,", png_sRGB_base[i++]);
fukasawa e60969
         }
fukasawa e60969
         while ((i & 0x7) != 0 && i<511);
fukasawa e60969
         if (i<511) printf("\n   ");
fukasawa e60969
      }
fukasawa e60969
      printf("%d\n};\n\n", png_sRGB_base[i]);
fukasawa e60969
fukasawa e60969
      printf("PNG_CONST png_byte png_sRGB_delta[512] =\n{\n   ");
fukasawa e60969
      for (i=0; i<511; )
fukasawa e60969
      {
fukasawa e60969
         do
fukasawa e60969
         {
fukasawa e60969
            printf("%d,", png_sRGB_delta[i++]);
fukasawa e60969
         }
fukasawa e60969
         while ((i & 0xf) != 0 && i<511);
fukasawa e60969
         if (i<511) printf("\n   ");
fukasawa e60969
      }
fukasawa e60969
      printf("%d\n};\n\n", png_sRGB_delta[i]);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return 0;
fukasawa e60969
}