test/jdk/javax/imageio/MultiReadTest.java
author erikj
Fri, 06 Apr 2018 02:52:24 +0200
changeset 49540 9704789737c1
parent 47216 71c04702a3d5
permissions -rw-r--r--
8201222: JDK-8199539 broke the OpenJDK build Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26743
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
     1
/*
35693
5bdca98e8410 8146881: [TEST] update some imageio plugins tests to affect TIFF format
avstepan
parents: 26743
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
26743
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
     4
 *
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
     7
 * published by the Free Software Foundation.
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
     8
 *
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    13
 * accompanied this code).
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    14
 *
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    18
 *
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    21
 * questions.
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    22
 */
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    23
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    24
/**
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    25
 * @test
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    26
 * @bug     8041465
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    27
 * @summary Test verifies that standard ImageIO plugins handle
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    28
 *          multiple reads correctly.
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    29
 *
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    30
 * @run     main MultiReadTest
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    31
 */
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    32
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    33
import java.awt.Color;
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    34
import java.awt.Graphics2D;
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    35
import java.awt.Rectangle;
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    36
import java.awt.image.BufferedImage;
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    37
import java.io.File;
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    38
import java.io.IOException;
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    39
import javax.imageio.ImageIO;
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    40
import javax.imageio.ImageReadParam;
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    41
import javax.imageio.ImageReader;
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    42
import javax.imageio.stream.ImageInputStream;
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    43
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    44
public class MultiReadTest {
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    45
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    46
    private static final File pwd = new File(".");
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    47
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    48
    private static final Color srcColor = Color.red;
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    49
    private static final int width = 200;
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    50
    private static final int height = 200;
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    51
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    52
    private static final int max = 5;
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    53
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    54
    public static void main(String[] args) throws IOException {
35693
5bdca98e8410 8146881: [TEST] update some imageio plugins tests to affect TIFF format
avstepan
parents: 26743
diff changeset
    55
        final String[] formats = { "bmp", "png", "gif", "jpg", "tif" };
26743
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    56
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    57
        for (String f : formats) {
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    58
            test(f);
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    59
        }
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    60
    }
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    61
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    62
    private static void test(String format) throws IOException {
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    63
        System.out.println("Format: " + format);
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    64
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    65
        BufferedImage src = createSrc();
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    66
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    67
        ImageInputStream iis = prepareInput(src, format);
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    68
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    69
        ImageReader reader = ImageIO.getImageReaders(iis).next();
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    70
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    71
        reader.setInput(iis);
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    72
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    73
        ImageReadParam p = reader.getDefaultReadParam();
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    74
        int cnt = 0;
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    75
        do {
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    76
            System.out.println("cnt: " + cnt);
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    77
            p.setSourceRegion(new Rectangle(width / 4, height / 4,
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    78
                                            width / 2, height / 2));
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    79
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    80
            BufferedImage dst = reader.read(0, p);
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    81
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    82
            final Color c = new Color(dst.getRGB(10, 10), true);
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    83
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    84
            if (!sameColor(c, srcColor)) {
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    85
                throw new RuntimeException(
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    86
                    String.format("Test failed: read color 0x%X\n",
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    87
                                  c.getRGB()));
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    88
            }
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    89
        } while (++cnt < max);
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    90
    }
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    91
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    92
    private static boolean sameColor(Color c1, Color c2) {
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    93
        final float delta = 0.1f;
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    94
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    95
        float[] rgb1 = new float[4];
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    96
        float[] rgb2 = new float[4];
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    97
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    98
        rgb1 = c1.getRGBComponents(rgb1);
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
    99
        rgb2 = c2.getRGBComponents(rgb2);
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   100
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   101
        for (int i = 0; i < rgb1.length; i++) {
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   102
            if (Math.abs(rgb1[i] - rgb2[i]) > delta) {
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   103
                return false;
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   104
            }
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   105
        }
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   106
        return true;
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   107
    }
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   108
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   109
    private static BufferedImage createSrc() {
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   110
        BufferedImage img = new BufferedImage(width, height,
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   111
                BufferedImage.TYPE_INT_RGB);
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   112
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   113
        Graphics2D g = img.createGraphics();
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   114
        g.setColor(srcColor);
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   115
        g.fillRect(0, 0, width, height);
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   116
        g.dispose();
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   117
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   118
        return img;
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   119
    }
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   120
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   121
    private static ImageInputStream prepareInput(BufferedImage src, String format) throws IOException {
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   122
        File f = File.createTempFile("src_", "." + format, pwd);
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   123
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   124
        if (ImageIO.write(src, format, f)) {
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   125
            return ImageIO.createImageInputStream(f);
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   126
        }
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   127
        return null;
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   128
    }
217c8f2c9cff 8041465: BMPImageReader read error using ImageReadParam with set sourceRectangle
bae
parents:
diff changeset
   129
}