[gimp/gimp-2-10] app: when creating palette only snap to black/white when present



commit 51955b2115aa258d3cfc8ca8b6364b0f49ae48f5
Author: Øyvind Kolås <pippin gimp org>
Date:   Fri May 1 02:19:15 2020 +0200

    app: when creating palette only snap to black/white when present
    
    By only doing the snapping to white/black for whitest/blackest palette
    entries when the original RGB data contained these colors we permit
    choosing near white/black colors in the cases where this might be
    a desirable result as the whitest/blackest colors.
    
    (cherry picked from commit d4387987b55eda4647f53778dc861cdf7b18be2e)

 app/core/gimpimage-convert-indexed.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
---
diff --git a/app/core/gimpimage-convert-indexed.c b/app/core/gimpimage-convert-indexed.c
index a675bae8d8..1cc5efd261 100644
--- a/app/core/gimpimage-convert-indexed.c
+++ b/app/core/gimpimage-convert-indexed.c
@@ -544,6 +544,8 @@ static void          compute_color_lin8      (QuantizeObj           *quantobj,
 static guchar    found_cols[MAXNUMCOLORS][3];
 static gint      num_found_cols;
 static gboolean  needs_quantize;
+static gboolean  had_white;
+static gboolean  had_black;
 
 
 /**********************************************************/
@@ -859,6 +861,8 @@ gimp_image_convert_indexed (GimpImage               *image,
        *  need to quantize or color-dither.
        */
       needs_quantize = FALSE;
+      had_black = FALSE;
+      had_white = FALSE;
       num_found_cols = 0;
 
       /*  Build the histogram  */
@@ -1158,6 +1162,18 @@ generate_histogram_gray (CFHistogram  histogram,
     }
 }
 
+static void
+check_white_or_black (const guchar *data)
+{
+  if (data[RED]   == 255 &&
+      data[GREEN] == 255 &&
+      data[BLUE]  == 255)
+    had_white = TRUE;
+  if (data[RED]  ==0 &&
+      data[GREEN]==0 &&
+      data[BLUE] ==0)
+    had_black = TRUE;
+}
 
 static void
 generate_histogram_rgb (CFHistogram   histogram,
@@ -1237,6 +1253,7 @@ generate_histogram_rgb (CFHistogram   histogram,
                                           data[RED],
                                           data[GREEN],
                                           data[BLUE]);
+                      check_white_or_black (data);
                       (*colfreq)++;
                     }
 
@@ -1261,6 +1278,7 @@ generate_histogram_rgb (CFHistogram   histogram,
                                           data[RED],
                                           data[GREEN],
                                           data[BLUE]);
+                      check_white_or_black (data);
                       (*colfreq)++;
                     }
 
@@ -1338,6 +1356,8 @@ generate_histogram_rgb (CFHistogram   histogram,
                           found_cols[num_found_cols-1][0] = data[RED];
                           found_cols[num_found_cols-1][1] = data[GREEN];
                           found_cols[num_found_cols-1][2] = data[BLUE];
+
+                          check_white_or_black (data);
                         }
                     }
                 }
@@ -2792,13 +2812,13 @@ snap_to_black_and_white (QuantizeObj *quantobj)
          }
     }
 
-  if (white_dist < POW2(24)) /* 24 units in sRGB ~= deltaE of 9.5 */
+  if (had_white && white_dist < POW2(32))
   {
      quantobj->cmap[whitest].red   =
      quantobj->cmap[whitest].green =
      quantobj->cmap[whitest].blue  = 255;
   }
-  if (black_dist < POW2(24))
+  if (had_black && black_dist < POW2(32))
   {
      quantobj->cmap[blackest].red   =
      quantobj->cmap[blackest].green =


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