1
0
mirror of https://github.com/gryf/wmaker.git synced 2026-03-19 17:23:33 +01:00

WRaster: Add optional support to JPEG XL

Detect if libjxl is intalled, and build-in support in raster lib.
Feature can be disabled/enabled at configure time.
This commit is contained in:
David Maciejak
2025-12-27 08:20:14 -05:00
committed by Carlos R. Mafra
parent 67e2f5e1ca
commit 4392fdc291
8 changed files with 297 additions and 3 deletions

View File

@@ -58,6 +58,10 @@ libwraster_la_SOURCES += load_jpeg.c
libwraster_la_SOURCES += save_jpeg.c
endif
if USE_JXL
libwraster_la_SOURCES += load_jxl.c
endif
if USE_PNG
libwraster_la_SOURCES += load_png.c
libwraster_la_SOURCES += save_png.c

View File

@@ -38,12 +38,13 @@ typedef enum {
IM_PPM = 4,
IM_JPEG = 5,
IM_GIF = 6,
IM_WEBP = 7
IM_WEBP = 7,
IM_JXL = 8
} WRImgFormat;
/* How many image types we have. */
/* Increase this when adding new image types! */
#define IM_TYPES 7
#define IM_TYPES 8
/*
* Function for Loading in a specific format
@@ -64,6 +65,10 @@ RImage *RLoadPNG(RContext *context, const char *file);
RImage *RLoadJPEG(const char *file);
#endif
#ifdef USE_JXL
RImage *RLoadJXL(const char *file);
#endif
#ifdef USE_GIF
RImage *RLoadGIF(const char *file, int index);
#endif

View File

@@ -3,7 +3,7 @@
* Raster graphics library
*
* Copyright (c) 1997-2003 Alfredo K. Kojima
* Copyright (c) 2014-2021 Window Maker Team
* Copyright (c) 2014-2025 Window Maker Team
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
@@ -93,6 +93,9 @@ char **RSupportedFileFormats(void)
#ifdef USE_JPEG
tmp[i++] = "JPEG";
#endif
#ifdef USE_JXL
tmp[i++] = "JXL";
#endif
#ifdef USE_GIF
tmp[i++] = "GIF";
#endif
@@ -219,6 +222,12 @@ RImage *RLoadImage(RContext *context, const char *file, int index)
break;
#endif /* USE_JPEG */
#ifdef USE_JXL
case IM_JXL:
image = RLoadJXL(file);
break;
#endif /* USE_JXL */
#ifdef USE_GIF
case IM_GIF:
image = RLoadGIF(file, index);
@@ -305,6 +314,11 @@ char *RGetImageFileFormat(const char *file)
return "JPEG";
#endif /* USE_JPEG */
#ifdef USE_JXL
case IM_JXL:
return "JXL";
#endif /* USE_JXL */
#ifdef USE_GIF
case IM_GIF:
return "GIF";
@@ -377,6 +391,13 @@ static WRImgFormat identFile(const char *path)
if (buffer[0] == 0xff && buffer[1] == 0xd8)
return IM_JPEG;
/* check for JXL */
if ((buffer[0] == 0xff && buffer[1] == 0x0a) || /* naked codestream */
(buffer[0] == 0x00 && buffer[1] == 0x00 && buffer[2] == 0x00 && buffer[3] == 0x0c && /* container format */
buffer[4] == 0x4a && buffer[5] == 0x58 && buffer[6] == 0x4c && buffer[7] == 0x20 &&
buffer[8] == 0x0d && buffer[9] == 0x0a && buffer[10] == 0x87 && buffer[11] == 0x0a))
return IM_JXL;
/* check for GIF */
if (buffer[0] == 'G' && buffer[1] == 'I' && buffer[2] == 'F' && buffer[3] == '8' &&
(buffer[4] == '7' || buffer[4] == '9') && buffer[5] == 'a')

211
wrlib/load_jxl.c Normal file
View File

