Skip to content

Commit

Permalink
fix(jxl): fix for signature change in 0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
gotson committed Jan 29, 2024
1 parent 2565f8a commit b3a5225
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ public class JpegXl {

private static final ValueLayout.OfInt pixelLayout = C_INT.withOrder(ByteOrder.BIG_ENDIAN);

private static int[] version;

private static void getLibVersionInt() {
int versionInt = decode_h.JxlDecoderVersion();
int major = versionInt / 1000000;
int minor = (versionInt - major * 1000000) / 1000;
int patch = versionInt - major * 1000000 - minor * 1000;
version = new int[] {major, minor, patch};
}

public static boolean canDecode(final ImageInputStream stream) throws JxlException {
try (var arena = Arena.ofConfined()) {
stream.mark();
Expand All @@ -55,11 +65,13 @@ public static boolean canDecode(final ImageInputStream stream) throws JxlExcepti
}

public static String getLibVersion() {
int versionInt = decode_h.JxlDecoderVersion();
int major = versionInt / 1000000;
int minor = (versionInt - major * 1000000) / 1000;
int patch = versionInt - major * 1000000 - minor * 1000;
return String.format("%d.%d.%d", major, minor, patch);
if (version == null) getLibVersionInt();
return String.format("%d.%d.%d", version[0], version[1], version[2]);
}

private static boolean legacyIcc() {
if (version == null) getLibVersionInt();
return version[0] == 0 && version[1] < 9;
}

public static BasicInfo getBasicInfo(final ImageInputStream stream) throws JxlException {
Expand Down Expand Up @@ -109,17 +121,31 @@ public static BasicInfo getBasicInfo(final ImageInputStream stream) throws JxlEx
} else if (status == JxlDecoderStatus.JXL_DEC_COLOR_ENCODING) {
// Get the ICC color profile of the pixel data
var iccSize = arena.allocate(C_LONG, 0);
int iccSizeResult;
if (legacyIcc()) {
iccSizeResult = decode_h.JxlDecoderGetICCProfileSizeLegacy(dec, format, JxlColorProfileTarget.JXL_COLOR_PROFILE_TARGET_DATA.intValue(), iccSize);
} else {
iccSizeResult = decode_h.JxlDecoderGetICCProfileSize(dec, JxlColorProfileTarget.JXL_COLOR_PROFILE_TARGET_DATA.intValue(), iccSize);
}
if (JxlDecoderStatus.JXL_DEC_SUCCESS != JxlDecoderStatus.fromId(
decode_h.JxlDecoderGetICCProfileSize(dec, format, JxlColorProfileTarget.JXL_COLOR_PROFILE_TARGET_DATA.intValue(), iccSize))) {
iccSizeResult)) {
throw new JxlException("JxlDecoderGetICCProfileSize failed");
}

iccProfile = ByteBuffer.allocateDirect(iccSize.get(C_INT, 0));
int iccProfileResult;
if (legacyIcc()) {
iccProfileResult = decode_h.JxlDecoderGetColorAsICCProfileLegacy(dec, format, JxlColorProfileTarget.JXL_COLOR_PROFILE_TARGET_DATA.intValue(),
MemorySegment.ofBuffer(iccProfile),
iccSize.get(C_LONG, 0));
} else {
iccProfileResult = decode_h.JxlDecoderGetColorAsICCProfile(dec, JxlColorProfileTarget.JXL_COLOR_PROFILE_TARGET_DATA.intValue(),
MemorySegment.ofBuffer(iccProfile),
iccSize.get(C_LONG, 0));
}
if (JxlDecoderStatus.JXL_DEC_SUCCESS !=
JxlDecoderStatus.fromId(
decode_h.JxlDecoderGetColorAsICCProfile(dec, format, JxlColorProfileTarget.JXL_COLOR_PROFILE_TARGET_DATA.intValue(),
MemorySegment.ofBuffer(iccProfile),
iccSize.get(C_LONG, 0)))) {
iccProfileResult)) {
throw new JxlException("JxlDecoderGetColorAsICCProfile failed");
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ final class constants$21 {
JAVA_INT,
RuntimeHelper.POINTER
);

static final FunctionDescriptor const$2a = FunctionDescriptor.of(JAVA_INT,
RuntimeHelper.POINTER,
JAVA_INT,
RuntimeHelper.POINTER
);
static final MethodHandle const$3 = RuntimeHelper.downcallHandle(
"JxlDecoderGetColorAsEncodedProfile",
constants$21.const$2
Expand All @@ -38,17 +44,33 @@ final class constants$21 {
"JxlDecoderGetICCProfileSize",
constants$21.const$2
);

static final MethodHandle const$4a = RuntimeHelper.downcallHandle(
"JxlDecoderGetICCProfileSize",
constants$21.const$2a
);

static final FunctionDescriptor const$5 = FunctionDescriptor.of(JAVA_INT,
RuntimeHelper.POINTER,
RuntimeHelper.POINTER,
JAVA_INT,
RuntimeHelper.POINTER,
JAVA_LONG
);
static final FunctionDescriptor const$5a = FunctionDescriptor.of(JAVA_INT,
RuntimeHelper.POINTER,
JAVA_INT,
RuntimeHelper.POINTER,
JAVA_LONG
);
static final MethodHandle const$6 = RuntimeHelper.downcallHandle(
"JxlDecoderGetColorAsICCProfile",
constants$21.const$5
);
static final MethodHandle const$6a = RuntimeHelper.downcallHandle(
"JxlDecoderGetColorAsICCProfile",
constants$21.const$5a
);
}


Original file line number Diff line number Diff line change
Expand Up @@ -1865,42 +1865,68 @@ public static int JxlDecoderGetColorAsEncodedProfile(MemorySegment dec, MemorySe
}
}

public static MethodHandle JxlDecoderGetICCProfileSize$MH() {
public static MethodHandle JxlDecoderGetICCProfileSizeLegacy$MH() {
return RuntimeHelper.requireNonNull(constants$21.const$4, "JxlDecoderGetICCProfileSize");
}

public static MethodHandle JxlDecoderGetICCProfileSize$MH() {
return RuntimeHelper.requireNonNull(constants$21.const$4a, "JxlDecoderGetICCProfileSize");
}

/**
* {@snippet :
* JxlDecoderStatus JxlDecoderGetICCProfileSize(const JxlDecoder* dec, const JxlPixelFormat* format, JxlColorProfileTarget target, size_t* size);
*}
*/
public static int JxlDecoderGetICCProfileSize(MemorySegment dec, MemorySegment format, int target, MemorySegment size) {
var mh$ = JxlDecoderGetICCProfileSize$MH();
public static int JxlDecoderGetICCProfileSizeLegacy(MemorySegment dec, MemorySegment format, int target, MemorySegment size) {
var mh$ = JxlDecoderGetICCProfileSizeLegacy$MH();
try {
return (int) mh$.invokeExact(dec, format, target, size);
} catch (Throwable ex$) {
throw new AssertionError("should not reach here", ex$);
}
}

public static MethodHandle JxlDecoderGetColorAsICCProfile$MH() {
public static int JxlDecoderGetICCProfileSize(MemorySegment dec, int target, MemorySegment size) {
var mh$ = JxlDecoderGetICCProfileSize$MH();
try {
return (int) mh$.invokeExact(dec, target, size);
} catch (Throwable ex$) {
throw new AssertionError("should not reach here", ex$);
}
}

public static MethodHandle JxlDecoderGetColorAsICCProfileLegacy$MH() {
return RuntimeHelper.requireNonNull(constants$21.const$6, "JxlDecoderGetColorAsICCProfile");
}

public static MethodHandle JxlDecoderGetColorAsICCProfile$MH() {
return RuntimeHelper.requireNonNull(constants$21.const$6a, "JxlDecoderGetColorAsICCProfile");
}

/**
* {@snippet :
* JxlDecoderStatus JxlDecoderGetColorAsICCProfile(const JxlDecoder* dec, const JxlPixelFormat* format, JxlColorProfileTarget target, uint8_t* icc_profile, size_t size);
*}
*/
public static int JxlDecoderGetColorAsICCProfile(MemorySegment dec, MemorySegment format, int target, MemorySegment icc_profile, long size) {
var mh$ = JxlDecoderGetColorAsICCProfile$MH();
public static int JxlDecoderGetColorAsICCProfileLegacy(MemorySegment dec, MemorySegment format, int target, MemorySegment icc_profile, long size) {
var mh$ = JxlDecoderGetColorAsICCProfileLegacy$MH();
try {
return (int) mh$.invokeExact(dec, format, target, icc_profile, size);
} catch (Throwable ex$) {
throw new AssertionError("should not reach here", ex$);
}
}

public static int JxlDecoderGetColorAsICCProfile(MemorySegment dec, int target, MemorySegment icc_profile, long size) {
var mh$ = JxlDecoderGetColorAsICCProfile$MH();
try {
return (int) mh$.invokeExact(dec, target, icc_profile, size);
} catch (Throwable ex$) {
throw new AssertionError("should not reach here", ex$);
}
}

public static MethodHandle JxlDecoderSetPreferredColorProfile$MH() {
return RuntimeHelper.requireNonNull(constants$22.const$0, "JxlDecoderSetPreferredColorProfile");
}
Expand Down

0 comments on commit b3a5225

Please sign in to comment.