Path: blob/master/test/jdk/javax/imageio/plugins/gif/OddPaletteTest.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 6275211 627662126* @summary Tests that GIF writer plugin is able to write indexed images if27* palette size is not a power of two28*/2930import java.awt.Color;31import java.awt.Graphics2D;32import java.awt.image.BufferedImage;33import java.awt.image.IndexColorModel;34import java.io.ByteArrayOutputStream;35import java.io.IOException;3637import javax.imageio.ImageIO;38import javax.imageio.ImageWriter;39import javax.imageio.stream.ImageOutputStream;4041public class OddPaletteTest {4243private static int w = 100;44private static int h = 100;4546public static void main(String[] args) {47BufferedImage[] srcs = new BufferedImage[2];48srcs[0] = createTestImage(7); // bug 627521149srcs[1] = createTestImage(1); // bug 62766215051for (int i = 0; i < srcs.length; i++) {52doTest(srcs[i]);53}54}5556private static void doTest(BufferedImage src) {57ImageWriter w = ImageIO.getImageWritersByFormatName("GIF").next();58if (w == null) {59throw new RuntimeException("No writer available!");60}6162try {63ByteArrayOutputStream baos = new ByteArrayOutputStream();64ImageOutputStream ios = ImageIO.createImageOutputStream(baos);65w.setOutput(ios);66w.write(src);67} catch (IOException e) {68throw new RuntimeException("Test failed.", e);69} catch (IllegalArgumentException e) {70throw new RuntimeException("Test failed.", e);71}72}7374private static BufferedImage createTestImage(int paletteSize) {75byte[] r = new byte[paletteSize];76byte[] g = new byte[paletteSize];77byte[] b = new byte[paletteSize];7879int shift = 256 / paletteSize;80for (int i = 0; i < paletteSize; i++) {81r[i] = g[i] = b[i] = (byte)(shift * i);82}8384int numBits = getNumBits(paletteSize);8586System.out.println("num of bits " + numBits);8788IndexColorModel icm =89new IndexColorModel(numBits, paletteSize, r, g, b);9091BufferedImage img = new BufferedImage(w, h,92BufferedImage.TYPE_BYTE_INDEXED,93icm);94Graphics2D g2d = img.createGraphics();95g2d.setColor(Color.white);96g2d.fillRect(0, 0, w, h);97g2d.setColor(Color.black);98g2d.drawLine(0, 0, w, h);99g2d.drawLine(0, h, w, 0);100101return img;102}103104private static int getNumBits(int paletteSize) {105if (paletteSize < 0) {106throw new IllegalArgumentException("negative palette size: " +107paletteSize);108}109if (paletteSize < 2) {110return 1;111}112int numBits = 0;113114paletteSize--;115116while (paletteSize > 0) {117numBits++;118paletteSize = paletteSize >> 1;119}120return numBits;121}122}123124125