Fix missing translation
[geeqie.git] / src / image-load-jpegxl.cc
1 /*
2  * Copyright (C) 2021- The Geeqie Team
3  *
4  * Author: Colin Clark
5  *
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.
10  *
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.
15  *
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.
19  *
20  *
21  * Including parts:
22  *
23  * Copyright (c) the JPEG XL Project Authors.
24  * All rights reserved.
25  *
26  * Redistribution and use in source and binary forms, with or without
27  * modification, are permitted provided that the following conditions are met:
28  *
29  * 1. Redistributions of source code must retain the above copyright notice, this
30  *    list of conditions and the following disclaimer.
31  *
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.
35  *
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.
39  *
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.
50  *
51  */
52
53 #include <config.h>
54
55 #ifdef HAVE_JPEGXL
56
57 #include "image-load-jpegxl.h"
58
59 #include <memory>
60
61 #include <jxl/decode.h>
62
63 #include "debug.h"
64 #include "image-load.h"
65
66 struct ImageLoaderJPEGXL {
67         ImageLoaderBackendCbAreaUpdated area_updated_cb;
68         ImageLoaderBackendCbSize size_cb;
69         ImageLoaderBackendCbAreaPrepared area_prepared_cb;
70         gpointer data;
71         GdkPixbuf *pixbuf;
72         guint requested_width;
73         guint requested_height;
74         gboolean abort;
75 };
76
77 static void free_buffer(guchar *pixels, gpointer)
78 {
79         g_free(pixels);
80 }
81
82 static uint8_t *JxlMemoryToPixels(const uint8_t *next_in, size_t size, size_t &xsize, size_t &ysize, size_t &stride)
83 {
84         std::unique_ptr<JxlDecoder, decltype(&JxlDecoderDestroy)> dec{JxlDecoderCreate(nullptr), JxlDecoderDestroy};
85         if (!dec)
86                 {
87                 log_printf("JxlDecoderCreate failed\n");
88                 return nullptr;
89                 }
90         if (JXL_DEC_SUCCESS != JxlDecoderSubscribeEvents(dec.get(), JXL_DEC_BASIC_INFO | JXL_DEC_FULL_IMAGE))
91                 {
92                 log_printf("JxlDecoderSubscribeEvents failed\n");
93                 return nullptr;
94                 }
95
96         /* Avoid compiler warning - used uninitialized */
97         /* This file will be replaced by libjxl at some time */
98         ysize = 0;
99         stride = 0;
100         
101         std::unique_ptr<uint8_t, decltype(&free)> pixels{nullptr, free};
102         JxlBasicInfo info;
103         JxlPixelFormat format = {4, JXL_TYPE_UINT8, JXL_NATIVE_ENDIAN, 0};
104         JxlDecoderSetInput(dec.get(), next_in, size);
105
106         for (;;)
107                 {
108                 JxlDecoderStatus status = JxlDecoderProcessInput(dec.get());
109
110                 switch (status)
111                         {
112                         case JXL_DEC_ERROR:
113                                 log_printf("Decoder error\n");
114                                 return nullptr;
115                         case JXL_DEC_NEED_MORE_INPUT:
116                                 log_printf("Error, already provided all input\n");
117                                 return nullptr;
118                         case JXL_DEC_BASIC_INFO:
119                                 if (JXL_DEC_SUCCESS != JxlDecoderGetBasicInfo(dec.get(), &info))
120                                         {
121                                         log_printf("JxlDecoderGetBasicInfo failed\n");
122                                         return nullptr;
123                                         }
124                                 xsize = info.xsize;
125                                 ysize = info.ysize;
126                                 stride = info.xsize * 4;
127                                 break;
128                         case JXL_DEC_NEED_IMAGE_OUT_BUFFER:
129                                 {
130                                 size_t buffer_size;
131                                 if (JXL_DEC_SUCCESS != JxlDecoderImageOutBufferSize(dec.get(), &format, &buffer_size))
132                                         {
133                                         log_printf("JxlDecoderImageOutBufferSize failed\n");
134                                         return nullptr;
135                                         }
136                                 if (buffer_size != stride * ysize)
137                                         {
138                                         log_printf("Invalid out buffer size %zu %zu\n", buffer_size, stride * ysize);
139                                         return nullptr;
140                                         }
141                                 size_t pixels_buffer_size = buffer_size * sizeof(uint8_t);
142                                 pixels.reset(static_cast<uint8_t *>(malloc(pixels_buffer_size)));
143                                 if (JXL_DEC_SUCCESS != JxlDecoderSetImageOutBuffer(dec.get(), &format, pixels.get(), pixels_buffer_size))
144                                         {
145                                         log_printf("JxlDecoderSetImageOutBuffer failed\n");
146                                         return nullptr;
147                                         }
148                                 }
149                                 break;
150                         case JXL_DEC_FULL_IMAGE:
151                                 // This means the decoder has decoded all pixels into the buffer.
152                                 return pixels.release();
153                         case JXL_DEC_SUCCESS:
154                                 log_printf("Decoding finished before receiving pixel data\n");
155                                 return nullptr;
156                         default:
157                                 log_printf("Unexpected decoder status: %d\n", status);
158                                 return nullptr;
159                         }
160                 }
161
162         return nullptr;
163 }
164
165 static gboolean image_loader_jpegxl_load(gpointer loader, const guchar *buf, gsize count, GError **)
166 {
167         auto ld = static_cast<ImageLoaderJPEGXL *>(loader);
168         gboolean ret = FALSE;
169         size_t xsize;
170         size_t ysize;
171         size_t stride;
172         uint8_t *decoded = nullptr;
173
174         decoded = JxlMemoryToPixels(buf, count, xsize, ysize, stride);
175
176         if (decoded)
177                 {
178                 ld->pixbuf = gdk_pixbuf_new_from_data(decoded, GDK_COLORSPACE_RGB, TRUE, 8, xsize, ysize, stride, free_buffer, nullptr);
179
180                 ld->area_updated_cb(loader, 0, 0, xsize, ysize, ld->data);
181
182                 ret = TRUE;
183                 }
184
185         return ret;
186 }
187
188 static gpointer image_loader_jpegxl_new(ImageLoaderBackendCbAreaUpdated area_updated_cb, ImageLoaderBackendCbSize size_cb, ImageLoaderBackendCbAreaPrepared area_prepared_cb, gpointer data)
189 {
190         auto loader = g_new0(ImageLoaderJPEGXL, 1);
191         loader->area_updated_cb = area_updated_cb;
192         loader->size_cb = size_cb;
193         loader->area_prepared_cb = area_prepared_cb;
194         loader->data = data;
195         return loader;
196 }
197
198 static void image_loader_jpegxl_set_size(gpointer loader, int width, int height)
199 {
200         auto ld = static_cast<ImageLoaderJPEGXL *>(loader);
201         ld->requested_width = width;
202         ld->requested_height = height;
203 }
204
205 static GdkPixbuf* image_loader_jpegxl_get_pixbuf(gpointer loader)
206 {
207         auto ld = static_cast<ImageLoaderJPEGXL *>(loader);
208         return ld->pixbuf;
209 }
210
211 static gchar* image_loader_jpegxl_get_format_name(gpointer)
212 {
213         return g_strdup("jxl");
214 }
215
216 static gchar** image_loader_jpegxl_get_format_mime_types(gpointer)
217 {
218         static const gchar *mime[] = {"image/jxl", nullptr};
219         return g_strdupv(const_cast<gchar **>(mime));
220 }
221
222 static gboolean image_loader_jpegxl_close(gpointer, GError **)
223 {
224         return TRUE;
225 }
226
227 static void image_loader_jpegxl_abort(gpointer loader)
228 {
229         auto ld = static_cast<ImageLoaderJPEGXL *>(loader);
230         ld->abort = TRUE;
231 }
232
233 static void image_loader_jpegxl_free(gpointer loader)
234 {
235         auto ld = static_cast<ImageLoaderJPEGXL *>(loader);
236         if (ld->pixbuf) g_object_unref(ld->pixbuf);
237         g_free(ld);
238 }
239
240 void image_loader_backend_set_jpegxl(ImageLoaderBackend *funcs)
241 {
242         funcs->loader_new = image_loader_jpegxl_new;
243         funcs->set_size = image_loader_jpegxl_set_size;
244         funcs->load = image_loader_jpegxl_load;
245         funcs->write = nullptr;
246         funcs->get_pixbuf = image_loader_jpegxl_get_pixbuf;
247         funcs->close = image_loader_jpegxl_close;
248         funcs->abort = image_loader_jpegxl_abort;
249         funcs->free = image_loader_jpegxl_free;
250         funcs->get_format_name = image_loader_jpegxl_get_format_name;
251         funcs->get_format_mime_types = image_loader_jpegxl_get_format_mime_types;
252 }
253
254 #endif
255 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */