1 /*
   2  * Copyright (c) 2001, 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 4432615
  27  * @summary Tests progressive writing in the PNG encoder
  28  * @modules java.desktop/com.sun.imageio.plugins.png
  29  */
  30 
  31 import java.awt.Color;
  32 import java.awt.Graphics2D;
  33 import java.awt.image.BufferedImage;
  34 import java.io.File;
  35 import java.io.IOException;
  36 import java.util.Iterator;
  37 import java.util.Random;
  38 
  39 import javax.imageio.IIOImage;
  40 import javax.imageio.ImageIO;
  41 import javax.imageio.ImageWriteParam;
  42 import javax.imageio.ImageWriter;
  43 import javax.imageio.stream.ImageOutputStream;
  44 
  45 public class WriteProgressive {
  46 
  47     public static void main(String[] args) throws IOException {
  48         Iterator witer = ImageIO.getImageWritersByFormatName("png");
  49         ImageWriter w = (ImageWriter)witer.next();
  50 
  51         File f = File.createTempFile("WriteProgressive", ".png");
  52         ImageOutputStream ios = ImageIO.createImageOutputStream(f);
  53         w.setOutput(ios);
  54 
  55         BufferedImage bi = new BufferedImage(100, 100,
  56                                              BufferedImage.TYPE_3BYTE_BGR);
  57         Graphics2D g = bi.createGraphics();
  58         Random r = new Random(10);
  59         for (int i = 0; i < 10000; i++) {
  60             Color c =
  61                 new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
  62             g.setColor(c);
  63             g.fillRect(r.nextInt(100), r.nextInt(100), 1, 1);
  64         }
  65 
  66         IIOImage iioimage = new IIOImage(bi, null, null);
  67 
  68         ImageWriteParam param = w.getDefaultWriteParam();
  69         param.setProgressiveMode(ImageWriteParam.MODE_DEFAULT);
  70 
  71         try {
  72             w.write(null, iioimage, param);
  73         } catch (NullPointerException npe) {
  74             throw new RuntimeException("Got NPE during write!");
  75         }
  76 
  77         ios.close();
  78 
  79         BufferedImage bi2 = ImageIO.read(f);
  80         f.delete();
  81 
  82         ImageCompare.compare(bi, bi2);
  83     }
  84 }