Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6433172

BMPImageReader fails if the Destination is set

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Cannot Reproduce
    • Affects Version/s: 5.0, 8, 9
    • Fix Version/s: 9
    • Component/s: client-libs
    • Labels:

      Description

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

      AND

      java version "1.6.0-beta2"
      Java(TM) SE Runtime Environment (build 1.6.0-beta2-b85)
      Java HotSpot(TM) Client VM (build 1.6.0-beta2-b85, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      Trying to read into a pre-created BufferedImage makes the BMP reader fail with a NPE

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the attached test code, passing the path to a BMP file as the command-line argument

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The image file should be read into the BufferedImage provided as it is when reading a JPEG image.
      ACTUAL -
      NullPointerException

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "main" java.lang.NullPointerException
              at javax.imageio.stream.ImageInputStreamImpl.readFully(Unknown Source)
              at com.sun.imageio.plugins.bmp.BMPImageReader.read24Bit(Unknown Source)
              at com.sun.imageio.plugins.bmp.BMPImageReader.read(Unknown Source)
              at ImageIoBug.main(ImageIoBug.java:52)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      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\n", 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
      {
      System.out.println("Usage: java ImageIoBug <bitmap_file>");
      }
      }

      }

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

      CUSTOMER SUBMITTED WORKAROUND :

      Copy the image into a new image after reading...

      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;
      }

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              jdv Jayathirth D V
              Reporter:
              ndcosta Nelson Dcosta (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: