United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-5076878 : ImageIO BMP Writer writes 32 bit files for 24 bit images

Details
Type:
Bug
Submit Date:
2004-07-22
Status:
Resolved
Updated Date:
2006-03-22
Project Name:
JDK
Resolved Date:
2006-03-22
Component:
client-libs
OS:
windows_xp
Sub-Component:
javax.imageio
CPU:
x86
Priority:
P4
Resolution:
Fixed
Affected Versions:
6
Fixed Versions:

Related Reports

Sub Tasks

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).
                                     
2006-03-13
CONVERTED DATA

BugTraq+ Release Management Values

COMMIT TO FIX:
mustang


                                     
2004-08-21
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
                                     
2004-07-22
WORK AROUND

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



Hardware and Software, Engineered to Work Together