[gimp] plug-ins: don't use gimp_rgb_to_hsv_int() and gimp_hsv_to_rgb_int()



commit befe80bd08c2c141a330af869221eccf6aa68b80
Author: Michael Natterer <mitch gimp org>
Date:   Mon Oct 22 12:50:11 2018 +0200

    plug-ins: don't use gimp_rgb_to_hsv_int() and gimp_hsv_to_rgb_int()
    
    Use the default APIs using GimpRGB and GimpHSV structs instead.
    
    This also changes/fixes the colors in all affected plug-ins which were
    broken/restricted in hue range ever since the _int() functions were
    changed from a 0..255 to 0..360 hue range 15 years ago in commit
    d93c2f61c8fd68c58a150eda7b43009badec6ce6.

 plug-ins/common/cml-explorer.c           | 84 +++++++++++++++++++++++---------
 plug-ins/common/sparkle.c                | 37 +++++++-------
 plug-ins/gradient-flare/gradient-flare.c | 25 ++++++----
 3 files changed, 96 insertions(+), 50 deletions(-)
---
diff --git a/plug-ins/common/cml-explorer.c b/plug-ins/common/cml-explorer.c
index 8d57ec4372..f5d531150c 100644
--- a/plug-ins/common/cml-explorer.c
+++ b/plug-ins/common/cml-explorer.c
@@ -746,17 +746,19 @@ CML_main_function (gboolean preview_p)
            index < MIN (cell_num, width_by_pixel / VALS.scale);
            index++)
         {
-          guchar buffer[4];
-          int   rgbi[3];
-          int   i;
+          guchar  buffer[4];
+          GimpRGB rgb;
+          GimpHSV hsv;
 
           gimp_pixel_rgn_get_pixel (&src_rgn, buffer,
                                     x + (index * VALS.scale), y);
-          for (i = 0; i < 3; i++) rgbi[i] = buffer[i];
-          gimp_rgb_to_hsv_int (rgbi, rgbi + 1, rgbi + 2);
-          hues[index] = (gdouble) rgbi[0] / (gdouble) 255;
-          sats[index] = (gdouble) rgbi[1] / (gdouble) 255;
-          vals[index] = (gdouble) rgbi[2] / (gdouble) 255;
+
+          gimp_rgb_set_uchar (&rgb, buffer[0], buffer[1], buffer[2]);
+          gimp_rgb_to_hsv (&rgb, &hsv);
+
+          hues[index] = hsv.h;
+          sats[index] = hsv.s;
+          vals[index] = hsv.v;
         }
     }
 
@@ -795,7 +797,21 @@ CML_main_function (gboolean preview_p)
           v = b = CANNONIZE (VALS.val, vals[dx]);
 
           if (! dest_is_gray)
-            gimp_hsv_to_rgb_int (&r, &g, &b);
+            {
+              GimpHSV hsv;
+              GimpRGB rgb;
+
+              gimp_hsv_set (&hsv,
+                            (gdouble) h / 255.0,
+                            (gdouble) s / 255.0,
+                            (gdouble) v / 255.0);
+
+              gimp_hsv_to_rgb (&hsv, &rgb);
+
+              r = ROUND (rgb.r * 255.0);
+              g = ROUND (rgb.g * 255.0);
+              b = ROUND (rgb.b * 255.0);
+            }
 
           /* render destination */
           for (offset_y = 0;
@@ -821,12 +837,29 @@ CML_main_function (gboolean preview_p)
                       }
                     else
                       {
-                        gimp_rgb_to_hsv_int (rgbi, rgbi + 1, rgbi + 2);
+                        GimpRGB rgb;
+                        GimpHSV hsv;
+
+                        rgb.r = (gdouble) rgbi[0] / 255.0;
+                        rgb.g = (gdouble) rgbi[1] / 255.0;
+                        rgb.b = (gdouble) rgbi[2] / 255.0;
+
+                        gimp_rgb_to_hsv (&rgb, &hsv);
+
+                        if (VALS.hue.function != CML_KEEP_VALUES)
+                          hsv.h = (gdouble) h / 255.0;
 
-                        r = (VALS.hue.function == CML_KEEP_VALUES) ? rgbi[0] : h;
-                        g = (VALS.sat.function == CML_KEEP_VALUES) ? rgbi[1] : s;
-                        b = (VALS.val.function == CML_KEEP_VALUES) ? rgbi[2] : v;
-                        gimp_hsv_to_rgb_int (&r, &g, &b);
+                        if  (VALS.sat.function != CML_KEEP_VALUES)
+                          hsv.s = (gdouble) s / 255.0;
+
+                        if (VALS.val.function != CML_KEEP_VALUES)
+                          hsv.v = (gdouble) v / 255.0;
+
+                        gimp_hsv_to_rgb (&hsv, &rgb);
+
+                        r = ROUND (rgb.r * 255.0);
+                        g = ROUND (rgb.g * 255.0);
+                        b = ROUND (rgb.b * 255.0);
                       }
                   }
 
@@ -1175,7 +1208,6 @@ CML_explorer_dialog (void)
   GtkWidget *hbox;
   GtkWidget *vbox;
   GtkWidget *frame;
-  GtkWidget *abox;
   GtkWidget *bbox;
   GtkWidget *button;
   gboolean   run;
