Taking abs((int8_t)s) straightly will yield 256 - s, not 255 - s. With 255, the absolute values of the negative numbers become off by one from what they should be. Also, for me, 256 behaves more often better than not: resulting PNGs are consistently smaller by several ppm. Was there any source for 255?
While we at it, it doesn't look like libpng prevents filter 0 (FILTER_NONE) from being analyzed by the minimal-sum-of-absolute-differences heuristic on a general basis, like you do. At least, if you turn a bunch of low-variation samples from [−1; 1] with the mean of 0 into an image by remapping them to [0; 255], it can fit it nicely.
sum += s < 128 ? s : (255U - s);
Taking
abs((int8_t)s)
straightly will yield256 - s
, not255 - s
. With255
, the absolute values of the negative numbers become off by one from what they should be. Also, for me,256
behaves more often better than not: resulting PNGs are consistently smaller by several ppm. Was there any source for255
?While we at it, it doesn't look like libpng prevents filter 0 (
FILTER_NONE
) from being analyzed by the minimal-sum-of-absolute-differences heuristic on a general basis, like you do. At least, if you turn a bunch of low-variation samples from [−1; 1] with the mean of 0 into an image by remapping them to [0; 255], it can fit it nicely.