DecoderTest.java (4353B)
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 static { 12 String jniLibrary = System.getProperty("org.jpeg.jpegxl.wrapper.lib"); 13 if (jniLibrary != null) { 14 try { 15 System.load(new java.io.File(jniLibrary).getAbsolutePath()); 16 } catch (UnsatisfiedLinkError ex) { 17 String message = 18 "If the nested exception message says that some standard library (stdc++, tcmalloc, etc.) was not found, " 19 + "it is likely that JDK discovered by the build system overrides library search path. " 20 + "Try specifying a different JDK via JAVA_HOME environment variable and doing a clean build."; 21 throw new RuntimeException(message, ex); 22 } 23 } 24 } 25 26 private static final int SIMPLE_IMAGE_DIM = 1024; 27 // Base64: "/wr6H0GRCAYBAGAASzgkunkeVbaSBu95EXDn0e7ABz2ShAMA" 28 private static final byte[] SIMPLE_IMAGE_BYTES = {-1, 10, -6, 31, 65, -111, 8, 6, 1, 0, 96, 0, 75, 29 56, 36, -70, 121, 30, 85, -74, -110, 6, -17, 121, 17, 112, -25, -47, -18, -64, 7, 61, -110, 30 -124, 3, 0}; 31 32 private static final int PIXEL_IMAGE_DIM = 1; 33 // Base64: "/woAELASCBAQABwASxLFgoUkDA==" 34 private static final byte[] PIXEL_IMAGE_BYTES = { 35 -1, 10, 0, 16, -80, 18, 8, 16, 16, 0, 28, 0, 75, 18, -59, -126, -123, 36, 12}; 36 37 static ByteBuffer makeByteBuffer(byte[] src, int length) { 38 ByteBuffer buffer = ByteBuffer.allocateDirect(length); 39 buffer.put(src, 0, length); 40 return buffer; 41 } 42 43 static ByteBuffer makeSimpleImage() { 44 return makeByteBuffer(SIMPLE_IMAGE_BYTES, SIMPLE_IMAGE_BYTES.length); 45 } 46 47 static void checkSimpleImageData(ImageData imageData) { 48 if (imageData.width != SIMPLE_IMAGE_DIM) { 49 throw new IllegalStateException("invalid width"); 50 } 51 if (imageData.height != SIMPLE_IMAGE_DIM) { 52 throw new IllegalStateException("invalid height"); 53 } 54 int iccSize = imageData.icc.capacity(); 55 // Do not expect ICC profile to be some exact size; currently it is 732 56 if (iccSize < 300 || iccSize > 1000) { 57 throw new IllegalStateException("unexpected ICC profile size"); 58 } 59 } 60 61 static void checkPixelFormat(PixelFormat pixelFormat, int bytesPerPixel) { 62 ImageData imageData = Decoder.decode(makeSimpleImage(), pixelFormat); 63 checkSimpleImageData(imageData); 64 if (imageData.pixels.limit() != SIMPLE_IMAGE_DIM * SIMPLE_IMAGE_DIM * bytesPerPixel) { 65 throw new IllegalStateException("Unexpected pixels size"); 66 } 67 } 68 69 static void testRgba() { 70 checkPixelFormat(PixelFormat.RGBA_8888, 4); 71 } 72 73 static void testRgbaF16() { 74 checkPixelFormat(PixelFormat.RGBA_F16, 8); 75 } 76 77 static void testRgb() { 78 checkPixelFormat(PixelFormat.RGB_888, 3); 79 } 80 81 static void testRgbF16() { 82 checkPixelFormat(PixelFormat.RGB_F16, 6); 83 } 84 85 static void checkGetInfo(ByteBuffer data, int dim, int alphaBits) { 86 StreamInfo streamInfo = Decoder.decodeInfo(data); 87 if (streamInfo.status != Status.OK) { 88 throw new IllegalStateException("Unexpected decoding error"); 89 } 90 if (streamInfo.width != dim || streamInfo.height != dim) { 91 throw new IllegalStateException("Invalid width / height"); 92 } 93 if (streamInfo.alphaBits != alphaBits) { 94 throw new IllegalStateException("Invalid alphaBits"); 95 } 96 } 97 98 static void testGetInfoNoAlpha() { 99 checkGetInfo(makeSimpleImage(), SIMPLE_IMAGE_DIM, 0); 100 } 101 102 static void testGetInfoAlpha() { 103 checkGetInfo(makeByteBuffer(PIXEL_IMAGE_BYTES, PIXEL_IMAGE_BYTES.length), PIXEL_IMAGE_DIM, 8); 104 } 105 106 static void testNotEnoughInput() { 107 for (int i = 0; i < 6; ++i) { 108 ByteBuffer jxlData = makeByteBuffer(SIMPLE_IMAGE_BYTES, i); 109 StreamInfo streamInfo = Decoder.decodeInfo(jxlData); 110 if (streamInfo.status != Status.NOT_ENOUGH_INPUT) { 111 throw new IllegalStateException( 112 "Expected 'not enough input', but got " + streamInfo.status + " " + i); 113 } 114 } 115 } 116 117 // Simple executable to avoid extra dependencies. 118 public static void main(String[] args) { 119 testRgba(); 120 testRgbaF16(); 121 testRgb(); 122 testRgbF16(); 123 testGetInfoNoAlpha(); 124 testGetInfoAlpha(); 125 testNotEnoughInput(); 126 } 127 }