Simplify vflist_get_formatted()
[geeqie.git] / src / image_load_jpegxl.c
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 "main.h"
54
55 #include "image-load.h"
56 #include "image_load_jpegxl.h"
57
58 #ifdef HAVE_JPEGXL
59
60 #include "jxl/decode.h"
61
62 typedef struct _ImageLoaderJPEGXL ImageLoaderJPEGXL;
63 struct _ImageLoaderJPEGXL {
64         ImageLoaderBackendCbAreaUpdated area_updated_cb;
65         ImageLoaderBackendCbSize size_cb;
66         ImageLoaderBackendCbAreaPrepared area_prepared_cb;
67         gpointer data;
68         GdkPixbuf *pixbuf;
69         guint requested_width;
70         guint requested_height;
71         gboolean abort;
72 };
73
74 static void free_buffer(guchar *pixels, gpointer data)
75 {
76         g_free(pixels);
77 }
78
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);
82   *has_alpha = 1;
83   uint8_t *pixels = NULL;
84   if (!dec) {
85     log_printf("JxlDecoderCreate failed\n");
86     return 0;
87   }
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);
92     return 0;
93   }
94
95   JxlBasicInfo info;
96   int success = 0;
97   JxlPixelFormat format = {4, JXL_TYPE_UINT8, JXL_NATIVE_ENDIAN, 0};
98   JxlDecoderSetInput(dec, next_in, size);
99
100   for (;;) {
101     JxlDecoderStatus status = JxlDecoderProcessInput(dec);
102
103     if (status == JXL_DEC_ERROR) {
104       log_printf("Decoder error\n");
105       break;
106     } else if (status == JXL_DEC_NEED_MORE_INPUT) {
107       log_printf("Error, already provided all input\n");
108       break;
109     } else if (status == JXL_DEC_BASIC_INFO) {
110       if (JXL_DEC_SUCCESS != JxlDecoderGetBasicInfo(dec, &info)) {
111         log_printf("JxlDecoderGetBasicInfo failed\n");
112         break;
113       }
114       *xsize = info.xsize;
115       *ysize = info.ysize;
116       *stride = info.xsize * 4;
117     } else if (status == JXL_DEC_NEED_IMAGE_OUT_BUFFER) {
118       size_t buffer_size;
119       if (JXL_DEC_SUCCESS !=
120           JxlDecoderImageOutBufferSize(dec, &format, &buffer_size)) {
121         log_printf("JxlDecoderImageOutBufferSize failed\n");
122         break;
123       }
124       if (buffer_size != *stride * *ysize) {
125         log_printf("Invalid out buffer size %zu %zu\n", buffer_size,
126                 *stride * *ysize);
127         break;
128       }
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,
133                                                          pixels_buffer,
134                                                          pixels_buffer_size)) {
135         log_printf("JxlDecoderSetImageOutBuffer failed\n");
136         break;
137       }
138     } else if (status == JXL_DEC_FULL_IMAGE) {
139       // This means the decoder has decoded all pixels into the buffer.
140       success = 1;
141       break;
142     } else if (status == JXL_DEC_SUCCESS) {
143       log_printf("Decoding finished before receiving pixel data\n");
144       break;
145     } else {
146       log_printf("Unexpected decoder status: %d\n", status);
147       break;
148     }
149   }
150
151   JxlDecoderDestroy(dec);
152   if (success){
153     return pixels;
154   } else {
155     free(pixels);
156     return NULL;
157   }
158 }
159
160 static gboolean image_loader_jpegxl_load(gpointer loader, const guchar *buf, gsize count, GError **error)
161 {
162         ImageLoaderJPEGXL *ld = (ImageLoaderJPEGXL *) loader;
163         gboolean ret = FALSE;
164         size_t stride;
165         size_t xsize;
166         size_t ysize;
167         int has_alpha;
168         uint8_t *decoded = NULL;
169
170         decoded = JxlMemoryToPixels(buf, count, &stride, &xsize, &ysize, &has_alpha);
171
172         if (decoded)
173                 {
174                 ld->pixbuf = gdk_pixbuf_new_from_data(decoded, GDK_COLORSPACE_RGB, has_alpha, 8, xsize, ysize, stride, free_buffer, NULL);
175
176                 ld->area_updated_cb(loader, 0, 0, xsize, ysize, ld->data);
177
178                 ret = TRUE;
179                 }
180
181         return ret;
182 }
183
184 static gpointer image_loader_jpegxl_new(ImageLoaderBackendCbAreaUpdated area_updated_cb, ImageLoaderBackendCbSize size_cb, ImageLoaderBackendCbAreaPrepared area_prepared_cb, gpointer data)
185 {
186         ImageLoaderJPEGXL *loader = g_new0(ImageLoaderJPEGXL, 1);
187         loader->area_updated_cb = area_updated_cb;
188         loader->size_cb = size_cb;
189         loader->area_prepared_cb = area_prepared_cb;
190         loader->data = data;
191         return (gpointer) loader;
192 }
193
194 static void image_loader_jpegxl_set_size(gpointer loader, int width, int height)
195 {
196         ImageLoaderJPEGXL *ld = (ImageLoaderJPEGXL *) loader;
197         ld->requested_width = width;
198         ld->requested_height = height;
199 }
200
201 static GdkPixbuf* image_loader_jpegxl_get_pixbuf(gpointer loader)
202 {
203         ImageLoaderJPEGXL *ld = (ImageLoaderJPEGXL *) loader;
204         return ld->pixbuf;
205 }
206
207 static gchar* image_loader_jpegxl_get_format_name(gpointer loader)
208 {
209         return g_strdup("jxl");
210 }
211
212 static gchar** image_loader_jpegxl_get_format_mime_types(gpointer loader)
213 {
214         static gchar *mime[] = {"image/jxl", NULL};
215         return g_strdupv(mime);
216 }
217
218 static gboolean image_loader_jpegxl_close(gpointer loader, GError **error)
219 {
220         return TRUE;
221 }
222
223 static void image_loader_jpegxl_abort(gpointer loader)
224 {
225         ImageLoaderJPEGXL *ld = (ImageLoaderJPEGXL *) loader;
226         ld->abort = TRUE;
227 }
228
229 static void image_loader_jpegxl_free(gpointer loader)
230 {
231         ImageLoaderJPEGXL *ld = (ImageLoaderJPEGXL *) loader;
232         if (ld->pixbuf) g_object_unref(ld->pixbuf);
233         g_free(ld);
234 }
235
236 void image_loader_backend_set_jpegxl(ImageLoaderBackend *funcs)
237 {
238         funcs->loader_new = image_loader_jpegxl_new;
239         funcs->set_size = image_loader_jpegxl_set_size;
240         funcs->load = image_loader_jpegxl_load;
241         funcs->write = NULL;
242         funcs->get_pixbuf = image_loader_jpegxl_get_pixbuf;
243         funcs->close = image_loader_jpegxl_close;
244         funcs->abort = image_loader_jpegxl_abort;
245         funcs->free = image_loader_jpegxl_free;
246         funcs->get_format_name = image_loader_jpegxl_get_format_name;
247         funcs->get_format_mime_types = image_loader_jpegxl_get_format_mime_types;
248 }
249
250 #endif
251 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */