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/12/04 05:51:25 UTC

svn commit: r1416773 - in /commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging: formats/png/ formats/png/transparencyfilters/ palette/

Author: damjan
Date: Tue Dec  4 04:51:24 2012
New Revision: 1416773

URL: http://svn.apache.org/viewvc?rev=1416773&view=rev
Log:
Revert a patch that was committed accidentally.


Modified:
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngConstants.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngWriter.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/transparencyfilters/TransparencyFilterIndexedColor.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/MedianCutQuantizer.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/formats/png/PngConstants.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngConstants.java?rev=1416773&r1=1416772&r2=1416773&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngConstants.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngConstants.java Tue Dec  4 04:51:24 2012
@@ -28,8 +28,6 @@ public interface PngConstants extends Im
             new byte[] { 73, 72, 68, 82 });
     public final static BinaryConstant PLTE_CHUNK_TYPE = new BinaryConstant(
             new byte[] { 80, 76, 84, 69 });
-    public final static BinaryConstant TRNS_CHUNK_TYPE = new BinaryConstant(
-            new byte[] { 't', 'R', 'N', 'S'});
     public final static BinaryConstant IEND_CHUNK_TYPE = new BinaryConstant(
             new byte[] { 73, 69, 78, 68 });
     public final static BinaryConstant IDAT_CHUNK_TYPE = new BinaryConstant(

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngWriter.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngWriter.java?rev=1416773&r1=1416772&r2=1416773&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngWriter.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngWriter.java Tue Dec  4 04:51:24 2012
@@ -48,20 +48,22 @@ public class PngWriter implements PngCon
     }
 
     /*
-     1. IHDR: image header, which is the first chunk in a PNG datastream.
-     2. PLTE: palette table associated with indexed PNG images.
-     3. IDAT: image data chunks.
-     4. IEND: image trailer, which is the last chunk in a PNG datastream.
-
-     The remaining 14 chunk types are termed ancillary chunk types, which encoders may generate and decoders may interpret.
-
-     1. Transparency information: tRNS (see 11.3.2: Transparency information).
-     2. Colour space information: cHRM, gAMA, iCCP, sBIT, sRGB (see 11.3.3: Colour space information).
-     3. Textual information: iTXt, tEXt, zTXt (see 11.3.4: Textual information).
-     4. Miscellaneous information: bKGD, hIST, pHYs, sPLT (see 11.3.5: Miscellaneous information).
-     5. Time information: tIME (see 11.3.6: Time stamp information).
-    */
-    
+     * 1. IHDR: image header, which is the first chunk in a PNG datastream. 2.
+     * PLTE: palette table associated with indexed PNG images. 3. IDAT: image
+     * data chunks. 4. IEND: image trailer, which is the last chunk in a PNG
+     * datastream.
+     * 
+     * The remaining 14 chunk types are termed ancillary chunk types, which
+     * encoders may generate and decoders may interpret.
+     * 
+     * 1. Transparency information: tRNS (see 11.3.2: Transparency information).
+     * 2. Colour space information: cHRM, gAMA, iCCP, sBIT, sRGB (see 11.3.3:
+     * Colour space information). 3. Textual information: iTXt, tEXt, zTXt (see
+     * 11.3.4: Textual information). 4. Miscellaneous information: bKGD, hIST,
+     * pHYs, sPLT (see 11.3.5: Miscellaneous information). 5. Time information:
+     * tIME (see 11.3.6: Time stamp information).
+     */
+
     private final void writeInt(OutputStream os, int value) throws IOException {
         os.write(0xff & (value >> 24));
         os.write(0xff & (value >> 16));
@@ -78,14 +80,26 @@ public class PngWriter implements PngCon
             os.write(data);
         }
 
