EdgeNoOpCrash.java revision 2365:4c234c13f66a
1/*
2 * Copyright (c) 2008, 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     6726779
27 * @summary Test verifies that ConvolveOp with the EDGE_NO_OP edge condition
28 *          does not cause JVM crash if size of source raster elements is
29 *          greather than size of the destination raster element.
30 *
31 * @run     main EdgeNoOpCrash
32 */
33import java.awt.Point;
34import java.awt.image.ConvolveOp;
35import java.awt.image.DataBuffer;
36import java.awt.image.ImagingOpException;
37import java.awt.image.Kernel;
38import java.awt.image.Raster;
39import java.awt.image.WritableRaster;
40import java.util.Arrays;
41
42public class EdgeNoOpCrash {
43    private static final int w = 3000;
44    private static final int h = 200;
45
46    public static void main(String[] args) {
47        crashTest();
48    }
49
50    private static void crashTest() {
51        Raster src = createSrcRaster();
52        WritableRaster dst = createDstRaster();
53        ConvolveOp op = createConvolveOp(ConvolveOp.EDGE_NO_OP);
54        try {
55            op.filter(src, dst);
56        } catch (ImagingOpException e) {
57            /*
58             * The test pair of source and destination rasters
59             * may cause failure of the medialib convolution routine,
60             * so this exception is expected.
61             *
62             * The JVM crash is the only manifestation of this
63             * test failure.
64             */
65        }
66        System.out.println("Test PASSED.");
67    }
68
69    private static Raster createSrcRaster() {
70        WritableRaster r = Raster.createInterleavedRaster(DataBuffer.TYPE_USHORT,
71                w, h, 4, new Point(0, 0));
72
73        return r;
74    }
75
76    private static WritableRaster createDstRaster() {
77        WritableRaster r = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
78                w, h, 4, new Point(0, 0));
79
80        return r;
81    }
82
83    private static ConvolveOp createConvolveOp(int edgeHint) {
84        final int kw = 3;
85        final int kh = 3;
86        float[] kdata = new float[kw * kh];
87        float v = 1f / kdata.length;
88        Arrays.fill(kdata, v);
89
90        Kernel k = new Kernel(kw, kh, kdata);
91        ConvolveOp op = new ConvolveOp(k, edgeHint, null);
92
93        return op;
94    }
95}
96