JDK-5076878 : ImageIO BMP Writer writes 32 bit files for 24 bit images
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.imageio
  • Affected Version: 6
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-07-22
  • Updated: 2006-03-22
  • Resolved: 2006-03-22
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.
JDK 6
6 b77Fixed
Description
Name: cc94850			Date: 07/21/2004


FULL PRODUCT VERSION :
java version "1.5.0-beta3"
Java(TM) Runtime Environment, Standard Edition (build 1.5.0-beta3-b58)
Java HotSpot(TM) Client VM (build 1.5.0-beta3-b58, mixed mode, sharing)

A DESCRIPTION OF THE PROBLEM :
Using ImageIO.write(RenderedImage,  "BMP", File)  you will get a BMP file representing a 32 bit image even when the source image is only 24 bits per pixel.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
A 24 bpp BMP file
ACTUAL -
A 32 bpp BMP file

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/*
 * Created on Jul 21, 2004 by scottpalmer
 */

/**
 * @author scottpalmer
 *
 */
public class ImageIOBMP {

	public static void main(String[] args) throws IOException {
		// Create a 24-bit image
		BufferedImage img = new BufferedImage(720,480,BufferedImage.TYPE_INT_RGB);
		// give it some content
		Graphics2D g = (Graphics2D) img.getGraphics();
		g.setColor(Color.RED);
		g.fillOval(0,0,img.getWidth(),img.getHeight());
		g.setColor(Color.GREEN);
		g.fillOval(100,100,img.getWidth()-200,img.getHeight()-200);
		g.setColor(Color.BLUE);
		g.fillOval(200,200,img.getWidth()-400,img.getHeight()-400);
		g.dispose();
		
		// Write it out with ImageIO BMP writer
		String filename = "ShouldBe24Bit.bmp";
		String format = "BMP";

		ImageIO.write(img,format, new File(filename));
		
		System.out.println("Created \""+filename+"\"");
		System.out.println("Size should be about 1MB, if it is closer to 1.3MB an alpha channel was written.");;
		System.out.println("Size of \""+filename+"\" is "+new File(filename).length());
	}
}

---------- END SOURCE ----------
(Incident Review ID: 286713) 
======================================================================

Comments
EVALUATION Now we count bitsPerPixel value for SinglePixelPackedSampleModel case using sizes of image bands. This change causes using of writePixels procedure for all cases when actually used number of bits per pixel is smaller than size of databuffer type (for example, in case of TYPE_INT_RGB actual bpp is 24 but size of databuffer type is 32).
13-03-2006

CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: mustang
21-08-2004

WORK AROUND Instead of TYPE_INT_RGB, use TYPE_3BYTE_BGR in order to save a 24-bit BMP image. ###@###.### 2004-07-22
22-07-2004

EVALUATION I believe the problem is at line 365 in BMPImageWriter.java... We incorrectly set the bitsPerPixel variable to the DataBuffer's data type size, which in the case of TYPE_INT_RGB is 32 bits, not 24. ###@###.### 2004-07-22
22-07-2004