1 /*
2  * Copyright (c) 2016, 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 /* @test
25  * @bug 8158356
26  * @summary Test AffineTransform transformations do not result in SIGSEGV
27  * if NaN or infinity parameter is passed as argument.
28  * @run main InvalidTransformParameterTest
29  */
30 
31 import java.awt.geom.AffineTransform;
32 import java.awt.image.AffineTransformOp;
33 import java.awt.image.BufferedImage;
34 import java.awt.image.ImagingOpException;
35 import java.awt.Point;
36 import java.awt.image.DataBuffer;
37 import java.awt.image.DataBufferByte;
38 import java.awt.image.PixelInterleavedSampleModel;
39 import java.awt.image.Raster;
40 import java.awt.image.WritableRaster;
41 import java.awt.image.RasterOp;
42 import java.awt.image.SampleModel;
43 
44 public class InvalidTransformParameterTest {
45 
main(String[] args)46     public static void main(String[] args) {
47         int count = 0;
48         final int testScenarios = 12;
49         double NaNArg = 0.0 / 0.0;
50         double positiveInfArg = 1.0 / 0.0;
51         double negativeInfArg = -1.0 / 0.0;
52 
53         BufferedImage img = new BufferedImage(5, 5, BufferedImage.TYPE_INT_ARGB);
54 
55         AffineTransform[] inputTransforms = new AffineTransform[testScenarios];
56 
57         for (int i = 0; i < inputTransforms.length; i++) {
58             inputTransforms[i] = new AffineTransform();
59         }
60 
61         inputTransforms[0].rotate(NaNArg, img.getWidth()/2, img.getHeight()/2);
62         inputTransforms[1].translate(NaNArg, NaNArg);
63         inputTransforms[2].scale(NaNArg, NaNArg);
64         inputTransforms[3].shear(NaNArg, NaNArg);
65 
66         inputTransforms[4].rotate(positiveInfArg, img.getWidth()/2, img.getHeight()/2);
67         inputTransforms[5].translate(positiveInfArg, positiveInfArg);
68         inputTransforms[6].scale(positiveInfArg, positiveInfArg);
69         inputTransforms[7].shear(positiveInfArg, positiveInfArg);
70 
71         inputTransforms[8].rotate(negativeInfArg, img.getWidth()/2, img.getHeight()/2);
72         inputTransforms[9].translate(negativeInfArg, negativeInfArg);
73         inputTransforms[10].scale(negativeInfArg, negativeInfArg);
74         inputTransforms[11].shear(negativeInfArg, negativeInfArg);
75 
76         // Test BufferedImage AffineTransform ---------------------------------
77 
78         for (int i = 0; i < inputTransforms.length; i++) {
79             try {
80                 testImageTransform(img, inputTransforms[i]);
81             } catch (ImagingOpException ex) {
82                 count++;
83             }
84         }
85 
86         if (count != testScenarios) {
87             throw new RuntimeException("Test failed. All test scenarios did not"
88                                        + " result in exception as expected.");
89         }
90 
91         // Test Raster AffineTransform ---------------------------------
92 
93         count = 0;
94         int[] bandOffsets = {0};
95         Point location = new Point(0, 0);
96         DataBuffer db = new DataBufferByte(10 * 10);
97         SampleModel sm = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE,
98                                                          10, 10, 1, 10,
99                                                          bandOffsets);
100 
101         Raster src = Raster.createRaster(sm, db, location);
102         WritableRaster dst = Raster.createWritableRaster(sm, db, location);
103 
104         for (int i = 0; i < inputTransforms.length; i++) {
105             try {
106                 testRasterTransform(src, dst, inputTransforms[i]);
107             } catch (ImagingOpException ex) {
108                 count++;
109             }
110         }
111 
112         if (count != testScenarios) {
113             throw new RuntimeException("Test failed. All test scenarios did not"
114                                        + " result in exception as expected.");
115         }
116     }
117 
testImageTransform(BufferedImage image, AffineTransform transform)118     public static BufferedImage testImageTransform(BufferedImage image,
119                                                    AffineTransform transform) {
120         AffineTransformOp op =
121                 new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
122 
123         BufferedImage transformedImage = new BufferedImage(image.getWidth(),
124                                                            image.getHeight(),
125                                                            image.getType());
126 
127         return op.filter(image, transformedImage);
128     }
129 
testRasterTransform(Raster src, WritableRaster dst, AffineTransform transform)130     public static Raster testRasterTransform(Raster src, WritableRaster dst,
131                                              AffineTransform transform) {
132         AffineTransformOp op =
133                 new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
134 
135         return op.filter(src, dst);
136     }
137 }
138 
139