From e8feb20e39f9bea766c8a713004a8dae9adae91d Mon Sep 17 00:00:00 2001 From: Marcin Kowalczyk Date: Wed, 2 Mar 2016 12:45:54 +0100 Subject: [PATCH] Fix FindClosestDiscretized in near lossless: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - The result is now indeed closest among possible results for all inputs, which was not the case for bits>4, where the mapping was not even monotonic because GetValAndDistance was correct only if the significant part of initial fit in a byte at most twice. - The set of results for a larger number of bits dropped is a subset of values for a smaller number of bits dropped. This implies that subsequent discretizations for a smaller number of bits dropped do not change already discretized pixels, which improves the quality (changes do not accumulate) and compression density (values tend to repeat more often). - Errors are more fairly distributed between upwards and downwards thanks to bankers’ rounding, which avoids images getting darker or lighter in overall. - Deltas between discretized values are more repetitive. This improves compression density if delta encoding is used. Also, the implementation is much shorter now. Change-Id: I0a98e7d5255e91a7b9c193a156cf5405d9701f16 --- src/enc/near_lossless.c | 43 ++++++++--------------------------------- 1 file changed, 8 insertions(+), 35 deletions(-) diff --git a/src/enc/near_lossless.c b/src/enc/near_lossless.c index 23464596..0074d4a7 100644 --- a/src/enc/near_lossless.c +++ b/src/enc/near_lossless.c @@ -14,6 +14,7 @@ // Author: Jyrki Alakuijala (jyrki@google.com) // Converted to C by Aleksander Kramarz (akramarz@google.com) +#include #include #include "../dsp/lossless.h" @@ -23,42 +24,14 @@ #define MIN_DIM_FOR_NEAR_LOSSLESS 64 #define MAX_LIMIT_BITS 5 -// Computes quantized pixel value and distance from original value. -static void GetValAndDistance(int a, int initial, int bits, - int* const val, int* const distance) { - const int mask = ~((1 << bits) - 1); - *val = (initial & mask) | (initial >> (8 - bits)); - *distance = 2 * abs(a - *val); -} - -// Clamps the value to range [0, 255]. -static int Clamp8b(int val) { - const int min_val = 0; - const int max_val = 0xff; - return (val < min_val) ? min_val : (val > max_val) ? max_val : val; -} - -// Quantizes values {a, a+(1<> 1) + ((a >> bits) & 1); + assert(bits > 0); + if (biased > 0xff) return 0xff; + return biased & ~mask; } // Applies FindClosestDiscretized to all channels of pixel.