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,27 @@
This directory (contrib/tools) contains tools used by the authors of libpng.
Code and data placed in this directory is not required to build libpng,
however the code in this directory has been used to generate data or code in
the body of the libpng source. The source code identifies where this has
been done. Code in this directory may not compile on all operating systems
that libpng supports.
NO COPYRIGHT RIGHTS ARE CLAIMED TO ANY OF THE FILES IN THIS DIRECTORY.
To the extent possible under law, the authors have waived all copyright and
related or neighboring rights to this work. This work is published from:
United States.
The files may be used freely in any way.
The source code and comments in this directory are the original work of the
people named below. No other person or organization has made contributions to
the work in this directory.
ORIGINAL AUTHORS
The following people have contributed to the code in this directory. None
of the people below claim any rights with regard to the contents of this
directory.
John Bowler <jbowler at acm.org>
Glenn Randers-Pehrson <glennrp at users.sourceforge.net>

View File

@ -0,0 +1,102 @@
/* checksum-icc.c
*
* 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 crc32 and adler32 checksums of the given input files, used to
* generate check-codes for use when matching ICC profiles within libpng.
*/
#include <stdio.h>
#include <zlib.h>
static int
read_one_file(FILE *ip, const char *name)
{
uLong length = 0;
uLong a32 = adler32(0, NULL, 0);
uLong c32 = crc32(0, NULL, 0);
Byte header[132];
for (;;)
{
int ch = getc(ip);
Byte b;
if (ch == EOF) break;
b = (Byte)ch;
if (length < sizeof header)
header[length] = b;
++length;
a32 = adler32(a32, &b, 1);
c32 = crc32(c32, &b, 1);
}
if (ferror(ip))
return 0;
/* Success */
printf("PNG_ICC_CHECKSUM(0x%8.8lx, 0x%8.8lx,\n PNG_MD5("
"0x%2.2x%2.2x%2.2x%2.2x, 0x%2.2x%2.2x%2.2x%2.2x, 0x%2.2x%2.2x%2.2x%2.2x,"
" 0x%2.2x%2.2x%2.2x%2.2x), %d,\n"
" \"%4.4d/%2.2d/%2.2d %2.2d:%2.2d:%2.2d\", %lu, \"%s\")\n",
(unsigned long)a32, (unsigned long)c32,
header[84], header[85], header[86], header[87],
header[88], header[89], header[90], header[91],
header[92], header[93], header[94], header[95],
header[96], header[97], header[98], header[99],
# define u16(x) (header[x] * 256 + header[x+1])
# define u32(x) (u16(x) * 65536 + u16(x+2))
u32(64), u16(24), u16(26), u16(28), u16(30), u16(32), u16(34),
(unsigned long)length, name);
return 1;
}
int main(int argc, char **argv)
{
int err = 0;
printf("/* adler32, crc32, MD5[16], intent, date, length, file-name */\n");
if (argc > 1)
{
int i;
for (i=1; i<argc; ++i)
{
FILE *ip = fopen(argv[i], "rb");
if (ip == NULL || !read_one_file(ip, argv[i]))
{
err = 1;
perror(argv[i]);
fprintf(stderr, "%s: read error\n", argv[i]);
printf("/* ERROR: %s */\n", argv[i]);
}
(void)fclose(ip);
}
}
else
{
if (!read_one_file(stdin, "-"))
{
err = 1;
perror("stdin");
fprintf(stderr, "stdin: read error\n");
printf("/* ERROR: stdin */\n");
}
}
return err;
}

View File

@ -0,0 +1,144 @@
#!/bin/sh
# chkfmt
#
# COPYRIGHT: Written by John Cunningham Bowler, 2010.
# 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.
#
# Check the format of the source files in the current directory - checks for a
# line length of 80 characters max and no tab characters.
#
# Optionally arguments are files or directories to check.
#
# -v: output the long lines (makes fixing them easier)
# -e: spawn an editor for each file that needs a change ($EDITOR must be
# defined). When using -e the script MUST be run from an interactive
# command line.
verbose=
edit=
vers=
test "$1" = "-v" && {
shift
verbose=yes
}
test "$1" = "-e" && {
shift
if test -n "$EDITOR"
then
edit=yes
# Copy the standard streams for the editor
exec 3>&0 4>&1 5>&2
else
echo "chkfmt -e: EDITOR must be defined" >&2
exit 1
fi
}
# Function to edit a single file - if the file isn't changed ask the user
# whether or not to continue. This stuff only works if the script is run from
# the command line (otherwise, don't specify -e or you will be sorry).
doed(){
cp "$file" "$file".orig
"$EDITOR" "$file" 0>&3 1>&4 2>&5 3>&- 4>&- 5>&- || exit 1
if cmp -s "$file".orig "$file"
then
rm "$file".orig
echo -n "$file: file not changed, type anything to continue: " >&5
read ans 0>&3
test -n "$ans" || return 1
fi
return 0
}
# In beta versions the version string which appears in files can be a little
# long and cause spuriously overlong lines. To avoid this substitute the version
# string with a 'standard' version a.b.cc before checking for long lines.
if test -r png.h
then
vers="`sed -n -e \
's/^#define PNG_LIBPNG_VER_STRING .\([0-9]\.[0-9]\.[0-9][0-9a-z]*\).$/\1/p' \
png.h`"
echo "chkfmt: checking version $vers"
fi
if test -z "$vers"
then
echo "chkfmt: png.h not found, ignoring version number" >&2
fi
test -n "$1" || set -- .
find "$@" \( -type d \( -name '.git' -o -name '.libs' -o -name 'projects' \) \
-prune \) -o \( -type f \
! -name '*.[oa]' ! -name '*.l[oa]' ! -name '*.png' ! -name '*.out' \
! -name '*.jpg' ! -name '*.patch' ! -name '*.obj' ! -name '*.exe' \
! -name '*.com' ! -name '*.tar.*' ! -name '*.zip' ! -name '*.ico' \
! -name '*.res' ! -name '*.rc' ! -name '*.mms' ! -name '*.rej' \
! -name '*.dsp' ! -name '*.orig' ! -name '*.dfn' ! -name '*.swp' \
! -name '~*' ! -name '*.3' \
! -name 'missing' ! -name 'mkinstalldirs' ! -name 'depcomp' \
! -name 'aclocal.m4' ! -name 'install-sh' ! -name 'Makefile.in' \
! -name 'ltmain.sh' ! -name 'config*' -print \) | {
st=0
while read file
do
case "$file" in
*.mak|*[Mm]akefile.*|*[Mm]akefile)
# Makefiles require tabs, dependency lines can be this long.
check_tabs=
line_length=100;;
*.awk)
# Includes literal tabs
check_tabs=
# The following is arbitrary
line_length=132;;
*contrib/*/*.[ch])
check_tabs=yes
line_length=96;;
*)
check_tabs=yes
line_length=80;;
esac
# Note that vers can only contain 0-9, . and a-z
if test -n "$vers"
then
sed -e "s/$vers/a.b.cc/g" "$file" >"$file".$$
else
cp "$file" "$file".$$
fi
splt="`fold -$line_length "$file".$$ | diff -c "$file".$$ -`"
rm "$file".$$
if test -n "$splt"
then
echo "$file: lines too long"
st=1
if test -n "$EDITOR" -a -n "$edit"
then
doed "$file" || exit 1
elif test -n "$verbose"
then
echo "$splt"
fi
fi
if test -n "$check_tabs"
then
tab="`tr -c -d '\t' <"$file"`"
if test -n "$tab"
then
echo "$file: file contains tab characters"
st=1
if test -n "$EDITOR" -a -n "$edit"
then
doed "$file" || exit 1
elif test -n "$verbose"
then
echo "$splt"
fi
fi
fi
done
exit $st
}

View File

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

View File

@ -0,0 +1,881 @@
/*- genpng
*
* COPYRIGHT: Written by John Cunningham Bowler, 2015.
* Revised by Glenn Randers-Pehrson, 2017, to add buffer-size check.
* To the extent possible under law, the authors have waived all copyright and
* related or neighboring rights to this work. This work is published from:
* United States.
*
* Generate a PNG with an alpha channel, correctly.
*
* This is a test case generator; the resultant PNG files are only of interest
* to those of us who care about whether the edges of circles are green, red,
* or yellow.
*
* The program generates an RGB+Alpha PNG of a given size containing the given
* shapes on a transparent background:
*
* genpng width height { shape }
* shape ::= color width shape x1 y1 x2 y2
*
* 'color' is:
*
* black white red green yellow blue brown purple pink orange gray cyan
*
* The point is to have colors that are linguistically meaningful plus that old
* bugbear of the department store dress murders, Cyan, the only color we argue
* about.
*
* 'shape' is:
*
* circle: an ellipse
* square: a rectangle
* line: a straight line
*
* Each shape is followed by four numbers, these are two points in the output
* coordinate space (as real numbers) which describe the circle, square, or
* line. The shape is filled if it is preceded by 'filled' (not valid for
* 'line') or is drawn with a line, in which case the width of the line must
* precede the shape.
*
* The whole set of information can be repeated as many times as desired:
*
* shape ::= color width shape x1 y1 x2 y2
*
* color ::= black|white|red|green|yellow|blue
* color ::= brown|purple|pink|orange|gray|cyan
* width ::= filled
* width ::= <number>
* shape ::= circle|square|line
* x1 ::= <number>
* x2 ::= <number>
* y1 ::= <number>
* y2 ::= <number>
*
* The output PNG is generated by down-sampling a 4x supersampled image using
* a bi-cubic filter. The bi-cubic has a 2 (output) pixel width, so an 8x8
* array of super-sampled points contribute to each output pixel. The value of
* a super-sampled point is found using an unfiltered, aliased, infinite
* precision image: Each shape from the last to the first is checked to see if
* the point is in the drawn area and, if it is, the color of the point is the
* color of the shape and the alpha is 1, if not the previous shape is checked.
*
* This is an aliased algorithm because no filtering is done; a point is either
* inside or outside each shape and 'close' points do not contribute to the
* sample. The down-sampling is relied on to correct the error of not using
* a filter.
*
* The line end-caps are 'flat'; they go through the points. The square line
* joins are mitres; the outside of the lines are continued to the point of
* intersection.
*/
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
/* Normally use <png.h> here to get the installed libpng, but this is done to
* ensure the code picks up the local libpng implementation:
*/
#include "../../png.h"
#if defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
static const struct color
{
const char *name;
double red;
double green;
double blue;
} colors[] =
/* color ::= black|white|red|green|yellow|blue
* color ::= brown|purple|pink|orange|gray|cyan
*/
{
{ "black", 0, 0, 0 },
{ "white", 1, 1, 1 },
{ "red", 1, 0, 0 },
{ "green", 0, 1, 0 },
{ "yellow", 1, 1, 0 },
{ "blue", 0, 0, 1 },
{ "brown", .5, .125, 0 },
{ "purple", 1, 0, 1 },
{ "pink", 1, .5, .5 },
{ "orange", 1, .5, 0 },
{ "gray", 0, .5, .5 },
{ "cyan", 0, 1, 1 }
};
#define color_count ((sizeof colors)/(sizeof colors[0]))
static const struct color *
color_of(const char *arg)
{
int icolor = color_count;
while (--icolor >= 0)
{
if (strcmp(colors[icolor].name, arg) == 0)
return colors+icolor;
}
fprintf(stderr, "genpng: invalid color %s\n", arg);
exit(1);
}
static double
width_of(const char *arg)
{
if (strcmp(arg, "filled") == 0)
return 0;
else
{
char *ep = NULL;
double w = strtod(arg, &ep);
if (ep != NULL && *ep == 0 && w > 0)
return w;
}
fprintf(stderr, "genpng: invalid line width %s\n", arg);
exit(1);
}
static double
coordinate_of(const char *arg)
{
char *ep = NULL;
double w = strtod(arg, &ep);
if (ep != NULL && *ep == 0)
return w;
fprintf(stderr, "genpng: invalid coordinate value %s\n", arg);
exit(1);
}
struct arg; /* forward declaration */
typedef int (*shape_fn_ptr)(const struct arg *arg, double x, double y);
/* A function to determine if (x,y) is inside the shape.
*
* There are two implementations:
*
* inside_fn: returns true if the point is inside
* check_fn: returns;
* -1: the point is outside the shape by more than the filter width (2)
* 0: the point may be inside the shape
* +1: the point is inside the shape by more than the filter width
*/
#define OUTSIDE (-1)
#define INSIDE (1)
struct arg
{
const struct color *color;
shape_fn_ptr inside_fn;
shape_fn_ptr check_fn;
double width; /* line width, 0 for 'filled' */
double x1, y1, x2, y2;
};
/* IMPLEMENTATION NOTE:
*
* We want the contribution of each shape to the sample corresponding to each
* pixel. This could be obtained by super sampling the image to infinite
* dimensions, finding each point within the shape and assigning that a value
* '1' while leaving every point outside the shape with value '0' then
* downsampling to the image size with sinc; computationally very expensive.
*
* Approximations are as follows:
*
* 1) If the pixel coordinate is within the shape assume the sample has the
* shape color and is opaque, else assume there is no contribution from
* the shape.
*
* This is the equivalent of aliased rendering or resampling an image with
* a block filter. The maximum error in the calculated alpha (which will
* always be 0 or 1) is 0.5.
*
* 2) If the shape is within a square of size 1x1 centered on the pixel assume
* that the shape obscures an amount of the pixel equal to its area within
* that square.
*
* This is the equivalent of 'pixel coverage' alpha calculation or resampling
* an image with a bi-linear filter. The maximum error is over 0.2, but the
* results are often acceptable.
*
* This can be approximated by applying (1) to a super-sampled image then
* downsampling with a bi-linear filter. The error in the super-sampled
* image is 0.5 per sample, but the resampling reduces this.
*
* 3) Use a better filter with a super-sampled image; in the limit this is the
* sinc() approach.
*
* 4) Do the geometric calculation; a bivariate definite integral across the
* shape, unfortunately this means evaluating Si(x), the integral of sinc(x),
* which is still a lot of math.
*
* This code uses approach (3) with a bi-cubic filter and 8x super-sampling
* and method (1) for the super-samples. This means that the sample is either
* 0 or 1, depending on whether the sub-pixel is within or outside the shape.
* The bi-cubic weights are also fixed and the 16 required weights are
* pre-computed here (note that the 'scale' setting will need to be changed if
* 'super' is increased).
*
* The code also calculates a sum to the edge of the filter. This is not
* currently used by could be used to optimize the calculation.
*/
#if 0 /* bc code */
scale=10
super=8
define bicubic(x) {
if (x <= 1) return (1.5*x - 2.5)*x*x + 1;
if (x < 2) return (((2.5 - 0.5*x)*x - 4)*x + 2);
return 0;
}
define sum(x) {
auto s;
s = 0;
while (x < 2*super) {
s = s + bicubic(x/super);
x = x + 1;
}
return s;
}
define results(x) {
auto b, s;
b = bicubic(x/super);
s = sum(x);
print " /*", x, "*/ { ", b, ", ", s, " }";
return 1;
}
x=0
while (x<2*super) {
x = x + results(x)
if (x < 2*super) print ","
print "\n"
}
quit
#endif
#define BICUBIC1(x) /* |x| <= 1 */ ((1.5*(x)* - 2.5)*(x)*(x) + 1)
#define BICUBIC2(x) /* 1 < |x| < 2 */ (((2.5 - 0.5*(x))*(x) - 4)*(x) + 2)
#define FILTER_WEIGHT 9 /* Twice the first sum below */
#define FILTER_WIDTH 2 /* Actually half the width; -2..+2 */
#define FILTER_STEPS 8 /* steps per filter unit */
static const double
bicubic[16][2] =
{
/* These numbers are exact; the weight for the filter is 1/9, but this
* would make the numbers inexact, so it is not included here.
*/
/* bicubic sum */
/* 0*/ { 1.0000000000, 4.5000000000 },
/* 1*/ { .9638671875, 3.5000000000 },
/* 2*/ { .8671875000, 2.5361328125 },
/* 3*/ { .7275390625, 1.6689453125 },
/* 4*/ { .5625000000, .9414062500 },
/* 5*/ { .3896484375, .3789062500 },
/* 6*/ { .2265625000, -.0107421875 },
/* 7*/ { .0908203125, -.2373046875 },
/* 8*/ { 0, -.3281250000 },
/* 9*/ { -.0478515625, -.3281250000 },
/*10*/ { -.0703125000, -.2802734375 },
/*11*/ { -.0732421875, -.2099609375 },
/*12*/ { -.0625000000, -.1367187500 },
/*13*/ { -.0439453125, -.0742187500 },
/*14*/ { -.0234375000, -.0302734375 },
/*15*/ { -.0068359375, -.0068359375 }
};
static double
alpha_calc(const struct arg *arg, double x, double y)
{
/* For [x-2..x+2],[y-2,y+2] calculate the weighted bicubic given a function
* which tells us whether a point is inside or outside the shape. First
* check if we need to do this at all:
*/
switch (arg->check_fn(arg, x, y))
{
case OUTSIDE:
return 0; /* all samples outside the shape */
case INSIDE:
return 1; /* all samples inside the shape */
default:
{
int dy;
double alpha = 0;
# define FILTER_D (FILTER_WIDTH*FILTER_STEPS-1)
for (dy=-FILTER_D; dy<=FILTER_D; ++dy)
{
double wy = bicubic[abs(dy)][0];
if (wy != 0)
{
double alphay = 0;
int dx;
for (dx=-FILTER_D; dx<=FILTER_D; ++dx)
{
double wx = bicubic[abs(dx)][0];
if (wx != 0 && arg->inside_fn(arg, x+dx/16, y+dy/16))
alphay += wx;
}
alpha += wy * alphay;
}
}
/* This needs to be weighted for each dimension: */
return alpha / (FILTER_WEIGHT*FILTER_WEIGHT);
}
}
}
/* These are the shape functions. */
/* "square",
* { inside_square_filled, check_square_filled },
* { inside_square, check_square }
*/
static int
square_check(double x, double y, double x1, double y1, double x2, double y2)
/* Is x,y inside the square (x1,y1)..(x2,y2)? */
{
/* Do a modified Cohen-Sutherland on one point, bit patterns that indicate
* 'outside' are:
*
* x<x1 | x<y1 | x<x2 | x<y2
* 0 x 0 x To the right
* 1 x 1 x To the left
* x 0 x 0 Below
* x 1 x 1 Above
*
* So 'inside' is (x<x1) != (x<x2) && (y<y1) != (y<y2);
*/
return ((x<x1) ^ (x<x2)) & ((y<y1) ^ (y<y2));
}
static int
inside_square_filled(const struct arg *arg, double x, double y)
{
return square_check(x, y, arg->x1, arg->y1, arg->x2, arg->y2);
}
static int
square_check_line(const struct arg *arg, double x, double y, double w)
/* Check for a point being inside the boundaries implied by the given arg
* and assuming a width 2*w each side of the boundaries. This returns the
* 'check' INSIDE/OUTSIDE/0 result but note the semantics:
*
* +--------------+
* | | OUTSIDE
* | INSIDE |
* | |
* +--------------+
*
* And '0' means within the line boundaries.
*/
{
double cx = (arg->x1+arg->x2)/2;
double wx = fabs(arg->x1-arg->x2)/2;
double cy = (arg->y1+arg->y2)/2;
double wy = fabs(arg->y1-arg->y2)/2;
if (square_check(x, y, cx-wx-w, cy-wy-w, cx+wx+w, cy+wy+w))
{
/* Inside, but maybe too far; check for the redundant case where
* the lines overlap:
*/
wx -= w;
wy -= w;
if (wx > 0 && wy > 0 && square_check(x, y, cx-wx, cy-wy, cx+wx, cy+wy))
return INSIDE; /* between (inside) the boundary lines. */
return 0; /* inside the lines themselves. */
}
return OUTSIDE; /* outside the boundary lines. */
}
static int
check_square_filled(const struct arg *arg, double x, double y)
{
/* The filter extends +/-FILTER_WIDTH each side of each output point, so
* the check has to expand and contract the square by that amount; '0'
* means close enough to the edge of the square that the bicubic filter has
* to be run, OUTSIDE means alpha==0, INSIDE means alpha==1.
*/
return square_check_line(arg, x, y, FILTER_WIDTH);
}
static int
inside_square(const struct arg *arg, double x, double y)
{
/* Return true if within the drawn lines, else false, no need to distinguish
* INSIDE vs OUTSIDE here:
*/
return square_check_line(arg, x, y, arg->width/2) == 0;
}
static int
check_square(const struct arg *arg, double x, double y)
{
/* So for this function a result of 'INSIDE' means inside the actual lines.
*/
double w = arg->width/2;
if (square_check_line(arg, x, y, w+FILTER_WIDTH) == 0)
{
/* Somewhere close to the boundary lines. If far enough inside one of
* them then we can return INSIDE:
*/
w -= FILTER_WIDTH;
if (w > 0 && square_check_line(arg, x, y, w) == 0)
return INSIDE;
/* Point is somewhere in the filter region: */
return 0;
}
else /* Inside or outside the square by more than w+FILTER_WIDTH. */
return OUTSIDE;
}
/* "circle",
* { inside_circle_filled, check_circle_filled },
* { inside_circle, check_circle }
*
* The functions here are analoguous to the square ones; however, they check
* the corresponding ellipse as opposed to the rectangle.
*/
static int
circle_check(double x, double y, double x1, double y1, double x2, double y2)
{
if (square_check(x, y, x1, y1, x2, y2))
{
/* Inside the square, so maybe inside the circle too: */
const double cx = (x1 + x2)/2;
const double cy = (y1 + y2)/2;
const double dx = x1 - x2;
const double dy = y1 - y2;
x = (x - cx)/dx;
y = (y - cy)/dy;
/* It is outside if the distance from the center is more than half the
* diameter:
*/
return x*x+y*y < .25;
}
return 0; /* outside */
}
static int
inside_circle_filled(const struct arg *arg, double x, double y)
{
return circle_check(x, y, arg->x1, arg->y1, arg->x2, arg->y2);
}
static int
circle_check_line(const struct arg *arg, double x, double y, double w)
/* Check for a point being inside the boundaries implied by the given arg
* and assuming a width 2*w each side of the boundaries. This function has
* the same semantic as square_check_line but tests the circle.
*/
{
double cx = (arg->x1+arg->x2)/2;
double wx = fabs(arg->x1-arg->x2)/2;
double cy = (arg->y1+arg->y2)/2;
double wy = fabs(arg->y1-arg->y2)/2;
if (circle_check(x, y, cx-wx-w, cy-wy-w, cx+wx+w, cy+wy+w))
{
/* Inside, but maybe too far; check for the redundant case where
* the lines overlap:
*/
wx -= w;
wy -= w;
if (wx > 0 && wy > 0 && circle_check(x, y, cx-wx, cy-wy, cx+wx, cy+wy))
return INSIDE; /* between (inside) the boundary lines. */
return 0; /* inside the lines themselves. */
}
return OUTSIDE; /* outside the boundary lines. */
}
static int
check_circle_filled(const struct arg *arg, double x, double y)
{
return circle_check_line(arg, x, y, FILTER_WIDTH);
}
static int
inside_circle(const struct arg *arg, double x, double y)
{
return circle_check_line(arg, x, y, arg->width/2) == 0;
}
static int
check_circle(const struct arg *arg, double x, double y)
{
/* Exactly as the 'square' code. */
double w = arg->width/2;
if (circle_check_line(arg, x, y, w+FILTER_WIDTH) == 0)
{
w -= FILTER_WIDTH;
if (w > 0 && circle_check_line(arg, x, y, w) == 0)
return INSIDE;
/* Point is somewhere in the filter region: */
return 0;
}
else /* Inside or outside the square by more than w+FILTER_WIDTH. */
return OUTSIDE;
}
/* "line",
* { NULL, NULL }, There is no 'filled' line.
* { inside_line, check_line }
*/
static int
line_check(double x, double y, double x1, double y1, double x2, double y2,
double w, double expand)
{
/* Shift all the points to (arg->x1, arg->y1) */
double lx = x2 - x1;
double ly = y2 - y1;
double len2 = lx*lx + ly*ly;
double cross, dot;
x -= x1;
y -= y1;
/* The dot product is the distance down the line, the cross product is
* the distance away from the line:
*
* distance = |cross| / sqrt(len2)
*/
cross = x * ly - y * lx;
/* If 'distance' is more than w the point is definitely outside the line:
*
* distance >= w
* |cross| >= w * sqrt(len2)
* cross^2 >= w^2 * len2:
*/
if (cross*cross >= (w+expand)*(w+expand)*len2)
return 0; /* outside */
/* Now find the distance *along* the line; this comes from the dot product
* lx.x+ly.y. The actual distance (in pixels) is:
*
* distance = dot / sqrt(len2)
*/
dot = lx * x + ly * y;
/* The test for 'outside' is:
*
* distance < 0 || distance > sqrt(len2)
* -> dot / sqrt(len2) > sqrt(len2)
* -> dot > len2
*
* But 'expand' is used for the filter width and needs to be handled too:
*/
return dot > -expand && dot < len2+expand;
}
static int
inside_line(const struct arg *arg, double x, double y)
{
return line_check(x, y, arg->x1, arg->y1, arg->x2, arg->y2, arg->width/2, 0);
}
static int
check_line(const struct arg *arg, double x, double y)
{
/* The end caps of the line must be checked too; it's not enough just to
* widen the line by FILTER_WIDTH; 'expand' exists for this purpose:
*/
if (line_check(x, y, arg->x1, arg->y1, arg->x2, arg->y2, arg->width/2,
FILTER_WIDTH))
{
/* Inside the line+filter; far enough inside that the filter isn't
* required?
*/
if (arg->width > 2*FILTER_WIDTH &&
line_check(x, y, arg->x1, arg->y1, arg->x2, arg->y2, arg->width/2,
-FILTER_WIDTH))
return INSIDE;
return 0;
}
return OUTSIDE;
}
static const struct
{
const char *name;
shape_fn_ptr function[2/*fill,line*/][2];
# define FN_INSIDE 0
# define FN_CHECK 1
} shape_defs[] =
{
{ "square",
{ { inside_square_filled, check_square_filled },
{ inside_square, check_square } }
},
{ "circle",
{ { inside_circle_filled, check_circle_filled },
{ inside_circle, check_circle } }
},
{ "line",
{ { NULL, NULL },
{ inside_line, check_line } }
}
};
#define shape_count ((sizeof shape_defs)/(sizeof shape_defs[0]))
static shape_fn_ptr
shape_of(const char *arg, double width, int f)
{
unsigned int i;
for (i=0; i<shape_count; ++i) if (strcmp(shape_defs[i].name, arg) == 0)
{
shape_fn_ptr fn = shape_defs[i].function[width != 0][f];
if (fn != NULL)
return fn;
fprintf(stderr, "genpng: %s %s not supported\n",
width == 0 ? "filled" : "unfilled", arg);
exit(1);
}
fprintf(stderr, "genpng: %s: not a valid shape name\n", arg);
exit(1);
}
static void
parse_arg(struct arg *arg, const char **argv/*7 arguments*/)
{
/* shape ::= color width shape x1 y1 x2 y2 */
arg->color = color_of(argv[0]);
arg->width = width_of(argv[1]);
arg->inside_fn = shape_of(argv[2], arg->width, FN_INSIDE);
arg->check_fn = shape_of(argv[2], arg->width, FN_CHECK);
arg->x1 = coordinate_of(argv[3]);
arg->y1 = coordinate_of(argv[4]);
arg->x2 = coordinate_of(argv[5]);
arg->y2 = coordinate_of(argv[6]);
}
static png_uint_32
read_wh(const char *name, const char *str)
/* read a PNG width or height */
{
char *ep = NULL;
unsigned long ul = strtoul(str, &ep, 10);
if (ep != NULL && *ep == 0 && ul > 0 && ul <= 0x7fffffff)
return (png_uint_32)/*SAFE*/ul;
fprintf(stderr, "genpng: %s: invalid number %s\n", name, str);
exit(1);
}
static void
pixel(png_uint_16p p, struct arg *args, int nargs, double x, double y)
{
/* Fill in the pixel by checking each shape (args[nargs]) for effects on
* the corresponding sample:
*/
double r=0, g=0, b=0, a=0;
while (--nargs >= 0 && a != 1)
{
/* NOTE: alpha_calc can return a value outside the range 0..1 with the
* bicubic filter.
*/
const double alpha = alpha_calc(args+nargs, x, y) * (1-a);
r += alpha * args[nargs].color->red;
g += alpha * args[nargs].color->green;
b += alpha * args[nargs].color->blue;
a += alpha;
}
/* 'a' may be negative or greater than 1; if it is, negative clamp the
* pixel to 0 if >1 clamp r/g/b:
*/
if (a > 0)
{
if (a > 1)
{
if (r > 1) r = 1;
if (g > 1) g = 1;
if (b > 1) b = 1;
a = 1;
}
/* And fill in the pixel: */
p[0] = (png_uint_16)/*SAFE*/round(r * 65535);
p[1] = (png_uint_16)/*SAFE*/round(g * 65535);
p[2] = (png_uint_16)/*SAFE*/round(b * 65535);
p[3] = (png_uint_16)/*SAFE*/round(a * 65535);
}
else
p[3] = p[2] = p[1] = p[0] = 0;
}
int
main(int argc, const char **argv)
{
int convert_to_8bit = 0;
/* There is one option: --8bit: */
if (argc > 1 && strcmp(argv[1], "--8bit") == 0)
--argc, ++argv, convert_to_8bit = 1;
if (argc >= 3)
{
png_uint_16p buffer;
int nshapes;
png_image image;
# define max_shapes 256
struct arg arg_list[max_shapes];
/* The libpng Simplified API write code requires a fully initialized
* structure.
*/
memset(&image, 0, sizeof image);
image.version = PNG_IMAGE_VERSION;
image.opaque = NULL;
image.width = read_wh("width", argv[1]);
image.height = read_wh("height", argv[2]);
image.format = PNG_FORMAT_LINEAR_RGB_ALPHA;
image.flags = 0;
image.colormap_entries = 0;
/* Check the remainder of the arguments */
for (nshapes=0; 3+7*(nshapes+1) <= argc && nshapes < max_shapes;
++nshapes)
parse_arg(arg_list+nshapes, argv+3+7*nshapes);
if (3+7*nshapes != argc)
{
fprintf(stderr, "genpng: %s: too many arguments\n", argv[3+7*nshapes]);
return 1;
}
#if 1
/* TO do: determine whether this guard against overflow is necessary.
* This comment in png.h indicates that it should be safe: "libpng will
* refuse to process an image where such an overflow would occur", but
* I don't see where the image gets rejected when the buffer is too
* large before the malloc is attempted.
*/
if (image.height > ((size_t)(-1))/(8*image.width)) {
fprintf(stderr, "genpng: image buffer would be too big");
return 1;
}
#endif
/* Create the buffer: */
buffer = malloc(PNG_IMAGE_SIZE(image));
if (buffer != NULL)
{
png_uint_32 y;
/* Write each row... */
for (y=0; y<image.height; ++y)
{
png_uint_32 x;
/* Each pixel in each row: */
for (x=0; x<image.width; ++x)
pixel(buffer + 4*(x + y*image.width), arg_list, nshapes, x, y);
}
/* Write the result (to stdout) */
if (png_image_write_to_stdio(&image, stdout, convert_to_8bit,
buffer, 0/*row_stride*/, NULL/*colormap*/))
{
free(buffer);
return 0; /* success */
}
else
fprintf(stderr, "genpng: write stdout: %s\n", image.message);
free(buffer);
}
else
fprintf(stderr, "genpng: out of memory: %lu bytes\n",
(unsigned long)PNG_IMAGE_SIZE(image));
}
else
{
/* Wrong number of arguments */
fprintf(stderr, "genpng: usage: genpng [--8bit] width height {shape}\n"
" Generate a transparent PNG in RGBA (truecolor+alpha) format\n"
" containing the given shape or shapes. Shapes are defined:\n"
"\n"
" shape ::= color width shape x1 y1 x2 y2\n"
" color ::= black|white|red|green|yellow|blue\n"
" color ::= brown|purple|pink|orange|gray|cyan\n"
" width ::= filled|<number>\n"
" shape ::= circle|square|line\n"
" x1,x2 ::= <number>\n"
" y1,y2 ::= <number>\n"
"\n"
" Numbers are floating point numbers describing points relative to\n"
" the top left of the output PNG as pixel coordinates. The 'width'\n"
" parameter is either the width of the line (in output pixels) used\n"
" to draw the shape or 'filled' to indicate that the shape should\n"
" be filled with the color.\n"
"\n"
" Colors are interpreted loosely to give access to the eight full\n"
" intensity RGB values:\n"
"\n"
" black, red, green, blue, yellow, cyan, purple, white,\n"
"\n"
" Cyan is full intensity blue+green; RGB(0,1,1), plus the following\n"
" lower intensity values:\n"
"\n"
" brown: red+orange: RGB(0.5, 0.125, 0) (dark red+orange)\n"
" pink: red+white: RGB(1.0, 0.5, 0.5)\n"
" orange: red+yellow: RGB(1.0, 0.5, 0)\n"
" gray: black+white: RGB(0.5, 0.5, 0.5)\n"
"\n"
" The RGB values are selected to make detection of aliasing errors\n"
" easy. The names are selected to make the description of errors\n"
" easy.\n"
"\n"
" The PNG is written to stdout, if --8bit is given a 32bpp RGBA sRGB\n"
" file is produced, otherwise a 64bpp RGBA linear encoded file is\n"
" written.\n");
}
return 1;
}
#endif /* SIMPLIFIED_WRITE && STDIO */

View File

@ -0,0 +1,110 @@
#!/bin/sh
#
# intgamma.sh
#
# Last changed in libpng 1.6.0 [February 14, 2013]
#
# COPYRIGHT: Written by John Cunningham Bowler, 2013.
# 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.
#
# Shell script to generate png.c 8-bit and 16-bit log tables (see the code in
# png.c for details).
#
# This script uses the "bc" arbitrary precision calculator to calculate 32-bit
# fixed point values of logarithms appropriate to finding the log of an 8-bit
# (0..255) value and a similar table for the exponent calculation.
#
# "bc" must be on the path when the script is executed, and the math library
# (-lm) must be available
#
# function to print out a list of numbers as integers; the function truncates
# the integers which must be one-per-line
function print(){
awk 'BEGIN{
str = ""
}
{
sub("\\.[0-9]*$", "")
if ($0 == "")
$0 = "0"
if (str == "")
t = " " $0 "U"
else
t = str ", " $0 "U"
if (length(t) >= 80) {
print str ","
str = " " $0 "U"
} else
str = t
}
END{
print str
}'
}
#
# The logarithm table.
cat <<END
/* 8-bit log table: png_8bit_l2[128]
* This is a table of -log(value/255)/log(2) for 'value' in the range 128 to
* 255, so it's the base 2 logarithm of a normalized 8-bit floating point
* mantissa. The numbers are 32-bit fractions.
*/
static const png_uint_32
png_8bit_l2[128] =
{
END
#
bc -lqws <<END | print
f=65536*65536/l(2)
for (i=128;i<256;++i) { .5 - l(i/255)*f; }
END
echo '};'
echo
#
# The exponent table.
cat <<END
/* The 'exp()' case must invert the above, taking a 20-bit fixed point
* logarithmic value and returning a 16 or 8-bit number as appropriate. In
* each case only the low 16 bits are relevant - the fraction - since the
* integer bits (the top 4) simply determine a shift.
*
* The worst case is the 16-bit distinction between 65535 and 65534; this
* requires perhaps spurious accuracy in the decoding of the logarithm to
* distinguish log2(65535/65534.5) - 10^-5 or 17 bits. There is little chance
* of getting this accuracy in practice.
*
* To deal with this the following exp() function works out the exponent of the
* frational part of the logarithm by using an accurate 32-bit value from the
* top four fractional bits then multiplying in the remaining bits.
*/
static const png_uint_32
png_32bit_exp[16] =
{
END
#
bc -lqws <<END | print
f=l(2)/16
for (i=0;i<16;++i) {
x = .5 + e(-i*f)*2^32;
if (x >= 2^32) x = 2^32-1;
x;
}
END
echo '};'
echo
#
# And the table of adjustment values.
cat <<END
/* Adjustment table; provided to explain the numbers in the code below. */
#if 0
END
bc -lqws <<END | awk '{ printf "%5d %s\n", 12-NR, $0 }'
for (i=11;i>=0;--i){
(1 - e(-(2^i)/65536*l(2))) * 2^(32-i)
}
END
echo '#endif'

View File

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

View File

@ -0,0 +1,164 @@
/* png-fix-itxt version 1.0.0
*
* Copyright 2015 Glenn Randers-Pehrson
* Last changed in libpng 1.6.18 [July 23, 2015]
*
* This code is released under the libpng license.
* For conditions of distribution and use, see the disclaimer
* and license in png.h
*
* Usage:
*
* png-fix-itxt.exe < bad.png > good.png
*
* Fixes a PNG file written with libpng-1.6.0 or 1.6.1 that has one or more
* uncompressed iTXt chunks. Assumes that the actual length is greater
* than or equal to the value in the length byte, and that the CRC is
* correct for the actual length. This program hunts for the CRC and
* adjusts the length byte accordingly. It is not an error to process a
* PNG file that has no iTXt chunks or one that has valid iTXt chunks;
* such files will simply be copied.
*
* Requires zlib (for crc32 and Z_NULL); build with
*
* gcc -O -o png-fix-itxt png-fix-itxt.c -lz
*
* If you need to handle iTXt chunks larger than 500000 kbytes you must
* rebuild png-fix-itxt with a larger values of MAX_LENGTH (or a smaller value
* if you know you will never encounter such huge iTXt chunks).
*/
#include <stdio.h>
#include <zlib.h>
#define MAX_LENGTH 500000
/* Read one character (inchar), also return octet (c), break if EOF */
#define GETBREAK inchar=getchar(); \
c=(inchar & 0xffU);\
if (inchar != c) break
int
main(void)
{
unsigned int i;
unsigned char buf[MAX_LENGTH];
unsigned long crc;
unsigned char c;
int inchar;
/* Skip 8-byte signature */
for (i=8; i; i--)
{
GETBREAK;
putchar(c);
}
if (inchar == c) /* !EOF */
for (;;)
{
/* Read the length */
unsigned long length; /* must be 32 bits! */
GETBREAK; buf[0] = c; length = c; length <<= 8;
GETBREAK; buf[1] = c; length += c; length <<= 8;
GETBREAK; buf[2] = c; length += c; length <<= 8;
GETBREAK; buf[3] = c; length += c;
/* Read the chunkname */
GETBREAK; buf[4] = c;
GETBREAK; buf[5] = c;
GETBREAK; buf[6] = c;
GETBREAK; buf[7] = c;
/* The iTXt chunk type expressed as integers is (105, 84, 88, 116) */
if (buf[4] == 105 && buf[5] == 84 && buf[6] == 88 && buf[7] == 116)
{
if (length >= MAX_LENGTH-12)
break; /* To do: handle this more gracefully */
/* Initialize the CRC */
crc = crc32(0, Z_NULL, 0);
/* Copy the data bytes */
for (i=8; i < length + 12; i++)
{
GETBREAK; buf[i] = c;
}
if (inchar != c) /* EOF */
break;
/* Calculate the CRC */
crc = crc32(crc, buf+4, (uInt)length+4);
for (;;)
{
/* Check the CRC */
if (((crc >> 24) & 0xffU) == buf[length+8] &&
((crc >> 16) & 0xffU) == buf[length+9] &&
((crc >> 8) & 0xffU) == buf[length+10] &&
((crc ) & 0xffU) == buf[length+11])
break;
length++;
if (length >= MAX_LENGTH-12)
break;
GETBREAK;
buf[length+11] = c;
/* Update the CRC */
crc = crc32(crc, buf+7+length, 1);
}
if (inchar != c) /* EOF */
break;
/* Update length bytes */
buf[0] = (unsigned char)((length >> 24) & 0xffU);
buf[1] = (unsigned char)((length >> 16) & 0xffU);
buf[2] = (unsigned char)((length >> 8) & 0xffU);
buf[3] = (unsigned char)((length ) & 0xffU);
/* Write the fixed iTXt chunk (length, name, data, crc) */
for (i=0; i<length+12; i++)
putchar(buf[i]);
}
else
{
if (inchar != c) /* EOF */
break;
/* Copy bytes that were already read (length and chunk name) */
for (i=0; i<8; i++)
putchar(buf[i]);
/* Copy data bytes and CRC */
for (i=8; i< length+12; i++)
{
GETBREAK;
putchar(c);
}
if (inchar != c) /* EOF */
{
break;
}
/* The IEND chunk type expressed as integers is (73, 69, 78, 68) */
if (buf[4] == 73 && buf[5] == 69 && buf[6] == 78 && buf[7] == 68)
break;
}
if (inchar != c) /* EOF */
break;
if (buf[4] == 73 && buf[5] == 69 && buf[6] == 78 && buf[7] == 68)
break;
}
return 0;
}

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,25 @@
#!/bin/sh
# reindent a libpng C source
# COPYRIGHT: Written by Glenn Randers-Pehrson, 2016.
# 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.
# Usage:
# reindent inputtabsize outputtabsize inputcontinuestring outputcontinuestring
#
# Assumes that continued lines begin with indentation plus one space, and
# that continued comments begin with indentation plus " *".
#
# eg, to change libpng coding style from 3-space indentation with 4-space
# continuations to 4-space indentation with 2-space continuations:
#
# reindent 3 4 "\t " " " < example.c > example.c_4_2
# and to restore the file back to libpng coding style
# reindent 4 3 " " " " < example.c_4_2 > example.c_3_4
unexpand --first-only --t $1 | \
sed -e "/^ *$3[^\*]/{s/$3/$4/}" | \
expand -t $2

View File

@ -0,0 +1,48 @@
/*-
* sRGB.h
*
* Last changed in libpng 1.6.0 [February 14, 2013]
*
* COPYRIGHT: Written by John Cunningham Bowler, 2013.
* 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.
*
* Utility file; not actually a header, this contains definitions of sRGB
* calculation functions for inclusion in those test programs that need them.
*
* All routines take and return a floating point value in the range
* 0 to 1.0, doing a calculation according to the sRGB specification
* (in fact the source of the numbers is the wikipedia article at
* https://en.wikipedia.org/wiki/SRGB).
*/
static double
sRGB_from_linear(double l)
{
if (l <= 0.0031308)
l *= 12.92;
else
l = 1.055 * pow(l, 1/2.4) - 0.055;
return l;
}
static double
linear_from_sRGB(double s)
{
if (s <= 0.04045)
return s / 12.92;
else
return pow((s+0.055)/1.055, 2.4);
}
static double
YfromRGB(double r, double g, double b)
{
/* Use the sRGB (rounded) coefficients for Rlinear, Glinear, Blinear to get
* the CIE Y value (also linear).
*/
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
}