@@ -1775,6 +1807,8 @@ function_graph_draw (GtkWidget *widget,
 {
   gint       x, y;
   gint       rgbi[3];
+  GimpHSV    hsv;
+  GimpRGB    rgb;
   gint       channel_id = GPOINTER_TO_INT (data[0]);
   CML_PARAM *param      = data[1];
 
@@ -1788,14 +1822,20 @@ function_graph_draw (GtkWidget *widget,
       rgbi[0] = rgbi[1] = rgbi[2] = 127;
       if ((0 <= channel_id) && (channel_id <= 2))
         rgbi[channel_id] = CANNONIZE ((*param), ((gdouble) x / (gdouble) 255));
-      gimp_hsv_to_rgb_int (rgbi, rgbi+1, rgbi+2);
+
+      hsv.h = (gdouble) rgbi[0] / 255.0;
+      hsv.s = (gdouble) rgbi[1] / 255.0;
+      hsv.v = (gdouble) rgbi[2] / 255.0;
+
+      gimp_hsv_to_rgb (&hsv, &rgb);
+
       for (y = 0; y < GRAPHSIZE; y++)
-      {
-        GIMP_CAIRO_RGB24_SET_PIXEL((img+(y*img_stride+x*4)),
-                                   rgbi[0],
-                                   rgbi[1],
-                                   rgbi[2]);
-      }
+        {
+          GIMP_CAIRO_RGB24_SET_PIXEL((img+(y*img_stride+x*4)),
+                                     ROUND (rgb.r * 255.0),
+                                     ROUND (rgb.g * 255.0),
+                                     ROUND (rgb.b * 255.0));
+        }
     }
 
   cairo_surface_mark_dirty (buffer);
diff --git a/plug-ins/common/sparkle.c b/plug-ins/common/sparkle.c
index 08880dcd4c..3fbfd6b271 100644
--- a/plug-ins/common/sparkle.c
+++ b/plug-ins/common/sparkle.c
@@ -1027,7 +1027,6 @@ fspike (GimpPixelRgn *src_rgn,
   gdouble        in;
   gdouble        theta;
   gdouble        sfac;
-  gint           r, g, b;
   GimpTile      *tile = NULL;
   gint           row, col;
   gint           i;
@@ -1090,30 +1089,32 @@ fspike (GimpPixelRgn *src_rgn,
 
       if (svals.random_hue > 0.0 || svals.random_saturation > 0.0)
         {
-          r = 255 - color[0];
-          g = 255 - color[1];
-          b = 255 - color[2];
+          GimpRGB rgb;
+          GimpHSV hsv;
 
-          gimp_rgb_to_hsv_int (&r, &g, &b);
+          rgb.r = (gdouble) (255 - color[0]) / 255.0;
+          rgb.g = (gdouble) (255 - color[1]) / 255.0;
+          rgb.b = (gdouble) (255 - color[2]) / 255.0;
 
-          r += svals.random_hue * g_rand_double_range (gr, -0.5, 0.5) * 255;
+          gimp_rgb_to_hsv (&rgb, &hsv);
 
-          if (r >= 255)
-            r -= 255;
-          else if (r < 0)
-            r += 255;
+          hsv.h += svals.random_hue * g_rand_double_range (gr, -0.5, 0.5);
 
-          b += (svals.random_saturation *
-                g_rand_double_range (gr, -1.0, 1.0)) * 255;
+          if (hsv.h >= 1.0)
+            hsv.h -= 1.0;
+          else if (hsv.h < 0.0)
+            hsv.h += 1.0;
 
-          if (b > 255)
-            b = 255;
+          hsv.v += (svals.random_saturation *
+                    g_rand_double_range (gr, -1.0, 1.0));
 
-          gimp_hsv_to_rgb_int (&r, &g, &b);
+          hsv.v = CLAMP (hsv.v, 0.0, 1.0);
 
-          color[0] = 255 - r;
-          color[1] = 255 - g;
-          color[2] = 255 - b;
+          gimp_hsv_to_rgb (&hsv, &rgb);
+
+          color[0] = 255 - ROUND (rgb.r * 255.0);
+          color[1] = 255 - ROUND (rgb.g * 255.0);
+          color[2] = 255 - ROUND (rgb.b * 255.0);
         }
 
       dx = 0.2 * cos (theta * G_PI / 180.0);
diff --git a/plug-ins/gradient-flare/gradient-flare.c b/plug-ins/gradient-flare/gradient-flare.c
index cd26159604..78b9167b0a 100644
--- a/plug-ins/gradient-flare/gradient-flare.c
+++ b/plug-ins/gradient-flare/gradient-flare.c
@@ -1856,19 +1856,24 @@ calc_sample_one_gradient (void)
                 {
                   for (j = 0; j < GRADIENT_RESOLUTION; j++)
                     {
-                      gint      r, g, b;
+                      GimpRGB rgb;
+                      GimpHSV hsv;
 
-                      r = gradient[j*4];
-                      g = gradient[j*4+1];
-                      b = gradient[j*4+2];
+                      rgb.r = (gdouble) gradient[j*4]   / 255.0;
+                      rgb.g = (gdouble) gradient[j*4+1] / 255.0;
+                      rgb.b = (gdouble) gradient[j*4+2] / 255.0;
 
-                      gimp_rgb_to_hsv_int (&r, &g, &b);
-                      r = (r + hue) % 256;
-                      gimp_hsv_to_rgb_int (&r, &g, &b);
+                      gimp_rgb_to_hsv (&rgb, &hsv);
 
-                      gradient[j*4] = r;
-                      gradient[j*4+1] = g;
-                      gradient[j*4+2] = b;
+                      hsv.h = (hsv.h + ((gdouble) hue / 255.0));
+                      if (hsv.h > 1.0)
+                        hsv.h -= 1.0;
+
+                      gimp_hsv_to_rgb (&hsv, &rgb);
+
+                      gradient[j*4]   = ROUND (rgb.r * 255.0);
+                      gradient[j*4+1] = ROUND (rgb.g * 255.0);
+                      gradient[j*4+2] = ROUND (rgb.b * 255.0);
                     }
                 }
             }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]