1 /*
2  * Copyright (c) 2014, 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     8038000 8047066
27  *
28  * @summary Verifies that we could create different type of Rasters with height 1
29  * and strideline which exceeds raster width.
30  * Also checks that a set of RasterOp work correctly with such kind of Rasters.
31  * For 8047066 verifies that ColorConvertOp could process
32  * Raster (ByteBuffer + SinglePixelPackedSampleModel)
33  *
34  * @run     main bug8038000
35  */
36 
37 import java.awt.*;
38 import java.awt.color.ColorSpace;
39 import java.awt.geom.AffineTransform;
40 import java.awt.image.*;
41 import java.util.Arrays;
42 
43 public class bug8038000 {
44 
main(String[] args)45     public static void main(String[] args) throws Exception {
46         new bug8038000().checkOps();
47 
48         // No exceptions - Passed
49     }
50 
checkOps()51     private void checkOps() throws Exception {
52 
53         RasterOp[] ops = new RasterOp[] {
54                 new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_sRGB),
55                         ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB), null),
56                 new AffineTransformOp(AffineTransform.getScaleInstance(1, 1.1), null)
57         };
58 
59 
60         for (RasterOp op: ops) {
61             // Banded rasters
62             checkOp(Raster.createBandedRaster(DataBuffer.TYPE_BYTE, 10, 1, 10,
63                             new int[] {0, 1, 2}, new int[]{2,1,0}, null),
64                     Raster.createBandedRaster(DataBuffer.TYPE_BYTE, 10, 1, 1001,
65                             new int[] {0, 1, 2}, new int[]{2,1,0}, null), op);
66             checkOp(Raster.createBandedRaster(DataBuffer.TYPE_USHORT, 10, 1, 10,
67                     new int[] {0, 1, 2}, new int[]{2,1,0}, null),
68                     Raster.createBandedRaster(DataBuffer.TYPE_USHORT, 10, 1, 1001,
69                             new int[] {0, 1, 2}, new int[]{2,1,0}, null), op);
70             checkOp(Raster.createBandedRaster(DataBuffer.TYPE_INT, 10, 1, 10,
71                     new int[] {0, 1, 2}, new int[]{2,1,0}, null),
72                     Raster.createBandedRaster(DataBuffer.TYPE_INT, 10, 1, 1001,
73                             new int[] {0, 1, 2}, new int[]{2,1,0}, null), op);
74 
75             // Interleaved rasters
76             checkOp(Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
77                             10, 1, 30, 3, new int[]{0, 1, 2}, null),
78                     Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
79                             10, 1, 1001, 3, new int[]{0, 1, 2}, null),
80                     op);
81 
82             checkOp(Raster.createInterleavedRaster(DataBuffer.TYPE_USHORT,
83                             10, 1, 30, 3, new int[]{0, 1, 2}, null),
84                     Raster.createInterleavedRaster(DataBuffer.TYPE_USHORT,
85                             10, 1, 1001, 3, new int[]{0, 1, 2}, null),
86                     op);
87 
88             // Packed rasters
89             checkOp(Raster.createPackedRaster(new DataBufferByte(10), 10, 1, 10,
90                             new int[] {0x01, 0x02, 0x04}, null),
91                     Raster.createPackedRaster(new DataBufferByte(10), 10, 1, 2000,
92                             new int[] {0x01, 0x02, 0x04}, null),
93                     op);
94             checkOp(Raster.createPackedRaster(new DataBufferInt(10), 10, 1, 10,
95                         new int[] {0xff0000, 0x00ff00, 0x0000ff}, null),
96                     Raster.createPackedRaster(new DataBufferInt(10), 10, 1, 20,
97                             new int[] {0xff0000, 0x00ff00, 0x0000ff}, null),
98                     op);
99 
100         }
101     }
102 
103     /**
104      *  Takes two identical rasters (identical with the exception of scanline stride)
105      *  fills their pixels with identical data, applies the RasterOp to both rasters
106      *  and checks that the result is the same
107      */
checkOp(WritableRaster wr1, WritableRaster wr2, RasterOp op)108     private void checkOp(WritableRaster wr1, WritableRaster wr2, RasterOp op) {
109         System.out.println("Checking " + op + " with rasters: \n    " + wr1 +
110                 "\n    " + wr2);
111         try {
112             WritableRaster r1 = op.filter(fillRaster(wr1), null);
113             WritableRaster r2 = op.filter(fillRaster(wr2), null);
114             compareRasters(r1, r2);
115         } catch (ImagingOpException e) {
116             System.out.println("    Skip: Op is not supported: " + e);
117         }
118     }
119 
fillRaster(WritableRaster wr)120     private WritableRaster fillRaster(WritableRaster wr) {
121         int c = 0;
122         for(int x = wr.getMinX(); x < wr.getMinX() + wr.getWidth(); x++) {
123             for(int y = wr.getMinY(); y < wr.getMinY() + wr.getHeight(); y++) {
124                 for (int b = 0; b < wr.getNumBands(); b++) {
125                     wr.setSample(x, y, b, c++);
126                 }
127             }
128         }
129         return wr;
130     }
131 
compareRasters(Raster r1, Raster r2)132     private void compareRasters(Raster r1, Raster r2) {
133         Rectangle bounds = r1.getBounds();
134         if (!bounds.equals(r2.getBounds())) {
135             throw new RuntimeException("Bounds differ.");
136         }
137 
138         if (r1.getNumBands() != r2.getNumBands()) {
139             throw new RuntimeException("Bands differ.");
140         }
141 
142         int[] b1 = new int[r1.getNumBands()];
143         int[] b2 = new int[r1.getNumBands()];
144 
145         for (int x = (int) bounds.getX(); x < bounds.getMaxX(); x++) {
146             for (int y = (int) bounds.getY(); y < bounds.getMaxY(); y++) {
147                 r1.getPixel(x,y, b1);
148                 r2.getPixel(x,y, b2);
149                 if (!Arrays.equals(b1, b2)) {
150                     throw new RuntimeException("Pixels differ.");
151                 }
152             }
153         }
154     }
155 }
156