1/* 2 * Copyright (c) 2005, 2017, 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 4339415 27 * @summary Tests that GIF writer plugin is able to write non-index images 28 */ 29 30import java.awt.Color; 31import java.awt.Graphics2D; 32import java.awt.Transparency; 33import java.awt.color.ColorSpace; 34import java.awt.image.BufferedImage; 35import java.awt.image.ComponentColorModel; 36import java.awt.image.DataBuffer; 37import java.awt.image.WritableRaster; 38import java.io.File; 39import java.util.Random; 40 41import javax.imageio.ImageIO; 42import javax.imageio.ImageWriter; 43 44public class IndexingTest { 45 46 protected static final String fname = "itest.gif"; 47 48 int w; 49 int h; 50 51 Random rnd; 52 53 public IndexingTest() { 54 w = h = 200; 55 rnd = new Random(); 56 } 57 58 public void doTest() { 59 ComponentColorModel ccm = createBitmaskColorModel(); 60 BufferedImage img = createComponentImage(w, h, ccm); 61 62 try { 63 ImageWriter w = ImageIO.getImageWritersByFormatName("GIF").next(); 64 w.setOutput(ImageIO.createImageOutputStream(new File(fname))); 65 w.write(img); 66 } catch (Exception e) { 67 throw new RuntimeException("Test failed.", e); 68 } 69 70 BufferedImage dst = null; 71 try { 72 dst = ImageIO.read(new File(fname)); 73 } catch (Exception e) { 74 throw new RuntimeException("Test failed.", e); 75 } 76 77 compareImages(img, dst); 78 79 System.out.println("Test passed."); 80 } 81 82 protected static ComponentColorModel createBitmaskColorModel() { 83 ComponentColorModel cm = 84 new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), 85 true, false, Transparency.BITMASK, 86 DataBuffer.TYPE_BYTE); 87 return cm; 88 } 89 90 protected static BufferedImage createComponentImage(int w, int h, 91 ComponentColorModel cm) 92 { 93 WritableRaster wr = cm.createCompatibleWritableRaster(w, h); 94 95 BufferedImage img = new BufferedImage(cm, wr, false, null); 96 Graphics2D g = img.createGraphics(); 97 int width = w / 8; 98 Color[] colors = new Color[8]; 99 colors[0] = Color.red; 100 colors[1] = Color.green; 101 colors[2] = Color.blue; 102 colors[3] = Color.white; 103 colors[4] = Color.black; 104 colors[5] = new Color(0x80, 0x80, 0x80, 0x00); 105 colors[6] = Color.yellow; 106 colors[7] = Color.cyan; 107 108 for (int i = 0; i < 8; i++) { 109 g.setColor(colors[i]); 110 g.fillRect(i * width, 0, width, h); 111 } 112 return img; 113 } 114 115 protected void compareImages(BufferedImage src, BufferedImage dst) { 116 int n = 10; 117 while (n-- > 0) { 118 int x = rnd.nextInt(w); 119 int y = rnd.nextInt(h); 120 121 int pSrc = src.getRGB(x, y); 122 int pDst = src.getRGB(x, y); 123 124 if (pSrc != pDst) { 125 throw new RuntimeException("Images are different"); 126 } 127 } 128 } 129 130 public static void main(String[] args) { 131 IndexingTest t = new IndexingTest(); 132 t.doTest(); 133 } 134} 135