-        PngCrc png_crc = new PngCrc();
+        // Debug.debug("writeChunk chunkType", chunkType);
+        // Debug.debug("writeChunk data", data);
+
+        {
+            PngCrc png_crc = new PngCrc();
 
-        long crc1 = png_crc.start_partial_crc(chunkType, chunkType.length);
-        long crc2 = data == null ? crc1 : png_crc.continue_partial_crc(
-                crc1, data, data.length);
-        int crc = (int) png_crc.finish_partial_crc(crc2);
+            long crc1 = png_crc.start_partial_crc(chunkType, chunkType.length);
+            long crc2 = data == null ? crc1 : png_crc.continue_partial_crc(
+                    crc1, data, data.length);
+            int crc = (int) png_crc.finish_partial_crc(crc2);
+
+            // Debug.debug("crc1", crc1 + " (" + Long.toHexString(crc1)
+            // + ")");
+            // Debug.debug("crc2", crc2 + " (" + Long.toHexString(crc2)
+            // + ")");
+            // Debug.debug("crc3", crc + " (" + Integer.toHexString(crc)
+            // + ")");
 
-        writeInt(os, crc);
+            writeInt(os, crc);
+        }
     }
 
     private static class ImageHeader {
@@ -250,16 +264,6 @@ public class PngWriter implements PngCon
 
         writeChunk(os, PLTE_CHUNK_TYPE.toByteArray(), bytes);
     }
