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 4843895
  27  * @summary Tests that we handle raster with non-zero minX and minY correctly
  28  * @modules java.desktop/com.sun.imageio.plugins.jpeg
  29  */
  30 
  31 import java.awt.Rectangle;
  32 import java.awt.image.BufferedImage;
  33 import java.awt.image.RasterFormatException;
  34 import java.awt.image.WritableRaster;
  35 import java.io.ByteArrayOutputStream;
  36 import java.util.Arrays;
  37 import java.util.Iterator;
  38 
  39 import javax.imageio.IIOImage;
  40 import javax.imageio.ImageIO;
  41 import javax.imageio.ImageTypeSpecifier;
  42 import javax.imageio.ImageWriteParam;
  43 import javax.imageio.ImageWriter;
  44 import javax.imageio.metadata.IIOMetadata;
  45 import javax.imageio.stream.ImageOutputStream;
  46 import javax.imageio.stream.MemoryCacheImageOutputStream;
  47 
  48 public class RasterWithMinXTest {
  49 
  50     public static void main(String[] args) {
  51         String format = "jpeg";
  52 
  53         // Set output file.
  54         ImageOutputStream output = new MemoryCacheImageOutputStream(new ByteArrayOutputStream());
  55 
  56         // Create image.
  57         BufferedImage bi = new BufferedImage(256, 256,
  58                                              BufferedImage.TYPE_3BYTE_BGR);
  59 
  60         // Populate image.
  61         int[] rgbArray = new int[256];
  62         for(int i = 0; i < 256; i++) {
  63             Arrays.fill(rgbArray, i);
  64             bi.setRGB(0, i, 256, 1, rgbArray, 0, 256);
  65         }
  66 
  67         // create translated raster in order to get non-zero minX and minY
  68         WritableRaster r = (WritableRaster)bi.getRaster().createTranslatedChild(64,64);
  69 
  70         Iterator i =  ImageIO.getImageWritersByFormatName(format);
  71         ImageWriter iw = null;
  72         while(i.hasNext() && iw == null) {
  73             Object o = i.next();
  74             if (o instanceof com.sun.imageio.plugins.jpeg.JPEGImageWriter) {
  75                 iw = (ImageWriter)o;
  76             }
  77         }
  78         if (iw == null) {
  79             throw new RuntimeException("No available image writer");
  80         }
  81 
  82          ImageWriteParam iwp = iw.getDefaultWriteParam();
  83          IIOMetadata metadata = iw.getDefaultImageMetadata(new ImageTypeSpecifier(bi.getColorModel(), r.getSampleModel()), iwp);
  84 
  85          IIOImage img = new IIOImage(r, null, metadata);
  86 
  87          iw.setOutput(output);
  88          try {
  89              iw.write(img);
  90          } catch (RasterFormatException e) {
  91              e.printStackTrace();
  92              throw new RuntimeException("RasterException occurs. Test Failed!");
  93          } catch (Exception ex) {
  94              ex.printStackTrace();
  95              throw new RuntimeException("Unexpected Exception");
  96          }
  97 
  98          // test case of theImageWriteParam with non-null sourceRegion
  99          iwp.setSourceRegion(new Rectangle(32,32,192,192));
 100          metadata = iw.getDefaultImageMetadata(new ImageTypeSpecifier(bi.getColorModel(), r.getSampleModel()), iwp);
 101          try {
 102              iw.write(metadata, img, iwp);
 103          } catch (RasterFormatException e) {
 104              e.printStackTrace();
 105              throw new RuntimeException("SetSourceRegion causes the RasterException. Test Failed!");
 106          } catch (Exception ex) {
 107              ex.printStackTrace();
 108              throw new RuntimeException("Unexpected Exception");
 109          }
 110 
 111     }
 112 }