2 * Copyright (C) 20019 - The Geeqie Team
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.
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.
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.
21 #include "image-load-djvu.h"
28 #include <gdk-pixbuf/gdk-pixbuf.h>
29 #include <glib-object.h>
31 #include <libdjvu/ddjvuapi.h>
33 #include "image-load.h"
38 struct ImageLoaderDJVU : public ImageLoaderBackend
41 ~ImageLoaderDJVU() override;
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;
52 AreaUpdatedCb area_updated_cb;
60 void free_buffer(guchar *pixels, gpointer)
65 gboolean ImageLoaderDJVU::write(const guchar *buf, gsize &chunk_size, gsize count, GError **)
68 ddjvu_document_t *doc;
76 gboolean alpha = FALSE;
77 cairo_surface_t *surface;
80 ctx = ddjvu_context_create(nullptr);
82 doc = ddjvu_document_create(ctx, nullptr, FALSE);
84 ddjvu_stream_write(doc, 0, reinterpret_cast<const char *>(buf), count );
85 while (!ddjvu_document_decoding_done(doc));
87 page_total = ddjvu_document_get_pagenum(doc);
89 page = ddjvu_page_create_by_pageno(doc, page_num);
90 while (!ddjvu_page_decoding_done(page));
92 fmt = ddjvu_format_create(DDJVU_FORMAT_RGB24, 0, nullptr);
94 width = ddjvu_page_get_width(page);
95 height = ddjvu_page_get_height(page);
98 pixels = static_cast<guchar *>(g_malloc(height * stride));
106 surface = cairo_image_surface_create_for_data(pixels, CAIRO_FORMAT_RGB24, width, height, stride);
108 ddjvu_page_render(page, DDJVU_RENDER_COLOR, &prect, &rrect, fmt, stride, reinterpret_cast<char *>(pixels));
111 * @FIXME implementation of rotation is not correct */
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);
118 pixbuf = gdk_pixbuf_rotate_simple(tmp2, GDK_PIXBUF_ROTATE_UPSIDEDOWN);
120 area_updated_cb(nullptr, 0, 0, width, height, data);
122 cairo_surface_destroy(surface);
123 ddjvu_page_release(page);
124 ddjvu_document_release(doc);
125 ddjvu_context_release(ctx);
131 void ImageLoaderDJVU::init(AreaUpdatedCb area_updated_cb, SizePreparedCb, AreaPreparedCb, gpointer data)
133 this->area_updated_cb = area_updated_cb;
137 GdkPixbuf *ImageLoaderDJVU::get_pixbuf()
142 gchar *ImageLoaderDJVU::get_format_name()
144 return g_strdup("djvu");
147 gchar **ImageLoaderDJVU::get_format_mime_types()
149 static const gchar *mime[] = {"image/vnd.djvu", nullptr};
150 return g_strdupv(const_cast<gchar **>(mime));
153 void ImageLoaderDJVU::set_page_num(gint page_num)
155 this->page_num = page_num;
158 gint ImageLoaderDJVU::get_page_total()
163 ImageLoaderDJVU::~ImageLoaderDJVU()
165 if (pixbuf) g_object_unref(pixbuf);
170 std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_djvu()
172 return std::make_unique<ImageLoaderDJVU>();
176 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */