You are viewing a plain text version of this content. The canonical link for it is here.
Posted to sanselan-commits@incubator.apache.org by cm...@apache.org on 2008/08/01 23:32:24 UTC

svn commit: r681902 - in /incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/pnm: FileInfo.java PBMFileInfo.java PBMWriter.java PNMConstants.java PNMImageParser.java

Author: cmchen
Date: Fri Aug  1 16:32:24 2008
New Revision: 681902

URL: http://svn.apache.org/viewvc?rev=681902&view=rev
Log:
A change to the PNM format handling.

Applied the BMP "buffer flushing" bug to the PBM reading and writing code.

Added:
    incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/pnm/PNMConstants.java   (with props)
Modified:
    incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/pnm/FileInfo.java
    incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/pnm/PBMFileInfo.java
    incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/pnm/PBMWriter.java
    incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/pnm/PNMImageParser.java

Modified: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/pnm/FileInfo.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/pnm/FileInfo.java?rev=681902&r1=681901&r2=681902&view=diff
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/pnm/FileInfo.java (original)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/pnm/FileInfo.java Fri Aug  1 16:32:24 2008
@@ -51,13 +51,18 @@
 
 	public abstract int getRGB(InputStream is) throws IOException;
 
+	protected void newline()
+	{
+		// do nothing by default.
+	}
+
 	public void readImage(BufferedImage bi, InputStream is) throws IOException
 	{
-		//			is = new BufferedInputStream(is);
-		//			int count = 0;
+		// is = new BufferedInputStream(is);
+		// int count = 0;
 		//
-		//			try
-		//			{
+		// try
+		// {
 		DataBuffer buffer = bi.getRaster().getDataBuffer();
 
 		if (!RAWBITS)
@@ -65,33 +70,36 @@
 			WhiteSpaceReader wsr = new WhiteSpaceReader(is);
 
 			for (int y = 0; y < height; y++)
+			{
 				for (int x = 0; x < width; x++)
 				{
 					int rgb = getRGB(wsr);
 
 					buffer.setElem(y * width + x, rgb);
-					//							count++;
+					// count++;
 				}
-		}
-		else
+				newline();
+			}
+		} else
 		{
 			for (int y = 0; y < height; y++)
 			{
-				//					System.out.println("y: " + y);
+				// System.out.println("y: " + y);
 				for (int x = 0; x < width; x++)
 				{
 					int rgb = getRGB(is);
 					buffer.setElem(y * width + x, rgb);
-					//							count++;
+					// count++;
 				}
+				newline();
 			}
 		}
-		//			}
-		//			finally
-		//			{
-		//				System.out.println("count: " + count);
-		//				dump();
-		//			}
+		// }
+		// finally
+		// {
+		// System.out.println("count: " + count);
+		// dump();
+		// }
 	}
 
 	public void dump()

Modified: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/pnm/PBMFileInfo.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/pnm/PBMFileInfo.java?rev=681902&r1=681901&r2=681902&view=diff
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/pnm/PBMFileInfo.java (original)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/pnm/PBMFileInfo.java Fri Aug  1 16:32:24 2008
@@ -59,6 +59,13 @@
 		return "image/x-portable-bitmap";
 	}
 
+	protected void newline()
+	{
+		bitcache = 0;
+		bits_in_cache = 0;
+	}
+
+	
 	private int bitcache = 0;
 	private int bits_in_cache = 0;
 

Modified: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/pnm/PBMWriter.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/pnm/PBMWriter.java?rev=681902&r1=681901&r2=681902&view=diff
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/pnm/PBMWriter.java (original)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/pnm/PBMWriter.java Fri Aug  1 16:32:24 2008
@@ -23,7 +23,7 @@
 
 import org.apache.sanselan.ImageWriteException;
 
