Path: blob/master/test/jdk/javax/imageio/plugins/gif/LogicalScreenDimensionTest.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 630761826* @summary Test verifies that GIF image writer updates the dimension of the27* logical screen according to image dimension28* @modules java.desktop/com.sun.imageio.plugins.gif29*/3031import java.awt.Color;32import java.awt.Graphics2D;33import java.awt.image.BufferedImage;34import java.io.ByteArrayInputStream;35import java.io.ByteArrayOutputStream;36import java.io.IOException;3738import javax.imageio.IIOImage;39import javax.imageio.ImageIO;40import javax.imageio.ImageReader;41import javax.imageio.ImageTypeSpecifier;42import javax.imageio.ImageWriteParam;43import javax.imageio.ImageWriter;44import javax.imageio.metadata.IIOMetadata;45import javax.imageio.stream.ImageInputStream;46import javax.imageio.stream.ImageOutputStream;4748import com.sun.imageio.plugins.gif.GIFStreamMetadata;4950public class LogicalScreenDimensionTest {51public static void main(String[] args) throws IOException {52String format = "GIF";53ImageWriter writer =54ImageIO.getImageWritersByFormatName(format).next();55if (writer == null) {56throw new RuntimeException("No available writers for " + format);57}5859BufferedImage img = createTestImage(100, 100, BufferedImage.TYPE_BYTE_GRAY);6061ImageWriteParam p = writer.getDefaultWriteParam();62ImageTypeSpecifier type =63ImageTypeSpecifier.createFromRenderedImage(img);64IIOMetadata inImageMetadata =65writer.getDefaultImageMetadata(type, p);6667IIOMetadata inStreamMetadata = writer.getDefaultStreamMetadata(p);6869// write and read image70ByteArrayOutputStream baos = new ByteArrayOutputStream();7172ImageOutputStream ios = ImageIO.createImageOutputStream(baos);73writer.setOutput(ios);7475writer.write(inStreamMetadata, new IIOImage(img, null, inImageMetadata), p);7677ios.flush();78ios.close();7980// read result81ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());82ImageInputStream iis = ImageIO.createImageInputStream(bais);83ImageReader reader = ImageIO.getImageReader(writer);84reader.setInput(iis);8586IIOMetadata outStreamMetadata = reader.getStreamMetadata();8788GIFStreamMetadata gifStreamMetadata = (GIFStreamMetadata)outStreamMetadata;8990if (gifStreamMetadata.logicalScreenWidth != img.getWidth() ||91gifStreamMetadata.logicalScreenHeight != img.getHeight()) {92throw new RuntimeException("Test failed due to wrong logical screen dimension.");93}94}9596private static BufferedImage createTestImage(int w, int h, int type) {97BufferedImage res = new BufferedImage(w, h, type);98Graphics2D g = res.createGraphics();99g.setColor(Color.white);100g.fillRect(0, 0, w, h);101g.setColor(Color.black);102g.fillRect(w/4, h/4, w/2, h/2);103104105return res;106}107}108109110