@@ -0,0 +1,211 @@
/* load_jxl.c - load JXL (JPEG XL) image from file
*
* Raster graphics library
*
* Copyright (c) 2025 Window Maker Team
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <config.h>
#ifdef USE_JXL
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <jxl/decode.h>
#include "wraster.h"
#include "imgformat.h"
#include "wr_i18n.h"
static unsigned char *do_read_file(const char *filename, size_t *size)
{
FILE *file;
struct stat st;
unsigned char *data;
if (stat(filename, &st) != 0) {
RErrorCode = RERR_OPEN;
return NULL;
}
file = fopen(filename, "rb");
if (!file) {
RErrorCode = RERR_OPEN;
return NULL;
}
*size = st.st_size;
data = malloc(*size);
if (!data) {
RErrorCode = RERR_NOMEMORY;
fclose(file);
return NULL;
}
if (fread(data, 1, *size, file) != *size) {
RErrorCode = RERR_READ;
free(data);
fclose(file);
return NULL;
}
fclose(file);
return data;
}
RImage *RLoadJXL(const char *file)
{
RImage *image = NULL;
unsigned char *data = NULL, *pixels = NULL;
size_t size;
JxlDecoder *dec = NULL;
JxlDecoderStatus status;
JxlBasicInfo info;
JxlPixelFormat format;
size_t buffer_size;
int width = 0, height = 0;
int has_alpha = 0;
/* Load file data */
data = do_read_file(file, &size);
if (!data)
return NULL;
/* Create decoder */
dec = JxlDecoderCreate(NULL);
if (!dec) {
RErrorCode = RERR_NOMEMORY;
goto error;
}
/* Subscribe to basic info and full image */
if (JxlDecoderSubscribeEvents(dec, JXL_DEC_BASIC_INFO | JXL_DEC_FULL_IMAGE) != JXL_DEC_SUCCESS) {
RErrorCode = RERR_BADIMAGEFILE;
goto error;
}
/* Set input data */
if (JxlDecoderSetInput(dec, data, size) != JXL_DEC_SUCCESS) {
RErrorCode = RERR_BADIMAGEFILE;
goto error;
}
/* Process events */
for (;;) {
status = JxlDecoderProcessInput(dec);
if (status == JXL_DEC_ERROR) {
RErrorCode = RERR_BADIMAGEFILE;
goto error;
}
if (status == JXL_DEC_NEED_MORE_INPUT) {
RErrorCode = RERR_BADIMAGEFILE;
goto error;
}
if (status == JXL_DEC_BASIC_INFO) {
if (JxlDecoderGetBasicInfo(dec, &info) != JXL_DEC_SUCCESS) {
RErrorCode = RERR_BADIMAGEFILE;
goto error;
}
width = info.xsize;
height = info.ysize;
if (width < 1 || height < 1) {
RErrorCode = RERR_BADIMAGEFILE;
goto error;
}
/* Check if image has alpha channel */
has_alpha = (info.alpha_bits > 0);
/* Set pixel format based on alpha channel presence */
if (has_alpha) {
format.num_channels = 4; /* RGBA */
} else {
format.num_channels = 3; /* RGB */
}
format.data_type = JXL_TYPE_UINT8;
format.endianness = JXL_NATIVE_ENDIAN;
format.align = 0;
} else if (status == JXL_DEC_NEED_IMAGE_OUT_BUFFER) {
/* Allocate image with or without alpha */
image = RCreateImage(width, height, has_alpha ? True : False);
if (!image) {
RErrorCode = RERR_NOMEMORY;
goto error;
}
/* Determine buffer size */
if (JxlDecoderImageOutBufferSize(dec, &format, &buffer_size) != JXL_DEC_SUCCESS) {
RErrorCode = RERR_BADIMAGEFILE;
goto error;
}
/* Allocate buffer */
pixels = malloc(buffer_size);
if (!pixels) {
RErrorCode = RERR_NOMEMORY;
goto error;
}
/* Set output buffer */
if (JxlDecoderSetImageOutBuffer(dec, &format, pixels, buffer_size) != JXL_DEC_SUCCESS) {
RErrorCode = RERR_BADIMAGEFILE;
goto error;
}
} else if (status == JXL_DEC_FULL_IMAGE) {
/* Image is ready, copy data directly for RGB or RGBA */
if (has_alpha) {
/* RGBA format - copy directly */
memcpy(image->data, pixels, width * height * 4);
} else {
/* RGB format - copy directly */
memcpy(image->data, pixels, width * height * 3);
}
break;
} else if (status == JXL_DEC_SUCCESS) {
/* All done */
break;
}
}
free(data);
free(pixels);
JxlDecoderDestroy(dec);
return image;
error:
if (data)
free(data);
if (pixels)
free(pixels);
if (image)
RReleaseImage(image);
if (dec)
JxlDecoderDestroy(dec);
return NULL;
}
#endif /* USE_JXL */

View File

@@ -29,6 +29,7 @@ POTFILES = \
$(top_srcdir)/wrlib/load_ppm.c \
$(top_srcdir)/wrlib/load_gif.c \
$(top_srcdir)/wrlib/load_jpeg.c \
$(top_srcdir)/wrlib/load_jxl.c \
$(top_srcdir)/wrlib/load_png.c \
$(top_srcdir)/wrlib/load_tiff.c \
$(top_srcdir)/wrlib/load_xpm.c \