Path: blob/master/test/jdk/javax/imageio/plugins/gif/AnimationTest.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 writes image sequences correctly27*/2829import java.awt.Color;30import java.awt.Graphics;31import java.awt.image.BufferedImage;32import java.awt.image.IndexColorModel;33import java.io.File;34import java.io.IOException;3536import javax.imageio.IIOImage;37import javax.imageio.ImageIO;38import javax.imageio.ImageReader;39import javax.imageio.ImageTypeSpecifier;40import javax.imageio.ImageWriteParam;41import javax.imageio.ImageWriter;42import javax.imageio.metadata.IIOMetadata;43import javax.imageio.stream.ImageOutputStream;4445public class AnimationTest {4647BufferedImage img = null;48int x, y;49int w, h;5051protected static String fname = "animtest.gif";5253public AnimationTest() {54w = h = 100;55}5657private void initFrame() {58if (img != null) {59return;60}61byte r[] = new byte[256];62byte g[] = new byte[256];63byte b[] = new byte[256];6465for (int i = 0; i < 256; i++) {66r[i] = g[i] = b[i] = (byte)0x00;67}68r[0] = (byte)0x00; g[0] = (byte)0x00; b[0] = (byte)0x00;69r[1] = (byte)0xFF; g[1] = (byte)0xFF; b[1] = (byte)0xFF;70r[2] = (byte)0xFF; g[3] = (byte)0xFF; b[4] = (byte)0xFF;7172IndexColorModel icm = new IndexColorModel(8, 256,73r, g, b);7475img = new BufferedImage(w, h,76BufferedImage.TYPE_BYTE_INDEXED,77icm);78}7980private BufferedImage createNextFrame() {81Graphics g = img.createGraphics();82g.setColor(Color.white);83g.fillRect(0, 0, w, h);8485g.setColor(Color.red);86g.drawLine(x, 0, x, h);8788g.setColor(Color.blue);89g.drawLine(0, y, w, y);9091x += 2;92y += 2;9394x %= w;95y %= h;9697return img;98}99100ImageWriter writer = null;101102private ImageWriter initWriter() throws IOException {103ImageOutputStream ios =104ImageIO.createImageOutputStream(new File(fname));105writer = ImageIO.getImageWritersByFormatName("GIF").next();106107writer.setOutput(ios);108109return writer;110}111112public static void main(String[] args) {113try {114AnimationTest t = new AnimationTest();115t.initFrame();116117ImageWriter w = t.initWriter();118119ImageWriteParam p = w.getDefaultWriteParam();120121IIOMetadata streamMetadata = w.getDefaultStreamMetadata(p);122123w.prepareWriteSequence(streamMetadata);124125for (int i = 0; i < 50; i++) {126BufferedImage f = t.createNextFrame();127128ImageTypeSpecifier type = new ImageTypeSpecifier(f);129130IIOMetadata m = w.getDefaultImageMetadata(type, p);131132w.writeToSequence(new IIOImage(f, null, m), p);133}134w.endWriteSequence();135136t.checkAnimation();137} catch (Exception e) {138throw new RuntimeException("Test failed.", e);139}140}141142protected void checkAnimation() throws IOException {143ImageReader r = ImageIO.getImageReadersByFormatName("GIF").next();144r.setInput(ImageIO.createImageInputStream(new File(fname)));145146int n = r.getNumImages(true);147for (int i = 0; i < n; i++) {148BufferedImage f = r.read(i);149checkFrame(i, f);150}151System.out.println("Test passed.");152}153154protected void checkFrame(int i, BufferedImage f) {155int x = 2 * i + 1;156for (int y = 0; y < h; y++) {157int argb = f.getRGB(x, y);158if (argb != 0xffffffff && !(argb == 0xff0000ff && y == 2 * i)) {159throw new RuntimeException("Test failed - bad frame");160}161argb = f.getRGB(y, x);162if (argb != 0xffffffff && !(argb == 0xffff0000 && y == 2 * i)) {163throw new RuntimeException("Test failed - bad frame");164}165}166}167}168169170