Compare commits

..

29 Commits

Author SHA1 Message Date
Glenn Randers-Pehrson
20a2f9b9be [libpng16] Imported from libpng-1.6.18beta05.tar 2015-05-31 10:20:13 -05:00
Glenn Randers-Pehrson
1088f4925f [libpng16] Fixed old cut&paste bug in the weighted filter selection code in
pngwutil.c, introduced in libpng-0.95, March 1997.
2015-05-30 22:58:54 -05:00
Glenn Randers-Pehrson
b66de48b3b [libpng16] Changed png_voidcast(), etc., to voidcast(), etc., in
contrib/tools/pngfix.c to avoid confusion with the libpng private macros.
2015-05-30 22:57:33 -05:00
Glenn Randers-Pehrson
e6877671f1 [libpng16] Backported filter selection code from libpng-1.7.0beta51, to combine
sub_row, up_row, avg_row, and paeth_row into try_row and tst_row.
2015-05-30 11:54:45 -05:00
Glenn Randers-Pehrson
9fbdce8fb5 [libpng16] Bump version to 1.6.18beta05 2015-05-29 11:35:15 -05:00
Glenn Randers-Pehrson
f6b9c993e1 [libpng16] Imported from libpng-1.6.18beta04.tar 2015-05-29 11:28:33 -05:00
Glenn Randers-Pehrson
d46b570919 [libpng16] Avoid a harmless potential integer overflow in png_XYZ_from_xy(). 2015-05-20 13:21:53 -05:00
Glenn Randers-Pehrson
918d23f658 [libpng16] Trivial editing of comments in png.c 2015-05-09 21:48:11 -05:00
Glenn Randers-Pehrson
d2fedd629d [libpng16] Avoid Coverity issue 80858 (REVERSE NULL) in pngtest.c
PNG_DEBUG builds.
2015-05-09 21:47:00 -05:00
Glenn Randers-Pehrson
2d069831fb [libpng16] Document PNG_RELEASE_BUILD in the man page. 2015-05-06 20:31:21 -05:00
John Bowler
8ee821e69a [libpng16] PNG_RELEASE_BUILD replaces tests where the code depended on the build base
type and can be defined on the command line, allowing testing in beta builds. Back
ported from libpng17.
2015-05-06 20:03:14 -05:00
Glenn Randers-Pehrson
279dc3f0bf [libpng16] Added some documentation about features of the Simplified Write API. 2015-05-06 17:02:47 -05:00
Glenn Randers-Pehrson
846357d592 [libpng16] Bump version to 1.6.18beta04 2015-05-06 16:39:42 -05:00
Glenn Randers-Pehrson
d8d18fbb02 [libpng16] Imported from libpng-1.6.18beta03.tar 2015-05-06 16:39:31 -05:00
John Bowler
74428df2fd [libpng16] Improved DIV65535 algorithm in simpleover.c. By experiment,
(x + (x>>16) + 32769) works as well as the previous form and avoids one shift.
2015-05-06 11:41:17 -05:00
Glenn Randers-Pehrson
d6b1375bf5 [libpng16] Trivial editing of comments in contrib/examples/simpleover.c 2015-05-04 20:25:31 -05:00
John Bowler
0f12df19a1 [libpng16] Added PNG generation tool, fixed unitialized pointer in
simpleover. Also added a comment to png.h pointing out that the pointer must
 be set to NULL!  (simpleover crashes with any slightly complex command lines
 without this fix.)
