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 4929367
  27  * @summary tests what BMP image was decoded correctly if destination buffered
  28  *          image is bigger than source image
  29  */
  30 
  31 import java.awt.Color;
  32 import java.awt.Graphics2D;
  33 import java.awt.image.BufferedImage;
  34 import java.io.ByteArrayInputStream;
  35 import java.io.ByteArrayOutputStream;
  36 import java.io.IOException;
  37 import java.util.Iterator;
  38 
  39 import javax.imageio.ImageIO;
  40 import javax.imageio.ImageReadParam;
  41 import javax.imageio.ImageReader;
  42 import javax.imageio.ImageTypeSpecifier;
  43 import javax.imageio.ImageWriter;
  44 
  45 public class BmpBigDestinationTest {
  46     static String format = "BMP";
  47     public static void main(String[] args) {
  48         try {
  49             BufferedImage src = new BufferedImage(100, 100,
  50                                                   BufferedImage.TYPE_INT_RGB);
  51             Graphics2D g = src.createGraphics();
  52             g.setColor(Color.red);
  53             g.fillRect(0,0,100, 100);
  54 
  55             ByteArrayOutputStream baos = new ByteArrayOutputStream();
  56 
  57             ImageWriter iw =
  58                 (ImageWriter)ImageIO.getImageWritersByFormatName(format).next();
  59             if (iw == null) {
  60                 throw new RuntimeException("No writer available. Test failed.");
  61             }
  62 
  63             iw.setOutput(ImageIO.createImageOutputStream(baos));
  64             iw.write(src);
  65 
  66             byte[] data = baos.toByteArray();
  67 
  68             ImageReader ir =
  69                 (ImageReader)ImageIO.getImageReadersByFormatName(format).next();
  70             ir.setInput(
  71                 ImageIO.createImageInputStream(
  72                     new ByteArrayInputStream(data)));
  73 
  74             Iterator specifiers = ir.getImageTypes(0);
  75             ImageTypeSpecifier typeSpecifier = null;
  76 
  77             if (specifiers.hasNext()) {
  78                 typeSpecifier = (ImageTypeSpecifier) specifiers.next();
  79             }
  80             ImageReadParam param = new ImageReadParam();
  81             BufferedImage dst = typeSpecifier.createBufferedImage(200, 200);
  82             param.setDestination(dst);
  83 
  84             ir.read(0, param);
  85 
  86             checkResults(src,dst);
  87 
  88         } catch (IOException e) {
  89             e.printStackTrace();
  90             throw new RuntimeException("Unexpected exception. Test failed.");
  91         }
  92     }
  93 
  94     private static void checkResults(BufferedImage src, BufferedImage dst) {
  95         for(int x=0; x<src.getWidth(); x++) {
  96             for(int y=0; y<src.getHeight(); y++) {
  97                 int srcRgb = src.getRGB(x,y);
  98                 int dstRgb = dst.getRGB(x,y);
  99                 if (srcRgb != dstRgb) {
 100                     throw new RuntimeException("Images are different at point ["
 101                                                + x + "," + y + "]");
 102                 }
 103             }
 104         }
 105     }
 106 }