Path: blob/master/test/jdk/javax/imageio/plugins/gif/RGBAnimationTest.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 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* @modules java.desktop/com.sun.imageio.plugins.gif30*/3132import java.awt.Color;33import java.awt.Graphics2D;34import java.awt.image.BufferedImage;35import java.io.ByteArrayInputStream;36import java.io.ByteArrayOutputStream;37import java.io.File;38import java.io.FileOutputStream;39import java.io.IOException;40import java.util.ArrayList;4142import javax.imageio.IIOImage;43import javax.imageio.ImageIO;44import javax.imageio.ImageReader;45import javax.imageio.ImageTypeSpecifier;46import javax.imageio.ImageWriteParam;47import javax.imageio.ImageWriter;48import javax.imageio.metadata.IIOMetadata;49import javax.imageio.stream.ImageOutputStream;5051import com.sun.imageio.plugins.gif.GIFImageMetadata;5253public class RGBAnimationTest {54protected static String format = "GIF";55protected static boolean doSave = true;5657Frame[] frames;58ImageWriter writer;59ImageReader reader;6061public static void main(String[] args) throws IOException {62RGBAnimationTest test = new RGBAnimationTest();63test.doTest();64}65/** Creates a new instance of RGBAnimationTest */66public RGBAnimationTest() {67frames = new Frame[4];686970frames[0] = new Frame(new Color[] {Color.red, Color.green});71frames[1] = new Frame(new Color[] {Color.green, Color.cyan});72frames[2] = new Frame(new Color[] {Color.cyan, Color.yellow});73frames[3] = new Frame(new Color[] {Color.yellow, Color.red});7475writer = ImageIO.getImageWritersByFormatName(format).next();76reader = ImageIO.getImageReadersByFormatName(format).next();77}7879public void doTest() throws IOException {80ByteArrayOutputStream baos = new ByteArrayOutputStream();81writer.reset();82ImageOutputStream ios = ImageIO.createImageOutputStream(baos);83writer.setOutput(ios);8485ImageWriteParam wparam = prepareWriteParam();8687IIOMetadata streamMetadata = prepareStreamMetadata(wparam);8889writer.prepareWriteSequence(streamMetadata);9091for (int i = 0; i < frames.length; i++) {92BufferedImage src = frames[i].getImage();93IIOMetadata imageMetadata = prepareImageMetadata(i, src, wparam);94IIOImage img = new IIOImage(src, null, imageMetadata);9596writer.writeToSequence(img, wparam);97}98writer.endWriteSequence();99ios.flush();100ios.close();101102if (doSave) {103File f = File.createTempFile("wr_test_", "." + format, new File("."));104System.out.println("Save to file: " + f.getCanonicalPath());105FileOutputStream fos = new FileOutputStream(f);106fos.write(baos.toByteArray());107fos.flush();108fos.close();109}110// read result111reader.reset();112ByteArrayInputStream bais =113new ByteArrayInputStream(baos.toByteArray());114reader.setInput(ImageIO.createImageInputStream(bais));115116int minIndex = reader.getMinIndex();117int numImages = reader.getNumImages(true);118119for (int i = 0; i < numImages; i++) {120BufferedImage dst = reader.read(i + minIndex);121frames[i].checkResult(dst);122}123}124125protected IIOMetadata prepareImageMetadata(int i, BufferedImage img, ImageWriteParam wparam) {126GIFImageMetadata idata = (GIFImageMetadata)127writer.getDefaultImageMetadata(ImageTypeSpecifier.createFromRenderedImage(img), wparam);128129idata.delayTime = 100;130idata.disposalMethod = 0;131idata.transparentColorFlag = false;132133if (i == 0) {134ArrayList<byte[]> appIDs = new ArrayList<byte[]>();135appIDs.add(new String("NETSCAPE").getBytes());136ArrayList<byte[]> authCodes = new ArrayList<byte[]>();137authCodes.add(new String("2.0").getBytes());138ArrayList<byte[]> appData = new ArrayList<byte[]>();139byte[] authData = {1, 0, 0};140appData.add(authData);141142idata.applicationIDs = appIDs;143idata.authenticationCodes = authCodes;144idata.applicationData = appData;145}146return idata;147}148149protected ImageWriteParam prepareWriteParam() {150return writer.getDefaultWriteParam();151}152153protected IIOMetadata prepareStreamMetadata(ImageWriteParam wparam) {154return writer.getDefaultStreamMetadata(wparam);155}156157}158159class Frame {160protected static int type = BufferedImage.TYPE_INT_RGB;161protected static int dx = 100;162protected static int h = 100;163164protected Color[] colors;165protected BufferedImage img;166167public Frame(Color[] colors) {168this.colors = colors;169img = null;170}171172public BufferedImage getImage() {173if (img == null) {174img = new BufferedImage(dx * colors.length, h, type);175Graphics2D g = img.createGraphics();176for (int i = 0; i < colors.length; i++) {177g.setColor(colors[i]);178g.fillRect(dx * i, 0, dx, h);179}180}181return img;182}183184public void checkResult(BufferedImage dst) {185int y = h / 2;186int x = dx / 2;187for (int i = 0; i < colors.length; i++) {188189int srcRgb = img.getRGB(i * dx + x, y);190int dstRgb = dst.getRGB(i * dx + x, y);191192if (srcRgb != dstRgb) {193throw new RuntimeException("Test failed due to color difference: " +194Integer.toHexString(dstRgb) + " instead of " +195Integer.toHexString(srcRgb));196}197}198}199}200201202