Path: blob/master/test/jdk/javax/imageio/plugins/png/WriteProgressive.java
41155 views
/*1* Copyright (c) 2001, 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 443261526* @summary Tests progressive writing in the PNG encoder27* @modules java.desktop/com.sun.imageio.plugins.png28*/2930import java.awt.Color;31import java.awt.Graphics2D;32import java.awt.image.BufferedImage;33import java.io.File;34import java.io.IOException;35import java.util.Iterator;36import java.util.Random;3738import javax.imageio.IIOImage;39import javax.imageio.ImageIO;40import javax.imageio.ImageWriteParam;41import javax.imageio.ImageWriter;42import javax.imageio.stream.ImageOutputStream;4344public class WriteProgressive {4546public static void main(String[] args) throws IOException {47Iterator witer = ImageIO.getImageWritersByFormatName("png");48ImageWriter w = (ImageWriter)witer.next();4950File f = File.createTempFile("WriteProgressive", ".png");51ImageOutputStream ios = ImageIO.createImageOutputStream(f);52w.setOutput(ios);5354BufferedImage bi = new BufferedImage(100, 100,55BufferedImage.TYPE_3BYTE_BGR);56Graphics2D g = bi.createGraphics();57Random r = new Random(10);58for (int i = 0; i < 10000; i++) {59Color c =60new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));61g.setColor(c);62g.fillRect(r.nextInt(100), r.nextInt(100), 1, 1);63}6465IIOImage iioimage = new IIOImage(bi, null, null);6667ImageWriteParam param = w.getDefaultWriteParam();68param.setProgressiveMode(ImageWriteParam.MODE_DEFAULT);6970try {71w.write(null, iioimage, param);72} catch (NullPointerException npe) {73throw new RuntimeException("Got NPE during write!");74}7576ios.close();7778BufferedImage bi2 = ImageIO.read(f);79f.delete();8081ImageCompare.compare(bi, bi2);82}83}848586