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
31import java.awt.Rectangle;
32import java.awt.image.BufferedImage;
33import java.awt.image.RasterFormatException;
34import java.awt.image.WritableRaster;
35import java.io.ByteArrayOutputStream;
36import java.util.Arrays;
37import java.util.Iterator;
38
39import javax.imageio.IIOImage;
40import javax.imageio.ImageIO;
41import javax.imageio.ImageTypeSpecifier;
42import javax.imageio.ImageWriteParam;
43import javax.imageio.ImageWriter;
44import javax.imageio.metadata.IIOMetadata;
45import javax.imageio.stream.ImageOutputStream;
46import javax.imageio.stream.MemoryCacheImageOutputStream;
47
48public 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}
113