1 /*
   2  * Copyright (c) 2003, 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 4893464
  27  * @summary Tests bmp writer behavior with different compression modes
  28  */
  29 
  30 import java.awt.Color;
  31 import java.awt.Graphics;
  32 import java.awt.image.BufferedImage;
  33 import java.io.File;
  34 
  35 import javax.imageio.IIOImage;
  36 import javax.imageio.ImageIO;
  37 import javax.imageio.ImageTypeSpecifier;
  38 import javax.imageio.ImageWriteParam;
  39 import javax.imageio.ImageWriter;
  40 import javax.imageio.metadata.IIOMetadata;
  41 import javax.imageio.stream.ImageOutputStream;
  42 
  43 public class CompressionModeTest {
  44 
  45     public static void main(String args[]) {
  46         int[] iModes = { ImageWriteParam.MODE_DISABLED,
  47                          ImageWriteParam.MODE_EXPLICIT,
  48                          ImageWriteParam.MODE_COPY_FROM_METADATA,
  49                          ImageWriteParam.MODE_DEFAULT };
  50 
  51         String[] strModes = { "ImageWriteParam.MODE_DISABLED",
  52                               "ImageWriteParam.MODE_EXPLICIT",
  53                               "ImageWriteParam.MODE_COPY_FROM_METADATA",
  54                               "ImageWriteParam.MODE_DEFAULT" };
  55 
  56         for(int i=0; i<iModes.length; i++) {
  57             System.out.println("Test compression mode "+strModes[i]);
  58             doTest(iModes[i]);
  59         }
  60     }
  61 
  62     private static void doTest(int mode) {
  63         String fileFormat = "bmp";
  64         try {
  65             ImageWriter iw = (ImageWriter)ImageIO.getImageWritersBySuffix(fileFormat).next();
  66             if(iw == null) {
  67                 throw new RuntimeException("No available image writer for "
  68                                            + fileFormat
  69                                            + " Test failed.");
  70             }
  71 
  72             File file = new File("image." + fileFormat);
  73             ImageOutputStream ios = ImageIO.createImageOutputStream(file);
  74             iw.setOutput(ios);
  75 
  76             BufferedImage bimg = new BufferedImage(100,
  77                                                    100, BufferedImage.TYPE_INT_RGB);
  78             Graphics g = bimg.getGraphics();
  79             g.setColor(Color.green);
  80             g.fillRect(0,0,100,100);
  81 
  82             ImageWriteParam param = iw.getDefaultWriteParam();
  83 
  84             param.setCompressionMode(mode);
  85 
  86             IIOMetadata meta = iw.getDefaultImageMetadata(new ImageTypeSpecifier(bimg),
  87                                                           param);
  88 
  89             IIOImage iioImg = new IIOImage(bimg, null, meta);
  90             iw.write(null, iioImg, param);
  91         } catch(Exception e) {
  92             e.printStackTrace();
  93             throw new RuntimeException("Test failed.");
  94         }
  95     }
  96 }