| 1 |
// Copyright (c) the JPEG XL Project Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Use of this source code is governed by a BSD-style |
| 4 |
// license that can be found in the LICENSE file. |
| 5 |
|
| 6 |
package org.jpeg.jpegxl.wrapper; |
| 7 |
|
| 8 |
import java.nio.ByteBuffer; |
| 9 |
|
| 10 |
public class DecoderTest { |
| 11 |
private static final int SIMPLE_IMAGE_DIM = 1024; |
| 12 |
// Base64: "/wr6H0GRCAYBAGAASzgkunkeVbaSBu95EXDn0e7ABz2ShAMA" |
| 13 |
private static final byte[] SIMPLE_IMAGE_BYTES = {-1, 10, -6, 31, 65, -111, 8, 6, 1, 0, 96, 0, 75, |
| 14 |
56, 36, -70, 121, 30, 85, -74, -110, 6, -17, 121, 17, 112, -25, -47, -18, -64, 7, 61, -110, |
| 15 |
-124, 3, 0}; |
| 16 |
|
| 17 |
private static final int PIXEL_IMAGE_DIM = 1; |
| 18 |
// Base64: "/woAELASCBAQABwASxLFgoUkDA==" |
| 19 |
private static final byte[] PIXEL_IMAGE_BYTES = { |
| 20 |
-1, 10, 0, 16, -80, 18, 8, 16, 16, 0, 28, 0, 75, 18, -59, -126, -123, 36, 12}; |
| 21 |
|
| 22 |
static ByteBuffer makeByteBuffer(byte[] src, int length) { |
| 23 |
ByteBuffer buffer = ByteBuffer.allocateDirect(length); |
| 24 |
buffer.put(src, 0, length); |
| 25 |
return buffer; |
| 26 |
} |
| 27 |
|
| 28 |
static ByteBuffer makeSimpleImage() { |
| 29 |
return makeByteBuffer(SIMPLE_IMAGE_BYTES, SIMPLE_IMAGE_BYTES.length); |
| 30 |
} |
| 31 |
|
| 32 |
static void checkSimpleImageData(ImageData imageData) { |
| 33 |
if (imageData.width != SIMPLE_IMAGE_DIM) { |
| 34 |
throw new IllegalStateException("invalid width"); |
| 35 |
} |
| 36 |
if (imageData.height != SIMPLE_IMAGE_DIM) { |
| 37 |
throw new IllegalStateException("invalid height"); |
| 38 |
} |
| 39 |
int iccSize = imageData.icc.capacity(); |
| 40 |
// Do not expect ICC profile to be some exact size; currently it is 732 |
| 41 |
if (iccSize < 300 || iccSize > 1000) { |
| 42 |
throw new IllegalStateException("unexpected ICC profile size"); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
static void checkPixelFormat(PixelFormat pixelFormat, int bytesPerPixel) { |
| 47 |
ImageData imageData = Decoder.decode(makeSimpleImage(), pixelFormat); |
| 48 |
checkSimpleImageData(imageData); |
| 49 |
if (imageData.pixels.limit() != SIMPLE_IMAGE_DIM * SIMPLE_IMAGE_DIM * bytesPerPixel) { |
| 50 |
throw new IllegalStateException("Unexpected pixels size"); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
static void testRgba() { |
| 55 |
checkPixelFormat(PixelFormat.RGBA_8888, 4); |
| 56 |
} |
| 57 |
|
| 58 |
static void testRgbaF16() { |
| 59 |
checkPixelFormat(PixelFormat.RGBA_F16, 8); |
| 60 |
} |
| 61 |
|
| 62 |
static void testRgb() { |
| 63 |
checkPixelFormat(PixelFormat.RGB_888, 3); |
| 64 |
} |
| 65 |
|
| 66 |
static void testRgbF16() { |
| 67 |
checkPixelFormat(PixelFormat.RGB_F16, 6); |
| 68 |
} |
| 69 |
|
| 70 |
static void checkGetInfo(ByteBuffer data, int dim, int alphaBits) { |
| 71 |
StreamInfo streamInfo = Decoder.decodeInfo(data); |
| 72 |
if (streamInfo.status != Status.OK) { |
| 73 |
throw new IllegalStateException("Unexpected decoding error"); |
| 74 |
} |
| 75 |
if (streamInfo.width != dim || streamInfo.height != dim) { |
| 76 |
throw new IllegalStateException("Invalid width / height"); |
| 77 |
} |
| 78 |
if (streamInfo.alphaBits != alphaBits) { |
| 79 |
throw new IllegalStateException("Invalid alphaBits"); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
static void testGetInfoNoAlpha() { |
| 84 |
checkGetInfo(makeSimpleImage(), SIMPLE_IMAGE_DIM, 0); |
| 85 |
} |
| 86 |
|
| 87 |
static void testGetInfoAlpha() { |
| 88 |
checkGetInfo(makeByteBuffer(PIXEL_IMAGE_BYTES, PIXEL_IMAGE_BYTES.length), PIXEL_IMAGE_DIM, 8); |
| 89 |
} |
| 90 |
|
| 91 |
static void testNotEnoughInput() { |
| 92 |
for (int i = 0; i < 6; ++i) { |
| 93 |
ByteBuffer jxlData = makeByteBuffer(SIMPLE_IMAGE_BYTES, i); |
| 94 |
StreamInfo streamInfo = Decoder.decodeInfo(jxlData); |
| 95 |
if (streamInfo.status != Status.NOT_ENOUGH_INPUT) { |
| 96 |
throw new IllegalStateException( |
| 97 |
"Expected 'not enough input', but got " + streamInfo.status + " " + i); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
// Simple executable to avoid extra dependencies. |
| 103 |
public static void main(String[] args) { |
| 104 |
testRgba(); |
| 105 |
testRgbaF16(); |
| 106 |
testRgb(); |
| 107 |
testRgbF16(); |
| 108 |
testGetInfoNoAlpha(); |
| 109 |
testGetInfoAlpha(); |
| 110 |
testNotEnoughInput(); |
| 111 |
} |
| 112 |
} |