Fix deprecation warning for poppler >= 0.82
[geeqie.git] / src / image-load-djvu.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 "image-load-djvu.h"
22
23 #include <config.h>
24
25 #if HAVE_DJVU
26
27 #include <cairo.h>
28 #include <gdk-pixbuf/gdk-pixbuf.h>
29 #include <glib-object.h>
30 #include <glib.h>
31 #include <libdjvu/ddjvuapi.h>
32
33 #include "image-load.h"
34
35 namespace
36 {
37
38 struct ImageLoaderDJVU {
39         ImageLoaderBackendCbAreaUpdated area_updated_cb;
40         ImageLoaderBackendCbSize size_cb;
41         ImageLoaderBackendCbAreaPrepared area_prepared_cb;
42         gpointer data;
43         GdkPixbuf *pixbuf;
44         guint requested_width;
45         guint requested_height;
46         gboolean abort;
47         gint page_num;
48         gint page_total;
49 };
50
51 void free_buffer(guchar *pixels, gpointer)
52 {
53         g_free (pixels);
54 }
55
56 gboolean image_loader_djvu_write(gpointer loader, const guchar *buf, gsize &chunk_size, gsize count, GError **)
57 {
58         auto ld = static_cast<ImageLoaderDJVU *>(loader);
59         ddjvu_context_t *ctx;
60         ddjvu_document_t *doc;
61         ddjvu_page_t *page;
62         ddjvu_rect_t rrect;
63         ddjvu_rect_t prect;
64         ddjvu_format_t *fmt;
65         gint width;
66         gint height;
67         gint stride;
68         gboolean alpha = FALSE;
69         cairo_surface_t *surface;
70         guchar *pixels;
71
72         ctx = ddjvu_context_create(nullptr);
73
74         doc = ddjvu_document_create(ctx, nullptr, FALSE);
75
76         ddjvu_stream_write(doc, 0, reinterpret_cast<const char *>(buf), count );
77         while (!ddjvu_document_decoding_done(doc));
78
79         ld->page_total = ddjvu_document_get_pagenum(doc);
80
81         page = ddjvu_page_create_by_pageno(doc, ld->page_num);
82         while (!ddjvu_page_decoding_done(page));
83
84         fmt = ddjvu_format_create(DDJVU_FORMAT_RGB24, 0, nullptr);
85
86         width = ddjvu_page_get_width(page);
87         height = ddjvu_page_get_height(page);
88         stride = width * 4;
89
90         pixels = static_cast<guchar *>(g_malloc(height * stride));
91
92         prect.x = 0;
93         prect.y = 0;
94         prect.w = width;
95         prect.h = height;
96         rrect = prect;
97
98         surface = cairo_image_surface_create_for_data(pixels, CAIRO_FORMAT_RGB24, width, height, stride);
99
100         ddjvu_page_render(page, DDJVU_RENDER_COLOR, &prect, &rrect, fmt, stride, reinterpret_cast<char *>(pixels));
101
102         /**
103          * @FIXME implementation of rotation is not correct */
104         GdkPixbuf *tmp1;
105         GdkPixbuf *tmp2;
106         tmp1 = gdk_pixbuf_new_from_data(pixels, GDK_COLORSPACE_RGB, alpha, 8, width, height, stride, free_buffer, nullptr);
107         tmp2 = gdk_pixbuf_flip(tmp1, TRUE);
108         g_object_unref(tmp1);
109
110         ld->pixbuf = gdk_pixbuf_rotate_simple(tmp2,GDK_PIXBUF_ROTATE_UPSIDEDOWN);
111
112         ld->area_updated_cb(loader, 0, 0, width, height, ld->data);
113
114
115         cairo_surface_destroy(surface);
116         ddjvu_page_release(page);
117         ddjvu_document_release(doc);
118         ddjvu_context_release(ctx);
119
120         chunk_size = count;
121         return TRUE;
122 }
123
124 gpointer image_loader_djvu_new(ImageLoaderBackendCbAreaUpdated area_updated_cb, ImageLoaderBackendCbSize size_cb, ImageLoaderBackendCbAreaPrepared area_prepared_cb, gpointer data)
125 {
126         auto loader = g_new0(ImageLoaderDJVU, 1);
127         loader->area_updated_cb = area_updated_cb;
128         loader->size_cb = size_cb;
129         loader->area_prepared_cb = area_prepared_cb;
130         loader->data = data;
131         return loader;
132 }
133
134 void image_loader_djvu_set_size(gpointer loader, int width, int height)
135 {
136         auto ld = static_cast<ImageLoaderDJVU *>(loader);
137         ld->requested_width = width;
138         ld->requested_height = height;
139 }
140
141 GdkPixbuf* image_loader_djvu_get_pixbuf(gpointer loader)
142 {
143         auto ld = static_cast<ImageLoaderDJVU *>(loader);
144         return ld->pixbuf;
145 }
146
147 gchar* image_loader_djvu_get_format_name(gpointer)
148 {
149         return g_strdup("djvu");
150 }
151
152 gchar** image_loader_djvu_get_format_mime_types(gpointer)
153 {
154         static const gchar *mime[] = {"image/vnd.djvu", nullptr};
155         return g_strdupv(const_cast<gchar **>(mime));
156 }
157
158 void image_loader_djvu_set_page_num(gpointer loader, gint page_num)
159 {
160         auto ld = static_cast<ImageLoaderDJVU *>(loader);
161
162         ld->page_num = page_num;
163 }
164
165 gint image_loader_djvu_get_page_total(gpointer loader)
166 {
167         auto ld = static_cast<ImageLoaderDJVU *>(loader);
168
169         return ld->page_total;
170 }
171
172 gboolean image_loader_djvu_close(gpointer, GError **)
173 {
174         return TRUE;
175 }
176
177 void image_loader_djvu_abort(gpointer loader)
178 {
179         auto ld = static_cast<ImageLoaderDJVU *>(loader);
180         ld->abort = TRUE;
181 }
182
183 void image_loader_djvu_free(gpointer loader)
184 {
185         auto ld = static_cast<ImageLoaderDJVU *>(loader);
186         if (ld->pixbuf) g_object_unref(ld->pixbuf);
187         g_free(ld);
188 }
189
190 } // namespace
191
192 void image_loader_backend_set_djvu(ImageLoaderBackend *funcs)
193 {
194         funcs->loader_new = image_loader_djvu_new;
195         funcs->set_size = image_loader_djvu_set_size;
196         funcs->write = image_loader_djvu_write;
197         funcs->get_pixbuf = image_loader_djvu_get_pixbuf;
198         funcs->close = image_loader_djvu_close;
199         funcs->abort = image_loader_djvu_abort;
200         funcs->free = image_loader_djvu_free;
201         funcs->get_format_name = image_loader_djvu_get_format_name;
202         funcs->get_format_mime_types = image_loader_djvu_get_format_mime_types;
203         funcs->set_page_num = image_loader_djvu_set_page_num;
204         funcs->get_page_total = image_loader_djvu_get_page_total;
205 }
206
207 #endif
208 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */