Skip to content

Commit b92ce0f

Browse files
committed
add tests for Image.convertImageFrame
1 parent be1c67a commit b92ce0f

File tree

3 files changed

+248
-5
lines changed

3 files changed

+248
-5
lines changed

modules/javafx.graphics/src/main/java/com/sun/javafx/iio/ImageFrame.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public final class ImageFrame {
5050
* @param width The image width.
5151
* @param height The image height.
5252
* @param stride The stride from a pixel position in one row to the same
53-
* horizontal position in the next row.
53+
* horizontal position in the next row, in bytes
5454
* @param palette The image palette. This is ignored unless the type is
5555
* one of the palette types.
5656
* @param metadata The image metadata.
@@ -73,7 +73,7 @@ public ImageFrame(ImageType imageType, Buffer imageData,
7373
* @param width The image width.
7474
* @param height The image height.
7575
* @param stride The stride from a pixel position in one row to the same
76-
* horizontal position in the next row.
76+
* horizontal position in the next row, in bytes
7777
* @param palette The image palette. This is ignored unless the type is
7878
* one of the palette types.
7979
* @param pixelScale The scale of a 72DPI virtual pixel in the resolution

modules/javafx.graphics/src/main/java/com/sun/prism/Image.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -284,19 +284,19 @@ public static Image convertImageFrame(ImageFrame frame) {
284284

285285
case INT_RGB -> {
286286
IntBuffer imageData = (IntBuffer)frame.getImageData();
287-
IntRgb.ToIntArgbPreConverter().convert(imageData, 0, stride, imageData, 0, stride, w, h);
287+
IntRgb.ToIntArgbPreConverter().convert(imageData, 0, stride / 4, imageData, 0, stride / 4, w, h);
288288
yield fromIntArgbPreData(imageData, w, h, stride, ps);
289289
}
290290

291291
case INT_BGR -> {
292292
IntBuffer imageData = (IntBuffer)frame.getImageData();
293-
IntBgr.ToIntArgbPreConverter().convert(imageData, 0, stride, imageData, 0, stride, w, h);
293+
IntBgr.ToIntArgbPreConverter().convert(imageData, 0, stride / 4, imageData, 0, stride / 4, w, h);
294294
yield fromIntArgbPreData(imageData, w, h, stride, ps);
295295
}
296296

297297
case INT_ARGB -> {
298298
IntBuffer imageData = (IntBuffer)frame.getImageData();
299-
IntArgb.ToIntArgbPreConverter().convert(imageData, 0, stride, imageData, 0, stride, w, h);
299+
IntArgb.ToIntArgbPreConverter().convert(imageData, 0, stride / 4, imageData, 0, stride / 4, w, h);
300300
yield fromIntArgbPreData(imageData, w, h, stride, ps);
301301
}
302302

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
/*
2+
* Copyright (c) 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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package test.com.sun.prism;
27+
28+
import com.sun.javafx.iio.ImageFrame;
29+
import com.sun.javafx.iio.ImageMetadata;
30+
import com.sun.javafx.iio.ImageStorage.ImageType;
31+
import com.sun.prism.Image;
32+
import com.sun.prism.PixelFormat;
33+
import org.junit.jupiter.params.ParameterizedTest;
34+
import org.junit.jupiter.params.provider.EnumSource;
35+
import java.nio.Buffer;
36+
import java.nio.ByteBuffer;
37+
import java.nio.IntBuffer;
38+
39+
import static org.junit.jupiter.api.Assertions.*;
40+
41+
public class ImageTest {
42+
43+
/**
44+
* All supported {@link ImageType} to {@link PixelFormat} conversions.
45+
*/
46+
enum SupportedConversions {
47+
GRAY(ImageType.GRAY, PixelFormat.BYTE_GRAY, 2,
48+
new byte[] { 1, 2, 3, 4 },
49+
new byte[] { 1, 2, 3, 4 }),
50+
51+
GRAY_ALPHA(ImageType.GRAY_ALPHA, PixelFormat.BYTE_BGRA_PRE, 4,
52+
new byte[] {
53+
100, 127, 50, 127,
54+
100, 0, 50, 0
55+
},
56+
new byte[] {
57+
50, 50, 50, 127, 25, 25, 25, 127,
58+
0, 0, 0, 0, 0, 0, 0, 0
59+
}),
60+
61+
GRAY_ALPHA_PRE(ImageType.GRAY_ALPHA_PRE, PixelFormat.BYTE_BGRA_PRE, 4,
62+
new byte[] {
63+
100, 127, 50, 127,
64+
100, 0, 50, 0
65+
},
66+
new byte[] {
67+
100, 100, 100, 127, 50, 50, 50, 127,
68+
100, 100, 100, 0, 50, 50, 50, 0
69+
}),
70+
71+
RGB(ImageType.RGB, PixelFormat.BYTE_RGB, 6,
72+
new byte[] {
73+
1, 2, 3, 4, 5, 6,
74+
7, 8, 9, 10, 11, 12
75+
},
76+
new byte[] {
77+
1, 2, 3, 4, 5, 6,
78+
7, 8, 9, 10, 11, 12
79+
}),
80+
81+
BGR(ImageType.BGR, PixelFormat.BYTE_RGB, 6,
82+
new byte[] {
83+
1, 2, 3, 4, 5, 6,
84+
7, 8, 9, 10, 11, 12
85+
},
86+
new byte[] {
87+
3, 2, 1, 6, 5, 4,
88+
9, 8, 7, 12, 11, 10
89+
}),
90+
91+
RGBA(ImageType.RGBA, PixelFormat.BYTE_BGRA_PRE, 8,
92+
new byte[] {
93+
100, 0, 0, 127, 0, 50, 0, 127,
94+
(byte)255, 127, 0, 127, 50, 60, 70, (byte)255
95+
},
96+
new byte[] {
97+
0, 0, 50, 127, 0, 25, 0, 127,
98+
0, 63, 127, 127, 70, 60, 50, (byte)255
99+
}),
100+
101+
RGBA_PRE(ImageType.RGBA_PRE, PixelFormat.BYTE_BGRA_PRE, 8,
102+
new byte[] {
103+
100, 0, 0, 127, 0, 50, 0, 127,
104+
(byte)255, 127, 0, 127, 50, 40, 30, 0
105+
},
106+
new byte[] {
107+
0, 0, 100, 127, 0, 50, 0, 127,
108+
0, 127, (byte)255, 127, 30, 40, 50, 0
109+
}),
110+
111+
BGRA(ImageType.BGRA, PixelFormat.BYTE_BGRA_PRE, 8,
112+
new byte[] {
113+
100, 0, 0, 127, 0, 50, 0, 127,
114+
(byte)255, 127, 0, 127, 50, 50, 50, 0
115+
},
116+
new byte[] {
117+
50, 0, 0, 127, 0, 25, 0, 127,
118+
127, 63, 0, 127, 0, 0, 0, 0
119+
}),
120+
121+
BGRA_PRE(ImageType.BGRA_PRE, PixelFormat.BYTE_BGRA_PRE, 8,
122+
new byte[] {
123+
100, 0, 0, 127, 0, 50, 0, 127,
124+
(byte)255, 127, 0, 127, 50, 50, 50, 0
125+
},
126+
new byte[] {
127+
100, 0, 0, 127, 0, 50, 0, 127,
128+
(byte)255, 127, 0, 127, 50, 50, 50, 0
129+
}),
130+
131+
ABGR(ImageType.ABGR, PixelFormat.BYTE_BGRA_PRE, 8,
132+
new byte[] {
133+
127, 100, 0, 0, 127, 50, 0, 100,
134+
127, (byte)255, 127, 0, 0, 50, 50, 50
135+
},
136+
new byte[] {
137+
50, 0, 0, 127, 25, 0, 50, 127,
138+
127, 63, 0, 127, 0, 0, 0, 0
139+
}),
140+
141+
ABGR_PRE(ImageType.ABGR_PRE, PixelFormat.BYTE_BGRA_PRE, 8,
142+
new byte[] {
143+
127, 100, 0, 0, 127, 50, 0, 100,
144+
127, (byte)255, 127, 0, 0, 50, 50, 50
145+
},
146+
new byte[] {
147+
100, 0, 0, 127, 50, 0, 100, 127,
148+
(byte)255, 127, 0, 127, 50, 50, 50, 0
149+
}),
150+
151+
INT_RGB(ImageType.INT_RGB, PixelFormat.INT_ARGB_PRE, 8,
152+
new int[] {
153+
rgb(50, 100, 150), rgb(10, 20, 30),
154+
rgb(40, 50, 60), rgb(255, 255, 255)
155+
},
156+
new int[] {
157+
argb(255, 50, 100, 150), argb(255, 10, 20, 30),
158+
argb(255, 40, 50, 60), argb(255, 255, 255, 255)
159+
}),
160+
161+
INT_BGR(ImageType.INT_BGR, PixelFormat.INT_ARGB_PRE, 8,
162+
new int[] {
163+
rgb(50, 100, 150), rgb(10, 20, 30),
164+
rgb(40, 50, 60), rgb(255, 255, 255)
165+
},
166+
new int[] {
167+
argb(255, 150, 100, 50), argb(255, 30, 20, 10),
168+
argb(255, 60, 50, 40), argb(255, 255, 255, 255)
169+
}),
170+
171+
INT_ARGB(ImageType.INT_ARGB, PixelFormat.INT_ARGB_PRE, 8,
172+
new int[] {
173+
argb(127, 50, 100, 150), argb(127, 10, 20, 30),
174+
argb(255, 40, 50, 60), argb(0, 255, 255, 255)
175+
},
176+
new int[] {
177+
argb(127, 25, 50, 75), argb(127, 5, 10, 15),
178+
argb(255, 40, 50, 60), argb(0, 0, 0, 0)
179+
}),
180+
181+
INT_ARGB_PRE(ImageType.INT_ARGB_PRE, PixelFormat.INT_ARGB_PRE, 8,
182+
new int[] {
183+
argb(127, 50, 100, 150), argb(127, 10, 20, 30),
184+
argb(255, 0, 0, 0), argb(0, 255, 255, 255)
185+
},
186+
new int[] {
187+
argb(127, 50, 100, 150), argb(127, 10, 20, 30),
188+
argb(255, 0, 0, 0), argb(0, 255, 255, 255)
189+
});
190+
191+
static int rgb(int r, int g, int b) {
192+
return 255 << 24 | (r & 0xff) << 16 | (g & 0xff) << 8 | (b & 0xff);
193+
}
194+
195+
static int argb(int a, int r, int g, int b) {
196+
return a << 24 | (r & 0xff) << 16 | (g & 0xff) << 8 | (b & 0xff);
197+
}
198+
199+
SupportedConversions(ImageType sourceType, PixelFormat targetType, int stride,
200+
byte[] sourceData, byte[] targetData) {
201+
this(sourceType, targetType, stride, ByteBuffer.wrap(sourceData), ByteBuffer.wrap(targetData));
202+
}
203+
204+
SupportedConversions(ImageType sourceType, PixelFormat targetType, int stride,
205+
int[] sourceData, int[] targetData) {
206+
this(sourceType, targetType, stride, IntBuffer.wrap(sourceData), IntBuffer.wrap(targetData));
207+
}
208+
209+
SupportedConversions(ImageType sourceType, PixelFormat targetType, int stride,
210+
Buffer sourceData, Buffer targetData) {
211+
this.sourceType = sourceType;
212+
this.targetType = targetType;
213+
this.sourceData = sourceData;
214+
this.targetData = targetData;
215+
this.stride = stride;
216+
}
217+
218+
final ImageType sourceType;
219+
final PixelFormat targetType;
220+
final Buffer sourceData;
221+
final Buffer targetData;
222+
final int stride;
223+
}
224+
225+
/**
226+
* Asserts that the content of an {@link ImageFrame} is correctly converted to {@link Image}
227+
* for all supported image formats.
228+
*/
229+
@ParameterizedTest
230+
@EnumSource(SupportedConversions.class)
231+
void convertImageFrame(SupportedConversions conversion) {
232+
var imageFrame = new ImageFrame(
233+
conversion.sourceType, conversion.sourceData, 2, 2, conversion.stride, null,
234+
new ImageMetadata(null, null, null, null, null, null, null, 2, 2, null, null, null));
235+
236+
var image = Image.convertImageFrame(imageFrame);
237+
238+
assertEquals(2, image.getWidth());
239+
assertEquals(2, image.getHeight());
240+
assertEquals(conversion.targetType, image.getPixelFormat());
241+
assertEquals(conversion.targetData, image.getPixelBuffer());
242+
}
243+
}

0 commit comments

Comments
 (0)