aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <m@maandree.se>2025-11-26 19:01:38 +0100
committerMattias Andrée <m@maandree.se>2025-11-26 19:01:38 +0100
commit7d15d0c247954edb224b35b7bfb5e8fe5d5d4fb7 (patch)
treefc60b55a73e22b26b4bae6947017e036b25f0ec9
parentUpdate README (diff)
downloadlibquanta-7d15d0c247954edb224b35b7bfb5e8fe5d5d4fb7.tar.gz
libquanta-7d15d0c247954edb224b35b7bfb5e8fe5d5d4fb7.tar.bz2
libquanta-7d15d0c247954edb224b35b7bfb5e8fe5d5d4fb7.tar.xz
Refine implementation of Wu's Colour QuantiserHEADmaster
Signed-off-by: Mattias Andrée <m@maandree.se>
-rw-r--r--libquanta_vquantise_wu.c15
1 files changed, 10 insertions, 5 deletions
diff --git a/libquanta_vquantise_wu.c b/libquanta_vquantise_wu.c
index 55129f2..d148c96 100644
--- a/libquanta_vquantise_wu.c
+++ b/libquanta_vquantise_wu.c
@@ -8,7 +8,8 @@
struct channel_data {
const struct libquanta_channel *ch;
size_t subindex_multiplier;
- size_t colour_shift;
+ size_t colour_shift_down;
+ size_t colour_shift_up;
struct bigint *histogram;
struct bigint whole;
struct bigint half;
@@ -63,7 +64,8 @@ compute_histogram(struct libquanta_image *image, struct histogram *histogram, st
else
abort();
square_subsum += (long double)v * (long double)v;
- v >>= channels[ch].colour_shift;
+ v >>= channels[ch].colour_shift_down;
+ v <<= channels[ch].colour_shift_up;
v += 1U;
index += v * channels[ch].subindex_multiplier;
bigint_add_small(&channels[ch].histogram[index], v);
@@ -462,8 +464,8 @@ maximise(const struct cube *cube, size_t channel, const struct histogram *histog
base_weight = bottom_over_occurrences(cube, channel, histogram, channels, nchannels);
first = cube->bounds[channel].min + 1U;
- last = cube->bounds[channel].max;
- for (pos = first; pos < last; pos++) { /* TODO should `last` really be exclusive? */
+ last = cube->bounds[channel].max; /* yes, the maximum should be excluded */
+ for (pos = first; pos < last; pos++) {
half_weight = top_over_occurrences(cube, channel, pos, histogram, channels, nchannels);
half_weight += base_weight;
if (!half_weight)
@@ -698,7 +700,10 @@ libquanta_vquantise_wu(struct libquanta_palette *palette, va_list args)
subindex_multiplier = 1U;
for (i = nchannels; i--; subindex_multiplier *= ((1U << WORKING_BITS) + 1U)) {
channels[i].subindex_multiplier = subindex_multiplier;
- channels[i].colour_shift = channels[i].ch->bits_in > WORKING_BITS ? channels[i].ch->bits_in - WORKING_BITS : 0U;
+ if (channels[i].ch->bits_in > WORKING_BITS)
+ channels[i].colour_shift_down = channels[i].ch->bits_in - WORKING_BITS;
+ else if (channels[i].ch->bits_in < WORKING_BITS)
+ channels[i].colour_shift_up = WORKING_BITS - channels[i].ch->bits_in;
}
for (i = 0; i < nchannels; i++) {
channels[i].histogram = calloc(subindex_multiplier, sizeof(*channels[i].histogram));