Path: blob/master/test/jdk/javax/imageio/plugins/jpeg/TestWriteARGBJPEG.java
41152 views
/*1* Copyright (c) 2018, 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 820418726* @run main TestWriteARGBJPEG27* @summary verify JPEG Alpha support is as reported.28*/2930import java.io.IOException;31import java.util.Iterator;32import java.io.ByteArrayOutputStream;33import java.awt.image.BufferedImage;34import javax.imageio.ImageIO;35import javax.imageio.ImageWriter;36import javax.imageio.ImageTypeSpecifier;37import javax.imageio.spi.ImageWriterSpi;38import javax.imageio.stream.MemoryCacheImageOutputStream;3940public class TestWriteARGBJPEG {41public static void main(String args[]) throws IOException {4243BufferedImage bi =44new BufferedImage(10,10,BufferedImage.TYPE_INT_ARGB);45ByteArrayOutputStream baos = new ByteArrayOutputStream();4647// There should be no exception from the next line48// which internally should be relying on the canEncodeImage49// method which we'll also test directly.50boolean ret = ImageIO.write(bi, "jpeg", baos);51System.out.println("ImageIO.write(..) returned " + ret);5253ImageTypeSpecifier its = new ImageTypeSpecifier(bi);54Iterator<ImageWriter> writers = ImageIO.getImageWriters(its, "jpeg");55boolean hasWriter = writers.hasNext();56// If this can't write it, an exception will be thrown.57if (writers.hasNext()) {58System.out.println("A writer was found.");59ImageWriter iw = writers.next();60MemoryCacheImageOutputStream mos =61new MemoryCacheImageOutputStream(baos);62iw.setOutput(mos);63iw.write(bi);64}6566// Now Let's also ask the default JPEG writer's SPI if it67// can write an ARGB image.68ImageWriter iw = ImageIO.getImageWritersByFormatName("jpeg").next();69ImageWriterSpi iwSpi = iw.getOriginatingProvider();70boolean canEncode = iwSpi.canEncodeImage(bi);71System.out.println("SPI canEncodeImage returned " + canEncode);7273// Now let's see if it is telling the truth.74try {75MemoryCacheImageOutputStream mos =76new MemoryCacheImageOutputStream(baos);77iw.setOutput(mos);78iw.write(bi);79} catch (IOException e) {80if (canEncode) {81throw e;82}83}84}85}868788