I tried the code below with a 24bit BMP file saved from the "Paint" program on 
Windows XP.  There are no ImageIO readers installed beyond what is included in 
the JRE/JDK.
The reader that fails is com.sun.imageio.plugins.bmp.BMPImageReader
This fails on Java 5 and Mustang.

[code]
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.FileImageInputStream;

/*
 * Created on 25-May-2006
 *
 */

public class ImageIoBug
{

        /**
         * @param args
         *            the path to an image file use a .BMP to fail.
         */
        public static void main(String[] args) throws Exception
        {
                BufferedImage testImage = null;
                if (args.length > 0)
                {
                        String path = args[0];
                        String ext = path.substring(path.lastIndexOf('.') + 1);
                        System.out.println("Readers claiming to handle this 
format:");

                        for (Iterator<ImageReader> ri = 
ImageIO.getImageReaders(new FileImageInputStream(new File(path))); 
ri.hasNext();)
                                System.out.println("  " + 
ri.next().getClass().getCanonicalName());
                        Iterator readerIt = 
ImageIO.getImageReadersBySuffix(ext);
                        if (readerIt.hasNext()) /* This code fails for BMP 
files */
                        {
                                System.out.println("Using reader for \"" + ext 
+ "\" suffix");
                                ImageReader reader = (ImageReader) 
readerIt.next();
                                
System.out.println(reader.getClass().getCanonicalName());
                                reader.setInput(new FileImageInputStream(new 
File(path)));
                                int width = reader.getWidth(0);
                                int height = reader.getHeight(0);
                                System.out.printf("Image is %d x %d", width, 
height);
                                testImage = new BufferedImage(width, height, 
BufferedImage.TYPE_INT_BGR);
                                ImageReadParam param = new ImageReadParam();
                                /* Setting the destination image makes the BMP 
reader fail! */
                                if (true)  /* Change to false to see that the 
.BMP file CAN be read */
                                        param.setDestination(testImage);
                                /* this method will throw some exceptions not 
mentioned in
                                 * the Java docs */
                                reader.read(0, param);
                        }
                        else /* the work-around */
                        {
                                System.out.println("ImageIO.read() then copy - 
the path that works.");
                                testImage = ImageIO.read(new File(path));
                                /* But I need the image data to be in INT_BGR 
form */
                                if (testImage != null && testImage.getType() != 
BufferedImage.TYPE_INT_BGR)
                                {
                                        BufferedImage testDest = new 
BufferedImage(
                                                testImage.getWidth(),
                                                testImage.getHeight(),
                                                BufferedImage.TYPE_INT_BGR);
                                        Graphics g = testDest.getGraphics();
                                        g.drawImage(testImage, 0, 0, null);
                                        g.dispose();
                                        testImage = testDest;
                                }
                        }
                }
                else
                {
                        System.out.println("Usage: java ImageIoBug 
<bitmap_file>");
                }
        }

}
[/code]

.. so I guess it's a trip to the bug reporter for me...
[Message sent by forum member 'swpalmer' (swpalmer)]

http://forums.java.net/jive/thread.jspa?messageID=118433

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to