[libpng15] Remove simplified interface

This commit is contained in:
John Bowler
2011-11-23 15:34:43 -06:00
committed by Glenn Randers-Pehrson
parent 12fb29f951
commit 4491fa237f
35 changed files with 298 additions and 5755 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,289 +0,0 @@
/* timepng.c
*
* Copyright (c) 2011 John Cunningham Bowler
*
* Last changed in libpng 1.5.7 [(PENDING RELEASE)]
*
* This code is released under the libpng license.
* For conditions of distribution and use, see the disclaimer
* and license in png.h
*
* Load an arbitrary number of PNG files (from the command line, or, if there
* are no arguments on the command line, from stdin) then run a time test by
* reading each file by row. The test does nothing with the read result and
* does no transforms. The only output is a time as a floating point number of
* seconds with 9 decimal digits.
*/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "png.h"
static int read_png(FILE *fp)
{
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,0,0,0);
png_infop info_ptr = NULL;
png_bytep row = NULL, display = NULL;
if (png_ptr == NULL)
return 0;
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
if (row != NULL) free(row);
if (display != NULL) free(display);
return 0;
}
png_init_io(png_ptr, fp);
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
png_error(png_ptr, "OOM allocating info structure");
png_read_info(png_ptr, info_ptr);
{
png_size_t rowbytes = png_get_rowbytes(png_ptr, info_ptr);
row = malloc(rowbytes);
display = malloc(rowbytes);
if (row == NULL || display == NULL)
png_error(png_ptr, "OOM allocating row buffers");
{
png_uint_32 height = png_get_image_height(png_ptr, info_ptr);
int passes = png_set_interlace_handling(png_ptr);
int pass;
png_start_read_image(png_ptr);
for (pass = 0; pass < passes; ++pass)
{
png_uint_32 y = height;
/* NOTE: this trashes the row each time; interlace handling won't
* work, but this avoids memory thrashing for speed testing.
*/
while (y-- > 0)
png_read_row(png_ptr, row, display);
}
}
}
/* Make sure to read to the end of the file: */
png_read_end(png_ptr, info_ptr);
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
free(row);
free(display);
return 1;
}
static int mytime(struct timespec *t)
{
/* Do the timing using clock_gettime and the per-process timer. */
if (!clock_gettime(CLOCK_PROCESS_CPUTIME_ID, t))
return 1;
perror("CLOCK_PROCESS_CPUTIME_ID");
fprintf(stderr, "timepng: could not get the time\n");
return 0;
}
static int perform_one_test(FILE *fp, int nfiles)
{
int i;
struct timespec before, after;
/* Clear out all errors: */
rewind(fp);
if (mytime(&before))
{
for (i=0; i<nfiles; ++i)
{
if (read_png(fp))
{
if (ferror(fp))
{
perror("temporary file");
fprintf(stderr, "file %d: error reading PNG data\n", i);
return 0;
}
}
else
{
perror("temporary file");
fprintf(stderr, "file %d: error from libpng\n", i);
return 0;
}
}
}
else
return 0;
if (mytime(&after))
{
/* Work out the time difference and print it - this is the only output,
* so flush it immediately.
*/
unsigned long s = after.tv_sec - before.tv_sec;
long ns = after.tv_nsec - before.tv_nsec;
if (ns < 0)
{
--s;
ns += 1000000000;
if (ns < 0)
{
fprintf(stderr, "timepng: bad clock from kernel\n");
return 0;
}
}
printf("%lu.%.9ld\n", s, ns);
fflush(stdout);
if (ferror(stdout))
{
fprintf(stderr, "timepng: error writing output\n");
return 0;
}
/* Successful return */
return 1;
}
else
return 0;
}
static int add_one_file(FILE *fp, char *name)
{
FILE *ip = fopen(name, "rb");
if (ip != NULL)
{
int ch;
for (;;)
{
ch = getc(ip);
if (ch == EOF) break;
putc(ch, fp);
}
if (ferror(ip))
{
perror(name);
fprintf(stderr, "%s: read error\n", name);
return 0;
}
(void)fclose(ip);
if (ferror(fp))
{
perror("temporary file");
fprintf(stderr, "temporary file write error\n");
return 0;
}
}
else
{
perror(name);
fprintf(stderr, "%s: open failed\n", name);
return 0;
}
return 1;
}
int main(int argc, char **argv)
{
int ok = 0;
FILE *fp = tmpfile();
if (fp != NULL)
{
int err = 0;
int nfiles = 0;
if (argc > 1)
{
int i;
for (i=1; i<argc; ++i)
{
if (add_one_file(fp, argv[i]))
++nfiles;
else
{
err = 1;
break;
}
}
}
else
{
char filename[FILENAME_MAX+1];
while (fgets(filename, FILENAME_MAX+1, stdin))
{
int len = strlen(filename);
if (filename[len-1] == '\n')
{
filename[len-1] = 0;
if (add_one_file(fp, filename))
++nfiles;
else
{
err = 1;
break;
}
}
else
{
fprintf(stderr, "timepng: truncated file name ...%s\n",
filename+len-32);
err = 1;
break;
}
}
if (ferror(stdin))
{
fprintf(stderr, "timepng: stdin: read error\n");
err = 1;
}
}
if (!err)
{
if (nfiles > 0)
ok = perform_one_test(fp, nfiles);
else
fprintf(stderr, "usage: timepng {files} or ls files | timepng\n");
}
(void)fclose(fp);
}
else
fprintf(stderr, "timepng: could not open temporary file\n");
/* Exit code 0 on success. */
return ok == 0;
}

