OpenImageIO/oiio — pull request #795 by lgritz (2014-01-29 06:16:55+00:00)
Title: Cleanup of quantization and add optional dithering on output to uint8 files

The default is no dither -- same as it's always been. It may be useful to
use dither when outputting to 8 bit files, to reduce banding and visible
artifacts. Using the same dither seed on multiple images of the same
resolution will result in the same reproducible dither pattern; intentionally
using different seeds for different images (suggestion: using a hash of
the output filename) can ensure no correlation of dither between separate
images. Which choice (or whether to dither) is up to the application and may
depend on the intended use of the images.

The implementation consists of the following changes:

* Eliminate the awkward quant_* fields from ImageSpec. We never used
them in any way other than the default conversions for each data
format type. A bunch of the quantization math and internal utility
functions related to quantization can be simplified.

* New add_dither() utility function will add dither of a given amplitude
to an array of float pixel values. Parameters include a 'dither' seed
for the hash that determines the individual per-pixel dither amounts,
as well as [xyz]origin parameters that place the pixels in the proper
place in the overall dither pattern.

* Add optional dither and [xyz]origin parameters to the ImageOutput
to_native_scanline, to_native_tile, and to_native_rectangle utility
methods. The default is 0, which is old behavior. If dither is
nonzero, it will use the add_dither() function to add dither prior to
any conversion from float to uint8 (that's the only case), passing
along the dither seed and origin values.

* All the image writers (TIFF, JPEG, etc.) that output uint8 images will
notice a "oiio:dither" metadata, which if nonzero is interpreted as a
request to dither float->uint8 output, using the value as the dither
pattern seed.

* Add --dither flag to oiiotool, off by default, but when used will
cause dithering to be added when outputting uint8 images.