Problem when quantizing values during dithering operation
I was recently playing around with GIMP and noticed something that I think is a bug. In the Colors->Dither dialog, if you select 2 levels on each channel and the Floyd-Steinberg method, and you apply this operation to, for example, the following image
the result is this image
and, as you can see, there are 3 different colors and not 2. Similar behavior occurs with the other methods.
I am not 100% sure what 'levels' means, but I interpret it as the total number of different values that any given channel can have. If my interpretation is correct, then this is actually a bug, and the fix is pretty easy:
Line 144 of file operations/common/dither.c
is
quantized = quantize_value ((guint) (value_clamped + 0.5 * 65536 / channel_levels[ch] ), channel_levels [ch]);
but should be instead
quantized = quantize_value ((guint) (value_clamped + 0.5 * 65536 / (channel_levels[ch]-1) ), (channel_levels [ch]-1));
Similar changes should be applied to (at least) lines 224, 258, 291, 326, 360, 392, 424 and 448 in the same file.