[libpng16] Bump version to 1.6.1beta05

This commit is contained in:
Glenn Randers-Pehrson
2013-03-02 14:58:22 -06:00
parent f3af706c2a
commit 871b1d0fab
186 changed files with 20681 additions and 8559 deletions

View File

@@ -0,0 +1,57 @@
/* Fake a PNG - just write it out directly. */
#include <stdio.h>
#include <zlib.h> /* for crc32 */
void
put_uLong(uLong val)
{
putchar(val >> 24);
putchar(val >> 16);
putchar(val >> 8);
putchar(val >> 0);
}
void
put_chunk(const unsigned char *chunk, uInt length)
{
uLong crc;
put_uLong(length-4); /* Exclude the tag */
fwrite(chunk, length, 1, stdout);
crc = crc32(0, Z_NULL, 0);
put_uLong(crc32(crc, chunk, length));
}
const unsigned char signature[] =
{
137, 80, 78, 71, 13, 10, 26, 10
};
const unsigned char IHDR[] =
{
73, 72, 68, 82, /* IHDR */
0, 0, 0, 1, /* width */
0, 0, 0, 1, /* height */
1, /* bit depth */
0, /* color type: greyscale */
0, /* compression method */
0, /* filter method */
0 /* interlace method: none */
};
const unsigned char unknown[] =
{
'u', 'n', 'K', 'n' /* "unKn" - private safe to copy */
};
int
main(void)
{
fwrite(signature, sizeof signature, 1, stdout);
put_chunk(IHDR, sizeof IHDR);
for(;;)
put_chunk(unknown, sizeof unknown);
}

102
contrib/libtests/gentests.sh Executable file
View File

@@ -0,0 +1,102 @@
#!/bin/sh
#
# Copyright (c) 2013 John Cunningham Bowler
#
# Last changed in libpng 1.6.0 [February 14, 2013]
#
# This code is released under the libpng license.
# For conditions of distribution and use, see the disclaimer
# and license in png.h
#
# Generate a set of PNG test images. The images are generated in a
# sub-directory called 'tests' by default, however a command line argument will
# change that name. The generation requires a built version of makepng in the
# current directory.
#
usage(){
exec >&2
echo "$0 [<directory>]"
echo ' Generate a set of PNG test files in "directory" ("tests" by default)'
exit 1
}
mp="$PWD/makepng"
test -x "$mp" || {
exec >&2
echo "$0: the 'makepng' program must exist"
echo " in the directory within which this program:"
echo " $mp"
echo " is executed"
usage
}
# Just one argument: the directory
testdir="tests"
test $# -gt 1 && {
testdir="$1"
shift
}
test $# -eq 0 || usage
# Take care not to clobber something
if test -e "$testdir"
then
test -d "$testdir" || usage
else
# mkdir -p isn't portable, so do the following
mkdir "$testdir" 2>/dev/null || mkdir -p "$testdir" || usage
fi
# This fails in a very satisfactory way if it's not accessible
cd "$testdir"
:>"test$$.png" || {
exec >&2
echo "$testdir: directory not writable"
usage
}
rm "test$$.png" || {
exec >&2
echo "$testdir: you have create but not write privileges here."
echo " This is unexpected. You have a spurion; "'"'"test$$.png"'"'"."
echo " You need to remove this yourself. Try a different directory."
exit 1
}
# Now call makepng ($mp) to create every file we can think of with a
# reasonable name
doit(){
for gamma in "" --sRGB --linear --1.8
do
case "$gamma" in
"")
gname=;;
--sRGB)
gname="-srgb";;
--linear)
gname="-lin";;
--1.8)
gname="-18";;
*)
gname="-$gamma";;
esac
"$mp" $gamma "$1" "$2" "test-$1-$2$gname.png"
done
}
#
for ct in gray palette
do
for bd in 1 2 4 8
do
doit "$ct" "$bd"
done
done
#
doit "gray" "16"
#
for ct in gray-alpha rgb rgb-alpha
do
for bd in 8 16
do
doit "$ct" "$bd"
done
done

