1/* 2 * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * @test 26 * @bug 4826548 27 * @summary Tests for reading PNG images with 5,6,7 and 8 colors in palette 28 */ 29 30import java.awt.image.BufferedImage; 31import java.awt.image.IndexColorModel; 32import java.io.ByteArrayInputStream; 33import java.io.ByteArrayOutputStream; 34import java.io.IOException; 35 36import javax.imageio.ImageIO; 37 38public class ShortPaletteTest { 39 40 public static void main(String[] args) { 41 42 for (int numberColors = 2; numberColors <= 16; numberColors++) { 43 try { 44 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 45 BufferedImage image = createImage(numberColors); 46 ImageIO.write(image, "png", baos); 47 baos.close(); 48 System.out.println("Number of colors: " + numberColors); 49 byte[] buffer = baos.toByteArray(); 50 ByteArrayInputStream bais = new ByteArrayInputStream(buffer); 51 ImageIO.read(bais); 52 System.out.println("OK"); 53 } catch (ArrayIndexOutOfBoundsException e) { 54 e.printStackTrace(); 55 throw new RuntimeException("Test failed."); 56 } catch (IOException e) { 57 e.printStackTrace(); 58 throw new RuntimeException("Unexpected exception was thrown." 59 + " Test failed."); 60 } 61 } 62 } 63 64 private static IndexColorModel createColorModel(int numberColors) { 65 66 byte[] colors = new byte[numberColors*3]; 67 int depth = 4; 68 int startIndex = 0; 69 70 return new IndexColorModel(depth, 71 numberColors, 72 colors, 73 startIndex, 74 false); 75 } 76 77 private static BufferedImage createImage(int numberColors) { 78 return new BufferedImage(32, 79 32, 80 BufferedImage.TYPE_BYTE_BINARY, 81 createColorModel(numberColors)); 82 } 83} 84