1/*
2 * Copyright (c) 2001, 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 4429934 4429950 4430991 4430993
27 * @summary Checks various aspects of ImageTypeSpecifier functionality
28 */
29
30import java.awt.color.ColorSpace;
31import java.awt.image.BufferedImage;
32import java.awt.image.ColorModel;
33import java.awt.image.DataBuffer;
34import java.awt.image.SampleModel;
35
36import javax.imageio.ImageTypeSpecifier;
37
38public class ImageTypeSpecifierTest {
39
40    private static void fail(String message) {
41        throw new RuntimeException(message);
42    }
43
44    private static void test4429934() {
45        try {
46            ImageTypeSpecifier itspecifier =
47                new ImageTypeSpecifier(null, null);
48            fail("Failed to get IAE!");
49        } catch( IllegalArgumentException e ) {
50        }
51
52        try {
53            ImageTypeSpecifier itspecifier = new ImageTypeSpecifier(null);
54            fail("Failed to get IAE!");
55        } catch (IllegalArgumentException e ) {
56        }
57    }
58
59    private static void test4429950() {
60        createPackedTest();
61        createInterleavedTest();
62        createBandedTest();
63        createIndexedTest();
64    }
65
66    public static void createPackedTest() {
67        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
68        int rmask = 0x00ff0000;
69        int gmask = 0x0000ff00;
70        int bmask = 0x000000ff;
71        int amask = 0xff000000;
72        try {
73            ImageTypeSpecifier.createPacked(null, rmask, gmask, bmask, amask, 0,
74false);
75            fail("Failed to get IAE!");
76        } catch (IllegalArgumentException e) {
77        }
78
79        ColorSpace cs1 = ColorSpace.getInstance(ColorSpace.CS_GRAY);
80        try {
81            ImageTypeSpecifier.createPacked
82                (cs1, rmask, gmask, bmask, amask, 0, false);
83            fail("Failed to get IAE!");
84        } catch (IllegalArgumentException e) {
85        }
86
87        try {
88            ImageTypeSpecifier.createPacked(cs, 0, 0, 0, 0, 0, false);
89            fail("Failed to get IAE!");
90        } catch (IllegalArgumentException e) {
91        }
92
93        try {
94            ImageTypeSpecifier.createPacked(cs, rmask, gmask, bmask, amask, -1,
95false);
96            fail("Failed to get IAE!");
97        } catch (IllegalArgumentException e) {
98        }
99    }
100
101    public static void createInterleavedTest() {
102        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
103        int[] bandOffsets = {0,0,0,0};
104        int dataType = 0;
105        boolean hasAlpha = true;
106        boolean isAlphaPremultiplied = true;
107
108        try {
109            ImageTypeSpecifier.createInterleaved
110                (null, bandOffsets, dataType, hasAlpha, isAlphaPremultiplied);
111            fail("Failed to get IAE!");
112        } catch (IllegalArgumentException e) {
113        }
114
115        try {
116            ImageTypeSpecifier.createInterleaved
117                (cs, null, dataType, hasAlpha, isAlphaPremultiplied);
118            fail("Failed to get IAE!");
119        } catch (IllegalArgumentException e) {
120        }
121
122        int[] bad_bandOffsets = {0,100,1000};
123        try {
124            ImageTypeSpecifier.createInterleaved
125                (cs, bad_bandOffsets, dataType, hasAlpha, isAlphaPremultiplied);
126            fail("Failed to get IAE!");
127        } catch (IllegalArgumentException e) {
128        }
129
130        int[] bad_bandOffsets_1 = {};
131        try {
132            ImageTypeSpecifier.createInterleaved
133                (cs, bad_bandOffsets_1, dataType, hasAlpha, isAlphaPremultiplied);
134            fail("Failed to get IAE!");
135        } catch (IllegalArgumentException e) {
136        }
137    }
138
139    public static void createBandedTest() {
140        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
141        int[] bankIndices = {0, 100, 1000, 10000};
142        int[] bandOffsets = {0, 100, 1000, 10000};
143        int dataType = 0;
144        boolean hasAlpha = true;
145        boolean isAlphaPremultiplied = true;
146
147        try {
148            ImageTypeSpecifier.createBanded(null, bankIndices, bandOffsets,
149                dataType, hasAlpha, isAlphaPremultiplied);
150            fail("Failed to get IAE!");
151        } catch (IllegalArgumentException e) {
152        }
153
154        int[] bad_bankIndices = {};
155        int[] bad_bandOffsets = {};
156        try {
157            ImageTypeSpecifier.createBanded(cs, bad_bankIndices, bad_bandOffsets,
158                dataType, hasAlpha, isAlphaPremultiplied);
159            fail("Failed to get IAE!");
160        } catch (IllegalArgumentException e) {
161        }
162
163        try {
164            ImageTypeSpecifier.createBanded(cs, bankIndices, bandOffsets,
165                99999, hasAlpha, isAlphaPremultiplied);
166            fail("Failed to get IAE!");
167        } catch (IllegalArgumentException e) {
168        }
169    }
170
171    public static void createGrayscaleTest() {
172        int bits = 8;
173        int dataType = DataBuffer.TYPE_BYTE;
174        boolean isSigned = true;
175        // testcase 1
176        try {
177            ImageTypeSpecifier.createGrayscale(100, dataType, isSigned);
178            fail("Failed to get IAE!");
179        } catch (IllegalArgumentException e) {
180        }
181
182        try {
183            ImageTypeSpecifier.createGrayscale(10, dataType, isSigned);
184            fail("Failed to get IAE!");
185        } catch (IllegalArgumentException e) {
186        }
187
188        try {
189            ImageTypeSpecifier.createGrayscale(bits, 100, isSigned);
190            fail("Failed to get IAE!");
191        } catch (IllegalArgumentException e) {
192        }
193   }
194
195   public static void createIndexedTest() {
196        byte[] redLUT = {0};
197        byte[] greenLUT = {0};
198        byte[] blueLUT = {0};
199        byte[] alphaLUT = {0};
200        int bits = 8;
201        int dataType = DataBuffer.TYPE_BYTE;
202
203        try {
204            ImageTypeSpecifier.createIndexed(redLUT, greenLUT,
205                blueLUT, alphaLUT, 0, dataType);
206            fail("Failed to get IAE!");
207        } catch (IllegalArgumentException e) {
208        }
209
210        try {
211            ImageTypeSpecifier.createIndexed(redLUT, greenLUT,
212                blueLUT, alphaLUT, 17, dataType);
213            fail("Failed to get IAE!");
214        } catch (IllegalArgumentException e) {
215        }
216
217        try {
218            ImageTypeSpecifier.createIndexed(redLUT, greenLUT,
219                blueLUT, alphaLUT, 10, dataType);
220            fail("Failed to get IAE!");
221        } catch (IllegalArgumentException e) {
222        }
223
224        byte[] greenLUT_4 = {};
225        try {
226            ImageTypeSpecifier.createIndexed(redLUT, greenLUT_4,
227                blueLUT, alphaLUT, bits, dataType);
228            fail("Failed to get IAE!");
229        } catch (IllegalArgumentException e) {
230        }
231
232        byte[] redLUT_5 = {};
233        try {
234            ImageTypeSpecifier.createIndexed(redLUT_5, greenLUT,
235                blueLUT, alphaLUT, bits, dataType);
236            fail("Failed to get IAE!");
237        } catch (IllegalArgumentException e) {
238        }
239
240        byte[] alphaLUT_6 = {};
241        try {
242            ImageTypeSpecifier.createIndexed(redLUT, greenLUT,
243                blueLUT, alphaLUT_6 , bits, dataType);
244            fail("Failed to get IAE!");
245        } catch (IllegalArgumentException e) {
246        }
247
248        try {
249            ImageTypeSpecifier.createIndexed(redLUT, greenLUT,
250                blueLUT, alphaLUT , bits, 100);
251            fail("Failed to get IAE!");
252        } catch (IllegalArgumentException e) {
253        }
254    }
255
256    private static void test4430991() {
257        ImageTypeSpecifier itspecifier;
258
259        itspecifier = ImageTypeSpecifier.createFromBufferedImageType
260            (BufferedImage.TYPE_INT_RGB);
261
262        try {
263            itspecifier.createBufferedImage(Integer.MAX_VALUE,
264                                            Integer.MAX_VALUE);
265            fail("Failed to get IAE!");
266        } catch (IllegalArgumentException e) {
267        }
268
269        try {
270            itspecifier.getSampleModel(Integer.MAX_VALUE, Integer.MAX_VALUE);
271            fail("Failed to get IAE!");
272        } catch (IllegalArgumentException e) {
273        }
274    }
275
276    private static void test4430993() {
277        ImageTypeSpecifier itspecifier;
278
279        int bits = 32;
280        int rmask = 0x00ff0000;
281        int gmask = 0x0000ff00;
282        int bmask = 0x000000ff;
283        ColorModel dcm = new java.awt.image.DirectColorModel
284            (bits, rmask, gmask, bmask);
285        int[] bandOffsets = new int[2];
286        bandOffsets[1] = 1;
287        SampleModel sm = new java.awt.image.ComponentSampleModel
288            (DataBuffer.TYPE_SHORT, 1, 1, 2, 2, bandOffsets);
289
290        try {
291            itspecifier = new ImageTypeSpecifier(dcm, sm);
292            fail("Failed to get IAE!");
293        } catch (IllegalArgumentException e) {
294        }
295    }
296
297    public static void main(String[] args) {
298        try {
299            test4429934();
300            test4429950();
301            test4430991();
302            test4430993();
303        } catch (RuntimeException e) {
304            throw e;
305        } catch (Exception e) {
306            e.printStackTrace();
307            System.out.println("Unexpected exception: " + e);
308        }
309    }
310}
311