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
24import java.awt.Point;
25import java.awt.image.DataBuffer;
26import java.awt.image.DataBufferByte;
27import java.awt.image.DataBufferDouble;
28import java.awt.image.DataBufferFloat;
29import java.awt.image.DataBufferInt;
30import java.awt.image.DataBufferShort;
31import java.awt.image.DataBufferUShort;
32import java.awt.image.Raster;
33import java.awt.image.SampleModel;
34import java.awt.image.MultiPixelPackedSampleModel;
35import java.awt.image.PixelInterleavedSampleModel;
36import java.awt.image.SinglePixelPackedSampleModel;
37
38/*
39 * @test
40 * @bug  6353518
41 * @summary  Test possible combinations of Raster creation
42 *           Test fails if any of Raster.createXXX() method throws exception.
43 */
44public class RasterCreationTest {
45
46    public static void main(String[] args) {
47
48        final int width = 10;
49        final int height = 5;
50        final int imageSize = width * height;
51        Point location = new Point(0, 0);
52        int[] bandOffsets = {0};
53        int[] bitMask = {0x00ff0000, 0x0000ff00, 0xff, 0x0};
54
55        SampleModel[] inputSampleModels = {
56            new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE,
57                    1, 1, 1, 1, bandOffsets),
58            new PixelInterleavedSampleModel(DataBuffer.TYPE_USHORT,
59                    1, 1, 1, 1, bandOffsets),
60            new PixelInterleavedSampleModel(DataBuffer.TYPE_INT,
61                    1, 1, 1, 1, bandOffsets),
62            new SinglePixelPackedSampleModel(DataBuffer.TYPE_BYTE,
63                    width, height, bitMask),
64            new SinglePixelPackedSampleModel(DataBuffer.TYPE_USHORT,
65                    width, height, bitMask),
66            new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT,
67                    width, height, bitMask),
68            new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE,
69                    width, height, 4),
70            new MultiPixelPackedSampleModel(DataBuffer.TYPE_USHORT,
71                    width, height, 2),
72            new MultiPixelPackedSampleModel(DataBuffer.TYPE_INT,
73                    width, height, 2)
74        };
75
76        // ---------------------------------------------------------------------
77        // Test ability to create Raster & WritableRaster with DataBuffer
78        // classes
79        // ---------------------------------------------------------------------
80        DataBuffer[] inputDataBuffer = {
81            new DataBufferByte(imageSize),
82            new DataBufferUShort(imageSize),
83            new DataBufferInt(imageSize, 1),
84            new DataBufferShort(imageSize),
85            new DataBufferFloat(imageSize),
86            new DataBufferDouble(imageSize)
87        };
88
89        for (SampleModel sm : inputSampleModels) {
90            for (DataBuffer db : inputDataBuffer) {
91                // Test Raster creation
92                Raster.createRaster(sm, db, location);
93
94                // Test writableRaster creation
95                Raster.createWritableRaster(sm, db, location);
96                Raster.createWritableRaster(sm, location);
97            }
98        }
99
100        // ---------------------------------------------------------------------
101        // Test ability to create Raster & WritableRaster with custom DataBuffer
102        // classes
103        // ---------------------------------------------------------------------
104        DataBuffer[] myDataBuffer = {
105            new MyDataBufferByte(imageSize),
106            new MyDataBufferUShort(imageSize),
107            new MyDataBufferInt(imageSize),
108            new MyDataBufferShort(imageSize),
109            new MyDataBufferDouble(imageSize),
110            new MyDataBufferFloat(imageSize)
111        };
112
113        for (SampleModel sm : inputSampleModels) {
114            for (DataBuffer db : myDataBuffer) {
115                // Test Raster creation
116                Raster.createRaster(sm, db, location);
117
118                // Test writableRaster creation
119                Raster.createWritableRaster(sm, db, location);
120                Raster.createWritableRaster(sm, location);
121            }
122        }
123
124        // ---------------------------------------------------------------------
125        // Test ability to create InterleavedRaster
126        // ---------------------------------------------------------------------
127        int[] interleavedInputDataTypes = {
128            DataBuffer.TYPE_BYTE,
129            DataBuffer.TYPE_USHORT
130        };
131
132        int numBands = 1;
133
134        for (int i : interleavedInputDataTypes) {
135            Raster.createInterleavedRaster(i, width, height, 1, location);
136            Raster.createInterleavedRaster(i, width, height, width * numBands,
137                    numBands, bandOffsets, location);
138        }
139
140        for (int i = 0; i < interleavedInputDataTypes.length ; i++) {
141            DataBuffer d1 = inputDataBuffer[i];
142            DataBuffer d2 = myDataBuffer[i];
143
144            Raster.createInterleavedRaster(d1, width, height, width * numBands,
145                    numBands, bandOffsets, location);
146            Raster.createInterleavedRaster(d2, width, height, width * numBands,
147                    numBands, bandOffsets, location);
148        }
149
150        // ---------------------------------------------------------------------
151        // Test ability to create BandedRaster
152        // ---------------------------------------------------------------------
153        int[] bankIndices = new int[numBands];
154        bankIndices[0] = 0;
155
156        int[] bandedInputDataTypes = {
157            DataBuffer.TYPE_BYTE,
158            DataBuffer.TYPE_USHORT,
159            DataBuffer.TYPE_INT
160        };
161
162        for (int i : bandedInputDataTypes) {
163            Raster.createBandedRaster(i, width, height, 1, location);
164            Raster.createBandedRaster(i, width, height, width,
165                    bankIndices, bandOffsets, location);
166        }
167
168        for (int i = 0; i < bandedInputDataTypes.length; i++) {
169            DataBuffer d1 = inputDataBuffer[i];
170            DataBuffer d2 = myDataBuffer[i];
171
172            Raster.createBandedRaster(d1, width, height, width,
173                    bankIndices, bandOffsets, location);
174            Raster.createBandedRaster(d2, width, height, width,
175                    bankIndices, bandOffsets, location);
176        }
177
178        // ---------------------------------------------------------------------
179        // Test ability to create PackedRaster
180        // ---------------------------------------------------------------------
181        int[] bandMasks = new int[numBands];
182        bandMasks[0] = 0;
183
184        int packedInputDataTypes[] = {
185            DataBuffer.TYPE_BYTE,
186            DataBuffer.TYPE_USHORT,
187            DataBuffer.TYPE_INT
188        };
189
190        for (int i : packedInputDataTypes) {
191            Raster.createPackedRaster(i, width, height, bandMasks, location);
192
193            for (int bits = 1; bits < 5; bits *= 2) {
194                Raster.createPackedRaster(i, width, height, 1, bits, location);
195            }
196        }
197
198        for (int i = 0; i < packedInputDataTypes.length; i++) {
199            DataBuffer d1 = inputDataBuffer[i];
200            DataBuffer d2 = myDataBuffer[i];
201
202            for (int bits = 1; bits < 5; bits *= 2) {
203                Raster.createPackedRaster(d1, width, height, bits, location);
204                Raster.createPackedRaster(d2, width, height, bits, location);
205            }
206
207            Raster.createPackedRaster(d1, width, height, 1,bandMasks, location);
208            Raster.createPackedRaster(d2, width, height, 1,bandMasks, location);
209        }
210    }
211}
212
213// ---------------------------------------------------------------------
214// Custom DataBuffer classes for testing purpose
215// ---------------------------------------------------------------------
216final class MyDataBufferByte extends DataBuffer {
217
218    byte[] data;
219    byte[][] bankdata;
220
221    public MyDataBufferByte(int size) {
222        super(TYPE_BYTE, size);
223        data = new byte[size];
224        bankdata = new byte[1][];
225        bankdata[0] = data;
226    }
227
228    @Override
229    public int getElem(int bank, int i) {
230        return bankdata[bank][i + offsets[bank]];
231    }
232
233    @Override
234    public void setElem(int bank, int i, int val) {
235        bankdata[bank][i + offsets[bank]] = (byte) val;
236    }
237}
238
239final class MyDataBufferDouble extends DataBuffer {
240
241    double[] data;
242    double[][] bankdata;
243
244    public MyDataBufferDouble(int size) {
245        super(TYPE_DOUBLE, size);
246        data = new double[size];
247        bankdata = new double[1][];
248        bankdata[0] = data;
249    }
250
251    @Override
252    public int getElem(int bank, int i) {
253        return (int) bankdata[bank][i + offsets[bank]];
254    }
255
256    @Override
257    public void setElem(int bank, int i, int val) {
258        bankdata[bank][i + offsets[bank]] = (double) val;
259    }
260}
261
262final class MyDataBufferFloat extends DataBuffer {
263
264    float[] data;
265    float[][] bankdata;
266
267    public MyDataBufferFloat(int size) {
268        super(TYPE_FLOAT, size);
269        data = new float[size];
270        bankdata = new float[1][];
271        bankdata[0] = data;
272    }
273
274    @Override
275    public int getElem(int bank, int i) {
276        return (int) bankdata[bank][i + offsets[bank]];
277    }
278
279    @Override
280    public void setElem(int bank, int i, int val) {
281        bankdata[bank][i + offsets[bank]] = (float) val;
282    }
283}
284
285final class MyDataBufferShort extends DataBuffer {
286
287    short[] data;
288    short[][] bankdata;
289
290    public MyDataBufferShort(int size) {
291        super(TYPE_SHORT, size);
292        data = new short[size];
293        bankdata = new short[1][];
294        bankdata[0] = data;
295    }
296
297    @Override
298    public int getElem(int bank, int i) {
299        return bankdata[bank][i + offsets[bank]];
300    }
301
302    @Override
303    public void setElem(int bank, int i, int val) {
304        bankdata[bank][i + offsets[bank]] = (short) val;
305    }
306}
307
308final class MyDataBufferUShort extends DataBuffer {
309
310    short[] data;
311    short[][] bankdata;
312
313    public MyDataBufferUShort(int size) {
314        super(TYPE_USHORT, size);
315        data = new short[size];
316        bankdata = new short[1][];
317        bankdata[0] = data;
318    }
319
320    @Override
321    public int getElem(int bank, int i) {
322        return bankdata[bank][i + offsets[bank]];
323    }
324
325    @Override
326    public void setElem(int bank, int i, int val) {
327        bankdata[bank][i + offsets[bank]] = (short) val;
328    }
329}
330
331final class MyDataBufferInt extends DataBuffer {
332
333    int[] data;
334    int[][] bankdata;
335
336    public MyDataBufferInt(int size) {
337        super(TYPE_INT, size);
338        data = new int[size];
339        bankdata = new int[1][];
340        bankdata[0] = data;
341    }
342
343    @Override
344    public int getElem(int bank, int i) {
345        return bankdata[bank][i + offsets[bank]];
346    }
347
348    @Override
349    public void setElem(int bank, int i, int val) {
350        bankdata[bank][i + offsets[bank]] = (int) val;
351    }
352}
353