scikits-image/scikits-image — pull request #193 by cgohlke (2012-05-22 07:54:58+00:00)
Title: Fix dtype.convert function

This PR tries to fix some potential issues in the dtype.convert function:

1) Signed -> unsigned conversions should clip negative values as pointed out by Tony Yu.

2) A error is raised if input floating point numbers are not in the range [0..1]. This seems unnecessarily limiting. Signed integers are already accepted. Normalized image samples can be negative or larger than one, e.g. the parts of complex number images or after filtering with Lanczos. This PR clips floating points to [0..1] or [-1..1] when converting to unsigned respectively signed integers. Similar, signed integers are converted to [-1..1], unsigned to [0..1] floating points.

3) The conversion from float to uint8 (for example) is `uint8(clip(f * 256, 0, 255))`, which has some nice properties. A more common conversion method is rounding `uint8(f * 255 + 0.5)`. This quantization is not exactly uniform but it minimizes the conversion error. It is used by DirectX, OpenGL, ImageJ, skimage <0.5, and in a number of CG articles (some references are attached). This PR changes the default conversion method in skimage back to rounding and adds an option to output uniformly quantized images. Similar considerations apply to signed conversions. There are other possible quantization/conversion methods that would be nice to have in skimage (not implemented here).

References:

[1] DirectX data conversion rules.
[2] Data Conversions. In "OpenGL ES 2.0 Specification v2.0.25", pp 7-8. Khronos Group, 2010.
[3] Proper treatment of pixels as integers. A.W. Paeth. In "Graphics Gems I", pp 249-256. Morgan Kaufmann, 1990.
[4] Dirty Pixels. J. Blinn. In "Jim Blinn's corner: Dirty Pixels", pp 47-57. Morgan Kaufmann, 1998.
[5] Quantization Error and Dithering. J. Blinn. In "Jim Blinn's corner: Dirty Pixels", pp 167-177. Morgan Kaufmann, 1998.