Skip to content

Commit 7213c0d

Browse files
committed
8340411: open source several 2D imaging tests
Backport-of: 8dcf7b8fa7b17bf34c62c561c6ed78e8080df1ff
1 parent 751130a commit 7213c0d

File tree

4 files changed

+485
-0
lines changed

4 files changed

+485
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
* Copyright (c) 1999, 2024, 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 4184283
27+
* @summary Checks rendering of dithered byte packed image does not crash.
28+
*/
29+
30+
import java.awt.Color;
31+
import java.awt.Component;
32+
import java.awt.Graphics2D;
33+
import java.awt.Image;
34+
import java.awt.image.BufferedImage;
35+
import java.awt.image.ColorModel;
36+
import java.awt.image.IndexColorModel;
37+
import java.awt.image.MemoryImageSource;
38+
import java.awt.image.WritableRaster;
39+
40+
public class DitherTest extends Component {
41+
42+
final static int NOOP = 0;
43+
final static int RED = 1;
44+
final static int GREEN = 2;
45+
final static int BLUE = 3;
46+
final static int ALPHA = 4;
47+
final static int SATURATION = 5;
48+
49+
final static byte red[] = {(byte)0, (byte)132, (byte)0, (byte)132, (byte)0, (byte)132,
50+
(byte)0, (byte)198, (byte)198, (byte)165, (byte)255, (byte)165, (byte)132,
51+
(byte)255, (byte)0, (byte)255};
52+
53+
final static byte green[] = {(byte)0, (byte)0, (byte)130, (byte)130, (byte)0,
54+
(byte)0, (byte)130, (byte)195, (byte)223, (byte)203, (byte)251, (byte)162,
55+
(byte)132, (byte)0, (byte)255, (byte)255};
56+
57+
final static byte blue[] = {(byte)0, (byte)0, (byte)0, (byte)0, (byte)132, (byte)132,
58+
(byte)132, (byte)198, (byte)198, (byte)247, (byte)247, (byte)165, (byte)132,
59+
(byte)0, (byte)0, (byte)0};
60+
61+
static IndexColorModel cm16 = new IndexColorModel( 4, 16, red, green, blue);
62+
63+
64+
public static void main(String args[]) {
65+
66+
int imageWidth = 256;
67+
int imageHeight = 256;
68+
WritableRaster raster = cm16.createCompatibleWritableRaster(imageWidth, imageHeight);
69+
BufferedImage intermediateImage = new BufferedImage(cm16, raster, false, null);
70+
Image calculatedImage = calculateImage();
71+
72+
Graphics2D ig = intermediateImage.createGraphics();
73+
// Clear background and fill a red rectangle just to prove that we can draw on intermediateImage
74+
ig.setColor(Color.white);
75+
ig.fillRect(0,0,imageWidth,imageHeight);
76+
ig.drawImage(calculatedImage, 0, 0, imageWidth, imageHeight, null);
77+
ig.setColor(Color.red);
78+
ig.fillRect(0,0,5,5);
79+
80+
BufferedImage destImage = new BufferedImage(imageWidth, imageWidth, BufferedImage.TYPE_INT_RGB);
81+
Graphics2D dg = destImage.createGraphics();
82+
dg.drawImage(intermediateImage, 0, 0, imageWidth, imageHeight, null);
83+
}
84+
85+
private static void applymethod(int c[], int method, int step, int total, int vals[]) {
86+
if (method == NOOP)
87+
return;
88+
int val = ((total < 2)
89+
? vals[0]
90+
: vals[0] + ((vals[1] - vals[0]) * step / (total - 1)));
91+
switch (method) {
92+
case RED:
93+
c[0] = val;
94+
break;
95+
case GREEN:
96+
c[1] = val;
97+
break;
98+
case BLUE:
99+
c[2] = val;
100+
break;
101+
case ALPHA:
102+
c[3] = val;
103+
break;
104+
case SATURATION:
105+
int max = Math.max(Math.max(c[0], c[1]), c[2]);
106+
int min = max * (255 - val) / 255;
107+
if (c[0] == 0) c[0] = min;
108+
if (c[1] == 0) c[1] = min;
109+
if (c[2] == 0) c[2] = min;
110+
break;
111+
}
112+
}
113+
114+
private static Image calculateImage() {
115+
116+
int xvals[] = { 0, 255 };
117+
int yvals[] = { 0, 255 };
118+
int xmethod = RED;
119+
int ymethod = BLUE;
120+
int width = 256;
121+
int height = 256;
122+
int pixels[] = new int[width * height];
123+
int c[] = new int[4];
124+
int index = 0;
125+
for (int j = 0; j < height; j++) {
126+
for (int i = 0; i < width; i++) {
127+
c[0] = c[1] = c[2] = 0;
128+
c[3] = 255;
129+
if (xmethod < ymethod) {
130+
applymethod(c, xmethod, i, width, xvals);
131+
applymethod(c, ymethod, j, height, yvals);
132+
} else {
133+
applymethod(c, ymethod, j, height, yvals);
134+
applymethod(c, xmethod, i, width, xvals);
135+
}
136+
pixels[index++] = ((c[3] << 24) |
137+
(c[0] << 16) |
138+
(c[1] << 8) |
139+
(c[2] << 0));
140+
}
141+
}
142+
143+
DitherTest dt = new DitherTest();
144+
return dt.createImage(new MemoryImageSource(width, height, ColorModel.getRGBdefault(), pixels, 0, width));
145+
}
146+
}
147+
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright (c) 1999, 2024, 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 4213160
27+
* @summary Should generate a black image
28+
*/
29+
30+
import java.awt.Color;
31+
import java.awt.Graphics2D;
32+
import java.awt.image.BufferedImage;
33+
import java.awt.image.ColorModel;
34+
import java.awt.image.DataBuffer;
35+
import java.awt.image.DataBufferByte;
36+
import java.awt.image.IndexColorModel;
37+
import java.awt.image.Raster;
38+
import java.awt.image.WritableRaster;
39+
import java.awt.geom.AffineTransform;
40+
41+
public class MultiOp {
42+
43+
public static void main(String[] argv) {
44+
45+
int width = 256;
46+
int height = 256;
47+
48+
int pixelBits = 2; // 1, 2, 4, or 8
49+
// 1 and 8 make the code throw ImagingOpException, 2 and 4
50+
// make the code SEGV on Sol.
51+
52+
byte[] lut1Arr = new byte[] {0, (byte)255 };
53+
byte[] lut2Arr = new byte[] {0, (byte)85, (byte)170, (byte)255};
54+
byte[] lut4Arr = new byte[] {0, (byte)17, (byte)34, (byte)51,
55+
(byte)68, (byte)85,(byte) 102, (byte)119,
56+
(byte)136, (byte)153, (byte)170, (byte)187,
57+
(byte)204, (byte)221, (byte)238, (byte)255};
58+
byte[] lut8Arr = new byte[256];
59+
for (int i = 0; i < 256; i++) {
60+
lut8Arr[i] = (byte)i;
61+
}
62+
63+
// Create the binary image
64+
int bytesPerRow = width * pixelBits / 8;
65+
byte[] imageData = new byte[height * bytesPerRow];
66+
ColorModel cm = null;
67+
68+
switch (pixelBits) {
69+
case 1:
70+
cm = new IndexColorModel(pixelBits, lut1Arr.length,
71+
lut1Arr, lut1Arr, lut1Arr);
72+
break;
73+
case 2:
74+
cm = new IndexColorModel(pixelBits, lut2Arr.length,
75+
lut2Arr, lut2Arr, lut2Arr);
76+
break;
77+
case 4:
78+
cm = new IndexColorModel(pixelBits, lut4Arr.length,
79+
lut4Arr, lut4Arr, lut4Arr);
80+
break;
81+
case 8:
82+
cm = new IndexColorModel(pixelBits, lut8Arr.length,
83+
lut8Arr, lut8Arr, lut8Arr);
84+
break;
85+
default:
86+
{new Exception("Invalid # of bit per pixel").printStackTrace();}
87+
}
88+
89+
DataBuffer db = new DataBufferByte(imageData, imageData.length);
90+
WritableRaster r = Raster.createPackedRaster(db, width, height,
91+
pixelBits, null);
92+
BufferedImage srcImage = new BufferedImage(cm, r, false, null);
93+
94+
BufferedImage destImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
95+
Graphics2D g = destImage.createGraphics();
96+
AffineTransform af = AffineTransform.getScaleInstance(.5, .5);
97+
// This draw image is the problem
98+
g.drawImage(srcImage, af, null);
99+
int blackPixel = Color.black.getRGB();
100+
for (int x = 0; x < width; x++) {
101+
for (int y = 0; y < height; y++) {
102+
if (destImage.getRGB(x, y) != blackPixel) {
103+
throw new RuntimeException("Not black");
104+
}
105+
}
106+
}
107+
}
108+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (c) 2002, 2024, 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 4673490
27+
* @summary This test verifies that Toolkit images with a 1-bit
28+
* IndexColorModel (known as ByteBinary) and a transparent index are rendered properly.
29+
*/
30+
31+
import java.awt.Color;
32+
import java.awt.Graphics2D;
33+
34+
import java.awt.image.BufferedImage;
35+
import java.awt.image.IndexColorModel;
36+
37+
public class ByteBinaryBitmask {
38+
39+
public static void main(String argv[]) throws Exception {
40+
41+
/* Create the image */
42+
int w = 16, h = 16;
43+
byte[] bw = { (byte)255, (byte)0, };
44+
IndexColorModel icm = new IndexColorModel(1, 2, bw, bw, bw, 0);
45+
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY, icm);
46+
Graphics2D g2d = img.createGraphics();
47+
g2d.setColor(Color.white);
48+
g2d.fillRect(0, 0, w, h);
49+
g2d.setColor(Color.black);
50+
int xoff = 5;
51+
g2d.fillRect(xoff, 5, 1, 10); // 1 pixel wide
52+
53+
int dw = 200, dh = 50;
54+
BufferedImage dest = new BufferedImage(dw, dh, BufferedImage.TYPE_INT_RGB);
55+
Graphics2D g = dest.createGraphics();
56+
g.setColor(Color.green);
57+
g.fillRect(0, 0, dw, dh);
58+
int x1 = 10;
59+
int x2 = 50;
60+
int x3 = 90;
61+
int x4 = 130;
62+
g.drawImage(img, x1, 10, null);
63+
g.drawImage(img, x2, 10, null);
64+
g.drawImage(img, x3, 10, null);
65+
g.drawImage(img, x4, 10, null);
66+
67+
int blackPix = Color.black.getRGB();
68+
for (int y = 0; y < dh; y++) {
69+
boolean isBlack = false;
70+
for (int x = 0; x < dw; x++) {
71+
int rgb = dest.getRGB(x, y);
72+
if (rgb == blackPix) {
73+
/* Src image has a one pixel wide vertical rect at off "xoff" and
74+
* this is drawn at x1/x2/x3/x4) so the sum of those are the x locations
75+
* to expect black.
76+
*/
77+
if (x != (x1 + xoff) && x != (x2 + xoff) && x != (x3 + xoff) && x!= (x4 + xoff)) {
78+
throw new RuntimeException("wrong x location: " +x);
79+
}
80+
if (isBlack) {
81+
throw new RuntimeException("black after black");
82+
}
83+
}
84+
isBlack = rgb == blackPix;
85+
}
86+
}
87+
}
88+
}

0 commit comments

Comments
 (0)