1 /*
2  * Copyright (c) 2018, 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 8194489
27  * @summary The test checks whether BandedSampleModel computes appropriate
28  *          size for allocating memory in DataBuffer.
29  * @run main BandedSampleModelSizeTest
30  */
31 import java.awt.image.BandedSampleModel;
32 import java.awt.image.DataBuffer;
33 import java.util.Arrays;
34 
35 public class BandedSampleModelSizeTest {
36     // Constants
37     private static final int TEST_NUM_BANDS = 3;
38     private static final int TEST_SRC_IMG_DIM = 10;
39 
40     // Required sample models
41     private static BandedSampleModel singleBankModel = null;
42     private static BandedSampleModel multiBankModel = null;
43 
initTest()44     private static void initTest() {
45         int[] bandOffsets = new int[TEST_NUM_BANDS];
46         int[] bankIndices = new int[TEST_NUM_BANDS];
47 
48         /*
49          * Create a BandedSampleModel to store samples of all bands in one
50          * bank of DataBuffer.
51          */
52         bandOffsets[0] = 0;
53         bandOffsets[1] = 120;
54         bandOffsets[2] = 240;
55         bankIndices[0] = 0;
56         bankIndices[1] = 0;
57         bankIndices[2] = 0;
58 
59         singleBankModel = new BandedSampleModel(DataBuffer.TYPE_BYTE,
60                 TEST_SRC_IMG_DIM, TEST_SRC_IMG_DIM, TEST_SRC_IMG_DIM,
61                 bankIndices, bandOffsets);
62 
63         /*
64          * Create a BandedSampleModel to store samples of all bands in
65          * different banks of DataBuffer.
66          */
67         bandOffsets[0] = 0;
68         bandOffsets[1] = 20;
69         bandOffsets[2] = 40;
70         bankIndices[0] = 0;
71         bankIndices[1] = 1;
72         bankIndices[2] = 2;
73 
74         multiBankModel = new BandedSampleModel(DataBuffer.TYPE_BYTE,
75                 TEST_SRC_IMG_DIM, TEST_SRC_IMG_DIM, TEST_SRC_IMG_DIM,
76                 bankIndices, bandOffsets);
77     }
78 
testSingleBankModel()79     private static void testSingleBankModel() {
80         int[] srcSamples = new int[TEST_NUM_BANDS];
81         int[] resSamples = new int[TEST_NUM_BANDS];
82 
83         // Create image buffer for the requried sample model
84         DataBuffer imgBuffer = singleBankModel.createDataBuffer();
85 
86         // Test the sample model by setting a pixel value & inspecting the same.
87         Arrays.fill(srcSamples, 125);
88         singleBankModel.setPixel(0, 0, srcSamples, imgBuffer);
89         singleBankModel.getPixel(0, 0, resSamples, imgBuffer);
90         if (!Arrays.equals(srcSamples, resSamples)) {
91             throw new RuntimeException("Test Failed. Incorrect samples found"
92                     + " in the image");
93         }
94 
95         Arrays.fill(srcSamples, 250);
96         singleBankModel.setPixel(9, 9, srcSamples, imgBuffer);
97         singleBankModel.getPixel(9, 9, resSamples, imgBuffer);
98         if (!Arrays.equals(srcSamples, resSamples)) {
99             throw new RuntimeException("Test Failed. Incorrect samples found"
100                     + " in the image");
101         }
102     }
103 
testMultiBankModel()104     private static void testMultiBankModel() {
105         int[] srcSamples = new int[TEST_NUM_BANDS];
106         int[] resSamples = new int[TEST_NUM_BANDS];
107 
108         // Create image buffer for the required sample model
109         DataBuffer imgBuffer = multiBankModel.createDataBuffer();
110 
111         // Test the sample model by setting a pixel value & inspecting the same.
112         Arrays.fill(srcSamples, 125);
113         multiBankModel.setPixel(0, 0, srcSamples, imgBuffer);
114         multiBankModel.getPixel(0, 0, resSamples, imgBuffer);
115         if (!Arrays.equals(srcSamples, resSamples)) {
116             throw new RuntimeException("Test Failed. Incorrect samples found"
117                     + " in the image");
118         }
119 
120         Arrays.fill(srcSamples, 250);
121         multiBankModel.setPixel(9, 9, srcSamples, imgBuffer);
122         multiBankModel.getPixel(9, 9, resSamples, imgBuffer);
123         if (!Arrays.equals(srcSamples, resSamples)) {
124             throw new RuntimeException("Test Failed. Incorrect samples found"
125                     + " in the image");
126         }
127     }
128 
main(String args[])129     public static void main(String args[]) {
130         // Initialize the test
131         initTest();
132 
133         // Test banded sample model with single bank of data buffer
134         testSingleBankModel();
135 
136         // Test banded sample model with multiple banks of data buffer
137         testMultiBankModel();
138     }
139 }
140