Kennedy said:
For the situation in the subject thread, I contest that integer division
by 256 (or a shift right by 8 bits) is perfect because it scales the
original number of states into the target number of states with equal
distribution and weighting throughout the full range. No other method
suggested so far achieves this property and the only arguments put
forward for their alleged superiority is a reference to some undefined -
and apparently undefinable - error magnitude.
While from a programmers view (this is why I don't like people who a
plain programmers) you're right. 256 values here, 16 there, equal
distribution and a linear histogram. That's all what counts for a
programmer.
Unfortunately there's some thing called 'reality'. And real life isn't
linear at all.
Not to mention the fact that human visual perception is far from being
anything like linear at all.
Using the comparative conversion suggested by Jens-Michael of an 8-bit
image to 4-bit for simplicity, the perfection of the integer division is
immediately apparent. Simply create a ramp from peak black to peak
white across and image 256 pixels wide. Then convert the image to 4-bit
data using either integer division by 16 or the equivalent of the
alternative method you argue. Ignoring your obvious computational
errors above, I suspect that this reduces to the function int((source +
8)/17) as suggested by Jens-Michael.
The two images are significantly different. Using simple integer
division by 16 and truncation, the full range from black to white is
produced with an even population - as would be expected from a linear
ramp original, which also has an even population. Each colour in the
range from 0 to 15 is represented by exactly 16 pixels wide.
And here the 'programmers intellectual limitation' kicks in.
yes, the result is no equal distribution. Yes, black and white are
represented by 8 values each (not 9!) and the rest with 17 instead of
the 'informatically correct' 16 values, BUT...
With an equal distribution, zero (black) represents the values 0..15,
very dark gray would be 16..31
Looking at the original value, 15 isn't black at all anymore. It is far
more a very dark gray than black. It is very close to 16 and very
distant from 0. But is treated as if it would have been 0 all the time.
With a non-linear distribution, 15 is mapped to 1 (and therefore much
closer to the original 16 than with just truncation).
This is why the typical error is half as big as with truncation.
With truncation you jump from one level to the next at hte point of its
best fit. So when a value appears first, it matches 100% and then
matches worse and worse and reaches worst match the moment it jumps to
the next level where it then fits best again.
With the '/257 and round' mehtod, a vlaue jumps to the next when it
crosses the border where the original value is equally far away from
both possible mappings.
The results are indeed very different.
And after staring a couple of hours onto the result of both (and other
even more complex) methods when writing my own scanner software, i
decided that the '/257 and round' method gives by far the best results -
as far as my personal opinion is allowed.
And since the result of any image operation is intended to please the
user and not the programmer (or the computer on which the conversion
takes place), I prefer the method which gives the (by far) better result
over a method that gives a linear histogram or an equal distribution of
values or just pleases a programmers heart.
By examining the resulting data from exactly this test it is very clear
that the reduced error argument for the alternative to simple integer
division is false, because it ignores one basic fact:
By examining the resulting data from exactly this test it is very clear
that it ignores one basic fact:
The resulting data is irrelevant, the resulting image is relevant. And
how it is perceived by the user who wants to do the conversion.
The 'reduced error argument' is not false, it is interpreted wrongly by
you. The error range in case of truncation is 0..1, while in case of
rounding it is -0.5..0.5. The error is NEVER bigger than 0.5. This is a
mathematical fact (as you seem to believe mathematics more than your own
eyes). The average value alone is unimportant because all values correct
and one being completely off (cannot happen in this case) would give
also a good average error value but a extremely poor visual result.
I guess you never did the 8->4 conversion with a real image, only on
paper. If you'd done it with a real image, you could really judge what
I'm talking about.
And there is only one result: the linear distribution of values leads to
a much worse optical result than the rounding method. Ask Einstein, why.
Period. (And wonder why there is no Nobel prize for Mathematics - Nobel
knew what he did.)
Grossibaer