-
Notifications
You must be signed in to change notification settings - Fork 1
/
DecoderTest.java
264 lines (227 loc) · 9.01 KB
/
DecoderTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package org.jpeg.jpegxl.wrapper;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.concurrent.CountDownLatch;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import vavi.util.Debug;
import vavi.util.properties.annotation.Property;
import vavi.util.properties.annotation.PropsEntity;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* JNI test.
*/
@PropsEntity(url = "file:local.properties")
public class DecoderTest {
static boolean localPropertiesExists() {
return Files.exists(Paths.get("local.properties"));
}
@Property(name = "file")
String file = "src/test/resources/test.jxl";
@BeforeEach
void setup() throws IOException {
if (localPropertiesExists()) {
PropsEntity.Util.bind(this);
}
}
private static final int SIMPLE_IMAGE_DIM = 1024;
// Base64: "/wr6H0GRCAYBAGAASzgkunkeVbaSBu95EXDn0e7ABz2ShAMA"
private static final byte[] SIMPLE_IMAGE_BYTES = {-1, 10, -6, 31, 65, -111, 8, 6, 1, 0, 96, 0, 75,
56, 36, -70, 121, 30, 85, -74, -110, 6, -17, 121, 17, 112, -25, -47, -18, -64, 7, 61, -110,
-124, 3, 0};
private static final int PIXEL_IMAGE_DIM = 1;
// Base64: "/woAELASCBAQABwASxLFgoUkDA=="
private static final byte[] PIXEL_IMAGE_BYTES = {
-1, 10, 0, 16, -80, 18, 8, 16, 16, 0, 28, 0, 75, 18, -59, -126, -123, 36, 12};
static ByteBuffer makeByteBuffer(byte[] src, int length) {
ByteBuffer buffer = ByteBuffer.allocateDirect(length);
buffer.put(src, 0, length);
return buffer;
}
static ByteBuffer makeSimpleImage() {
return makeByteBuffer(SIMPLE_IMAGE_BYTES, SIMPLE_IMAGE_BYTES.length);
}
static void checkSimpleImageData(ImageData imageData) {
assertEquals(imageData.width, SIMPLE_IMAGE_DIM, "invalid width");
assertEquals(imageData.height, SIMPLE_IMAGE_DIM, "invalid height");
int iccSize = imageData.icc.capacity();
// Do not expect ICC profile to be some exact size; currently it is 732
assertTrue(iccSize >= 300 && iccSize <= 1000, "unexpected ICC profile size");
}
static void checkPixelFormat(PixelFormat pixelFormat, int bytesPerPixel) {
ImageData imageData = Decoder.decode(makeSimpleImage(), pixelFormat);
checkSimpleImageData(imageData);
assertEquals(imageData.pixels.limit(), SIMPLE_IMAGE_DIM * SIMPLE_IMAGE_DIM * bytesPerPixel, "Unexpected pixels size");
}
static void testRgba() {
checkPixelFormat(PixelFormat.RGBA_8888, 4);
}
static void testRgbaF16() {
checkPixelFormat(PixelFormat.RGBA_F16, 8);
}
static void testRgb() {
checkPixelFormat(PixelFormat.RGB_888, 3);
}
static void testRgbF16() {
checkPixelFormat(PixelFormat.RGB_F16, 6);
}
static void checkGetInfo(ByteBuffer data, int dim, int alphaBits) {
StreamInfo streamInfo = Decoder.decodeInfo(data);
assertSame(streamInfo.status, Status.OK, "Unexpected decoding error");
assertTrue(streamInfo.width == dim && streamInfo.height == dim, "Invalid width / height");
assertEquals(streamInfo.alphaBits, alphaBits, "Invalid alphaBits");
}
static void testGetInfoNoAlpha() {
checkGetInfo(makeSimpleImage(), SIMPLE_IMAGE_DIM, 0);
}
static void testGetInfoAlpha() {
checkGetInfo(makeByteBuffer(PIXEL_IMAGE_BYTES, PIXEL_IMAGE_BYTES.length), PIXEL_IMAGE_DIM, 8);
}
static void testNotEnoughInput() {
for (int i = 0; i < 6; ++i) {
ByteBuffer jxlData = makeByteBuffer(SIMPLE_IMAGE_BYTES, i);
StreamInfo streamInfo = Decoder.decodeInfo(jxlData);
assertSame(streamInfo.status, Status.NOT_ENOUGH_INPUT,
"Expected 'not enough input', but got " + streamInfo.status + " " + i);
}
}
// Simple executable to avoid extra dependencies.
@Test
void testBulk() {
testRgba();
testRgbaF16();
testRgb();
testRgbF16();
testGetInfoNoAlpha();
testGetInfoAlpha();
testNotEnoughInput();
}
/** direct */
private BufferedImage getImage() throws Exception {
byte[] jxl = Files.readAllBytes(Paths.get(file));
ByteBuffer bb = ByteBuffer.allocateDirect(jxl.length);
bb.put(jxl);
ImageData imageData = Decoder.decode(bb, PixelFormat.RGBA_8888);
Debug.println("image: " + imageData.width + "x" + imageData.height);
Debug.println("decoded: " + imageData.pixels.capacity());
BufferedImage image = new BufferedImage(imageData.width, imageData.height, BufferedImage.TYPE_4BYTE_ABGR);
ByteBuffer decoded = (ByteBuffer) imageData.pixels;
byte[] raster = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
Debug.println("raster: " + raster.length);
// decoded.get(raster);
// TODO i don't wanna do this.
// but using JXL_BIG_ENDIAN at decoder_jni.cc#ToPixelFormat doesn't work (nothing changes)
int p = 0;
for (int y = 0; y < imageData.height; y++) {
for (int x = 0; x < imageData.width; x++) {
raster[p + 3] = decoded.get();
raster[p + 2] = decoded.get();
raster[p + 1] = decoded.get();
raster[p + 0] = decoded.get();
p += 4;
}
}
return image;
}
/** direct gui */
public static void main(String[] args) throws Exception {
DecoderTest app = new DecoderTest();
app.setup();
app.show(app.getImage());
}
/** using cdl cause junit stops awt thread suddenly */
private void show(BufferedImage image) throws Exception {
CountDownLatch cdl = new CountDownLatch(1);
JFrame frame = new JFrame();
JPanel panel = new JPanel() {
public void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, this);
}
};
frame.addWindowListener(new WindowAdapter() {
@Override public void windowClosing(WindowEvent e) { cdl.countDown(); }
});
panel.setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
frame.setContentPane(new JScrollPane(panel));
frame.setTitle("JPEG XL (JNI)");
frame.pack();
frame.setVisible(true);
cdl.await();
}
@Test
@DisplayName("direct")
@EnabledIfSystemProperty(named = "vavi.test", matches = "ide")
void test0() throws Exception {
Debug.println(System.getProperty("os.arch"));
BufferedImage image = getImage();
assertNotNull(image);
show(image);
}
@Test
@DisplayName("direct")
void test1() throws Exception {
BufferedImage image = getImage();
assertNotNull(image);
}
@Test
@Disabled("currently jna version is used")
@DisplayName("spi specified")
void test02() throws Exception {
ImageReader ir = ImageIO.getImageReadersByFormatName("jpegxl").next();
ImageInputStream iis = ImageIO.createImageInputStream(Files.newInputStream(Paths.get(file)));
ir.setInput(iis);
BufferedImage image = ir.read(0);
assertNotNull(image);
}
@Test
@Disabled("currently jna version is used")
@DisplayName("spi auto")
void test03() throws Exception {
BufferedImage image = ImageIO.read(new File(file));
assertNotNull(image);
}
@Test
@Disabled("currently jna version is used")
@DisplayName("spi specified gui")
@EnabledIfSystemProperty(named = "vavi.test", matches = "ide")
void test2() throws Exception {
ImageReader ir = ImageIO.getImageReadersByFormatName("jpegxl").next();
ImageInputStream iis = ImageIO.createImageInputStream(Files.newInputStream(Paths.get(file)));
ir.setInput(iis);
BufferedImage image = ir.read(0);
show(image);
}
@Test
@Disabled("currently jna version is used")
@DisplayName("spi auto gui")
@EnabledIfSystemProperty(named = "vavi.test", matches = "ide")
void test3() throws Exception {
BufferedImage image = ImageIO.read(new File(file));
show(image);
}
}