-    
-    private void writeChunkTRNS(OutputStream os, Palette palette) throws IOException {
-        byte[] bytes = new byte[palette.length()];
-        
-        for (int i = 0; i < bytes.length; i++) {
-            bytes[i] = (byte) (0xff & (palette.getEntry(i) >> 24));
-        }
-        
-        writeChunk(os, TRNS_CHUNK_TYPE.toByteArray(), bytes);
-    }
 
     private void writeChunkIEND(OutputStream os) throws IOException {
         writeChunk(os, IEND_CHUNK_TYPE.toByteArray(), null);
@@ -344,33 +348,21 @@ public class PngWriter implements PngCon
     }
 
     /*
-     between two chunk types indicates alternatives.
-     Table 5.3 — Chunk ordering rules Critical chunks
-     (shall appear in this order, except PLTE is optional)
-     Chunk name     Multiple allowed    Ordering constraints
-     IHDR   No  Shall be first
-     PLTE   No  Before first IDAT
-     IDAT   Yes     Multiple IDAT chunks shall be consecutive
-     IEND   No  Shall be last
-     Ancillary chunks
-     (need not appear in this order)
-     Chunk name     Multiple allowed    Ordering constraints
-     cHRM   No  Before PLTE and IDAT
-     gAMA   No  Before PLTE and IDAT
-     iCCP   No  Before PLTE and IDAT. If the iCCP chunk is present, the sRGB chunk should not be present.
-     sBIT   No  Before PLTE and IDAT
-     sRGB   No  Before PLTE and IDAT. If the sRGB chunk is present, the iCCP chunk should not be present.
-     bKGD   No  After PLTE; before IDAT
-     hIST   No  After PLTE; before IDAT
-     tRNS   No  After PLTE; before IDAT
-     pHYs   No  Before IDAT
-     sPLT   Yes     Before IDAT
-     tIME   No  None
-     iTXt   Yes     None
-     tEXt   Yes     None
-     zTXt   Yes     None
-    */
-    
+     * between two chunk types indicates alternatives. Table 5.3 — Chunk
+     * ordering rules Critical chunks (shall appear in this order, except PLTE
+     * is optional) Chunk name Multiple allowed Ordering constraints IHDR No
+     * Shall be first PLTE No Before first IDAT IDAT Yes Multiple IDAT chunks
+     * shall be consecutive IEND No Shall be last Ancillary chunks (need not
+     * appear in this order) Chunk name Multiple allowed Ordering constraints
+     * cHRM No Before PLTE and IDAT gAMA No Before PLTE and IDAT iCCP No Before
+     * PLTE and IDAT. If the iCCP chunk is present, the sRGB chunk should not be
+     * present. sBIT No Before PLTE and IDAT sRGB No Before PLTE and IDAT. If
+     * the sRGB chunk is present, the iCCP chunk should not be present. bKGD No
+     * After PLTE; before IDAT hIST No After PLTE; before IDAT tRNS No After
+     * PLTE; before IDAT pHYs No Before IDAT sPLT Yes Before IDAT tIME No None
+     * iTXt Yes None tEXt Yes None zTXt Yes None
+     */
+
     public void writeImage(BufferedImage src, OutputStream os, Map<String,Object> params)
             throws ImageWriteException, IOException {
         // make copy of params; we'll clear keys as we consume them.
@@ -488,25 +480,29 @@ public class PngWriter implements PngCon
         if (colorType == COLOR_TYPE_INDEXED_COLOR) {
             // PLTE No Before first IDAT
 
-            palette = new MedianCutQuantizer(!hasAlpha).process(src, 256,
+            int max_colors = hasAlpha ? 255 : 256;
+
+            palette = new MedianCutQuantizer(true).process(src, max_colors,
                     verbose);
+            // Palette palette2 = new PaletteFactory().makePaletteSimple(src,
+            // max_colors);
+
+            // palette.dump();
+
             writeChunkPLTE(os, palette);
-            if (hasAlpha) {
-                writeChunkTRNS(os, palette);
-            }
         }
 
         Object pixelDensityObj = params.get(PARAM_KEY_PIXEL_DENSITY);
         if (pixelDensityObj instanceof PixelDensity) {
             PixelDensity pixelDensity = (PixelDensity) pixelDensityObj;
             if (pixelDensity.isUnitless()) {
-                writeChunkPHYS(os,
-                        (int) Math.round(pixelDensity.getRawHorizontalDensity()),
+                writeChunkPHYS(os, (int) Math.round(pixelDensity
+                        .getRawHorizontalDensity()),
                         (int) Math.round(pixelDensity.getRawVerticalDensity()),
                         (byte) 0);
             } else {
-                writeChunkPHYS(os,
-                        (int) Math.round(pixelDensity.horizontalDensityMetres()),
+                writeChunkPHYS(os, (int) Math.round(pixelDensity
+                        .horizontalDensityMetres()),
                         (int) Math.round(pixelDensity.verticalDensityMetres()),
                         (byte) 1);
             }
@@ -632,24 +628,16 @@ public class PngWriter implements PngCon
         }
 
         /*
-         Ancillary chunks
-         (need not appear in this order)
-         Chunk name     Multiple allowed    Ordering constraints
-         cHRM           No                  Before PLTE and IDAT
-         gAMA           No                  Before PLTE and IDAT
-         iCCP           No                  Before PLTE and IDAT. If the iCCP chunk is present, the sRGB chunk should not be present.
-         sBIT           No                  Before PLTE and IDAT
-         sRGB           No                  Before PLTE and IDAT. If the sRGB chunk is present, the iCCP chunk should not be present.
-         bKGD           No                  After PLTE; before IDAT
-         hIST           No                  After PLTE; before IDAT
-         tRNS           No                  After PLTE; before IDAT
-         pHYs           No                  Before IDAT
-         sPLT           Yes                 Before IDAT
-         tIME           No                  None
-         iTXt           Yes                 None
-         tEXt           Yes                 None
-         zTXt           Yes                 None
-        */
+         * Ancillary chunks (need not appear in this order) Chunk name Multiple
+         * allowed Ordering constraints cHRM No Before PLTE and IDAT gAMA No
+         * Before PLTE and IDAT iCCP No Before PLTE and IDAT. If the iCCP chunk
+         * is present, the sRGB chunk should not be present. sBIT No Before PLTE
+         * and IDAT sRGB No Before PLTE and IDAT. If the sRGB chunk is present,
+         * the iCCP chunk should not be present. bKGD No After PLTE; before IDAT
+         * hIST No After PLTE; before IDAT tRNS No After PLTE; before IDAT pHYs
+         * No Before IDAT sPLT Yes Before IDAT tIME No None iTXt Yes None tEXt
+         * Yes None zTXt Yes None
+         */
 
         os.close();
     } // todo: filter types

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/transparencyfilters/TransparencyFilterIndexedColor.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/transparencyfilters/TransparencyFilterIndexedColor.java?rev=1416773&r1=1416772&r2=1416773&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/transparencyfilters/TransparencyFilterIndexedColor.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/transparencyfilters/TransparencyFilterIndexedColor.java Tue Dec  4 04:51:24 2012
@@ -26,19 +26,27 @@ public class TransparencyFilterIndexedCo
         super(bytes);
     }
 
+    int count = 0;
+
     @Override
     public int filter(int rgb, int index) throws ImageReadException,
             IOException {
         if (index >= bytes.length) {
             return rgb;
-        } else if (index < 0) {
+        }
+
+        if ((index < 0) || (index > bytes.length)) {
             throw new ImageReadException(
-                    "Invalid TransparencyFilterIndexedColor index: " + index);
+                    "TransparencyFilterIndexedColor index: " + index
+                            + ", bytes.length: " + bytes.length);
         }
 
         int alpha = bytes[index];
         int result = ((0xff & alpha) << 24) | (0x00ffffff & rgb);
 
+        if ((count < 100) && (index > 0)) {
+            count++;
+        }
         return result;
     }
 }

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/MedianCutQuantizer.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/MedianCutQuantizer.java?rev=1416773&r1=1416772&r2=1416773&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/MedianCutQuantizer.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/MedianCutQuantizer.java Tue Dec  4 04:51:24 2012
@@ -21,10 +21,8 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 
 import org.apache.commons.imaging.ImageWriteException;
 import org.apache.commons.imaging.util.Debug;
