Path: blob/master/test/jdk/javax/imageio/plugins/jpeg/UshortGrayTest.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 JPEGImageWriter allows images with > 8-bit samples to27* be written. Also tests the JPEGImageWriterSpi.canEncodeImage()28* mechanism for this same behavior.29*/3031import java.awt.image.BufferedImage;32import java.io.ByteArrayOutputStream;33import java.io.IOException;34import java.util.Iterator;3536import javax.imageio.ImageIO;37import javax.imageio.ImageTypeSpecifier;38import javax.imageio.ImageWriter;39import javax.imageio.stream.ImageOutputStream;4041public class UshortGrayTest {4243public static void main(String[] args) {44Iterator iter;45BufferedImage bi = new BufferedImage(10, 10,46BufferedImage.TYPE_USHORT_GRAY);4748// Part 1: ensure that JPEGImageWriter throws an exception if it49// encounters an image with 16-bit samples50ImageWriter writer = null;51iter = ImageIO.getImageWritersByFormatName("jpeg");52if (iter.hasNext()) {53writer = (ImageWriter)iter.next();54} else {55throw new RuntimeException("No JPEG reader found");56}5758ByteArrayOutputStream baos = new ByteArrayOutputStream();59ImageOutputStream ios = null;60boolean exceptionThrown = false;6162try {63ios = ImageIO.createImageOutputStream(baos);64} catch (IOException ioe) {65throw new RuntimeException("Could not create ImageOutputStream");66}6768try {69writer.setOutput(ios);70writer.write(bi);71} catch (IOException ioe) {72exceptionThrown = true;73}7475if (!exceptionThrown) {76throw new RuntimeException("JPEG writer should not be able to " +77"write USHORT_GRAY images");78}7980// Part 2: ensure that JPEGImageWriterSpi.canEncodeImage() returns81// false for images with 16-bit samples82ImageTypeSpecifier its =83ImageTypeSpecifier.createFromRenderedImage(bi);8485iter = ImageIO.getImageWriters(its, "jpeg");86if (iter.hasNext()) {87throw new RuntimeException("JPEG writer should not be available" +88" for USHORT_GRAY images");89}90}91}929394