[IPOL discuss] RGB->gray conversion and speed comparison

Nicolas Limare nicolas.limare at cmla.ens-cachan.fr
Mon Aug 1 04:04:25 CEST 2011

> libpng bug reported here:
> https://sourceforge.net/tracker/?func=detail&aid=3381606&group_id=5624&atid=105624

The full discussion is here:

If you want to know more about RGB->Y conversions and implementation
details, the long answers by John Bowler (who writes some of the
libpng code) are very interesting.

The conclusions from this discussion on the libpng mailing-list are:
* the correct reference is not Poynton but the sRGB standard
* only 4 digits are significant in the RGB weights
* the current weights used for the RGB->Y conversion are the result of
  a typo, but won't be changed because the maintainers don't want to
  break existing test suites, and it doesn't really matter because...
* there are some other sources of inaccuracies, for example in the
  gamma function which
  - is not the correct sRGB gamma, only a power gamma
  - is performed in 8bit integer
* libpng is not an image processing library, sometimes it wants to be
  fast, not accurate

So, for the moment I modified io_png to use the correct Rec 709
weights with 4 digits only, and I modified the integer formula to
perform rounding instead of truncation (with bitwise AND). I added a
preprocessor test to use long int if int is <24 bits (I prefer to
avoid int32 types, they are from C99, not C89).

Later, I think the correct thing to do is
* read the png file as float
* perform gamma correction (with the correct sRGB gamma function) to
  get to linear color space
* convert to CIE Y
* optionally quantize when an integer output array is wanted

This will be slower, but accurate. As explained by John Bowler, we
can't pass the gamma step with accuracy if we don't work with
floating-point. One benefit will be that I can eslily implement 16bit
png read/write functions with this unified float processing.

Nicolas LIMARE - CMLA - ENS Cachan    http://www.cmla.ens-cachan.fr/~limare/
IPOL - image processing on line                          http://www.ipol.im/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://tools.ipol.im/mailman/archive/discuss/attachments/20110801/39a5f505/attachment.pgp>

More information about the discuss mailing list