69 lines
2.5 KiB
C
69 lines
2.5 KiB
C
#include <jxl/decode.h>
|
|
|
|
#ifndef use_jxl_library
|
|
#define use_jxl_library
|
|
#endif
|
|
|
|
static void * jxl_image_import (const char * path, uint * width, uint * height) {
|
|
JxlDecoder * decoder = null;
|
|
JxlBasicInfo information = { 0 };
|
|
JxlPixelFormat format = { 4, JXL_TYPE_UINT8, JXL_NATIVE_ENDIAN, 0 };
|
|
JxlDecoderStatus status = JXL_DEC_ERROR;
|
|
|
|
fatal_failure (path == null, "jxl_image_import: File path is null pointer.");
|
|
fatal_failure (width == null, "jxl_image_import: Width is null pointer.");
|
|
fatal_failure (height == null, "jxl_image_import: Height is null pointer.");
|
|
|
|
ulong size = file_size (path);
|
|
uchar * data = file_record (path);
|
|
uint * pixel_array = null;
|
|
ulong output_size = 0;
|
|
|
|
decoder = JxlDecoderCreate (null);
|
|
|
|
fatal_failure (decoder == null, "jxl_image_import: Failed to create a decoder.");
|
|
|
|
status = JxlDecoderSubscribeEvents (decoder, JXL_DEC_BASIC_INFO | JXL_DEC_FULL_IMAGE);
|
|
|
|
fatal_failure (status != JXL_DEC_SUCCESS, "jxl_image_import: Failed to subscribe decoder basic information and full image events.");
|
|
|
|
status = JxlDecoderSetInput (decoder, data, size);
|
|
|
|
fatal_failure (status != JXL_DEC_SUCCESS, "jxl_image_import: Failed to set decoder input data and size.");
|
|
|
|
for (status = JxlDecoderProcessInput (decoder); true; status = JxlDecoderProcessInput (decoder)) {
|
|
fatal_failure (status == JXL_DEC_ERROR, "jxl_image_import: Decoder internal error.");
|
|
fatal_failure (status == JXL_DEC_NEED_MORE_INPUT, "jxl_image_import: Decoder needs more input data.");
|
|
|
|
if (status == JXL_DEC_BASIC_INFO) {
|
|
status = JxlDecoderGetBasicInfo(decoder, &information);
|
|
fatal_failure (status != JXL_DEC_SUCCESS, "jxl_image_import: Failed to get basic image information.");
|
|
continue;
|
|
}
|
|
|
|
if (status == JXL_DEC_NEED_IMAGE_OUT_BUFFER) {
|
|
status = JxlDecoderImageOutBufferSize(decoder, & format, & output_size);
|
|
fatal_failure (status != JXL_DEC_SUCCESS, "jxl_image_import: Failed to get image output buffer size.");
|
|
if (pixel_array != null) {
|
|
pixel_array = deallocate (pixel_array);
|
|
}
|
|
pixel_array = allocate (output_size);
|
|
status = JxlDecoderSetImageOutBuffer(decoder, & format, pixel_array, output_size);
|
|
fatal_failure (status != JXL_DEC_SUCCESS, "jxl_image_import: Failed to set image output buffer data.");
|
|
continue;
|
|
}
|
|
|
|
if (status == JXL_DEC_FULL_IMAGE) continue;
|
|
if (status == JXL_DEC_SUCCESS) break;
|
|
}
|
|
|
|
* width = information.xsize;
|
|
* height = information.ysize;
|
|
|
|
JxlDecoderDestroy (decoder);
|
|
|
|
data = deallocate (data);
|
|
|
|
return (pixel_array);
|
|
}
|