1 /*
   2  * Copyright (c) 2005, 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 6294920 6294926
  27  * @summary Test verifies that BMP images with compression types BI_JPEG and
  28  *          BI_PNG are read correctly in case of 1, 8, 16, 24 and 32 bpp
  29  */
  30 
  31 import java.awt.Color;
  32 import java.awt.Graphics;
  33 import java.awt.image.BufferedImage;
  34 import java.io.ByteArrayInputStream;
  35 import java.io.ByteArrayOutputStream;
  36 import java.io.IOException;
  37 
  38 import javax.imageio.IIOImage;
  39 import javax.imageio.ImageIO;
  40 import javax.imageio.ImageReader;
  41 import javax.imageio.ImageWriteParam;
  42 import javax.imageio.ImageWriter;
  43 import javax.imageio.stream.ImageInputStream;
  44 import javax.imageio.stream.ImageOutputStream;
  45 
  46 public class EmbeddedFormatTest {
  47     ImageWriter writer;
  48     ImageReader reader;
  49 
  50     static int[] bi_types = {
  51         BufferedImage.TYPE_INT_RGB,
  52         BufferedImage.TYPE_3BYTE_BGR,
  53         BufferedImage.TYPE_USHORT_555_RGB,
  54         BufferedImage.TYPE_BYTE_GRAY,
  55         BufferedImage.TYPE_BYTE_BINARY
  56     };
  57 
  58     public EmbeddedFormatTest() {
  59         writer = ImageIO.getImageWritersByFormatName("BMP").next();
  60         reader = ImageIO.getImageReadersByFormatName("BMP").next();
  61     }
  62 
  63     public void doTest(String compression, int bi_type) throws IOException {
  64         System.out.println("Test " + compression + " on " + getImageTypeName(bi_type));
  65         BufferedImage src = createTestImage(bi_type);
  66         writer.reset();
  67 
  68         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  69         ImageOutputStream ios =
  70                 ImageIO.createImageOutputStream(baos);
  71         writer.setOutput(ios);
  72 
  73         ImageWriteParam wparam = prepareWriteParam(compression);
  74         writer.write(null, new IIOImage(src, null, null), wparam);
  75         ios.flush();
  76         ios.close();
  77 
  78         // read result
  79         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
  80         ImageInputStream iis = ImageIO.createImageInputStream(bais);
  81         reader.reset();
  82         reader.setInput(iis);
  83 
  84         BufferedImage dst = reader.read(0);
  85 
  86         checkResult(dst);
  87     }
  88 
  89     protected BufferedImage createTestImage(int type) {
  90         BufferedImage img = new BufferedImage(200, 200, type);
  91         Graphics g = img.createGraphics();
  92         g.setColor(Color.black);
  93         g.fillRect(0, 0, 200, 200);
  94         g.setColor(Color.white);
  95         g.fillRect(50, 50, 100, 100);
  96 
  97         return img;
  98     }
  99 
 100     protected void  checkResult(BufferedImage img) {
 101         int imgBlack = img.getRGB(25, 25);
 102         if (imgBlack != 0xff000000) {
 103             throw new RuntimeException("Wrong black color: " +
 104                     Integer.toHexString(imgBlack));
 105         }
 106 
 107         int imgWhite = img.getRGB(100, 100);
 108         if (imgWhite != 0xffffffff) {
 109             throw new RuntimeException("Wrong white color: " +
 110                     Integer.toHexString(imgWhite));
 111         }
 112     }
 113 
 114     protected ImageWriteParam prepareWriteParam(String compression) {
 115         ImageWriteParam imageWriteParam = writer.getDefaultWriteParam();
 116         imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
 117         imageWriteParam.setCompressionType(compression);
 118         return imageWriteParam;
 119     }
 120 
 121 
 122     public static void main(String[] args) throws IOException {
 123         EmbeddedFormatTest t = new EmbeddedFormatTest();
 124 
 125         for (int i = 0; i < bi_types.length; i++) {
 126             t.doTest("BI_JPEG", bi_types[i]);
 127             t.doTest("BI_PNG",  bi_types[i]);
 128         }
 129     }
 130 
 131     static String getImageTypeName(int type) {
 132         switch(type) {
 133             case BufferedImage.TYPE_INT_RGB:
 134                 return "TYPE_INT_RGB";
 135             case BufferedImage.TYPE_3BYTE_BGR:
 136                 return "TYPE_3BYTE_BGR";
 137             case BufferedImage.TYPE_USHORT_555_RGB:
 138                 return "TYPE_USHORT_555_RGB";
 139             case BufferedImage.TYPE_BYTE_GRAY:
 140                 return "TYPE_BYTE_GRAY";
 141             case BufferedImage.TYPE_BYTE_BINARY:
 142                 return "TYPE_BYTE_BINARY";
 143             default:
 144                 return "TBD";
 145         }
 146     }
 147 }