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/10/04 07:36:24 UTC

svn commit: r1393921 - in /commons/proper/imaging/trunk/src: main/java/org/apache/commons/imaging/formats/pnm/ site/xdoc/ test/java/org/apache/commons/imaging/roundtrip/

Author: damjan
Date: Thu Oct  4 05:36:24 2012
New Revision: 1393921

URL: http://svn.apache.org/viewvc?rev=1393921&view=rev
Log:
Add support for writing PAM files.
Document and test this.



Added:
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PamWriter.java   (with props)
Modified:
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PnmConstants.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PnmImageParser.java
    commons/proper/imaging/trunk/src/site/xdoc/formatsupport.xml
    commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/roundtrip/RoundtripTest.java

Added: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PamWriter.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PamWriter.java?rev=1393921&view=auto
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PamWriter.java (added)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PamWriter.java Thu Oct  4 05:36:24 2012
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.imaging.formats.pnm;
+
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Map;
+
+import org.apache.commons.imaging.ImageWriteException;
+
+public class PamWriter extends PnmWriter implements PnmConstants {
+    public PamWriter() {
+        super(true);
+    }
+    
+    @Override
+    public void writeImage(BufferedImage src, OutputStream os,
+            Map<String, Object> params) throws ImageWriteException, IOException {
+
+        os.write(PNM_PREFIX_BYTE);
+        os.write(PAM_RAW_CODE);
+        os.write(PNM_NEWLINE);
+        
+        int width = src.getWidth();
+        int height = src.getHeight();
+
+        os.write(("WIDTH " + width).getBytes("US-ASCII"));
+        os.write(PNM_NEWLINE);
+
+        os.write(("HEIGHT " + height).getBytes("US-ASCII"));
+        os.write(PNM_NEWLINE);
+        
+        os.write(("DEPTH 3").getBytes("US-ASCII"));
+        os.write(PNM_NEWLINE);
+        
+        os.write(("MAXVAL 255").getBytes("US-ASCII"));
+        os.write(PNM_NEWLINE);
+
+        os.write(("TUPLTYPE RGB_ALPHA").getBytes("US-ASCII"));
+        os.write(PNM_NEWLINE);
+        
+        os.write(("ENDHDR").getBytes("US-ASCII"));
+        os.write(PNM_NEWLINE);
+        
+        for (int y = 0; y < height; y++) {
+            for (int x = 0; x < width; x++) {
+                int argb = src.getRGB(x, y);
+                int alpha = 0xff & (argb >> 24);
+                int red = 0xff & (argb >> 16);
+                int green = 0xff & (argb >> 8);
+                int blue = 0xff & (argb >> 0);
+
+                os.write((byte) alpha);
+                os.write((byte) red);
+                os.write((byte) green);
+                os.write((byte) blue);
+            }
+        }
+    }
+}

Propchange: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PamWriter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PnmConstants.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PnmConstants.java?rev=1393921&r1=1393920&r2=1393921&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PnmConstants.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PnmConstants.java Thu Oct  4 05:36:24 2012
@@ -25,7 +25,7 @@ public interface PnmConstants {
     public static final byte PGM_RAW_CODE = 0x35; // RAW GrayMap
     public static final byte PBM_RAW_CODE = 0x34; // RAW Bitmap
     public static final byte PPM_RAW_CODE = 0x36; // RAW Pixmap
-    public static final byte PAM_TEXT_CODE = 0x37; // PAM Pixmap
+    public static final byte PAM_RAW_CODE = 0x37; // PAM Pixmap
 
     public static final byte PNM_SEPARATOR = 0x20; // Space
     public static final byte PNM_NEWLINE = 0x0A; // "usually a newline"

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PnmImageParser.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PnmImageParser.java?rev=1393921&r1=1393920&r2=1393921&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PnmImageParser.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PnmImageParser.java Thu Oct  4 05:36:24 2012
@@ -38,6 +38,7 @@ import org.apache.commons.imaging.common
 import org.apache.commons.imaging.common.IImageMetadata;
 import org.apache.commons.imaging.common.ImageBuilder;
 import org.apache.commons.imaging.common.bytesource.ByteSource;
+import org.apache.commons.imaging.palette.PaletteFactory;
 import org.apache.commons.imaging.util.Debug;
 
 public class PnmImageParser extends ImageParser implements PnmConstants {
@@ -118,7 +119,7 @@ public class PnmImageParser extends Imag
             } else {
                 throw new ImageReadException("PNM file has invalid header.");
             }
-        } else if (identifier2 == PAM_TEXT_CODE) {
+        } else if (identifier2 == PAM_RAW_CODE) {
             int width = -1;
             boolean seenWidth = false;
             int height = -1;
@@ -330,6 +331,7 @@ public class PnmImageParser extends Imag
             throws ImageWriteException, IOException {
         PnmWriter writer = null;
         boolean useRawbits = true;
+        boolean hasAlpha = new PaletteFactory().hasTransparency(src);
 
         if (params != null) {
             Object useRawbitsParam = params.get(PARAM_KEY_PNM_RAWBITS);
@@ -347,12 +349,18 @@ public class PnmImageParser extends Imag
                     writer = new PgmWriter(useRawbits);
                 } else if (subtype.equals(ImageFormat.IMAGE_FORMAT_PPM)) {
                     writer = new PpmWriter(useRawbits);
+                } else if (subtype.equals(ImageFormat.IMAGE_FORMAT_PAM)) { 
+                    writer = new PamWriter();
                 }
             }
         }
 
         if (writer == null) {
-            writer = new PpmWriter(useRawbits);
+            if (hasAlpha) {
+                writer = new PamWriter();
+            } else {   
+                writer = new PpmWriter(useRawbits);
+            }
         }
 
         // make copy of params; we'll clear keys as we consume them.
