1 /*
2 * Copyright (c) 2025, 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 import java.awt.color.ColorSpace;
25 import java.awt.image.BufferedImage;
26 import java.awt.image.ColorModel;
27 import java.awt.image.ComponentColorModel;
28 import java.awt.image.DataBuffer;
29 import java.awt.image.DataBufferByte;
30 import java.awt.image.Raster;
31 import java.awt.image.WritableRaster;
32
33 /*
34 * @test
35 * @bug 4954405
36 * @summary Verify DataBuffer offsets are handled by ByteInterleavedRaster
37 */
38
39 public class ByteInterleavedRasterOffsetsTest {
40
41 public static void main(String[] args) {
42 byte[] data = { 0, -1, 0, 0 }; // only set the R sample.
43 int[] bandOffsets = { 0, 1, 2 };
44 DataBuffer databuf = new DataBufferByte(data, 3, 1);
45 WritableRaster raster =
46 Raster.createInterleavedRaster(databuf, 1, 1, 3, 3, bandOffsets, null);
47 int[] pixels = raster.getPixels(0, 0, 1, 1, (int[])null);
48 byte[] elements = (byte[])raster.getDataElements(0, 0, null);
49 ColorModel colorModel = new ComponentColorModel(
50 ColorSpace.getInstance(ColorSpace.CS_sRGB), false, false,
51 ColorModel.OPAQUE, DataBuffer.TYPE_BYTE);
52 BufferedImage img = new BufferedImage(colorModel, raster, false, null);
53 int pixel = img.getRGB(0, 0);
54
55 System.out.println("PIXEL0=" + Integer.toHexString(pixels[0]));
56 System.out.println("PIXEL1=" + Integer.toHexString(pixels[1]));
57 System.out.println("PIXEL2=" + Integer.toHexString(pixels[2]));
58 System.out.println("ELEMENT0=" + Integer.toHexString(elements[0] & 0xff));
59 System.out.println("ELEMENT1=" + Integer.toHexString(elements[1] & 0xff));
60 System.out.println("ELEMENT2=" + Integer.toHexString(elements[2] & 0xff));
61 System.out.println("PIXEL=" + Integer.toHexString(pixel));
62
63 if ((pixels[0] != 0xff) || (pixels[1] != 0) || (pixels[2] != 0)) {
64 throw new RuntimeException("Unexpected pixels");
65 }
66 if (((elements[0] & 0xff) != 0xff) || (elements[1] != 0) || (elements[2] != 0)) {
67 throw new RuntimeException("Unexpected elements");
68 }
69 if (pixel != 0xffff0000) {
70 throw new RuntimeException("Unexpected pixel");
71 }
72 }
73 }