1/* 2 * Copyright (c) 2002, 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 4450894 27 * @summary Tests if the JPEG writer properly encodes IndexColorModel images 28 * that contain less than 8-bit indices (such as TYPE_BYTE_BINARY) 29 */ 30 31import java.awt.Color; 32import java.awt.Graphics; 33import java.awt.image.BufferedImage; 34import java.io.ByteArrayInputStream; 35import java.io.ByteArrayOutputStream; 36import java.io.IOException; 37 38import javax.imageio.ImageIO; 39 40public class ByteBinaryTest { 41 42 private static final int[] expectedVals = 43 { 0xffffffff, 0xff000000, 0xffffffff }; 44 45 public static void main(String[] args) { 46 BufferedImage bi = new BufferedImage(100, 100, 47 BufferedImage.TYPE_BYTE_BINARY); 48 49 Graphics g = bi.createGraphics(); 50 g.setColor(Color.white); 51 g.fillRect(0, 0, 100, 100); 52 g.setColor(Color.black); 53 g.fillRect(20, 20, 40, 40); 54 g.setColor(Color.white); 55 g.fillRect(25, 25, 25, 25); 56 g.dispose(); 57 58 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 59 boolean success; 60 61 try { 62 success = ImageIO.write(bi, "jpeg", baos); 63 } catch (IOException ioe) { 64 throw new RuntimeException("Could not write JPEG to stream"); 65 } 66 67 if (!success) { 68 throw new RuntimeException("Could not find valid JPEG writer..."); 69 } 70 71 byte[] bytearr = baos.toByteArray(); 72 ByteArrayInputStream bais = new ByteArrayInputStream(bytearr); 73 BufferedImage bi2 = null; 74 75 try { 76 bi2 = ImageIO.read(bais); 77 } catch (IOException ioe) { 78 throw new RuntimeException("Could not read JPEG stream"); 79 } 80 81 int[] actualVals = new int[3]; 82 83 actualVals[0] = bi2.getRGB(27, 5); 84 actualVals[1] = bi2.getRGB(27, 22); 85 actualVals[2] = bi2.getRGB(35, 35); 86 87 for (int i = 0; i < actualVals.length; i++) { 88 if (actualVals[i] != expectedVals[i]) { 89 throw new RuntimeException("Pixel mismatch at index: " + i); 90 } 91 } 92 } 93} 94