test/jdk/javax/imageio/plugins/jpeg/UshortGrayTest.java
changeset 47216 71c04702a3d5
parent 45351 f7f615a3aaff
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     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 JPEGImageWriter allows images with > 8-bit samples to
       
    28  *          be written. Also tests the JPEGImageWriterSpi.canEncodeImage()
       
    29  *          mechanism for this same behavior.
       
    30  */
       
    31 
       
    32 import java.awt.image.BufferedImage;
       
    33 import java.io.ByteArrayOutputStream;
       
    34 import java.io.IOException;
       
    35 import java.util.Iterator;
       
    36 
       
    37 import javax.imageio.ImageIO;
       
    38 import javax.imageio.ImageTypeSpecifier;
       
    39 import javax.imageio.ImageWriter;
       
    40 import javax.imageio.stream.ImageOutputStream;
       
    41 
       
    42 public class UshortGrayTest {
       
    43 
       
    44     public static void main(String[] args) {
       
    45         Iterator iter;
       
    46         BufferedImage bi = new BufferedImage(10, 10,
       
    47                                              BufferedImage.TYPE_USHORT_GRAY);
       
    48 
       
    49         // Part 1: ensure that JPEGImageWriter throws an exception if it
       
    50         // encounters an image with 16-bit samples
       
    51         ImageWriter writer = null;
       
    52         iter = ImageIO.getImageWritersByFormatName("jpeg");
       
    53         if (iter.hasNext()) {
       
    54             writer = (ImageWriter)iter.next();
       
    55         } else {
       
    56             throw new RuntimeException("No JPEG reader found");
       
    57         }
       
    58 
       
    59         ByteArrayOutputStream baos = new ByteArrayOutputStream();
       
    60         ImageOutputStream ios = null;
       
    61         boolean exceptionThrown = false;
       
    62 
       
    63         try {
       
    64             ios = ImageIO.createImageOutputStream(baos);
       
    65         } catch (IOException ioe) {
       
    66             throw new RuntimeException("Could not create ImageOutputStream");
       
    67         }
       
    68 
       
    69         try {
       
    70             writer.setOutput(ios);
       
    71             writer.write(bi);
       
    72         } catch (IOException ioe) {
       
    73             exceptionThrown = true;
       
    74         }
       
    75 
       
    76         if (!exceptionThrown) {
       
    77             throw new RuntimeException("JPEG writer should not be able to " +
       
    78                                        "write USHORT_GRAY images");
       
    79         }
       
    80 
       
    81         // Part 2: ensure that JPEGImageWriterSpi.canEncodeImage() returns
       
    82         // false for images with 16-bit samples
       
    83         ImageTypeSpecifier its =
       
    84             ImageTypeSpecifier.createFromRenderedImage(bi);
       
    85 
       
    86         iter = ImageIO.getImageWriters(its, "jpeg");
       
    87         if (iter.hasNext()) {
       
    88             throw new RuntimeException("JPEG writer should not be available" +
       
    89                                        " for USHORT_GRAY images");
       
    90         }
       
    91     }
       
    92 }