Path: blob/master/test/jdk/javax/imageio/plugins/jpeg/ByteBinaryTest.java
41152 views
/*1* Copyright (c) 2002, 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 445089426* @summary Tests if the JPEG writer properly encodes IndexColorModel images27* that contain less than 8-bit indices (such as TYPE_BYTE_BINARY)28*/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.ImageIO;3839public class ByteBinaryTest {4041private static final int[] expectedVals =42{ 0xffffffff, 0xff000000, 0xffffffff };4344public static void main(String[] args) {45BufferedImage bi = new BufferedImage(100, 100,46BufferedImage.TYPE_BYTE_BINARY);4748Graphics g = bi.createGraphics();49g.setColor(Color.white);50g.fillRect(0, 0, 100, 100);51g.setColor(Color.black);52g.fillRect(20, 20, 40, 40);53g.setColor(Color.white);54g.fillRect(25, 25, 25, 25);55g.dispose();5657ByteArrayOutputStream baos = new ByteArrayOutputStream();58boolean success;5960try {61success = ImageIO.write(bi, "jpeg", baos);62} catch (IOException ioe) {63throw new RuntimeException("Could not write JPEG to stream");64}6566if (!success) {67throw new RuntimeException("Could not find valid JPEG writer...");68}6970byte[] bytearr = baos.toByteArray();71ByteArrayInputStream bais = new ByteArrayInputStream(bytearr);72BufferedImage bi2 = null;7374try {75bi2 = ImageIO.read(bais);76} catch (IOException ioe) {77throw new RuntimeException("Could not read JPEG stream");78}7980int[] actualVals = new int[3];8182actualVals[0] = bi2.getRGB(27, 5);83actualVals[1] = bi2.getRGB(27, 22);84actualVals[2] = bi2.getRGB(35, 35);8586for (int i = 0; i < actualVals.length; i++) {87if (actualVals[i] != expectedVals[i]) {88throw new RuntimeException("Pixel mismatch at index: " + i);89}90}91}92}939495