Fix untranslated text
[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 #if HAVE_JPEGXL
56
57 #include "image-load-jpegxl.h"
58
59 #include <cstdint>
60 #include <cstdlib>
61 #include <memory>
62
63 #include <gdk-pixbuf/gdk-pixbuf.h>
64 #include <glib-object.h>
65 #include <glib.h>
66 #include <jxl/codestream_header.h>
67 #include <jxl/decode.h> //TODO Use decode_cxx.h?
68 #include <jxl/types.h>
69
70 #include "debug.h"
71 #include "image-load.h"
72
73 namespace
74 {
75
76 struct ImageLoaderJPEGXL : public ImageLoaderBackend
77 {
78 public:
79         ~ImageLoaderJPEGXL() override;
80
81         void init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data) override;
82         gboolean write(const guchar *buf, gsize &chunk_size, gsize count, GError **error) override;
83         GdkPixbuf *get_pixbuf() override;
84         gchar *get_format_name() override;
85         gchar **get_format_mime_types() override;
86
87 private:
88         AreaUpdatedCb area_updated_cb;
89         gpointer data;
90
91         GdkPixbuf *pixbuf;
92 };
93
94 void free_buffer(guchar *pixels, gpointer)
95 {
96         g_free(pixels);
97 }
98
99 uint8_t *JxlMemoryToPixels(const uint8_t *next_in, size_t size, size_t &xsize, size_t &ysize, size_t &stride)
100 {
101         std::unique_ptr<JxlDecoder, decltype(&JxlDecoderDestroy)> dec{JxlDecoderCreate(nullptr), JxlDecoderDestroy};
102         if (!dec)
103                 {
104                 log_printf("JxlDecoderCreate failed\n");
105                 return nullptr;
106                 }
107         if (JXL_DEC_SUCCESS != JxlDecoderSubscribeEvents(dec.get(), JXL_DEC_BASIC_INFO | JXL_DEC_FULL_IMAGE))
108                 {
109                 log_printf("JxlDecoderSubscribeEvents failed\n");
110                 return nullptr;
111                 }
112
113         /* Avoid compiler warning - used uninitialized */
114         /* This file will be replaced by libjxl at some time */
115         ysize = 0;
116         stride = 0;
117         
118         std::unique_ptr<uint8_t, decltype(&free)> pixels{nullptr, free};
119         JxlBasicInfo info;
120         JxlPixelFormat format = {4, JXL_TYPE_UINT8, JXL_NATIVE_ENDIAN, 0};
121         JxlDecoderSetInput(dec.get(), next_in, size);
122
123         for (;;)
124                 {
125                 JxlDecoderStatus status = JxlDecoderProcessInput(dec.get());
126
127                 switch (status)
128                         {
129                         case JXL_DEC_ERROR:
130                                 log_printf("Decoder error\n");
131                                 return nullptr;
132                         case JXL_DEC_NEED_MORE_INPUT:
133                                 log_printf("Error, already provided all input\n");
134                                 return nullptr;
135                         case JXL_DEC_BASIC_INFO:
136                                 if (JXL_DEC_SUCCESS != JxlDecoderGetBasicInfo(dec.get(), &info))
137                                         {
138                                         log_printf("JxlDecoderGetBasicInfo failed\n");
139                                         return nullptr;
140                                         }
141                                 xsize = info.xsize;
142                                 ysize = info.ysize;
143                                 stride = info.xsize * 4;
144                                 break;
145                         case JXL_DEC_NEED_IMAGE_OUT_BUFFER:
146                                 {
147                                 size_t buffer_size;
148                                 if (JXL_DEC_SUCCESS != JxlDecoderImageOutBufferSize(dec.get(), &format, &buffer_size))
149                                         {
150                                         log_printf("JxlDecoderImageOutBufferSize failed\n");
151                                         return nullptr;
152                                         }
153                                 if (buffer_size != stride * ysize)
154                                         {
155                                         log_printf("Invalid out buffer size %zu %zu\n", buffer_size, stride * ysize);
156                                         return nullptr;
157                                         }
158                                 size_t pixels_buffer_size = buffer_size * sizeof(uint8_t);
159                                 pixels.reset(static_cast<uint8_t *>(malloc(pixels_buffer_size)));
160                                 if (JXL_DEC_SUCCESS != JxlDecoderSetImageOutBuffer(dec.get(), &format, pixels.get(), pixels_buffer_size))
161                                         {
162                                         log_printf("JxlDecoderSetImageOutBuffer failed\n");
163                                         return nullptr;
164                                         }
165                                 }
166                                 break;
167                         case JXL_DEC_FULL_IMAGE:
168                                 // This means the decoder has decoded all pixels into the buffer.
169                                 return pixels.release();
170                         case JXL_DEC_SUCCESS:
171                                 log_printf("Decoding finished before receiving pixel data\n");
172                                 return nullptr;
173                         default:
174                                 log_printf("Unexpected decoder status: %d\n", status);
175                                 return nullptr;
176                         }
177                 }
178
179         return nullptr;
180 }
181
182 gboolean ImageLoaderJPEGXL::write(const guchar *buf, gsize &chunk_size, gsize count, GError **)
183 {
184         gboolean ret = FALSE;
185         size_t xsize;
186         size_t ysize;
187         size_t stride;
188         uint8_t *pixels = nullptr;
189
190         pixels = JxlMemoryToPixels(buf, count, xsize, ysize, stride);
191
192         if (pixels)
193                 {
194                 pixbuf = gdk_pixbuf_new_from_data(pixels, GDK_COLORSPACE_RGB, TRUE, 8, xsize, ysize, stride, free_buffer, nullptr);
195
196                 area_updated_cb(nullptr, 0, 0, xsize, ysize, data);
197
198                 chunk_size = count;
199                 ret = TRUE;
200                 }
201
202         return ret;
203 }
204
205 void ImageLoaderJPEGXL::init(AreaUpdatedCb area_updated_cb, SizePreparedCb, AreaPreparedCb, gpointer data)
206 {
207         this->area_updated_cb = area_updated_cb;
208         this->data = data;
209 }
210
211 GdkPixbuf *ImageLoaderJPEGXL::get_pixbuf()
212 {
213         return pixbuf;
214 }
215
216 gchar *ImageLoaderJPEGXL::get_format_name()
217 {
218         return g_strdup("jxl");
219 }
220
221 gchar **ImageLoaderJPEGXL::get_format_mime_types()
222 {
223         static const gchar *mime[] = {"image/jxl", nullptr};
224         return g_strdupv(const_cast<gchar **>(mime));
225 }
226
227 ImageLoaderJPEGXL::~ImageLoaderJPEGXL()
228 {
229         if (pixbuf) g_object_unref(pixbuf);
230 }
231
232 } // namespace
233
234 std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_jpegxl()
235 {
236         return std::make_unique<ImageLoaderJPEGXL>();
237 }
238
239 #endif
240 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */