-
Notifications
You must be signed in to change notification settings - Fork 0
/
Renderer.cpp
189 lines (174 loc) · 5.99 KB
/
Renderer.cpp
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "Renderer.hpp"
#include <QImage>
#include <poppler/qt5/poppler-qt5.h>
#include <iostream>
#include <fcntl.h>
#include <sys/mman.h>
#include <cstdio>
//----------------------------------------------------------------------------
using namespace std;
//----------------------------------------------------------------------------
Renderer::Renderer()
: doc(0),file(0),cacheStart(0),cacheEnd(0),stopped(false),mustStop(false)
// Constructor
{
}
//----------------------------------------------------------------------------
Renderer::~Renderer()
// Destructor
{
cleanup();
}
//----------------------------------------------------------------------------
static unsigned long maxSizeBytes(const QSize& size)
// Estimate the maximum space consumpton in bytes
{
unsigned long pixels=static_cast<unsigned long>(size.width()+1)*static_cast<unsigned long>(size.height()+1);
unsigned long bytes=4*pixels;
// Add some safety
return bytes+100+(bytes/8);
}
//----------------------------------------------------------------------------
bool Renderer::prepare(Poppler::Document& doc,const QSize& imageSize,const QSize& thumbSize)
// Prepare the rendering. Must be called before run or starting a thread
{
cleanup();
this->doc=&doc;
this->imageSize=imageSize;
this->thumbSize=thumbSize;
images.resize(doc.numPages());
thumbnails.resize(doc.numPages());
darkThumbnails.resize(doc.numPages());
//doc.setRenderBackend(Poppler::Document::ArthurBackend);
doc.setRenderHint(Poppler::Document::Antialiasing);
doc.setRenderHint(Poppler::Document::TextAntialiasing);
// Allocate a cache file
unsigned long reservedSpace=(maxSizeBytes(imageSize)+2*maxSizeBytes(thumbSize))*(images.size()+1);
file=tmpfile();
int fd=fileno(static_cast<FILE*>(file));
if (posix_fallocate(fd,0,reservedSpace)) {
cerr << "unable to allocate " << (reservedSpace/1024/1024) << " MB for caching." << endl;
fclose(static_cast<FILE*>(file));
file=0;
return false;
}
void* mapping=mmap(0,reservedSpace,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
if (mapping==MAP_FAILED) {
cerr << "unable to map cache into memory" << endl;
fclose(static_cast<FILE*>(file));
file=0;
return false;
}
cacheStart=static_cast<unsigned char*>(mapping);
cacheEnd=cacheStart+reservedSpace;
return true;
}
//----------------------------------------------------------------------------
void Renderer::cleanup()
// Cleanup
{
for (unsigned index=0;index<images.size();index++) {
delete images[index]; images[index]=0;
delete thumbnails[index]; thumbnails[index]=0;
delete darkThumbnails[index]; darkThumbnails[index]=0;
}
images.clear();
thumbnails.clear();
if (cacheStart) {
munmap(cacheStart,cacheEnd-cacheStart);
cacheStart=cacheEnd=0;
}
if (file) {
fclose(static_cast<FILE*>(file));
file=0;
}
}
//----------------------------------------------------------------------------
void Renderer::run()
// Render the images
{
// Render all pages
unsigned char* writer=cacheStart;
#pragma omp parallel for schedule(dynamic)
for (unsigned index=0;index<images.size();index++) {
if (mustStop) { // break
index=images.size();
continue;
}
delete images[index]; images[index]=0;
delete thumbnails[index]; thumbnails[index]=0;
delete darkThumbnails[index]; darkThumbnails[index]=0;
// Compute the desired DPI
Poppler::Page* page=doc->page(index);
double DPIx=static_cast<double>(imageSize.width())/(page->pageSizeF().width()/72.0);
double DPIy=static_cast<double>(imageSize.height())/(page->pageSizeF().height()/72.0);
double DPI=(DPIx<DPIy)?DPIx:DPIy;
// Render
QImage rawImg=page->renderToImage(DPI,DPI);
if (rawImg.isNull()) {
cerr << "unable to render page " << (index+1) << endl;
continue;
}
QImage img=rawImg.convertToFormat(QImage::Format_RGB32);
// Store in cache file
unsigned len=img.byteCount();
unsigned char* imgWriter;
#pragma omp critical(writer)
{
if (writer+len>cacheEnd) {
cerr << "out of cache space, stopping rendering!" << endl;
throw;
}
imgWriter=writer;
writer+=len;
}
memcpy(imgWriter,img.bits(),len);
images[index]=new QImage(imgWriter,img.width(),img.height(),img.bytesPerLine(),img.format());
// Create a thumbnail
QImage thumb=img.scaled(thumbSize,Qt::KeepAspectRatio,Qt::SmoothTransformation);
len=thumb.byteCount();
unsigned char* thumbWriter;
#pragma omp critical(writer)
{
if (writer+len>cacheEnd) {
cerr << "out of cache space, stopping rendering!" << endl;
throw;
}
thumbWriter=writer;
writer+=len;
}
memcpy(thumbWriter,thumb.bits(),len);
thumbnails[index]=new QImage(thumbWriter,thumb.width(),thumb.height(),thumb.bytesPerLine(),thumb.format());
thumbWriter+=len;
unsigned char* darkThumbWriter;
#pragma omp critical(writer)
{
if (writer+len>cacheEnd) {
cerr << "out of cache space, stopping rendering!" << endl;
throw;
}
darkThumbWriter=writer;
writer+=len;
}
// Create a grayed thumnail
unsigned char* reader=thumb.bits();
for (unsigned index2=0;index2<len;index2++)
darkThumbWriter[index2]=reader[index2]/2;
darkThumbnails[index]=new QImage(darkThumbWriter,thumb.width(),thumb.height(),thumb.bytesPerLine(),thumb.format());
darkThumbWriter+=len;
/// Notify
QMetaObject::invokeMethod(this,"pageRendered",Qt::QueuedConnection,Q_ARG(unsigned,index));
}
stopped=true;
}
//----------------------------------------------------------------------------
void Renderer::stop()
// Stop the rendered
{
if (!stopped) {
mustStop=true;
while (!stopped)
QThread::msleep(10);
}
}
//----------------------------------------------------------------------------