View File

@@ -1,188 +0,0 @@
/*-
* convert.c
*
* Convert 8-bit sRGB or 16-bit linear values to another format.
*
* Last changed in libpng 1.5.7 [(PENDING RELEASE)]
* Copyright (c) 2011 Written by John Cunningham Bowler
*
* This code is released under the libpng license.
* For conditions of distribution and use, see the disclaimer
* and license in png.h
*/
#define _ISOC99_SOURCE 1
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <fenv.h>
#include "sRGB.h"
static void
usage(const char *prog)
{
fprintf(stderr,
"%s: usage: %s [-linear|-sRGB] [-gray|-color] component{1,4}\n",
prog, prog);
exit(1);
}
unsigned long
component(const char *prog, const char *arg, int issRGB)
{
char *ep;
unsigned long c = strtoul(arg, &ep, 0);
if (ep <= arg || *ep || c > 65535 || (issRGB && c > 255))
{
fprintf(stderr, "%s: %s: invalid component value (%lu)\n", prog, arg, c);
usage(prog);
}
return c;
}
int
main(int argc, const char **argv)
{
const char *prog = *argv++;
int to_linear = 0, to_gray = 0, to_color = 0;
int channels = 0;
double c[4];
/* FE_TONEAREST is the IEEE754 round to nearest, preferring even, mode; i.e.
* everything rounds to the nearest value except that '.5' rounds to the
* nearest even value.
*/
fesetround(FE_TONEAREST);
c[3] = c[2] = c[1] = c[0] = 0;
while (--argc > 0 && **argv == '-')
{
const char *arg = 1+*argv++;
if (strcmp(arg, "sRGB") == 0)
to_linear = 0;
else if (strcmp(arg, "linear") == 0)
to_linear = 1;
else if (strcmp(arg, "gray") == 0)
to_gray = 1, to_color = 0;
else if (strcmp(arg, "color") == 0)
to_gray = 0, to_color = 1;
else
usage(prog);
}
switch (argc)
{
default:
usage(prog);
break;
case 4:
c[3] = component(prog, argv[3], to_linear);
++channels;
case 3:
c[2] = component(prog, argv[2], to_linear);
++channels;
case 2:
c[1] = component(prog, argv[1], to_linear);
++channels;
case 1:
c[0] = component(prog, argv[0], to_linear);
++channels;
break;
}
if (to_linear)
{
int i;
int components = channels;
if ((components & 1) == 0)
--components;
for (i=0; i<components; ++i) c[i] = linear_from_sRGB(c[i] / 255);
if (components < channels)
c[components] = c[components] / 255;
}
else
{
int i;
for (i=0; i<4; ++i) c[i] /= 65535;
if ((channels & 1) == 0)
{
double alpha = c[channels-1];
if (alpha > 0)
for (i=0; i<channels-1; ++i) c[i] /= alpha;
else
for (i=0; i<channels-1; ++i) c[i] = 1;
}
}
if (to_gray)
{
if (channels < 3)
{
fprintf(stderr, "%s: too few channels (%d) for -gray\n",
prog, channels);
usage(prog);
}
c[0] = YfromRGB(c[0], c[1], c[2]);
channels -= 2;
}
if (to_color)
{
if (channels > 2)
{
fprintf(stderr, "%s: too many channels (%d) for -color\n",
prog, channels);
usage(prog);
}
c[3] = c[1]; /* alpha, if present */
c[2] = c[1] = c[0];
}
if (to_linear)
{
int i;
if ((channels & 1) == 0)
{
double alpha = c[channels-1];
for (i=0; i<channels-1; ++i) c[i] *= alpha;
}
for (i=0; i<channels; ++i) c[i] = nearbyint(c[i] * 65535);
}
else /* to sRGB */
{
int i = (channels+1)&~1;
while (--i >= 0)
c[i] = sRGB_from_linear(c[i]);
for (i=0; i<channels; ++i) c[i] = nearbyint(c[i] * 255);
}
{
int i;
for (i=0; i<channels; ++i) printf(" %g", c[i]);
}
printf("\n");
return 0;
}

View File

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

View File

@@ -1,48 +0,0 @@
/*-
* sRGB.h
*
* Last changed in libpng 1.5.7 [(PENDING RELEASE)]
* Copyright (c) 2011 John Cunningham Bowler
*
* This code is released under the libpng license.
* For conditions of distribution and use, see the disclaimer
* and license in png.h
*
* Utility file; not actually a header, this contains definitions of sRGB
* calculation functions for inclusion in those test programs that need them.
*
* All routines take and return a floating point value in the range
* 0 to 1.0, doing a calculation according to the sRGB specification
* (in fact the source of the numbers is the wikipedia article at
* http://en.wikipedia.org/wiki/SRGB).
*/
static double
sRGB_from_linear(double l)
{
if (l <= 0.0031308)
l *= 12.92;
else
l = 1.055 * pow(l, 1/2.4) - 0.055;
return l;
}
static double
linear_from_sRGB(double s)
{
if (s <= 0.04045)
return s / 12.92;
else
return pow((s+0.055)/1.055, 2.4);
}
static double
YfromRGB(double r, double g, double b)
{
/* Use the sRGB (rounded) coefficients for Rlinear, Glinear, Blinear to get
* the CIE Y value (also linear).
*/
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
}