| 1 |
/* |
| 2 |
* This file is part of the apvlv package |
| 3 |
* Copyright (C) <2010> <Alf> |
| 4 |
* |
| 5 |
* Contact: Alf <[email protected]> |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License along |
| 18 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 |
* |
| 21 |
*/ |
| 22 |
/* @CPPFILE ApvlvDjvu.cc |
| 23 |
* |
| 24 |
* Author: Alf <[email protected]> |
| 25 |
*/ |
| 26 |
|
| 27 |
#include <QDebug> |
| 28 |
|
| 29 |
#include "ApvlvDjvu.h" |
| 30 |
#include "ApvlvUtil.h" |
| 31 |
|
| 32 |
namespace apvlv |
| 33 |
{ |
| 34 |
using namespace std; |
| 35 |
|
| 36 |
void |
| 37 |
handleDdjvuMessages (ddjvu_context_t *ctx, int wait) |
| 38 |
{ |
| 39 |
const ddjvu_message_t *msg; |
| 40 |
if (wait) |
| 41 |
ddjvu_message_wait (ctx); |
| 42 |
while ((msg = ddjvu_message_peek (ctx))) |
| 43 |
{ |
| 44 |
qDebug () << "tag: " << msg->m_any.tag; |
| 45 |
switch (msg->m_any.tag) |
| 46 |
{ |
| 47 |
case DDJVU_ERROR: |
| 48 |
break; |
| 49 |
case DDJVU_INFO: |
| 50 |
break; |
| 51 |
case DDJVU_PAGEINFO: |
| 52 |
break; |
| 53 |
default: |
| 54 |
break; |
| 55 |
} |
| 56 |
ddjvu_message_pop (ctx); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
FILE_TYPE_DEFINITION ("djvulibre", ApvlvDJVU, { ".djv", ".djvu" }); |
| 61 |
|
| 62 |
bool |
| 63 |
ApvlvDJVU::load (const string &filename) |
| 64 |
{ |
| 65 |
mContext = ddjvu_context_create ("apvlv"); |
| 66 |
if (mContext == nullptr) |
| 67 |
{ |
| 68 |
qCritical () << "djvu context error"; |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
mDoc |
| 73 |
= ddjvu_document_create_by_filename (mContext, filename.c_str (), false); |
| 74 |
if (mDoc == nullptr) |
| 75 |
{ |
| 76 |
qCritical ("djvu create document error"); |
| 77 |
ddjvu_context_release (mContext); |
| 78 |
mContext = nullptr; |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
if (ddjvu_document_get_type (mDoc) != DDJVU_DOCTYPE_SINGLEPAGE) |
| 83 |
{ |
| 84 |
qCritical () << "djvu type: " << ddjvu_document_get_type (mDoc); |
| 85 |
ddjvu_document_release (mDoc); |
| 86 |
mDoc = nullptr; |
| 87 |
ddjvu_context_release (mContext); |
| 88 |
mContext = nullptr; |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
return true; |
| 93 |
} |
| 94 |
|
| 95 |
ApvlvDJVU::~ApvlvDJVU () |
| 96 |
{ |
| 97 |
if (mContext) |
| 98 |
{ |
| 99 |
ddjvu_context_release (mContext); |
| 100 |
} |
| 101 |
|
| 102 |
if (mDoc) |
| 103 |
{ |
| 104 |
ddjvu_document_release (mDoc); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
SizeF |
| 109 |
ApvlvDJVU::pageSizeF (int pn, int rot) |
| 110 |
{ |
| 111 |
ddjvu_status_t t; |
| 112 |
ddjvu_pageinfo_t info; |
| 113 |
while ((t = ddjvu_document_get_pageinfo (mDoc, 0, &info)) < DDJVU_JOB_OK) |
| 114 |
{ |
| 115 |
handleDdjvuMessages (mContext, true); |
| 116 |
} |
| 117 |
|
| 118 |
SizeF sizef{ 0.0f, 0.0f }; |
| 119 |
if (t == DDJVU_JOB_OK) |
| 120 |
{ |
| 121 |
sizef.width = static_cast<double> (info.width); |
| 122 |
sizef.height = static_cast<double> (info.height); |
| 123 |
} |
| 124 |
return sizef; |
| 125 |
} |
| 126 |
|
| 127 |
int |
| 128 |
ApvlvDJVU::sum () |
| 129 |
{ |
| 130 |
return mDoc ? ddjvu_document_get_pagenum (mDoc) : 0; |
| 131 |
} |
| 132 |
|
| 133 |
bool |
| 134 |
ApvlvDJVU::pageRenderToImage (int pn, double zm, int rot, QImage *pix) |
| 135 |
{ |
| 136 |
ddjvu_page_t *tpage; |
| 137 |
|
| 138 |
if ((tpage = ddjvu_page_create_by_pageno (mDoc, pn)) == nullptr) |
| 139 |
{ |
| 140 |
qDebug () << "no this page: " << pn; |
| 141 |
return false; |
| 142 |
} |
| 143 |
|
| 144 |
auto size = pageSizeF (pn, rot); |
| 145 |
|
| 146 |
auto dx = size.width * zm; |
| 147 |
auto dy = size.height * zm; |
| 148 |
|
| 149 |
auto ix = static_cast<int> (dx); |
| 150 |
auto iy = static_cast<int> (dy); |
| 151 |
ddjvu_rect_t prect = { 0, 0, static_cast<unsigned int> (ix), |
| 152 |
static_cast<unsigned int> (iy) }; |
| 153 |
ddjvu_rect_t rrect = { 0, 0, static_cast<unsigned int> (ix), |
| 154 |
static_cast<unsigned int> (iy) }; |
| 155 |
ddjvu_format_t *format |
| 156 |
= ddjvu_format_create (DDJVU_FORMAT_RGB24, 0, nullptr); |
| 157 |
ddjvu_format_set_row_order (format, true); |
| 158 |
|
| 159 |
auto psize = 3 * ix * iy; |
| 160 |
auto buffer = make_unique<char[]> (psize); |
| 161 |
|
| 162 |
int retry = 0; |
| 163 |
while (retry <= 20 |
| 164 |
&& ddjvu_page_render (tpage, DDJVU_RENDER_COLOR, &prect, &rrect, |
| 165 |
format, 3 * ix, buffer.get ()) |
| 166 |
== false) |
| 167 |
{ |
| 168 |
this_thread::sleep_for (50ms); |
| 169 |
++retry; |
| 170 |
qDebug () << "fender failed, retry " << retry; |
| 171 |
} |
| 172 |
|
| 173 |
auto image = QImage (ix, iy, QImage::Format_RGB888); |
| 174 |
for (auto y = 0; y < iy; ++y) |
| 175 |
{ |
| 176 |
for (auto x = 0; x < ix; ++x) |
| 177 |
{ |
| 178 |
auto rgb = buffer.get () + 3 * (x + y * ix); |
| 179 |
image.setPixel (x, y, qRgb (rgb[0], rgb[1], rgb[2])); |
| 180 |
} |
| 181 |
} |
| 182 |
*pix = std::move (image); |
| 183 |
|
| 184 |
return true; |
| 185 |
} |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
// Local Variables: |
| 190 |
// mode: c++ |
| 191 |
// End: |