jdk/test/javax/imageio/plugins/gif/GifTransparencyTest.java
author serb
Tue, 12 Nov 2013 20:24:25 +0400
changeset 21596 0e3a39f29dbc
parent 2 90ce3da70b43
child 23010 6dadb192ad81
permissions -rw-r--r--
8027696: Incorrect copyright header in the tests Reviewed-by: alanb, malenkov, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21596
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
     1
/*
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
     2
 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
     4
 *
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
     8
 *
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    13
 * accompanied this code).
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    14
 *
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    18
 *
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    21
 * questions.
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    22
 */
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 2
diff changeset
    23
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug     6276653 6287936
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary Test verifes that Image I/O gif writer correctly handles
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *          image what supports tranclucent transparency type but contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *          picture with opaque or bitmask transparecy (i.e. each image pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *          is ether opaque or fully transparent).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @run     main GifTransparencyTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.BorderLayout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.Dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.Graphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.awt.Graphics2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.awt.geom.Area;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.geom.RoundRectangle2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.imageio.ImageIO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.imageio.ImageWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.imageio.spi.ImageWriterSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.swing.JComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.swing.JFrame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.swing.JPanel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class GifTransparencyTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    BufferedImage src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    BufferedImage dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public GifTransparencyTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        src = createTestImage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public void doTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        File pwd = new File(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            File f = File.createTempFile("transparency_test_", ".gif", pwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            System.out.println("file: " + f.getCanonicalPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            ImageWriter w = ImageIO.getImageWritersByFormatName("GIF").next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            ImageWriterSpi spi = w.getOriginatingProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            boolean succeed_write = ImageIO.write(src, "gif", f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            if (!succeed_write) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                throw new RuntimeException("Test failed: failed to write src.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            dst = ImageIO.read(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            checkResult(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            throw new RuntimeException("Test failed.", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Failure criteria:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *  - src and dst have different dimension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *  - any transparent pixel was lost
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    protected void checkResult(BufferedImage src, BufferedImage dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        int w = src.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        int h = src.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (dst.getWidth() != w || dst.getHeight() != h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new RuntimeException("Test failed: wrong result dimension");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        BufferedImage bg = new BufferedImage(2 * w, h, BufferedImage.TYPE_INT_RGB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        Graphics g = bg.createGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        g.setColor(Color.white);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        g.fillRect(0, 0, 2 * w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        g.drawImage(src, 0, 0, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        g.drawImage(dst, w, 0, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        for (int y = 0; y < h; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            for (int x = 0; x < w; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                int src_rgb = bg.getRGB(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                int dst_rgb = bg.getRGB(x + w, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                if (dst_rgb != src_rgb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    throw new RuntimeException("Test failed: wrong color " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                            Integer.toHexString(dst_rgb) + " at " + x + ", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                            y + " (instead of " + Integer.toHexString(src_rgb) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                            ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        System.out.println("Test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public void show() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        JPanel p = new JPanel(new BorderLayout()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            public void paintComponent(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                g.setColor(Color.blue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                g.fillRect(0, 0, getWidth(), getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        p.add(new ImageComponent(src), BorderLayout.WEST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (dst != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        p.add(new ImageComponent(dst), BorderLayout.EAST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        JFrame f = new JFrame("Transparency");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        f.add(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        f.pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        f.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public static class ImageComponent extends JComponent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        BufferedImage img;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        public ImageComponent(BufferedImage img) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            this.img = img;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        public Dimension getPreferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            return new Dimension(img.getWidth() + 2, img.getHeight() + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        public void paintComponent(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            g.drawImage(img, 1, 1, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    protected BufferedImage createTestImage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        BufferedImage img = new BufferedImage(200, 200,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                                              BufferedImage.TYPE_INT_ARGB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        Graphics g = img.createGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        g.setColor(Color.red);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        g.fillRect(50, 50, 100, 100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return img;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public static class Empty extends GifTransparencyTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        protected BufferedImage createTestImage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            return new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public static class Opaque extends GifTransparencyTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        protected BufferedImage createTestImage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            BufferedImage img = new BufferedImage(200, 200,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                                  BufferedImage.TYPE_INT_ARGB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            Graphics g = img.createGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            g.setColor(Color.cyan);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            g.fillRect(0, 0, 200, 200);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            g.setColor(Color.red);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            g.fillRect(50, 50, 100, 100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            return img;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        System.out.println("Test bitmask...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        new GifTransparencyTest().doTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        System.out.println("Test opaque...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        new GifTransparencyTest.Opaque().doTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        System.out.println("Test empty...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        new GifTransparencyTest.Empty().doTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
}