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.
57 #include "image-load-jpegxl.h"
63 #include <gdk-pixbuf/gdk-pixbuf.h>
64 #include <glib-object.h>
66 #include <jxl/codestream_header.h>
67 #include <jxl/decode.h> //TODO Use decode_cxx.h?
68 #include <jxl/types.h>
71 #include "image-load.h"
76 struct ImageLoaderJPEGXL {
77 ImageLoaderBackendCbAreaUpdated area_updated_cb;
78 ImageLoaderBackendCbSize size_cb;
79 ImageLoaderBackendCbAreaPrepared area_prepared_cb;
82 guint requested_width;
83 guint requested_height;
87 void free_buffer(guchar *pixels, gpointer)
92 uint8_t *JxlMemoryToPixels(const uint8_t *next_in, size_t size, size_t &xsize, size_t &ysize, size_t &stride)
94 std::unique_ptr<JxlDecoder, decltype(&JxlDecoderDestroy)> dec{JxlDecoderCreate(nullptr), JxlDecoderDestroy};
97 log_printf("JxlDecoderCreate failed\n");
100 if (JXL_DEC_SUCCESS != JxlDecoderSubscribeEvents(dec.get(), JXL_DEC_BASIC_INFO | JXL_DEC_FULL_IMAGE))
102 log_printf("JxlDecoderSubscribeEvents failed\n");
106 /* Avoid compiler warning - used uninitialized */
107 /* This file will be replaced by libjxl at some time */
111 std::unique_ptr<uint8_t, decltype(&free)> pixels{nullptr, free};
113 JxlPixelFormat format = {4, JXL_TYPE_UINT8, JXL_NATIVE_ENDIAN, 0};
114 JxlDecoderSetInput(dec.get(), next_in, size);
118 JxlDecoderStatus status = JxlDecoderProcessInput(dec.get());
123 log_printf("Decoder error\n");
125 case JXL_DEC_NEED_MORE_INPUT:
126 log_printf("Error, already provided all input\n");
128 case JXL_DEC_BASIC_INFO:
129 if (JXL_DEC_SUCCESS != JxlDecoderGetBasicInfo(dec.get(), &info))
131 log_printf("JxlDecoderGetBasicInfo failed\n");
136 stride = info.xsize * 4;
138 case JXL_DEC_NEED_IMAGE_OUT_BUFFER:
141 if (JXL_DEC_SUCCESS != JxlDecoderImageOutBufferSize(dec.get(), &format, &buffer_size))
143 log_printf("JxlDecoderImageOutBufferSize failed\n");
146 if (buffer_size != stride * ysize)
148 log_printf("Invalid out buffer size %zu %zu\n", buffer_size, stride * ysize);
151 size_t pixels_buffer_size = buffer_size * sizeof(uint8_t);
152 pixels.reset(static_cast<uint8_t *>(malloc(pixels_buffer_size)));
153 if (JXL_DEC_SUCCESS != JxlDecoderSetImageOutBuffer(dec.get(), &format, pixels.get(), pixels_buffer_size))
155 log_printf("JxlDecoderSetImageOutBuffer failed\n");
160 case JXL_DEC_FULL_IMAGE:
161 // This means the decoder has decoded all pixels into the buffer.
162 return pixels.release();
163 case JXL_DEC_SUCCESS:
164 log_printf("Decoding finished before receiving pixel data\n");
167 log_printf("Unexpected decoder status: %d\n", status);
175 gboolean image_loader_jpegxl_write(gpointer loader, const guchar *buf, gsize &chunk_size, gsize count, GError **)
177 auto ld = static_cast<ImageLoaderJPEGXL *>(loader);
178 gboolean ret = FALSE;
182 uint8_t *decoded = nullptr;
184 decoded = JxlMemoryToPixels(buf, count, xsize, ysize, stride);
188 ld->pixbuf = gdk_pixbuf_new_from_data(decoded, GDK_COLORSPACE_RGB, TRUE, 8, xsize, ysize, stride, free_buffer, nullptr);
190 ld->area_updated_cb(loader, 0, 0, xsize, ysize, ld->data);
199 gpointer image_loader_jpegxl_new(ImageLoaderBackendCbAreaUpdated area_updated_cb, ImageLoaderBackendCbSize size_cb, ImageLoaderBackendCbAreaPrepared area_prepared_cb, gpointer data)
201 auto loader = g_new0(ImageLoaderJPEGXL, 1);
202 loader->area_updated_cb = area_updated_cb;
203 loader->size_cb = size_cb;
204 loader->area_prepared_cb = area_prepared_cb;
209 void image_loader_jpegxl_set_size(gpointer loader, int width, int height)
211 auto ld = static_cast<ImageLoaderJPEGXL *>(loader);
212 ld->requested_width = width;
213 ld->requested_height = height;
216 GdkPixbuf* image_loader_jpegxl_get_pixbuf(gpointer loader)
218 auto ld = static_cast<ImageLoaderJPEGXL *>(loader);
222 gchar* image_loader_jpegxl_get_format_name(gpointer)
224 return g_strdup("jxl");
227 gchar** image_loader_jpegxl_get_format_mime_types(gpointer)
229 static const gchar *mime[] = {"image/jxl", nullptr};
230 return g_strdupv(const_cast<gchar **>(mime));
233 gboolean image_loader_jpegxl_close(gpointer, GError **)
238 void image_loader_jpegxl_abort(gpointer loader)
240 auto ld = static_cast<ImageLoaderJPEGXL *>(loader);
244 void image_loader_jpegxl_free(gpointer loader)
246 auto ld = static_cast<ImageLoaderJPEGXL *>(loader);
247 if (ld->pixbuf) g_object_unref(ld->pixbuf);
253 void image_loader_backend_set_jpegxl(ImageLoaderBackend *funcs)
255 funcs->loader_new = image_loader_jpegxl_new;
256 funcs->set_size = image_loader_jpegxl_set_size;
257 funcs->write = image_loader_jpegxl_write;
258 funcs->get_pixbuf = image_loader_jpegxl_get_pixbuf;
259 funcs->close = image_loader_jpegxl_close;
260 funcs->abort = image_loader_jpegxl_abort;
261 funcs->free = image_loader_jpegxl_free;
262 funcs->get_format_name = image_loader_jpegxl_get_format_name;
263 funcs->get_format_mime_types = image_loader_jpegxl_get_format_mime_types;
267 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */