Built SDL2_image and _mixer static

This commit is contained in:
2022-09-30 15:49:16 -04:00
parent e2605bf6c1
commit 1dec4347e0
4473 changed files with 1964551 additions and 9 deletions

View File

@ -0,0 +1,65 @@
/* Fake a PNG - just write it out directly.
*
* COPYRIGHT: Written by John Cunningham Bowler, 2014.
* To the extent possible under law, the author has waived all copyright and
* related or neighboring rights to this work. This work is published from:
* United States.
*
*/
#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);
}

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

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,165 @@
/* contrib/libtests/pngstest-errors.h
*
* BUILT USING: libpng version 1.6.19beta03 - September 25, 2015
*
* This code is released under the libpng license.
* For conditions of distribution and use, see the disclaimer
* and license in png.h
*
* THIS IS A MACHINE GENERATED FILE: do not edit it directly!
* Instead run:
*
* pngstest --accumulate
*
* on as many PNG files as possible; at least PNGSuite and
* contrib/libtests/testpngs.
*/
static png_uint_16 gpc_error[16/*in*/][16/*out*/][4/*a*/] =
{
{ /* input: sRGB-gray */
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 372, 0 }, { 0, 0, 372, 0 }, { 0, 0, 372, 0 }, { 0, 0, 372, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
}, { /* input: sRGB-gray+alpha */
{ 0, 19, 0, 0 }, { 0, 0, 0, 0 }, { 0, 20, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 897, 788, 0 }, { 0, 897, 788, 0 }, { 0, 897, 788, 0 }, { 0, 897, 788, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
}, { /* input: sRGB-rgb */
{ 0, 0, 19, 0 }, { 0, 0, 19, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 893, 0 }, { 0, 0, 893, 0 }, { 0, 0, 811, 0 }, { 0, 0, 811, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
}, { /* input: sRGB-rgb+alpha */
{ 0, 16, 17, 0 }, { 0, 17, 17, 0 }, { 0, 19, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 850, 875, 0 }, { 0, 850, 875, 0 }, { 0, 897, 788, 0 }, { 0, 897, 788, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
}, { /* input: linear-gray */
{ 0, 0, 9, 0 }, { 0, 0, 9, 0 }, { 0, 0, 9, 0 }, { 0, 0, 9, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
}, { /* input: linear-gray+alpha */
{ 0, 74, 9, 0 }, { 0, 20, 9, 0 }, { 0, 74, 9, 0 }, { 0, 20, 9, 0 },
{ 0, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 0, 0 }, { 0, 1, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
}, { /* input: linear-rgb */
{ 0, 0, 9, 0 }, { 0, 0, 9, 0 }, { 0, 0, 9, 0 }, { 0, 0, 9, 0 },
{ 0, 0, 4, 0 }, { 0, 0, 4, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
}, { /* input: linear-rgb+alpha */
{ 0, 126, 143, 0 }, { 0, 11, 7, 0 }, { 0, 74, 9, 0 }, { 0, 17, 9, 0 },
{ 0, 4, 4, 0 }, { 0, 5, 4, 0 }, { 0, 0, 0, 0 }, { 0, 1, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
}, { /* input: color-mapped-sRGB-gray */
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
}, { /* input: color-mapped-sRGB-gray+alpha */
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
}, { /* input: color-mapped-sRGB-rgb */
{ 0, 0, 13, 0 }, { 0, 0, 13, 0 }, { 0, 0, 9, 0 }, { 0, 0, 9, 0 },
{ 0, 0, 673, 0 }, { 0, 0, 673, 0 }, { 0, 0, 674, 0 }, { 0, 0, 674, 0 },
{ 0, 0, 1, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 460, 0 }, { 0, 0, 460, 0 }, { 0, 0, 263, 0 }, { 0, 0, 263, 0 }
}, { /* input: color-mapped-sRGB-rgb+alpha */
{ 0, 6, 8, 0 }, { 0, 7, 8, 0 }, { 0, 75, 9, 0 }, { 0, 9, 9, 0 },
{ 0, 585, 427, 0 }, { 0, 585, 427, 0 }, { 0, 717, 514, 0 }, { 0, 717, 514, 0 },
{ 0, 1, 1, 0 }, { 0, 1, 1, 0 }, { 0, 1, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 13323, 460, 0 }, { 0, 427, 460, 0 }, { 0, 16480, 263, 0 }, { 0, 243, 263, 0 }
}, { /* input: color-mapped-linear-gray */
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 282, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
}, { /* input: color-mapped-linear-gray+alpha */
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 253, 282, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
}, { /* input: color-mapped-linear-rgb */
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 265, 0 }, { 0, 0, 0, 0 }
}, { /* input: color-mapped-linear-rgb+alpha */
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 243, 265, 0 }
}
};
static png_uint_16 gpc_error_via_linear[16][4/*out*/][4] =
{
{ /* input: sRGB-gray */
{ 0, 0, 7, 0 }, { 0, 0, 7, 0 }, { 0, 0, 7, 0 }, { 0, 0, 7, 0 }
}, { /* input: sRGB-gray+alpha */
{ 0, 15, 15, 0 }, { 0, 186, 15, 0 }, { 0, 15, 15, 0 }, { 0, 186, 15, 0 }
}, { /* input: sRGB-rgb */
{ 0, 0, 20, 0 }, { 0, 0, 20, 0 }, { 0, 0, 15, 0 }, { 0, 0, 15, 0 }
}, { /* input: sRGB-rgb+alpha */
{ 0, 16, 17, 0 }, { 0, 187, 17, 0 }, { 0, 15, 15, 0 }, { 0, 186, 15, 0 }
}, { /* input: linear-gray */
{ 0, 0, 1, 0 }, { 0, 0, 1, 0 }, { 0, 0, 1, 0 }, { 0, 0, 1, 0 }
}, { /* input: linear-gray+alpha */
{ 0, 1, 1, 0 }, { 0, 1, 1, 0 }, { 0, 1, 1, 0 }, { 0, 1, 1, 0 }
}, { /* input: linear-rgb */
{ 0, 0, 1, 0 }, { 0, 0, 1, 0 }, { 0, 0, 1, 0 }, { 0, 0, 1, 0 }
}, { /* input: linear-rgb+alpha */
{ 0, 1, 1, 0 }, { 0, 9, 1, 0 }, { 0, 1, 1, 0 }, { 0, 1, 1, 0 }
}, { /* input: color-mapped-sRGB-gray */
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
}, { /* input: color-mapped-sRGB-gray+alpha */
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
}, { /* input: color-mapped-sRGB-rgb */
{ 0, 0, 13, 0 }, { 0, 0, 13, 0 }, { 0, 0, 14, 0 }, { 0, 0, 14, 0 }
}, { /* input: color-mapped-sRGB-rgb+alpha */
{ 0, 4, 8, 0 }, { 0, 9, 8, 0 }, { 0, 9, 5, 0 }, { 0, 32, 5, 0 }
}, { /* input: color-mapped-linear-gray */
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
}, { /* input: color-mapped-linear-gray+alpha */
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
}, { /* input: color-mapped-linear-rgb */
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
}, { /* input: color-mapped-linear-rgb+alpha */
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
}
};
static png_uint_16 gpc_error_to_colormap[8/*i*/][8/*o*/][4] =
{
{ /* input: sRGB-gray */
{ 0, 0, 9, 0 }, { 0, 0, 9, 0 }, { 0, 0, 9, 0 }, { 0, 0, 9, 0 },
{ 0, 0, 560, 0 }, { 0, 0, 560, 0 }, { 0, 0, 560, 0 }, { 0, 0, 560, 0 }
}, { /* input: sRGB-gray+alpha */
{ 0, 19, 9, 0 }, { 0, 255, 9, 25 }, { 0, 88, 9, 0 }, { 0, 255, 9, 25 },
{ 0, 1012, 928, 0 }, { 0, 16026, 928, 6425 }, { 0, 1012, 928, 0 }, { 0, 16026, 928, 6425 }
}, { /* input: sRGB-rgb */
{ 0, 0, 19, 0 }, { 0, 0, 19, 0 }, { 0, 0, 25, 0 }, { 0, 0, 25, 0 },
{ 0, 0, 962, 0 }, { 0, 0, 962, 0 }, { 0, 0, 13677, 0 }, { 0, 0, 13677, 0 }
}, { /* input: sRGB-rgb+alpha */
{ 0, 63, 77, 0 }, { 0, 255, 19, 25 }, { 0, 225, 25, 0 }, { 0, 255, 25, 67 },
{ 0, 17534, 18491, 0 }, { 0, 15736, 2824, 6425 }, { 0, 14019, 13677, 0 }, { 0, 50115, 13677, 17219 }
}, { /* input: linear-gray */
{ 0, 0, 73, 0 }, { 0, 0, 73, 0 }, { 0, 0, 73, 0 }, { 0, 0, 73, 0 },
{ 0, 0, 18817, 0 }, { 0, 0, 18817, 0 }, { 0, 0, 18817, 0 }, { 0, 0, 18817, 0 }
}, { /* input: linear-gray+alpha */
{ 0, 74, 74, 0 }, { 0, 255, 74, 25 }, { 0, 99, 74, 0 }, { 0, 255, 74, 25 },
{ 0, 18919, 18907, 0 }, { 0, 24549, 18907, 6553 }, { 0, 18919, 18907, 0 }, { 0, 24549, 18907, 6553 }
}, { /* input: linear-rgb */
{ 0, 0, 73, 0 }, { 0, 0, 73, 0 }, { 0, 0, 98, 0 }, { 0, 0, 98, 0 },
{ 0, 0, 18664, 0 }, { 0, 0, 18664, 0 }, { 0, 0, 24998, 0 }, { 0, 0, 24998, 0 }
}, { /* input: linear-rgb+alpha */
{ 0, 181, 196, 0 }, { 0, 255, 61, 25 }, { 206, 187, 98, 0 }, { 0, 255, 98, 67 },
{ 0, 18141, 18137, 0 }, { 0, 17494, 17504, 6553 }, { 0, 24979, 24992, 0 }, { 0, 49172, 24992, 17347 }
}
};
/* END MACHINE GENERATED */

File diff suppressed because it is too large Load Diff

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,115 @@
/* readpng.c
*
* Copyright (c) 2013 John Cunningham Bowler
*
* Last changed in libpng 1.6.1 [March 28, 2013]
*
* 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);
{
size_t rowbytes = png_get_rowbytes(png_ptr, info_ptr);
/* Failure to initialize these is harmless */
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);
# ifdef PNG_READ_INTERLACING_SUPPORTED
int passes = png_set_interlace_handling(png_ptr);
# else /* !READ_INTERLACING */
int passes = png_get_interlace_type(png_ptr, info_ptr) ==
PNG_INTERLACE_ADAM7 ? PNG_INTERLACE_ADAM7_PASSES : 1;
# endif /* !READ_INTERLACING */
int pass;
png_start_read_image(png_ptr);
for (pass = 0; pass < passes; ++pass)
{
png_uint_32 y = height;
# ifndef PNG_READ_INTERLACING_SUPPORTED
if (passes == PNG_INTERLACE_ADAM7_PASSES)
y = PNG_PASS_ROWS(y, pass);
# endif /* READ_INTERLACING */
/* 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);
}

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
{
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 positive 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.
*/
size_t index = 0;
const char test = (char)ch;
int number_is_valid = png_check_fp_number(&test, 1, &c.state, &index);
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

@ -0,0 +1,608 @@
/* timepng.c
*
* Copyright (c) 2013,2016 John Cunningham Bowler
*
* Last changed in libpng 1.6.22 [May 26, 2016]
*
* 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 or by image (possibly with transforms in the latter
* case). The only output is a time as a floating point number of seconds with
* 9 decimal digits.
*/
#define _POSIX_C_SOURCE 199309L /* for clock_gettime */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <time.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
/* The following is to support direct compilation of this file as C++ */
#ifdef __cplusplus
# define voidcast(type, value) static_cast<type>(value)
#else
# define voidcast(type, value) (value)
#endif /* __cplusplus */
/* 'CLOCK_PROCESS_CPUTIME_ID' is one of the clock timers for clock_gettime. It
* need not be supported even when clock_gettime is available. It returns the
* 'CPU' time the process has consumed. 'CPU' time is assumed to include time
* when the CPU is actually blocked by a pending cache fill but not time
* waiting for page faults. The attempt is to get a measure of the actual time
* the implementation takes to read a PNG ignoring the potentially very large IO
* overhead.
*/
#if defined (CLOCK_PROCESS_CPUTIME_ID) && defined(PNG_STDIO_SUPPORTED) &&\
defined(PNG_EASY_ACCESS_SUPPORTED) &&\
(PNG_LIBPNG_VER >= 10700 ? defined(PNG_READ_PNG_SUPPORTED) :\
defined (PNG_SEQUENTIAL_READ_SUPPORTED) &&\
defined(PNG_INFO_IMAGE_SUPPORTED))
typedef struct
{
FILE *input;
FILE *output;
} io_data;
static PNG_CALLBACK(void, read_and_copy,
(png_structp png_ptr, png_bytep buffer, size_t cb))
{
io_data *io = (io_data*)png_get_io_ptr(png_ptr);
if (fread(buffer, cb, 1, io->input) != 1)
png_error(png_ptr, strerror(errno));
if (fwrite(buffer, cb, 1, io->output) != 1)
{
perror("temporary file");
fprintf(stderr, "temporary file PNG write failed\n");
exit(1);
}
}
static void read_by_row(png_structp png_ptr, png_infop info_ptr,
FILE *write_ptr, FILE *read_ptr)
{
/* These don't get freed on error, this is fine; the program immediately
* exits.
*/
png_bytep row = NULL, display = NULL;
io_data io_copy;
if (write_ptr != NULL)
{
/* Set up for a copy to the temporary file: */
io_copy.input = read_ptr;
io_copy.output = write_ptr;
png_set_read_fn(png_ptr, &io_copy, read_and_copy);
}
png_read_info(png_ptr, info_ptr);
{
size_t rowbytes = png_get_rowbytes(png_ptr, info_ptr);
row = voidcast(png_bytep,malloc(rowbytes));
display = voidcast(png_bytep,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 and is
* somewhat representative of an application that works row-by-row.
*/
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);
/* Free this up: */
free(row);
free(display);
}
static PNG_CALLBACK(void, no_warnings, (png_structp png_ptr,
png_const_charp warning))
{
(void)png_ptr;
(void)warning;
}
static int read_png(FILE *fp, png_int_32 transforms, FILE *write_file)
{
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,0,0,
no_warnings);
png_infop info_ptr = NULL;
if (png_ptr == NULL)
return 0;
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
return 0;
}
# ifdef PNG_BENIGN_ERRORS_SUPPORTED
png_set_benign_errors(png_ptr, 1/*allowed*/);
# endif
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");
if (transforms < 0)
read_by_row(png_ptr, info_ptr, write_file, fp);
else
png_read_png(png_ptr, info_ptr, transforms, NULL/*params*/);
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
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, png_int_32 transforms)
{
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, transforms, NULL/*write*/))
{
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)
{
/* Read the file using libpng; this detects errors and also deals with
* files which contain data beyond the end of the file.
*/
int ok = 0;
fpos_t pos;
if (fgetpos(fp, &pos))
{
/* Fatal error reading the start: */
perror("temporary file");
fprintf(stderr, "temporary file fgetpos error\n");
exit(1);
}
if (read_png(ip, -1/*by row*/, fp/*output*/))
{
if (ferror(ip))
{
perror(name);
fprintf(stderr, "%s: read error\n", name);
}
else
ok = 1; /* read ok */
}
else
fprintf(stderr, "%s: file not added\n", name);
(void)fclose(ip);
/* An error in the output is fatal; exit immediately: */
if (ferror(fp))
{
perror("temporary file");
fprintf(stderr, "temporary file write error\n");
exit(1);
}
if (ok)
return 1;
/* Did not read the file successfully, simply rewind the temporary
* file. This must happen after the ferror check above to avoid clearing
* the error.
*/
if (fsetpos(fp, &pos))
{
perror("temporary file");
fprintf(stderr, "temporary file fsetpos error\n");
exit(1);
}
}
else
{
/* file open error: */
perror(name);
fprintf(stderr, "%s: open failed\n", name);
}
return 0; /* file not added */
}
static void
usage(FILE *fp)
{
if (fp != NULL) fclose(fp);
fprintf(stderr,
"Usage:\n"
" timepng --assemble <assembly> {files}\n"
" Read the files into <assembly>, output the count. Options are ignored.\n"
" timepng --dissemble <assembly> <count> [options]\n"
" Time <count> files from <assembly>, additional files may not be given.\n"
" Otherwise:\n"
" Read the files into a temporary file and time the decode\n"
"Transforms:\n"
" --by-image: read by image with png_read_png\n"
" --<transform>: implies by-image, use PNG_TRANSFORM_<transform>\n"
" Otherwise: read by row using png_read_row (to a single row buffer)\n"
/* ISO C90 string length max 509 */);fprintf(stderr,
"{files}:\n"
" PNG files to copy into the assembly and time. Invalid files are skipped\n"
" with appropriate error messages. If no files are given the list of files\n"
" is read from stdin with each file name terminated by a newline\n"
"Output:\n"
" For --assemble the output is the name of the assembly file followed by the\n"
" count of the files it contains; the arguments for --dissemble. Otherwise\n"
" the output is the total decode time in seconds.\n");
exit(99);
}
int main(int argc, char **argv)
{
int ok = 0;
int err = 0;
int nfiles = 0;
int transforms = -1; /* by row */
const char *assembly = NULL;
FILE *fp;
if (argc > 2 && strcmp(argv[1], "--assemble") == 0)
{
/* Just build the test file, argv[2] is the file name. */
assembly = argv[2];
fp = fopen(assembly, "wb");
if (fp == NULL)
{
perror(assembly);
fprintf(stderr, "timepng --assemble %s: could not open for write\n",
assembly);
usage(NULL);
}
argv += 2;
argc -= 2;
}
else if (argc > 3 && strcmp(argv[1], "--dissemble") == 0)
{
fp = fopen(argv[2], "rb");
if (fp == NULL)
{
perror(argv[2]);
fprintf(stderr, "timepng --dissemble %s: could not open for read\n",
argv[2]);
usage(NULL);
}
nfiles = atoi(argv[3]);
if (nfiles <= 0)
{
fprintf(stderr,
"timepng --dissemble <file> <count>: %s is not a count\n",
argv[3]);
exit(99);
}
#ifdef __COVERITY__
else
{
nfiles &= PNG_UINT_31_MAX;
}
#endif
argv += 3;
argc -= 3;
}
else /* Else use a temporary file */
{
#ifndef __COVERITY__
fp = tmpfile();
#else
/* Experimental. Coverity says tmpfile() is insecure because it
* generates predictable names.
*
* It is possible to satisfy Coverity by using mkstemp(); however,
* any platform supporting mkstemp() undoubtedly has a secure tmpfile()
* implementation as well, and doesn't need the fix. Note that
* the fix won't work on platforms that don't support mkstemp().
*
* https://www.securecoding.cert.org/confluence/display/c/
* FIO21-C.+Do+not+create+temporary+files+in+shared+directories
* says that most historic implementations of tmpfile() provide
* only a limited number of possible temporary file names
* (usually 26) before file names are recycled. That article also
* provides a secure solution that unfortunately depends upon mkstemp().
*/
char tmpfile[] = "timepng-XXXXXX";
int filedes;
umask(0177);
filedes = mkstemp(tmpfile);
if (filedes < 0)
fp = NULL;
else
{
fp = fdopen(filedes,"w+");
/* Hide the filename immediately and ensure that the file does
* not exist after the program ends
*/
(void) unlink(tmpfile);
}
#endif
if (fp == NULL)
{
perror("tmpfile");
fprintf(stderr, "timepng: could not open the temporary file\n");
exit(1); /* not a user error */
}
}
/* Handle the transforms: */
while (argc > 1 && argv[1][0] == '-' && argv[1][1] == '-')
{
const char *opt = *++argv + 2;
--argc;
/* Transforms turn on the by-image processing and maybe set some
* transforms:
*/
if (transforms == -1)
transforms = PNG_TRANSFORM_IDENTITY;
if (strcmp(opt, "by-image") == 0)
{
/* handled above */
}
# define OPT(name) else if (strcmp(opt, #name) == 0)\
transforms |= PNG_TRANSFORM_ ## name
OPT(STRIP_16);
OPT(STRIP_ALPHA);
OPT(PACKING);
OPT(PACKSWAP);
OPT(EXPAND);
OPT(INVERT_MONO);
OPT(SHIFT);
OPT(BGR);
OPT(SWAP_ALPHA);
OPT(SWAP_ENDIAN);
OPT(INVERT_ALPHA);
OPT(STRIP_FILLER);
OPT(STRIP_FILLER_BEFORE);
OPT(STRIP_FILLER_AFTER);
OPT(GRAY_TO_RGB);
OPT(EXPAND_16);
OPT(SCALE_16);
else
{
fprintf(stderr, "timepng %s: unrecognized transform\n", opt);
usage(fp);
}
}
/* Handle the files: */
if (argc > 1 && nfiles > 0)
usage(fp); /* Additional files not valid with --dissemble */
else if (argc > 1)
{
int i;
for (i=1; i<argc; ++i)
{
if (nfiles == INT_MAX)
{
fprintf(stderr, "%s: skipped, too many files\n", argv[i]);
break;
}
else if (add_one_file(fp, argv[i]))
++nfiles;
}
}
else if (nfiles == 0) /* Read from stdin withoout --dissemble */
{
char filename[FILENAME_MAX+1];
while (fgets(filename, FILENAME_MAX+1, stdin))
{
size_t len = strlen(filename);
if (filename[len-1] == '\n')
{
filename[len-1] = 0;
if (nfiles == INT_MAX)
{
fprintf(stderr, "%s: skipped, too many files\n", filename);
break;
}
else if (add_one_file(fp, filename))
++nfiles;
}
else
{
fprintf(stderr, "timepng: file name too long: ...%s\n",
filename+len-32);
err = 1;
break;
}
}
if (ferror(stdin))
{
fprintf(stderr, "timepng: stdin: read error\n");
err = 1;
}
}
/* Perform the test, or produce the --assemble output: */
if (!err)
{
if (nfiles > 0)
{
if (assembly != NULL)
{
if (fflush(fp) && !ferror(fp) && fclose(fp))
{
perror(assembly);
fprintf(stderr, "%s: close failed\n", assembly);
}
else
{
printf("%s %d\n", assembly, nfiles);
fflush(stdout);
ok = !ferror(stdout);
}
}
else
{
ok = perform_one_test(fp, nfiles, transforms);
(void)fclose(fp);
}
}
else
usage(fp);
}
else
(void)fclose(fp);
/* Exit code 0 on success. */
return ok == 0;
}
#else /* !sufficient support */
int main(void) { return 77; }
#endif /* !sufficient support */