Move third-party sources to separate sub-directory
[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 : public ImageLoaderBackend
39 {
40 public:
41         ~ImageLoaderDJVU() override;
42
43         void init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data) override;
44         gboolean write(const guchar *buf, gsize &chunk_size, gsize count, GError **error) override;
45         GdkPixbuf *get_pixbuf() override;
46         gchar *get_format_name() override;
47         gchar **get_format_mime_types() override;
48         void set_page_num(gint page_num) override;
49         gint get_page_total() override;
50
51 private:
52         AreaUpdatedCb area_updated_cb;
53         gpointer data;
54
55         GdkPixbuf *pixbuf;
56         gint page_num;
57         gint page_total;
58 };
59
60 void free_buffer(guchar *pixels, gpointer)
61 {
62         g_free (pixels);
63 }
64
65 gboolean ImageLoaderDJVU::write(const guchar *buf, gsize &chunk_size, gsize count, GError **)
66 {
67         ddjvu_context_t *ctx;
68         ddjvu_document_t *doc;
69         ddjvu_page_t *page;
70         ddjvu_rect_t rrect;
71         ddjvu_rect_t prect;
72         ddjvu_format_t *fmt;
73         gint width;
74         gint height;
75         gint stride;
76         gboolean alpha = FALSE;
77         cairo_surface_t *surface;
78         guchar *pixels;
79
80         ctx = ddjvu_context_create(nullptr);
81
82         doc = ddjvu_document_create(ctx, nullptr, FALSE);
83
84         ddjvu_stream_write(doc, 0, reinterpret_cast<const char *>(buf), count );
85         while (!ddjvu_document_decoding_done(doc));
86
87         page_total = ddjvu_document_get_pagenum(doc);
88
89         page = ddjvu_page_create_by_pageno(doc, page_num);
90         while (!ddjvu_page_decoding_done(page));
91
92         fmt = ddjvu_format_create(DDJVU_FORMAT_RGB24, 0, nullptr);
93
94         width = ddjvu_page_get_width(page);
95         height = ddjvu_page_get_height(page);
96         stride = width * 4;
97
98         pixels = static_cast<guchar *>(g_malloc(height * stride));
99
100         prect.x = 0;
101         prect.y = 0;
102         prect.w = width;
103         prect.h = height;
104         rrect = prect;
105
106         surface = cairo_image_surface_create_for_data(pixels, CAIRO_FORMAT_RGB24, width, height, stride);
107
108         ddjvu_page_render(page, DDJVU_RENDER_COLOR, &prect, &rrect, fmt, stride, reinterpret_cast<char *>(pixels));
109
110         /**
111          * @FIXME implementation of rotation is not correct */
112         GdkPixbuf *tmp1;
113         GdkPixbuf *tmp2;
114         tmp1 = gdk_pixbuf_new_from_data(pixels, GDK_COLORSPACE_RGB, alpha, 8, width, height, stride, free_buffer, nullptr);
115         tmp2 = gdk_pixbuf_flip(tmp1, TRUE);
116         g_object_unref(tmp1);
117
118         pixbuf = gdk_pixbuf_rotate_simple(tmp2, GDK_PIXBUF_ROTATE_UPSIDEDOWN);
119
120         area_updated_cb(nullptr, 0, 0, width, height, data);
121
122         cairo_surface_destroy(surface);
123         ddjvu_page_release(page);
124         ddjvu_document_release(doc);
125         ddjvu_context_release(ctx);
126
127         chunk_size = count;
128         return TRUE;
129 }
130
131 void ImageLoaderDJVU::init(AreaUpdatedCb area_updated_cb, SizePreparedCb, AreaPreparedCb, gpointer data)
132 {
133         this->area_updated_cb = area_updated_cb;
134         this->data = data;
135 }
136
137 GdkPixbuf *ImageLoaderDJVU::get_pixbuf()
138 {
139         return pixbuf;
140 }
141
142 gchar *ImageLoaderDJVU::get_format_name()
143 {
144         return g_strdup("djvu");
145 }
146
147 gchar **ImageLoaderDJVU::get_format_mime_types()
148 {
149         static const gchar *mime[] = {"image/vnd.djvu", nullptr};
150         return g_strdupv(const_cast<gchar **>(mime));
151 }
152
153 void ImageLoaderDJVU::set_page_num(gint page_num)
154 {
155         this->page_num = page_num;
156 }
157
158 gint ImageLoaderDJVU::get_page_total()
159 {
160         return page_total;
161 }
162
163 ImageLoaderDJVU::~ImageLoaderDJVU()
164 {
165         if (pixbuf) g_object_unref(pixbuf);
166 }
167
168 } // namespace
169
170 std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_djvu()
171 {
172         return std::make_unique<ImageLoaderDJVU>();
173 }
174
175 #endif
176 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */