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