changeset 26743:217c8f2c9cff

8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle Reviewed-by: serb, prr
author bae
date Fri, 12 Sep 2014 13:04:18 +0100
parents ff54dcccf417
children fda83f5c88d1
files jdk/src/java.desktop/share/classes/com/sun/imageio/plugins/bmp/BMPImageReader.java jdk/test/javax/imageio/MultiReadTest.java
diffstat 2 files changed, 134 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/jdk/src/java.desktop/share/classes/com/sun/imageio/plugins/bmp/BMPImageReader.java	Fri Sep 12 10:40:07 2014 +0400
+++ b/jdk/src/java.desktop/share/classes/com/sun/imageio/plugins/bmp/BMPImageReader.java	Fri Sep 12 13:04:18 2014 +0100
@@ -108,6 +108,7 @@
     // BMP variables
     private long bitmapFileSize;
     private long bitmapOffset;
+    private long bitmapStart;
     private long compression;
     private long imageSize;
     private byte palette[];
@@ -677,6 +678,8 @@
         //start of image data
         iis.reset();
         iis.skipBytes(bitmapOffset);
+        bitmapStart = iis.getStreamPosition();
+
         gotHeader = true;
     }
 
@@ -813,6 +816,8 @@
                 idata = ((DataBufferInt)raster.getDataBuffer()).getData();
         }
 
+        iis.seek(bitmapStart);
+
         // There should only be one tile.
         switch(imageType) {
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/imageio/MultiReadTest.java	Fri Sep 12 13:04:18 2014 +0100
@@ -0,0 +1,129 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @bug     8041465
+ * @summary Test verifies that standard ImageIO plugins handle
+ *          multiple reads correctly.
+ *
+ * @run     main MultiReadTest
+ */
+
+import java.awt.Color;
+import java.awt.Graphics2D;
+import java.awt.Rectangle;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+import javax.imageio.ImageIO;
+import javax.imageio.ImageReadParam;
+import javax.imageio.ImageReader;
+import javax.imageio.stream.ImageInputStream;
+
+public class MultiReadTest {
+
+    private static final File pwd = new File(".");
+
+    private static final Color srcColor = Color.red;
+    private static final int width = 200;
+    private static final int height = 200;
+
+    private static final int max = 5;
+
+    public static void main(String[] args) throws IOException {
+        final String[] formats = { "bmp", "png", "gif", "jpg" };
+
+        for (String f : formats) {
+            test(f);
+        }
+    }
+
+    private static void test(String format) throws IOException {
+        System.out.println("Format: " + format);
+
+        BufferedImage src = createSrc();
+
+        ImageInputStream iis = prepareInput(src, format);
+
+        ImageReader reader = ImageIO.getImageReaders(iis).next();
+
+        reader.setInput(iis);
+
+        ImageReadParam p = reader.getDefaultReadParam();
+        int cnt = 0;
+        do {
+            System.out.println("cnt: " + cnt);
+            p.setSourceRegion(new Rectangle(width / 4, height / 4,
+                                            width / 2, height / 2));
+
+            BufferedImage dst = reader.read(0, p);
+
+            final Color c = new Color(dst.getRGB(10, 10), true);
+
+            if (!sameColor(c, srcColor)) {
+                throw new RuntimeException(
+                    String.format("Test failed: read color 0x%X\n",
+                                  c.getRGB()));
+            }
+        } while (++cnt < max);
+    }
+
+    private static boolean sameColor(Color c1, Color c2) {
+        final float delta = 0.1f;
+
+        float[] rgb1 = new float[4];
+        float[] rgb2 = new float[4];
+
+        rgb1 = c1.getRGBComponents(rgb1);
+        rgb2 = c2.getRGBComponents(rgb2);
+
+        for (int i = 0; i < rgb1.length; i++) {
+            if (Math.abs(rgb1[i] - rgb2[i]) > delta) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    private static BufferedImage createSrc() {
+        BufferedImage img = new BufferedImage(width, height,
+                BufferedImage.TYPE_INT_RGB);
+
+        Graphics2D g = img.createGraphics();
+        g.setColor(srcColor);
+        g.fillRect(0, 0, width, height);
+        g.dispose();
+
+        return img;
+    }
+
+    private static ImageInputStream prepareInput(BufferedImage src, String format) throws IOException {
+        File f = File.createTempFile("src_", "." + format, pwd);
+
+        if (ImageIO.write(src, format, f)) {
+            return ImageIO.createImageInputStream(f);
+        }
+        return null;
+    }
+}