2015-05-04 19:58:41 -05:00
John Bowler
cde3078963 [libpng16] Added contrib/examples/simpleover.c, to demonstrate how to handle
alpha compositing of multiple images, using the "simplified API"
2015-05-02 19:53:40 -05:00
Glenn Randers-Pehrson
216ab04994 [libpng16] Updated comment in pngpriv.h about ARM support. 2015-04-29 11:30:35 -05:00
Glenn Randers-Pehrson
e0f0d05938 [libpng16] Replaced "unexpected" with an integer (0xabadca11) in pngset.c 2015-04-29 11:29:31 -05:00
Glenn Randers-Pehrson
23da29af91 [libpng16] Bump version to 1.6.18beta03 2015-04-26 16:38:18 -05:00
Glenn Randers-Pehrson
ecc391bbcf [libpng16] Imported from libpng-1.6.18beta02.tar 2015-04-26 16:38:10 -05:00
Glenn Randers-Pehrson
c861dc8923 [libpng16] Suppressed some warnings from the Borland C++ 5.5.1/5.82 compiler
(Bug report by Viktor Szaka'ts).
2015-04-01 12:06:01 -05:00
Glenn Randers-Pehrson
507a8cdc5e [libpng16] Bump version to 1.6.18beta02 2015-04-01 08:24:26 -05:00
Glenn Randers-Pehrson
bd85b5713c [libpng16] Imported from libpng-1.6.18beta01.tar 2015-04-01 08:24:14 -05:00
John Bowler
a21a5b22c1 [libpng16] Fixed rgb_to_gray checks and added tRNS checks to pngvalid.c 2015-03-30 21:38:31 -05:00
Glenn Randers-Pehrson
d344589703 [libpng16] Removed PNG_SET_CHUNK_[CACHE|MALLOC]_LIMIT_SUPPORTED macros. They
have been combined with PNG_SET_USER_LIMITS_SUPPORTED (resolves
bug report by Andrew Church).
2015-03-27 08:58:32 -05:00
Glenn Randers-Pehrson
218a6fe9e5 [libpng16] Bump version to 1.6.18beta01 2015-03-26 08:55:25 -05:00
Glenn Randers-Pehrson
c98f7fb4e3 [libpng16] Imported from libpng-1.6.17.tar 2015-03-26 08:11:12 -05:00
48 changed files with 2543 additions and 784 deletions

121
ANNOUNCE
View File

@@ -1,4 +1,4 @@
Libpng 1.6.17rc06 - March 23, 2015
Libpng 1.6.18beta05 - May 31, 2015
This is not intended to be a public release. It will be replaced
within a few weeks by a public version or by another test version.
@@ -8,98 +8,59 @@ Files available for download:
Source files with LF line endings (for Unix/Linux) and with a
"configure" script
1.6.17rc06.tar.xz (LZMA-compressed, recommended)
1.6.17rc06.tar.gz
1.6.18beta05.tar.xz (LZMA-compressed, recommended)
1.6.18beta05.tar.gz
Source files with CRLF line endings (for Windows), without the
"configure" script
lp1617r06.7z (LZMA-compressed, recommended)
lp1617r06.zip
lp1618b05.7z (LZMA-compressed, recommended)
lp1618b05.zip
Other information:
1.6.17rc06-README.txt
1.6.17rc06-LICENSE.txt
libpng-1.6.17rc06-*.asc (armored detached GPG signatures)
1.6.18beta05-README.txt
1.6.18beta05-LICENSE.txt
libpng-1.6.18beta05-*.asc (armored detached GPG signatures)
Changes since the last public release (1.6.16):
Changes since the last public release (1.6.17):
Version 1.6.17beta01 [January 29, 2015]
Removed duplicate PNG_SAFE_LIMITS_SUPPORTED handling from pngconf.h
Corrected the width limit calculation in png_check_IHDR().
Removed user limits from pngfix. Also pass NULL pointers to
png_read_row to skip the unnecessary row de-interlace stuff.
Added testing of png_set_packing() to pngvalid.c
Regenerated configure scripts in the *.tar distributions with libtool-2.4.4
Implement previously untested cases of libpng transforms in pngvalid.c
Fixed byte order in 2-byte filler, in png_do_read_filler().
Made the check for out-of-range values in png_set_tRNS() detect
values that are exactly 2^bit_depth, and work on 16-bit platforms.
Merged some parts of libpng-1.6.17beta01 and libpng-1.7.0beta47.
Added #ifndef __COVERITY__ where needed in png.c, pngrutil.c and
pngset.c to avoid warnings about dead code.
Added "& 0xff" to many instances of expressions that are typecast
to (png_byte), to avoid Coverity gripes.
Version 1.6.18beta01 [April 1, 2015]
Removed PNG_SET_CHUNK_[CACHE|MALLOC]_LIMIT_SUPPORTED macros. They
have been combined with PNG_SET_USER_LIMITS_SUPPORTED (resolves
bug report by Andrew Church).
Fixed rgb_to_gray checks and added tRNS checks to pngvalid.c. This
fixes some arithmetic errors that caused some tests to fail on
some 32-bit platforms (Bug reports by Peter Breitenlohner [i686]
and Petr Gajdos [i586]).
Version 1.6.17beta02 [February 7, 2015]
Work around one more Coverity-scan dead-code warning.
Do not build png_product2() when it is unused.
Version 1.6.18beta02 [April 26, 2015]
Suppressed some warnings from the Borland C++ 5.5.1/5.82 compiler
(Bug report by Viktor Szaka'ts).
Version 1.6.17beta03 [February 17, 2015]
Display user limits in the output from pngtest.
Eliminated the PNG_SAFE_LIMITS macro and restored the 1-million-column
and 1-million-row default limits in pnglibconf.dfa, that can be reset
by the user at build time or run time. This provides a more robust
defense against DOS and as-yet undiscovered overflows.
Version 1.6.18beta03 [May 6, 2015]
Replaced "unexpected" with an integer (0xabadca11) in pngset.c
where a long was expected, to avoid a compiler warning when PNG_DEBUG > 1.
Added contrib/examples/simpleover.c, to demonstrate how to handle
alpha compositing of multiple images, using the "simplified API"
and an example PNG generation tool, contrib/examples/genpng.c
(John Bowler).
Version 1.6.17beta04 [February 21, 2015]
Added PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED macro, on by default.
Allow user to call png_get_IHDR() with NULL arguments (Reuben Hawkins).
Rebuilt configure scripts with automake-1.15 and libtool-2.4.6
Version 1.6.18beta04 [May 20, 2015]
PNG_RELEASE_BUILD replaces tests where the code depended on the build base
type and can be defined on the command line, allowing testing in beta
builds (John Bowler).
Avoid Coverity issue 80858 (REVERSE NULL) in pngtest.c
Avoid a harmless potential integer overflow in png_XYZ_from_xy() (Bug
report from Christopher Ferris).
Version 1.6.17beta05 [February 25, 2015]
Restored compiling of png_reciprocal2 with PNG_NO_16BIT.
Version 1.6.17beta06 [February 27, 2015]
Moved png_set_filter() prototype into a PNG_WRITE_SUPPORTED block
of png.h.
Avoid runtime checks when converting integer to png_byte with
Visual Studio (Sergey Kosarevsky)
Version 1.6.17rc01 [March 4, 2015]
No changes.
Version 1.6.17rc02 [March 9, 2015]
Removed some comments that the configure script did not handle
properly from scripts/pnglibconf.dfa and pnglibconf.h.prebuilt.
Free the unknown_chunks structure even when it contains no data.
Version 1.6.17rc03 [March 12, 2015]
Updated CMakeLists.txt to add OSX framework, change YES/NO to ON/OFF
for consistency, and remove some useless tests (Alexey Petruchik).
Version 1.6.17rc04 [March 16, 2015]
Remove pnglibconf.h, pnglibconf.c, and pnglibconf.out instead of
pnglibconf.* in "make clean" (Cosmin).
Fix bug in calculation of maxbits, in png_write_sBIT, introduced
in libpng-1.6.17beta01 (John Bowler).
Version 1.6.17rc05 [March 21, 2015]
Define PNG_FILTER_* and PNG_FILTER_VALUE_* in png.h even when WRITE
is not supported (John Bowler). This fixes an error introduced in
libpng-1.6.17beta06.
Reverted "& 0xff" additions of version 1.6.17beta01. Libpng passes
the Coverity scan without them.
Version 1.6.17rc06 [March 23, 2015]
Remove pnglibconf.dfn and pnglibconf.pre with "make clean".
Reformatted some "&0xff" instances to "& 0xff".
Fixed simplified 8-bit-linear to sRGB alpha. The calculated alpha
value was wrong. It's not clear if this affected the final stored
value; in the obvious code path the upper and lower 8-bits of the
alpha value were identical and the alpha was truncated to 8-bits
rather than dividing by 257 (John Bowler).
Version 1.6.18beta05 [May 31, 2015]
Backport filter selection code from libpng-1.7.0beta51, to combine
sub_row, up_row, avg_row, and paeth_row into try_row and tst_row.
Changed png_voidcast(), etc., to voidcast(), etc., in contrib/tools/pngfix.c
to avoid confusion with the libpng private macros.
Fixed old cut&paste bug in the weighted filter selection code in
pngwutil.c, introduced in libpng-0.95, March 1997.
Send comments/corrections/commendations to png-mng-implement at lists.sf.net
(subscription required; visit

72
CHANGES
View File

@@ -1,11 +1,14 @@
#if 0
CHANGES - changes for libpng
Version 0.2
version 0.1 [March 29, 1995]
initial work-in-progress release
version 0.2 [April 1, 1995]
added reader into png.h
fixed small problems in stub file
Version 0.3
version 0.3 [April 8, 1995]
added pull reader
split up pngwrite.c to several files
added pnglib.txt
@@ -14,9 +17,9 @@ Version 0.3
fixed some bugs in writer
interfaced with zlib 0.5
added K&R support
added check for 64 KB blocks for 16-bit machines
added check for 64 KB blocks for 16 bit machines
Version 0.4
version 0.4 [April 26, 1995]
cleaned up code and commented code
simplified time handling into png_time
created png_color_16 and png_color_8 to handle color needs
@@ -27,28 +30,29 @@ Version 0.4
cleaned up zTXt reader and writer (using zlib's Reset functions)
split transformations into pngrtran.c and pngwtran.c
Version 0.5
version 0.5 [April 30, 1995]
interfaced with zlib 0.8
fixed many reading and writing bugs
saved using 3 spaces instead of tabs
Version 0.6
version 0.6 [May 1, 1995]
first beta release
added png_large_malloc() and png_large_free()
added png_size_t
cleaned up some compiler warnings
added png_start_read_image()
Version 0.7
version 0.7 [June 24, 1995]
cleaned up lots of bugs
finished dithering and other stuff
added test program
changed name from pnglib to libpng
Version 0.71 [June, 1995]
version 0.71 [June 26, 1995]
changed pngtest.png for zlib 0.93
fixed error in libpng.txt and example.c
Version 0.8
version 0.8 [August 20, 1995]
cleaned up some bugs
added png_set_filler()
split up pngstub.c into pngmem.c, pngio.c, and pngerror.c
@@ -1449,8 +1453,9 @@ Version 1.2.6beta4 [July 28, 2004]
Use png_malloc instead of png_zalloc to allocate the pallete.
Version 1.0.16rc1 and 1.2.6rc1 [August 4, 2004]
Fixed buffer overflow vulnerability in png_handle_tRNS()
Fixed integer arithmetic overflow vulnerability in png_read_png().
Fixed buffer overflow vulnerability (CVE-2004-0597) in png_handle_tRNS().
Fixed NULL dereference vulnerability (CVE-2004-0598) in png_handle_iCCP().
Fixed integer overflow vulnerability (CVE-2004-0599) in png_read_png().
Fixed some harmless bugs in png_handle_sBIT, etc, that would cause
duplicate chunk types to go undetected.
Fixed some timestamps in the -config version
@@ -5141,7 +5146,7 @@ Version 1.6.17beta01 [January 29, 2015]
Added #ifndef __COVERITY__ where needed in png.c, pngrutil.c and
pngset.c to avoid warnings about dead code.
Added "& 0xff" to many instances of expressions that are typecast
to (png_byte), to avoid Coverity gripes.
to (png_byte), to avoid Coverity warnings.
Version 1.6.17beta02 [February 7, 2015]
Work around one more Coverity-scan dead-code warning.
@@ -5202,6 +5207,46 @@ Version 1.6.17rc06 [March 23, 2015]
alpha value were identical and the alpha was truncated to 8-bits
rather than dividing by 257 (John Bowler).
Version 1.6.17 [March 26, 2015]
No changes.
Version 1.6.18beta01 [April 1, 2015]
Removed PNG_SET_CHUNK_[CACHE|MALLOC]_LIMIT_SUPPORTED macros. They
have been combined with PNG_SET_USER_LIMITS_SUPPORTED (resolves
bug report by Andrew Church).
Fixed rgb_to_gray checks and added tRNS checks to pngvalid.c. This
fixes some arithmetic errors that caused some tests to fail on
some 32-bit platforms (Bug reports by Peter Breitenlohner [i686]
and Petr Gajdos [i586]).
Version 1.6.18beta02 [April 26, 2015]
Suppressed some warnings from the Borland C++ 5.5.1/5.82 compiler
(Bug report by Viktor Szaka'ts).
Version 1.6.18beta03 [May 6, 2015]
Replaced "unexpected" with an integer (0xabadca11) in pngset.c
where a long was expected, to avoid a compiler warning when PNG_DEBUG > 1.
Added contrib/examples/simpleover.c, to demonstrate how to handle
alpha compositing of multiple images, using the "simplified API"
and an example PNG generation tool, contrib/examples/genpng.c
(John Bowler).
Version 1.6.18beta04 [May 20, 2015]
PNG_RELEASE_BUILD replaces tests where the code depended on the build base
type and can be defined on the command line, allowing testing in beta
builds (John Bowler).
Avoid Coverity issue 80858 (REVERSE NULL) in pngtest.c PNG_DEBUG builds.
Avoid a harmless potential integer overflow in png_XYZ_from_xy() (Bug
report from Christopher Ferris).
Version 1.6.18beta05 [May 31, 2015]
Backport filter selection code from libpng-1.7.0beta51, to combine
sub_row, up_row, avg_row, and paeth_row into try_row and tst_row.
Changed png_voidcast(), etc., to voidcast(), etc., in contrib/tools/pngfix.c
to avoid confusion with the libpng private macros.
Fixed old cut&paste bug in the weighted filter selection code in
pngwutil.c, introduced in libpng-0.95, March 1997.
Send comments/corrections/commendations to png-mng-implement at lists.sf.net
(subscription required; visit
https://lists.sourceforge.net/lists/listinfo/png-mng-implement
@@ -5209,3 +5254,4 @@ to subscribe)
or to glennrp at users.sourceforge.net
Glenn R-P
#endif

View File

@@ -16,7 +16,7 @@ enable_testing()
set(PNGLIB_MAJOR 1)
set(PNGLIB_MINOR 6)
set(PNGLIB_RELEASE 17)
set(PNGLIB_RELEASE 18)
set(PNGLIB_NAME libpng${PNGLIB_MAJOR}${PNGLIB_MINOR})
set(PNGLIB_VERSION ${PNGLIB_MAJOR}.${PNGLIB_MINOR}.${PNGLIB_RELEASE})
@@ -261,7 +261,7 @@ endif(NOT WIN32 OR CYGWIN OR MINGW)
# SET UP LINKS
if(PNG_SHARED)
set_target_properties(${PNG_LIB_NAME} PROPERTIES
# VERSION 16.${PNGLIB_RELEASE}.1.6.17rc06
# VERSION 16.${PNGLIB_RELEASE}.1.6.18beta05
VERSION 16.${PNGLIB_RELEASE}.0
SOVERSION 16
CLEAN_DIRECT_OUTPUT 1)

View File

@@ -10,7 +10,7 @@ this sentence.
This code is released under the libpng license.
libpng versions 1.2.6, August 15, 2004, through 1.6.17rc06, March 23, 2015, are
libpng versions 1.2.6, August 15, 2004, through 1.6.18beta05, May 31, 2015, are
Copyright (c) 2004, 2006-2015 Glenn Randers-Pehrson, and are
distributed according to the same disclaimer and license as libpng-1.2.5
with the following individual added to the list of Contributing Authors
@@ -108,4 +108,4 @@ certification mark of the Open Source Initiative.
Glenn Randers-Pehrson
glennrp at users.sourceforge.net
March 23, 2015
May 31, 2015

4
README
View File

@@ -1,4 +1,4 @@
README for libpng version 1.6.17rc06 - March 23, 2015 (shared library 16.0)
README for libpng version 1.6.18beta05 - May 31, 2015 (shared library 16.0)
See the note about version numbers near the top of png.h
See INSTALL for instructions on how to install libpng.
@@ -134,7 +134,7 @@ and ...". If in doubt, send questions to me. I'll bounce them
to others, if necessary.
Please do not send suggestions on how to change PNG. We have
been discussing PNG for nineteen years now, and it is official and
been discussing PNG for twenty years now, and it is official and
finished. If you have suggestions for libpng, however, I'll
gladly listen. Even if your suggestion is not used immediately,
it may be used later.

View File

@@ -73,8 +73,9 @@ done
# present bad things are happening.
#
# The autotools generated files:
libpng_autotools_files="Makefile.in aclocal.m4 config.guess config.h.in\
config.sub configure depcomp install-sh ltmain.sh missing test-driver"
libpng_autotools_files="Makefile.in aclocal.m4 config.guess config.h.in
config.h.in~ config.sub configure depcomp install-sh ltmain.sh missing\
test-driver"
#
# Files generated by versions of configue >2.68 or automake >1.13 (i.e. later
# versions than those required by configure.ac):

View File

@@ -18,7 +18,7 @@ AC_PREREQ([2.68])
dnl Version number stuff here:
AC_INIT([libpng],[1.6.17rc06],[png-mng-implement@lists.sourceforge.net])
AC_INIT([libpng],[1.6.18beta05],[png-mng-implement@lists.sourceforge.net])
AC_CONFIG_MACRO_DIR([scripts])
# libpng does not follow GNU file name conventions (hence 'foreign')
@@ -39,10 +39,10 @@ dnl automake, so the following is not necessary (and is not defined anyway):
dnl AM_PREREQ([1.11.2])
dnl stop configure from automagically running automake
PNGLIB_VERSION=1.6.17rc06
PNGLIB_VERSION=1.6.18beta05
PNGLIB_MAJOR=1
PNGLIB_MINOR=6
PNGLIB_RELEASE=17
PNGLIB_RELEASE=18
dnl End of version number stuff

View File

@@ -0,0 +1,645 @@
/*- simpleover
*
* COPYRIGHT: Written by John Cunningham Bowler, 2015.
* 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.
*
* Read several PNG files, which should have an alpha channel or transparency
* information, and composite them together to produce one or more 16-bit linear
* RGBA intermediates. This involves doing the correct 'over' composition to
* combine the alpha channels and corresponding data.
*
* Finally read an output (background) PNG using the 24-bit RGB format (the
* PNG will be composited on green (#00ff00) by default if it has an alpha
* channel), and apply the intermediate image generated above to specified
* locations in the image.
*
* The command line has the general format:
*
* simpleover <background.png> [output.png]
* {--sprite=width,height,name {[--at=x,y] {sprite.png}}}
* {--add=name {x,y}}
*
* The --sprite and --add options may occur multiple times. They are executed
* in order. --add may refer to any sprite already read.
*
* This code is intended to show how to composite multiple images together
* correctly. Apart from the libpng Simplified API the only work done in here
* is to combine multiple input PNG images into a single sprite; this involves
* a Porter-Duff 'over' operation and the input PNG images may, as a result,
* be regarded as being layered one on top of the other with the first (leftmost
* on the command line) being at the bottom and the last on the top.
*/
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.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, so long as this
* file is linked against a sufficiently recent libpng (1.6+) it is ok to
* change this to <png.h>:
*/
#include "../../png.h"
#define sprite_name_chars 15
struct sprite {
FILE *file;
png_uint_16p buffer;
unsigned int width;
unsigned int height;
char name[sprite_name_chars+1];
};
#if 0 /* div by 65535 test program */
#include <math.h>
#include <stdio.h>
int main(void) {
double err = 0;
unsigned int xerr = 0;
unsigned int r = 32769;
{
unsigned int x = 0;
do {
unsigned int t = x + (x >> 16) /*+ (x >> 31)*/ + r;
double v = x, errtest;
if (t < x) {
fprintf(stderr, "overflow: %u+%u -> %u\n", x, r, t);
return 1;
}
v /= 65535;
errtest = v;
t >>= 16;
errtest -= t;
if (errtest > err) {
err = errtest;
xerr = x;
if (errtest >= .5) {
fprintf(stderr, "error: %u/65535 = %f, not %u, error %f\n",
x, v, t, errtest);
return 0;
}
}
} while (++x <= 65535U*65535U);
}
printf("error %f @ %u\n", err, xerr);
return 0;
}
#endif /* div by 65535 test program */
static void
sprite_op(const struct sprite *sprite, int x_offset, int y_offset,
png_imagep image, const png_uint_16 *buffer)
{
/* This is where the Porter-Duff 'Over' operator is evaluated; change this
* code to change the operator (this could be parameterized). Any other
* image processing operation could be used here.
*/
/* Check for an x or y offset that pushes any part of the image beyond the
* right or bottom of the sprite:
*/
if ((y_offset < 0 || (unsigned)/*SAFE*/y_offset < sprite->height) &&
(x_offset < 0 || (unsigned)/*SAFE*/x_offset < sprite->width))
{
unsigned int y = 0;
if (y_offset < 0)
y = -y_offset; /* Skip to first visible row */
do
{
unsigned int x = 0;
if (x_offset < 0)
x = -x_offset;
do
{
/* In and out are RGBA values, so: */
const png_uint_16 *in_pixel = buffer + (y * image->width + x)*4;
png_uint_32 in_alpha = in_pixel[3];
/* This is the optimized Porter-Duff 'Over' operation, when the
* input alpha is 0 the output is not changed.
*/
if (in_alpha > 0)
{
png_uint_16 *out_pixel = sprite->buffer +
((y+y_offset) * sprite->width + (x+x_offset))*4;
/* This is the weight to apply to the output: */
in_alpha = 65535-in_alpha;
if (in_alpha > 0)
{
/* The input must be composed onto the output. This means
* multiplying the current output pixel value by the inverse
* of the input alpha (1-alpha). A division is required but
* it is by the constant 65535. Approximate this as:
*
* (x + (x >> 16) + 32769) >> 16;
*
* This is exact (and does not overflow) for all values of
* x in the range 0..65535*65535. (Note that the calculation
* produces the closest integer; the maximum error is <0.5).
*/
png_uint_32 tmp;
# define compose(c)\
tmp = out_pixel[c] * in_alpha;\
tmp = (tmp + (tmp >> 16) + 32769) >> 16;\
out_pixel[c] = tmp + in_pixel[c]
/* The following is very vectorizable... */
compose(0);
compose(1);
compose(2);
compose(3);
}
else
out_pixel[0] = in_pixel[0],
out_pixel[1] = in_pixel[1],
out_pixel[2] = in_pixel[2],
out_pixel[3] = in_pixel[3];
}
}
while (++x < image->width);
}
while (++y < image->height);
}
}
static int
create_sprite(struct sprite *sprite, int *argc, const char ***argv)
{
/* Read the arguments and create this sprite. The sprite buffer has already
* been allocated. This reads the input PNGs one by one in linear format,
* composes them onto the sprite buffer (the code in the function above)
* then saves the result, converting it on the fly to PNG RGBA 8-bit format.
*/
while (*argc > 0)
{
char tombstone;
int x = 0, y = 0;
if ((*argv)[0][0] == '-' && (*argv)[0][1] == '-')
{
/* The only supported option is --at. */
if (sscanf((*argv)[0], "--at=%d,%d%c", &x, &y, &tombstone) != 2)
break; /* success; caller will parse this option */
++*argv, --*argc;
}
else
{
/* The argument has to be a file name */
png_image image;
image.version = PNG_IMAGE_VERSION;
image.opaque = NULL;
if (png_image_begin_read_from_file(&image, (*argv)[0]))
{
png_uint_16p buffer;
image.format = PNG_FORMAT_LINEAR_RGB_ALPHA;
buffer = malloc(PNG_IMAGE_SIZE(image));
if (buffer != NULL)
{
if (png_image_finish_read(&image, NULL/*background*/, buffer,
0/*row_stride*/,
NULL/*colormap for PNG_FORMAT_FLAG_COLORMAP*/))
{
/* This is the place where the Porter-Duff 'Over' operator
* needs to be done by this code. In fact, any image
* processing required can be done here; the data is in
* the correct format (linear, 16-bit) and source and
* destination are in memory.
*/
sprite_op(sprite, x, y, &image, buffer);
free(buffer);
++*argv, --*argc;
/* And continue to the next argument */
continue;
}
else
{
free(buffer);
fprintf(stderr, "simpleover: read %s: %s\n", (*argv)[0],
image.message);
}
}
else
{
fprintf(stderr, "simpleover: out of memory: %lu bytes\n",
(unsigned long)PNG_IMAGE_SIZE(image));
/* png_image_free must be called if we abort the Simplified API
* read because of a problem detected in this code. If problems
* are detected in the Simplified API it cleans up itself.
*/
png_image_free(&image);
}
}
else
{
/* Failed to read the first argument: */
fprintf(stderr, "simpleover: %s: %s\n", (*argv)[0], image.message);
}
return 0; /* failure */
}
}
/* All the sprite operations have completed successfully. Save the RGBA
* buffer as a PNG using the simplified write API.
*/
sprite->file = tmpfile();
if (sprite->file != NULL)
{
png_image save;
memset(&save, 0, sizeof save);
save.version = PNG_IMAGE_VERSION;
save.opaque = NULL;
save.width = sprite->width;
save.height = sprite->height;
save.format = PNG_FORMAT_LINEAR_RGB_ALPHA;
save.flags = PNG_IMAGE_FLAG_FAST;
save.colormap_entries = 0;
if (png_image_write_to_stdio(&save, sprite->file, 1/*convert_to_8_bit*/,
sprite->buffer, 0/*row_stride*/, NULL/*colormap*/))
{
/* Success; the buffer is no longer needed: */
free(sprite->buffer);
sprite->buffer = NULL;
return 1; /* ok */
}
else
fprintf(stderr, "simpleover: write sprite %s: %s\n", sprite->name,
save.message);
}
else
fprintf(stderr, "simpleover: sprite %s: could not allocate tmpfile: %s\n",
sprite->name, strerror(errno));
return 0; /* fail */
}
static int
add_sprite(png_imagep output, png_bytep out_buf, struct sprite *sprite,
int *argc, const char ***argv)
{
/* Given a --add argument naming this sprite, perform the operations listed
* in the following arguments. The arguments are expected to have the form
* (x,y), which is just an offset at which to add the sprite to the
* output.
*/
while (*argc > 0)
{
char tombstone;
int x, y;
if ((*argv)[0][0] == '-' && (*argv)[0][1] == '-')
return 1; /* success */
if (sscanf((*argv)[0], "%d,%d%c", &x, &y, &tombstone) == 2)
{
/* Now add the new image into the sprite data, but only if it
* will fit.
*/
if (x < 0 || y < 0 ||
(unsigned)/*SAFE*/x >= output->width ||
(unsigned)/*SAFE*/y >= output->height ||
sprite->width > output->width-x ||
sprite->height > output->height-y)
{
fprintf(stderr, "simpleover: sprite %s @ (%d,%d) outside image\n",
sprite->name, x, y);
/* Could just skip this, but for the moment it is an error */
return 0; /* error */
}
else
{
/* Since we know the sprite fits we can just read it into the
* output using the simplified API.
*/
png_image in;
in.version = PNG_IMAGE_VERSION;
rewind(sprite->file);
if (png_image_begin_read_from_stdio(&in, sprite->file))
{
in.format = PNG_FORMAT_RGB; /* force compose */
if (png_image_finish_read(&in, NULL/*background*/,
out_buf + (y*output->width + x)*3/*RGB*/,
output->width*3/*row_stride*/,
NULL/*colormap for PNG_FORMAT_FLAG_COLORMAP*/))
{
++*argv, --*argc;
continue;
}
}
/* The read failed: */
fprintf(stderr, "simpleover: add sprite %s: %s\n", sprite->name,
in.message);
return 0; /* error */
}
}
else
{
fprintf(stderr, "simpleover: --add='%s': invalid position %s\n",
sprite->name, (*argv)[0]);
return 0; /* error */
}
}
return 1; /* ok */
}
static int
simpleover_process(png_imagep output, png_bytep out_buf, int argc,
const char **argv)
{
int result = 1; /* success */
# define csprites 10/*limit*/
# define str(a) #a
int nsprites = 0;
struct sprite sprites[csprites];
while (argc > 0)
{
result = 0; /* fail */
if (strncmp(argv[0], "--sprite=", 9) == 0)
{
char tombstone;
if (nsprites < csprites)
{
int n;
sprites[nsprites].width = sprites[nsprites].height = 0;
sprites[nsprites].name[0] = 0;
n = sscanf(argv[0], "--sprite=%u,%u,%" str(sprite_name_chars) "s%c",
&sprites[nsprites].width, &sprites[nsprites].height,
sprites[nsprites].name, &tombstone);
if ((n == 2 || n == 3) &&
sprites[nsprites].width > 0 && sprites[nsprites].height > 0)
{
size_t buf_size, tmp;
/* Default a name if not given. */
if (sprites[nsprites].name[0] == 0)
sprintf(sprites[nsprites].name, "sprite-%d", nsprites+1);
/* Allocate a buffer for the sprite and calculate the buffer
* size:
*/
buf_size = sizeof (png_uint_16 [4]);
buf_size *= sprites[nsprites].width;
buf_size *= sprites[nsprites].height;
/* This can overflow a (size_t); check for this: */
tmp = buf_size;
tmp /= sprites[nsprites].width;
tmp /= sprites[nsprites].height;
if (tmp == sizeof (png_uint_16 [4]))
{
sprites[nsprites].buffer = malloc(buf_size);
/* This buffer must be initialized to transparent: */
memset(sprites[nsprites].buffer, 0, buf_size);
if (sprites[nsprites].buffer != NULL)
{
sprites[nsprites].file = NULL;
++argv, --argc;
if (create_sprite(sprites+nsprites++, &argc, &argv))
{
result = 1; /* still ok */
continue;
}
break; /* error */
}
}
/* Overflow, or OOM */
fprintf(stderr, "simpleover: %s: sprite too large\n", argv[0]);
break;
}
else
{
fprintf(stderr, "simpleover: %s: invalid sprite (%u,%u)\n",
argv[0], sprites[nsprites].width, sprites[nsprites].height);
break;
}
}
else
{
fprintf(stderr, "simpleover: %s: too many sprites\n", argv[0]);
break;
}
}
else if (strncmp(argv[0], "--add=", 6) == 0)
{
const char *name = argv[0]+6;
int isprite = nsprites;
++argv, --argc;
while (--isprite >= 0)
{
if (strcmp(sprites[isprite].name, name) == 0)
{
if (!add_sprite(output, out_buf, sprites+isprite, &argc, &argv))
goto out; /* error in add_sprite */
break;
}
}
if (isprite < 0) /* sprite not found */
{
fprintf(stderr, "simpleover: --add='%s': sprite not found\n", name);
break;
}
}
else
{
fprintf(stderr, "simpleover: %s: unrecognized operation\n", argv[0]);
break;
}
result = 1; /* ok */
}
/* Clean up the cache of sprites: */
out:
while (--nsprites >= 0)
{
if (sprites[nsprites].buffer != NULL)
free(sprites[nsprites].buffer);
if (sprites[nsprites].file != NULL)
(void)fclose(sprites[nsprites].file);
}
return result;
}
int main(int argc, const char **argv)
{
int result = 1; /* default to fail */
if (argc >= 2)
{
int argi = 2;
const char *output = NULL;
png_image image;
if (argc > 2 && argv[2][0] != '-'/*an operation*/)
{
output = argv[2];
argi = 3;
}
image.version = PNG_IMAGE_VERSION;
image.opaque = NULL;
if (png_image_begin_read_from_file(&image, argv[1]))
{
png_bytep buffer;
image.format = PNG_FORMAT_RGB; /* 24-bit RGB */
buffer = malloc(PNG_IMAGE_SIZE(image));
if (buffer != NULL)
{
png_color background = {0, 0xff, 0}; /* fully saturated green */
if (png_image_finish_read(&image, &background, buffer,
0/*row_stride*/, NULL/*colormap for PNG_FORMAT_FLAG_COLORMAP */))
{
/* At this point png_image_finish_read has cleaned up the
* allocated data in png_image, and only the buffer needs to be
* freed.
*
* Perform the remaining operations:
*/
if (simpleover_process(&image, buffer, argc-argi, argv+argi))
{
/* Write the output: */
if ((output != NULL &&
png_image_write_to_file(&image, output,
0/*convert_to_8bit*/, buffer, 0/*row_stride*/,
NULL/*colormap*/)) ||
(output == NULL &&
png_image_write_to_stdio(&image, stdout,
0/*convert_to_8bit*/, buffer, 0/*row_stride*/,
NULL/*colormap*/)))
result = 0;
else
fprintf(stderr, "simpleover: write %s: %s\n",
output == NULL ? "stdout" : output, image.message);
}
/* else simpleover_process writes an error message */
}
else
fprintf(stderr, "simpleover: read %s: %s\n", argv[1],
image.message);
free(buffer);
}
else
{
fprintf(stderr, "simpleover: out of memory: %lu bytes\n",
(unsigned long)PNG_IMAGE_SIZE(image));
/* This is the only place where a 'free' is required; libpng does
* the cleanup on error and success, but in this case we couldn't
* complete the read because of running out of memory.
*/
png_image_free(&image);
}
}
else
{
/* Failed to read the first argument: */
fprintf(stderr, "simpleover: %s: %s\n", argv[1], image.message);
}
}
else
{
/* Usage message */
fprintf(stderr,
"simpleover: usage: simpleover background.png [output.png]\n"
" Output 'background.png' as a 24-bit RGB PNG file in 'output.png'\n"
" or, if not given, stdout. 'background.png' will be composited\n"
" on fully saturated green.\n"
"\n"
" Optionally, before output, process additional PNG files:\n"
"\n"
" --sprite=width,height,name {[--at=x,y] {sprite.png}}\n"
" Produce a transparent sprite of size (width,height) and with\n"
" name 'name'.\n"
" For each sprite.png composite it using a Porter-Duff 'Over'\n"
" operation at offset (x,y) in the sprite (defaulting to (0,0)).\n"
" Input PNGs will be truncated to the area of the sprite.\n"
"\n"
" --add='name' {x,y}\n"
" Optionally, before output, composite a sprite, 'name', which\n"
" must have been previously produced using --sprite, at each\n"
" offset (x,y) in the output image. Each sprite must fit\n"
" completely within the output image.\n"
"\n"
" PNG files are processed in the order they occur on the command\n"
" line and thus the first PNG processed appears as the bottommost\n"
" in the output image.\n");
}
return result;
}

View File

@@ -258,7 +258,7 @@ make_four_random_bytes(png_uint_32* seed, png_bytep bytes)
make_random_bytes(seed, bytes, 4);
}
#ifdef PNG_READ_SUPPORTED
#if defined PNG_READ_SUPPORTED || defined PNG_WRITE_tRNS_SUPPORTED
static void
randomize(void *pv, size_t size)
{
@@ -267,7 +267,9 @@ randomize(void *pv, size_t size)
}
#define RANDOMIZE(this) randomize(&(this), sizeof (this))
#endif /* READ || WRITE_tRNS */
#ifdef PNG_READ_SUPPORTED
static unsigned int
random_mod(unsigned int max)
{
@@ -295,7 +297,8 @@ random_choice(void)
/* A numeric ID based on PNG file characteristics. The 'do_interlace' field
* simply records whether pngvalid did the interlace itself or whether it
* was done by libpng. Width and height must be less than 256. 'palette' is an
* index of the palette to use for formats with a palette (0 otherwise.)
* index of the palette to use for formats with a palette otherwise a boolean
* indicating if a tRNS chunk was generated.
*/
#define FILEID(col, depth, palette, interlace, width, height, do_interlace) \
((png_uint_32)((col) + ((depth)<<3) + ((palette)<<8) + ((interlace)<<13) + \
@@ -316,12 +319,16 @@ standard_name(char *buffer, size_t bufsize, size_t pos, png_byte colour_type,
png_uint_32 w, png_uint_32 h, int do_interlace)
{
pos = safecat(buffer, bufsize, pos, colour_types[colour_type]);
if (npalette > 0)
if (colour_type == 3) /* must have a palette */
{
pos = safecat(buffer, bufsize, pos, "[");
pos = safecatn(buffer, bufsize, pos, npalette);
pos = safecat(buffer, bufsize, pos, "]");
}
else if (npalette != 0)
pos = safecat(buffer, bufsize, pos, "+tRNS");
pos = safecat(buffer, bufsize, pos, " ");
pos = safecatn(buffer, bufsize, pos, bit_depth);
pos = safecat(buffer, bufsize, pos, " bit");
@@ -378,25 +385,32 @@ standard_name_from_id(char *buffer, size_t bufsize, size_t pos, png_uint_32 id)
static int
next_format(png_bytep colour_type, png_bytep bit_depth,
unsigned int* palette_number, int no_low_depth_gray)
unsigned int* palette_number, int low_depth_gray, int tRNS)
{
if (*bit_depth == 0)
{
*colour_type = 0;
if (no_low_depth_gray)
*bit_depth = 8;
else
if (low_depth_gray)
*bit_depth = 1;
else
*bit_depth = 8;
*palette_number = 0;
return 1;
}
if (*colour_type == 3)
if (*colour_type < 4/*no alpha channel*/)
{
/* Add multiple palettes for colour type 3. */
if (++*palette_number < PALETTE_COUNT(*bit_depth))
/* Add multiple palettes for colour type 3, one image with tRNS
* and one without for other non-alpha formats:
*/
unsigned int pn = ++*palette_number;
png_byte ct = *colour_type;
if (((ct == 0/*GRAY*/ || ct/*RGB*/ == 2) && tRNS && pn < 2) ||
(ct == 3/*PALETTE*/ && pn < PALETTE_COUNT(*bit_depth)))
return 1;
/* No: next bit depth */
*palette_number = 0;
}
@@ -1959,6 +1973,7 @@ typedef struct png_modifier
/* Run tests on reading with a combination of transforms, */
unsigned int test_transform :1;
unsigned int test_tRNS :1; /* Includes tRNS images */
/* When to use the use_input_precision option, this controls the gamma
* validation code checks. If set any value that is within the transformed
@@ -1990,6 +2005,16 @@ typedef struct png_modifier
unsigned int test_gamma_expand16 :1;
unsigned int test_exhaustive :1;
/* Whether or not to run the low-bit-depth grayscale tests. This fail on
* gamma images in some cases because of gross inaccuracies in the grayscale
* gamma handling for low bit depth.
*/
unsigned int test_lbg :1;
unsigned int test_lbg_gamma_threshold :1;
unsigned int test_lbg_gamma_transform :1;
unsigned int test_lbg_gamma_sbit :1;
unsigned int test_lbg_gamma_composition :1;
unsigned int log :1; /* Log max error */
/* Buffer information, the buffer size limits the size of the chunks that can
@@ -2042,6 +2067,11 @@ modifier_init(png_modifier *pm)
pm->test_standard = 0;
pm->test_size = 0;
pm->test_transform = 0;
# ifdef PNG_WRITE_tRNS_SUPPORTED
pm->test_tRNS = 1;
# else
pm->test_tRNS = 0;
# endif
pm->use_input_precision = 0;
pm->use_input_precision_sbit = 0;
pm->use_input_precision_16to8 = 0;
@@ -2054,6 +2084,11 @@ modifier_init(png_modifier *pm)
pm->test_gamma_background = 0;
pm->test_gamma_alpha_mode = 0;
pm->test_gamma_expand16 = 0;
pm->test_lbg = 1;
pm->test_lbg_gamma_threshold = 1;
pm->test_lbg_gamma_transform = 1;
pm->test_lbg_gamma_sbit = 1;
pm->test_lbg_gamma_composition = 1;
pm->test_exhaustive = 0;
pm->log = 0;
@@ -3192,6 +3227,45 @@ init_standard_palette(png_store *ps, png_structp pp, png_infop pi, int npalette,
}
}
#ifdef PNG_WRITE_tRNS_SUPPORTED
static void
set_random_tRNS(png_structp pp, png_infop pi, PNG_CONST png_byte colour_type,
PNG_CONST int bit_depth)
{
/* To make this useful the tRNS color needs to match at least one pixel.
* Random values are fine for gray, including the 16-bit case where we know
* that the test image contains all the gray values. For RGB we need more
* method as only 65536 different RGB values are generated.
*/
png_color_16 tRNS;
const png_uint_16 mask = (png_uint_16)((1U << bit_depth)-1);
RANDOMIZE(tRNS);
if (colour_type & 2/*RGB*/)
{
if (bit_depth == 8)
{
tRNS.blue = tRNS.red ^ tRNS.green;
tRNS.red &= mask;
tRNS.green &= mask;
tRNS.blue &= mask;
}
else /* bit_depth == 16 */
{
tRNS.green = (png_uint_16)(tRNS.red * 257);
tRNS.blue = (png_uint_16)(tRNS.green * 17);
}
}
else
tRNS.gray &= mask;
png_set_tRNS(pp, pi, NULL, 0, &tRNS);
}
#endif
/* The number of passes is related to the interlace type. There was no libpng
* API to determine this prior to 1.5, so we need an inquiry function:
*/
@@ -3525,6 +3599,11 @@ make_transform_image(png_store* PNG_CONST ps, png_byte PNG_CONST colour_type,
if (colour_type == 3) /* palette */
init_standard_palette(ps, pp, pi, 1U << bit_depth, 1/*do tRNS*/);
# ifdef PNG_WRITE_tRNS_SUPPORTED
else if (palette_number)
set_random_tRNS(pp, pi, colour_type, bit_depth);
# endif
png_write_info(pp, pi);
if (png_get_rowbytes(pp, pi) !=
@@ -3598,19 +3677,20 @@ make_transform_image(png_store* PNG_CONST ps, png_byte PNG_CONST colour_type,
}
static void
make_transform_images(png_store *ps)
make_transform_images(png_modifier *pm)
{
png_byte colour_type = 0;
png_byte bit_depth = 0;
unsigned int palette_number = 0;
/* This is in case of errors. */
safecat(ps->test, sizeof ps->test, 0, "make standard images");
safecat(pm->this.test, sizeof pm->this.test, 0, "make standard images");
/* Use next_format to enumerate all the combinations we test, including
* generating multiple low bit depth palette images.
* generating multiple low bit depth palette images. Non-A images (palette
* and direct) are created with and without tRNS chunks.
*/
while (next_format(&colour_type, &bit_depth, &palette_number, 0))
while (next_format(&colour_type, &bit_depth, &palette_number, 1, 1))
{
int interlace_type;
@@ -3621,7 +3701,7 @@ make_transform_images(png_store *ps)
standard_name(name, sizeof name, 0, colour_type, bit_depth,
palette_number, interlace_type, 0, 0, 0);
make_transform_image(ps, colour_type, bit_depth, palette_number,
make_transform_image(&pm->this, colour_type, bit_depth, palette_number,
interlace_type, name);
}
}
@@ -4287,6 +4367,7 @@ typedef struct standard_display
size_t cbRow; /* Bytes in a row of the output image */
int do_interlace; /* Do interlacing internally */
int is_transparent; /* Transparency information was present. */
int has_tRNS; /* color type GRAY or RGB with a tRNS chunk. */
int speed; /* Doing a speed test */
int use_update_info;/* Call update_info, not start_image */
struct
@@ -4619,14 +4700,14 @@ standard_info_part1(standard_display *dp, png_structp pp, png_infop pi)
case 0:
dp->transparent.red = dp->transparent.green = dp->transparent.blue =
trans_color->gray;
dp->is_transparent = 1;
dp->has_tRNS = 1;
break;
case 2:
dp->transparent.red = trans_color->red;
dp->transparent.green = trans_color->green;
dp->transparent.blue = trans_color->blue;
dp->is_transparent = 1;
dp->has_tRNS = 1;
break;
case 3:
@@ -5518,7 +5599,7 @@ image_pixel_add_alpha(image_pixel *this, PNG_CONST standard_display *display)
if (this->colour_type == PNG_COLOR_TYPE_GRAY)
{
if (this->bit_depth < 8)
this->bit_depth = 8;
this->bit_depth = this->sample_depth = 8;
if (this->have_tRNS)
{
@@ -5553,9 +5634,11 @@ image_pixel_add_alpha(image_pixel *this, PNG_CONST standard_display *display)
this->alphaf = 0;
else
this->alphaf = 1;
this->colour_type = PNG_COLOR_TYPE_RGB_ALPHA;
}
else
this->alphaf = 1;
this->colour_type = PNG_COLOR_TYPE_RGB_ALPHA;
}
/* The error in the alpha is zero and the sBIT value comes from the
@@ -5848,8 +5931,9 @@ transform_info_imp(transform_display *dp, png_structp pp, png_infop pi)
/* If png_set_filler is in action then fake the output color type to include
* an alpha channel where appropriate.
*/
if (dp->output_bit_depth >= 8 && (dp->output_colour_type == PNG_COLOR_TYPE_RGB ||
dp->output_colour_type == PNG_COLOR_TYPE_GRAY) && dp->this.filler)
if (dp->output_bit_depth >= 8 &&
(dp->output_colour_type == PNG_COLOR_TYPE_RGB ||
dp->output_colour_type == PNG_COLOR_TYPE_GRAY) && dp->this.filler)
dp->output_colour_type |= 4;
/* Validate the combination of colour type and bit depth that we are getting
@@ -6372,6 +6456,13 @@ image_transform_png_set_tRNS_to_alpha_set(PNG_CONST image_transform *this,
transform_display *that, png_structp pp, png_infop pi)
{
png_set_tRNS_to_alpha(pp);
/* If there was a tRNS chunk that would get expanded and add an alpha
* channel is_transparent must be updated:
*/
if (that->this.has_tRNS)
that->this.is_transparent = 1;
this->next->set(this->next, that, pp, pi);
}
@@ -6430,6 +6521,7 @@ image_transform_png_set_gray_to_rgb_set(PNG_CONST image_transform *this,
transform_display *that, png_structp pp, png_infop pi)
{
png_set_gray_to_rgb(pp);
/* NOTE: this doesn't result in tRNS expansion. */
this->next->set(this->next, that, pp, pi);
}
@@ -6489,6 +6581,10 @@ image_transform_png_set_expand_set(PNG_CONST image_transform *this,
transform_display *that, png_structp pp, png_infop pi)
{
png_set_expand(pp);
if (that->this.has_tRNS)
that->this.is_transparent = 1;
this->next->set(this->next, that, pp, pi);
}
@@ -6539,6 +6635,7 @@ image_transform_png_set_expand_gray_1_2_4_to_8_set(
png_infop pi)
{
png_set_expand_gray_1_2_4_to_8(pp);
/* NOTE: don't expect this to expand tRNS */
this->next->set(this->next, that, pp, pi);
}
@@ -6570,6 +6667,11 @@ image_transform_png_set_expand_16_set(PNG_CONST image_transform *this,
transform_display *that, png_structp pp, png_infop pi)
{
png_set_expand_16(pp);
/* NOTE: at present libpng does SET_EXPAND as well, so tRNS is expanded. */
if (that->this.has_tRNS)
that->this.is_transparent = 1;
this->next->set(this->next, that, pp, pi);
}
@@ -6940,14 +7042,14 @@ image_transform_png_set_rgb_to_gray_ini(PNG_CONST image_transform *this,
* When DIGITIZE is set because a pre-1.7 version of libpng is being
* tested allow a bigger slack.
*
* NOTE: this magic number was determined by experiment to be 1.25.
* There's no great merit to the value below, however it only affects
* the limit used for checking for internal calculation errors, not
* the actual limit imposed by pngvalid on the output errors.
* NOTE: this magic number was determined by experiment to be about
* 1.263. There's no great merit to the value below, however it only
* affects the limit used for checking for internal calculation errors,
* not the actual limit imposed by pngvalid on the output errors.
*/
that->pm->limit += pow(
# if DIGITIZE
1.25
1.3
# else
1.0
# endif
@@ -7111,7 +7213,8 @@ image_transform_png_set_rgb_to_gray_mod(PNG_CONST image_transform *this,
const unsigned int sample_depth = that->sample_depth;
const unsigned int calc_depth = (pm->assume_16_bit_calculations ? 16 :
sample_depth);
const unsigned int gamma_depth = (sample_depth == 16 ? 16 :
const unsigned int gamma_depth = (sample_depth == 16 ?
PNG_MAX_GAMMA_8 :
(pm->assume_16_bit_calculations ? PNG_MAX_GAMMA_8 : sample_depth));
int isgray;
double r, g, b;
@@ -7125,56 +7228,73 @@ image_transform_png_set_rgb_to_gray_mod(PNG_CONST image_transform *this,
* will be identical after this operation if there is only one
* transform, feel free to delete the png_error checks on this below in
* the future (this is just me trying to ensure it works!)
*
* Interval arithmetic is exact, but to implement it it must be
* possible to control the floating point implementation rounding mode.
* This cannot be done in ANSI-C, so instead I reduce the 'lo' values
* by DBL_EPSILON and increase the 'hi' values by the same.
*/
# define DD(v,d,r) (digitize(v*(1-DBL_EPSILON), d, r) * (1-DBL_EPSILON))
# define DU(v,d,r) (digitize(v*(1+DBL_EPSILON), d, r) * (1+DBL_EPSILON))
r = rlo = rhi = that->redf;
rlo -= that->rede;
rlo = digitize(rlo, calc_depth, 1/*round*/);
rlo = DD(rlo, calc_depth, 1/*round*/);
rhi += that->rede;
rhi = digitize(rhi, calc_depth, 1/*round*/);
rhi = DU(rhi, calc_depth, 1/*round*/);
g = glo = ghi = that->greenf;
glo -= that->greene;
glo = digitize(glo, calc_depth, 1/*round*/);
glo = DD(glo, calc_depth, 1/*round*/);
ghi += that->greene;
ghi = digitize(ghi, calc_depth, 1/*round*/);
ghi = DU(ghi, calc_depth, 1/*round*/);
b = blo = bhi = that->bluef;
blo -= that->bluee;
blo = digitize(blo, calc_depth, 1/*round*/);
blo = DD(blo, calc_depth, 1/*round*/);
bhi += that->greene;
bhi = digitize(bhi, calc_depth, 1/*round*/);
bhi = DU(bhi, calc_depth, 1/*round*/);
isgray = r==g && g==b;
if (data.gamma != 1)
{
PNG_CONST double power = 1/data.gamma;
PNG_CONST double abse = calc_depth == 16 ? .5/65535 : .5/255;
PNG_CONST double abse = .5/(sample_depth == 16 ? 65535 : 255);
/* 'abse' is the absolute error permitted in linear calculations. It
* is used here to capture the error permitted in the handling
* (undoing) of the gamma encoding. Once again digitization occurs
* to handle the upper and lower bounds of the values. This is
* where the real errors are introduced.
/* If a gamma calculation is done it is done using lookup tables of
* precision gamma_depth, so the already digitized value above may
* need to be further digitized here.
*/
if (gamma_depth != calc_depth)
{
rlo = DD(rlo, gamma_depth, 0/*truncate*/);
rhi = DU(rhi, gamma_depth, 0/*truncate*/);
glo = DD(glo, gamma_depth, 0/*truncate*/);
ghi = DU(ghi, gamma_depth, 0/*truncate*/);
blo = DD(blo, gamma_depth, 0/*truncate*/);
bhi = DU(bhi, gamma_depth, 0/*truncate*/);
}
/* 'abse' is the error in the gamma table calculation itself. */
r = pow(r, power);
rlo = digitize(pow(rlo, power)-abse, calc_depth, 1);
rhi = digitize(pow(rhi, power)+abse, calc_depth, 1);
rlo = DD(pow(rlo, power)-abse, calc_depth, 1);
rhi = DU(pow(rhi, power)+abse, calc_depth, 1);
g = pow(g, power);
glo = digitize(pow(glo, power)-abse, calc_depth, 1);
ghi = digitize(pow(ghi, power)+abse, calc_depth, 1);
glo = DD(pow(glo, power)-abse, calc_depth, 1);
ghi = DU(pow(ghi, power)+abse, calc_depth, 1);
b = pow(b, power);
blo = digitize(pow(blo, power)-abse, calc_depth, 1);
bhi = digitize(pow(bhi, power)+abse, calc_depth, 1);
blo = DD(pow(blo, power)-abse, calc_depth, 1);
bhi = DU(pow(bhi, power)+abse, calc_depth, 1);
}
/* Now calculate the actual gray values. Although the error in the
* coefficients depends on whether they were specified on the command
* line (in which case truncation to 15 bits happened) or not (rounding
* was used) the maxium error in an individual coefficient is always
* 1/32768, because even in the rounding case the requirement that
* 2/32768, because even in the rounding case the requirement that
* coefficients add up to 32768 can cause a larger rounding error.
*
* The only time when rounding doesn't occur in 1.5.5 and later is when
@@ -7185,19 +7305,19 @@ image_transform_png_set_rgb_to_gray_mod(PNG_CONST image_transform *this,
{
PNG_CONST int do_round = data.gamma != 1 || calc_depth == 16;
PNG_CONST double ce = 1. / 32768;
PNG_CONST double ce = 2. / 32768;
graylo = digitize(rlo * (data.red_coefficient-ce) +
graylo = DD(rlo * (data.red_coefficient-ce) +
glo * (data.green_coefficient-ce) +
blo * (data.blue_coefficient-ce), gamma_depth, do_round);
if (graylo <= 0)
graylo = 0;
blo * (data.blue_coefficient-ce), calc_depth, do_round);
if (graylo > gray) /* always accept the right answer */
graylo = gray;
grayhi = digitize(rhi * (data.red_coefficient+ce) +
grayhi = DU(rhi * (data.red_coefficient+ce) +
ghi * (data.green_coefficient+ce) +
bhi * (data.blue_coefficient+ce), gamma_depth, do_round);
if (grayhi >= 1)
grayhi = 1;
bhi * (data.blue_coefficient+ce), calc_depth, do_round);
if (grayhi < gray)
grayhi = gray;
}
/* And invert the gamma. */
@@ -7205,11 +7325,25 @@ image_transform_png_set_rgb_to_gray_mod(PNG_CONST image_transform *this,
{
PNG_CONST double power = data.gamma;
/* And this happens yet again, shifting the values once more. */
if (gamma_depth != sample_depth)
{
rlo = DD(rlo, gamma_depth, 0/*truncate*/);
rhi = DU(rhi, gamma_depth, 0/*truncate*/);
glo = DD(glo, gamma_depth, 0/*truncate*/);
ghi = DU(ghi, gamma_depth, 0/*truncate*/);
blo = DD(blo, gamma_depth, 0/*truncate*/);
bhi = DU(bhi, gamma_depth, 0/*truncate*/);
}
gray = pow(gray, power);
graylo = digitize(pow(graylo, power), sample_depth, 1);
grayhi = digitize(pow(grayhi, power), sample_depth, 1);
graylo = DD(pow(graylo, power), sample_depth, 1);
grayhi = DU(pow(grayhi, power), sample_depth, 1);
}
# undef DD
# undef DU
/* Now the error can be calculated.
*
* If r==g==b because there is no overall gamma correction libpng
@@ -7260,16 +7394,28 @@ image_transform_png_set_rgb_to_gray_mod(PNG_CONST image_transform *this,
{
/* There is no need to do the conversions to and from linear space,
* so the calculation should be a lot more accurate. There is a
* built in 1/32768 error in the coefficients because they only have
* 15 bits and are adjusted to make sure they add up to 32768, so
* the result may have an additional error up to 1/32768. (Note
* that adding the 1/32768 here avoids needing to increase the
* global error limits to take this into account.)
* built in error in the coefficients because they only have 15 bits
* and are adjusted to make sure they add up to 32768. This
* involves a integer calculation with truncation of the form:
*
* ((int)(coefficient * 100000) * 32768)/100000
*
* This is done to the red and green coefficients (the ones
* provided to the API) then blue is calculated from them so the
* result adds up to 32768. In the worst case this can result in
* a -1 error in red and green and a +2 error in blue. Consequently
* the worst case in the calculation below is 2/32768 error.
*
* TODO: consider fixing this in libpng by rounding the calculation
* limiting the error to 1/32768.
*
* Handling this by adding 2/32768 here avoids needing to increase
* the global error limits to take this into account.)
*/
gray = r * data.red_coefficient + g * data.green_coefficient +
b * data.blue_coefficient;
err = re * data.red_coefficient + ge * data.green_coefficient +
be * data.blue_coefficient + 1./32768 + gray * 5 * DBL_EPSILON;
be * data.blue_coefficient + 2./32768 + gray * 5 * DBL_EPSILON;
}
else
@@ -7304,7 +7450,7 @@ image_transform_png_set_rgb_to_gray_mod(PNG_CONST image_transform *this,
* previously added input quantization error at this point.
*/
gray = r * data.red_coefficient + g * data.green_coefficient +
b * data.blue_coefficient - 1./32768 - out_qe;
b * data.blue_coefficient - 2./32768 - out_qe;
if (gray <= 0)
gray = 0;
else
@@ -7314,7 +7460,7 @@ image_transform_png_set_rgb_to_gray_mod(PNG_CONST image_transform *this,
}
grayhi = rhi * data.red_coefficient + ghi * data.green_coefficient +
bhi * data.blue_coefficient + 1./32768 + out_qe;
bhi * data.blue_coefficient + 2./32768 + out_qe;
grayhi *= (1 + 6 * DBL_EPSILON);
if (grayhi >= 1)
grayhi = 1;
@@ -7429,6 +7575,9 @@ image_transform_png_set_background_set(PNG_CONST image_transform *this,
else
{
if (that->this.has_tRNS)
that->this.is_transparent = 1;
bit_depth = that->this.bit_depth;
expand = 1;
}
@@ -7506,14 +7655,14 @@ image_transform_png_set_background_mod(PNG_CONST image_transform *this,
/* Remove the alpha type and set the alpha (not in that order.) */
that->alphaf = 1;
that->alphae = 0;
if (that->colour_type == PNG_COLOR_TYPE_RGB_ALPHA)
that->colour_type = PNG_COLOR_TYPE_RGB;
else if (that->colour_type == PNG_COLOR_TYPE_GRAY_ALPHA)
that->colour_type = PNG_COLOR_TYPE_GRAY;
/* PNG_COLOR_TYPE_PALETTE is not changed */
}
if (that->colour_type == PNG_COLOR_TYPE_RGB_ALPHA)
that->colour_type = PNG_COLOR_TYPE_RGB;
else if (that->colour_type == PNG_COLOR_TYPE_GRAY_ALPHA)
that->colour_type = PNG_COLOR_TYPE_GRAY;
/* PNG_COLOR_TYPE_PALETTE is not changed */
this->next->mod(this->next, that, pp, display);
}
@@ -8301,7 +8450,8 @@ perform_transform_test(png_modifier *pm)
png_byte bit_depth = 0;
unsigned int palette_number = 0;
while (next_format(&colour_type, &bit_depth, &palette_number, 0))
while (next_format(&colour_type, &bit_depth, &palette_number, pm->test_lbg,
pm->test_tRNS))
{
png_uint_32 counter = 0;
size_t base_pos;
@@ -8604,7 +8754,9 @@ init_validate_info(validate_info *vi, gamma_display *dp, png_const_structp pp,
vi->outlog = outlog(dp->pm, in_depth, out_depth);
if ((dp->this.colour_type & PNG_COLOR_MASK_ALPHA) != 0 ||
(dp->this.colour_type == 3 && dp->this.is_transparent))
(dp->this.colour_type == 3 && dp->this.is_transparent) ||
((dp->this.colour_type == 0 || dp->this.colour_type == 2) &&
dp->this.has_tRNS))
{
vi->do_background = dp->do_background;
@@ -8634,7 +8786,7 @@ init_validate_info(validate_info *vi, gamma_display *dp, png_const_structp pp,
vi->background_blue = b;
}
}
else
else /* Do not expect any background processing */
vi->do_background = 0;
if (vi->do_background == 0)
@@ -9350,6 +9502,7 @@ gamma_image_validate(gamma_display *dp, png_const_structp pp,
png_uint_32 y;
PNG_CONST store_palette_entry *in_palette = dp->this.palette;
PNG_CONST int in_is_transparent = dp->this.is_transparent;
int process_tRNS;
int out_npalette = -1;
int out_is_transparent = 0; /* Just refers to the palette case */
store_palette out_palette;
@@ -9365,6 +9518,7 @@ gamma_image_validate(gamma_display *dp, png_const_structp pp,
processing = (vi.gamma_correction > 0 && !dp->threshold_test)
|| in_bd != out_bd || in_ct != out_ct || vi.do_background;
process_tRNS = dp->this.has_tRNS && vi.do_background;
/* TODO: FIX THIS: MAJOR BUG! If the transformations all happen inside
* the palette there is no way of finding out, because libpng fails to
@@ -9403,8 +9557,8 @@ gamma_image_validate(gamma_display *dp, png_const_structp pp,
/* Handle input alpha - png_set_background will cause the output
* alpha to disappear so there is nothing to check.
*/
if ((in_ct & PNG_COLOR_MASK_ALPHA) != 0 || (in_ct == 3 &&
in_is_transparent))
if ((in_ct & PNG_COLOR_MASK_ALPHA) != 0 ||
(in_ct == 3 && in_is_transparent))
{
PNG_CONST unsigned int input_alpha = in_ct == 3 ?
dp->this.palette[in_index].alpha :
@@ -9436,6 +9590,35 @@ gamma_image_validate(gamma_display *dp, png_const_structp pp,
}
}
else if (process_tRNS)
{
/* alpha needs to be set appropriately for this pixel, it is
* currently 1 and needs to be 0 for an input pixel which matches
* the values in tRNS.
*/
switch (in_ct)
{
case 0: /* gray */
if (sample(std, in_ct, in_bd, x, 0, 0, 0) ==
dp->this.transparent.red)
alpha = 0;
break;
case 2: /* RGB */
if (sample(std, in_ct, in_bd, x, 0, 0, 0) ==
dp->this.transparent.red &&
sample(std, in_ct, in_bd, x, 1, 0, 0) ==
dp->this.transparent.green &&
sample(std, in_ct, in_bd, x, 2, 0, 0) ==
dp->this.transparent.blue)
alpha = 0;
break;
default:
break;
}
}
/* Handle grayscale or RGB components. */
if ((in_ct & PNG_COLOR_MASK_COLOR) == 0) /* grayscale */
(void)gamma_component_validate("gray", &vi,
@@ -9545,7 +9728,7 @@ gamma_test(png_modifier *pmIn, PNG_CONST png_byte colour_typeIn,
modification_reset(d.pm->modifications);
/* Get a png_struct for writing the image. */
/* Get a png_struct for reading the image. */
pp = set_modifier_for_read(d.pm, &pi, d.this.id, name);
standard_palette_init(&d.this);
@@ -9684,9 +9867,13 @@ perform_gamma_threshold_tests(png_modifier *pm)
/* Don't test more than one instance of each palette - it's pointless, in
* fact this test is somewhat excessive since libpng doesn't make this
* decision based on colour type or bit depth!
*
* CHANGED: now test two palettes and, as a side effect, images with and
* without tRNS.
*/
while (next_format(&colour_type, &bit_depth, &palette_number, 1/*gamma*/))
if (palette_number == 0)
while (next_format(&colour_type, &bit_depth, &palette_number,
pm->test_lbg_gamma_threshold, pm->test_tRNS))
if (palette_number < 2)
{
double test_gamma = 1.0;
while (test_gamma >= .4)
@@ -9746,7 +9933,8 @@ static void perform_gamma_transform_tests(png_modifier *pm)
png_byte bit_depth = 0;
unsigned int palette_number = 0;
while (next_format(&colour_type, &bit_depth, &palette_number, 1/*gamma*/))
while (next_format(&colour_type, &bit_depth, &palette_number,
pm->test_lbg_gamma_transform, pm->test_tRNS))
{
unsigned int i, j;
@@ -9776,7 +9964,8 @@ static void perform_gamma_sbit_tests(png_modifier *pm)
png_byte colour_type = 0, bit_depth = 0;
unsigned int npalette = 0;
while (next_format(&colour_type, &bit_depth, &npalette, 1/*gamma*/))
while (next_format(&colour_type, &bit_depth, &npalette,
pm->test_lbg_gamma_sbit, pm->test_tRNS))
if ((colour_type & PNG_COLOR_MASK_ALPHA) == 0 &&
((colour_type == 3 && sbit < 8) ||
(colour_type != 3 && sbit < bit_depth)))
@@ -9967,8 +10156,17 @@ static void gamma_composition_test(png_modifier *pm,
}
background.index = 193; /* rgb(193,193,193) to detect errors */
if (!(colour_type & PNG_COLOR_MASK_COLOR))
{
/* Because, currently, png_set_background is always called with
* 'need_expand' false in this case and because the gamma test itself
* doesn't cause an expand to 8-bit for lower bit depths the colour must
* be reduced to the correct range.
*/
if (bit_depth < 8)
background.gray &= (png_uint_16)((1U << bit_depth)-1);
/* Grayscale input, we do not convert to RGB (TBD), so we must set the
* background to gray - else libpng seems to fail.
*/
@@ -10017,9 +10215,18 @@ perform_gamma_composition_tests(png_modifier *pm, int do_background,
/* Skip the non-alpha cases - there is no setting of a transparency colour at
* present.
*
* TODO: incorrect; the palette case sets tRNS and, now RGB and gray do,
* however the palette case fails miserably so is commented out below.
*/
while (next_format(&colour_type, &bit_depth, &palette_number, 1/*gamma*/))
if ((colour_type & PNG_COLOR_MASK_ALPHA) != 0)
while (next_format(&colour_type, &bit_depth, &palette_number,
pm->test_lbg_gamma_composition, pm->test_tRNS))
if ((colour_type & PNG_COLOR_MASK_ALPHA) != 0
#if 0 /* TODO: FIXME */
/*TODO: FIXME: this should work */
|| colour_type == 3
#endif
|| (colour_type != 3 && palette_number != 0))
{
unsigned int i, j;
@@ -10751,6 +10958,18 @@ int main(int argc, char **argv)
pm.ngammas = ARRAY_SIZE(gammas);
pm.ngamma_tests = 0; /* default to off */
/* Low bit depth gray images don't do well in the gamma tests, until
* this is fixed turn them off for some gamma cases:
*/
# ifdef PNG_WRITE_tRNS_SUPPORTED
pm.test_tRNS = 1;
# endif
pm.test_lbg = 0;
pm.test_lbg_gamma_threshold = 1;
pm.test_lbg_gamma_transform = 0/*PNG_LIBPNG_VER >= 10700*/;
pm.test_lbg_gamma_sbit = 1;
pm.test_lbg_gamma_composition = 0;
/* And the test encodings */
pm.encodings = test_encodings;
pm.nencodings = ARRAY_SIZE(test_encodings);
@@ -10863,7 +11082,7 @@ int main(int argc, char **argv)
pm.test_gamma_transform = 1;
pm.test_gamma_sbit = 1;
pm.test_gamma_scale16 = 1;
pm.test_gamma_background = 1;
pm.test_gamma_background = 1; /* composition */
pm.test_gamma_alpha_mode = 1;
}
@@ -10912,6 +11131,24 @@ int main(int argc, char **argv)
else if (strcmp(*argv, "--noexpand16") == 0)
pm.test_gamma_expand16 = 0;
else if (strcmp(*argv, "--low-depth-gray") == 0)
pm.test_lbg = pm.test_lbg_gamma_threshold =
pm.test_lbg_gamma_transform = pm.test_lbg_gamma_sbit =
pm.test_lbg_gamma_composition = 1;
else if (strcmp(*argv, "--nolow-depth-gray") == 0)
pm.test_lbg = pm.test_lbg_gamma_threshold =
pm.test_lbg_gamma_transform = pm.test_lbg_gamma_sbit =
pm.test_lbg_gamma_composition = 0;
# ifdef PNG_WRITE_tRNS_SUPPORTED
else if (strcmp(*argv, "--tRNS") == 0)
pm.test_tRNS = 1;
# endif
else if (strcmp(*argv, "--notRNS") == 0)
pm.test_tRNS = 0;
else if (strcmp(*argv, "--more-gammas") == 0)
pm.ngamma_tests = 3U;
@@ -11102,7 +11339,7 @@ int main(int argc, char **argv)
Try
{
/* Make useful base images */
make_transform_images(&pm.this);
make_transform_images(&pm);
/* Perform the standard and gamma tests. */
if (pm.test_standard)

864
contrib/tools/genpng.c Normal file
View File

@@ -0,0 +1,864 @@
/*- genpng
*
* COPYRIGHT: Written by John Cunningham Bowler, 2015.
* 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.
*
* 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"
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;
}
/* 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;
}

View File

@@ -71,8 +71,8 @@
* with older builds.
*/
#if ZLIB_VERNUM < 0x1260
# define PNGZ_MSG_CAST(s) png_constcast(char*,s)
# define PNGZ_INPUT_CAST(b) png_constcast(png_bytep,b)
# define PNGZ_MSG_CAST(s) constcast(char*,s)
# define PNGZ_INPUT_CAST(b) constcast(png_bytep,b)
#else
# define PNGZ_MSG_CAST(s) (s)
# define PNGZ_INPUT_CAST(b) (b)
@@ -86,17 +86,17 @@
/* Copied from pngpriv.h */
#ifdef __cplusplus
# define png_voidcast(type, value) static_cast<type>(value)
# define png_constcast(type, value) const_cast<type>(value)
# define png_aligncast(type, value) \
# define voidcast(type, value) static_cast<type>(value)
# define constcast(type, value) const_cast<type>(value)
# define aligncast(type, value) \
static_cast<type>(static_cast<void*>(value))
# define png_aligncastconst(type, value) \
# define aligncastconst(type, value) \
static_cast<type>(static_cast<const void*>(value))
#else
# define png_voidcast(type, value) (value)
# define png_constcast(type, value) ((type)(value))
# define png_aligncast(type, value) ((void*)(value))
# define png_aligncastconst(type, value) ((const void*)(value))
# define voidcast(type, value) (value)
# define constcast(type, value) ((type)(value))
# define aligncast(type, value) ((void*)(value))
# define aligncastconst(type, value) ((const void*)(value))
#endif /* __cplusplus */
#if PNG_LIBPNG_VER < 10700
@@ -446,7 +446,7 @@ static void
make_random_bytes(png_uint_32* seed, void* pv, size_t size)
{
png_uint_32 u0 = seed[0], u1 = seed[1];
png_bytep bytes = png_voidcast(png_bytep, pv);
png_bytep bytes = voidcast(png_bytep, pv);
/* There are thirty-three bits; the next bit in the sequence is bit-33 XOR
* bit-20. The top 1 bit is in u1, the bottom 32 are in u0.
@@ -668,7 +668,7 @@ IDAT_list_extend(struct IDAT_list *tail)
if (length < tail->length) /* arithmetic overflow */
length = tail->length;
next = png_voidcast(IDAT_list*, malloc(IDAT_list_size(NULL, length)));
next = voidcast(IDAT_list*, malloc(IDAT_list_size(NULL, length)));
CLEAR(*next);
/* The caller must handle this: */
@@ -3535,7 +3535,7 @@ get_control(png_const_structrp png_ptr)
/* This just returns the (file*). The chunk and idat control structures
* don't always exist.
*/
struct control *control = png_voidcast(struct control*,
struct control *control = voidcast(struct control*,
png_get_error_ptr(png_ptr));
return &control->file;
}
@@ -3543,7 +3543,7 @@ get_control(png_const_structrp png_ptr)
static void
allocate(struct file *file, int allocate_idat)
{
struct control *control = png_voidcast(struct control*, file->alloc_ptr);
struct control *control = voidcast(struct control*, file->alloc_ptr);
if (allocate_idat)
{

View File

@@ -1,6 +1,6 @@
libpng-manual.txt - A description on how to use and modify libpng
libpng version 1.6.17rc06 - March 23, 2015
libpng version 1.6.18beta05 - May 31, 2015
Updated and distributed by Glenn Randers-Pehrson
<glennrp at users.sourceforge.net>
Copyright (c) 1998-2015 Glenn Randers-Pehrson
@@ -11,7 +11,7 @@ libpng-manual.txt - A description on how to use and modify libpng
Based on:
libpng versions 0.97, January 1998, through 1.6.17rc06 - March 23, 2015
libpng versions 0.97, January 1998, through 1.6.18beta05 - May 31, 2015
Updated and distributed by Glenn Randers-Pehrson
Copyright (c) 1998-2015 Glenn Randers-Pehrson
@@ -4065,10 +4065,11 @@ a 16-bit linear encoded PNG file is written.
With all APIs row_stride is handled as in the read APIs - it is the spacing
from one row to the next in component sized units (float) and if negative
indicates a bottom-up row layout in the buffer.
indicates a bottom-up row layout in the buffer. If you pass zero, libpng will
calculate the row_stride for you from the width and number of channels.
Note that the write API does not support interlacing, sub-8-bit pixels,
and indexed (paletted) images.
indexed (paletted) images, or most ancillary chunks.
VI. Modifying/Customizing libpng
@@ -5109,6 +5110,23 @@ length, which resulted in PNG files that cannot be read beyond the bad iTXt
chunk. This error was fixed in libpng-1.6.3, and a tool (called
contrib/tools/png-fix-itxt) has been added to the libpng distribution.
Starting with libpng-1.6.17, the PNG_SAFE_LIMITS macro was eliminated
and safe limits are used by default (users who need larger limits
can still override them at compile time or run time, as described above).
The new limits are
default spec limit
png_user_width_max 1,000,000 2,147,483,647
png_user_height_max 1,000,000 2,147,483,647
png_user_chunk_cache_max 128 unlimited
png_user_chunk_malloc_max 8,000,000 unlimited
Starting with libpng-1.6.18, a PNG_RELEASE_BUILD macro was added, which allows
library builders to control compilation for an installed system (a release build).
It can be set for testing debug or beta builds to ensure that they will compile
when the build type is switched to RC or STABLE. In essence this overrides the
PNG_LIBPNG_BUILD_BASE_TYPE definition which is not directly user controllable.
XIII. Detecting libpng
The png_get_io_ptr() function has been present since libpng-0.88, has never
@@ -5279,13 +5297,13 @@ Other rules can be inferred by inspecting the libpng source.
XVI. Y2K Compliance in libpng
March 23, 2015
May 31, 2015
Since the PNG Development group is an ad-hoc body, we can't make
an official declaration.
This is your unofficial assurance that libpng from version 0.71 and
upward through 1.6.17rc06 are Y2K compliant. It is my belief that earlier
upward through 1.6.18beta05 are Y2K compliant. It is my belief that earlier
versions were also Y2K compliant.
Libpng only has two year fields. One is a 2-byte unsigned integer

View File

@@ -1,6 +1,6 @@
.TH LIBPNG 3 "March 23, 2015"
.TH LIBPNG 3 "May 31, 2015"
.SH NAME
libpng \- Portable Network Graphics (PNG) Reference Library 1.6.17rc06
libpng \- Portable Network Graphics (PNG) Reference Library 1.6.18beta05
.SH SYNOPSIS
\fB
#include <png.h>\fP
@@ -504,7 +504,7 @@ Following is a copy of the libpng-manual.txt file that accompanies libpng.
.SH LIBPNG.TXT
libpng-manual.txt - A description on how to use and modify libpng
libpng version 1.6.17rc06 - March 23, 2015
libpng version 1.6.18beta05 - May 31, 2015
Updated and distributed by Glenn Randers-Pehrson
<glennrp at users.sourceforge.net>
Copyright (c) 1998-2015 Glenn Randers-Pehrson
@@ -515,7 +515,7 @@ libpng-manual.txt - A description on how to use and modify libpng
Based on:
libpng versions 0.97, January 1998, through 1.6.17rc06 - March 23, 2015
libpng versions 0.97, January 1998, through 1.6.18beta05 - May 31, 2015
Updated and distributed by Glenn Randers-Pehrson
Copyright (c) 1998-2015 Glenn Randers-Pehrson
@@ -4569,10 +4569,11 @@ a 16-bit linear encoded PNG file is written.
With all APIs row_stride is handled as in the read APIs - it is the spacing
from one row to the next in component sized units (float) and if negative
indicates a bottom-up row layout in the buffer.
indicates a bottom-up row layout in the buffer. If you pass zero, libpng will
calculate the row_stride for you from the width and number of channels.
Note that the write API does not support interlacing, sub-8-bit pixels,
and indexed (paletted) images.
indexed (paletted) images, or most ancillary chunks.
.SH VI. Modifying/Customizing libpng
@@ -5613,6 +5614,23 @@ length, which resulted in PNG files that cannot be read beyond the bad iTXt
chunk. This error was fixed in libpng-1.6.3, and a tool (called
contrib/tools/png-fix-itxt) has been added to the libpng distribution.
Starting with libpng-1.6.17, the PNG_SAFE_LIMITS macro was eliminated
and safe limits are used by default (users who need larger limits
can still override them at compile time or run time, as described above).
The new limits are
default spec limit
png_user_width_max 1,000,000 2,147,483,647
png_user_height_max 1,000,000 2,147,483,647
png_user_chunk_cache_max 128 unlimited
png_user_chunk_malloc_max 8,000,000 unlimited
Starting with libpng-1.6.18, a PNG_RELEASE_BUILD macro was added, which allows
library builders to control compilation for an installed system (a release build).
It can be set for testing debug or beta builds to ensure that they will compile
when the build type is switched to RC or STABLE. In essence this overrides the
PNG_LIBPNG_BUILD_BASE_TYPE definition which is not directly user controllable.
.SH XIII. Detecting libpng
The png_get_io_ptr() function has been present since libpng-0.88, has never
@@ -5783,13 +5801,13 @@ Other rules can be inferred by inspecting the libpng source.
.SH XVI. Y2K Compliance in libpng
March 23, 2015
May 31, 2015
Since the PNG Development group is an ad-hoc body, we can't make
an official declaration.
This is your unofficial assurance that libpng from version 0.71 and
upward through 1.6.17rc06 are Y2K compliant. It is my belief that earlier
upward through 1.6.18beta05 are Y2K compliant. It is my belief that earlier
versions were also Y2K compliant.
Libpng only has two year fields. One is a 2-byte unsigned integer
@@ -6043,6 +6061,8 @@ the first widely used release:
1.6.16 16 10616 16.so.16.16[.0]
1.6.17beta01-06 16 10617 16.so.16.17[.0]
1.6.17rc01-06 16 10617 16.so.16.17[.0]
1.6.17 16 10617 16.so.16.17[.0]
1.6.18beta01-05 16 10618 16.so.16.18[.0]
Henceforth the source version will match the shared-library minor
and patch numbers; the shared-library major version number will be
@@ -6099,7 +6119,7 @@ possible without all of you.
Thanks to Frank J. T. Wojcik for helping with the documentation.
Libpng version 1.6.17rc06 - March 23, 2015:
Libpng version 1.6.18beta05 - May 31, 2015:
Initially created in 1995 by Guy Eric Schalnat, then of Group 42, Inc.
Currently maintained by Glenn Randers-Pehrson (glennrp at users.sourceforge.net).
@@ -6122,7 +6142,7 @@ this sentence.
This code is released under the libpng license.
libpng versions 1.2.6, August 15, 2004, through 1.6.17rc06, March 23, 2015, are
libpng versions 1.2.6, August 15, 2004, through 1.6.18beta05, May 31, 2015, are
Copyright (c) 2004,2006-2015 Glenn Randers-Pehrson, and are
distributed according to the same disclaimer and license as libpng-1.2.5
with the following individual added to the list of Contributing Authors
@@ -6221,7 +6241,7 @@ certification mark of the Open Source Initiative.
Glenn Randers-Pehrson
glennrp at users.sourceforge.net
March 23, 2015
May 31, 2015
.\" end of man page

View File

@@ -1,6 +1,6 @@
.TH LIBPNGPF 3 "March 23, 2015"
.TH LIBPNGPF 3 "May 31, 2015"
.SH NAME
libpng \- Portable Network Graphics (PNG) Reference Library 1.6.17rc06
libpng \- Portable Network Graphics (PNG) Reference Library 1.6.18beta05
(private functions)
.SH SYNOPSIS
\fB#include \fI"pngpriv.h"

2
png.5
View File

@@ -1,4 +1,4 @@
.TH PNG 5 "March 23, 2015"
.TH PNG 5 "May 31, 2015"
.SH NAME
png \- Portable Network Graphics (PNG) format
.SH DESCRIPTION

33
png.c
View File

@@ -1,7 +1,7 @@
/* png.c - location for general purpose libpng functions
*
* Last changed in libpng 1.6.17 [(PENDING RELEASE)]
* Last changed in libpng 1.6.18 [(PENDING RELEASE)]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
@@ -14,7 +14,7 @@
#include "pngpriv.h"
/* Generate a compiler error if there is an old png.h in the search path. */
typedef png_libpng_version_1_6_17rc06 Your_png_h_is_not_version_1_6_17rc06;
typedef png_libpng_version_1_6_18beta05 Your_png_h_is_not_version_1_6_18beta05;
/* Tells libpng that we have already handled the first "num_bytes" bytes
* of the PNG file signature. If the PNG data is embedded into another
@@ -101,7 +101,7 @@ png_zfree(voidpf png_ptr, voidpf ptr)
void /* PRIVATE */
png_reset_crc(png_structrp png_ptr)
{
/* The cast is safe because the crc is a 32 bit value. */
/* The cast is safe because the crc is a 32-bit value. */
png_ptr->crc = (png_uint_32)crc32(0, Z_NULL, 0);
}
@@ -129,7 +129,7 @@ png_calculate_crc(png_structrp png_ptr, png_const_bytep ptr, png_size_t length)
}
/* 'uLong' is defined in zlib.h as unsigned long; this means that on some
* systems it is a 64 bit value. crc32, however, returns 32 bits so the
* systems it is a 64-bit value. crc32, however, returns 32 bits so the
* following cast is safe. 'uInt' may be no more than 16 bits, so it is
* necessary to perform a loop here.
*/
@@ -275,7 +275,9 @@ png_create_png_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
# ifdef PNG_SETJMP_SUPPORTED
if (!setjmp(create_jmp_buf))
# endif
{
# ifdef PNG_SETJMP_SUPPORTED
/* Temporarily fake out the longjmp information until we have
* successfully completed this function. This only works if we have
* setjmp() support compiled in, but it is safe - this stuff should
@@ -284,8 +286,6 @@ png_create_png_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
create_struct.jmp_buf_ptr = &create_jmp_buf;
create_struct.jmp_buf_size = 0; /*stack allocation*/
create_struct.longjmp_fn = longjmp;
# else
{
# endif
/* Call the general version checker (shared with read and write code):
*/
@@ -766,13 +766,13 @@ png_get_copyright(png_const_structrp png_ptr)
#else
# ifdef __STDC__
return PNG_STRING_NEWLINE \
"libpng version 1.6.17rc06 - March 23, 2015" PNG_STRING_NEWLINE \
"libpng version 1.6.18beta05 - May 31, 2015" PNG_STRING_NEWLINE \
"Copyright (c) 1998-2015 Glenn Randers-Pehrson" PNG_STRING_NEWLINE \
"Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \
"Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \
PNG_STRING_NEWLINE;
# else
return "libpng version 1.6.17rc06 - March 23, 2015\
return "libpng version 1.6.18beta05 - May 31, 2015\
Copyright (c) 1998-2015 Glenn Randers-Pehrson\
Copyright (c) 1996-1997 Andreas Dilger\
Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.";
@@ -1235,16 +1235,17 @@ png_XYZ_from_xy(png_XYZ *XYZ, const png_xy *xy)
/* Check xy and, implicitly, z. Note that wide gamut color spaces typically
* have end points with 0 tristimulus values (these are impossible end
* points, but they are used to cover the possible colors.)
* points, but they are used to cover the possible colors). We check
* xy->whitey against 5, not 0, to avoid a possible integer overflow.
*/
if (xy->redx < 0 || xy->redx > PNG_FP_1) return 1;
if (xy->redy < 0 || xy->redy > PNG_FP_1-xy->redx) return 1;
if (xy->redx < 0 || xy->redx > PNG_FP_1) return 1;
if (xy->redy < 0 || xy->redy > PNG_FP_1-xy->redx) return 1;
if (xy->greenx < 0 || xy->greenx > PNG_FP_1) return 1;
if (xy->greeny < 0 || xy->greeny > PNG_FP_1-xy->greenx) return 1;
if (xy->bluex < 0 || xy->bluex > PNG_FP_1) return 1;
if (xy->bluey < 0 || xy->bluey > PNG_FP_1-xy->bluex) return 1;
if (xy->bluex < 0 || xy->bluex > PNG_FP_1) return 1;
if (xy->bluey < 0 || xy->bluey > PNG_FP_1-xy->bluex) return 1;
if (xy->whitex < 0 || xy->whitex > PNG_FP_1) return 1;
if (xy->whitey < 0 || xy->whitey > PNG_FP_1-xy->whitex) return 1;
if (xy->whitey < 5 || xy->whitey > PNG_FP_1-xy->whitex) return 1;
/* The reverse calculation is more difficult because the original tristimulus
* value had 9 independent values (red,green,blue)x(X,Y,Z) however only 8
@@ -2276,7 +2277,7 @@ png_compare_ICC_profile_with_sRGB(png_const_structrp png_ptr,
/* Length *and* intent must match */
if (length == png_sRGB_checks[i].length &&
intent == png_sRGB_checks[i].intent)
intent == (png_uint_32) png_sRGB_checks[i].intent)
{
/* Now calculate the adler32 if not done already. */
if (adler == 0)
@@ -4236,7 +4237,7 @@ png_set_option(png_structrp png_ptr, int option, int onoff)
* contrib/tools/makesRGB.c. The actual sRGB transfer curve defined in the
* specification (see the article at http://en.wikipedia.org/wiki/SRGB)
* is used, not the gamma=1/2.2 approximation use elsewhere in libpng.
* The sRGB to linear table is exact (to the nearest 16 bit linear fraction).
* The sRGB to linear table is exact (to the nearest 16-bit linear fraction).
* The inverse (linear to sRGB) table has accuracies as follows:
*
* For all possible (255*65535+1) input values:

37
png.h
View File

@@ -1,7 +1,7 @@
/* png.h - header file for PNG reference library
*
* libpng version 1.6.17rc06, March 23, 2015
* libpng version 1.6.18beta05, May 31, 2015
*
* Copyright (c) 1998-2015 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
@@ -12,7 +12,7 @@
* Authors and maintainers:
* libpng versions 0.71, May 1995, through 0.88, January 1996: Guy Schalnat
* libpng versions 0.89c, June 1996, through 0.96, May 1997: Andreas Dilger
* libpng versions 0.97, January 1998, through 1.6.17rc06, March 23, 2015: Glenn
* libpng versions 0.97, January 1998, through 1.6.18beta05, May 31, 2015: Glenn
* See also "Contributing Authors", below.
*
* Note about libpng version numbers:
@@ -215,6 +215,8 @@
* 1.6.16 16 10616 16.so.16.16[.0]
* 1.6.17beta01-06 16 10617 16.so.16.17[.0]
* 1.6.17rc01-06 16 10617 16.so.16.17[.0]
* 1.6.17 16 10617 16.so.16.17[.0]
* 1.6.18beta01-05 16 10618 16.so.16.18[.0]
*
* Henceforth the source version will match the shared-library major
* and minor numbers; the shared-library major version number will be
@@ -246,7 +248,7 @@
*
* This code is released under the libpng license.
*
* libpng versions 1.2.6, August 15, 2004, through 1.6.17rc06, March 23, 2015, are
* libpng versions 1.2.6, August 15, 2004, through 1.6.18beta05, May 31, 2015, are
* Copyright (c) 2004, 2006-2015 Glenn Randers-Pehrson, and are
* distributed according to the same disclaimer and license as libpng-1.2.5
* with the following individual added to the list of Contributing Authors:
@@ -358,13 +360,13 @@
* Y2K compliance in libpng:
* =========================
*
* March 23, 2015
* May 31, 2015
*
* Since the PNG Development group is an ad-hoc body, we can't make
* an official declaration.
*
* This is your unofficial assurance that libpng from version 0.71 and
* upward through 1.6.17rc06 are Y2K compliant. It is my belief that
* upward through 1.6.18beta05 are Y2K compliant. It is my belief that
* earlier versions were also Y2K compliant.
*
* Libpng only has two year fields. One is a 2-byte unsigned integer
@@ -426,9 +428,9 @@
*/
/* Version information for png.h - this should match the version in png.c */
#define PNG_LIBPNG_VER_STRING "1.6.17rc06"
#define PNG_LIBPNG_VER_STRING "1.6.18beta05"
#define PNG_HEADER_VERSION_STRING \
" libpng version 1.6.17rc06 - March 23, 2015\n"
" libpng version 1.6.18beta05 - May 31, 2015\n"
#define PNG_LIBPNG_VER_SONUM 16
#define PNG_LIBPNG_VER_DLLNUM 16
@@ -436,13 +438,13 @@
/* These should match the first 3 components of PNG_LIBPNG_VER_STRING: */
#define PNG_LIBPNG_VER_MAJOR 1
#define PNG_LIBPNG_VER_MINOR 6
#define PNG_LIBPNG_VER_RELEASE 17
#define PNG_LIBPNG_VER_RELEASE 18
/* This should match the numeric part of the final component of
* PNG_LIBPNG_VER_STRING, omitting any leading zero:
*/
#define PNG_LIBPNG_VER_BUILD 06
#define PNG_LIBPNG_VER_BUILD 05
/* Release Status */
#define PNG_LIBPNG_BUILD_ALPHA 1
@@ -459,7 +461,7 @@
#define PNG_LIBPNG_BUILD_SPECIAL 32 /* Cannot be OR'ed with
PNG_LIBPNG_BUILD_PRIVATE */
#define PNG_LIBPNG_BUILD_BASE_TYPE PNG_LIBPNG_BUILD_RC
#define PNG_LIBPNG_BUILD_BASE_TYPE PNG_LIBPNG_BUILD_BETA
/* Careful here. At one time, Guy wanted to use 082, but that would be octal.
* We must not include leading zeros.
@@ -467,7 +469,7 @@
* version 1.0.0 was mis-numbered 100 instead of 10000). From
* version 1.0.1 it's xxyyzz, where x=major, y=minor, z=release
*/
#define PNG_LIBPNG_VER 10617 /* 1.6.17 */
#define PNG_LIBPNG_VER 10618 /* 1.6.18 */
/* Library configuration: these options cannot be changed after
* the library has been built.
@@ -572,7 +574,7 @@ extern "C" {
/* This triggers a compiler error in png.c, if png.c and png.h
* do not agree upon the version number.
*/
typedef char* png_libpng_version_1_6_17rc06;
typedef char* png_libpng_version_1_6_18beta05;
/* Basic control structions. Read libpng-manual.txt or libpng.3 for more info.
*
@@ -2782,8 +2784,9 @@ PNG_EXPORT(207, void, png_save_uint_16, (png_bytep buf, unsigned int i));
*
* To read a PNG file using the simplified API:
*
* 1) Declare a 'png_image' structure (see below) on the stack and set the
* version field to PNG_IMAGE_VERSION.
* 1) Declare a 'png_image' structure (see below) on the stack, set the
* version field to PNG_IMAGE_VERSION and the 'opaque' pointer to NULL
* (this is REQUIRED, your program may crash if you don't do it.)
* 2) Call the appropriate png_image_begin_read... function.
* 3) Set the png_image 'format' member to the required sample format.
* 4) Allocate a buffer for the image and, if required, the color-map.
@@ -3208,9 +3211,11 @@ PNG_EXPORT(240, int, png_image_write_to_stdio, (png_imagep image, FILE *file,
*
* With all APIs row_stride is handled as in the read APIs - it is the spacing
* from one row to the next in component sized units (1 or 2 bytes) and if
* negative indicates a bottom-up row layout in the buffer.
* negative indicates a bottom-up row layout in the buffer. If row_stride is zero,
* libpng will calculate it for you from the image width and number of channels.
*
* Note that the write API does not support interlacing or sub-8-bit pixels.
* Note that the write API does not support interlacing, sub-8-bit pixels, indexed
* PNG (color_type 3) or most ancillary chunks.
*/
#endif /* STDIO */
#endif /* SIMPLIFIED_WRITE */

View File

@@ -1,7 +1,7 @@
/* pngconf.h - machine configurable file for libpng
*
* libpng version 1.6.17rc06, (PENDING RELEASE)
* libpng version 1.6.18beta05, (PENDING RELEASE)
*
* Copyright (c) 1998-2015 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)

View File

@@ -1,7 +1,7 @@
/* pngget.c - retrieval of values from info struct
*
* Last changed in libpng 1.6.17 [(PENDING RELEASE)]
* Last changed in libpng 1.6.17 [March 26, 2015]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)

View File

@@ -77,6 +77,9 @@ png_malloc_base,(png_const_structrp png_ptr, png_alloc_size_t size),
PNG_UNUSED(png_ptr)
#endif
/* Some compilers complain that this is always true. However, it
* can be false when integer overflow happens.
*/
if (size > 0 && size <= PNG_SIZE_MAX
# ifdef PNG_MAX_MALLOC_64K
&& size <= 65536U

View File

@@ -1,7 +1,7 @@
/* pngpread.c - read a png file in push mode
*
* Last changed in libpng 1.6.17 [(PENDING RELEASE)]
* Last changed in libpng 1.6.18 [(PENDING RELEASE)]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
@@ -584,13 +584,11 @@ png_push_save_buffer(png_structrp png_ptr)
if (png_ptr->save_buffer == NULL)
{
png_free(png_ptr, old_buffer);
old_buffer = NULL;
png_error(png_ptr, "Insufficient memory for save_buffer");
}
memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
png_free(png_ptr, old_buffer);
old_buffer = NULL;
png_ptr->save_buffer_max = new_max;
}
if (png_ptr->current_buffer_size)

View File

@@ -1,7 +1,7 @@
/* pngpriv.h - private declarations for use inside libpng
*
* Last changed in libpng 1.6.17 [(PENDING RELEASE)]
* Last changed in libpng 1.6.18 [(PENDING RELEASE)]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
@@ -118,8 +118,12 @@
* to compile with an appropriate #error if ALIGNED_MEMORY has been turned
* off.
*
* Note that gcc-4.9 defines __ARM_NEON instead of __ARM_NEON__, so we
* check both variants.
* Note that gcc-4.9 defines __ARM_NEON instead of the deprecated
* __ARM_NEON__, so we check both variants.
*
* To disable ARM_NEON optimizations entirely, and skip compiling the
* associated assembler code, pass --enable-arm-neon=no to configure
* or put -DPNG_ARM_NEON_OPT=0 in CPPFLAGS.
*/
# if (defined(__ARM_NEON__) || defined(__ARM_NEON)) && \
defined(PNG_ALIGNED_MEMORY_SUPPORTED)
@@ -296,6 +300,22 @@
# define PNG_DLL_EXPORT
#endif
/* This is a global switch to set the compilation for an installed system
* (a release build). It can be set for testing debug builds to ensure that
* they will compile when the build type is switched to RC or STABLE, the
* default is just to use PNG_LIBPNG_BUILD_BASE_TYPE. Set this in CPPFLAGS
* with either:
*
* -DPNG_RELEASE_BUILD Turns on the release compile path
* -DPNG_RELEASE_BUILD=0 Turns it off
* or in your pngusr.h with
* #define PNG_RELEASE_BUILD=1 Turns on the release compile path
* #define PNG_RELEASE_BUILD=0 Turns it off
*/
#ifndef PNG_RELEASE_BUILD
# define PNG_RELEASE_BUILD (PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC)
#endif
/* SECURITY and SAFETY:
*
* libpng is built with support for internal limits on image dimensions and

View File

@@ -1,7 +1,7 @@
/* pngread.c - read a PNG file
*
* Last changed in libpng 1.6.17 [(PENDING RELEASE)]
* Last changed in libpng 1.6.17 [March 26, 2015]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
@@ -63,7 +63,7 @@ png_create_read_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
/* In stable builds only warn if an application error can be completely
* handled.
*/
# if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC
# if PNG_RELEASE_BUILD
png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN;
# endif
# endif

View File

@@ -1,7 +1,7 @@
/* pngrio.c - functions for data input
*
* Last changed in libpng 1.6.17 [(PENDING RELEASE)]
* Last changed in libpng 1.6.17 [March 26, 2015]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)

View File

@@ -1,7 +1,7 @@
/* pngrtran.c - transforms the data in a row for PNG readers
*
* Last changed in libpng 1.6.17 [(PENDING RELEASE)]
* Last changed in libpng 1.6.18 [(PENDING RELEASE)]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
@@ -4460,7 +4460,7 @@ png_do_expand(png_row_infop row_info, png_bytep row,
for (i = 0; i < row_width; i++)
{
if (*sp == gray)
if ((*sp & 0xff) == gray)
*dp-- = 0;
else
@@ -4478,7 +4478,8 @@ png_do_expand(png_row_infop row_info, png_bytep row,
dp = row + (row_info->rowbytes << 1) - 1;
for (i = 0; i < row_width; i++)
{
if (*(sp - 1) == gray_high && *(sp) == gray_low)
if ((*(sp - 1) & 0xff) == gray_high &&
(*(sp) & 0xff) == gray_low)
{
*dp-- = 0;
*dp-- = 0;

View File

@@ -1,7 +1,7 @@
/* pngrutil.c - utilities to read a PNG file
*
* Last changed in libpng 1.6.17 [(PENDING RELEASE)]
* Last changed in libpng 1.6.18 [(PENDING RELEASE)]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
@@ -341,7 +341,7 @@ png_inflate_claim(png_structrp png_ptr, png_uint_32 owner)
* are minimal.
*/
(void)png_safecat(msg, (sizeof msg), 4, " using zstream");
#if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC
#if PNG_RELEASE_BUILD
png_chunk_warning(png_ptr, msg);
png_ptr->zowner = 0;
#else
@@ -575,7 +575,7 @@ png_decompress_chunk(png_structrp png_ptr,
*/
png_alloc_size_t limit = PNG_SIZE_MAX;
# ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED
# ifdef PNG_SET_USER_LIMITS_SUPPORTED
if (png_ptr->user_chunk_malloc_max > 0 &&
png_ptr->user_chunk_malloc_max < limit)
limit = png_ptr->user_chunk_malloc_max;
@@ -670,7 +670,6 @@ png_decompress_chunk(png_structrp png_ptr,
* success)
*/
png_free(png_ptr, text);
text = NULL;
/* This really is very benign, but it's still an error because
* the extra space may otherwise be used as a Trojan Horse.
@@ -1817,7 +1816,8 @@ png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
return;
}
if (length > png_ptr->num_palette || length > PNG_MAX_PALETTE_LENGTH ||
if (length > png_ptr->num_palette ||
length > (unsigned int) PNG_MAX_PALETTE_LENGTH ||
length == 0)
{
png_crc_finish(png_ptr, length);
@@ -1980,7 +1980,7 @@ png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
num = length / 2 ;
if (num != png_ptr->num_palette || num > PNG_MAX_PALETTE_LENGTH)
if (num != png_ptr->num_palette || num > (unsigned int) PNG_MAX_PALETTE_LENGTH)
{
png_crc_finish(png_ptr, length);
png_chunk_benign_error(png_ptr, "invalid");
@@ -2715,7 +2715,7 @@ png_cache_unknown_chunk(png_structrp png_ptr, png_uint_32 length)
png_ptr->unknown_chunk.data = NULL;
}
# ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED
# ifdef PNG_SET_USER_LIMITS_SUPPORTED
if (png_ptr->user_chunk_malloc_max > 0 &&
png_ptr->user_chunk_malloc_max < limit)
limit = png_ptr->user_chunk_malloc_max;

View File

@@ -1,7 +1,7 @@
/* pngset.c - storage of image information into info struct
*
* Last changed in libpng 1.6.17 [(PENDING RELEASE)]
* Last changed in libpng 1.6.18 [(PENDING RELEASE)]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
@@ -673,7 +673,6 @@ png_set_iCCP(png_const_structrp png_ptr, png_inforp info_ptr,
if (new_iccp_profile == NULL)
{
png_free(png_ptr, new_iccp_name);
new_iccp_name = NULL;
png_benign_error(png_ptr,
"Insufficient memory to process iCCP profile");
@@ -710,7 +709,7 @@ png_set_text_2(png_const_structrp png_ptr, png_inforp info_ptr,
{
int i;
png_debug1(1, "in %lx storage function", png_ptr == NULL ? "unexpected" :
png_debug1(1, "in %lx storage function", png_ptr == NULL ? 0xabadca11 :
(unsigned long)png_ptr->chunk_name);
if (png_ptr == NULL || info_ptr == NULL || num_text <= 0 || text_ptr == NULL)
@@ -1522,6 +1521,9 @@ png_set_compression_buffer_size(png_structrp png_ptr, png_size_t size)
}
#ifndef __COVERITY__
/* Some compilers complain that this is always false. However, it
* can be true when integer overflow happens.
*/
if (size > ZLIB_IO_MAX)
{
png_warning(png_ptr,

View File

@@ -1,8 +1,8 @@
/* pngstruct.h - header file for PNG reference library
*
* Last changed in libpng 1.6.1 [March 28, 2013]
* Copyright (c) 1998-2013 Glenn Randers-Pehrson
* Last changed in libpng 1.6.18 [(PENDING RELEASE)]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
*
@@ -219,16 +219,18 @@ struct png_struct_def
png_uint_32 row_number; /* current row in interlace pass */
png_uint_32 chunk_name; /* PNG_CHUNK() id of current chunk */
png_bytep prev_row; /* buffer to save previous (unfiltered) row.
* This is a pointer into big_prev_row
* While reading this is a pointer into
* big_prev_row; while writing it is separately
* allocated if needed.
*/
png_bytep row_buf; /* buffer to save current (unfiltered) row.
* This is a pointer into big_row_buf
* While reading, this is a pointer into
* big_row_buf; while writing it is separately
* allocated.
*/
#ifdef PNG_WRITE_SUPPORTED
png_bytep sub_row; /* buffer to save "sub" row when filtering */
png_bytep up_row; /* buffer to save "up" row when filtering */
png_bytep avg_row; /* buffer to save "avg" row when filtering */
png_bytep paeth_row; /* buffer to save "Paeth" row when filtering */
#ifdef PNG_WRITE_FILTER_SUPPORTED
png_bytep try_row; /* buffer to save trial row when filtering */
png_bytep tst_row; /* buffer to save best trial row when filtering */
#endif
png_size_t info_rowbytes; /* Added in 1.5.4: cache of updated row bytes */

View File

@@ -1,7 +1,7 @@
/* pngtest.c - a simple test program to test libpng
*
* Last changed in libpng 1.6.17 [(PENDING RELEASE)]
* Last changed in libpng 1.6.18 [(PENDING RELEASE)]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
@@ -565,6 +565,7 @@ png_debug_free(png_structp png_ptr, png_voidp ptr)
}
/* Unlink the element from the list. */
if (pinformation != NULL)
{
memory_infop *ppinfo = &pinformation;
@@ -581,8 +582,7 @@ png_debug_free(png_structp png_ptr, png_voidp ptr)
/* We must free the list element too, but first kill
the memory that is to be freed. */
memset(ptr, 0x55, pinfo->size);
if (pinfo != NULL)
free(pinfo);
free(pinfo);
pinfo = NULL;
break;
}
@@ -1833,10 +1833,10 @@ main(int argc, char *argv[])
k, (unsigned long)filters_used[k]);
#endif
#ifdef PNG_TIME_RFC1123_SUPPORTED
if (tIME_chunk_present != 0)
fprintf(STDERR, " tIME = %s\n", tIME_string);
if (tIME_chunk_present != 0)
fprintf(STDERR, " tIME = %s\n", tIME_string);
tIME_chunk_present = 0;
tIME_chunk_present = 0;
#endif /* TIME_RFC1123 */
}
@@ -2028,4 +2028,4 @@ main(void)
#endif
/* Generate a compiler error if there is an old png.h in the search path. */
typedef png_libpng_version_1_6_17rc06 Your_png_h_is_not_version_1_6_17rc06;
typedef png_libpng_version_1_6_18beta05 Your_png_h_is_not_version_1_6_18beta05;

View File

@@ -1,7 +1,7 @@
/* pngtrans.c - transforms the data in a row (used by both readers and writers)
*
* Last changed in libpng 1.6.17 [(PENDING RELEASE)]
* Last changed in libpng 1.6.18 [(PENDING RELEASE)]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
@@ -704,7 +704,7 @@ png_do_check_palette_indexes(png_structrp png_ptr, png_row_infop row_info)
*/
for (; rp > png_ptr->row_buf; rp--)
{
if (*rp >> padding != 0)
if ((*rp >> padding) != 0)
png_ptr->num_palette_max = 1;
padding = 0;
}

View File

@@ -1,7 +1,7 @@
/* pngwrite.c - general routines to write a PNG file
*
* Last changed in libpng 1.6.17 [(PENDING RELEASE)]
* Last changed in libpng 1.6.18 [(PENDING RELEASE)]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
@@ -216,8 +216,13 @@ png_write_info(png_structrp png_ptr, png_const_inforp info_ptr)
if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0 &&
info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
{
int j;
for (j = 0; j<(int)info_ptr->num_trans; j++)
int j, jend;
jend = info_ptr->num_trans;
if (jend > PNG_MAX_PALETTE_LENGTH)
jend = PNG_MAX_PALETTE_LENGTH;
for (j = 0; j<jend; ++j)
info_ptr->trans_alpha[j] =
(png_byte)(255 - info_ptr->trans_alpha[j]);
}
@@ -538,7 +543,7 @@ png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
/* App warnings are warnings in release (or release candidate) builds but
* are errors during development.
*/
#if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC
#if PNG_RELEASE_BUILD
png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN;
#endif
@@ -937,15 +942,11 @@ png_write_destroy(png_structrp png_ptr)
png_ptr->row_buf = NULL;
#ifdef PNG_WRITE_FILTER_SUPPORTED
png_free(png_ptr, png_ptr->prev_row);
png_free(png_ptr, png_ptr->sub_row);
png_free(png_ptr, png_ptr->up_row);
png_free(png_ptr, png_ptr->avg_row);
png_free(png_ptr, png_ptr->paeth_row);
png_free(png_ptr, png_ptr->try_row);
png_free(png_ptr, png_ptr->tst_row);
png_ptr->prev_row = NULL;
png_ptr->sub_row = NULL;
png_ptr->up_row = NULL;
png_ptr->avg_row = NULL;
png_ptr->paeth_row = NULL;
png_ptr->try_row = NULL;
png_ptr->tst_row = NULL;
#endif
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
@@ -1052,75 +1053,67 @@ png_set_filter(png_structrp png_ptr, int method, int filters)
* wants to start and stop using particular filters during compression,
* it should start out with all of the filters, and then remove them
* or add them back after the start of compression.
*
* NOTE: this is a nasty constraint on the code, because it means that the
* prev_row buffer must be maintained even if there are currently no
* 'prev_row' requiring filters active.
*/
if (png_ptr->row_buf != NULL)
{
#ifdef PNG_WRITE_FILTER_SUPPORTED
if ((png_ptr->do_filter & PNG_FILTER_SUB) != 0 &&
png_ptr->sub_row == NULL)
int num_filters;
png_alloc_size_t buf_size;
/* Repeat the checks in png_write_start_row; 1 pixel high or wide
* images cannot benefit from certain filters. If this isn't done here
* the check below will fire on 1 pixel high images.
*/
if (png_ptr->height == 1)
filters &= ~(PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH);
if (png_ptr->width == 1)
filters &= ~(PNG_FILTER_SUB|PNG_FILTER_AVG|PNG_FILTER_PAETH);
if ((filters & (PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH)) != 0
&& png_ptr->prev_row == NULL)
{
png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
(png_ptr->rowbytes + 1));
png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
/* This is the error case, however it is benign - the previous row
* is not available so the filter can't be used. Just warn here.
*/
png_app_warning(png_ptr,
"png_set_filter: UP/AVG/PAETH cannot be added after start");
filters &= ~(PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH);
}
if ((png_ptr->do_filter & PNG_FILTER_UP) != 0 &&
png_ptr->up_row == NULL)
num_filters = 0;
if (filters & PNG_FILTER_SUB)
num_filters++;
if (filters & PNG_FILTER_UP)
num_filters++;
if (filters & PNG_FILTER_AVG)
num_filters++;
if (filters & PNG_FILTER_PAETH)
num_filters++;
/* Allocate needed row buffers if they have not already been
* allocated.
*/
buf_size = PNG_ROWBYTES(png_ptr->usr_channels * png_ptr->usr_bit_depth,
png_ptr->width) + 1;
if (png_ptr->try_row == NULL)
png_ptr->try_row = png_malloc(png_ptr, buf_size);
if (num_filters > 1)
{
if (png_ptr->prev_row == NULL)
{
png_warning(png_ptr, "Can't add Up filter after starting");
png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
~PNG_FILTER_UP);
}
else
{
png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
(png_ptr->rowbytes + 1));
png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
}
if (png_ptr->tst_row == NULL)
png_ptr->tst_row = png_malloc(png_ptr, buf_size);
}
if ((png_ptr->do_filter & PNG_FILTER_AVG) != 0 &&
png_ptr->avg_row == NULL)
{
if (png_ptr->prev_row == NULL)
{
png_warning(png_ptr, "Can't add Average filter after starting");
png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
~PNG_FILTER_AVG);
}
else
{
png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
(png_ptr->rowbytes + 1));
png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
}
}
if ((png_ptr->do_filter & PNG_FILTER_PAETH) != 0 &&
png_ptr->paeth_row == NULL)
{
if (png_ptr->prev_row == NULL)
{
png_warning(png_ptr, "Can't add Paeth filter after starting");
png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
}
else
{
png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
(png_ptr->rowbytes + 1));
png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
}
}
if (png_ptr->do_filter == PNG_NO_FILTERS)
#endif /* WRITE_FILTER */
png_ptr->do_filter = PNG_FILTER_NONE;
}
png_ptr->do_filter = (png_byte)filters;
}
else
png_error(png_ptr, "Unknown custom filter method");

View File

@@ -1,7 +1,7 @@
/* pngwtran.c - transforms the data in a row for PNG writers
*
* Last changed in libpng 1.6.17 [(PENDING RELEASE)]
* Last changed in libpng 1.6.18 [(PENDING RELEASE)]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
@@ -422,7 +422,7 @@ png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
*(dp++) = *(sp++);
*/
sp+=3; dp = sp;
*(dp++) = (png_byte)(255 - *(sp++));
*dp = (png_byte)(255 - *(sp++));
}
}
@@ -446,7 +446,7 @@ png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
*/
sp+=6; dp = sp;
*(dp++) = (png_byte)(255 - *(sp++));
*(dp++) = (png_byte)(255 - *(sp++));
*dp = (png_byte)(255 - *(sp++));
}
}
#endif /* WRITE_16BIT */
@@ -484,7 +484,7 @@ png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
*/
sp+=2; dp = sp;
*(dp++) = (png_byte)(255 - *(sp++));
*(dp++) = (png_byte)(255 - *(sp++));
*dp = (png_byte)(255 - *(sp++));
}
}
#endif /* WRITE_16BIT */

View File

@@ -1,7 +1,7 @@
/* pngwutil.c - utilities to write a PNG file
*
* Last changed in libpng 1.6.17 [(PENDING RELEASE)]
* Last changed in libpng 1.6.18 [(PENDING RELEASE)]
* Copyright (c) 1998-2015 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
@@ -308,7 +308,7 @@ png_deflate_claim(png_structrp png_ptr, png_uint_32 owner,
*/
(void)png_safecat(msg, (sizeof msg), 10, " using zstream");
#endif
#if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC
#if PNG_RELEASE_BUILD
png_warning(png_ptr, msg);
/* Attempt sane error recovery */
@@ -1961,6 +1961,10 @@ png_write_start_row(png_structrp png_ptr)
png_alloc_size_t buf_size;
int usr_pixel_depth;
#ifdef PNG_WRITE_FILTER_SUPPORTED
int filters;
#endif
png_debug(1, "in png_write_start_row");
usr_pixel_depth = png_ptr->usr_channels * png_ptr->usr_bit_depth;
@@ -1971,50 +1975,53 @@ png_write_start_row(png_structrp png_ptr)
png_ptr->maximum_pixel_depth = (png_byte)usr_pixel_depth;
/* Set up row buffer */
png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, buf_size);
png_ptr->row_buf = png_voidcast(png_bytep, png_malloc(png_ptr, buf_size));
png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
#ifdef PNG_WRITE_FILTER_SUPPORTED
/* Set up filtering buffer, if using this filter */
if (png_ptr->do_filter & PNG_FILTER_SUB)
{
png_ptr->sub_row = (png_bytep)png_malloc(png_ptr, png_ptr->rowbytes + 1);
filters = png_ptr->do_filter;
png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
if (png_ptr->height == 1)
filters &= ~(PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH);
if (png_ptr->width == 1)
filters &= ~(PNG_FILTER_SUB|PNG_FILTER_AVG|PNG_FILTER_PAETH);
if (filters == 0)
filters = PNG_FILTER_NONE;
png_ptr->do_filter = filters;
if (((filters & (PNG_FILTER_SUB | PNG_FILTER_UP | PNG_FILTER_AVG |
PNG_FILTER_PAETH)) != 0) && png_ptr->try_row == NULL)
{
int num_filters = 0;
png_ptr->try_row = png_voidcast(png_bytep, png_malloc(png_ptr, buf_size));
if (filters & PNG_FILTER_SUB)
num_filters++;
if (filters & PNG_FILTER_UP)
num_filters++;
if (filters & PNG_FILTER_AVG)
num_filters++;
if (filters & PNG_FILTER_PAETH)
num_filters++;
if (num_filters > 1)
png_ptr->tst_row = png_voidcast(png_bytep, png_malloc(png_ptr,
buf_size));
}
/* We only need to keep the previous row if we are using one of these. */
if ((png_ptr->do_filter &
(PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH)) != 0)
{
/* Set up previous row buffer */
png_ptr->prev_row = (png_bytep)png_calloc(png_ptr, buf_size);
if ((png_ptr->do_filter & PNG_FILTER_UP) != 0)
{
png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
png_ptr->rowbytes + 1);
png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
}
if ((png_ptr->do_filter & PNG_FILTER_AVG) != 0)
{
png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
png_ptr->rowbytes + 1);
png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
}
if ((png_ptr->do_filter & PNG_FILTER_PAETH) != 0)
{
png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
png_ptr->rowbytes + 1);
png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
}
}
/* We only need to keep the previous row if we are using one of the following
* filters.
*/
if ((filters & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH)) != 0)
png_ptr->prev_row = png_calloc(png_ptr, buf_size);
#endif /* WRITE_FILTER */
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
@@ -2310,6 +2317,7 @@ png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
}
#endif
/* This filters the row, chooses which filter to use, if it has not already
* been specified by the application, and then writes the row out with the
* chosen filter.
@@ -2318,42 +2326,232 @@ static void /* PRIVATE */
png_write_filtered_row(png_structrp png_ptr, png_bytep filtered_row,
png_size_t row_bytes);
#ifdef PNG_WRITE_FILTER_SUPPORTED
static png_size_t /* PRIVATE */
png_setup_sub_row(png_structrp png_ptr, const png_uint_32 bpp,
const png_size_t row_bytes, const png_size_t lmins)
{
png_bytep rp, dp, lp;
png_size_t i;
png_size_t sum = 0;
int v;
png_ptr->try_row[0] = PNG_FILTER_VALUE_SUB;
for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1; i < bpp;
i++, rp++, dp++)
{
v = *dp = *rp;
sum += (v < 128) ? v : 256 - v;
}
for (lp = png_ptr->row_buf + 1; i < row_bytes;
i++, rp++, lp++, dp++)
{
v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
sum += (v < 128) ? v : 256 - v;
if (sum > lmins) /* We are already worse, don't continue. */
break;
}
return (sum);
}
static png_size_t /* PRIVATE */
png_setup_up_row(png_structrp png_ptr, const png_size_t row_bytes,
const png_size_t lmins)
{
png_bytep rp, dp, pp;
png_size_t i;
png_size_t sum = 0;
int v;
png_ptr->try_row[0] = PNG_FILTER_VALUE_UP;
for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1,
pp = png_ptr->prev_row + 1; i < row_bytes;
i++, rp++, pp++, dp++)
{
v = *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
sum += (v < 128) ? v : 256 - v;
if (sum > lmins) /* We are already worse, don't continue. */
break;
}
return (sum);
}
static png_size_t /* PRIVATE */
png_setup_avg_row(png_structrp png_ptr, const png_uint_32 bpp,
const png_size_t row_bytes, const png_size_t lmins)
{
png_bytep rp, dp, pp, lp;
png_uint_32 i;
png_size_t sum = 0;
int v;
png_ptr->try_row[0] = PNG_FILTER_VALUE_AVG;
for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1,
pp = png_ptr->prev_row + 1; i < bpp; i++)
{
v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
sum += (v < 128) ? v : 256 - v;
}
for (lp = png_ptr->row_buf + 1; i < row_bytes; i++)
{
v = *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
& 0xff);
sum += (v < 128) ? v : 256 - v;
if (sum > lmins) /* We are already worse, don't continue. */
break;
}
return (sum);
}
static png_size_t /* PRIVATE */
png_setup_paeth_row(png_structrp png_ptr, const png_uint_32 bpp,
const png_size_t row_bytes, const png_size_t lmins)
{
png_bytep rp, dp, pp, cp, lp;
png_size_t i;
png_size_t sum = 0;
int v;
png_ptr->try_row[0] = PNG_FILTER_VALUE_PAETH;
for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1,
pp = png_ptr->prev_row + 1; i < bpp; i++)
{
v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
sum += (v < 128) ? v : 256 - v;
}
for (lp = png_ptr->row_buf + 1, cp = png_ptr->prev_row + 1; i < row_bytes;
i++)
{
int a, b, c, pa, pb, pc, p;
b = *pp++;
c = *cp++;
a = *lp++;
p = b - c;
pc = a - c;
#ifdef PNG_USE_ABS
pa = abs(p);
pb = abs(pc);
pc = abs(p + pc);
#else
pa = p < 0 ? -p : p;
pb = pc < 0 ? -pc : pc;
pc = (p + pc) < 0 ? -(p + pc) : p + pc;
#endif
p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
sum += (v < 128) ? v : 256 - v;
if (sum > lmins) /* We are already worse, don't continue. */
break;
}
return (sum);
}
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
#define PNG_HISHIFT 10
#define PNG_LOMASK ((png_uint_32)0xffffL)
#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
static png_size_t /* PRIVATE */
png_increase_lmins(png_structrp png_ptr, png_size_t lmins)
{
/* We temporarily increase the "minimum sum" by the factor we
* would reduce the sum of this filter, so that we can do the
* early exit comparison without scaling the sum each time.
*/
int j;
png_uint_32 lmhi, lmlo;
lmlo = lmins & PNG_LOMASK;
lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
for (j = 0; j < png_ptr->num_prev_filters; j++)
{
if (png_ptr->prev_filters[j] == png_ptr->try_row[0])
{
lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
PNG_WEIGHT_SHIFT;
lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
PNG_WEIGHT_SHIFT;
}
}
lmlo = (lmlo * png_ptr->inv_filter_costs[png_ptr->try_row[0]]) >>
PNG_COST_SHIFT;
lmhi = (lmhi * png_ptr->inv_filter_costs[png_ptr->try_row[0]]) >>
PNG_COST_SHIFT;
if (lmhi > PNG_HIMASK)
lmins = PNG_MAXSUM;
else
lmins = (lmhi << PNG_HISHIFT) + lmlo;
return (lmins);
}
#endif /* WRITE_WEIGHTED_FILTER */
#endif /* WRITE_FILTER */
void /* PRIVATE */
png_write_find_filter(png_structrp png_ptr, png_row_infop row_info)
{
png_bytep best_row;
#ifdef PNG_WRITE_FILTER_SUPPORTED
png_bytep prev_row, row_buf;
png_uint_32 mins, bpp;
#ifndef PNG_WRITE_FILTER_SUPPORTED
png_write_filtered_row(png_ptr, png_ptr->row_buf, row_info->rowbytes+1);
#else
png_byte filter_to_do = png_ptr->do_filter;
png_size_t row_bytes = row_info->rowbytes;
png_bytep row_buf;
png_bytep best_row;
png_uint_32 bpp;
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
int num_p_filters = png_ptr->num_prev_filters;
#endif
png_size_t mins;
png_size_t row_bytes = row_info->rowbytes;
png_debug(1, "in png_write_find_filter");
#ifndef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
if (png_ptr->row_number == 0 && filter_to_do == PNG_ALL_FILTERS)
{
if (png_ptr->row_number == 0 && filter_to_do == PNG_ALL_FILTERS)
{
/* These will never be selected so we need not test them. */
filter_to_do &= ~(PNG_FILTER_UP | PNG_FILTER_PAETH);
}
}
#endif
/* Find out how many bytes offset each pixel is */
bpp = (row_info->pixel_depth + 7) >> 3;
prev_row = png_ptr->prev_row;
#endif
best_row = png_ptr->row_buf;
#ifdef PNG_WRITE_FILTER_SUPPORTED
row_buf = best_row;
mins = PNG_MAXSUM;
row_buf = png_ptr->row_buf;
mins = PNG_SIZE_MAX - 256/* so we can detect potential overflow of the
running sum */;
/* The prediction method we use is to find which method provides the
* smallest value when summing the absolute values of the distances
@@ -2383,19 +2581,16 @@ png_write_find_filter(png_structrp png_ptr, png_row_infop row_info)
/* We don't need to test the 'no filter' case if this is the only filter
* that has been chosen, as it doesn't actually do anything to the data.
*/
best_row = png_ptr->row_buf;
if ((filter_to_do & PNG_FILTER_NONE) != 0 && filter_to_do != PNG_FILTER_NONE)
{
png_bytep rp;
png_uint_32 sum = 0;
png_size_t sum = 0;
png_size_t i;
int v;
for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
{
v = *rp;
sum += (v < 128) ? v : 256 - v;
}
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
{
@@ -2434,6 +2629,28 @@ png_write_find_filter(png_structrp png_ptr, png_row_infop row_info)
sum = (sumhi << PNG_HISHIFT) + sumlo;
}
#endif
if (PNG_SIZE_MAX/128 <= row_bytes)
{
for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
{
/* Check for overflow */
if (sum > PNG_SIZE_MAX/128 - 256)
break;
v = *rp;
sum += (v < 128) ? v : 256 - v;
}
}
else /* Overflow is not possible */
{
for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
{
v = *rp;
sum += (v < 128) ? v : 256 - v;
}
}
mins = sum;
}
@@ -2441,87 +2658,21 @@ png_write_find_filter(png_structrp png_ptr, png_row_infop row_info)
if (filter_to_do == PNG_FILTER_SUB)
/* It's the only filter so no testing is needed */
{
png_bytep rp, lp, dp;
png_size_t i;
for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
i++, rp++, dp++)
{
*dp = *rp;
}
for (lp = row_buf + 1; i < row_bytes;
i++, rp++, lp++, dp++)
{
*dp = (png_byte)((int)*rp - (int)*lp);
}
best_row = png_ptr->sub_row;
(void) png_setup_sub_row(png_ptr, bpp, row_bytes, mins);
best_row = png_ptr->try_row;
}
else if ((filter_to_do & PNG_FILTER_SUB) != 0)
{
png_bytep rp, dp, lp;
png_uint_32 sum = 0, lmins = mins;
png_size_t i;
int v;
png_size_t sum;
png_size_t lmins = mins;
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
/* We temporarily increase the "minimum sum" by the factor we
* would reduce the sum of this filter, so that we can do the
* early exit comparison without scaling the sum each time.
*/
png_ptr->try_row[0] = PNG_FILTER_VALUE_SUB;
if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
{
int j;
png_uint_32 lmhi, lmlo;
lmlo = lmins & PNG_LOMASK;
lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
for (j = 0; j < num_p_filters; j++)
{
if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
{
lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
PNG_WEIGHT_SHIFT;
lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
PNG_WEIGHT_SHIFT;
}
}
lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
PNG_COST_SHIFT;
lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
PNG_COST_SHIFT;
if (lmhi > PNG_HIMASK)
lmins = PNG_MAXSUM;
else
lmins = (lmhi << PNG_HISHIFT) + lmlo;
}
lmins = png_increase_lmins(png_ptr, lmins);
#endif
for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
i++, rp++, dp++)
{
v = *dp = *rp;
sum += (v < 128) ? v : 256 - v;
}
for (lp = row_buf + 1; i < row_bytes;
i++, rp++, lp++, dp++)
{
v = *dp = (png_byte)((int)*rp - (int)*lp);
sum += (v < 128) ? v : 256 - v;
if (sum > lmins) /* We are already worse, don't continue. */
break;
}
sum = png_setup_sub_row(png_ptr, bpp, row_bytes, lmins);
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
@@ -2560,78 +2711,33 @@ png_write_find_filter(png_structrp png_ptr, png_row_infop row_info)
if (sum < mins)
{
mins = sum;
best_row = png_ptr->sub_row;
best_row = png_ptr->try_row;
if (png_ptr->tst_row != NULL)
{
png_ptr->try_row = png_ptr->tst_row;
png_ptr->tst_row = best_row;
}
}
}
/* Up filter */
if (filter_to_do == PNG_FILTER_UP)
{
png_bytep rp, dp, pp;
png_size_t i;
for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
pp = prev_row + 1; i < row_bytes;
i++, rp++, pp++, dp++)
{
*dp = (png_byte)((int)*rp - (int)*pp);
}
best_row = png_ptr->up_row;
(void) png_setup_up_row(png_ptr, row_bytes, mins);
best_row = png_ptr->try_row;
}
else if ((filter_to_do & PNG_FILTER_UP) != 0)
{
png_bytep rp, dp, pp;
png_uint_32 sum = 0, lmins = mins;
png_size_t i;
int v;
png_size_t sum;
png_size_t lmins = mins;
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
png_ptr->try_row[0] = PNG_FILTER_VALUE_UP;
if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
{
int j;
png_uint_32 lmhi, lmlo;
lmlo = lmins & PNG_LOMASK;
lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
for (j = 0; j < num_p_filters; j++)
{
if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
{
lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
PNG_WEIGHT_SHIFT;
lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
PNG_WEIGHT_SHIFT;
}
}
lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
PNG_COST_SHIFT;
lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
PNG_COST_SHIFT;
if (lmhi > PNG_HIMASK)
lmins = PNG_MAXSUM;
else
lmins = (lmhi << PNG_HISHIFT) + lmlo;
}
lmins = png_increase_lmins(png_ptr, lmins);
#endif
for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
pp = prev_row + 1; i < row_bytes; i++)
{
v = *dp++ = (png_byte)((int)*rp++ - (int)*pp++);
sum += (v < 128) ? v : 256 - v;
if (sum > lmins) /* We are already worse, don't continue. */
break;
}
sum = png_setup_up_row(png_ptr, row_bytes, lmins);
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
@@ -2670,89 +2776,33 @@ png_write_find_filter(png_structrp png_ptr, png_row_infop row_info)
if (sum < mins)
{
mins = sum;
best_row = png_ptr->up_row;
best_row = png_ptr->try_row;
if (png_ptr->tst_row != NULL)
{
png_ptr->try_row = png_ptr->tst_row;
png_ptr->tst_row = best_row;
}
}
}
/* Avg filter */
if (filter_to_do == PNG_FILTER_AVG)
{
png_bytep rp, dp, pp, lp;
png_uint_32 i;
for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
pp = prev_row + 1; i < bpp; i++)
{
*dp++ = (png_byte)((int)*rp++ - ((int)*pp++ / 2));
}
for (lp = row_buf + 1; i < row_bytes; i++)
{
*dp++ =
(png_byte)((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2));
}
best_row = png_ptr->avg_row;
(void) png_setup_avg_row(png_ptr, bpp, row_bytes, mins);
best_row = png_ptr->try_row;
}
else if ((filter_to_do & PNG_FILTER_AVG) != 0)
{
png_bytep rp, dp, pp, lp;
png_uint_32 sum = 0, lmins = mins;
png_size_t i;
int v;
png_size_t sum;
png_size_t lmins = mins;
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
png_ptr->try_row[0] = PNG_FILTER_VALUE_AVG;
if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
{
int j;
png_uint_32 lmhi, lmlo;
lmlo = lmins & PNG_LOMASK;
lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
for (j = 0; j < num_p_filters; j++)
{
if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
{
lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
PNG_WEIGHT_SHIFT;
lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
PNG_WEIGHT_SHIFT;
}
}
lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
PNG_COST_SHIFT;
lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
PNG_COST_SHIFT;
if (lmhi > PNG_HIMASK)
lmins = PNG_MAXSUM;
else
lmins = (lmhi << PNG_HISHIFT) + lmlo;
}
lmins = png_increase_lmins(png_ptr, lmins);
#endif
for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
pp = prev_row + 1; i < bpp; i++)
{
v = *dp++ = (png_byte)((int)*rp++ - ((int)*pp++ / 2));
sum += (v < 128) ? v : 256 - v;
}
for (lp = row_buf + 1; i < row_bytes; i++)
{
v = *dp++ =
(png_byte)(((int)*rp++ - ((int)*pp++ + (int)*lp++) / 2));
sum += (v < 128) ? v : 256 - v;
if (sum > lmins) /* We are already worse, don't continue. */
break;
}
sum= png_setup_avg_row(png_ptr, bpp, row_bytes, lmins);
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
@@ -2764,7 +2814,7 @@ png_write_find_filter(png_structrp png_ptr, png_row_infop row_info)
for (j = 0; j < num_p_filters; j++)
{
if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
{
sumlo = (sumlo * png_ptr->filter_weights[j]) >>
PNG_WEIGHT_SHIFT;
@@ -2791,143 +2841,33 @@ png_write_find_filter(png_structrp png_ptr, png_row_infop row_info)
if (sum < mins)
{
mins = sum;
best_row = png_ptr->avg_row;
best_row = png_ptr->try_row;
if (png_ptr->tst_row != NULL)
{
png_ptr->try_row = png_ptr->tst_row;
png_ptr->tst_row = best_row;
}
}
}
/* Paeth filter */
if ((filter_to_do == PNG_FILTER_PAETH) != 0)
{
png_bytep rp, dp, pp, cp, lp;
png_size_t i;
for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
pp = prev_row + 1; i < bpp; i++)
{
*dp++ = (png_byte)((int)*rp++ - (int)*pp++);
}
for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
{
int a, b, c, pa, pb, pc, p;
b = *pp++;
c = *cp++;
a = *lp++;
p = b - c;
pc = a - c;
#ifdef PNG_USE_ABS
pa = abs(p);
pb = abs(pc);
pc = abs(p + pc);
#else
pa = p < 0 ? -p : p;
pb = pc < 0 ? -pc : pc;
pc = (p + pc) < 0 ? -(p + pc) : p + pc;
#endif
p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
*dp++ = (png_byte)((int)*rp++ - p);
}
best_row = png_ptr->paeth_row;
(void) png_setup_paeth_row(png_ptr, bpp, row_bytes, mins);
best_row = png_ptr->try_row;
}
else if ((filter_to_do & PNG_FILTER_PAETH) != 0)
{
png_bytep rp, dp, pp, cp, lp;
png_uint_32 sum = 0, lmins = mins;
png_size_t i;
int v;
png_size_t sum;
png_size_t lmins = mins;
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
png_ptr->try_row[0] = PNG_FILTER_VALUE_PAETH;
if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
{
int j;
png_uint_32 lmhi, lmlo;
lmlo = lmins & PNG_LOMASK;
lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
for (j = 0; j < num_p_filters; j++)
{
if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
{
lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
PNG_WEIGHT_SHIFT;
lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
PNG_WEIGHT_SHIFT;
}
}
lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
PNG_COST_SHIFT;
lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
PNG_COST_SHIFT;
if (lmhi > PNG_HIMASK)
lmins = PNG_MAXSUM;
else
lmins = (lmhi << PNG_HISHIFT) + lmlo;
}
lmins = png_increase_lmins(png_ptr, lmins);
#endif
for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
pp = prev_row + 1; i < bpp; i++)
{
v = *dp++ = (png_byte)((int)*rp++ - (int)*pp++);
sum += (v < 128) ? v : 256 - v;
}
for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
{
int a, b, c, pa, pb, pc, p;
b = *pp++;
c = *cp++;
a = *lp++;
#ifndef PNG_SLOW_PAETH
p = b - c;
pc = a - c;
#ifdef PNG_USE_ABS
pa = abs(p);
pb = abs(pc);
pc = abs(p + pc);
#else
pa = p < 0 ? -p : p;
pb = pc < 0 ? -pc : pc;
pc = (p + pc) < 0 ? -(p + pc) : p + pc;
#endif
p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
#else /* SLOW_PAETH */
p = a + b - c;
pa = abs(p - a);
pb = abs(p - b);
pc = abs(p - c);
if (pa <= pb && pa <= pc)
p = a;
else if (pb <= pc)
p = b;
else
p = c;
#endif /* SLOW_PAETH */
v = *dp++ = (png_byte)((int)*rp++ - p);
sum += (v < 128) ? v : 256 - v;
if (sum > lmins) /* We are already worse, don't continue. */
break;
}
sum = png_setup_paeth_row(png_ptr, bpp, row_bytes, lmins);
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
@@ -2965,15 +2905,19 @@ png_write_find_filter(png_structrp png_ptr, png_row_infop row_info)
if (sum < mins)
{
best_row = png_ptr->paeth_row;
mins = sum;
best_row = png_ptr->try_row;
if (png_ptr->tst_row != NULL)
{
png_ptr->try_row = png_ptr->tst_row;
png_ptr->tst_row = best_row;
}
}
}
#endif /* WRITE_FILTER */
/* Do the actual writing of the filtered row data from the chosen filter. */
png_write_filtered_row(png_ptr, best_row, row_info->rowbytes+1);
#ifdef PNG_WRITE_FILTER_SUPPORTED
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
/* Save the type of filter we picked this time for future calculations */
if (png_ptr->num_prev_filters > 0)
@@ -2987,7 +2931,7 @@ png_write_find_filter(png_structrp png_ptr, png_row_infop row_info)
png_ptr->prev_filters[j] = best_row[0];
}
#endif
#endif /* WRITE_WEIGHTED_FILTER */
#endif /* WRITE_FILTER */
}

View File

@@ -1,7 +1,7 @@
VisualStudio instructions
libpng version 1.6.17rc06 - March 23, 2015
libpng version 1.6.18beta05 - May 31, 2015
Copyright (c) 1998-2010 Glenn Randers-Pehrson

View File

@@ -2,7 +2,7 @@
<!--
* zlib.props - location of zlib source
*
* libpng version 1.6.17rc06 - March 23, 2015
* libpng version 1.6.18beta05 - May 31, 2015
*
* Copyright (c) 1998-2011 Glenn Randers-Pehrson
*

View File

@@ -1,9 +1,9 @@
Makefiles for libpng version 1.6.17rc06 - March 23, 2015
Makefiles for libpng version 1.6.18beta05 - May 31, 2015
pnglibconf.h.prebuilt => Stores configuration settings
makefile.linux => Linux/ELF makefile
(gcc, creates libpng16.so.16.1.6.17rc06)
(gcc, creates libpng16.so.16.1.6.18beta05)
makefile.gcc => Generic makefile (gcc, creates static libpng.a)
makefile.knr => Archaic UNIX Makefile that converts files with
ansi2knr (Requires ansi2knr.c from
@@ -33,12 +33,12 @@ pnglibconf.h.prebuilt => Stores configuration settings
makefile.os2 => OS/2 Makefile (gcc and emx, requires libpng.def)
makefile.sco => For SCO OSr5 ELF and Unixware 7 with Native cc
makefile.sggcc => Silicon Graphics (gcc,
creates libpng16.so.16.1.6.17rc06)
creates libpng16.so.16.1.6.18beta05)
makefile.sgi => Silicon Graphics IRIX makefile (cc, creates static lib)
makefile.solaris => Solaris 2.X makefile (gcc,
creates libpng16.so.16.1.6.17rc06)
creates libpng16.so.16.1.6.18beta05)
makefile.so9 => Solaris 9 makefile (gcc,
creates libpng16.so.16.1.6.17rc06)
creates libpng16.so.16.1.6.18beta05)
makefile.std => Generic UNIX makefile (cc, creates static libpng.a)
makefile.sunos => Sun makefile
makefile.32sunu => Sun Ultra 32-bit makefile

