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