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/02 17:44:21 UTC

svn commit: r682028 - in /incubator/sanselan/trunk/src: main/java/org/apache/sanselan/ main/java/org/apache/sanselan/common/ main/java/org/apache/sanselan/formats/bmp/ main/java/org/apache/sanselan/formats/png/ main/java/org/apache/sanselan/formats/psd...

Author: cmchen
Date: Sat Aug  2 10:44:21 2008
New Revision: 682028

URL: http://svn.apache.org/viewvc?rev=682028&view=rev
Log:
Altered the BMP image writer to include some optional color information.

Added:
    incubator/sanselan/trunk/src/main/java/org/apache/sanselan/common/RgbBufferedImageFactory.java   (with props)
Modified:
    incubator/sanselan/trunk/src/main/java/org/apache/sanselan/ImageInfo.java
    incubator/sanselan/trunk/src/main/java/org/apache/sanselan/common/BinaryFileFunctions.java
    incubator/sanselan/trunk/src/main/java/org/apache/sanselan/common/SimpleBufferedImageFactory.java
    incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/BmpImageParser.java
    incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/PngImageParser.java
    incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/PngWriter.java
    incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/ScanExpediter.java
    incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/ScanExpediterSimple.java
    incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/ImageContents.java
    incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/PSDHeaderInfo.java
    incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/TiffField.java
    incubator/sanselan/trunk/src/main/java/org/apache/sanselan/icc/IccTag.java
    incubator/sanselan/trunk/src/test/java/org/apache/sanselan/formats/tiff/TiffRoundtripTest.java

Modified: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/ImageInfo.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/ImageInfo.java?rev=682028&r1=682027&r2=682028&view=diff
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/ImageInfo.java (original)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/ImageInfo.java Sat Aug  2 10:44:21 2008
@@ -297,7 +297,8 @@
 			PrintWriter pw = new PrintWriter(sw);
 
 			toString(pw, "");
-
+			pw.flush();
+			
 			return sw.toString();
 		}
 		catch (Exception e)

