mirror of
https://git.code.sf.net/p/libpng/code.git
synced 2025-07-10 18:04:09 +02:00
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:
78
pngwutil.c
78
pngwutil.c
@@ -3918,6 +3918,12 @@ typedef struct filter_selector
|
||||
* filling the entire window (i.e. the maximum number that can be fitted
|
||||
* in (window-1) bytes).
|
||||
*/
|
||||
png_uint_32 sum_bias[PNG_FILTER_VALUE_LAST];
|
||||
/* For each filter a measure of its goodness in the filter sum
|
||||
* calculation. This allows filter selection based on the
|
||||
* sum-of-absolute-dfferences method to be biased to favour particular
|
||||
* filters. There was no such bias before 1.7
|
||||
*/
|
||||
png_uint_32 distance; /* Distance from beginning */
|
||||
png_codeset codeset; /* Set of seen codes */
|
||||
png_uint_32 code_distance[256]; /* Distance at last occurence */
|
||||
@@ -3942,6 +3948,7 @@ png_start_filter_select(png_zlib_statep ps, unsigned int bpp)
|
||||
ps->filter_select_window = window = PNG_FILTER_SELECT_WINDOW_MAX;
|
||||
|
||||
fs->code_count = 0;
|
||||
memset(fs->sum_bias, 0U, sizeof fs->sum_bias);
|
||||
|
||||
/* This is the maximum row width, in pixels, of a row which fits and
|
||||
* leaves 1 byte free in the window. For any bigger row filter
|
||||
@@ -3975,17 +3982,31 @@ typedef struct
|
||||
/* Per-filter data. This remains separate from the above until the filter
|
||||
* selection has been made. It reflects the above however the codeset only
|
||||
* records codes present in this row.
|
||||
*
|
||||
* The 'sum' fields are the sum of the absolute deviation of each code from
|
||||
* 0, the algorithm from 1.6 and earlier. In other words:
|
||||
*
|
||||
* if (code >= 128)
|
||||
* sum += code;
|
||||
* else
|
||||
* sum += 256-code;
|
||||
*/
|
||||
unsigned int code_count; /* Number of distinct codes seen in row */
|
||||
unsigned int new_code_count; /* Number of new codes seen in row */
|
||||
png_uint_32 sum_low; /* Low 31 bits of code sum */
|
||||
png_uint_32 sum_high; /* High 32 bits of code sum */
|
||||
png_codeset codeset; /* Set of codes seen in this row */
|
||||
png_uint_32 code_distance[256]; /* Distance at last occurence in this row */
|
||||
} filter_data;
|
||||
|
||||
static void
|
||||
filter_data_init(filter_data *fd, png_uint_32 distance, unsigned int filter)
|
||||
filter_data_init(filter_data *fd, png_uint_32 distance, unsigned int filter,
|
||||
unsigned int code_is_set)
|
||||
{
|
||||
fd->code_count = 1U;
|
||||
fd->new_code_count = !code_is_set;
|
||||
fd->sum_low = filter;
|
||||
fd->sum_high = 0U;
|
||||
memset(&fd->codeset, 0U, sizeof fd->codeset);
|
||||
PNG_CODE_SET(fd->codeset, filter);
|
||||
fd->code_distance[filter] = distance;
|
||||
@@ -4003,6 +4024,23 @@ add_code(const filter_selector *fs, filter_data *fd, png_uint_32 distance,
|
||||
if (!PNG_CODE_IS_SET(fs->codeset, code))
|
||||
++(fd->new_code_count);
|
||||
}
|
||||
|
||||
{
|
||||
png_uint_32 low = fd->sum_low;
|
||||
|
||||
if (code >= 128U)
|
||||
low += code;
|
||||
|
||||
else
|
||||
low += 256U-code;
|
||||
|
||||
/* Handle overflow into the top bit: */
|
||||
if (low & 0x80000000U)
|
||||
fd->sum_low = low & 0x7FFFFFFFU, ++fd->sum_high;
|
||||
|
||||
else
|
||||
fd->sum_low = low;
|
||||
}
|
||||
}
|
||||
|
||||
static png_byte
|
||||
@@ -4139,7 +4177,8 @@ select_filter(png_zlib_statep ps, png_const_bytep row,
|
||||
|
||||
distance = fs->distance;
|
||||
filter_data_init(fd+PNG_FILTER_VALUE_NONE, distance++,
|
||||
PNG_FILTER_VALUE_NONE);
|
||||
PNG_FILTER_VALUE_NONE,
|
||||
PNG_CODE_IS_SET(fs->codeset, PNG_FILTER_VALUE_NONE));
|
||||
|
||||
if (bpp >= 8) /* complete bytes */
|
||||
{
|
||||
@@ -4192,7 +4231,8 @@ select_filter(png_zlib_statep ps, png_const_bytep row,
|
||||
|
||||
for (i=PNG_FILTER_VALUE_NONE+1U; i<PNG_FILTER_VALUE_LAST; ++i)
|
||||
if (PNG_FILTER_MASK(i) & filters)
|
||||
filter_data_init(fd+i, distance, i);
|
||||
filter_data_init(fd+i, distance, i,
|
||||
PNG_CODE_IS_SET(fs->codeset, i));
|
||||
}
|
||||
|
||||
++distance;
|
||||
@@ -4297,16 +4337,40 @@ select_filter(png_zlib_statep ps, png_const_bytep row,
|
||||
* adjacent values, the assumption is that this will result in values
|
||||
* close to 0.
|
||||
*/
|
||||
{
|
||||
png_uint_32 high = -1;
|
||||
png_uint_32 low = -1;
|
||||
unsigned int min_f = 0 /*unset, but safe*/;
|
||||
unsigned int f;
|
||||
|
||||
for (f=PNG_FILTER_VALUE_NONE; f<PNG_FILTER_VALUE_LAST; ++f)
|
||||
if ((PNG_FILTER_MASK(f) & filters) != 0 &&
|
||||
(fd[f].sum_high < high ||
|
||||
(fd[f].sum_high == high && fd[f].sum_low < low)))
|
||||
{
|
||||
high = fd[f].sum_high;
|
||||
/* The bias is the per-filter bias, a measure of the preference
|
||||
* of this filter over the others.
|
||||
*/
|
||||
low = fd[f].sum_low - fs->sum_bias[f];
|
||||
|
||||
/* The final fallback is to use the 'none' filter. */
|
||||
return filter_data_select(ps, fd, PNG_FILTER_VALUE_NONE, distance, width);
|
||||
if (low & 0x80000000U)
|
||||
{
|
||||
low &= 0x7FFFFFFFU, --high;
|
||||
if (high & 0x80000000U)
|
||||
low = high = 0U;
|
||||
}
|
||||
|
||||
min_f = f;
|
||||
}
|
||||
|
||||
return filter_data_select(ps, fd, min_f, distance, width);
|
||||
}
|
||||
}
|
||||
|
||||
debug(filters < PNG_FILTER_VALUE_LAST);
|
||||
# undef png_ptr
|
||||
|
||||
return ps->filters = filters;
|
||||
# undef png_ptr
|
||||
}
|
||||
#else /* !SELECT_FILTER */
|
||||
/* Filter selection not being done, just call png_write_start_row: */
|
||||
|
||||
Reference in New Issue
Block a user