View File

@@ -21,7 +21,7 @@ PNG_DFN "OS2 DESCRIPTION "PNG image compression library""
PNG_DFN "OS2 CODE PRELOAD MOVEABLE DISCARDABLE"
PNG_DFN ""
PNG_DFN "EXPORTS"
PNG_DFN ";Version 1.6.17rc06"
PNG_DFN ";Version 1.6.18beta05"
#define PNG_EXPORTA(ordinal, type, name, args, attributes)\
PNG_DFN "@" SYMBOL_PREFIX "@@" name "@"

View File

@@ -11,7 +11,7 @@
# Modeled after libxml-config.
version=1.6.17rc06
version=1.6.18beta05
prefix=""
libdir=""
libs=""

View File

@@ -5,6 +5,6 @@ includedir=@includedir@/libpng16
Name: libpng
Description: Loads and saves PNG files
Version: 1.6.17rc06
Version: 1.6.18beta05
Libs: -L${libdir} -lpng16
Cflags: -I${includedir}

View File

@@ -23,7 +23,7 @@
VERMAJ = 1
VERMIN = 6
VERMIC = 17
VERMIC = 18
VER = $(VERMAJ).$(VERMIN).$(VERMIC)
NAME = libpng
PACKAGE = $(NAME)-$(VER)

