Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

@Guyygarty has right! with integer division if C = A /B then A = C * B + A % B

As suggested by @LBerger I would suggest to reduce the effect of the error. Take a look at histogram. Scaling from 16bit to 8bit you collapse N bins into 1 bin. Where N=maxval/256. Going back to 16bit your histogram will have again 256 bins but separated by a gap of N bins.... get lost maxval-256 bins !

To reduce the error you could try to reduce N. What if you have just 1 pixels=maxval but all others are really lower ? Why to scale from 0 to maxval even if you don't have pixels at 0 ?

For example you could use cumulative distribution to select a good range for scaling. N=(maxgood-mingood)/256 where maxgood is the value greater than 99% of your pixels and mingood is lower that 1% of your pixels. Yes, pixels below mingood will become 0, pixels upper than maxgood will become 255 but almost 98% of your pixels will be scaled using more accurate step.

Is the same of histogram clipping in mine BrightnessAndContrastAuto. You could adapt it for 16 bit images than get resulting maxvalue and proceed as your code.