-public class PBMWriter extends PNMWriter
+public class PBMWriter extends PNMWriter implements PNMConstants
 {
 	public PBMWriter(boolean RAWBITS)
 	{
@@ -33,23 +33,18 @@
 	public void writeImage(BufferedImage src, OutputStream os, Map params)
 			throws ImageWriteException, IOException
 	{
-		//			System.out.println
-		// (b1 == 0x50 && b2 == 0x36)
-		os.write(0x50);
-		os.write(RAWBITS ? 0x34 : 0x31);
-		os.write(' ');
+		os.write(PNM_PREFIX_BYTE);
+		os.write(RAWBITS ? PBM_RAW_CODE : PBM_TEXT_CODE);
+		os.write(PNM_SEPARATOR);
 
 		int width = src.getWidth();
 		int height = src.getHeight();
 
 		os.write(("" + width).getBytes());
-		os.write(' ');
+		os.write(PNM_SEPARATOR);
 
 		os.write(("" + height).getBytes());
-		os.write(' ');
-
-		//			os.write(("" + 255).getBytes()); // max component value
-		//			os.write('\n');
+		os.write(PNM_SEPARATOR);
 
 		int bitcache = 0;
 		int bits_in_cache = 0;
@@ -79,16 +74,16 @@
 						bitcache = 0;
 						bits_in_cache = 0;
 					}
-				}
-				else
+				} else
 				{
 					os.write(("" + sample).getBytes()); // max component value
-					os.write(' ');
+					os.write(PNM_SEPARATOR);
 				}
 			}
 
 			if ((RAWBITS) && (bits_in_cache > 0))
 			{
+				bitcache = bitcache << (8-bits_in_cache);
 				os.write((byte) bitcache);
 				bitcache = 0;
 				bits_in_cache = 0;

Added: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/pnm/PNMConstants.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/pnm/PNMConstants.java?rev=681902&view=auto
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/pnm/PNMConstants.java (added)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/pnm/PNMConstants.java Fri Aug  1 16:32:24 2008
@@ -0,0 +1,32 @@
+/*
+ * 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.sanselan.formats.pnm;
+
+public interface PNMConstants
+{
+	public static final byte PNM_PREFIX_BYTE = 0x50; // P
+
+	public static final byte PBM_TEXT_CODE = 0x31; // Textual Bitmap
+	public static final byte PGM_TEXT_CODE = 0x32; // Textual GrayMap
+	public static final byte PPM_TEXT_CODE = 0x33; // Textual Pixmap
+	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 PNM_SEPARATOR = 0x20; // Space
+
+}
\ No newline at end of file

Propchange: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/pnm/PNMConstants.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/pnm/PNMImageParser.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/pnm/PNMImageParser.java?rev=681902&r1=681901&r2=681902&view=diff
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/pnm/PNMImageParser.java (original)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/pnm/PNMImageParser.java Fri Aug  1 16:32:24 2008
@@ -36,18 +36,22 @@
 import org.apache.sanselan.common.byteSources.ByteSource;
 import org.apache.sanselan.util.Debug;
 
-public class PNMImageParser extends ImageParser {
+public class PNMImageParser extends ImageParser implements PNMConstants
+{
 
-	public PNMImageParser() {
+	public PNMImageParser()
+	{
 		super.setByteOrder(BYTE_ORDER_LSB);
 		// setDebug(true);
 	}
 
-	public String getName() {
+	public String getName()
+	{
 		return "Pbm-Custom";
 	}
 
-	public String getDefaultExtension() {
+	public String getDefaultExtension()
+	{
 		return DEFAULT_EXTENSION;
 	}
 
@@ -56,11 +60,13 @@
 	private static final String ACCEPTED_EXTENSIONS[] = { ".pbm", ".pgm",
 			".ppm", ".pnm", };
 
-	protected String[] getAcceptedExtensions() {
+	protected String[] getAcceptedExtensions()
+	{
 		return ACCEPTED_EXTENSIONS;
 	}
 
-	protected ImageFormat[] getAcceptedTypes() {
+	protected ImageFormat[] getAcceptedTypes()
+	{
 		return new ImageFormat[] { ImageFormat.IMAGE_FORMAT_PBM, //
 				ImageFormat.IMAGE_FORMAT_PGM, //
 				ImageFormat.IMAGE_FORMAT_PPM, //
@@ -68,9 +74,10 @@
 	}
 
 	private FileInfo readHeader(InputStream is) throws ImageReadException,
-			IOException {
-		byte Identifier1 = readByte("Identifier1", is, "Not a Valid PNM File");
-		byte Identifier2 = readByte("Identifier2", is, "Not a Valid PNM File");
+			IOException
+	{
+		byte identifier1 = readByte("Identifier1", is, "Not a Valid PNM File");
+		byte identifier2 = readByte("Identifier2", is, "Not a Valid PNM File");
 
 		WhiteSpaceReader wsr = new WhiteSpaceReader(is);
 
@@ -84,61 +91,65 @@
 		// System.out.println("((width*height+7)/8): "
 		// + ((width * height + 7) / 8));
 
-		if ((Identifier1 == 'P') && (Identifier2 == '1')) {
+		if (identifier1 != PNM_PREFIX_BYTE)
+			throw new ImageReadException("PNM file has invalid header.");
+
+		if (identifier2 == PBM_TEXT_CODE)
 			return new PBMFileInfo(width, height, false);
-		}
-		if ((Identifier1 == 'P') && (Identifier2 == '2')) {
+		else if (identifier2 == PBM_RAW_CODE)
+			return new PBMFileInfo(width, height, true);
+		else if (identifier2 == PGM_TEXT_CODE)
+		{
 			int maxgray = Integer.parseInt(wsr.readtoWhiteSpace());
-			// System.out.println("maxgray: " + maxgray);
 			return new PGMFileInfo(width, height, false, maxgray);
-		}
-		if ((Identifier1 == 'P') && (Identifier2 == '3')) {
-			int max = Integer.parseInt(wsr.readtoWhiteSpace());
-			// System.out.println("max: " + max);
-			return new PPMFileInfo(width, height, false, max);
-		}
-		if ((Identifier1 == 'P') && (Identifier2 == '4')) {
-			return new PBMFileInfo(width, height, true);
-		}
-		if ((Identifier1 == 'P') && (Identifier2 == '5')) {
+		} else if (identifier2 == PGM_RAW_CODE)
+		{
 			int maxgray = Integer.parseInt(wsr.readtoWhiteSpace());
-			// System.out.println("maxgray: " + maxgray);
 			return new PGMFileInfo(width, height, true, maxgray);
-		}
-		if ((Identifier1 == 'P') && (Identifier2 == '6')) {
+		} else if (identifier2 == PPM_TEXT_CODE)
+		{
+			int max = Integer.parseInt(wsr.readtoWhiteSpace());
+			return new PPMFileInfo(width, height, false, max);
+		} else if (identifier2 == PPM_RAW_CODE)
+		{
 			int max = Integer.parseInt(wsr.readtoWhiteSpace());
 			// System.out.println("max: " + max);
 			return new PPMFileInfo(width, height, true, max);
-		}
-
-		throw new ImageReadException("PNM: Bad Magic Number: " + Identifier1
-				+ ", " + Identifier2);
+		} else
+			throw new ImageReadException("PNM file has invalid header.");
 	}
 
 	private FileInfo readHeader(ByteSource byteSource)
-			throws ImageReadException, IOException {
+			throws ImageReadException, IOException
+	{
 		InputStream is = null;
 
-		try {
+		try
+		{
 			is = byteSource.getInputStream();
 
 			return readHeader(is);
-		} finally {
-			try {
+		} finally
+		{
+			try
+			{
 				is.close();
-			} catch (Exception e) {
+			} catch (Exception e)
+			{
 				Debug.debug(e);
 			}
 		}
 	}
 
 	public byte[] getICCProfileBytes(ByteSource byteSource, Map params)
-			throws ImageReadException, IOException {
+			throws ImageReadException, IOException
+	{
 		return null;
 	}
 
 	public Dimension getImageSize(ByteSource byteSource, Map params)
-			throws ImageReadException, IOException {
+			throws ImageReadException, IOException
+	{
 		FileInfo info = readHeader(byteSource);
 
 		if (info == null)
@@ -147,21 +158,25 @@
 		return new Dimension(info.width, info.height);
 	}
 
-	public byte[] embedICCProfile(byte image[], byte profile[]) {
+	public byte[] embedICCProfile(byte image[], byte profile[])
+	{
 		return null;
 	}
 
-	public boolean embedICCProfile(File src, File dst, byte profile[]) {
+	public boolean embedICCProfile(File src, File dst, byte profile[])
+	{
 		return false;
 	}
 
 	public IImageMetadata getMetadata(ByteSource byteSource, Map params)
-			throws ImageReadException, IOException {
+			throws ImageReadException, IOException
+	{
 		return null;
 	}
 
 	public ImageInfo getImageInfo(ByteSource byteSource, Map params)
-			throws ImageReadException, IOException {
+			throws ImageReadException, IOException
+	{
 		FileInfo info = readHeader(byteSource);
 
 		if (info == null)
@@ -201,15 +216,16 @@
 	}
 
 	public boolean dumpImageFile(PrintWriter pw, ByteSource byteSource)
-			throws ImageReadException, IOException {
+			throws ImageReadException, IOException
+	{
 		pw.println("pnm.dumpImageFile");
 
 		{
-			ImageInfo fImageData = getImageInfo(byteSource);
-			if (fImageData == null)
+			ImageInfo imageData = getImageInfo(byteSource);
+			if (imageData == null)
 				return false;
 
-			fImageData.toString(pw, "");
+			imageData.toString(pw, "");
 		}
 
 		pw.println("");
@@ -218,7 +234,8 @@
 	}
 
 	private int[] getColorTable(byte bytes[]) throws ImageReadException,
-			IOException {
+			IOException
+	{
 		if ((bytes.length % 3) != 0)
 			throw new ImageReadException("Bad Color Table Length: "
 					+ bytes.length);
@@ -226,7 +243,8 @@
 
 		int result[] = new int[length];
 
-		for (int i = 0; i < length; i++) {
+		for (int i = 0; i < length; i++)
+		{
 			int red = 0xff & bytes[(i * 3) + 0];
 			int green = 0xff & bytes[(i * 3) + 1];
 			int blue = 0xff & bytes[(i * 3) + 2];
@@ -241,10 +259,12 @@
 	}
 
 	public BufferedImage getBufferedImage(ByteSource byteSource, Map params)
-			throws ImageReadException, IOException {
+			throws ImageReadException, IOException
+	{
 		InputStream is = null;
 
-		try {
+		try
+		{
 			is = byteSource.getInputStream();
 
 			FileInfo info = readHeader(is);
@@ -259,10 +279,13 @@
 			info.readImage(result, is);
 
 			return result;
-		} finally {
-			try {
+		} finally
+		{
+			try
+			{
 				is.close();
-			} catch (Exception e) {
+			} catch (Exception e)
+			{
 				Debug.debug(e);
 			}
 		}
@@ -273,30 +296,34 @@
 	public static final String PARAM_VALUE_PNM_RAWBITS_NO = "NO";
 
 	public void writeImage(BufferedImage src, OutputStream os, Map params)
-			throws ImageWriteException, IOException {
+			throws ImageWriteException, IOException
+	{
 		PNMWriter writer = null;
-		boolean RAWBITS = true;
+		boolean useRawbits = true;
 
-		if (params != null) {
-			Object fRAWBITS = params.get(PARAM_KEY_PNM_RAWBITS);
-			if (fRAWBITS != null) {
-				if (fRAWBITS.equals(PARAM_VALUE_PNM_RAWBITS_NO))
-					RAWBITS = false;
+		if (params != null)
+		{
+			Object useRawbitsParam = params.get(PARAM_KEY_PNM_RAWBITS);
+			if (useRawbitsParam != null)
+			{
+				if (useRawbitsParam.equals(PARAM_VALUE_PNM_RAWBITS_NO))
+					useRawbits = false;
 			}
 
 			Object subtype = params.get(PARAM_KEY_FORMAT);
-			if (subtype != null) {
+			if (subtype != null)
+			{
 				if (subtype.equals(ImageFormat.IMAGE_FORMAT_PBM))
-					writer = new PBMWriter(RAWBITS);
+					writer = new PBMWriter(useRawbits);
 				else if (subtype.equals(ImageFormat.IMAGE_FORMAT_PGM))
-					writer = new PGMWriter(RAWBITS);
+					writer = new PGMWriter(useRawbits);
 				else if (subtype.equals(ImageFormat.IMAGE_FORMAT_PPM))
-					writer = new PPMWriter(RAWBITS);
+					writer = new PPMWriter(useRawbits);
 			}
 		}
 
 		if (writer == null)
-			writer = new PPMWriter(RAWBITS);
+			writer = new PPMWriter(useRawbits);
 
 		// make copy of params; we'll clear keys as we consume them.
 		params = new HashMap(params);
@@ -305,7 +332,8 @@
 		if (params.containsKey(PARAM_KEY_FORMAT))
 			params.remove(PARAM_KEY_FORMAT);
 
-		if (params.size() > 0) {
+		if (params.size() > 0)
+		{
 			Object firstKey = params.keySet().iterator().next();
 			throw new ImageWriteException("Unknown parameter: " + firstKey);
 		}
@@ -321,10 +349,11 @@
 	 *            File containing image data.
 	 * @param params
 	 *            Map of optional parameters, defined in SanselanConstants.
-	 * @return Xmp Xml as String, if present.  Otherwise, returns null..
+	 * @return Xmp Xml as String, if present. Otherwise, returns null..
 	 */
 	public String getXmpXml(ByteSource byteSource, Map params)
-			throws ImageReadException, IOException {
+			throws ImageReadException, IOException
+	{
 		return null;
 	}
 }
\ No newline at end of file