RGB to Lab colorspace conversion

[email protected] (Joe Lencioni) Mon, 25 Jan 2010 16:41:03 -0600
Newsgroups php.gd.devel
Message-ID <[email protected]>
First of all, thank you for developing GD. It is an indispensable tool for
working with images in PHP.

I am working on an image resizing and cropping script that attempts to
detect which areas of an image are more important when making cropping
decisions--essentially asking the computer to determine perceptual color
differences. To accomplish this, I convert pixels from the RGB colorspace
into Lab colorspace and compare the differences. (
http://en.wikipedia.org/wiki/Lab_color_space#Advantages_of_Lab). While I can
make this happen in PHP, it is slow and uses too much memory. I would love
to see functions to facilitate this conversion and comparison directly in
GD.

To convert from RGB to Lab, you first need to convert from RGB to XYZ
colorspace and then from XYZ to Lab colorspace. Here are the functions that
I use for conversion in PHP (which have been derived from the formulas on
the EasyRGB website http://www.easyrgb.com/index.php?X=MATH):

GD color index to RGB:
http://code.google.com/p/smart-lencioni-image-resizer/source/browse/trunk/slirimage.class.php?spec=svn59&r=46#755
RGB to XYZ:
http://code.google.com/p/smart-lencioni-image-resizer/source/browse/trunk/slirimage.class.php?spec=svn59&r=46#770

XYZ to CIE-L*a*b* (more complex):
http://code.google.com/p/smart-lencioni-image-resizer/source/browse/trunk/slirimage.class.php?spec=svn59&r=46#825
or
XYZ to Hunter-Lab (less complex):
http://code.google.com/p/smart-lencioni-image-resizer/source/browse/trunk/slirimage.class.php?spec=svn59&r=46#806

And comparing the colors (more info at
http://en.wikipedia.org/wiki/Color_difference):

deltaE 2000 (complex):
http://code.google.com/p/smart-lencioni-image-resizer/source/browse/trunk/slirimage.class.php?spec=svn59&r=46#874
or
deltaE 76 (simple):
http://code.google.com/p/smart-lencioni-image-resizer/source/browse/trunk/slirimage.class.php?spec=svn59&r=46#857
or
deltaE CMC:
http://code.google.com/p/smart-lencioni-image-resizer/source/browse/trunk/slirimage.class.php?spec=svn59&r=46#967

For accuracy purposes, I would prefer to be able to use the more complex
equations. However, if they prove to be too slow, it would be nice to fall
back on the simpler versions.

So my question is this: does this kind of functionality make sense to
include in GD (I hope so)? If so, will you add it? I am unfamiliar with C,
otherwise I would submit the code, but hopefully it won't be too much work
to convert the PHP functions I have provided here.

Thanks!
Joe