commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dam...@apache.org
Subject svn commit: r1390266 - in /commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette: ColorSpaceSubset.java PaletteFactory.java
Date Wed, 26 Sep 2012 03:19:10 GMT
Author: damjan
Date: Wed Sep 26 03:19:10 2012
New Revision: 1390266

URL: http://svn.apache.org/viewvc?rev=1390266&view=rev
Log:
Clean up some more code.


Modified:
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/ColorSpaceSubset.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/PaletteFactory.java

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/ColorSpaceSubset.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/ColorSpaceSubset.java?rev=1390266&r1=1390265&r2=1390266&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/ColorSpaceSubset.java
(original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/ColorSpaceSubset.java
Wed Sep 26 03:19:10 2012
@@ -35,7 +35,6 @@ class ColorSpaceSubset {
         maxs = new int[PaletteFactory.components];
         for (int i = 0; i < PaletteFactory.components; i++) {
             mins[i] = 0;
-            // maxs[i] = 255;
             maxs[i] = precision_mask;
         }
 
@@ -53,11 +52,7 @@ class ColorSpaceSubset {
         rgb = -1;
     }
 
-    public static long compares = 0;
-
     public final boolean contains(int red, int green, int blue) {
-        compares++;
-
         red >>= (8 - precision);
         if (mins[0] > red)
             return false;
@@ -128,33 +123,27 @@ class ColorSpaceSubset {
     }
 
     public void setAverageRGB(int table[]) {
+        long redsum = 0, greensum = 0, bluesum = 0;
 
-        {
-            long redsum = 0, greensum = 0, bluesum = 0;
-
-            for (int red = mins[0]; red <= maxs[0]; red++)
-                for (int green = mins[1]; green <= maxs[1]; green++)
-                    for (int blue = mins[2]; blue <= maxs[2]; blue++)
-                    // for (int red = 0; red <= precision_mask; red++)
-                    // for (int green = 0; green <= precision_mask; green++)
-                    // for (int blue = 0; blue <= precision_mask; blue++)
-                    {
-                        int index = (blue << (2 * precision)) // note: order
-                                                              // reversed
-                                | (green << (1 * precision))
-                                | (red << (0 * precision));
-                        int count = table[index];
-                        redsum += count * (red << (8 - precision));
-                        greensum += count * (green << (8 - precision));
-                        bluesum += count * (blue << (8 - precision));
-
-                    }
-
-            redsum /= total;
-            greensum /= total;
-            bluesum /= total;
-            rgb = (int) (((redsum & 0xff) << 16) | ((greensum & 0xff) <<
8) | ((bluesum & 0xff) << 0));
+        for (int red = mins[0]; red <= maxs[0]; red++) {
+            for (int green = mins[1]; green <= maxs[1]; green++) {
+                for (int blue = mins[2]; blue <= maxs[2]; blue++) {
+                    // note: order reversed
+                    int index = (blue << (2 * precision)) 
+                            | (green << (1 * precision))
+                            | (red << (0 * precision));
+                    int count = table[index];
+                    redsum += count * (red << (8 - precision));
+                    greensum += count * (green << (8 - precision));
+                    bluesum += count * (blue << (8 - precision));
+                }
+            }
         }
+
+        redsum /= total;
+        greensum /= total;
+        bluesum /= total;
+        rgb = (int) (((redsum & 0xff) << 16) | ((greensum & 0xff) <<
8) | ((bluesum & 0xff) << 0));
     }
 
     public int index;

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/PaletteFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/PaletteFactory.java?rev=1390266&r1=1390265&r2=1390266&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/PaletteFactory.java
(original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/PaletteFactory.java
Wed Sep 26 03:19:10 2012
@@ -115,7 +115,6 @@ public class PaletteFactory {
             int b = (blue << (2 * precision));
             for (int green = mins[1]; green <= maxs[1]; green++) {
                 int g = (green << (1 * precision));
-
                 for (int red = mins[0]; red <= maxs[0]; red++) {
                     int index = b | g | red;
 
@@ -149,8 +148,6 @@ public class PaletteFactory {
             return null;
         }
 
-        // List result = new ArrayList();
-
         int slice_mins[] = new int[subset.mins.length];
         System.arraycopy(subset.mins, 0, slice_mins, 0, subset.mins.length);
         int slice_maxs[] = new int[subset.maxs.length];
@@ -194,26 +191,23 @@ public class PaletteFactory {
         int slice1, slice2;
         int last = 0;
 
-        {
-            for (slice1 = subset.mins[component]; slice1 != subset.maxs[component] + 1; slice1++)
{
+        for (slice1 = subset.mins[component]; slice1 != subset.maxs[component] + 1; slice1++)
{
 
-                slice_mins[component] = slice1;
-                slice_maxs[component] = slice1;
+            slice_mins[component] = slice1;
+            slice_maxs[component] = slice1;
 
-                last = getFrequencyTotal(table, slice_mins, slice_maxs,
-                        precision);
+            last = getFrequencyTotal(table, slice_mins, slice_maxs,
+                    precision);
 
-                sum1 += last;
-
-                if (sum1 >= (total / 2))
-                    break;
-            }
-
-            sum2 = sum1 - last;
-            slice2 = slice1 - 1;
+            sum1 += last;
 
+            if (sum1 >= (total / 2))
+                break;
         }
 
+        sum2 = sum1 - last;
+        slice2 = slice1 - 1;
+
         DivisionCandidate dc1 = finishDivision(table, subset, component,
                 precision, sum1, slice1);
         DivisionCandidate dc2 = finishDivision(table, subset, component,
@@ -238,7 +232,6 @@ public class PaletteFactory {
         dcs.addAll(divideSubset2(table, subset, 2, precision));
 
         DivisionCandidate best_v = null;
-        // int best_split
         double best_score = Double.MAX_VALUE;
 
         for (int i = 0; i < dcs.size(); i++) {
@@ -250,9 +243,7 @@ public class PaletteFactory {
             int area2 = second.total;
 
             int diff = Math.abs(area1 - area2);
-            double split = ((double) diff) / ((double) Math.max(area1, area2));
-
-            double score = split;
+            double score = ((double) diff) / ((double) Math.max(area1, area2));
 
             if (best_v == null) {
                 best_v = dc;
@@ -317,27 +308,23 @@ public class PaletteFactory {
             if (debug)
                 System.out.println("\t" + "area: " + max_area);
 
-            {
-
-                DivisionCandidate dc = divideSubset2(table, max_subset,
-                        precision);
-                if (dc != null) {
-                    v.remove(max_subset);
-                    v.add(dc.dst_a);
-                    v.add(dc.dst_b);
-                } else
-                    ignore.add(max_subset);
-            }
+            DivisionCandidate dc = divideSubset2(table, max_subset,
+                    precision);
+            if (dc != null) {
+                v.remove(max_subset);
+                v.add(dc.dst_a);
+                v.add(dc.dst_b);
+            } else
+                ignore.add(max_subset);
 
             if (v.size() == desired_count)
                 return v;
         }
-
-        // return result;
     }
 
     /**
-     * Builds an inexact palette of at most {@code max} colors in {@code src}.
+     * Builds an inexact palette of at most {@code max} colors in {@code src}
+     * using the Median Cut algorithm.
      * @param src the image whose palette to build
      * @param max the maximum number of colors the palette can contain
      * @return the palette of at most {@code max} colors
@@ -362,7 +349,7 @@ public class PaletteFactory {
         }
 
         // step 1: count frequency of colors
-        for (int y = 0; y < height; y++)
+        for (int y = 0; y < height; y++) {
             for (int x = 0; x < width; x++) {
                 int argb = src.getRGB(x, y);
 
@@ -370,6 +357,7 @@ public class PaletteFactory {
 
                 table[index]++;
             }
+        }
 
         if (debug) {
             int all_total = getFrequencyTotal(table, all.mins, all.maxs, precision);



Mime
View raw message