@@ -91,7 +89,6 @@ public class MedianCutQuantizer {
 
         public final int max_diff;
         public final int diff_total;
-        public final int totalPoints;
 
         public ColorGroup(final List<ColorCount> color_counts)
                 throws ImageWriteException {
@@ -101,10 +98,8 @@ public class MedianCutQuantizer {
                 throw new ImageWriteException("empty color_group");
             }
 
-            int totalPoints = 0;
             for (int i = 0; i < color_counts.size(); i++) {
                 ColorCount color = color_counts.get(i);
-                totalPoints += color.count;
 
                 min_alpha = Math.min(min_alpha, color.alpha);
                 max_alpha = Math.max(max_alpha, color.alpha);
@@ -115,7 +110,6 @@ public class MedianCutQuantizer {
                 min_blue = Math.min(min_blue, color.blue);
                 max_blue = Math.max(max_blue, color.blue);
             }
-            this.totalPoints = totalPoints;
 
             alpha_diff = max_alpha - min_alpha;
             red_diff = max_red - min_red;
@@ -277,37 +271,21 @@ public class MedianCutQuantizer {
 
         final Comparator<ColorGroup> comparator = new Comparator<ColorGroup>() {
             public int compare(ColorGroup cg1, ColorGroup cg2) {
-                return cg2.totalPoints - cg1.totalPoints;
-//                if (cg1.max_diff == cg2.max_diff) {
-//                    return cg2.diff_total - cg1.diff_total;
-//                }
-//                return cg2.max_diff - cg1.max_diff;
+                if (cg1.max_diff == cg2.max_diff) {
+                    return cg2.diff_total - cg1.diff_total;
+                }
+                return cg2.max_diff - cg1.max_diff;
             }
         };
 
-        Set<ColorGroup> ignore = new HashSet<ColorGroup>();
         while (color_groups.size() < max_colors) {
             Collections.sort(color_groups, comparator);
 
             ColorGroup color_group = color_groups.get(0);
-//            if (color_group.max_diff == 0) {
-//                for (ColorGroup c : color_groups) {
-//                    System.out.println("max_diff: " + c.max_diff);
-//                }
-//                break;
-//            }
-            
-//            ColorGroup color_group = null;
-//            for (ColorGroup c : color_groups) {
-//                if (c.max_diff > 0) {
-//                    color_group = c;
-//                    break;
-//                }
-//            }
-//            if (color_group == null) {
-//                break;
-//            }
-            
+
+            if (color_group.max_diff == 0) {
+                break;
+            }
             if (!ignoreAlpha
                     && color_group.alpha_diff > color_group.red_diff
                     && color_group.alpha_diff > color_group.green_diff

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=1416773&r1=1416772&r2=1416773&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 Dec  4 04:51:24 2012
@@ -334,7 +334,7 @@ public class PaletteFactory {
      * Builds an inexact opaque palette of at most {@code max} colors in {@code src}
      * using a variation of the Median Cut algorithm. Accurate to 6 bits per component,
      * and works by splitting the color bounding box most heavily populated by colors
-     * along the component which splits the colors in that box most evenly.
+     * along the component which splits the colors most evenly.
      * @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