View File

@@ -10,7 +10,7 @@
# Library name:
LIBNAME = libpng16
PNGMAJ = 16
RELEASE = 17
RELEASE = 18
# Shared library names:
LIBSO=$(LIBNAME).so

View File

@@ -18,7 +18,7 @@ exec_prefix=$(prefix)
# Library name:
LIBNAME = libpng16
PNGMAJ = 16
RELEASE = 17
RELEASE = 18
# Shared library names:
LIBSO=$(LIBNAME).dll

View File

@@ -17,7 +17,7 @@ INCSDIR=${LOCALBASE}/include/libpng16
LIB= png16
SHLIB_MAJOR= 0
SHLIB_MINOR= 1.6.17rc06
SHLIB_MINOR= 1.6.18beta05
SRCS= png.c pngset.c pngget.c pngrutil.c pngtrans.c pngwutil.c \
pngread.c pngrio.c pngwio.c pngwrite.c pngrtran.c \
pngwtran.c pngmem.c pngerror.c pngpread.c

View File

@@ -17,7 +17,7 @@ INCSDIR=${LOCALBASE}/include
LIB= png
SHLIB_MAJOR= 16
SHLIB_MINOR= 1.6.17rc06
SHLIB_MINOR= 1.6.18beta05
SRCS= png.c pngset.c pngget.c pngrutil.c pngtrans.c pngwutil.c \
pngread.c pngrio.c pngwio.c pngwrite.c pngrtran.c \
pngwtran.c pngmem.c pngerror.c pngpread.c

