2 * Copyright (C) 2019 - 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"
24 #include <gdk-pixbuf/gdk-pixbuf.h>
25 #include <glib-object.h>
27 #include <libdjvu/ddjvuapi.h>
29 #include "image-load.h"
34 struct ImageLoaderDJVU : public ImageLoaderBackend
37 ~ImageLoaderDJVU() override;
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;
48 AreaUpdatedCb area_updated_cb;
56 void free_buffer(guchar *pixels, gpointer)
61 gboolean ImageLoaderDJVU::write(const guchar *buf, gsize &chunk_size, gsize count, GError **)
64 ddjvu_document_t *doc;
72 gboolean alpha = FALSE;
73 cairo_surface_t *surface;
76 ctx = ddjvu_context_create(nullptr);
78 doc = ddjvu_document_create(ctx, nullptr, FALSE);
80 ddjvu_stream_write(doc, 0, reinterpret_cast<const char *>(buf), count );
81 while (!ddjvu_document_decoding_done(doc));
83 page_total = ddjvu_document_get_pagenum(doc);
85 page = ddjvu_page_create_by_pageno(doc, page_num);
86 while (!ddjvu_page_decoding_done(page));
88 fmt = ddjvu_format_create(DDJVU_FORMAT_RGB24, 0, nullptr);
90 width = ddjvu_page_get_width(page);
91 height = ddjvu_page_get_height(page);
94 pixels = static_cast<guchar *>(g_malloc(height * stride));
102 surface = cairo_image_surface_create_for_data(pixels, CAIRO_FORMAT_RGB24, width, height, stride);
104 ddjvu_page_render(page, DDJVU_RENDER_COLOR, &prect, &rrect, fmt, stride, reinterpret_cast<char *>(pixels));
107 * @FIXME implementation of rotation is not correct */
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);
114 pixbuf = gdk_pixbuf_rotate_simple(tmp2, GDK_PIXBUF_ROTATE_UPSIDEDOWN);
116 area_updated_cb(nullptr, 0, 0, width, height, data);
118 cairo_surface_destroy(surface);
119 ddjvu_page_release(page);
120 ddjvu_document_release(doc);
121 ddjvu_context_release(ctx);
127 void ImageLoaderDJVU::init(AreaUpdatedCb area_updated_cb, SizePreparedCb, AreaPreparedCb, gpointer data)
129 this->area_updated_cb = area_updated_cb;
133 GdkPixbuf *ImageLoaderDJVU::get_pixbuf()
138 gchar *ImageLoaderDJVU::get_format_name()
140 return g_strdup("djvu");
143 gchar **ImageLoaderDJVU::get_format_mime_types()
145 static const gchar *mime[] = {"image/vnd.djvu", nullptr};
146 return g_strdupv(const_cast<gchar **>(mime));
149 void ImageLoaderDJVU::set_page_num(gint page_num)
151 this->page_num = page_num;
154 gint ImageLoaderDJVU::get_page_total()
159 ImageLoaderDJVU::~ImageLoaderDJVU()
161 if (pixbuf) g_object_unref(pixbuf);
166 std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_djvu()
168 return std::make_unique<ImageLoaderDJVU>();
171 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */