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     8204187
  27  * @run     main TestWriteARGBJPEG
  28  * @summary verify JPEG Alpha support is as reported.
  29  */
  30 
  31 import java.io.IOException;
  32 import java.util.Iterator;
  33 import java.io.ByteArrayOutputStream;
  34 import java.awt.image.BufferedImage;
  35 import javax.imageio.ImageIO;
  36 import javax.imageio.ImageWriter;
  37 import javax.imageio.ImageTypeSpecifier;
  38 import javax.imageio.spi.ImageWriterSpi;
  39 import javax.imageio.stream.MemoryCacheImageOutputStream;
  40 
  41 public class TestWriteARGBJPEG {
  42     public static void main(String args[]) throws IOException {
  43 
  44         BufferedImage bi =
  45             new BufferedImage(10,10,BufferedImage.TYPE_INT_ARGB);
  46     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
  47 
  48     // There should be no exception from the next line
  49     // which internally should be relying on the canEncodeImage
  50     // method which we'll also test directly.
  51     boolean ret = ImageIO.write(bi, "jpeg", baos);
  52     System.out.println("ImageIO.write(..) returned " + ret);
  53 
  54     ImageTypeSpecifier its = new ImageTypeSpecifier(bi);
  55     Iterator<ImageWriter> writers = ImageIO.getImageWriters(its, "jpeg");
  56     boolean hasWriter = writers.hasNext();
  57     // If this can't write it, an exception will be thrown.
  58     if (writers.hasNext()) {
  59         System.out.println("A writer was found.");
  60         ImageWriter iw = writers.next();
  61         MemoryCacheImageOutputStream mos =
  62             new MemoryCacheImageOutputStream(baos);
  63         iw.setOutput(mos);
  64         iw.write(bi);
  65     }
  66 
  67     // Now Let's also ask the default JPEG writer's SPI if it
  68     // can write an ARGB image.
  69     ImageWriter iw = ImageIO.getImageWritersByFormatName("jpeg").next();
  70     ImageWriterSpi iwSpi = iw.getOriginatingProvider();
  71     boolean canEncode = iwSpi.canEncodeImage(bi);
  72     System.out.println("SPI canEncodeImage returned " + canEncode);
  73 
  74     // Now let's see if it is telling the truth.
  75     try {
  76         MemoryCacheImageOutputStream mos =
  77             new MemoryCacheImageOutputStream(baos);
  78         iw.setOutput(mos);
  79         iw.write(bi);
  80     } catch (IOException e) {
  81          if (canEncode) {
  82            throw e;
  83          }
  84     }
  85   }
  86 }