View File

@@ -11,7 +11,7 @@ LIBDIR= ${PREFIX}/lib
MANDIR= ${PREFIX}/man/cat
SHLIB_MAJOR= 16
SHLIB_MINOR= 1.6.17rc06
SHLIB_MINOR= 1.6.18beta05
LIB= png
SRCS= png.c pngerror.c pngget.c pngmem.c pngpread.c \

View File

@@ -222,7 +222,7 @@ option SET_OPTION disabled
# with png_set_option
# ARM_NEON_CHECK: (PNG_ARM_NEON == 1) compile a run-time check to see if Neon
# extensions are supported. This is poorly supported and
# deprectated - use the png_set_option API.
# deprecated - use the png_set_option API.
setting ARM_NEON_OPT
option ARM_NEON_API disabled requires ALIGNED_MEMORY enables SET_OPTION,
sets ARM_NEON_OPT 1

View File

@@ -1,8 +1,8 @@
/* libpng 1.6.17rc06 STANDARD API DEFINITION */
/* libpng 1.6.18beta05 STANDARD API DEFINITION */
/* pnglibconf.h - library build configuration */
/* Libpng version 1.6.17rc06 - March 23, 2015 */
/* Libpng version 1.6.18beta05 - May 31, 2015 */
/* Copyright (c) 1998-2014 Glenn Randers-Pehrson */
@@ -101,8 +101,6 @@
#define PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
#define PNG_SEQUENTIAL_READ_SUPPORTED
#define PNG_SETJMP_SUPPORTED
#define PNG_SET_CHUNK_CACHE_LIMIT_SUPPORTED
#define PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED
#define PNG_SET_OPTION_SUPPORTED
#define PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
#define PNG_SET_USER_LIMITS_SUPPORTED

View File

@@ -1,4 +1,4 @@
;Version 1.6.17rc06
;Version 1.6.18beta05
;--------------------------------------------------------------
; LIBPNG symbol list as a Win32 DEF file
; Contains all the symbols that can be exported from libpng