Path: blob/master/test/jdk/javax/imageio/plugins/gif/IndexingTest.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 non-index images27*/2829import java.awt.Color;30import java.awt.Graphics2D;31import java.awt.Transparency;32import java.awt.color.ColorSpace;33import java.awt.image.BufferedImage;34import java.awt.image.ComponentColorModel;35import java.awt.image.DataBuffer;36import java.awt.image.WritableRaster;37import java.io.File;38import java.util.Random;3940import javax.imageio.ImageIO;41import javax.imageio.ImageWriter;4243public class IndexingTest {4445protected static final String fname = "itest.gif";4647int w;48int h;4950Random rnd;5152public IndexingTest() {53w = h = 200;54rnd = new Random();55}5657public void doTest() {58ComponentColorModel ccm = createBitmaskColorModel();59BufferedImage img = createComponentImage(w, h, ccm);6061try {62ImageWriter w = ImageIO.getImageWritersByFormatName("GIF").next();63w.setOutput(ImageIO.createImageOutputStream(new File(fname)));64w.write(img);65} catch (Exception e) {66throw new RuntimeException("Test failed.", e);67}6869BufferedImage dst = null;70try {71dst = ImageIO.read(new File(fname));72} catch (Exception e) {73throw new RuntimeException("Test failed.", e);74}7576compareImages(img, dst);7778System.out.println("Test passed.");79}8081protected static ComponentColorModel createBitmaskColorModel() {82ComponentColorModel cm =83new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),84true, false, Transparency.BITMASK,85DataBuffer.TYPE_BYTE);86return cm;87}8889protected static BufferedImage createComponentImage(int w, int h,90ComponentColorModel cm)91{92WritableRaster wr = cm.createCompatibleWritableRaster(w, h);9394BufferedImage img = new BufferedImage(cm, wr, false, null);95Graphics2D g = img.createGraphics();96int width = w / 8;97Color[] colors = new Color[8];98colors[0] = Color.red;99colors[1] = Color.green;100colors[2] = Color.blue;101colors[3] = Color.white;102colors[4] = Color.black;103colors[5] = new Color(0x80, 0x80, 0x80, 0x00);104colors[6] = Color.yellow;105colors[7] = Color.cyan;106107for (int i = 0; i < 8; i++) {108g.setColor(colors[i]);109g.fillRect(i * width, 0, width, h);110}111return img;112}113114protected void compareImages(BufferedImage src, BufferedImage dst) {115int n = 10;116while (n-- > 0) {117int x = rnd.nextInt(w);118int y = rnd.nextInt(h);119120int pSrc = src.getRGB(x, y);121int pDst = src.getRGB(x, y);122123if (pSrc != pDst) {124throw new RuntimeException("Images are different");125}126}127}128129public static void main(String[] args) {130IndexingTest t = new IndexingTest();131t.doTest();132}133}134135136