1/*
2 * Copyright (c) 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 8177393
27 * @summary Verify RescaleOp applied to BufferedImages.
28 * @run main ImageRescaleOpTest
29 */
30
31import java.awt.Graphics2D;
32import java.awt.image.BufferedImage;
33import static java.awt.image.BufferedImage.*;
34import java.awt.image.RescaleOp;
35import java.awt.Color;
36import java.io.File;
37import java.io.IOException;
38import javax.imageio.ImageIO;
39
40public class ImageRescaleOpTest {
41
42    int w = 10, h = 10;
43    float scaleFactor = 0.5f;
44    float offset = 0.0f;
45    static boolean saveImage = false;
46
47    public static void main(String[] args) throws Exception {
48        saveImage = args.length > 0;
49        ImageRescaleOpTest test = new ImageRescaleOpTest();
50        test.startTest();
51    }
52
53    String getFileName(int s, int d) {
54        return textFor(s)+"_to_"+textFor(d)+".png";
55    }
56
57    String getMsgText(int s, int d) {
58        return textFor(s)+"->"+textFor(d)+": ";
59    }
60
61    String textFor(int t) {
62       switch (t) {
63           case TYPE_INT_ARGB        : return "ARGB";
64           case TYPE_INT_RGB         : return "RGB";
65           case TYPE_4BYTE_ABGR      : return "4BYTEABGR";
66           case TYPE_3BYTE_BGR       : return "3BYTEBGR";
67           case TYPE_USHORT_555_RGB  : return "USHORT_555_RGB";
68           case TYPE_USHORT_565_RGB  : return "USHORT_565_RGB";
69           case TYPE_USHORT_GRAY     : return "USHORT_GRAY";
70           default                   : return "OTHER";
71       }
72    }
73
74    private void startTest() throws Exception {
75
76        int expect = 0xff7f7f7f;
77        runTest(TYPE_INT_RGB, TYPE_INT_RGB, expect);
78        runTest(TYPE_INT_ARGB, TYPE_INT_ARGB, expect);
79        runTest(TYPE_INT_ARGB, TYPE_INT_RGB, expect);
80        runTest(TYPE_INT_RGB, TYPE_INT_ARGB, expect);
81
82        runTest(TYPE_3BYTE_BGR, TYPE_3BYTE_BGR, expect);
83        runTest(TYPE_3BYTE_BGR, TYPE_4BYTE_ABGR, expect);
84        runTest(TYPE_4BYTE_ABGR, TYPE_3BYTE_BGR, expect);
85        runTest(TYPE_4BYTE_ABGR, TYPE_4BYTE_ABGR, expect);
86
87        /* Slightly different values here due to limited precision */
88        runTest(TYPE_USHORT_555_RGB, TYPE_USHORT_555_RGB, 0xff7b7b7b);
89        runTest(TYPE_USHORT_565_RGB, TYPE_USHORT_565_RGB, 0xff7b7d7b);
90
91        /* 565->555 and 555->565 results are wrong as the slow code
92         * path used is not accounting for the difference in the range.
93         */
94        //runTest(TYPE_USHORT_555_RGB, TYPE_USHORT_565_RGB, expect);
95        //runTest(TYPE_USHORT_565_RGB, TYPE_USHORT_555_RGB, expect);
96
97        runTest(TYPE_USHORT_GRAY, TYPE_USHORT_GRAY, 0xffbcbcbc);
98
99    }
100
101   private void check(BufferedImage bi, int expect, String msg) {
102        int argb = bi.getRGB(w-1, h-1);
103        System.out.println(msg + Integer.toHexString(argb));
104        if (argb != expect) {
105            throw new RuntimeException(msg +
106                   " expected " + Integer.toHexString(expect) +
107                   " but got " + Integer.toHexString(argb));
108        }
109    }
110
111    private void runTest(int sType, int dType, int expect) {
112
113        BufferedImage src  = new BufferedImage(w, h, sType);
114        BufferedImage dst  = new BufferedImage(w, h, dType);
115        String msg = getMsgText(sType, dType);
116
117        Graphics2D g2d = src.createGraphics();
118        g2d.setColor(Color.WHITE);
119        g2d.fillRect(0, 0, w, h);
120        RescaleOp res = new RescaleOp(scaleFactor, offset, null);
121        res.filter(src, dst);
122        if (saveImage) {
123            try {
124               String fname = getFileName(sType, dType);
125               ImageIO.write(dst, "png", new File(fname));
126            } catch (IOException e) {
127            }
128        }
129        check(dst, expect, msg);
130   }
131}
132