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