Filter selection, palette index checks

Palette index checking: checking on read was erroneously skipped.  pngcp can now
turn off the palette index checking or fix the errors (mainly to allow
comparison with libpng 1.6, which defaulted to ignoring it).  The API now
documents how the 'enabled' parameter works.  On read the check is on by default
(it was apparently off in libpng 1.6) however now if explicitly turned on the
warning message is stopped, this provides better interaction with
get_palette_max at the cost of a small API change (may need to be reviewed.)

Palette size errors: invalid entries in the palette are now set to #beaded to
make the errors more obvious and allow easy detection in memory.

Read transform pipeline caching: changes to fix the palette index check (which
are erroneously cached), the pipeline can now contain multiple caches.  E.g.
caching of packing of palette indices can be combined with caching of palette
RGB transforms.

Read code now checks for callbacks to read 0 data (and faults them).  Fixed the
reading of PNGs with multiple 0 length IDATs that failed as a result plus the
handling of zero length unknown.  (Which occurs, validly; the spurious warning
has been removed).

filter selection: the 1.6 and earlier sum of absolute differences algorithm has
been reintroduced with an option to disfavor some filters over others where the
sums are close (not yet exposed).  The selection code also logs the last known
occurence of each possible byte code across multiple lines.  This allows
detection of PNG images with lower bit depth than the format implies and,
therefore, allows the filtering to be turned off in those cases as well.

The default write zlib settings are still lower than libpng16.  The selection
algorithm is being refined (the overall results are worse than not doing any
filtering).

Signed-off-by: John Bowler <jbowler@acm.org>
This commit is contained in:
John Bowler
2016-05-15 16:32:01 -07:00
parent cf46e3748f
commit 5652acdd48
13 changed files with 298 additions and 98 deletions

16
png.h
View File

@@ -2885,9 +2885,17 @@ PNG_EXPORT(207, void, png_save_uint_16, (png_bytep buf, unsigned int i));
#ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED
PNG_EXPORT(242, void, png_set_check_for_invalid_index,
(png_structrp png_ptr, int enabled_if_greater_than_0));
/* By default the check is enabled on both read and write, passing a value
* which is 0 or negative disables the check. Disabling the check also
* prevents the following API from working.
/* By default the check is enabled on both read and write when the number of
* entries in the palette is less than the maximum required by the bit depth
* of a palette image.
*
* Passing 1 to 'enabled' turns the check on in all cases.
* Passing -1 turns it off and the PNG may have invalid palette index values.
* Passing 0 restores the default.
*
* On read chunk (benign) error messages are only produced with the default
* setting; it is assumed that when the check is turned on explicitly the
* caller will call png_get_palette_max to check the result.
*/
#endif /* CHECK_FOR_INVALID_INDEX */
#ifdef PNG_GET_PALETTE_MAX_SUPPORTED
@@ -2895,7 +2903,7 @@ PNG_EXPORT(243, int, png_get_palette_max, (png_const_structrp png_ptr,
png_const_inforp info_ptr));
/* The info_ptr is not used, it may be NULL in 1.7.0 (not in earlier
* versions). If the information is not available because
* png_set_check_for_invalid_index disabled the check this API returns -1.
* png_set_check_for_invalid_index was not used to turn it on -1 is returned.
* Valid results can only be obtained after the complete image has been read,
* though it may be called at any time to get the result so far.
*/