commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dam...@apache.org
Subject svn commit: r1441320 - in /commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png: PngWriter.java transparencyfilters/TransparencyFilterIndexedColor.java
Date Fri, 01 Feb 2013 05:48:54 GMT
Author: damjan
Date: Fri Feb  1 05:48:54 2013
New Revision: 1441320

URL: http://svn.apache.org/viewvc?rev=1441320&view=rev
Log:
Fix writing transparent color-index PNGs.


Modified:
    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

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=1441320&r1=1441319&r2=1441320&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
Fri Feb  1 05:48:54 2013
@@ -30,6 +30,7 @@ import org.apache.commons.imaging.PixelD
 import org.apache.commons.imaging.common.ZLibUtils;
 import org.apache.commons.imaging.palette.Palette;
 import org.apache.commons.imaging.palette.PaletteFactory;
+import org.apache.commons.imaging.palette.SimplePalette;
 import org.apache.commons.imaging.util.Debug;
 import org.apache.commons.imaging.util.ParamMap;
 import org.apache.commons.imaging.util.UnicodeUtils;
@@ -342,6 +343,42 @@ public class PngWriter implements PngCon
         return result;
     }
 
+    /// Wraps a palette by adding a single transparent entry at index 0.
+    private static class TransparentPalette extends Palette {
+        private final Palette palette;
+        
+        TransparentPalette(Palette palette) {
+            this.palette = palette;
+        }
+        
+        @Override
+        public int getEntry(int index) {
+            if (index == 0) {
+                return 0x00000000;
+            } else {
+                return palette.getEntry(index - 1);
+            }
+        }
+        
+        @Override
+        public int length() {
+            return 1 + palette.length();
+        }
+        
+        @Override
+        public int getPaletteIndex(int rgb) throws ImageWriteException {
+            if (rgb == 0x00000000) {
+                return 0;
+            } else {
+                int index = palette.getPaletteIndex(rgb);
+                if (index >= 0) {
+                    return 1 + index;
+                } else {
+                    return index;
+                }
+            }
+        }
+    }
     /*
      between two chunk types indicates alternatives.
      Table 5.3 - Chunk ordering rules
@@ -489,13 +526,20 @@ public class PngWriter implements PngCon
 
             final int max_colors = hasAlpha ? 255 : 256;
 
-            palette = new PaletteFactory().makeQuantizedRgbPalette(src, max_colors);
+            PaletteFactory paletteFactory = new PaletteFactory();
+            palette = paletteFactory.makeQuantizedRgbPalette(src, max_colors);
             // Palette palette2 = new PaletteFactory().makePaletteSimple(src,
             // max_colors);
 
             // palette.dump();
 
-            writeChunkPLTE(os, palette);
+            if (hasAlpha) {
+                palette = new TransparentPalette(palette);
+                writeChunkPLTE(os, palette);                
+                writeChunkTRNS(os, new SimplePalette(new int[] { 0x00000000 }));
+            } else {
+                writeChunkPLTE(os, palette);
+            }
         }
 
         final Object pixelDensityObj = params.get(PARAM_KEY_PIXEL_DENSITY);
@@ -559,8 +603,12 @@ public class PngWriter implements PngCon
                         final int argb = row[x];
 
                         if (palette != null) {
-                            final int index = palette.getPaletteIndex(argb);
-                            baos.write(0xff & index);
+                            if (hasAlpha && (argb >>> 24) == 0x00) {
+                                baos.write(0);
+                            } else {
+                                final int index = palette.getPaletteIndex(argb);
+                                baos.write(0xff & index);
+                            }
                         } else {
                             final int alpha = 0xff & (argb >> 24);
                             final int red = 0xff & (argb >> 16);

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=1441320&r1=1441319&r2=1441320&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
Fri Feb  1 05:48:54 2013
@@ -26,8 +26,6 @@ public class TransparencyFilterIndexedCo
         super(bytes);
     }
 
-    int count = 0;
-
     @Override
     public int filter(final int rgb, final int index) throws ImageReadException,
             IOException {
@@ -44,9 +42,6 @@ public class TransparencyFilterIndexedCo
         final int alpha = bytes[index];
         final int result = ((0xff & alpha) << 24) | (0x00ffffff & rgb);
 
-        if ((count < 100) && (index > 0)) {
-            count++;
-        }
         return result;
     }
 }



Mime
View raw message