Suggestion: HCL -> RGB conversion enhancement

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
ippei
Posts: 5
Joined: 2012-08-13T04:26:41-07:00
Authentication code: 67789
Location: Tokyo, JAPAN

Suggestion: HCL -> RGB conversion enhancement

Post by ippei » 2012-08-15T03:01:25-07:00

related to viewtopic.php?f=2&t=21646

Since HCL colorspace is wider than RGB, we should choose a saturation strategy when converting into RGB colorspace. Existing code simply clamps into [0, 1], not so bad.
But it's more useful if hue and luma are preserved, as my code below does. (patch for branches/ImageMagick-6)
As as result, Colorize composite operator will be more compatible with Photoshop's "color" blending mode. Please take it into account.

Code: Select all

--- a/magick/gem.c
+++ b/magick/gem.c
@@ -93,5 +93,6 @@
     m,
     r,
-    x;
+    x,
+    z;

   /*	
@@ -143,7 +144,21 @@
               }
   m=luma-(0.298839*r+0.586811*g+0.114350*b);
-  *red=ClampToQuantum(QuantumRange*(r+m));
-  *green=ClampToQuantum(QuantumRange*(g+m));
-  *blue=ClampToQuantum(QuantumRange*(b+m));
+  /* we should choose saturation strategy to clip it into the RGB cube; */
+  /* hue and luma are always preserved and chroma may be changed. */
+  z=1.0;
+  if (m < 0.0)
+    {
+      z=luma/(luma-m);
+      m=0.0;
+    }
+  else
+    if (m+c > 1.0)
+      {
+        z=(1.0-luma)/(m+c-luma);
+        m=1.0-z*c;
+      }
+  *red=ClampToQuantum(QuantumRange*(z*r+m));
+  *green=ClampToQuantum(QuantumRange*(z*g+m));
+  *blue=ClampToQuantum(QuantumRange*(z*b+m));
 }
 ^L

User avatar
magick
Site Admin
Posts: 10980
Joined: 2003-05-31T11:32:55-07:00

Re: Suggestion: HCL -> RGB conversion enhancement

Post by magick » 2012-08-15T06:11:53-07:00

Thanks for the patch. Look for it in ImageMagick 6.7.9-0 Beta by sometime tomorrow.

Post Reply