JDK-4826548 : PNG-files with 5,6,7, or 8 colors in the palette cannot be loaded
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.imageio
  • Affected Version: 1.4.0,1.4.1,1.4.2
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS:
    linux,solaris_7,solaris_8,windows_2000,windows_xp linux,solaris_7,solaris_8,windows_2000,windows_xp
  • CPU: x86,sparc
  • Submitted: 2003-03-03
  • Updated: 2004-08-19
  • Resolved: 2003-09-29
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
5.0 tigerFixed
Related Reports
Relates :  
Description

Name: rmT116609			Date: 03/03/2003


FULL PRODUCT VERSION :
java version "1.4.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-b21)
Java HotSpot(TM) Client VM (build 1.4.1-b21, mixed mode)

FULL OS VERSION :
Microsoft Windows 2000 [Version 5.00.2195]


A DESCRIPTION OF THE PROBLEM :
I've instantiated an BufferedImage of type TYPE_BYTE_BINARY with an IndexColorModel.
This image was saved as PNG with javax.imageio.ImageIO.write(image, "png", out).

The image cannot be loaded via ImageIO.read() if the number of colors in the image's palette is 5, 6, 7, or 8.
In that case, the following exception is thrown:

java.lang.ArrayIndexOutOfBoundsException: 4
	at com.sun.imageio.plugins.png.PNGImageReader.parse_PLTE_chunk(PNGImageReader.java:347)
	at com.sun.imageio.plugins.png.PNGImageReader.readMetadata(PNGImageReader.java:635)
	at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1309)
	at com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1530)
	at javax.imageio.ImageIO.read(ImageIO.java:1384)
	at javax.imageio.ImageIO.read(ImageIO.java:1270)
	at PngTest.testPng(PngTest.java:19)
	at PngTest.main(PngTest.java:50)

The problem is in PNGImageReader.parse_PLTE_chunk in a section commented with "Round array sizes up to 2^2^n".
This section doesn't fulfils the pre-condition of the following for-loop that ist numEntries<=paletteEntries

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
I used a test class for producing the problem.
Basically, I stored an indexed Image as PNG file and was not able to reload it with ImageIO if the ColorModel contains a specific number of colors.



EXPECTED VERSUS ACTUAL BEHAVIOR :
The output of the test class should be

Number of colors: 2
OK
Number of colors: 3
OK

with the number of colors counting up to 16
The output produces exceptions for the number of colors of 5, 6, 7, or 8

ERROR MESSAGES/STACK TRACES THAT OCCUR :
java.lang.ArrayIndexOutOfBoundsException: 4
	at com.sun.imageio.plugins.png.PNGImageReader.parse_PLTE_chunk(PNGImageReader.java:347)
	at com.sun.imageio.plugins.png.PNGImageReader.readMetadata(PNGImageReader.java:635)
	at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1309)
	at com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1530)
	at javax.imageio.ImageIO.read(ImageIO.java:1384)
	at javax.imageio.ImageIO.read(ImageIO.java:1270)
	at PngTest.testPng(PngTest.java:19)
	at PngTest.main(PngTest.java:50)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.image.BufferedImage;
import java.awt.image.IndexColorModel;
import java.io.File;

import javax.imageio.ImageIO;

/**
 * Checks for a problem reading PNG images with 5 to 8 colors.
 */
public class PngTest {

	private void testPng() {
		File pngFile = new File("PngTest.png");
		for (int numberColors = 2; numberColors <= 16; numberColors++) {
			try {
				BufferedImage image = createImage(numberColors);
				ImageIO.write(image, "png", pngFile);
				System.out.println("Number of colors: " + numberColors);
				ImageIO.read(pngFile);
				System.out.println("OK");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private BufferedImage createImage(int numberColors) {
		return new BufferedImage(
			32,
			32,
			BufferedImage.TYPE_BYTE_BINARY,
			createColorModel(numberColors));
	}

	private IndexColorModel createColorModel(int numberColors) {
		
		byte[] colors = new byte[16 * 3];
		int depth = 4;
		int startIndex = 0;
		
		return new IndexColorModel(
			depth,
			numberColors,
			colors,
			startIndex,
			false);
	}

	public static void main(String[] args) {
		new PngTest().testPng();
	}
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
At the time, I use a palette size of 9 ignoring the last color, since the desired palette size 8 has the described problem.
(Review ID: 182053) 
======================================================================

Name: rmT116609			Date: 03/03/2003


DESCRIPTION OF THE PROBLEM :
Attached is a simple app that reads of png file and writes a jpeg file.
It works fine when the file in question is a png file with 44kb of data.
But it fails if the file is small with only 8kb of data.   Note that the
files in question can be properly displayed with a browser, for example
netscape 4.76.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1.run with a "large" png file
2.run with a "small" png file


EXPECTED VERSUS ACTUAL BEHAVIOR :
A jpeg file is the expected result in all cases.
It seems to work for larger png files.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
java.lang.ArrayIndexOutOfBoundsException: 4
	at com.sun.imageio.plugins.png.PNGImageReader.parse_PLTE_chunk(PNGImageReader.java:347)
	at com.sun.imageio.plugins.png.PNGImageReader.readMetadata(PNGImageReader.java:635)
	at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1309)
	at com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1530)
	at javax.imageio.ImageIO.read(ImageIO.java:1384)
	at javax.imageio.ImageIO.read(ImageIO.java:1270)
	at test_code.DumpJPEG.main(DumpJPEG.java:17)
Exception in thread "main"

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
package test_code;

import java.io.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

public class DumpJPEG{

    public DumpJPEG(){}

    public static void main(String args[]){

		String fn = "web/src/test_code/in_2d_image.png";
		System.out.println(fn);
		try{
			File infile = new File(fn);
			BufferedImage im = ImageIO.read(infile);
		
			File outfile = new File("web/src/test_code/out_2d_image.jpg");
			ImageIO.write(im,"jpg",outfile);
		} catch(IOException ioe) {
			System.out.println("file problem");
		}
    }
}

---------- END SOURCE ----------
(Review ID: 179075)
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: tiger FIXED IN: tiger INTEGRATED IN: tiger tiger-b22
20-08-2004

EVALUATION Name: abR10136 Date: 09/11/2003 The reason of problem is that the length of the color arrays (paletteEntries) is not calculated correctly. It should be 16 if number of palette entries (numEntries) is greater than 4 (instead of 8 in original version) and smaller than 16. ======================================================================
20-08-2004