1 /*
   2  * Copyright (c) 2018, 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 4954348
  27  * @summary Checks whether JpegImageWriter returns -1 as recommended by the
  28  *          specification when getNumThumbnailsSupported method is invoked
  29  *          with insufficient data.
  30  * @run main JpegNumThumbnailsTest
  31  */
  32 import javax.imageio.ImageIO;
  33 import javax.imageio.ImageTypeSpecifier;
  34 import javax.imageio.ImageWriter;
  35 import javax.imageio.metadata.IIOMetadata;
  36 import java.awt.image.BufferedImage;
  37 import java.util.Iterator;
  38 import static java.awt.image.BufferedImage.TYPE_INT_RGB;
  39 
  40 public class JpegNumThumbnailsTest {
  41 
  42     public static void main(String args[]) {
  43         // Test variables.
  44         Iterator<ImageWriter> iterWriter = null;
  45         ImageWriter jpgWriter = null;
  46         IIOMetadata imageMetadata = null;
  47         BufferedImage testImage = null;
  48         ImageTypeSpecifier imageType = null;
  49         int numThumbnails = 0;
  50 
  51         iterWriter = ImageIO.getImageWritersByFormatName("JPEG");
  52         if (iterWriter.hasNext()) {
  53             // JpegImageWriter requires either image type or image metadata
  54             // to determine the number of thumbnails that could be supported.
  55             // Hence we test for all possible input combinations and observe
  56             // the result.
  57             jpgWriter = iterWriter.next();
  58             testImage = new BufferedImage(32, 32, TYPE_INT_RGB);
  59             imageType = ImageTypeSpecifier.createFromRenderedImage(testImage);
  60             imageMetadata = jpgWriter.getDefaultImageMetadata( imageType, null);
  61 
  62             // Observe the result with insufficient data.
  63             numThumbnails = jpgWriter.getNumThumbnailsSupported(null,
  64                                     null, null, null);
  65             if (numThumbnails != -1) {
  66                 throw new RuntimeException("Test Failed. Incorrect number of" +
  67                         " thumbnails returned.");
  68             }
  69 
  70             // Observe the result with valid image type.
  71             numThumbnails = jpgWriter.getNumThumbnailsSupported(imageType,
  72                                     null, null, null);
  73             if (numThumbnails != Integer.MAX_VALUE) {
  74                 throw new RuntimeException("Test Failed. Incorrect number of" +
  75                         " thumbnails returned.");
  76             }
  77 
  78             // Observe the result with valid image metadata.
  79             numThumbnails = jpgWriter.getNumThumbnailsSupported(null,
  80                                     null, null, imageMetadata);
  81             if (numThumbnails != Integer.MAX_VALUE) {
  82                 throw new RuntimeException("Test Failed. Incorrect number of" +
  83                         " thumbnails returned.");
  84             }
  85 
  86             // Observe the result with valid image type and metadata.
  87             numThumbnails = jpgWriter.getNumThumbnailsSupported(imageType,
  88                     null, null, imageMetadata);
  89             if (numThumbnails != Integer.MAX_VALUE) {
  90                 throw new RuntimeException("Test Failed. Incorrect number of" +
  91                         " thumbnails returned.");
  92             }
  93 
  94             // Dispose the writer
  95             jpgWriter.dispose();
  96         }
  97     }
  98 }