@@ -366,6 +374,9 @@ public class PnmImageParser extends Imag
         if (params.containsKey(PARAM_KEY_FORMAT)) {
             params.remove(PARAM_KEY_FORMAT);
         }
+        
+        // clear pixel density
+        params.remove(PARAM_KEY_PIXEL_DENSITY);
 
         if (params.size() > 0) {
             Object firstKey = params.keySet().iterator().next();

Modified: commons/proper/imaging/trunk/src/site/xdoc/formatsupport.xml
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/site/xdoc/formatsupport.xml?rev=1393921&r1=1393920&r2=1393921&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/site/xdoc/formatsupport.xml (original)
+++ commons/proper/imaging/trunk/src/site/xdoc/formatsupport.xml Thu Oct  4 05:36:24 2012
@@ -114,14 +114,14 @@ limitations under the License.
     </td>
   </tr>
 
-  <!-- PNM/PGM/PBM/PPM Portable Pixmap -->
+  <!-- PNM/PGM/PBM/PPM/PAM Portable Pixmap -->
   <tr>
-    <td>PNM/PGM/PBM/PPM Portable Pixmap</td><td>yes</td><td>yes</td>
+    <td>PNM/PGM/PBM/PPM/PAM Portable Pixmap</td><td>yes</td><td>mostly</td>
     <td>
-        Complete.
+        Reading complete. PAM writing only in RGB_ALPHA format.
     </td>
     <td>
-       No spec, see:
+       <a href="http://netpbm.sourceforge.net/doc/index.html">spec</a>
        <a href="http://en.wikipedia.org/wiki/Portable_pixmap">wikipedia</a>
     </td>
   </tr>

Modified: commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/roundtrip/RoundtripTest.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/roundtrip/RoundtripTest.java?rev=1393921&r1=1393920&r2=1393921&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/roundtrip/RoundtripTest.java (original)
+++ commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/roundtrip/RoundtripTest.java Thu Oct  4 05:36:24 2012
@@ -84,8 +84,8 @@ public class RoundtripTest extends Imagi
                     COLOR_GRAYSCALE, true, false), //
             new FormatInfo(ImageFormat.IMAGE_FORMAT_PPM, true, true,
                     COLOR_FULL_RGB, true, false), //
-            new FormatInfo(ImageFormat.IMAGE_FORMAT_PAM, true, false,
-                    COLOR_FULL_RGB, true, true),//
+            new FormatInfo(ImageFormat.IMAGE_FORMAT_PAM, true, true,
+                    COLOR_FULL_RGB, true, false),//
             // new FormatInfo(ImageFormat.IMAGE_FORMAT_PNM, true, true,
             // COLOR_FULL_RGB, true), //
             new FormatInfo(ImageFormat.IMAGE_FORMAT_TGA, false, false,
@@ -425,7 +425,7 @@ public class RoundtripTest extends Imagi
             ImageReadException, ImageWriteException {
         File temp1 = createTempFile(tempPrefix + ".", "."
                 + formatInfo.format.getExtension());
-        // Debug.debug("tempFile: " + tempFile.getName());
+        Debug.debug("tempFile: " + temp1.getName());
 
         Map<String,Object> params = new HashMap<String,Object>();
         Imaging.writeImage(testImage, temp1, formatInfo.format, params);