RC RANDOM CHAOS

Divide by 255 or 256? The Quiet Debate Behind Image Float Conversion

· via Hacker News

Original source

Should you normalize RGB values by 255 or 256?

Hacker News →

When converting 8-bit color values to floating point, programmers face a subtle choice: divide by 255 (mapping 0→0.0 and 255→1.0) or add a 0.5 bias and divide by 256 (placing each float at the midpoint between integers). The standard divide-by-255 approach is what GPUs use and has the practical benefit that black is exactly 0.0 and white is exactly 1.0, letting downstream code stay agnostic to bit depth. But it produces a quantizer whose extreme bins extend beyond [0,1], meaning uniform random floats round to 0 and 255 half as often as other values.

The divide-by-256 alternative corresponds to a textbook mid-tread quantizer with exact float representations (128 maps cleanly to 0.5) and uniform bin widths, which simplifies dithering and avoids edge-case handling when adding noise. The divide-by-255 method is technically a mid-riser quantizer awkwardly applied to unsigned data with only 255 levels, wasting a small amount of representational range.

In practice the round-off error in the standard approach sits at the least significant bit of a 32-bit float, well below any perceptual or computational threshold. The author concludes the debate is largely aesthetic: the standard formula wins on ergonomics, while the alternative wins on mathematical tidiness and a few niche signal-processing use cases.

Read the full article

Continue reading at Hacker News →

This is an AI-generated summary. Read the original for the full story.