1/*
2 * Copyright (c) 2007, 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     6585922
27 * @summary Test verifies ConvolveOp creates compatible destination images
28 *          of same type and this pair {src, dst} can be handled by the
29 *          ConvolveOp filter.
30 *
31 * @run     main OpCompatibleImageTest
32 */
33
34import java.awt.Color;
35import java.awt.Graphics;
36import java.awt.image.BufferedImage;
37import java.awt.image.BufferedImageOp;
38import java.awt.image.ColorModel;
39import java.awt.image.ConvolveOp;
40import java.awt.image.ImagingOpException;
41import java.awt.image.Kernel;
42
43public class OpCompatibleImageTest {
44
45    public static void main(String[] args) {
46        OpCompatibleImageTest t = new OpCompatibleImageTest();
47        t.doTest(BufferedImage.TYPE_3BYTE_BGR);
48        t.doTest(BufferedImage.TYPE_4BYTE_ABGR);
49        t.doTest(BufferedImage.TYPE_BYTE_GRAY);
50        t.doTest(BufferedImage.TYPE_INT_ARGB);
51        t.doTest(BufferedImage.TYPE_INT_BGR);
52        t.doTest(BufferedImage.TYPE_INT_RGB);
53        t.doTest(BufferedImage.TYPE_BYTE_INDEXED);
54    }
55
56    private BufferedImageOp op;
57
58    public OpCompatibleImageTest() {
59        final Kernel kernel = new Kernel(3, 3,
60                new float[] {
61            1f/9f, 1f/9f, 1f/9f,
62            1f/9f, 1f/9f, 1f/9f,
63            1f/9f, 1f/9f, 1f/9f});
64        op = new ConvolveOp(kernel);
65    }
66
67    public void doTest(int type) {
68        System.out.println("Test for type " + describeType(type));
69
70        BufferedImage src = createTestImage(type);
71
72        BufferedImage res = null;
73
74        System.out.println("Testing null destination...");
75        try {
76            res = op.filter(src, null);
77        } catch (ImagingOpException e) {
78            throw new RuntimeException("Test FAILED!", e);
79        }
80
81        if (res == null ||
82            ((src.getType() != BufferedImage.TYPE_BYTE_INDEXED) &&
83             (res.getType() != src.getType())))
84        {
85            throw new RuntimeException("Test FAILED!");
86        }
87        System.out.println("Test PASSED.");
88    }
89
90    private BufferedImage createCompatible(ColorModel cm, int w, int h) {
91        return new BufferedImage (cm,
92                                  cm.createCompatibleWritableRaster(w, h),
93                                  cm.isAlphaPremultiplied(), null);
94    }
95
96    private BufferedImage createTestImage(int type) {
97        BufferedImage img = new BufferedImage(100, 100, type);
98        Graphics g = img.createGraphics();
99        g.setColor(Color.red);
100        g.fillRect(0, 0, 100, 100);
101        g.dispose();
102
103        return img;
104    }
105
106    private static String describeType(int type) {
107        switch(type) {
108        case BufferedImage.TYPE_3BYTE_BGR:
109            return "TYPE_3BYTE_BGR";
110        case BufferedImage.TYPE_4BYTE_ABGR:
111            return "TYPE_4BYTE_ABGR";
112        case BufferedImage.TYPE_BYTE_GRAY:
113            return "TYPE_BYTE_GRAY";
114        case BufferedImage.TYPE_INT_ARGB:
115            return "TYPE_INT_ARGB";
116        case BufferedImage.TYPE_INT_BGR:
117            return  "TYPE_INT_BGR";
118        case BufferedImage.TYPE_INT_RGB:
119            return "TYPE_INT_RGB";
120        case BufferedImage.TYPE_BYTE_INDEXED:
121            return "TYPE_BYTE_INDEXED";
122        default:
123            throw new RuntimeException("Test FAILED: unknown type " + type);
124        }
125    }
126}
127