1486
contrib/libtests/makepng.c Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,960 @@
/* pngunknown.c - test the read side unknown chunk handling
*
* Last changed in libpng 1.6.0 [(PENDING RELEASE)]
* Copyright (c) 2013 Glenn Randers-Pehrson
* 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
*
* NOTES:
* This is a C program that is intended to be linked against libpng. It
* allows the libpng unknown handling code to be tested by interpreting
* arguments to save or discard combinations of chunks. The program is
* currently just a minimal validation for the built-in libpng facilities.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <setjmp.h>
/* Define the following to use this test against your installed libpng, rather
* than the one being built here:
*/
#ifdef PNG_FREESTANDING_TESTS
# include <png.h>
#else
# include "../../png.h"
#endif
#ifdef PNG_READ_SUPPORTED
#if PNG_LIBPNG_VER < 10500
/* This deliberately lacks the PNG_CONST. */
typedef png_byte *png_const_bytep;
/* This is copied from 1.5.1 png.h: */
#define PNG_INTERLACE_ADAM7_PASSES 7
#define PNG_PASS_START_ROW(pass) (((1U&~(pass))<<(3-((pass)>>1)))&7)
#define PNG_PASS_START_COL(pass) (((1U& (pass))<<(3-(((pass)+1)>>1)))&7)
#define PNG_PASS_ROW_SHIFT(pass) ((pass)>2?(8-(pass))>>1:3)
#define PNG_PASS_COL_SHIFT(pass) ((pass)>1?(7-(pass))>>1:3)
#define PNG_PASS_ROWS(height, pass) (((height)+(((1<<PNG_PASS_ROW_SHIFT(pass))\
-1)-PNG_PASS_START_ROW(pass)))>>PNG_PASS_ROW_SHIFT(pass))
#define PNG_PASS_COLS(width, pass) (((width)+(((1<<PNG_PASS_COL_SHIFT(pass))\
-1)-PNG_PASS_START_COL(pass)))>>PNG_PASS_COL_SHIFT(pass))
#define PNG_ROW_FROM_PASS_ROW(yIn, pass) \
(((yIn)<<PNG_PASS_ROW_SHIFT(pass))+PNG_PASS_START_ROW(pass))
#define PNG_COL_FROM_PASS_COL(xIn, pass) \
(((xIn)<<PNG_PASS_COL_SHIFT(pass))+PNG_PASS_START_COL(pass))
#define PNG_PASS_MASK(pass,off) ( \
((0x110145AFU>>(((7-(off))-(pass))<<2)) & 0xFU) | \
((0x01145AF0U>>(((7-(off))-(pass))<<2)) & 0xF0U))
#define PNG_ROW_IN_INTERLACE_PASS(y, pass) \
((PNG_PASS_MASK(pass,0) >> ((y)&7)) & 1)
#define PNG_COL_IN_INTERLACE_PASS(x, pass) \
((PNG_PASS_MASK(pass,1) >> ((x)&7)) & 1)
/* These are needed too for the default build: */
#define PNG_WRITE_16BIT_SUPPORTED
#define PNG_READ_16BIT_SUPPORTED
/* This comes from pnglibconf.h afer 1.5: */
#define PNG_FP_1 100000
#define PNG_GAMMA_THRESHOLD_FIXED\
((png_fixed_point)(PNG_GAMMA_THRESHOLD * PNG_FP_1))
#endif
#if PNG_LIBPNG_VER < 10600
/* 1.6.0 constifies many APIs. The following exists to allow pngvalid to be
* compiled against earlier versions.
*/
# define png_const_structp png_structp
#endif
/* Copied from pngpriv.h */
#define PNG_32b(b,s) ((png_uint_32)(b) << (s))
#define PNG_CHUNK(b1,b2,b3,b4) \
(PNG_32b(b1,24) | PNG_32b(b2,16) | PNG_32b(b3,8) | PNG_32b(b4,0))
#define png_IHDR PNG_CHUNK( 73, 72, 68, 82)
#define png_IDAT PNG_CHUNK( 73, 68, 65, 84)
#define png_IEND PNG_CHUNK( 73, 69, 78, 68)
#define png_PLTE PNG_CHUNK( 80, 76, 84, 69)
#define png_bKGD PNG_CHUNK( 98, 75, 71, 68)
#define png_cHRM PNG_CHUNK( 99, 72, 82, 77)
#define png_gAMA PNG_CHUNK(103, 65, 77, 65)
#define png_hIST PNG_CHUNK(104, 73, 83, 84)
#define png_iCCP PNG_CHUNK(105, 67, 67, 80)
#define png_iTXt PNG_CHUNK(105, 84, 88, 116)
#define png_oFFs PNG_CHUNK(111, 70, 70, 115)
#define png_pCAL PNG_CHUNK(112, 67, 65, 76)
#define png_sCAL PNG_CHUNK(115, 67, 65, 76)
#define png_pHYs PNG_CHUNK(112, 72, 89, 115)
#define png_sBIT PNG_CHUNK(115, 66, 73, 84)
#define png_sPLT PNG_CHUNK(115, 80, 76, 84)
#define png_sRGB PNG_CHUNK(115, 82, 71, 66)
#define png_sTER PNG_CHUNK(115, 84, 69, 82)
#define png_tEXt PNG_CHUNK(116, 69, 88, 116)
#define png_tIME PNG_CHUNK(116, 73, 77, 69)
#define png_tRNS PNG_CHUNK(116, 82, 78, 83)
#define png_zTXt PNG_CHUNK(122, 84, 88, 116)
#define png_vpAg PNG_CHUNK('v', 'p', 'A', 'g')
/* Test on flag values as defined in the spec (section 5.4): */
#define PNG_CHUNK_ANCILLARY(c ) (1 & ((c) >> 29))
#define PNG_CHUNK_CRITICAL(c) (!PNG_CHUNK_ANCILLARY(c))
#define PNG_CHUNK_PRIVATE(c) (1 & ((c) >> 21))
#define PNG_CHUNK_RESERVED(c) (1 & ((c) >> 13))
#define PNG_CHUNK_SAFE_TO_COPY(c) (1 & ((c) >> 5))
/* Chunk information */
#define PNG_INFO_tEXt 0x10000000U
#define PNG_INFO_iTXt 0x20000000U
#define PNG_INFO_zTXt 0x40000000U
#define PNG_INFO_sTER 0x01000000U
#define PNG_INFO_vpAg 0x02000000U
#define ABSENT 0
#define START 1
#define END 2
static struct
{
char name[5];
png_uint_32 flag;
png_uint_32 tag;
int unknown; /* Chunk not known to libpng */
int all; /* Chunk set by the '-1' option */
int position; /* position in pngtest.png */
int keep; /* unknown handling setting */
} chunk_info[] = {
/* Critical chunks */
{ "IDAT", PNG_INFO_IDAT, png_IDAT, 0, 0, START, 0 }, /* must be [0] */
{ "PLTE", PNG_INFO_PLTE, png_PLTE, 0, 0, ABSENT, 0 },
/* Non-critical chunks that libpng handles */
{ "bKGD", PNG_INFO_bKGD, png_bKGD, 0, 1, START, 0 },
{ "cHRM", PNG_INFO_cHRM, png_cHRM, 0, 1, START, 0 },
{ "gAMA", PNG_INFO_gAMA, png_gAMA, 0, 1, START, 0 },
{ "hIST", PNG_INFO_hIST, png_hIST, 0, 1, ABSENT, 0 },
{ "iCCP", PNG_INFO_iCCP, png_iCCP, 0, 1, ABSENT, 0 },
{ "iTXt", PNG_INFO_iTXt, png_iTXt, 0, 1, ABSENT, 0 },
{ "oFFs", PNG_INFO_oFFs, png_oFFs, 0, 1, START, 0 },
{ "pCAL", PNG_INFO_pCAL, png_pCAL, 0, 1, START, 0 },
{ "pHYs", PNG_INFO_pHYs, png_pHYs, 0, 1, START, 0 },
{ "sBIT", PNG_INFO_sBIT, png_sBIT, 0, 1, START, 0 },
{ "sCAL", PNG_INFO_sCAL, png_sCAL, 0, 1, START, 0 },
{ "sPLT", PNG_INFO_sPLT, png_sPLT, 0, 1, ABSENT, 0 },
{ "sRGB", PNG_INFO_sRGB, png_sRGB, 0, 1, START, 0 },
{ "tEXt", PNG_INFO_tEXt, png_tEXt, 0, 1, START, 0 },
{ "tIME", PNG_INFO_tIME, png_tIME, 0, 1, START, 0 },
{ "tRNS", PNG_INFO_tRNS, png_tRNS, 0, 0, ABSENT, 0 },
{ "zTXt", PNG_INFO_zTXt, png_zTXt, 0, 1, END, 0 },
/* No libpng handling */
{ "sTER", PNG_INFO_sTER, png_sTER, 1, 1, START, 0 },
{ "vpAg", PNG_INFO_vpAg, png_vpAg, 1, 0, START, 0 },
};
#define NINFO ((int)((sizeof chunk_info)/(sizeof chunk_info[0])))
static void
clear_keep(void)
{
int i = NINFO;
while (--i >= 0)
chunk_info[i].keep = 0;
}
static int
find(const char *name)
{
int i = NINFO;
while (--i >= 0)
{
if (memcmp(chunk_info[i].name, name, 4) == 0)
break;
}
return i;
}
static int
findb(const png_byte *name)
{
int i = NINFO;
while (--i >= 0)
{
if (memcmp(chunk_info[i].name, name, 4) == 0)
break;
}
return i;
}
static int
find_by_flag(png_uint_32 flag)
{
int i = NINFO;
while (--i >= 0) if (chunk_info[i].flag == flag) return i;
fprintf(stderr, "pngunknown: internal error\n");
exit(4);
}
static int
ancillary(const char *name)
{
return PNG_CHUNK_ANCILLARY(PNG_CHUNK(name[0], name[1], name[2], name[3]));
}
static int
ancillaryb(const png_byte *name)
{
return PNG_CHUNK_ANCILLARY(PNG_CHUNK(name[0], name[1], name[2], name[3]));
}
/* Type of an error_ptr */
typedef struct
{
jmp_buf error_return;
png_structp png_ptr;
png_infop info_ptr, end_ptr;
int error_count;
int warning_count;
const char *program;
const char *file;
const char *test;
} display;
static const char init[] = "initialization";
static const char cmd[] = "command line";
static void
init_display(display *d, const char *program)
{
memset(d, 0, sizeof *d);
d->png_ptr = NULL;
d->info_ptr = d->end_ptr = NULL;
d->error_count = d->warning_count = 0;
d->program = program;
d->file = program;
d->test = init;
}
static void
clean_display(display *d)
{
png_destroy_read_struct(&d->png_ptr, &d->info_ptr, &d->end_ptr);
/* This must not happen - it might cause an app crash */
if (d->png_ptr != NULL || d->info_ptr != NULL || d->end_ptr != NULL)
{
fprintf(stderr, "%s(%s): png_destroy_read_struct error\n", d->file,
d->test);
exit(1);
}
/* Invalidate the test */
d->test = init;
}
PNG_FUNCTION(void, display_exit, (display *d), static PNG_NORETURN)
{
++(d->error_count);
if (d->png_ptr != NULL)
clean_display(d);
/* During initialization and if this is a single command line argument set
* exit now - there is only one test, otherwise longjmp to do the next test.
*/
if (d->test == init || d->test == cmd)
exit(1);
longjmp(d->error_return, 1);
}
static int
display_rc(const display *d, int strict)
{
return d->error_count + (strict ? d->warning_count : 0);
}
/* libpng error and warning callbacks */
PNG_FUNCTION(void, error, (png_structp png_ptr, const char *message),
static PNG_NORETURN)
{
display *d = (display*)png_get_error_ptr(png_ptr);
fprintf(stderr, "%s(%s): libpng error: %s\n", d->file, d->test, message);
display_exit(d);
}
static void
warning(png_structp png_ptr, const char *message)
{
display *d = (display*)png_get_error_ptr(png_ptr);
fprintf(stderr, "%s(%s): libpng warning: %s\n", d->file, d->test, message);
++(d->warning_count);
}
static png_uint_32
get_valid(display *d, png_infop info_ptr)
{
png_uint_32 flags = png_get_valid(d->png_ptr, info_ptr, (png_uint_32)~0);
/* Map the text chunks back into the flags */
{
png_textp text;
png_uint_32 ntext = png_get_text(d->png_ptr, info_ptr, &text, NULL);
while (ntext-- > 0) switch (text[ntext].compression)
{
case -1:
flags |= PNG_INFO_tEXt;
break;
case 0:
flags |= PNG_INFO_zTXt;
break;
case 1:
case 2:
flags |= PNG_INFO_iTXt;
break;
default:
fprintf(stderr, "%s(%s): unknown text compression %d\n", d->file,
d->test, text[ntext].compression);
display_exit(d);
}
}
return flags;
}
static png_uint_32
get_unknown(display *d, int def, png_infop info_ptr)
{
/* Create corresponding 'unknown' flags */
png_uint_32 flags = 0;
{
png_unknown_chunkp unknown;
int num_unknown = png_get_unknown_chunks(d->png_ptr, info_ptr, &unknown);
while (--num_unknown >= 0)
{
int chunk = findb(unknown[num_unknown].name);
/* Chunks not known to pngunknown must be validated here; since they
* must also be unknown to libpng the 'def' behavior should have been
* used.
*/
if (chunk < 0) switch (def)
{
default: /* impossible */
case PNG_HANDLE_CHUNK_AS_DEFAULT:
case PNG_HANDLE_CHUNK_NEVER:
fprintf(stderr, "%s(%s): %s: %s: unknown chunk saved\n",
d->file, d->test, def ? "discard" : "default",
unknown[num_unknown].name);
++(d->error_count);
break;
case PNG_HANDLE_CHUNK_IF_SAFE:
if (!ancillaryb(unknown[num_unknown].name))
{
fprintf(stderr,
"%s(%s): if-safe: %s: unknown critical chunk saved\n",
d->file, d->test, unknown[num_unknown].name);
++(d->error_count);
break;
}
/* FALL THROUGH (safe) */
case PNG_HANDLE_CHUNK_ALWAYS:
break;
}
else
flags |= chunk_info[chunk].flag;
}
}
return flags;
}
static int
check(FILE *fp, int argc, const char **argv, png_uint_32p flags/*out*/,
display *d)
{
int i, def = PNG_HANDLE_CHUNK_AS_DEFAULT, npasses, ipass;
png_uint_32 height;
/* Some of these errors are permanently fatal and cause an exit here, others
* are per-test and cause an error return.
*/
d->png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, d, error,
warning);
if (d->png_ptr == NULL)
{
fprintf(stderr, "%s(%s): could not allocate png struct\n", d->file,
d->test);
/* Terminate here, this error is not test specific. */
exit(1);
}
d->info_ptr = png_create_info_struct(d->png_ptr);
d->end_ptr = png_create_info_struct(d->png_ptr);
if (d->info_ptr == NULL || d->end_ptr == NULL)
{
fprintf(stderr, "%s(%s): could not allocate png info\n", d->file,
d->test);
clean_display(d);
exit(1);
}
png_init_io(d->png_ptr, fp);
/* Handle each argument in turn; multiple settings are possible for the same
* chunk and multiple calls will occur (the last one should override all
* preceding ones).
*/
for (i=0; i<argc; ++i)
{
const char *equals = strchr(argv[i], '=');
if (equals != NULL)
{
int chunk, option;
if (strcmp(equals+1, "default") == 0)
option = PNG_HANDLE_CHUNK_AS_DEFAULT;
else if (strcmp(equals+1, "discard") == 0)
option = PNG_HANDLE_CHUNK_NEVER;
else if (strcmp(equals+1, "if-safe") == 0)
option = PNG_HANDLE_CHUNK_IF_SAFE;
else if (strcmp(equals+1, "save") == 0)
option = PNG_HANDLE_CHUNK_ALWAYS;
else
{
fprintf(stderr, "%s(%s): %s: unrecognized chunk option\n", d->file,
d->test, argv[i]);
display_exit(d);
}
switch (equals - argv[i])
{
case 4: /* chunk name */
chunk = find(argv[i]);
if (chunk >= 0)
{
/* These #if tests have the effect of skipping the arguments
* if SAVE support is unavailable - we can't do a useful test
* in this case, so we just check the arguments! This could
* be improved in the future by using the read callback.
*/
# ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
png_byte name[5];
memcpy(name, chunk_info[chunk].name, 5);
png_set_keep_unknown_chunks(d->png_ptr, option, name, 1);
chunk_info[chunk].keep = option;
# endif
continue;
}
break;
case 7: /* default */
if (memcmp(argv[i], "default", 7) == 0)
{
# ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
png_set_keep_unknown_chunks(d->png_ptr, option, NULL, 0);
# endif
def = option;
continue;
}
break;
case 3: /* all */
if (memcmp(argv[i], "all", 3) == 0)
{
# ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
png_set_keep_unknown_chunks(d->png_ptr, option, NULL, -1);
def = option;
for (chunk = 0; chunk < NINFO; ++chunk)
if (chunk_info[chunk].all)
chunk_info[chunk].keep = option;
# endif
continue;
}
break;
default: /* some misplaced = */
break;
}
}
fprintf(stderr, "%s(%s): %s: unrecognized chunk argument\n", d->file,
d->test, argv[i]);
display_exit(d);
}
png_read_info(d->png_ptr, d->info_ptr);
switch (png_get_interlace_type(d->png_ptr, d->info_ptr))
{
case PNG_INTERLACE_NONE:
npasses = 1;
break;
case PNG_INTERLACE_ADAM7:
npasses = PNG_INTERLACE_ADAM7_PASSES;
break;
default:
/* Hard error because it is not test specific */
fprintf(stderr, "%s(%s): invalid interlace type\n", d->file, d->test);
clean_display(d);
exit(1);
}
/* Skip the image data, if IDAT is not being handled then don't do this
* because it will cause a CRC error.
*/
if (chunk_info[0/*IDAT*/].keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
{
png_start_read_image(d->png_ptr);
height = png_get_image_height(d->png_ptr, d->info_ptr);
if (npasses > 1)
{
png_uint_32 width = png_get_image_width(d->png_ptr, d->info_ptr);
for (ipass=0; ipass<npasses; ++ipass)
{
png_uint_32 wPass = PNG_PASS_COLS(width, ipass);
if (wPass > 0)
{
png_uint_32 y;
for (y=0; y<height; ++y) if (PNG_ROW_IN_INTERLACE_PASS(y, ipass))
png_read_row(d->png_ptr, NULL, NULL);
}
}
} /* interlaced */
else /* not interlaced */
{
png_uint_32 y;
for (y=0; y<height; ++y)
png_read_row(d->png_ptr, NULL, NULL);
}
}
png_read_end(d->png_ptr, d->end_ptr);
flags[0] = get_valid(d, d->info_ptr);
flags[1] = get_unknown(d, def, d->info_ptr);
/* Only png_read_png sets PNG_INFO_IDAT! */
flags[chunk_info[0/*IDAT*/].keep != PNG_HANDLE_CHUNK_AS_DEFAULT] |=
PNG_INFO_IDAT;
flags[2] = get_valid(d, d->end_ptr);
flags[3] = get_unknown(d, def, d->end_ptr);
clean_display(d);
return def;
}
static void
check_error(display *d, png_uint_32 flags, const char *message)
{
while (flags)
{
png_uint_32 flag = flags & -(png_int_32)flags;
int i = find_by_flag(flag);
fprintf(stderr, "%s(%s): chunk %s: %s\n", d->file, d->test,
chunk_info[i].name, message);
++(d->error_count);
flags &= ~flag;
}
}
static void
check_handling(display *d, int def, png_uint_32 chunks, png_uint_32 known,
png_uint_32 unknown, const char *position)
{
while (chunks)
{
png_uint_32 flag = chunks & -(png_int_32)chunks;
int i = find_by_flag(flag);
int keep = chunk_info[i].keep;
const char *type;
const char *errorx = NULL;
if (chunk_info[i].unknown)
{
if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
{
type = "UNKNOWN (default)";
keep = def;
}
else
type = "UNKNOWN (specified)";
if (flag & known)
errorx = "chunk processed";
else switch (keep)
{
case PNG_HANDLE_CHUNK_AS_DEFAULT:
if (flag & unknown)
errorx = "DEFAULT: unknown chunk saved";
break;
case PNG_HANDLE_CHUNK_NEVER:
if (flag & unknown)
errorx = "DISCARD: unknown chunk saved";
break;
case PNG_HANDLE_CHUNK_IF_SAFE:
if (ancillary(chunk_info[i].name))
{
if (!(flag & unknown))
errorx = "IF-SAFE: unknown ancillary chunk lost";
}
else if (flag & unknown)
errorx = "IF-SAFE: unknown critical chunk saved";
break;
case PNG_HANDLE_CHUNK_ALWAYS:
if (!(flag & unknown))
errorx = "SAVE: unknown chunk lost";
break;
default:
errorx = "internal error: bad keep";
break;
}
} /* unknown chunk */
else /* known chunk */
{
type = "KNOWN";
if (flag & known)
{
/* chunk was processed, it won't have been saved because that is
* caught below when checking for inconsistent processing.
*/
if (keep != PNG_HANDLE_CHUNK_AS_DEFAULT)
errorx = "!DEFAULT: known chunk processed";
}
else /* not processed */ switch (keep)
{
case PNG_HANDLE_CHUNK_AS_DEFAULT:
errorx = "DEFAULT: known chunk not processed";
break;
case PNG_HANDLE_CHUNK_NEVER:
if (flag & unknown)
errorx = "DISCARD: known chunk saved";
break;
case PNG_HANDLE_CHUNK_IF_SAFE:
if (ancillary(chunk_info[i].name))
{
if (!(flag & unknown))
errorx = "IF-SAFE: known ancillary chunk lost";
}
else if (flag & unknown)
errorx = "IF-SAFE: known critical chunk saved";
break;
case PNG_HANDLE_CHUNK_ALWAYS:
if (!(flag & unknown))
errorx = "SAVE: known chunk lost";
break;
default:
errorx = "internal error: bad keep (2)";
break;
}
}
if (errorx != NULL)
{
++(d->error_count);
fprintf(stderr, "%s(%s): %s %s %s: %s\n",
d->file, d->test, type, chunk_info[i].name, position, errorx);
}
chunks &= ~flag;
}
}
static void
perform_one_test(FILE *fp, int argc, const char **argv,
png_uint_32 *default_flags, display *d)
{
int def;
png_uint_32 flags[2][4];
rewind(fp);
clear_keep();
memcpy(flags[0], default_flags, sizeof flags[0]);
def = check(fp, argc, argv, flags[1], d);
/* Chunks should either be known or unknown, never both and this should apply
* whether the chunk is before or after the IDAT (actually, the app can
* probably change this by swapping the handling after the image, but this
* test does not do that.)
*/
check_error(d, (flags[0][0]|flags[0][2]) & (flags[0][1]|flags[0][3]),
"chunk handled inconsistently in count tests");
check_error(d, (flags[1][0]|flags[1][2]) & (flags[1][1]|flags[1][3]),
"chunk handled inconsistently in option tests");
/* Now find out what happened to each chunk before and after the IDAT and
* determine if the behavior was correct. First some basic sanity checks,
* any known chunk should be known in the original count, any unknown chunk
* should be either known or unknown in the original.
*/
{
png_uint_32 test;
test = flags[1][0] & ~flags[0][0];
check_error(d, test, "new known chunk before IDAT");
test = flags[1][1] & ~(flags[0][0] | flags[0][1]);
check_error(d, test, "new unknown chunk before IDAT");
test = flags[1][2] & ~flags[0][2];
check_error(d, test, "new known chunk after IDAT");
test = flags[1][3] & ~(flags[0][2] | flags[0][3]);
check_error(d, test, "new unknown chunk after IDAT");
}
/* Now each chunk in the original list should have been handled according to
* the options set for that chunk, regardless of whether libpng knows about
* it or not.
*/
check_handling(d, def, flags[0][0] | flags[0][1], flags[1][0], flags[1][1],
"before IDAT");
check_handling(d, def, flags[0][2] | flags[0][3], flags[1][2], flags[1][3],
"after IDAT");
}
static void
perform_one_test_safe(FILE *fp, int argc, const char **argv,
png_uint_32 *default_flags, display *d, const char *test)
{
if (setjmp(d->error_return) == 0)
{
d->test = test; /* allow use of d->error_return */
perform_one_test(fp, argc, argv, default_flags, d);
d->test = init; /* prevent use of d->error_return */
}
}
static const char *standard_tests[] =
{
"discard", "default=discard", 0,
"save", "default=save", 0,
"if-safe", "default=if-safe", 0,
"vpAg", "vpAg=if-safe", 0,
"sTER", "sTER=if-safe", 0,
"IDAT", "default=discard", "IDAT=save", 0,
"sAPI", "bKGD=save", "cHRM=save", "gAMA=save", "all=discard", "iCCP=save",
"sBIT=save", "sRGB=save", 0,
0/*end*/
};
static PNG_NORETURN void
usage(const char *program, const char *reason)
{
fprintf(stderr, "pngunknown: %s: usage:\n %s [--strict] "
"--default|{(CHNK|default|all)=(default|discard|if-safe|save)} "
"testfile.png\n", reason, program);
exit(2);
}
int
main(int argc, const char **argv)
{
FILE *fp;
png_uint_32 default_flags[4/*valid,unknown{before,after}*/];
int strict = 0, default_tests = 0;
const char *count_argv = "default=save";
const char *touch_file = NULL;
display d;
init_display(&d, argv[0]);
while (++argv, --argc > 0)
{
if (strcmp(*argv, "--strict") == 0)
strict = 1;
else if (strcmp(*argv, "--default") == 0)
default_tests = 1;
else if (strcmp(*argv, "--touch") == 0)
{
if (argc > 1)
touch_file = *++argv, --argc;
else
usage(d.program, "--touch: missing file name");
}
else
break;
}
/* A file name is required, but there should be no other arguments if
* --default was specified.
*/
if (argc <= 0)
usage(d.program, "missing test file");
/* GCC BUG: if (default_tests && argc != 1) triggers some weird GCC argc
* optimization which causes warnings with -Wstrict-overflow!
*/
else if (default_tests) if (argc != 1)
usage(d.program, "extra arguments");
# ifndef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
fprintf(stderr, "%s: warning: no 'save' support so arguments ignored\n",
d.program);
# endif
/* The name of the test file is the last argument; remove it. */
d.file = argv[--argc];
fp = fopen(d.file, "rb");
if (fp == NULL)
{
perror(d.file);
exit(2);
}
/* First find all the chunks, known and unknown, in the test file, a failure
* here aborts the whole test.
*/
if (check(fp, 1, &count_argv, default_flags, &d) !=
PNG_HANDLE_CHUNK_ALWAYS)
{
fprintf(stderr, "%s: %s: internal error\n", d.program, d.file);
exit(3);
}
/* Now find what the various supplied options cause to change: */
if (!default_tests)
{
d.test = cmd; /* acts as a flag to say exit, do not longjmp */
perform_one_test(fp, argc, argv, default_flags, &d);
d.test = init;
}
else
{
const char **test = standard_tests;
/* Set the exit_test pointer here so we can continue after a libpng error.
* NOTE: this leaks memory because the png_struct data from the failing
* test is never freed.
*/
while (*test)
{
const char *this_test = *test++;
const char **next = test;
int count = display_rc(&d, strict), new_count;
const char *result;
int arg_count = 0;
while (*next) ++next, ++arg_count;
perform_one_test_safe(fp, arg_count, test, default_flags, &d,
this_test);
new_count = display_rc(&d, strict);
if (new_count == count)
result = "PASS";
else
result = "FAIL";
printf("%s: %s %s\n", result, d.program, this_test);
test = next+1;
}
}
fclose(fp);
if (display_rc(&d, strict) == 0)
{
/* Success, touch the success file if appropriate */
if (touch_file != NULL)
{
FILE *fsuccess = fopen(touch_file, "wt");
if (fsuccess != NULL)
{
int err = 0;
fprintf(fsuccess, "PNG unknown tests succeeded\n");
fflush(fsuccess);
err = ferror(fsuccess);
if (fclose(fsuccess) || err)
{
fprintf(stderr, "%s: write failed\n", touch_file);
exit(1);
}
}
else
{
fprintf(stderr, "%s: open failed\n", touch_file);
exit(1);
}
}
return 0;
}
return 1;
}
#else
int
main(void)
{
fprintf(stderr,
" test ignored because libpng was not built with unknown chunk support\n");
return 0;
}
#endif

