Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/imageio/plugins/gif/RGBAnimationTest.java
38918 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 632458126* @summary Test verifies that RGB images are written to animated GIF image use27* local color table if image palette is not equals to the global color28* table29*/3031import java.awt.Color;32import java.awt.Graphics2D;33import java.awt.image.BufferedImage;34import java.io.ByteArrayInputStream;35import java.io.ByteArrayOutputStream;36import java.io.File;37import java.io.FileOutputStream;38import java.io.IOException;39import java.util.ArrayList;4041import javax.imageio.IIOImage;42import javax.imageio.ImageIO;43import javax.imageio.ImageReader;44import javax.imageio.ImageTypeSpecifier;45import javax.imageio.ImageWriteParam;46import javax.imageio.ImageWriter;47import javax.imageio.metadata.IIOMetadata;48import javax.imageio.stream.ImageOutputStream;4950import com.sun.imageio.plugins.gif.GIFImageMetadata;5152public class RGBAnimationTest {53protected static String format = "GIF";54protected static boolean doSave = true;5556Frame[] frames;57ImageWriter writer;58ImageReader reader;5960public static void main(String[] args) throws IOException {61RGBAnimationTest test = new RGBAnimationTest();62test.doTest();63}64/** Creates a new instance of RGBAnimationTest */65public RGBAnimationTest() {66frames = new Frame[4];676869frames[0] = new Frame(new Color[] {Color.red, Color.green});70frames[1] = new Frame(new Color[] {Color.green, Color.cyan});71frames[2] = new Frame(new Color[] {Color.cyan, Color.yellow});72frames[3] = new Frame(new Color[] {Color.yellow, Color.red});7374writer = ImageIO.getImageWritersByFormatName(format).next();75reader = ImageIO.getImageReadersByFormatName(format).next();76}7778public void doTest() throws IOException {79ByteArrayOutputStream baos = new ByteArrayOutputStream();80writer.reset();81ImageOutputStream ios = ImageIO.createImageOutputStream(baos);82writer.setOutput(ios);8384ImageWriteParam wparam = prepareWriteParam();8586IIOMetadata streamMetadata = prepareStreamMetadata(wparam);8788writer.prepareWriteSequence(streamMetadata);8990for (int i = 0; i < frames.length; i++) {91BufferedImage src = frames[i].getImage();92IIOMetadata imageMetadata = prepareImageMetadata(i, src, wparam);93IIOImage img = new IIOImage(src, null, imageMetadata);9495writer.writeToSequence(img, wparam);96}97writer.endWriteSequence();98ios.flush();99ios.close();100101if (doSave) {102File f = File.createTempFile("wr_test_", "." + format, new File("."));103System.out.println("Save to file: " + f.getCanonicalPath());104FileOutputStream fos = new FileOutputStream(f);105fos.write(baos.toByteArray());106fos.flush();107fos.close();108}109// read result110reader.reset();111ByteArrayInputStream bais =112new ByteArrayInputStream(baos.toByteArray());113reader.setInput(ImageIO.createImageInputStream(bais));114115int minIndex = reader.getMinIndex();116int numImages = reader.getNumImages(true);117118for (int i = 0; i < numImages; i++) {119BufferedImage dst = reader.read(i + minIndex);120frames[i].checkResult(dst);121}122}123124protected IIOMetadata prepareImageMetadata(int i, BufferedImage img, ImageWriteParam wparam) {125GIFImageMetadata idata = (GIFImageMetadata)126writer.getDefaultImageMetadata(ImageTypeSpecifier.createFromRenderedImage(img), wparam);127128idata.delayTime = 100;129idata.disposalMethod = 0;130idata.transparentColorFlag = false;131132if (i == 0) {133ArrayList<byte[]> appIDs = new ArrayList<byte[]>();134appIDs.add(new String("NETSCAPE").getBytes());135ArrayList<byte[]> authCodes = new ArrayList<byte[]>();136authCodes.add(new String("2.0").getBytes());137ArrayList<byte[]> appData = new ArrayList<byte[]>();138byte[] authData = {1, 0, 0};139appData.add(authData);140141idata.applicationIDs = appIDs;142idata.authenticationCodes = authCodes;143idata.applicationData = appData;144}145return idata;146}147148protected ImageWriteParam prepareWriteParam() {149return writer.getDefaultWriteParam();150}151152protected IIOMetadata prepareStreamMetadata(ImageWriteParam wparam) {153return writer.getDefaultStreamMetadata(wparam);154}155156}157158class Frame {159protected static int type = BufferedImage.TYPE_INT_RGB;160protected static int dx = 100;161protected static int h = 100;162163protected Color[] colors;164protected BufferedImage img;165166public Frame(Color[] colors) {167this.colors = colors;168img = null;169}170171public BufferedImage getImage() {172if (img == null) {173img = new BufferedImage(dx * colors.length, h, type);174Graphics2D g = img.createGraphics();175for (int i = 0; i < colors.length; i++) {176g.setColor(colors[i]);177g.fillRect(dx * i, 0, dx, h);178}179}180return img;181}182183public void checkResult(BufferedImage dst) {184int y = h / 2;185int x = dx / 2;186for (int i = 0; i < colors.length; i++) {187188int srcRgb = img.getRGB(i * dx + x, y);189int dstRgb = dst.getRGB(i * dx + x, y);190191if (srcRgb != dstRgb) {192throw new RuntimeException("Test failed due to color difference: " +193Integer.toHexString(dstRgb) + " instead of " +194Integer.toHexString(srcRgb));195}196}197}198}199200201