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