You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by da...@apache.org on 2012/09/25 21:18:16 UTC

svn commit: r1390069 - in /commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette: PaletteFactory.java SimplePalette.java

Author: damjan
Date: Tue Sep 25 19:18:16 2012
New Revision: 1390069

URL: http://svn.apache.org/viewvc?rev=1390069&view=rev
Log:
Simplify and document some of the palette code.


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

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=1390069&r1=1390068&r2=1390069&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 Tue Sep 25 19:18:16 2012
@@ -22,9 +22,9 @@ import java.awt.image.ColorModel;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
-import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
-import java.util.Map;
+import java.util.Set;
 
 public class PaletteFactory {
     private static final boolean debug = false;
@@ -33,8 +33,6 @@ public class PaletteFactory {
         // map what rgb values have been used
 
         byte rgbmap[] = new byte[256 * 256 * 32];
-        for (int i = 0; i < rgbmap.length; i++)
-            rgbmap[i] = 0;
 
         int width = src.getWidth();
         int height = src.getHeight();
@@ -338,6 +336,12 @@ public class PaletteFactory {
         // return result;
     }
 
+    /**
+     * Builds an inexact palette of at most {@code max} colors in {@code src}.
+     * @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
+     */
     public Palette makePaletteQuantized(BufferedImage src, int max) {
         int precision = 6; // in bits
 
@@ -357,17 +361,15 @@ public class PaletteFactory {
             System.out.println("pre total: " + pre_total);
         }
 
-        { // step 1: count frequency of colors
-
-            for (int y = 0; y < height; y++)
-                for (int x = 0; x < width; x++) {
-                    int argb = src.getRGB(x, y);
+        // step 1: count frequency of colors
+        for (int y = 0; y < height; y++)
+            for (int x = 0; x < width; x++) {
+                int argb = src.getRGB(x, y);
 
-                    int index = pixelToQuantizationTableIndex(argb, precision);
+                int index = pixelToQuantizationTableIndex(argb, precision);
 
-                    table[index]++;
-                }
-        }
+                table[index]++;
+            }
 
         if (debug) {
             int all_total = getFrequencyTotal(table, all.mins, all.maxs, precision);
@@ -396,37 +398,40 @@ public class PaletteFactory {
         return new QuantizedPalette(subsets, precision);
     }
 
-    public SimplePalette makePaletteSimple(BufferedImage src, int max)
-    // This is not efficient for large values of max, say, max > 256;
-    {
-        Map<String, String> map = new HashMap<String, String>();
-        int rgbs[] = new int[max];
-        int rgb_count = 0;
+    /**
+     * Builds an exact and complete palette containing all the colors in {@code src},
+     * and fails by returning {@code null} if there are more than {@code max} colors necessary to do this.
+     * @param src the image whose palette to build
+     * @param max the maximum number of colors the palette can contain
+     * @return the complete palette of {@code max} or less colors, or {@code null} if more than {@code max} colors are necessary
+     */
+    public SimplePalette makePaletteSimple(BufferedImage src, int max) {
+        // This is not efficient for large values of max, say, max > 256;
+        Set<Integer> rgbs = new HashSet<Integer>();
 
         int width = src.getWidth();
         int height = src.getHeight();
 
-        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);
                 int rgb = 0xffffff & argb;
 
-                String key = "" + rgb;
-                if (null == map.get(key)) {
-                    if (rgb_count == max)
+                if (rgbs.add(rgb)) {
+                    if (rgbs.size() > max) {
                         return null;
-
-                    rgbs[rgb_count] = rgb;
-                    map.put(key, key);
-                    rgb_count++;
+                    }
                 }
             }
-
-        int result[] = new int[rgb_count];
-        System.arraycopy(rgbs, 0, result, 0, rgb_count);
+        }
+        
+        int result[] = new int[rgbs.size()];
+        int next = 0;
+        for (int rgb : rgbs) {
+            result[next++] = rgb;
+        }
         Arrays.sort(result);
 
-        // return result;
         return new SimplePalette(result);
     }
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/SimplePalette.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/SimplePalette.java?rev=1390069&r1=1390068&r2=1390069&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/SimplePalette.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/SimplePalette.java Tue Sep 25 19:18:16 2012
@@ -33,18 +33,10 @@ public class SimplePalette extends Palet
     @Override
     public int getEntry(int index) {
         return palette[index];
-        // return getPaletteIndex(palette, rgb);
     }
 
     private int getPaletteIndex(int palette[], int argb) {
-
-        // Debug.debug("getPaletteIndex argb", argb + " ("
-        // + Integer.toHexString(argb) + ")");
-
         for (int i = 0; i < palette.length; i++) {
-            // Debug.debug("\t" + "palette[" + i + "]", palette[i] + " ("
-            // + Integer.toHexString(palette[i]) + ")");
-
             if (palette[i] == argb)
                 return i;
         }