Path: blob/master/test/jdk/javax/imageio/plugins/gif/GifTransparencyTest.java
41154 views
/*1* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6276653 628793626*27* @summary Test verifes that Image I/O gif writer correctly handles28* image what supports tranclucent transparency type but contains29* picture with opaque or bitmask transparecy (i.e. each image pixel30* is ether opaque or fully transparent).31*32* @run main GifTransparencyTest33*/343536import java.awt.BorderLayout;37import java.awt.Color;38import java.awt.Dimension;39import java.awt.Graphics;40import java.awt.Graphics2D;41import java.awt.geom.Area;42import java.awt.geom.RoundRectangle2D;43import java.awt.image.BufferedImage;44import java.io.File;45import java.io.IOException;46import javax.imageio.ImageIO;47import javax.imageio.ImageWriter;48import javax.imageio.spi.ImageWriterSpi;49import javax.swing.JComponent;50import javax.swing.JFrame;51import javax.swing.JPanel;525354public class GifTransparencyTest {5556BufferedImage src;57BufferedImage dst;5859public GifTransparencyTest() {60src = createTestImage();61}6263public void doTest() {64File pwd = new File(".");65try {66File f = File.createTempFile("transparency_test_", ".gif", pwd);67System.out.println("file: " + f.getCanonicalPath());6869ImageWriter w = ImageIO.getImageWritersByFormatName("GIF").next();7071ImageWriterSpi spi = w.getOriginatingProvider();7273boolean succeed_write = ImageIO.write(src, "gif", f);7475if (!succeed_write) {76throw new RuntimeException("Test failed: failed to write src.");77}7879dst = ImageIO.read(f);8081checkResult(src, dst);8283} catch (IOException e) {84throw new RuntimeException("Test failed.", e);85}86}8788/*89* Failure criteria:90* - src and dst have different dimension91* - any transparent pixel was lost92*/93protected void checkResult(BufferedImage src, BufferedImage dst) {94int w = src.getWidth();95int h = src.getHeight();969798if (dst.getWidth() != w || dst.getHeight() != h) {99throw new RuntimeException("Test failed: wrong result dimension");100}101102BufferedImage bg = new BufferedImage(2 * w, h, BufferedImage.TYPE_INT_RGB);103Graphics g = bg.createGraphics();104g.setColor(Color.white);105g.fillRect(0, 0, 2 * w, h);106107g.drawImage(src, 0, 0, null);108g.drawImage(dst, w, 0, null);109110g.dispose();111112for (int y = 0; y < h; y++) {113for (int x = 0; x < w; x++) {114int src_rgb = bg.getRGB(x, y);115int dst_rgb = bg.getRGB(x + w, y);116117if (dst_rgb != src_rgb) {118throw new RuntimeException("Test failed: wrong color " +119Integer.toHexString(dst_rgb) + " at " + x + ", " +120y + " (instead of " + Integer.toHexString(src_rgb) +121")");122}123}124}125System.out.println("Test passed.");126}127128public void show() {129JPanel p = new JPanel(new BorderLayout()) {130public void paintComponent(Graphics g) {131g.setColor(Color.blue);132g.fillRect(0, 0, getWidth(), getHeight());133}134};135p.add(new ImageComponent(src), BorderLayout.WEST);136if (dst != null) {137p.add(new ImageComponent(dst), BorderLayout.EAST);138}139140JFrame f = new JFrame("Transparency");141f.add(p);142143f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);144f.pack();145f.setVisible(true);146}147148public static class ImageComponent extends JComponent {149BufferedImage img;150151public ImageComponent(BufferedImage img) {152this.img = img;153}154155public Dimension getPreferredSize() {156return new Dimension(img.getWidth() + 2, img.getHeight() + 2);157}158159public void paintComponent(Graphics g) {160g.drawImage(img, 1, 1, this);161}162}163164protected BufferedImage createTestImage() {165BufferedImage img = new BufferedImage(200, 200,166BufferedImage.TYPE_INT_ARGB);167Graphics g = img.createGraphics();168169g.setColor(Color.red);170g.fillRect(50, 50, 100, 100);171g.dispose();172173return img;174}175176public static class Empty extends GifTransparencyTest {177protected BufferedImage createTestImage() {178return new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);179}180}181182public static class Opaque extends GifTransparencyTest {183protected BufferedImage createTestImage() {184BufferedImage img = new BufferedImage(200, 200,185BufferedImage.TYPE_INT_ARGB);186Graphics g = img.createGraphics();187g.setColor(Color.cyan);188g.fillRect(0, 0, 200, 200);189190g.setColor(Color.red);191g.fillRect(50, 50, 100, 100);192g.dispose();193194return img;195}196}197198public static void main(String[] args) {199System.out.println("Test bitmask...");200new GifTransparencyTest().doTest();201202System.out.println("Test opaque...");203new GifTransparencyTest.Opaque().doTest();204205System.out.println("Test empty...");206new GifTransparencyTest.Empty().doTest();207}208}209210211