Modified: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/common/BinaryFileFunctions.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/common/BinaryFileFunctions.java?rev=682028&r1=682027&r2=682028&view=diff
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/common/BinaryFileFunctions.java (original)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/common/BinaryFileFunctions.java Sat Aug  2 10:44:21 2008
@@ -58,9 +58,12 @@
 
 	public final void debugNumber(String msg, int data, int bytes)
 	{
-		debugNumber(new PrintWriter(new OutputStreamWriter(System.out)), msg,
+		PrintWriter pw = new PrintWriter(System.out);
+		debugNumber(pw, msg,
 				data, bytes);
+		pw.flush();
 	}
+	
 
 	public final void debugNumber(PrintWriter pw, String msg, int data)
 	{
@@ -126,7 +129,7 @@
 			if (data < 0)
 				throw new ImageReadException("Unexpected EOF.");
 
-			if(b != expected[i])
+			if (b != expected[i])
 			{
 				// System.out.println("i" + ": " + i);
 

Added: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/common/RgbBufferedImageFactory.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/common/RgbBufferedImageFactory.java?rev=682028&view=auto
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/common/RgbBufferedImageFactory.java (added)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/common/RgbBufferedImageFactory.java Sat Aug  2 10:44:21 2008
@@ -0,0 +1,40 @@
+/*
+ * 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.common;
+
+import java.awt.image.BufferedImage;
+
+import org.apache.sanselan.util.Debug;
+
+public class RgbBufferedImageFactory implements IBufferedImageFactory
+{
+	public BufferedImage getColorBufferedImage(int width, int height,
+			boolean hasAlpha)
+	{
+		if (hasAlpha)
+			return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
+		return new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
+	}
+
+	public BufferedImage getGrayscaleBufferedImage(int width, int height,
+			boolean hasAlpha)
+	{
+		// always use color.
+		return getColorBufferedImage(width, height, hasAlpha);
+	}
+}

Propchange: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/common/RgbBufferedImageFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/common/SimpleBufferedImageFactory.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/common/SimpleBufferedImageFactory.java?rev=682028&r1=682027&r2=682028&view=diff
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/common/SimpleBufferedImageFactory.java (original)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/common/SimpleBufferedImageFactory.java Sat Aug  2 10:44:21 2008
@@ -19,6 +19,8 @@
 
 import java.awt.image.BufferedImage;
 
+import org.apache.sanselan.util.Debug;
+
 public class SimpleBufferedImageFactory implements IBufferedImageFactory
 {
 	public BufferedImage getColorBufferedImage(int width, int height,
@@ -34,6 +36,7 @@
 	{
 		if (hasAlpha)
 			return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
+
 		return new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
 	}
 }

Modified: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/BmpImageParser.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/BmpImageParser.java?rev=682028&r1=682027&r2=682028&view=diff
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/BmpImageParser.java (original)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/BmpImageParser.java Sat Aug  2 10:44:21 2008
@@ -253,7 +253,7 @@
 			if (debug)
 				System.out.println("Compression: BI_BITFIELDS");
 			paletteLength = 3 * 4; // TODO: is this right? are the masks always
-									// LONGs?
+			// LONGs?
 			// BytesPerPixel = 2;
 			// BytesPerPaletteEntry = 4;
 			break;
@@ -267,7 +267,7 @@
 		if (paletteLength > 0)
 			colorTable = this.readByteArray("ColorTable", paletteLength, is,
 					"Not a Valid BMP File");
-		
+
 		if (debug)
 		{
 			this.debugNumber("paletteLength", paletteLength, 4);
@@ -306,10 +306,10 @@
 		}
 		int extraBytes = bhi.bitmapDataOffset - expectedDataOffset;
 		if (extraBytes < 0)
-			throw new ImageReadException("BMP has valid image data offset: "
+			throw new ImageReadException("BMP has invalid image data offset: "
 					+ bhi.bitmapDataOffset + " (expected: "
-					+ expectedDataOffset + ", paletteLength: "
-					+ paletteLength + ", headerSize: " + headerSize + ")");
+					+ expectedDataOffset + ", paletteLength: " + paletteLength
+					+ ", headerSize: " + headerSize + ")");
 		else if (extraBytes > 0)
 			this.readByteArray("BitmapDataOffset", extraBytes, is,
 					"Not a Valid BMP File");
@@ -584,7 +584,7 @@
 			os.write(0x4d); // M
 
 			int filesize = BITMAP_FILE_HEADER_SIZE + BITMAP_INFO_HEADER_SIZE + // header
-																				// size
+					// size
 					4 * writer.getPaletteSize() + // palette size in bytes
 					imagedata.length;
 			bos.write4Bytes(filesize);
@@ -608,7 +608,10 @@
 			bos.write4Bytes(imagedata.length); // Bitmap Data Size
 			bos.write4Bytes(0); // HResolution
 			bos.write4Bytes(0); // VResolution
-			bos.write4Bytes(0); // Colors
+			if (palette == null)
+				bos.write4Bytes(0); // Colors
+			else
+				bos.write4Bytes(palette.length()); // Colors
 			bos.write4Bytes(0); // Important Colors
 			// bos.write_4_bytes(0); // Compression
 		}

Modified: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/PngImageParser.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/PngImageParser.java?rev=682028&r1=682027&r2=682028&view=diff
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/PngImageParser.java (original)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/PngImageParser.java Sat Aug  2 10:44:21 2008
@@ -840,6 +840,8 @@
 
 		pw.println("");
 
+		pw.flush();
+		
 		return true;
 	}
 

Modified: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/PngWriter.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/PngWriter.java?rev=682028&r1=682027&r2=682028&view=diff
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/PngWriter.java (original)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/PngWriter.java Sat Aug  2 10:44:21 2008
@@ -110,21 +110,21 @@
 		public final int height;
 		public final byte bit_depth;
 		public final byte colorType;
-		public final byte compression_method;
-		public final byte filter_method;
-		public final byte interlace_method;
+		public final byte compressionMethod;
+		public final byte filterMethod;
+		public final byte interlaceMethod;
 
 		public ImageHeader(int width, int height, byte bit_depth,
-				byte colorType, byte compression_method, byte filter_method,
-				byte interlace_method)
+				byte colorType, byte compressionMethod, byte filterMethod,
+				byte interlaceMethod)
 		{
 			this.width = width;
 			this.height = height;
 			this.bit_depth = bit_depth;
 			this.colorType = colorType;
-			this.compression_method = compression_method;
-			this.filter_method = filter_method;
-			this.interlace_method = interlace_method;
+			this.compressionMethod = compressionMethod;
+			this.filterMethod = filterMethod;
+			this.interlaceMethod = interlaceMethod;
 		}
 
 	}
@@ -137,9 +137,9 @@
 		writeInt(baos, value.height);
 		baos.write(0xff & value.bit_depth);
 		baos.write(0xff & value.colorType);
-		baos.write(0xff & value.compression_method);
-		baos.write(0xff & value.filter_method);
-		baos.write(0xff & value.interlace_method);
+		baos.write(0xff & value.compressionMethod);
+		baos.write(0xff & value.filterMethod);
+		baos.write(0xff & value.interlaceMethod);
 
 		// Debug.debug("baos", baos.toByteArray());
 
@@ -319,18 +319,18 @@
 
 		byte colorType;
 		{
-			boolean force_indexed_color = ParamMap.getParamBoolean(params,
+			boolean forceIndexedColor = ParamMap.getParamBoolean(params,
 					PARAM_KEY_PNG_FORCE_INDEXED_COLOR, false);
-			boolean force_true_color = ParamMap.getParamBoolean(params,
+			boolean forceTrueColor = ParamMap.getParamBoolean(params,
 					PARAM_KEY_PNG_FORCE_TRUE_COLOR, false);
 
-			if (force_indexed_color && force_true_color)
+			if (forceIndexedColor && forceTrueColor)
 				throw new ImageWriteException(
 						"Params: Cannot force both indexed and true color modes");
-			else if (force_indexed_color)
+			else if (forceIndexedColor)
 			{
 				colorType = COLOR_TYPE_INDEXED_COLOR;
-			} else if (force_true_color)
+			} else if (forceTrueColor)
 			{
 				colorType = (byte) (hasAlpha ? COLOR_TYPE_TRUE_COLOR_WITH_ALPHA
 						: COLOR_TYPE_TRUE_COLOR);
@@ -340,33 +340,32 @@
 				Debug.debug("colorType", colorType);
 		}
 
-		byte bit_depth = getBitDepth(colorType, params);
+		byte bitDepth = getBitDepth(colorType, params);
 		if (verbose)
-			Debug.debug("bit_depth", bit_depth);
+			Debug.debug("bit_depth", bitDepth);
 
-		int sample_depth;
+		int sampleDepth;
 		if (colorType == COLOR_TYPE_INDEXED_COLOR)
-			sample_depth = 8;
+			sampleDepth = 8;
 		else
-			sample_depth = bit_depth;
+			sampleDepth = bitDepth;
 		if (verbose)
-			Debug.debug("sample_depth", sample_depth);
+			Debug.debug("sample_depth", sampleDepth);
 
 		{
 			os.write(PNG_Signature);
 		}
 		{
-			// IHDR Shall be first
+			// IHDR must be first
 
-			byte compression_method = COMPRESSION_TYPE_INFLATE_DEFLATE;
-			byte filter_method = FILTER_METHOD_ADAPTIVE;
-			byte interlace_method = INTERLACE_METHOD_NONE; // charles
+			byte compressionMethod = COMPRESSION_TYPE_INFLATE_DEFLATE;
+			byte filterMethod = FILTER_METHOD_ADAPTIVE;
+			byte interlaceMethod = INTERLACE_METHOD_NONE;
 
-			ImageHeader image_header = new ImageHeader(width, height,
-					bit_depth, colorType, compression_method, filter_method,
-					interlace_method);
+			ImageHeader imageHeader = new ImageHeader(width, height, bitDepth,
+					colorType, compressionMethod, filterMethod, interlaceMethod);
 
-			writeChunkIHDR(os, image_header);
+			writeChunkIHDR(os, imageHeader);
 		}
 
 		{
@@ -439,8 +438,9 @@
 								int gray = (red + green + blue) / 3;
 								// if(y==0)
 								// {
-								// Debug.debug(x + ", " + y + " argb",
-								// Integer.toHexString(argb));
+								// Debug.debug("gray: " + x + ", " + y + " argb: 0x"
+								//		+ Integer.toHexString(argb) + " gray: 0x"
+								// 		+ Integer.toHexString(gray));
 								// // Debug.debug(x + ", " + y + " gray", gray);
 								// // Debug.debug(x + ", " + y + " gray", gray);
 								// Debug.debug(x + ", " + y + " gray", gray +
@@ -456,13 +456,13 @@
 							}
 							if (useAlpha)
 								baos.write(alpha);
-
 						}
 					}
 				}
 				uncompressed = baos.toByteArray();
 			}
-			// Debug.debug("uncompressed", uncompressed.length);
+
+			 // Debug.debug("uncompressed", uncompressed.length);
 
 			ByteArrayOutputStream baos = new ByteArrayOutputStream();
 			DeflaterOutputStream dos = new DeflaterOutputStream(baos);

Modified: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/ScanExpediter.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/ScanExpediter.java?rev=682028&r1=682027&r2=682028&view=diff
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/ScanExpediter.java (original)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/ScanExpediter.java Sat Aug  2 10:44:21 2008
@@ -232,14 +232,14 @@
 	protected byte[] getNextScanline(InputStream is, int length, byte prev[],
 			int BytesPerPixel) throws ImageReadException, IOException
 	{
-		int filter_type = is.read();
-		if (filter_type < 0)
+		int filterType = is.read();
+		if (filterType < 0)
 			throw new ImageReadException("PNG: missing filter type");
 
 		byte scanline[] = this.readByteArray("scanline", length, is,
 				"PNG: missing image data");
 
-		byte unfiltered[] = unfilterScanline(filter_type, scanline, prev,
+		byte unfiltered[] = unfilterScanline(filterType, scanline, prev,
 				BytesPerPixel);
 
 		return unfiltered;

Modified: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/ScanExpediterSimple.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/ScanExpediterSimple.java?rev=682028&r1=682027&r2=682028&view=diff
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/ScanExpediterSimple.java (original)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/ScanExpediterSimple.java Sat Aug  2 10:44:21 2008
@@ -45,7 +45,6 @@
 
 		for (int y = 0; y < height; y++)
 		{
-
 			byte unfiltered[] = getNextScanline(is, pixelBytesPerScanLine,
 					prev, bytesPerPixel);
 

Modified: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/ImageContents.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/ImageContents.java?rev=682028&r1=682027&r2=682028&view=diff
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/ImageContents.java (original)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/ImageContents.java Sat Aug  2 10:44:21 2008
@@ -42,7 +42,9 @@
 
 	public void dump()
 	{
-		dump(new PrintWriter(new OutputStreamWriter(System.out)));
+		PrintWriter pw = new PrintWriter(System.out);
+		dump(pw);
+		pw.flush();
 	}
 
 	public void dump(PrintWriter pw)

Modified: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/PSDHeaderInfo.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/PSDHeaderInfo.java?rev=682028&r1=682027&r2=682028&view=diff
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/PSDHeaderInfo.java (original)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/PSDHeaderInfo.java Sat Aug  2 10:44:21 2008
@@ -44,7 +44,9 @@
 
 	public void dump()
 	{
-		dump(new PrintWriter(new OutputStreamWriter(System.out)));
+		PrintWriter pw = new PrintWriter(System.out);
+		dump(pw);
+		pw.flush();
 	}
 
 	public void dump(PrintWriter pw)

Modified: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/TiffField.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/TiffField.java?rev=682028&r1=682027&r2=682028&view=diff
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/TiffField.java (original)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/TiffField.java Sat Aug  2 10:44:21 2008
@@ -509,7 +509,9 @@
 
 	public void dump()
 	{
-		dump(new PrintWriter(new OutputStreamWriter(System.out)));
+		PrintWriter pw = new PrintWriter(System.out);
+		dump(pw);
+		pw.flush();
 	}
 
 	public void dump(PrintWriter pw)

Modified: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/icc/IccTag.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/icc/IccTag.java?rev=682028&r1=682027&r2=682028&view=diff
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/icc/IccTag.java (original)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/icc/IccTag.java Sat Aug  2 10:44:21 2008
@@ -76,6 +76,8 @@
 		PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
 
 		dump(pw, prefix);
+		
+		pw.flush();
 	}
 
 	public void dump(PrintWriter pw, String prefix) throws ImageReadException,

Modified: incubator/sanselan/trunk/src/test/java/org/apache/sanselan/formats/tiff/TiffRoundtripTest.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/test/java/org/apache/sanselan/formats/tiff/TiffRoundtripTest.java?rev=682028&r1=682027&r2=682028&view=diff
==============================================================================
--- incubator/sanselan/trunk/src/test/java/org/apache/sanselan/formats/tiff/TiffRoundtripTest.java (original)
+++ incubator/sanselan/trunk/src/test/java/org/apache/sanselan/formats/tiff/TiffRoundtripTest.java Sat Aug  2 10:44:21 2008
@@ -62,7 +62,7 @@
 					ImageFormat.IMAGE_FORMAT_TIFF, params);
 
 			BufferedImage image2 = Sanselan.getBufferedImage(tempFile);
-			assertNotNull(image);
+			assertNotNull(image2);
 		}
 	}