-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathimage-load-djvu.cc
169 lines (133 loc) · 4.25 KB
/
image-load-djvu.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright (C) 2019 - The Geeqie Team
*
* Author: Colin Clark
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "image-load-djvu.h"
#include <cairo.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <glib-object.h>
#include <glib.h>
#include <libdjvu/ddjvuapi.h>
#include "image-load.h"
namespace
{
struct ImageLoaderDJVU : public ImageLoaderBackend
{
public:
~ImageLoaderDJVU() override;
void init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data) override;
gboolean write(const guchar *buf, gsize &chunk_size, gsize count, GError **error) override;
GdkPixbuf *get_pixbuf() override;
gchar *get_format_name() override;
gchar **get_format_mime_types() override;
void set_page_num(gint page_num) override;
gint get_page_total() override;
private:
AreaUpdatedCb area_updated_cb;
gpointer data;
GdkPixbuf *pixbuf;
gint page_num;
gint page_total;
};
void free_buffer(guchar *pixels, gpointer)
{
g_free (pixels);
}
gboolean ImageLoaderDJVU::write(const guchar *buf, gsize &chunk_size, gsize count, GError **)
{
ddjvu_context_t *ctx;
ddjvu_document_t *doc;
ddjvu_page_t *page;
ddjvu_rect_t rrect;
ddjvu_rect_t prect;
ddjvu_format_t *fmt;
gint width;
gint height;
gint stride;
gboolean alpha = FALSE;
cairo_surface_t *surface;
guchar *pixels;
ctx = ddjvu_context_create(nullptr);
doc = ddjvu_document_create(ctx, nullptr, FALSE);
ddjvu_stream_write(doc, 0, reinterpret_cast<const char *>(buf), count );
while (!ddjvu_document_decoding_done(doc));
page_total = ddjvu_document_get_pagenum(doc);
page = ddjvu_page_create_by_pageno(doc, page_num);
while (!ddjvu_page_decoding_done(page));
fmt = ddjvu_format_create(DDJVU_FORMAT_RGB24, 0, nullptr);
width = ddjvu_page_get_width(page);
height = ddjvu_page_get_height(page);
stride = width * 4;
pixels = static_cast<guchar *>(g_malloc(height * stride));
prect.x = 0;
prect.y = 0;
prect.w = width;
prect.h = height;
rrect = prect;
surface = cairo_image_surface_create_for_data(pixels, CAIRO_FORMAT_RGB24, width, height, stride);
ddjvu_page_render(page, DDJVU_RENDER_COLOR, &prect, &rrect, fmt, stride, reinterpret_cast<char *>(pixels));
/**
* @FIXME implementation of rotation is not correct */
g_autoptr(GdkPixbuf) tmp1 = gdk_pixbuf_new_from_data(pixels, GDK_COLORSPACE_RGB, alpha, 8, width, height, stride, free_buffer, nullptr);
g_autoptr(GdkPixbuf) tmp2 = gdk_pixbuf_flip(tmp1, TRUE);
pixbuf = gdk_pixbuf_rotate_simple(tmp2, GDK_PIXBUF_ROTATE_UPSIDEDOWN);
area_updated_cb(nullptr, 0, 0, width, height, data);
cairo_surface_destroy(surface);
ddjvu_format_release(fmt);
ddjvu_page_release(page);
ddjvu_document_release(doc);
ddjvu_context_release(ctx);
chunk_size = count;
return TRUE;
}
void ImageLoaderDJVU::init(AreaUpdatedCb area_updated_cb, SizePreparedCb, AreaPreparedCb, gpointer data)
{
this->area_updated_cb = area_updated_cb;
this->data = data;
}
GdkPixbuf *ImageLoaderDJVU::get_pixbuf()
{
return pixbuf;
}
gchar *ImageLoaderDJVU::get_format_name()
{
return g_strdup("djvu");
}
gchar **ImageLoaderDJVU::get_format_mime_types()
{
static const gchar *mime[] = {"image/vnd.djvu", nullptr};
return g_strdupv(const_cast<gchar **>(mime));
}
void ImageLoaderDJVU::set_page_num(gint page_num)
{
this->page_num = page_num;
}
gint ImageLoaderDJVU::get_page_total()
{
return page_total;
}
ImageLoaderDJVU::~ImageLoaderDJVU()
{
if (pixbuf) g_object_unref(pixbuf);
}
} // namespace
std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_djvu()
{
return std::make_unique<ImageLoaderDJVU>();
}
/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */