Path: blob/master/test/jdk/javax/imageio/plugins/gif/RGBImageTest.java
41155 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 628657826* @summary Test verifies that RGB images does not convert to gray-scaled if27* default image metadata is used28*/2930import java.awt.Color;31import java.awt.Graphics;32import java.awt.image.BufferedImage;33import java.io.ByteArrayInputStream;34import java.io.ByteArrayOutputStream;35import java.io.IOException;3637import javax.imageio.IIOImage;38import javax.imageio.ImageIO;39import javax.imageio.ImageReader;40import javax.imageio.ImageTypeSpecifier;41import javax.imageio.ImageWriteParam;42import javax.imageio.ImageWriter;43import javax.imageio.metadata.IIOMetadata;44import javax.imageio.stream.ImageInputStream;45import javax.imageio.stream.ImageOutputStream;4647public class RGBImageTest {4849Color[] usedColors = {50Color.red, Color.green, Color.blue, Color.yellow,51Color.cyan, Color.magenta, Color.white, Color.black };5253BufferedImage src = null;54int dx= 20;55int height = 100;5657protected BufferedImage getSrc() {58if (src == null) {59src = new BufferedImage(dx * usedColors.length, height,60BufferedImage.TYPE_INT_RGB);61Graphics g = src.createGraphics();62for (int i = 0; i < usedColors.length; i++) {63g.setColor(usedColors[i]);64g.fillRect(dx * i, 0, dx, height);65}66}67return src;68}6970protected void doTest() throws IOException {71BufferedImage biSrc = getSrc();7273ImageWriter writer = ImageIO.getImageWritersByFormatName("GIF").next();74ByteArrayOutputStream baos = new ByteArrayOutputStream();75ImageOutputStream ios = ImageIO.createImageOutputStream(baos);76writer.setOutput(ios);7778ImageWriteParam writeParam = writer.getDefaultWriteParam();79IIOMetadata imageMetadata =80writer.getDefaultImageMetadata(new ImageTypeSpecifier(biSrc), writeParam);8182IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(writeParam);8384IIOImage iioImg = new IIOImage(biSrc, null, imageMetadata);85writer.write(streamMetadata, iioImg, writeParam);86ios.close();8788ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());89ImageInputStream iis = ImageIO.createImageInputStream(bais);90ImageReader reader = ImageIO.getImageReader(writer);91reader.setInput(iis);92BufferedImage dst = reader.read(0);9394// do test95int x = dx / 2;96int y = height / 2;9798for (int i = 0; i < usedColors.length; i++) {99int dstRgb = dst.getRGB(x, y);100System.out.println("dstColor: " + Integer.toHexString(dstRgb));101int srcRgb = usedColors[i].getRGB();102System.out.println("srcColor: " + Integer.toHexString(srcRgb));103if (dstRgb != srcRgb) {104throw new RuntimeException("wrong color " + i + ": " + Integer.toHexString(dstRgb));105}106x += dx;107}108109}110111public static void main(String[] args) throws IOException {112RGBImageTest t = new RGBImageTest();113t.doTest();114}115}116117118