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