2 * Copyright (C) 2021- The Geeqie Team
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * Copyright (c) the JPEG XL Project Authors.
24 * All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions are met:
29 * 1. Redistributions of source code must retain the above copyright notice, this
30 * list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright notice,
33 * this list of conditions and the following disclaimer in the documentation
34 * and/or other materials provided with the distribution.
36 * 3. Neither the name of the copyright holder nor the names of its
37 * contributors may be used to endorse or promote products derived from
38 * this software without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
41 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
46 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
47 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
49 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 #include "image-load.h"
56 #include "image_load_jpegxl.h"
60 #include "jxl/decode.h"
62 typedef struct _ImageLoaderJPEGXL ImageLoaderJPEGXL;
63 struct _ImageLoaderJPEGXL {
64 ImageLoaderBackendCbAreaUpdated area_updated_cb;
65 ImageLoaderBackendCbSize size_cb;
66 ImageLoaderBackendCbAreaPrepared area_prepared_cb;
69 guint requested_width;
70 guint requested_height;
74 static void free_buffer(guchar *pixels, gpointer data)
79 static uint8_t *JxlMemoryToPixels(const uint8_t *next_in, size_t size, size_t *stride,
80 size_t *xsize, size_t *ysize, int *has_alpha) {
81 JxlDecoder *dec = JxlDecoderCreate(NULL);
83 uint8_t *pixels = NULL;
85 log_printf("JxlDecoderCreate failed\n");
88 if (JXL_DEC_SUCCESS !=
89 JxlDecoderSubscribeEvents(dec, JXL_DEC_BASIC_INFO | JXL_DEC_FULL_IMAGE)) {
90 log_printf("JxlDecoderSubscribeEvents failed\n");
91 JxlDecoderDestroy(dec);
97 JxlPixelFormat format = {4, JXL_TYPE_UINT8, JXL_NATIVE_ENDIAN, 0};
98 JxlDecoderSetInput(dec, next_in, size);
101 JxlDecoderStatus status = JxlDecoderProcessInput(dec);
103 if (status == JXL_DEC_ERROR) {
104 log_printf("Decoder error\n");
106 } else if (status == JXL_DEC_NEED_MORE_INPUT) {
107 log_printf("Error, already provided all input\n");
109 } else if (status == JXL_DEC_BASIC_INFO) {
110 if (JXL_DEC_SUCCESS != JxlDecoderGetBasicInfo(dec, &info)) {
111 log_printf("JxlDecoderGetBasicInfo failed\n");
116 *stride = info.xsize * 4;
117 } else if (status == JXL_DEC_NEED_IMAGE_OUT_BUFFER) {
119 if (JXL_DEC_SUCCESS !=
120 JxlDecoderImageOutBufferSize(dec, &format, &buffer_size)) {
121 log_printf("JxlDecoderImageOutBufferSize failed\n");
124 if (buffer_size != *stride * *ysize) {
125 log_printf("Invalid out buffer size %zu %zu\n", buffer_size,
129 size_t pixels_buffer_size = buffer_size * sizeof(uint8_t);
130 pixels = malloc(pixels_buffer_size);
131 void *pixels_buffer = (void *)pixels;
132 if (JXL_DEC_SUCCESS != JxlDecoderSetImageOutBuffer(dec, &format,
134 pixels_buffer_size)) {
135 log_printf("JxlDecoderSetImageOutBuffer failed\n");
138 } else if (status == JXL_DEC_FULL_IMAGE) {
139 // This means the decoder has decoded all pixels into the buffer.
142 } else if (status == JXL_DEC_SUCCESS) {
143 log_printf("Decoding finished before receiving pixel data\n");
146 log_printf("Unexpected decoder status: %d\n", status);
158 static gboolean image_loader_jpegxl_load(gpointer loader, const guchar *buf, gsize count, GError **error)
160 ImageLoaderJPEGXL *ld = (ImageLoaderJPEGXL *) loader;
161 gboolean ret = FALSE;
166 uint8_t *decoded = NULL;
168 decoded = JxlMemoryToPixels(buf, count, &stride, &xsize, &ysize, &has_alpha);
172 ld->pixbuf = gdk_pixbuf_new_from_data(decoded, GDK_COLORSPACE_RGB, has_alpha, 8, xsize, ysize, stride, free_buffer, NULL);
174 ld->area_updated_cb(loader, 0, 0, xsize, ysize, ld->data);
182 static gpointer image_loader_jpegxl_new(ImageLoaderBackendCbAreaUpdated area_updated_cb, ImageLoaderBackendCbSize size_cb, ImageLoaderBackendCbAreaPrepared area_prepared_cb, gpointer data)
184 ImageLoaderJPEGXL *loader = g_new0(ImageLoaderJPEGXL, 1);
185 loader->area_updated_cb = area_updated_cb;
186 loader->size_cb = size_cb;
187 loader->area_prepared_cb = area_prepared_cb;
189 return (gpointer) loader;
192 static void image_loader_jpegxl_set_size(gpointer loader, int width, int height)
194 ImageLoaderJPEGXL *ld = (ImageLoaderJPEGXL *) loader;
195 ld->requested_width = width;
196 ld->requested_height = height;
199 static GdkPixbuf* image_loader_jpegxl_get_pixbuf(gpointer loader)
201 ImageLoaderJPEGXL *ld = (ImageLoaderJPEGXL *) loader;
205 static gchar* image_loader_jpegxl_get_format_name(gpointer loader)
207 return g_strdup("jxl");
210 static gchar** image_loader_jpegxl_get_format_mime_types(gpointer loader)
212 static gchar *mime[] = {"image/jxl", NULL};
213 return g_strdupv(mime);
216 static gboolean image_loader_jpegxl_close(gpointer loader, GError **error)
221 static void image_loader_jpegxl_abort(gpointer loader)
223 ImageLoaderJPEGXL *ld = (ImageLoaderJPEGXL *) loader;
227 static void image_loader_jpegxl_free(gpointer loader)
229 ImageLoaderJPEGXL *ld = (ImageLoaderJPEGXL *) loader;
230 if (ld->pixbuf) g_object_unref(ld->pixbuf);
234 void image_loader_backend_set_jpegxl(ImageLoaderBackend *funcs)
236 funcs->loader_new = image_loader_jpegxl_new;
237 funcs->set_size = image_loader_jpegxl_set_size;
238 funcs->load = image_loader_jpegxl_load;
240 funcs->get_pixbuf = image_loader_jpegxl_get_pixbuf;
241 funcs->close = image_loader_jpegxl_close;
242 funcs->abort = image_loader_jpegxl_abort;
243 funcs->free = image_loader_jpegxl_free;
244 funcs->get_format_name = image_loader_jpegxl_get_format_name;
245 funcs->get_format_mime_types = image_loader_jpegxl_get_format_mime_types;
249 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */