Fix missing translation
[geeqie.git] / src / image-load-j2k.cc
1 /*
2  * Copyright (C) 20019 - 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 #include <config.h>
22
23 #ifdef HAVE_J2K
24
25 #include "image-load-j2k.h"
26
27 #include <openjpeg.h>
28
29 #include "debug.h"
30 #include "image-load.h"
31 #include "intl.h"
32 #include "misc.h"
33
34 struct ImageLoaderJ2K {
35         ImageLoaderBackendCbAreaUpdated area_updated_cb;
36         ImageLoaderBackendCbSize size_cb;
37         ImageLoaderBackendCbAreaPrepared area_prepared_cb;
38         gpointer data;
39         GdkPixbuf *pixbuf;
40         guint requested_width;
41         guint requested_height;
42         gboolean abort;
43 };
44
45 static void free_buffer(guchar *pixels, gpointer)
46 {
47         g_free (pixels);
48 }
49
50 struct opj_buffer_info_t {
51     OPJ_BYTE* buf;
52     OPJ_BYTE* cur;
53     OPJ_SIZE_T len;
54 };
55
56 static OPJ_SIZE_T opj_read_from_buffer (void* pdst, OPJ_SIZE_T len, opj_buffer_info_t* psrc)
57 {
58     OPJ_SIZE_T n = psrc->buf + psrc->len - psrc->cur;
59
60     if (n) {
61         if (n > len)
62             n = len;
63
64         memcpy (pdst, psrc->cur, n);
65         psrc->cur += n;
66     }
67     else
68         n = static_cast<OPJ_SIZE_T>(-1);
69
70     return n;
71 }
72
73 static OPJ_SIZE_T opj_write_to_buffer (void* p_buffer, OPJ_SIZE_T p_nb_bytes,
74                      opj_buffer_info_t* p_source_buffer)
75 {
76     void* pbuf = p_source_buffer->buf;
77     void* pcur = p_source_buffer->cur;
78
79     OPJ_SIZE_T len = p_source_buffer->len;
80
81     if (0 == len)
82         len = 1;
83
84     OPJ_SIZE_T dist = static_cast<guchar *>(pcur) - static_cast<guchar *>(pbuf);
85     OPJ_SIZE_T n = len - dist;
86     g_assert (dist <= len);
87
88     while (n < p_nb_bytes) {
89         len *= 2;
90         n = len - dist;
91     }
92
93     if (len != p_source_buffer->len) {
94         pbuf = malloc (len);
95
96         if (nullptr == pbuf)
97             return static_cast<OPJ_SIZE_T>(-1);
98
99         if (p_source_buffer->buf) {
100             memcpy (pbuf, p_source_buffer->buf, dist);
101             free (p_source_buffer->buf);
102         }
103
104         p_source_buffer->buf = static_cast<OPJ_BYTE *>(pbuf);
105         p_source_buffer->cur = static_cast<guchar *>(pbuf) + dist;
106         p_source_buffer->len = len;
107     }
108
109     memcpy (p_source_buffer->cur, p_buffer, p_nb_bytes);
110     p_source_buffer->cur += p_nb_bytes;
111
112     return p_nb_bytes;
113 }
114
115 static OPJ_SIZE_T opj_skip_from_buffer (OPJ_SIZE_T len, opj_buffer_info_t* psrc)
116 {
117     OPJ_SIZE_T n = psrc->buf + psrc->len - psrc->cur;
118
119     if (n) {
120         if (n > len)
121             n = len;
122
123         psrc->cur += len;
124     }
125     else
126         n = static_cast<OPJ_SIZE_T>(-1);
127
128     return n;
129 }
130
131 static OPJ_BOOL opj_seek_from_buffer (OPJ_OFF_T len, opj_buffer_info_t* psrc)
132 {
133     OPJ_SIZE_T n = psrc->len;
134
135     if (n > static_cast<gulong>(len))
136         n = len;
137
138     psrc->cur = psrc->buf + n;
139
140     return OPJ_TRUE;
141 }
142
143 opj_stream_t* OPJ_CALLCONV opj_stream_create_buffer_stream (opj_buffer_info_t* psrc, OPJ_BOOL input)
144 {
145     if (!psrc)
146         return nullptr;
147
148     opj_stream_t* ps = opj_stream_default_create (input);
149
150     if (nullptr == ps)
151         return nullptr;
152
153     opj_stream_set_user_data        (ps, psrc, nullptr);
154     opj_stream_set_user_data_length (ps, psrc->len);
155
156     if (input)
157         opj_stream_set_read_function (
158             ps, reinterpret_cast<opj_stream_read_fn>(opj_read_from_buffer));
159     else
160         opj_stream_set_write_function(
161             ps,reinterpret_cast<opj_stream_write_fn>(opj_write_to_buffer));
162
163     opj_stream_set_skip_function (
164         ps, reinterpret_cast<opj_stream_skip_fn>(opj_skip_from_buffer));
165
166     opj_stream_set_seek_function (
167         ps, reinterpret_cast<opj_stream_seek_fn>(opj_seek_from_buffer));
168
169     return ps;
170 }
171
172 static gboolean image_loader_j2k_load(gpointer loader, const guchar *buf, gsize count, GError **)
173 {
174         auto ld = static_cast<ImageLoaderJ2K *>(loader);
175         opj_stream_t *stream;
176         opj_codec_t *codec;
177         opj_dparameters_t parameters;
178         opj_image_t *image;
179         gint width;
180         gint height;
181         gint num_components;
182         gint i;
183         gint j;
184         gint k;
185         guchar *pixels;
186         gint  bytes_per_pixel;
187         opj_buffer_info_t *decode_buffer;
188     guchar *buf_copy;
189
190         stream = nullptr;
191         codec = nullptr;
192         image = nullptr;
193
194         buf_copy = static_cast<guchar *>(g_malloc(count));
195         memcpy(buf_copy, buf, count);
196
197         decode_buffer = g_new0(opj_buffer_info_t, 1);
198         decode_buffer->buf = buf_copy;
199         decode_buffer->len = count;
200         decode_buffer->cur = buf_copy;
201
202         stream = opj_stream_create_buffer_stream(decode_buffer, OPJ_TRUE);
203
204         if (!stream)
205                 {
206                 log_printf(_("Could not open file for reading"));
207                 return FALSE;
208                 }
209
210         if (memcmp(buf_copy + 20, "jp2", 3) == 0)
211                 {
212                 codec = opj_create_decompress(OPJ_CODEC_JP2);
213                 }
214         else
215                 {
216                 log_printf(_("Unknown jpeg2000 decoder type"));
217                 return FALSE;
218                 }
219
220         opj_set_default_decoder_parameters(&parameters);
221         if (opj_setup_decoder (codec, &parameters) != OPJ_TRUE)
222                 {
223                 log_printf(_("Couldn't set parameters on decoder for file."));
224                 return FALSE;
225                 }
226
227         opj_codec_set_threads(codec, get_cpu_cores());
228
229         if (opj_read_header(stream, codec, &image) != OPJ_TRUE)
230                 {
231                 log_printf(_("Couldn't read JP2 header from file"));
232                 return FALSE;
233                 }
234
235         if (opj_decode(codec, stream, image) != OPJ_TRUE)
236                 {
237                 log_printf(_("Couldn't decode JP2 image in file"));
238                 return FALSE;
239                 }
240
241         if (opj_end_decompress(codec, stream) != OPJ_TRUE)
242                 {
243                 log_printf(_("Couldn't decompress JP2 image in file"));
244                 return FALSE;
245                 }
246
247         num_components = image->numcomps;
248         if (num_components != 3)
249                 {
250                 log_printf(_("JP2 image not rgb"));
251                 return FALSE;
252                 }
253
254         width = image->comps[0].w;
255         height = image->comps[0].h;
256
257         bytes_per_pixel = 3;
258
259         pixels = g_new0(guchar, width * bytes_per_pixel * height);
260         for (i = 0; i < height; i++)
261                 {
262                 for (j = 0; j < num_components; j++)
263                         {
264                         for (k = 0; k < width; k++)
265                                 {
266                                 pixels[(k * bytes_per_pixel + j) + (i * width * bytes_per_pixel)] =   image->comps[j].data[i * width + k];
267                                 }
268                         }
269                 }
270
271         ld->pixbuf = gdk_pixbuf_new_from_data(pixels, GDK_COLORSPACE_RGB, FALSE , 8, width, height, width * bytes_per_pixel, free_buffer, nullptr);
272
273         ld->area_updated_cb(loader, 0, 0, width, height, ld->data);
274
275         g_free(decode_buffer);
276         g_free(buf_copy);
277         if (image)
278                 opj_image_destroy (image);
279         if (codec)
280                 opj_destroy_codec (codec);
281         if (stream)
282                 opj_stream_destroy (stream);
283
284         return TRUE;
285 }
286
287 static gpointer image_loader_j2k_new(ImageLoaderBackendCbAreaUpdated area_updated_cb, ImageLoaderBackendCbSize size_cb, ImageLoaderBackendCbAreaPrepared area_prepared_cb, gpointer data)
288 {
289         auto loader = g_new0(ImageLoaderJ2K, 1);
290         loader->area_updated_cb = area_updated_cb;
291         loader->size_cb = size_cb;
292         loader->area_prepared_cb = area_prepared_cb;
293         loader->data = data;
294         return loader;
295 }
296
297 static void image_loader_j2k_set_size(gpointer loader, int width, int height)
298 {
299         auto ld = static_cast<ImageLoaderJ2K *>(loader);
300         ld->requested_width = width;
301         ld->requested_height = height;
302 }
303
304 static GdkPixbuf* image_loader_j2k_get_pixbuf(gpointer loader)
305 {
306         auto ld = static_cast<ImageLoaderJ2K *>(loader);
307         return ld->pixbuf;
308 }
309
310 static gchar* image_loader_j2k_get_format_name(gpointer)
311 {
312         return g_strdup("j2k");
313 }
314
315 static gchar** image_loader_j2k_get_format_mime_types(gpointer)
316 {
317         static const gchar *mime[] = {"image/jp2", nullptr};
318         return g_strdupv(const_cast<gchar **>(mime));
319 }
320
321 static gboolean image_loader_j2k_close(gpointer, GError **)
322 {
323         return TRUE;
324 }
325
326 static void image_loader_j2k_abort(gpointer loader)
327 {
328         auto ld = static_cast<ImageLoaderJ2K *>(loader);
329         ld->abort = TRUE;
330 }
331
332 static void image_loader_j2k_free(gpointer loader)
333 {
334         auto ld = static_cast<ImageLoaderJ2K *>(loader);
335         if (ld->pixbuf) g_object_unref(ld->pixbuf);
336         g_free(ld);
337 }
338
339 void image_loader_backend_set_j2k(ImageLoaderBackend *funcs)
340 {
341         funcs->loader_new = image_loader_j2k_new;
342         funcs->set_size = image_loader_j2k_set_size;
343         funcs->load = image_loader_j2k_load;
344         funcs->write = nullptr;
345         funcs->get_pixbuf = image_loader_j2k_get_pixbuf;
346         funcs->close = image_loader_j2k_close;
347         funcs->abort = image_loader_j2k_abort;
348         funcs->free = image_loader_j2k_free;
349         funcs->get_format_name = image_loader_j2k_get_format_name;
350         funcs->get_format_mime_types = image_loader_j2k_get_format_mime_types;
351 }
352
353 #endif
354 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */