Path: blob/master/test/jdk/javax/imageio/plugins/gif/TransparencyTest.java
41154 views
/*1* Copyright (c) 2005, 2017, 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 433941526* @summary Tests that GIF writer plugin is able to write images with BITMASK27* transparency28*/2930import java.awt.Color;31import java.awt.Graphics2D;32import java.awt.image.BufferedImage;33import java.awt.image.IndexColorModel;34import java.awt.image.WritableRaster;35import java.io.File;36import java.io.IOException;3738import javax.imageio.ImageIO;39import javax.imageio.ImageWriter;4041public class TransparencyTest {4243protected static final String fname = "ttest.gif";44protected BufferedImage src;45protected BufferedImage dst;4647public static void main(String[] args) {48System.out.println("Test indexed image...");49IndexColorModel icm = createIndexedBitmaskColorModel();50BufferedImage img = createIndexedImage(200, 200, icm);51TransparencyTest t = new TransparencyTest(img);5253try {54t.doTest();55} catch (Exception e) {56throw new RuntimeException("Test failed!", e);57}58System.out.println("Test passed.");59}6061protected TransparencyTest(BufferedImage src) {62this.src = src;63}6465protected void doTest() throws IOException {66int w = src.getWidth();67int h = src.getHeight();6869System.out.println("Write image...");70try {71ImageWriter writer =72ImageIO.getImageWritersByFormatName("GIF").next();73writer.setOutput(ImageIO.createImageOutputStream(new File(fname)));74writer.write(src);75} catch (Exception e) {76throw new RuntimeException("Test failed.", e);77}78System.out.println("Read image....");79dst = ImageIO.read(new File(fname));8081BufferedImage tmp = new BufferedImage(w, 2 * h,82BufferedImage.TYPE_INT_ARGB);83Graphics2D g = tmp.createGraphics();84g.setColor(Color.pink);85g.fillRect(0, 0, tmp.getWidth(), tmp.getHeight());8687g.drawImage(src, 0, 0, null);88g.drawImage(dst, 0, h, null);8990int width = w / 8;91int x = 5 * width + width / 2;92for (int y = 0; y < h; y++) {93int argb = tmp.getRGB(x, y);94if (Color.pink.getRGB() != argb) {95throw new RuntimeException("Bad color at " + x + "," + y +96" - " + Integer.toHexString(argb));97}98}99}100101protected static BufferedImage createIndexedImage(int w, int h,102IndexColorModel icm)103{104BufferedImage img = new BufferedImage(w, h,105BufferedImage.TYPE_BYTE_INDEXED,106icm);107108int mapSize = icm.getMapSize();109int width = w / mapSize;110111WritableRaster wr = img.getRaster();112for (int i = 0; i < mapSize; i++) {113for (int y = 0; y < h; y++) {114for (int x = 0; x < width; x++) {115wr.setSample(i * width + x, y, 0, i);116}117}118}119return img;120}121122protected static IndexColorModel createIndexedBitmaskColorModel() {123int paletteSize = 8;124byte[] red = new byte[paletteSize];125byte[] green = new byte[paletteSize];126byte[] blue = new byte[paletteSize];127128red[0] = (byte)0xff; green[0] = (byte)0x00; blue[0] = (byte)0x00;129red[1] = (byte)0x00; green[1] = (byte)0xff; blue[1] = (byte)0x00;130red[2] = (byte)0x00; green[2] = (byte)0x00; blue[2] = (byte)0xff;131red[3] = (byte)0xff; green[3] = (byte)0xff; blue[3] = (byte)0xff;132red[4] = (byte)0x00; green[4] = (byte)0x00; blue[4] = (byte)0x00;133red[5] = (byte)0x80; green[5] = (byte)0x80; blue[5] = (byte)0x80;134red[6] = (byte)0xff; green[6] = (byte)0xff; blue[6] = (byte)0x00;135red[7] = (byte)0x00; green[7] = (byte)0xff; blue[7] = (byte)0xff;136137int numBits = 3;138139IndexColorModel icm = new IndexColorModel(numBits, paletteSize,140red, green, blue, 5);141142return icm;143}144}145146147