commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cmc...@apache.org
Subject svn commit: r995505 - in /commons/proper/sanselan/trunk/src: main/java/org/apache/sanselan/formats/png/ test/data/images/png/4/ test/java/org/apache/sanselan/formats/png/
Date Thu, 09 Sep 2010 17:27:51 GMT
Author: cmchen
Date: Thu Sep  9 17:27:51 2010
New Revision: 995505

URL: http://svn.apache.org/viewvc?rev=995505&view=rev
Log:
Fixed a bug wherein grayscale pngs were not written correctly.

Added:
    commons/proper/sanselan/trunk/src/test/data/images/png/4/
    commons/proper/sanselan/trunk/src/test/data/images/png/4/buttons_level_menu_down.ipad.png
  (with props)
    commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/formats/png/PngWriteForceTrueColorText.java
  (with props)
Modified:
    commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/PngConstants.java
    commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/PngImageParser.java
    commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/PngWriter.java
    commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/formats/png/ConvertPngToGifTest.java

Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/PngConstants.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/PngConstants.java?rev=995505&r1=995504&r2=995505&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/PngConstants.java
(original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/PngConstants.java
Thu Sep  9 17:27:51 2010
@@ -73,8 +73,8 @@ public interface PngConstants extends Sa
 	public final static int iTXt = PngImageParser.CharsToQuad('i', 'T', 'X',
 			't');
 
-	public static final byte PNG_Signature[] = { (byte) 137, 80, 78, 71, 13,
-			10, 26, 10, };
+	public static final byte PNG_Signature[] = { 
+		(byte) 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, };
 
 	public static final String PARAM_KEY_PNG_BIT_DEPTH = "PNG_BIT_DEPTH";
 	public static final String PARAM_KEY_PNG_FORCE_INDEXED_COLOR = "PNG_FORCE_INDEXED_COLOR";

Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/PngImageParser.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/PngImageParser.java?rev=995505&r1=995504&r2=995505&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/PngImageParser.java
(original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/PngImageParser.java
Thu Sep  9 17:27:51 2010
@@ -96,7 +96,55 @@ public class PngImageParser extends Imag
 	}
 
 	// private final static int tRNS = CharsToQuad('t', 'R', 'N', 's');
+	
+	public static final String getChunkTypeName(int chunkType) {
+		StringBuffer result = new StringBuffer();
+		result.append((char) (0xff & (chunkType >> 24)));
+		result.append((char) (0xff & (chunkType >> 16)));
+		result.append((char) (0xff & (chunkType >> 8)));
+		result.append((char) (0xff & (chunkType >> 0)));
+		return result.toString();
+	}
+	
+	/**
+	 * @return List of String-formatted chunk types, ie. "tRNs".
+	 */
+	public List getChuckTypes(InputStream is) throws ImageReadException, IOException {
+		List chunks = readChunks(is, null, false);
+		List chunkTypes = new ArrayList();
+		for (int i=0; i<chunks.size(); i++) {
+			PNGChunk chunk = (PNGChunk) chunks.get(i);
+			chunkTypes.add(getChunkTypeName(chunk.chunkType));
+		}
+		return chunkTypes;
+	}
+	
+	public boolean hasChuckType(ByteSource byteSource, int chunkType) throws ImageReadException,
IOException
+	{
+		InputStream is = null;
 
+		try
+		{
+			is = byteSource.getInputStream();
+
+			ArrayList chunks = null;
+
+			readSignature(is);
+			chunks = readChunks(is, new int[] { chunkType, }, true);
+			return chunks.size() > 0;
+		} finally
+		{
+			try
+			{
+				is.close();
+			} catch (Exception e)
+			{
+				Debug.debug(e);
+			}
+		}
+	}
+	
+	
 	private boolean keepChunk(int ChunkType, int chunkTypes[])
 	{
 		// System.out.println("keepChunk: ");
@@ -123,7 +171,7 @@ public class PngImageParser extends Imag
 
 			int length = read4Bytes("Length", is, "Not a Valid PNG File");
 			int chunkType = read4Bytes("ChunkType", is, "Not a Valid PNG File");
-
+			
 			if (debug)
 			{
 				printCharQuad("ChunkType", chunkType);
@@ -144,7 +192,7 @@ public class PngImageParser extends Imag
 					debugNumber("bytes", bytes.length, 4);
 
 			int CRC = read4Bytes("CRC", is, "Not a Valid PNG File");
-
+			
 			if (keep)
 			{
 				if (chunkType == iCCP)
@@ -181,7 +229,7 @@ public class PngImageParser extends Imag
 
 	}
 
-	private void readSignature(InputStream is) throws ImageReadException,
+	public void readSignature(InputStream is) throws ImageReadException,
 			IOException
 	{
 		readAndVerifyBytes(is, PNG_Signature,
@@ -622,7 +670,6 @@ public class PngImageParser extends Imag
 	public BufferedImage getBufferedImage(ByteSource byteSource, Map params)
 			throws ImageReadException, IOException
 	{
-
 		boolean verbose = ParamMap.getParamBoolean(params, PARAM_KEY_VERBOSE,
 				false);
 

Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/PngWriter.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/PngWriter.java?rev=995505&r1=995504&r2=995505&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/PngWriter.java
(original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/png/PngWriter.java
Thu Sep  9 17:27:51 2010
@@ -420,6 +420,7 @@ public class PngWriter implements PngCon
 			{
 				colorType = (byte) (hasAlpha ? COLOR_TYPE_TRUE_COLOR_WITH_ALPHA
 						: COLOR_TYPE_TRUE_COLOR);
+				isGrayscale = false;
 			} else
 				colorType = getColourType(hasAlpha, isGrayscale);
 			if (verbose)

Added: commons/proper/sanselan/trunk/src/test/data/images/png/4/buttons_level_menu_down.ipad.png
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/test/data/images/png/4/buttons_level_menu_down.ipad.png?rev=995505&view=auto
==============================================================================
Binary file - no diff available.

Propchange: commons/proper/sanselan/trunk/src/test/data/images/png/4/buttons_level_menu_down.ipad.png
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Modified: commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/formats/png/ConvertPngToGifTest.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/formats/png/ConvertPngToGifTest.java?rev=995505&r1=995504&r2=995505&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/formats/png/ConvertPngToGifTest.java
(original)
+++ commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/formats/png/ConvertPngToGifTest.java
Thu Sep  9 17:27:51 2010
@@ -42,10 +42,8 @@ public class ConvertPngToGifTest extends
 				Debug.purgeMemory();
 
 			File imageFile = (File) images.get(i);
-			
 			if (isInvalidPNGTestFile(imageFile))
 				continue;
-			
 			// Debug.debug("imageFile", imageFile);
 			// Debug.debug();
 

Added: commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/formats/png/PngWriteForceTrueColorText.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/formats/png/PngWriteForceTrueColorText.java?rev=995505&view=auto
==============================================================================
--- commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/formats/png/PngWriteForceTrueColorText.java
(added)
+++ commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/formats/png/PngWriteForceTrueColorText.java
Thu Sep  9 17:27:51 2010
@@ -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.sanselan.formats.png;
+
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.sanselan.ImageFormat;
+import org.apache.sanselan.ImageReadException;
+import org.apache.sanselan.ImageWriteException;
+import org.apache.sanselan.Sanselan;
+import org.apache.sanselan.util.Debug;
+
+public class PngWriteForceTrueColorText extends PngBaseTest {
+
+	public void test() throws Exception {
+
+		List images = getPngImages();
+		for (int i = 0; i < images.size(); i++) {
+			if (i % 10 == 0)
+				Debug.purgeMemory();
+
+			File imageFile = (File) images.get(i);
+			try {
+				if (isInvalidPNGTestFile(imageFile))
+					continue;
+
+				Debug.debug("imageFile", imageFile);
+				// Debug.debug();
+
+				// params.put(SanselanConstants.PARAM_KEY_VERBOSE,
+				// Boolean.TRUE);
+
+				BufferedImage image = Sanselan.getBufferedImage(imageFile,
+						new HashMap());
+				assertNotNull(image);
+
+				File outFile = createTempFile(imageFile.getName() + ".", ".gif");
+				// Debug.debug("outFile", outFile);
+
+				Map params = new HashMap();
+				params.put(PngConstants.PARAM_KEY_PNG_FORCE_TRUE_COLOR,
+						Boolean.TRUE);
+				Sanselan.writeImage(image, outFile,
+						ImageFormat.IMAGE_FORMAT_PNG, params);
+
+				BufferedImage image2 = Sanselan.getBufferedImage(outFile,
+						new HashMap());
+			} catch (Exception e) {
+				Debug.debug("imageFile", imageFile);
+				throw e;
+			}
+		}
+		Debug.debug("complete.");
+	}
+
+}

Propchange: commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/formats/png/PngWriteForceTrueColorText.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message