0N/A/*
0N/A * @test
0N/A * @bug 6276653 6287936
0N/A *
0N/A * @summary Test verifes that Image I/O gif writer correctly handles
0N/A * image what supports tranclucent transparency type but contains
0N/A * picture with opaque or bitmask transparecy (i.e. each image pixel
0N/A * is ether opaque or fully transparent).
0N/A *
0N/A * @run main GifTransparencyTest
0N/A */
0N/A
0N/A
0N/Aimport java.awt.BorderLayout;
0N/Aimport java.awt.Color;
0N/Aimport java.awt.Dimension;
0N/Aimport java.awt.Graphics;
0N/Aimport java.awt.Graphics2D;
0N/Aimport java.awt.geom.Area;
0N/Aimport java.awt.geom.RoundRectangle2D;
0N/Aimport java.awt.image.BufferedImage;
0N/Aimport java.io.File;
0N/Aimport java.io.IOException;
0N/Aimport javax.imageio.ImageIO;
0N/Aimport javax.imageio.ImageWriter;
0N/Aimport javax.imageio.spi.ImageWriterSpi;
0N/Aimport javax.swing.JComponent;
0N/Aimport javax.swing.JFrame;
0N/Aimport javax.swing.JPanel;
0N/A
0N/A
0N/Apublic class GifTransparencyTest {
0N/A
0N/A BufferedImage src;
0N/A BufferedImage dst;
0N/A
0N/A public GifTransparencyTest() {
0N/A src = createTestImage();
0N/A }
0N/A
0N/A public void doTest() {
0N/A File pwd = new File(".");
0N/A try {
0N/A File f = File.createTempFile("transparency_test_", ".gif", pwd);
0N/A System.out.println("file: " + f.getCanonicalPath());
0N/A
0N/A ImageWriter w = ImageIO.getImageWritersByFormatName("GIF").next();
0N/A
0N/A ImageWriterSpi spi = w.getOriginatingProvider();
0N/A
0N/A boolean succeed_write = ImageIO.write(src, "gif", f);
0N/A
0N/A if (!succeed_write) {
0N/A throw new RuntimeException("Test failed: failed to write src.");
0N/A }
0N/A
0N/A dst = ImageIO.read(f);
0N/A
0N/A checkResult(src, dst);
0N/A
0N/A } catch (IOException e) {
0N/A throw new RuntimeException("Test failed.", e);
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Failure criteria:
0N/A * - src and dst have different dimension
0N/A * - any transparent pixel was lost
0N/A */
0N/A protected void checkResult(BufferedImage src, BufferedImage dst) {
0N/A int w = src.getWidth();
0N/A int h = src.getHeight();
0N/A
0N/A
0N/A if (dst.getWidth() != w || dst.getHeight() != h) {
0N/A throw new RuntimeException("Test failed: wrong result dimension");
0N/A }
0N/A
0N/A BufferedImage bg = new BufferedImage(2 * w, h, BufferedImage.TYPE_INT_RGB);
0N/A Graphics g = bg.createGraphics();
0N/A g.setColor(Color.white);
0N/A g.fillRect(0, 0, 2 * w, h);
0N/A
0N/A g.drawImage(src, 0, 0, null);
0N/A g.drawImage(dst, w, 0, null);
0N/A
0N/A g.dispose();
0N/A
0N/A for (int y = 0; y < h; y++) {
0N/A for (int x = 0; x < w; x++) {
0N/A int src_rgb = bg.getRGB(x, y);
0N/A int dst_rgb = bg.getRGB(x + w, y);
0N/A
0N/A if (dst_rgb != src_rgb) {
0N/A throw new RuntimeException("Test failed: wrong color " +
0N/A Integer.toHexString(dst_rgb) + " at " + x + ", " +
0N/A y + " (instead of " + Integer.toHexString(src_rgb) +
0N/A ")");
0N/A }
0N/A }
0N/A }
0N/A System.out.println("Test passed.");
0N/A }
0N/A
0N/A public void show() {
0N/A JPanel p = new JPanel(new BorderLayout()) {
0N/A public void paintComponent(Graphics g) {
0N/A g.setColor(Color.blue);
0N/A g.fillRect(0, 0, getWidth(), getHeight());
0N/A }
0N/A };
0N/A p.add(new ImageComponent(src), BorderLayout.WEST);
0N/A if (dst != null) {
0N/A p.add(new ImageComponent(dst), BorderLayout.EAST);
0N/A }
0N/A
0N/A JFrame f = new JFrame("Transparency");
0N/A f.add(p);
0N/A
0N/A f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
0N/A f.pack();
0N/A f.setVisible(true);
0N/A }
0N/A
0N/A public static class ImageComponent extends JComponent {
0N/A BufferedImage img;
0N/A
0N/A public ImageComponent(BufferedImage img) {
0N/A this.img = img;
0N/A }
0N/A
0N/A public Dimension getPreferredSize() {
0N/A return new Dimension(img.getWidth() + 2, img.getHeight() + 2);
0N/A }
0N/A
0N/A public void paintComponent(Graphics g) {
0N/A g.drawImage(img, 1, 1, this);
0N/A }
0N/A }
0N/A
0N/A protected BufferedImage createTestImage() {
0N/A BufferedImage img = new BufferedImage(200, 200,
0N/A BufferedImage.TYPE_INT_ARGB);
0N/A Graphics g = img.createGraphics();
0N/A
0N/A g.setColor(Color.red);
0N/A g.fillRect(50, 50, 100, 100);
0N/A g.dispose();
0N/A
0N/A return img;
0N/A }
0N/A
0N/A public static class Empty extends GifTransparencyTest {
0N/A protected BufferedImage createTestImage() {
0N/A return new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
0N/A }
0N/A }
0N/A
0N/A public static class Opaque extends GifTransparencyTest {
0N/A protected BufferedImage createTestImage() {
0N/A BufferedImage img = new BufferedImage(200, 200,
0N/A BufferedImage.TYPE_INT_ARGB);
0N/A Graphics g = img.createGraphics();
0N/A g.setColor(Color.cyan);
0N/A g.fillRect(0, 0, 200, 200);
0N/A
0N/A g.setColor(Color.red);
0N/A g.fillRect(50, 50, 100, 100);
0N/A g.dispose();
0N/A
0N/A return img;
0N/A }
0N/A }
0N/A
0N/A public static void main(String[] args) {
0N/A System.out.println("Test bitmask...");
0N/A new GifTransparencyTest().doTest();
0N/A
0N/A System.out.println("Test opaque...");
0N/A new GifTransparencyTest.Opaque().doTest();
0N/A
0N/A System.out.println("Test empty...");
0N/A new GifTransparencyTest.Empty().doTest();
0N/A }
0N/A}