View File

@@ -1,8 +1,8 @@
/* pngvalid.c - validate libpng by constructing then reading png files.
*
* Last changed in libpng 1.6.0 [(PENDING RELEASE)]
* Copyright (c) 2012 Glenn Randers-Pehrson
* Last changed in libpng 1.6.0 [February 14, 2013]
* Copyright (c) 2013 Glenn Randers-Pehrson
* Written by John Cunningham Bowler
*
* This code is released under the libpng license.
@@ -24,8 +24,9 @@
#define _GNU_SOURCE 1 /* For the floating point exception extension */
#include <signal.h>
#include <stdio.h>
#if (defined HAVE_CONFIG_H) && !(defined PNG_NO_CONFIG_H)
#if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
# include <config.h>
#endif
@@ -42,6 +43,8 @@
# include "../../png.h"
#endif
#ifdef PNG_WRITE_SUPPORTED /* else pngvalid can do nothing */
#if PNG_LIBPNG_VER < 10500
/* This deliberately lacks the PNG_CONST. */
typedef png_byte *png_const_bytep;
@@ -82,10 +85,10 @@ typedef png_byte *png_const_bytep;
/* 1.6.0 constifies many APIs, the following exists to allow pngvalid to be
* compiled against earlier versions.
*/
# define png_const_strutp png_structp
# define png_const_structp png_structp
#endif
#include "zlib.h" /* For crc32 */
#include <zlib.h> /* For crc32 */
#include <float.h> /* For floating point constants */
#include <stdlib.h> /* For malloc */
@@ -104,7 +107,11 @@ typedef png_byte *png_const_bytep;
#endif
/***************************** EXCEPTION HANDLING *****************************/
#include "../visupng/cexcept.h"
#ifdef PNG_FREESTANDING_TESTS
# include <cexcept.h>
#else
# include "../visupng/cexcept.h"
#endif
#ifdef __cplusplus
# define this not_the_cpp_this
@@ -170,6 +177,7 @@ static PNG_CONST char *colour_types[8] =
"grayscale with alpha", invalid, "truecolour with alpha", invalid
};
#ifdef PNG_READ_SUPPORTED
/* Convert a double precision value to fixed point. */
static png_fixed_point
fix(double d)
@@ -177,12 +185,14 @@ fix(double d)
d = floor(d * PNG_FP_1 + .5);
return (png_fixed_point)d;
}
#endif /* PNG_READ_SUPPORTED */
/* Generate random bytes. This uses a boring repeatable algorithm and it
* is implemented here so that it gives the same set of numbers on every
* architecture. It's a linear congruential generator (Knuth or Sedgewick
* "Algorithms") but it comes from the 'feedback taps' table in Horowitz and
* Hill, "The Art of Electronics".
* Hill, "The Art of Electronics" (Pseudo-Random Bit Sequences and Noise
* Generation.)
*/
static void
make_random_bytes(png_uint_32* seed, void* pv, size_t size)
@@ -215,6 +225,7 @@ make_four_random_bytes(png_uint_32* seed, png_bytep bytes)
make_random_bytes(seed, bytes, 4);
}
#ifdef PNG_READ_SUPPORTED
static void
randomize(void *pv, size_t size)
{
@@ -234,6 +245,7 @@ random_mod(unsigned int max)
return x % max; /* 0 .. max-1 */
}
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
static int
random_choice(void)
{
@@ -243,6 +255,8 @@ random_choice(void)
return x & 1;
}
#endif
#endif /* PNG_READ_SUPPORTED */
/* A numeric ID based on PNG file characteristics. The 'do_interlace' field
* simply records whether pngvalid did the interlace itself or whether it
@@ -255,7 +269,7 @@ random_choice(void)
#define COL_FROM_ID(id) ((png_byte)((id)& 0x7U))
#define DEPTH_FROM_ID(id) ((png_byte)(((id) >> 3) & 0x1fU))
#define PALETTE_FROM_ID(id) ((int)(((id) >> 8) & 0x1f))
#define PALETTE_FROM_ID(id) (((id) >> 8) & 0x1f)
#define INTERLACE_FROM_ID(id) ((int)(((id) >> 13) & 0x3))
#define DO_INTERLACE_FROM_ID(id) ((int)(((id)>>15) & 1))
#define WIDTH_FROM_ID(id) (((id)>>16) & 0xff)
@@ -264,7 +278,7 @@ random_choice(void)
/* Utility to construct a standard name for a standard image. */
static size_t
standard_name(char *buffer, size_t bufsize, size_t pos, png_byte colour_type,
int bit_depth, int npalette, int interlace_type,
int bit_depth, unsigned int npalette, int interlace_type,
png_uint_32 w, png_uint_32 h, int do_interlace)
{
pos = safecat(buffer, bufsize, pos, colour_types[colour_type]);
@@ -326,10 +340,11 @@ standard_name_from_id(char *buffer, size_t bufsize, size_t pos, png_uint_32 id)
/* The following defines the number of different palettes to generate for
* each log bit depth of a colour type 3 standard image.
*/
#define PALETTE_COUNT(bit_depth) ((bit_depth) > 4 ? 1 : 16)
#define PALETTE_COUNT(bit_depth) ((bit_depth) > 4 ? 1U : 16U)
static int
next_format(png_bytep colour_type, png_bytep bit_depth, int* palette_number)
next_format(png_bytep colour_type, png_bytep bit_depth,
unsigned int* palette_number)
{
if (*bit_depth == 0)
{
@@ -460,6 +475,7 @@ pixel_copy(png_bytep toBuffer, png_uint_32 toIndex,
memmove(toBuffer+(toIndex>>3), fromBuffer+(fromIndex>>3), pixelSize>>3);
}
#ifdef PNG_READ_SUPPORTED
/* Copy a complete row of pixels, taking into account potential partial
* bytes at the end.
*/
@@ -525,6 +541,7 @@ pixel_cmp(png_const_bytep pa, png_const_bytep pb, png_uint_32 bit_width)
return 1+where;
}
}
#endif /* PNG_READ_SUPPORTED */
/*************************** BASIC PNG FILE WRITING ***************************/
/* A png_store takes data from the sequential writer or provides data
@@ -635,6 +652,7 @@ store_pool_mark(png_bytep mark)
make_four_random_bytes(store_seed, mark);
}
#ifdef PNG_READ_SUPPORTED
/* Use this for random 32 bit values; this function makes sure the result is
* non-zero.
*/
@@ -654,6 +672,7 @@ random_32(void)
return result;
}
}
#endif /* PNG_READ_SUPPORTED */
static void
store_pool_init(png_store *ps, store_pool *pool)
@@ -859,6 +878,7 @@ store_log(png_store* ps, png_const_structp pp, png_const_charp message,
store_verbose(ps, pp, is_error ? "error: " : "warning: ", message);
}
#ifdef PNG_READ_SUPPORTED
/* Internal error function, called with a png_store but no libpng stuff. */
static void
internal_error(png_store *ps, png_const_charp message)
@@ -871,6 +891,7 @@ internal_error(png_store *ps, png_const_charp message)
Throw ps;
}
}
#endif /* PNG_READ_SUPPORTED */
/* Functions to use as PNG callbacks. */
static void
@@ -1008,6 +1029,7 @@ store_ensure_image(png_store *ps, png_const_structp pp, int nImages,
}
}
#ifdef PNG_READ_SUPPORTED
static void
store_image_check(PNG_CONST png_store* ps, png_const_structp pp, int iImage)
{
@@ -1037,6 +1059,7 @@ store_image_check(PNG_CONST png_store* ps, png_const_structp pp, int iImage)
}
}
}
#endif /* PNG_READ_SUPPORTED */
static void
store_write(png_structp ppIn, png_bytep pb, png_size_t st)
@@ -1072,6 +1095,7 @@ store_flush(png_structp ppIn)
UNUSED(ppIn) /*DOES NOTHING*/
}
#ifdef PNG_READ_SUPPORTED
static size_t
store_read_buffer_size(png_store *ps)
{
@@ -1189,6 +1213,7 @@ store_progressive_read(png_store *ps, png_structp pp, png_infop pi)
}
while (store_read_buffer_next(ps));
}
#endif /* PNG_READ_SUPPORTED */
/* The caller must fill this in: */
static store_palette_entry *
@@ -1215,6 +1240,7 @@ store_write_palette(png_store *ps, int npalette)
return ps->palette;
}
#ifdef PNG_READ_SUPPORTED
static store_palette_entry *
store_current_palette(png_store *ps, int *npalette)
{
@@ -1228,6 +1254,7 @@ store_current_palette(png_store *ps, int *npalette)
*npalette = ps->current->npalette;
return ps->current->palette;
}
#endif /* PNG_READ_SUPPORTED */
/***************************** MEMORY MANAGEMENT*** ***************************/
/* A store_memory is simply the header for an allocated block of memory. The
@@ -1502,25 +1529,29 @@ set_store_for_write(png_store *ps, png_infopp ppi,
}
/* Cleanup when finished reading (either due to error or in the success case).
* This routine exists even when there is no read support to make the code
* tidier (avoid a mass of ifdefs) and so easier to maintain.
*/
static void
store_read_reset(png_store *ps)
{
if (ps->pread != NULL)
{
anon_context(ps);
Try
png_destroy_read_struct(&ps->pread, &ps->piread, NULL);
Catch_anonymous
# ifdef PNG_READ_SUPPORTED
if (ps->pread != NULL)
{
/* error already output: continue */
}
anon_context(ps);
ps->pread = NULL;
ps->piread = NULL;
}
Try
png_destroy_read_struct(&ps->pread, &ps->piread, NULL);
Catch_anonymous
{
/* error already output: continue */
}
ps->pread = NULL;
ps->piread = NULL;
}
# endif
/* Always do this to be safe. */
store_pool_delete(ps, &ps->read_memory_pool);
@@ -1531,6 +1562,7 @@ store_read_reset(png_store *ps)
ps->validated = 0;
}
#ifdef PNG_READ_SUPPORTED
static void
store_read_set(png_store *ps, png_uint_32 id)
{
@@ -1608,6 +1640,7 @@ set_store_for_read(png_store *ps, png_infopp ppi, png_uint_32 id,
return ps->pread;
}
#endif /* PNG_READ_SUPPORTED */
/* The overall cleanup of a store simply calls the above then removes all the
* saved files. This does not delete the store itself.
@@ -1647,18 +1680,6 @@ typedef struct CIE_color
double X, Y, Z;
} CIE_color;
static double
chromaticity_x(CIE_color c)
{
return c.X / (c.X + c.Y + c.Z);
}
static double
chromaticity_y(CIE_color c)
{
return c.Y / (c.X + c.Y + c.Z);
}
typedef struct color_encoding
{
/* A description of an (R,G,B) encoding of color (as defined above); this
@@ -1671,11 +1692,24 @@ typedef struct color_encoding
CIE_color red, green, blue; /* End points */
} color_encoding;
#ifdef PNG_READ_SUPPORTED
static double
chromaticity_x(CIE_color c)
{
return c.X / (c.X + c.Y + c.Z);
}
static double
chromaticity_y(CIE_color c)
{
return c.Y / (c.X + c.Y + c.Z);
}
static CIE_color
white_point(PNG_CONST color_encoding *encoding)
{
CIE_color white;
white.X = encoding->red.X + encoding->green.X + encoding->blue.X;
white.Y = encoding->red.Y + encoding->green.Y + encoding->blue.Y;
white.Z = encoding->red.Z + encoding->green.Z + encoding->blue.Z;
@@ -1683,6 +1717,7 @@ white_point(PNG_CONST color_encoding *encoding)
return white;
}
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
static void
normalize_color_encoding(color_encoding *encoding)
{
@@ -1702,6 +1737,7 @@ normalize_color_encoding(color_encoding *encoding)
encoding->blue.Z /= whiteY;
}
}
#endif
static size_t
safecat_color_encoding(char *buffer, size_t bufsize, size_t pos,
@@ -1742,6 +1778,7 @@ safecat_color_encoding(char *buffer, size_t bufsize, size_t pos,
return pos;
}
#endif /* PNG_READ_SUPPORTED */
typedef struct png_modifier
{
@@ -1935,6 +1972,7 @@ modifier_init(png_modifier *pm)
* to a calculation - not a digitization operation - unless the following API is
* called directly.
*/
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
static double digitize(PNG_CONST png_modifier *pm, double value,
int sample_depth, int do_round)
{
@@ -1959,7 +1997,10 @@ static double digitize(PNG_CONST png_modifier *pm, double value,
if (do_round) value += .5;
return floor(value)/digitization_factor;
}
#endif
#if defined(PNG_READ_GAMMA_SUPPORTED) ||\
defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
static double abserr(PNG_CONST png_modifier *pm, int in_depth, int out_depth)
{
/* Absolute error permitted in linear values - affected by the bit depth of
@@ -1971,7 +2012,9 @@ static double abserr(PNG_CONST png_modifier *pm, int in_depth, int out_depth)
else
return pm->maxabs8;
}
#endif
#ifdef PNG_READ_GAMMA_SUPPORTED
static double calcerr(PNG_CONST png_modifier *pm, int in_depth, int out_depth)
{
/* Error in the linear composition arithmetic - only relevant when
@@ -2088,6 +2131,7 @@ static int output_quantization_factor(PNG_CONST png_modifier *pm, int in_depth,
else
return 1;
}
#endif /* PNG_READ_GAMMA_SUPPORTED */
/* One modification structure must be provided for each chunk to be modified (in
* fact more than one can be provided if multiple separate changes are desired
@@ -2140,6 +2184,7 @@ modification_init(png_modification *pmm)
modification_reset(pmm);
}
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
static void
modifier_current_encoding(PNG_CONST png_modifier *pm, color_encoding *ce)
{
@@ -2151,6 +2196,7 @@ modifier_current_encoding(PNG_CONST png_modifier *pm, color_encoding *ce)
ce->gamma = pm->current_gamma;
}
#endif
static size_t
safecat_current_encoding(char *buffer, size_t bufsize, size_t pos,
@@ -2776,6 +2822,7 @@ srgb_modification_init(srgb_modification *me, png_modifier *pm, png_byte intent)
pm->modifications = &me->this;
}
#ifdef PNG_READ_GAMMA_SUPPORTED
typedef struct sbit_modification
{
png_modification this;
@@ -2842,6 +2889,7 @@ sbit_modification_init(sbit_modification *me, png_modifier *pm, png_byte sbit)
me->this.next = pm->modifications;
pm->modifications = &me->this;
}
#endif /* PNG_READ_GAMMA_SUPPORTED */
#endif /* PNG_READ_TRANSFORMS_SUPPORTED */
/***************************** STANDARD PNG FILES *****************************/
@@ -2903,9 +2951,9 @@ make_standard_palette(png_store* ps, int npalette, int do_tRNS)
*/
for (; i<8; ++i)
{
values[i][1] = (i&1) ? 255 : 0;
values[i][2] = (i&2) ? 255 : 0;
values[i][3] = (i&4) ? 255 : 0;
values[i][1] = (png_byte)((i&1) ? 255U : 0U);
values[i][2] = (png_byte)((i&2) ? 255U : 0U);
values[i][3] = (png_byte)((i&4) ? 255U : 0U);
}
/* Then add 62 grays (one quarter of the remaining 256 slots). */
@@ -3120,6 +3168,7 @@ transform_height(png_const_structp pp, png_byte colour_type, png_byte bit_depth)
}
}
#ifdef PNG_READ_SUPPORTED
/* The following can only be defined here, now we have the definitions
* of the transform image sizes.
*/
@@ -3155,6 +3204,7 @@ standard_rowsize(png_const_structp pp, png_uint_32 id)
width *= bit_size(pp, COL_FROM_ID(id), DEPTH_FROM_ID(id));
return (width + 7) / 8;
}
#endif /* PNG_READ_SUPPORTED */
static void
transform_row(png_const_structp pp, png_byte buffer[TRANSFORM_ROWMAX],
@@ -3166,20 +3216,20 @@ transform_row(png_const_structp pp, png_byte buffer[TRANSFORM_ROWMAX],
switch (bit_size(pp, colour_type, bit_depth))
{
case 1:
while (i<128/8) buffer[i] = v & 0xff, v += 17, ++i;
while (i<128/8) buffer[i] = (png_byte)(v & 0xff), v += 17, ++i;
return;
case 2:
while (i<128/4) buffer[i] = v & 0xff, v += 33, ++i;
while (i<128/4) buffer[i] = (png_byte)(v & 0xff), v += 33, ++i;
return;
case 4:
while (i<128/2) buffer[i] = v & 0xff, v += 65, ++i;
while (i<128/2) buffer[i] = (png_byte)(v & 0xff), v += 65, ++i;
return;
case 8:
/* 256 bytes total, 128 bytes in each row set as follows: */
while (i<128) buffer[i] = v & 0xff, ++v, ++i;
while (i<128) buffer[i] = (png_byte)(v & 0xff), ++v, ++i;
return;
case 16:
@@ -3187,7 +3237,12 @@ transform_row(png_const_structp pp, png_byte buffer[TRANSFORM_ROWMAX],
* GA case as well as the 16 bit G case.
*/
while (i<128)
buffer[2*i] = (v>>8) & 0xff, buffer[2*i+1] = v & 0xff, ++v, ++i;
{
buffer[2*i] = (png_byte)((v>>8) & 0xff);
buffer[2*i+1] = (png_byte)(v & 0xff);
++v;
++i;
}
return;
@@ -3196,9 +3251,9 @@ transform_row(png_const_structp pp, png_byte buffer[TRANSFORM_ROWMAX],
while (i<128)
{
/* Three bytes per pixel, r, g, b, make b by r^g */
buffer[3*i+0] = (v >> 8) & 0xff;
buffer[3*i+1] = v & 0xff;
buffer[3*i+2] = ((v >> 8) ^ v) & 0xff;
buffer[3*i+0] = (png_byte)((v >> 8) & 0xff);
buffer[3*i+1] = (png_byte)(v & 0xff);
buffer[3*i+2] = (png_byte)(((v >> 8) ^ v) & 0xff);
++v;
++i;
}
@@ -3209,10 +3264,10 @@ transform_row(png_const_structp pp, png_byte buffer[TRANSFORM_ROWMAX],
/* 65535 pixels, r, g, b, a; just replicate */
while (i<128)
{
buffer[4*i+0] = (v >> 8) & 0xff;
buffer[4*i+1] = v & 0xff;
buffer[4*i+2] = (v >> 8) & 0xff;
buffer[4*i+3] = v & 0xff;
buffer[4*i+0] = (png_byte)((v >> 8) & 0xff);
buffer[4*i+1] = (png_byte)(v & 0xff);
buffer[4*i+2] = (png_byte)((v >> 8) & 0xff);
buffer[4*i+3] = (png_byte)(v & 0xff);
++v;
++i;
}
@@ -3226,14 +3281,14 @@ transform_row(png_const_structp pp, png_byte buffer[TRANSFORM_ROWMAX],
while (i<128)
{
png_uint_32 t = v++;
buffer[6*i+0] = (t >> 8) & 0xff;
buffer[6*i+1] = t & 0xff;
buffer[6*i+0] = (png_byte)((t >> 8) & 0xff);
buffer[6*i+1] = (png_byte)(t & 0xff);
t *= 257;
buffer[6*i+2] = (t >> 8) & 0xff;
buffer[6*i+3] = t & 0xff;
buffer[6*i+2] = (png_byte)((t >> 8) & 0xff);
buffer[6*i+3] = (png_byte)(t & 0xff);
t *= 17;
buffer[6*i+4] = (t >> 8) & 0xff;
buffer[6*i+5] = t & 0xff;
buffer[6*i+4] = (png_byte)((t >> 8) & 0xff);
buffer[6*i+5] = (png_byte)(t & 0xff);
++i;
}
@@ -3244,15 +3299,15 @@ transform_row(png_const_structp pp, png_byte buffer[TRANSFORM_ROWMAX],
while (i<128)
{
png_uint_32 t = v++;
buffer[8*i+0] = (t >> 8) & 0xff;
buffer[8*i+1] = t & 0xff;
buffer[8*i+4] = (t >> 8) & 0xff;
buffer[8*i+5] = t & 0xff;
buffer[8*i+0] = (png_byte)((t >> 8) & 0xff);
buffer[8*i+1] = (png_byte)(t & 0xff);
buffer[8*i+4] = (png_byte)((t >> 8) & 0xff);
buffer[8*i+5] = (png_byte)(t & 0xff);
t *= 257;
buffer[8*i+2] = (t >> 8) & 0xff;
buffer[8*i+3] = t & 0xff;
buffer[8*i+6] = (t >> 8) & 0xff;
buffer[8*i+7] = t & 0xff;
buffer[8*i+2] = (png_byte)((t >> 8) & 0xff);
buffer[8*i+3] = (png_byte)(t & 0xff);
buffer[8*i+6] = (png_byte)((t >> 8) & 0xff);
buffer[8*i+7] = (png_byte)(t & 0xff);
++i;
}
return;
@@ -3277,8 +3332,8 @@ transform_row(png_const_structp pp, png_byte buffer[TRANSFORM_ROWMAX],
*/
static void
make_transform_image(png_store* PNG_CONST ps, png_byte PNG_CONST colour_type,
png_byte PNG_CONST bit_depth, int palette_number, int interlace_type,
png_const_charp name)
png_byte PNG_CONST bit_depth, unsigned int palette_number,
int interlace_type, png_const_charp name)
{
context(ps, fault);
@@ -3302,6 +3357,11 @@ make_transform_image(png_store* PNG_CONST ps, png_byte PNG_CONST colour_type,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
#ifdef PNG_TEXT_SUPPORTED
# if defined(PNG_READ_zTXt_SUPPORTED) && defined(PNG_WRITE_zTXt_SUPPORTED)
# define TEXT_COMPRESSION PNG_TEXT_COMPRESSION_zTXt
# else
# define TEXT_COMPRESSION PNG_TEXT_COMPRESSION_NONE
# endif
{
static char key[] = "image name"; /* must be writeable */
size_t pos;
@@ -3311,7 +3371,7 @@ make_transform_image(png_store* PNG_CONST ps, png_byte PNG_CONST colour_type,
/* Use a compressed text string to test the correct interaction of text
* compression and IDAT compression.
*/
text.compression = PNG_TEXT_COMPRESSION_zTXt;
text.compression = TEXT_COMPRESSION;
text.key = key;
/* Yuck: the text must be writable! */
pos = safecat(copy, sizeof copy, 0, ps->wname);
@@ -3369,7 +3429,7 @@ make_transform_image(png_store* PNG_CONST ps, png_byte PNG_CONST colour_type,
/* Use a compressed text string to test the correct interaction of text
* compression and IDAT compression.
*/
text.compression = PNG_TEXT_COMPRESSION_zTXt;
text.compression = TEXT_COMPRESSION;
text.key = key;
text.text = comment;
text.text_length = (sizeof comment)-1;
@@ -3405,7 +3465,7 @@ make_transform_images(png_store *ps)
{
png_byte colour_type = 0;
png_byte bit_depth = 0;
int palette_number = 0;
unsigned int palette_number = 0;
/* This is in case of errors. */
safecat(ps->test, sizeof ps->test, 0, "make standard images");
@@ -3453,6 +3513,7 @@ interlace_row(png_bytep buffer, png_const_bytep imageRow,
}
}
#ifdef PNG_READ_SUPPORTED
static void
deinterlace_row(png_bytep buffer, png_const_bytep row,
unsigned int pixel_size, png_uint_32 w, int pass)
@@ -3473,6 +3534,7 @@ deinterlace_row(png_bytep buffer, png_const_bytep row,
++xin;
}
}
#endif /* PNG_READ_SUPPORTED */
/* Build a single row for the 'size' test images; this fills in only the
* first bit_width bits of the sample row.
@@ -3530,6 +3592,30 @@ make_size_image(png_store* PNG_CONST ps, png_byte PNG_CONST colour_type,
png_set_IHDR(pp, pi, w, h, bit_depth, colour_type, interlace_type,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
#ifdef PNG_TEXT_SUPPORTED
{
static char key[] = "image name"; /* must be writeable */
size_t pos;
png_text text;
char copy[FILE_NAME_SIZE];
/* Use a compressed text string to test the correct interaction of text
* compression and IDAT compression.
*/
text.compression = TEXT_COMPRESSION;
text.key = key;
/* Yuck: the text must be writable! */
pos = safecat(copy, sizeof copy, 0, ps->wname);
text.text = copy;
text.text_length = pos;
text.itxt_length = 0;
text.lang = 0;
text.lang_key = 0;
png_set_text(pp, pi, &text, 1);
}
#endif
if (colour_type == 3) /* palette */
init_standard_palette(ps, pp, pi, 1U << bit_depth, 0/*do tRNS*/);
@@ -3607,6 +3693,27 @@ make_size_image(png_store* PNG_CONST ps, png_byte PNG_CONST colour_type,
}
}
#ifdef PNG_TEXT_SUPPORTED
{
static char key[] = "end marker";
static char comment[] = "end";
png_text text;
/* Use a compressed text string to test the correct interaction of text
* compression and IDAT compression.
*/
text.compression = TEXT_COMPRESSION;
text.key = key;
text.text = comment;
text.text_length = (sizeof comment)-1;
text.itxt_length = 0;
text.lang = 0;
text.lang_key = 0;
png_set_text(pp, pi, &text, 1);
}
#endif
png_write_end(pp, pi);
/* And store this under the appropriate id, then clean up. */
@@ -3671,6 +3778,7 @@ make_size_images(png_store *ps)
make_size(ps, 6, 3, WRITE_BDHI);
}
#ifdef PNG_READ_SUPPORTED
/* Return a row based on image id and 'y' for checking: */
static void
standard_row(png_const_structp pp, png_byte std[STANDARD_ROWMAX],
@@ -3682,6 +3790,7 @@ standard_row(png_const_structp pp, png_byte std[STANDARD_ROWMAX],
size_row(std, WIDTH_FROM_ID(id) * bit_size(pp, COL_FROM_ID(id),
DEPTH_FROM_ID(id)), y);
}
#endif /* PNG_READ_SUPPORTED */
/* Tests - individual test cases */
/* Like 'make_standard' but errors are deliberately introduced into the calls
@@ -3966,6 +4075,7 @@ perform_formatting_test(png_store *volatile ps)
#endif
}
#ifdef PNG_READ_SUPPORTED
/* Because we want to use the same code in both the progressive reader and the
* sequential reader it is necessary to deal with the fact that the progressive
* reader callbacks only have one parameter (png_get_progressive_ptr()), so this
@@ -4560,6 +4670,114 @@ sequential_row(standard_display *dp, png_structp pp, png_infop pi,
png_read_end(pp, pi);
}
#ifdef PNG_TEXT_SUPPORTED
static void
standard_check_text(png_const_structp pp, png_const_textp tp,
png_const_charp keyword, png_const_charp text)
{
char msg[1024];
size_t pos = safecat(msg, sizeof msg, 0, "text: ");
size_t ok;
pos = safecat(msg, sizeof msg, pos, keyword);
pos = safecat(msg, sizeof msg, pos, ": ");
ok = pos;
if (tp->compression != TEXT_COMPRESSION)
{
char buf[64];
sprintf(buf, "compression [%d->%d], ", TEXT_COMPRESSION,
tp->compression);
pos = safecat(msg, sizeof msg, pos, buf);
}
if (tp->key == NULL || strcmp(tp->key, keyword) != 0)
{
pos = safecat(msg, sizeof msg, pos, "keyword \"");
if (tp->key != NULL)
{
pos = safecat(msg, sizeof msg, pos, tp->key);
pos = safecat(msg, sizeof msg, pos, "\", ");
}
else
pos = safecat(msg, sizeof msg, pos, "null, ");
}
if (tp->text == NULL)
pos = safecat(msg, sizeof msg, pos, "text lost, ");
else
{
if (tp->text_length != strlen(text))
{
char buf[64];
sprintf(buf, "text length changed[%lu->%lu], ",
(unsigned long)strlen(text), (unsigned long)tp->text_length);
pos = safecat(msg, sizeof msg, pos, buf);
}
if (strcmp(tp->text, text) != 0)
{
pos = safecat(msg, sizeof msg, pos, "text becomes \"");
pos = safecat(msg, sizeof msg, pos, tp->text);
pos = safecat(msg, sizeof msg, pos, "\" (was \"");
pos = safecat(msg, sizeof msg, pos, text);
pos = safecat(msg, sizeof msg, pos, "\"), ");
}
}
if (tp->itxt_length != 0)
pos = safecat(msg, sizeof msg, pos, "iTXt length set, ");
if (tp->lang != NULL)
{
pos = safecat(msg, sizeof msg, pos, "iTXt language \"");
pos = safecat(msg, sizeof msg, pos, tp->lang);
pos = safecat(msg, sizeof msg, pos, "\", ");
}
if (tp->lang_key != NULL)
{
pos = safecat(msg, sizeof msg, pos, "iTXt keyword \"");
pos = safecat(msg, sizeof msg, pos, tp->lang_key);
pos = safecat(msg, sizeof msg, pos, "\", ");
}
if (pos > ok)
{
msg[pos-2] = '\0'; /* Remove the ", " at the end */
png_error(pp, msg);
}
}
static void
standard_text_validate(standard_display *dp, png_const_structp pp,
png_infop pi)
{
png_textp tp = NULL;
png_uint_32 num_text = png_get_text(pp, pi, &tp, NULL);
if (num_text == 2 && tp != NULL)
{
standard_check_text(pp, tp, "image name", dp->ps->current->name);
standard_check_text(pp, tp+1, "end marker", "end");
}
else
{
char msg[64];
sprintf(msg, "expected two text items, got %lu",
(unsigned long)num_text);
png_error(pp, msg);
}
}
#else
# define standard_text_validate(dp,pp,pi) ((void)0)
#endif
static void
standard_row_validate(standard_display *dp, png_const_structp pp,
int iImage, int iDisplay, png_uint_32 y)
@@ -4591,8 +4809,8 @@ standard_row_validate(standard_display *dp, png_const_structp pp,
dp->bit_width)) != 0)
{
char msg[64];
sprintf(msg, "PNG image row[%d][%d] changed from %.2x to %.2x", y,
where-1, std[where-1],
sprintf(msg, "PNG image row[%lu][%d] changed from %.2x to %.2x",
(unsigned long)y, where-1, std[where-1],
store_image_row(dp->ps, pp, iImage, y)[where-1]);
png_error(pp, msg);
}
@@ -4609,8 +4827,8 @@ standard_row_validate(standard_display *dp, png_const_structp pp,
dp->bit_width)) != 0)
{
char msg[64];
sprintf(msg, "display row[%d][%d] changed from %.2x to %.2x", y,
where-1, std[where-1],
sprintf(msg, "display row[%lu][%d] changed from %.2x to %.2x",
(unsigned long)y, where-1, std[where-1],
store_image_row(dp->ps, pp, iDisplay, y)[where-1]);
png_error(pp, msg);
}
@@ -4647,6 +4865,7 @@ standard_end(png_structp ppIn, png_infop pi)
/* Validate the image - progressive reading only produces one variant for
* interlaced images.
*/
standard_text_validate(dp, pp, pi);
standard_image_validate(dp, pp, 0, -1);
}
@@ -4716,7 +4935,10 @@ standard_test(png_store* PNG_CONST psIn, png_uint_32 PNG_CONST id,
* image is correct.
*/
if (!d.speed)
{
standard_text_validate(&d, pp, pi);
standard_image_validate(&d, pp, 0, 1);
}
else
d.ps->validated = 1;
}
@@ -5794,6 +6016,7 @@ image_transform_default_ini(PNG_CONST image_transform *this,
this->next->ini(this->next, that);
}
#ifdef PNG_READ_BACKGROUND_SUPPORTED
static int
image_transform_default_add(image_transform *this,
PNG_CONST image_transform **that, png_byte colour_type, png_byte bit_depth)
@@ -5806,6 +6029,7 @@ image_transform_default_add(image_transform *this,
return 1;
}
#endif
#ifdef PNG_READ_EXPAND_SUPPORTED
/* png_set_palette_to_rgb */
@@ -7121,7 +7345,7 @@ perform_transform_test(png_modifier *pm)
{
png_byte colour_type = 0;
png_byte bit_depth = 0;
int palette_number = 0;
unsigned int palette_number = 0;
while (next_format(&colour_type, &bit_depth, &palette_number))
{
@@ -7484,6 +7708,7 @@ gamma_component_compose(int do_background, double input_sample, double alpha,
{
switch (do_background)
{
#ifdef PNG_READ_BACKGROUND_SUPPORTED
case PNG_BACKGROUND_GAMMA_SCREEN:
case PNG_BACKGROUND_GAMMA_FILE:
case PNG_BACKGROUND_GAMMA_UNIQUE:
@@ -7501,6 +7726,7 @@ gamma_component_compose(int do_background, double input_sample, double alpha,
input_sample = background;
}
break;
#endif
#ifdef PNG_READ_ALPHA_MODE_SUPPORTED
case ALPHA_MODE_OFFSET + PNG_ALPHA_STANDARD:
@@ -7533,6 +7759,9 @@ gamma_component_compose(int do_background, double input_sample, double alpha,
/* Standard cases where no compositing is done (so the component
* value is already correct.)
*/
UNUSED(alpha)
UNUSED(background)
UNUSED(compose)
break;
}
@@ -7932,11 +8161,13 @@ gamma_component_validate(PNG_CONST char *name, PNG_CONST validate_info *vi,
*/
switch (do_background)
{
case PNG_BACKGROUND_GAMMA_SCREEN:
case PNG_BACKGROUND_GAMMA_FILE:
case PNG_BACKGROUND_GAMMA_UNIQUE:
use_background = (alpha >= 0 && alpha < 1);
/*FALL THROUGH*/
# ifdef PNG_READ_BACKGROUND_SUPPORTED
case PNG_BACKGROUND_GAMMA_SCREEN:
case PNG_BACKGROUND_GAMMA_FILE:
case PNG_BACKGROUND_GAMMA_UNIQUE:
use_background = (alpha >= 0 && alpha < 1);
/*FALL THROUGH*/
# endif
# ifdef PNG_READ_ALPHA_MODE_SUPPORTED
case ALPHA_MODE_OFFSET + PNG_ALPHA_STANDARD:
case ALPHA_MODE_OFFSET + PNG_ALPHA_BROKEN:
@@ -8277,7 +8508,8 @@ gamma_image_validate(gamma_display *dp, png_const_structp pp,
char msg[64];
/* No transform is expected on the threshold tests. */
sprintf(msg, "gamma: below threshold row %d changed", y);
sprintf(msg, "gamma: below threshold row %lu changed",
(unsigned long)y);
png_error(pp, msg);
}
@@ -8482,7 +8714,7 @@ perform_gamma_threshold_tests(png_modifier *pm)
{
png_byte colour_type = 0;
png_byte bit_depth = 0;
int palette_number = 0;
unsigned int palette_number = 0;
/* Don't test more than one instance of each palette - it's pointless, in
* fact this test is somewhat excessive since libpng doesn't make this
@@ -8547,7 +8779,7 @@ static void perform_gamma_transform_tests(png_modifier *pm)
{
png_byte colour_type = 0;
png_byte bit_depth = 0;
int palette_number = 0;
unsigned int palette_number = 0;
while (next_format(&colour_type, &bit_depth, &palette_number))
{
@@ -8576,11 +8808,8 @@ static void perform_gamma_sbit_tests(png_modifier *pm)
*/
for (sbit=pm->sbitlow; sbit<(1<<READ_BDHI); ++sbit)
{
png_byte colour_type, bit_depth;
int npalette;
colour_type = bit_depth = 0;
npalette = 0;
png_byte colour_type = 0, bit_depth = 0;
unsigned int npalette = 0;
while (next_format(&colour_type, &bit_depth, &npalette))
if ((colour_type & PNG_COLOR_MASK_ALPHA) == 0 &&
@@ -8666,8 +8895,8 @@ static void perform_gamma_scale16_tests(png_modifier *pm)
}
#endif /* 16 to 8 bit conversion */
#if defined PNG_READ_BACKGROUND_SUPPORTED ||\
defined PNG_READ_ALPHA_MODE_SUPPORTED
#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
defined(PNG_READ_ALPHA_MODE_SUPPORTED)
static void gamma_composition_test(png_modifier *pm,
PNG_CONST png_byte colour_type, PNG_CONST png_byte bit_depth,
PNG_CONST int palette_number,
@@ -8796,7 +9025,7 @@ perform_gamma_composition_tests(png_modifier *pm, int do_background,
{
png_byte colour_type = 0;
png_byte bit_depth = 0;
int palette_number = 0;
unsigned int palette_number = 0;
/* Skip the non-alpha cases - there is no setting of a transparency colour at
* present.
@@ -8857,7 +9086,9 @@ perform_gamma_test(png_modifier *pm, int summary)
/* Save certain values for the temporary overrides below. */
unsigned int calculations_use_input_precision =
pm->calculations_use_input_precision;
double maxout8 = pm->maxout8;
# ifdef PNG_READ_BACKGROUND_SUPPORTED
double maxout8 = pm->maxout8;
# endif
/* First some arbitrary no-transform tests: */
if (!pm->this.speed && pm->test_gamma_threshold)
@@ -8976,6 +9207,7 @@ perform_gamma_test(png_modifier *pm, int summary)
#endif
}
#endif /* PNG_READ_GAMMA_SUPPORTED */
#endif /* PNG_READ_SUPPORTED */
/* INTERLACE MACRO VALIDATION */
/* This is copied verbatim from the specification, it is simply the pass
@@ -9425,7 +9657,7 @@ static void signal_handler(int signum)
}
/* main program */
int main(int argc, PNG_CONST char **argv)
int main(int argc, char **argv)
{
volatile int summary = 1; /* Print the error summary at the end */
volatile int memstats = 0; /* Print memory statistics at the end */
@@ -9784,7 +10016,9 @@ int main(int argc, PNG_CONST char **argv)
{
perform_interlace_macro_validation();
perform_formatting_test(&pm.this);
perform_standard_test(&pm);
# ifdef PNG_READ_SUPPORTED
perform_standard_test(&pm);
# endif
perform_error_test(&pm);
}
@@ -9792,7 +10026,9 @@ int main(int argc, PNG_CONST char **argv)
if (pm.test_size)
{
make_size_images(&pm.this);
perform_size_test(&pm);
# ifdef PNG_READ_SUPPORTED
perform_size_test(&pm);
# endif
}
#ifdef PNG_READ_TRANSFORMS_SUPPORTED
@@ -9885,7 +10121,20 @@ int main(int argc, PNG_CONST char **argv)
exit(1);
}
}
else
{
fprintf(stderr, "%s: open failed\n", touch);
exit(1);
}
}
return 0;
}
#else /* write not supported */
int main(void)
{
fprintf(stderr, "pngvalid: no write support in libpng, all tests skipped\n");
return 0;
}
#endif

104
contrib/libtests/readpng.c Normal file
View File

@@ -0,0 +1,104 @@
/* readpng.c
*
* Copyright (c) 2013 John Cunningham Bowler
*
* Last changed in libpng 1.6.0 [(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 <string.h>
#if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
# include <config.h>
#endif
/* Define the following to use this test against your installed libpng, rather
* than the one being built here:
*/
#ifdef PNG_FREESTANDING_TESTS
# include <png.h>
#else
# include "../../png.h"
#endif
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_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_ALWAYS, NULL, 0);
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;
}
int
main(void)
{
/* Exit code 0 on success. */
return !read_png(stdin);
}

999
contrib/libtests/tarith.c Normal file
View File

@@ -0,0 +1,999 @@
/* tarith.c
*
* Copyright (c) 2011-2013 John Cunningham Bowler
*
* Last changed in libpng 1.6.0 [February 14, 2013]
*
* This code is released under the libpng license.
* For conditions of distribution and use, see the disclaimer
* and license in png.h
*
* Test internal arithmetic functions of libpng.
*
* This code must be linked against a math library (-lm), but does not require
* libpng or zlib to work. Because it includes the complete source of 'png.c'
* it tests the code with whatever compiler options are used to build it.
* Changing these options can substantially change the errors in the
* calculations that the compiler chooses!
*/
#define _POSIX_SOURCE 1
#define _ISOC99_SOURCE 1
/* Obtain a copy of the code to be tested (plus other things), disabling
* stuff that is not required.
*/
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#include "../../pngpriv.h"
#define png_error png_warning
void png_warning(png_const_structrp png_ptr, png_const_charp msg)
{
fprintf(stderr, "validation: %s\n", msg);
}
#define png_fixed_error png_fixed_warning
void png_fixed_warning(png_const_structrp png_ptr, png_const_charp msg)
{
fprintf(stderr, "overflow in: %s\n", msg);
}
#define png_set_error_fn(pp, ep, efp, wfp) ((void)0)
#define png_malloc(pp, s) malloc(s)
#define png_malloc_warn(pp, s) malloc(s)
#define png_malloc_base(pp, s) malloc(s)
#define png_calloc(pp, s) calloc(1, (s))
#define png_free(pp, s) free(s)
#define png_safecat(b, sb, pos, str) (pos)
#define png_format_number(start, end, format, number) (start)
#define crc32(crc, pp, s) (crc)
#define inflateReset(zs) Z_OK
#define png_create_struct(type) (0)
#define png_destroy_struct(pp) ((void)0)
#define png_create_struct_2(type, m, mm) (0)
#define png_destroy_struct_2(pp, f, mm) ((void)0)
#undef PNG_SIMPLIFIED_READ_SUPPORTED
#undef PNG_SIMPLIFIED_WRITE_SUPPORTED
#undef PNG_USER_MEM_SUPPORTED
#include "../../png.c"
/* Validate ASCII to fp routines. */
static int verbose = 0;
int validation_ascii_to_fp(int count, int argc, char **argv)
{
int showall = 0;
double max_error=2; /* As a percentage error-in-last-digit/.5 */
double max_error_abs=17; /* Used when precision is DBL_DIG */
double max = 0;
double max_abs = 0;
double test = 0; /* Important to test this. */
int precision = 5;
int nonfinite = 0;
int finite = 0;
int ok = 0;
int failcount = 0;
int minorarith = 0;
while (--argc > 0)
if (strcmp(*++argv, "-a") == 0)
showall = 1;
else if (strcmp(*argv, "-e") == 0 && argc > 0)
{
--argc;
max_error = atof(*++argv);
}
else if (strcmp(*argv, "-E") == 0 && argc > 0)
{
--argc;
max_error_abs = atof(*++argv);
}
else
{
fprintf(stderr, "unknown argument %s\n", *argv);
return 1;
}
do
{
png_size_t index;
int state, failed = 0;
char buffer[64];
if (isfinite(test))
++finite;
else
++nonfinite;
if (verbose)
fprintf(stderr, "%.*g %d\n", DBL_DIG, test, precision);
/* Check for overflow in the buffer by setting a marker. */
memset(buffer, 71, sizeof buffer);
png_ascii_from_fp(0, buffer, precision+10, test, precision);
/* Allow for a three digit exponent, this stuff will fail if
* the exponent is bigger than this!
*/
if (buffer[precision+7] != 71)
{
fprintf(stderr, "%g[%d] -> '%s'[%lu] buffer overflow\n", test,
precision, buffer, (unsigned long)strlen(buffer));
failed = 1;
}
/* Following are used for the number parser below and must be
* initialized to zero.
*/
state = 0;
index = 0;
if (!isfinite(test))
{
/* Expect 'inf' */
if (test >= 0 && strcmp(buffer, "inf") ||
test < 0 && strcmp(buffer, "-inf"))
{
fprintf(stderr, "%g[%d] -> '%s' but expected 'inf'\n", test,
precision, buffer);
failed = 1;
}
}
else if (!png_check_fp_number(buffer, precision+10, &state, &index) ||
buffer[index] != 0)
{
fprintf(stderr, "%g[%d] -> '%s' but has bad format ('%c')\n", test,
precision, buffer, buffer[index]);
failed = 1;
}
else if (PNG_FP_IS_NEGATIVE(state) && !(test < 0))
{
fprintf(stderr, "%g[%d] -> '%s' but negative value not so reported\n",
test, precision, buffer);
failed = 1;
assert(!PNG_FP_IS_ZERO(state));
assert(!PNG_FP_IS_POSITIVE(state));
}
else if (PNG_FP_IS_ZERO(state) && !(test == 0))
{
fprintf(stderr, "%g[%d] -> '%s' but zero value not so reported\n",
test, precision, buffer);
failed = 1;
assert(!PNG_FP_IS_NEGATIVE(state));
assert(!PNG_FP_IS_POSITIVE(state));
}
else if (PNG_FP_IS_POSITIVE(state) && !(test > 0))
{
fprintf(stderr, "%g[%d] -> '%s' but postive value not so reported\n",
test, precision, buffer);
failed = 1;
assert(!PNG_FP_IS_NEGATIVE(state));
assert(!PNG_FP_IS_ZERO(state));
}
else
{
/* Check the result against the original. */
double out = atof(buffer);
double change = fabs((out - test)/test);
double allow = .5/pow(10,
(precision >= DBL_DIG) ? DBL_DIG-1 : precision-1);
/* NOTE: if you hit this error case are you compiling with gcc
* and -O0? Try -O2 - the errors can accumulate if the FP
* code above is not optimized and may drift outside the .5 in
* DBL_DIG allowed. In any case a small number of errors may
* occur (very small ones - 1 or 2%) because of rounding in the
* calculations, either in the conversion API or in atof.
*/
if (change >= allow && (isfinite(out) ||
fabs(test/DBL_MAX) <= 1-allow))
{
double percent = (precision >= DBL_DIG) ? max_error_abs : max_error;
double allowp = (change-allow)*100/allow;
if (precision >= DBL_DIG)
{
if (max_abs < allowp) max_abs = allowp;
}
else
{
if (max < allowp) max = allowp;
}
if (showall || allowp >= percent)
{
fprintf(stderr,
"%.*g[%d] -> '%s' -> %.*g number changed (%g > %g (%d%%))\n",
DBL_DIG, test, precision, buffer, DBL_DIG, out, change, allow,
(int)round(allowp));
failed = 1;
}
else
++minorarith;
}
}
if (failed)
++failcount;
else
++ok;
skip:
/* Generate a new number and precision. */
precision = rand();
if (precision & 1) test = -test;
precision >>= 1;
/* Generate random numbers. */
if (test == 0 || !isfinite(test))
test = precision+1;
else
{
/* Derive the exponent from the previous rand() value. */
int exponent = precision % (DBL_MAX_EXP - DBL_MIN_EXP) + DBL_MIN_EXP;
int tmp;
test = frexp(test * rand(), &tmp);
test = ldexp(test, exponent);
precision >>= 8; /* arbitrary */
}
/* This limits the precision to 32 digits, enough for standard
* IEEE implementations which have at most 15 digits.
*/
precision = (precision & 0x1f) + 1;
}
while (--count);
printf("Tested %d finite values, %d non-finite, %d OK (%d failed) %d minor "
"arithmetic errors\n", finite, nonfinite, ok, failcount, minorarith);
printf(" Error with >=%d digit precision %.2f%%\n", DBL_DIG, max_abs);
printf(" Error with < %d digit precision %.2f%%\n", DBL_DIG, max);
return 0;
}
/* Observe that valid FP numbers have the forms listed in the PNG extensions
* specification:
*
* [+,-]{integer,integer.fraction,.fraction}[{e,E}[+,-]integer]
*
* Test each of these in turn, including invalid cases.
*/
typedef enum checkfp_state
{
start, fraction, exponent, states
} checkfp_state;
/* The characters (other than digits) that characterize the states: */
static const char none[] = "";
static const char hexdigits[16] = "0123456789ABCDEF";
static const struct
{
const char *start; /* Characters valid at the start */
const char *end; /* Valid characters that end the state */
const char *tests; /* Characters to test after 2 digits seen */
}
state_characters[states] =
{
/* start: */ { "+-.", ".eE", "+-.e*0369" },
/* fraction: */ { none, "eE", "+-.E#0147" },
/* exponent: */ { "+-", none, "+-.eE^0258" }
};
typedef struct
{
char number[1024]; /* Buffer for number being tested */
int limit; /* Command line limit */
int verbose; /* Shadows global variable */
int ctimes; /* Number of numbers tested */
int cmillions; /* Count of millions of numbers */
int cinvalid; /* Invalid strings checked */
int cnoaccept; /* Characters not accepted */
}
checkfp_command;
typedef struct
{
int cnumber; /* Index into number string */
checkfp_state check_state; /* Current number state */
int at_start; /* At start (first character) of state */
int cdigits_in_state; /* Digits seen in that state */
int limit; /* Limit on same for checking all chars */
int state; /* Current parser state */
int is_negative; /* Number is negative */
int is_zero; /* Number is (still) zero */
int number_was_valid; /* Previous character validity */
}
checkfp_control;
static int check_all_characters(checkfp_command *co, checkfp_control c);
static int check_some_characters(checkfp_command *co, checkfp_control c,
const char *tests);
static int check_one_character(checkfp_command *co, checkfp_control c, int ch)
{
/* Test this character (ch) to ensure the parser does the correct thing.
*/
png_size_t index = 0;
const char test = (char)ch;
const int number_is_valid = png_check_fp_number(&test, 1, &c.state, &index);
const int character_accepted = (index == 1);
if (c.check_state != exponent && isdigit(ch) && ch != '0')
c.is_zero = 0;
if (c.check_state == start && c.at_start && ch == '-')
c.is_negative = 1;
if (isprint(ch))
co->number[c.cnumber++] = (char)ch;
else
{
co->number[c.cnumber++] = '<';
co->number[c.cnumber++] = hexdigits[(ch >> 4) & 0xf];
co->number[c.cnumber++] = hexdigits[ch & 0xf];
co->number[c.cnumber++] = '>';
}
co->number[c.cnumber] = 0;
if (co->verbose > 1)
fprintf(stderr, "%s\n", co->number);
if (++(co->ctimes) == 1000000)
{
if (co->verbose == 1)
fputc('.', stderr);
co->ctimes = 0;
++(co->cmillions);
}
if (!number_is_valid)
++(co->cinvalid);
if (!character_accepted)
++(co->cnoaccept);
/* This should never fail (it's a serious bug if it does): */
if (index != 0 && index != 1)
{
fprintf(stderr, "%s: read beyond end of string (%lu)\n", co->number,
(unsigned long)index);
return 0;
}
/* Validate the new state, note that the PNG_FP_IS_ macros all return
* false unless the number is valid.
*/
if (PNG_FP_IS_NEGATIVE(c.state) !=
(number_is_valid && !c.is_zero && c.is_negative))
{
fprintf(stderr, "%s: negative when it is not\n", co->number);
return 0;
}
if (PNG_FP_IS_ZERO(c.state) != (number_is_valid && c.is_zero))
{
fprintf(stderr, "%s: zero when it is not\n", co->number);
return 0;
}
if (PNG_FP_IS_POSITIVE(c.state) !=
(number_is_valid && !c.is_zero && !c.is_negative))
{
fprintf(stderr, "%s: positive when it is not\n", co->number);
return 0;
}
/* Testing a digit */
if (isdigit(ch))
{
if (!character_accepted)
{
fprintf(stderr, "%s: digit '%c' not accepted\n", co->number, ch);
return 0;
}
if (!number_is_valid)
{
fprintf(stderr, "%s: saw a digit (%c) but number not valid\n",
co->number, ch);
return 0;
}
++c.cdigits_in_state;
c.at_start = 0;
c.number_was_valid = 1;
/* Continue testing characters in this state. Either test all of
* them or, if we have already seen one digit in this state, just test a
* limited set.
*/
if (c.cdigits_in_state < 1)
return check_all_characters(co, c);
else
return check_some_characters(co, c,
state_characters[c.check_state].tests);
}
/* A non-digit; is it allowed here? */
else if (((ch == '+' || ch == '-') && c.check_state != fraction &&
c.at_start) ||
(ch == '.' && c.check_state == start) ||
((ch == 'e' || ch == 'E') && c.number_was_valid &&
c.check_state != exponent))
{
if (!character_accepted)
{
fprintf(stderr, "%s: character '%c' not accepted\n", co->number, ch);
return 0;
}
/* The number remains valid after start of fraction but nowhere else. */
if (number_is_valid && (c.check_state != start || ch != '.'))
{
fprintf(stderr, "%s: saw a non-digit (%c) but number valid\n",
co->number, ch);
return 0;
}
c.number_was_valid = number_is_valid;
/* Check for a state change. When changing to 'fraction' if the number
* is valid at this point set the at_start to false to allow an exponent
* 'e' to come next.
*/
if (c.check_state == start && ch == '.')
{
c.check_state = fraction;
c.at_start = !number_is_valid;
c.cdigits_in_state = 0;
c.limit = co->limit;
return check_all_characters(co, c);
}
else if (c.check_state < exponent && (ch == 'e' || ch == 'E'))
{
c.check_state = exponent;
c.at_start = 1;
c.cdigits_in_state = 0;
c.limit = co->limit;
return check_all_characters(co, c);
}
/* Else it was a sign, and the state doesn't change. */
else
{
if (ch != '-' && ch != '+')
{
fprintf(stderr, "checkfp: internal error (1)\n");
return 0;
}
c.at_start = 0;
return check_all_characters(co, c);
}
}
/* Testing an invalid character */
else
{
if (character_accepted)
{
fprintf(stderr, "%s: character '%c' [0x%.2x] accepted\n", co->number,
ch, ch);
return 0;
}
if (number_is_valid != c.number_was_valid)
{
fprintf(stderr,
"%s: character '%c' [0x%.2x] changed number validity\n", co->number,
ch, ch);
return 0;
}
/* Do nothing - the parser has stuck; return success and keep going with
* the next character.
*/
}
/* Successful return (the caller will try the next character.) */
return 1;
}
static int check_all_characters(checkfp_command *co, checkfp_control c)
{
int ch;
if (c.cnumber+4 < sizeof co->number) for (ch=0; ch<256; ++ch)
{
if (!check_one_character(co, c, ch))
return 0;
}
return 1;
}
static int check_some_characters(checkfp_command *co, checkfp_control c,
const char *tests)
{
int i;
--(c.limit);
if (c.cnumber+4 < sizeof co->number && c.limit >= 0)
{
if (c.limit > 0) for (i=0; tests[i]; ++i)
{
if (!check_one_character(co, c, tests[i]))
return 0;
}
/* At the end check all the characters. */
else
return check_all_characters(co, c);
}
return 1;
}
int validation_checkfp(int count, int argc, char **argv)
{
int result;
checkfp_command command;
checkfp_control control;
command.number[0] = 0;
command.limit = 3;
command.verbose = verbose;
command.ctimes = 0;
command.cmillions = 0;
command.cinvalid = 0;
command.cnoaccept = 0;
while (--argc > 0)
{
++argv;
if (argc > 1 && strcmp(*argv, "-l") == 0)
{
--argc;
command.limit = atoi(*++argv);
}
else
{
fprintf(stderr, "unknown argument %s\n", *argv);
return 1;
}
}
control.cnumber = 0;
control.check_state = start;
control.at_start = 1;
control.cdigits_in_state = 0;
control.limit = command.limit;
control.state = 0;
control.is_negative = 0;
control.is_zero = 1;
control.number_was_valid = 0;
result = check_all_characters(&command, control);
printf("checkfp: %s: checked %d,%.3d,%.3d,%.3d strings (%d invalid)\n",
result ? "pass" : "FAIL", command.cmillions / 1000,
command.cmillions % 1000, command.ctimes / 1000, command.ctimes % 1000,
command.cinvalid);
return result;
}
int validation_muldiv(int count, int argc, char **argv)
{
int tested = 0;
int overflow = 0;
int error = 0;
int error64 = 0;
int passed = 0;
int randbits = 0;
png_uint_32 randbuffer;
png_fixed_point a;
png_int_32 times, div;
while (--argc > 0)
{
fprintf(stderr, "unknown argument %s\n", *++argv);
return 1;
}
/* Find out about the random number generator. */
randbuffer = RAND_MAX;
while (randbuffer != 0) ++randbits, randbuffer >>= 1;
printf("Using random number generator that makes %d bits\n", randbits);
for (div=0; div<32; div += randbits)
randbuffer = (randbuffer << randbits) ^ rand();
a = 0;
times = div = 0;
do
{
png_fixed_point result;
/* NOTE: your mileage may vary, a type is required below that can
* hold 64 bits or more, if floating point is used a 64 bit or
* better mantissa is required.
*/
long long int fp, fpround;
unsigned long hi, lo;
int ok;
/* Check the values, png_64bit_product can only handle positive
* numbers, so correct for that here.
*/
{
long u1, u2;
int n = 0;
if (a < 0) u1 = -a, n = 1; else u1 = a;
if (times < 0) u2 = -times, n = !n; else u2 = times;
png_64bit_product(u1, u2, &hi, &lo);
if (n)
{
/* -x = ~x+1 */
lo = ((~lo) + 1) & 0xffffffff;
hi = ~hi;
if (lo == 0) ++hi;
}
}
fp = a;
fp *= times;
if ((fp & 0xffffffff) != lo || ((fp >> 32) & 0xffffffff) != hi)
{
fprintf(stderr, "png_64bit_product %d * %d -> %lx|%.8lx not %llx\n",
a, times, hi, lo, fp);
++error64;
}
if (div != 0)
{
/* Round - this is C round to zero. */
if ((fp < 0) != (div < 0))
fp -= div/2;
else
fp += div/2;
fp /= div;
fpround = fp;
/* Assume 2's complement here: */
ok = fpround <= PNG_UINT_31_MAX &&
fpround >= -1-(long long int)PNG_UINT_31_MAX;
if (!ok) ++overflow;
}
else
ok = 0, ++overflow, fpround = fp/*misleading*/;
if (verbose)
fprintf(stderr, "TEST %d * %d / %d -> %lld (%s)\n", a, times, div,
fp, ok ? "ok" : "overflow");
++tested;
if (png_muldiv(&result, a, times, div) != ok)
{
++error;
if (ok)
fprintf(stderr, "%d * %d / %d -> overflow (expected %lld)\n", a,
times, div, fp);
else
fprintf(stderr, "%d * %d / %d -> %d (expected overflow %lld)\n", a,
times, div, result, fp);
}
else if (ok && result != fpround)
{
++error;
fprintf(stderr, "%d * %d / %d -> %d not %lld\n", a, times, div, result,
fp);
}
else
++passed;
/* Generate three new values, this uses rand() and rand() only returns
* up to RAND_MAX.
*/
/* CRUDE */
a += times;
times += div;
div = randbuffer;
randbuffer = (randbuffer << randbits) ^ rand();
}
while (--count > 0);
printf("%d tests including %d overflows, %d passed, %d failed (%d 64 bit "
"errors)\n", tested, overflow, passed, error, error64);
return 0;
}
/* When FP is on this just becomes a speed test - compile without FP to get real
* validation.
*/
#ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
#define LN2 .000010576586617430806112933839 /* log(2)/65536 */
#define L2INV 94548.46219969910586572651 /* 65536/log(2) */
/* For speed testing, need the internal functions too: */
static png_uint_32 png_log8bit(unsigned x)
{
if (x > 0)
return (png_uint_32)floor(.5-log(x/255.)*L2INV);
return 0xffffffff;
}
static png_uint_32 png_log16bit(png_uint_32 x)
{
if (x > 0)
return (png_uint_32)floor(.5-log(x/65535.)*L2INV);
return 0xffffffff;
}
static png_uint_32 png_exp(png_uint_32 x)
{
return (png_uint_32)floor(.5 + exp(x * -LN2) * 0xffffffffU);
}
static png_byte png_exp8bit(png_uint_32 log)
{
return (png_byte)floor(.5 + exp(log * -LN2) * 255);
}
static png_uint_16 png_exp16bit(png_uint_32 log)
{
return (png_uint_16)floor(.5 + exp(log * -LN2) * 65535);
}
#endif /* FLOATING_ARITHMETIC */
int validation_gamma(int argc, char **argv)
{
double gamma[9] = { 2.2, 1.8, 1.52, 1.45, 1., 1/1.45, 1/1.52, 1/1.8, 1/2.2 };
double maxerr;
int i, silent=0, onlygamma=0;
/* Silence the output with -s, just test the gamma functions with -g: */
while (--argc > 0)
if (strcmp(*++argv, "-s") == 0)
silent = 1;
else if (strcmp(*argv, "-g") == 0)
onlygamma = 1;
else
{
fprintf(stderr, "unknown argument %s\n", *argv);
return 1;
}
if (!onlygamma)
{
/* First validate the log functions: */
maxerr = 0;
for (i=0; i<256; ++i)
{
double correct = -log(i/255.)/log(2.)*65536;
double error = png_log8bit(i) - correct;
if (i != 0 && fabs(error) > maxerr)
maxerr = fabs(error);
if (i == 0 && png_log8bit(i) != 0xffffffff ||
i != 0 && png_log8bit(i) != floor(correct+.5))
{
fprintf(stderr, "8 bit log error: %d: got %u, expected %f\n",
i, png_log8bit(i), correct);
}
}
if (!silent)
printf("maximum 8 bit log error = %f\n", maxerr);
maxerr = 0;
for (i=0; i<65536; ++i)
{
double correct = -log(i/65535.)/log(2.)*65536;
double error = png_log16bit(i) - correct;
if (i != 0 && fabs(error) > maxerr)
maxerr = fabs(error);
if (i == 0 && png_log16bit(i) != 0xffffffff ||
i != 0 && png_log16bit(i) != floor(correct+.5))
{
if (error > .68) /* By experiment error is less than .68 */
{
fprintf(stderr, "16 bit log error: %d: got %u, expected %f"
" error: %f\n", i, png_log16bit(i), correct, error);
}
}
}
if (!silent)
printf("maximum 16 bit log error = %f\n", maxerr);
/* Now exponentiations. */
maxerr = 0;
for (i=0; i<=0xfffff; ++i)
{
double correct = exp(-i/65536. * log(2.)) * (65536. * 65536);
double error = png_exp(i) - correct;
if (fabs(error) > maxerr)
maxerr = fabs(error);
if (fabs(error) > 1883) /* By experiment. */
{
fprintf(stderr, "32 bit exp error: %d: got %u, expected %f"
" error: %f\n", i, png_exp(i), correct, error);
}
}
if (!silent)
printf("maximum 32 bit exp error = %f\n", maxerr);
maxerr = 0;
for (i=0; i<=0xfffff; ++i)
{
double correct = exp(-i/65536. * log(2.)) * 255;
double error = png_exp8bit(i) - correct;
if (fabs(error) > maxerr)
maxerr = fabs(error);
if (fabs(error) > .50002) /* By experiment */
{
fprintf(stderr, "8 bit exp error: %d: got %u, expected %f"
" error: %f\n", i, png_exp8bit(i), correct, error);
}
}
if (!silent)
printf("maximum 8 bit exp error = %f\n", maxerr);
maxerr = 0;
for (i=0; i<=0xfffff; ++i)
{
double correct = exp(-i/65536. * log(2.)) * 65535;
double error = png_exp16bit(i) - correct;
if (fabs(error) > maxerr)
maxerr = fabs(error);
if (fabs(error) > .524) /* By experiment */
{
fprintf(stderr, "16 bit exp error: %d: got %u, expected %f"
" error: %f\n", i, png_exp16bit(i), correct, error);
}
}
if (!silent)
printf("maximum 16 bit exp error = %f\n", maxerr);
} /* !onlygamma */
/* Test the overall gamma correction. */
for (i=0; i<9; ++i)
{
unsigned j;
double g = gamma[i];
png_fixed_point gfp = floor(g * PNG_FP_1 + .5);
if (!silent)
printf("Test gamma %f\n", g);
maxerr = 0;
for (j=0; j<256; ++j)
{
double correct = pow(j/255., g) * 255;
png_byte out = png_gamma_8bit_correct(j, gfp);
double error = out - correct;
if (fabs(error) > maxerr)
maxerr = fabs(error);
if (out != floor(correct+.5))
{
fprintf(stderr, "8bit %d ^ %f: got %d expected %f error %f\n",
j, g, out, correct, error);
}
}
if (!silent)
printf("gamma %f: maximum 8 bit error %f\n", g, maxerr);
maxerr = 0;
for (j=0; j<65536; ++j)
{
double correct = pow(j/65535., g) * 65535;
png_uint_16 out = png_gamma_16bit_correct(j, gfp);
double error = out - correct;
if (fabs(error) > maxerr)
maxerr = fabs(error);
if (fabs(error) > 1.62)
{
fprintf(stderr, "16bit %d ^ %f: got %d expected %f error %f\n",
j, g, out, correct, error);
}
}
if (!silent)
printf("gamma %f: maximum 16 bit error %f\n", g, maxerr);
}
return 0;
}
/**************************** VALIDATION TESTS ********************************/
/* Various validation routines are included herein, they require some
* definition for png_warning and png_error, seetings of VALIDATION:
*
* 1: validates the ASCII to floating point conversions
* 2: validates png_muldiv
* 3: accuracy test of fixed point gamma tables
*/
/* The following COUNT (10^8) takes about 1 hour on a 1GHz Pentium IV
* processor.
*/
#define COUNT 1000000000
int main(int argc, char **argv)
{
int count = COUNT;
while (argc > 1)
{
if (argc > 2 && strcmp(argv[1], "-c") == 0)
{
count = atoi(argv[2]);
argc -= 2;
argv += 2;
}
else if (strcmp(argv[1], "-v") == 0)
{
++verbose;
--argc;
++argv;
}
else
break;
}
if (count > 0 && argc > 1)
{
if (strcmp(argv[1], "ascii") == 0)
return validation_ascii_to_fp(count, argc-1, argv+1);
else if (strcmp(argv[1], "checkfp") == 0)
return validation_checkfp(count, argc-1, argv+1);
else if (strcmp(argv[1], "muldiv") == 0)
return validation_muldiv(count, argc-1, argv+1);
else if (strcmp(argv[1], "gamma") == 0)
return validation_gamma(argc-1, argv+1);
}
/* Bad argument: */
fprintf(stderr,
"usage: tarith [-v] [-c count] {ascii,muldiv,gamma} [args]\n");
fprintf(stderr, " arguments: ascii [-a (all results)] [-e error%%]\n");
fprintf(stderr, " checkfp [-l max-number-chars]\n");
fprintf(stderr, " muldiv\n");
fprintf(stderr, " gamma -s (silent) -g (only gamma; no log)\n");
return 1;
}

View File

@@ -1,8 +1,8 @@
/* timepng.c
*
* Copyright (c) 2012 John Cunningham Bowler
* Copyright (c) 2013 John Cunningham Bowler
*
* Last changed in libpng 1.6.0 [(PENDING RELEASE)]
* Last changed in libpng 1.6.0 [February 14, 2013]
*
* This code is released under the libpng license.
* For conditions of distribution and use, see the disclaimer
@@ -22,7 +22,7 @@
#include <time.h>
#if (defined HAVE_CONFIG_H) && !(defined PNG_NO_CONFIG_H)
#if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
# include <config.h>
#endif