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 writes image sequences correctly 28 */ 29 30 import java.awt.Color; 31 import java.awt.Graphics; 32 import java.awt.image.BufferedImage; 33 import java.awt.image.IndexColorModel; 34 import java.io.File; 35 import java.io.IOException; 36 37 import javax.imageio.IIOImage; 38 import javax.imageio.ImageIO; 39 import javax.imageio.ImageReader; 40 import javax.imageio.ImageTypeSpecifier; 41 import javax.imageio.ImageWriteParam; 42 import javax.imageio.ImageWriter; 43 import javax.imageio.metadata.IIOMetadata; 44 import javax.imageio.stream.ImageOutputStream; 45 46 public class AnimationTest { 47 48 BufferedImage img = null; 49 int x, y; 50 int w, h; 51 52 protected static String fname = "animtest.gif"; 53 54 public AnimationTest() { 55 w = h = 100; 56 } 57 58 private void initFrame() { 59 if (img != null) { 60 return; 61 } 62 byte r[] = new byte[256]; 63 byte g[] = new byte[256]; 64 byte b[] = new byte[256]; 65 66 for (int i = 0; i < 256; i++) { 67 r[i] = g[i] = b[i] = (byte)0x00; 68 } 69 r[0] = (byte)0x00; g[0] = (byte)0x00; b[0] = (byte)0x00; 70 r[1] = (byte)0xFF; g[1] = (byte)0xFF; b[1] = (byte)0xFF; 71 r[2] = (byte)0xFF; g[3] = (byte)0xFF; b[4] = (byte)0xFF; 72 73 IndexColorModel icm = new IndexColorModel(8, 256, 74 r, g, b); 75 76 img = new BufferedImage(w, h, 77 BufferedImage.TYPE_BYTE_INDEXED, 78 icm); 79 } 80 81 private BufferedImage createNextFrame() { 82 Graphics g = img.createGraphics(); 83 g.setColor(Color.white); 84 g.fillRect(0, 0, w, h); 85 86 g.setColor(Color.red); 87 g.drawLine(x, 0, x, h); 88 89 g.setColor(Color.blue); 90 g.drawLine(0, y, w, y); 91 92 x += 2; 93 y += 2; 94 95 x %= w; 96 y %= h; 97 98 return img; 99 } 100 101 ImageWriter writer = null; 102 103 private ImageWriter initWriter() throws IOException { 104 ImageOutputStream ios = 105 ImageIO.createImageOutputStream(new File(fname)); 106 writer = ImageIO.getImageWritersByFormatName("GIF").next(); 107 108 writer.setOutput(ios); 109 110 return writer; 111 } 112 113 public static void main(String[] args) { 114 try { 115 AnimationTest t = new AnimationTest(); 116 t.initFrame(); 117 118 ImageWriter w = t.initWriter(); 119 120 ImageWriteParam p = w.getDefaultWriteParam(); 121 122 IIOMetadata streamMetadata = w.getDefaultStreamMetadata(p); 123 124 w.prepareWriteSequence(streamMetadata); 125 126 for (int i = 0; i < 50; i++) { 127 BufferedImage f = t.createNextFrame(); 128 129 ImageTypeSpecifier type = new ImageTypeSpecifier(f); 130 131 IIOMetadata m = w.getDefaultImageMetadata(type, p); 132 133 w.writeToSequence(new IIOImage(f, null, m), p); 134 } 135 w.endWriteSequence(); 136 137 t.checkAnimation(); 138 } catch (Exception e) { 139 throw new RuntimeException("Test failed.", e); 140 } 141 } 142 143 protected void checkAnimation() throws IOException { 144 ImageReader r = ImageIO.getImageReadersByFormatName("GIF").next(); 145 r.setInput(ImageIO.createImageInputStream(new File(fname))); 146 147 int n = r.getNumImages(true); 148 for (int i = 0; i < n; i++) { 149 BufferedImage f = r.read(i); 150 checkFrame(i, f); 151 } 152 System.out.println("Test passed."); 153 } 154 155 protected void checkFrame(int i, BufferedImage f) { 156 int x = 2 * i + 1; 157 for (int y = 0; y < h; y++) { 158 int argb = f.getRGB(x, y); 159 if (argb != 0xffffffff && !(argb == 0xff0000ff && y == 2 * i)) { 160 throw new RuntimeException("Test failed - bad frame"); 161 } 162 argb = f.getRGB(y, x); 163 if (argb != 0xffffffff && !(argb == 0xffff0000 && y == 2 * i)) { 164 throw new RuntimeException("Test failed - bad frame"); 165 } 166 } 167 } 168 }