forked from dzindra/pdfpreview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdfpreview.cpp
executable file
·209 lines (179 loc) · 6.82 KB
/
pdfpreview.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//
// pdfpreview
//
// Copyright (c) 2015 Jindrich Dolezy <jindrich@dolezy.cz>
//
// Licensed under GPL version 2
//
#include <stdio.h>
#include <math.h>
#include "poppler/goo/GooString.h"
#include "poppler/GlobalParams.h"
#include "poppler/PDFDoc.h"
#include "poppler/PDFDocFactory.h"
#include "poppler/SplashOutputDev.h"
#include "poppler/splash/SplashBitmap.h"
#include "poppler/splash/Splash.h"
// Enabling this will fill background of image boxes with different colors, so they are easily recognizable.
//#define SQUARE_FILL
// SplashBitmap does not provide info about bytes per pixel, so we lazily hardcode it here
#define BPP 3
// Fills rectangle on given bitmap with color c. Does not perform any range checks.
static void fillRect(SplashBitmap *bitmap, int x, int y, int w, int h, int c) {
for (int ypos = y; ypos < y + h; ypos++) {
memset(bitmap->getDataPtr() + ypos * bitmap->getRowSize() + BPP * x, c, (size_t) (w * BPP));
}
}
// Draws whole source bitmap to dest bitmap at position x,y. Does not perform any range checks.
static void drawBitmap(SplashBitmap *source, SplashBitmap *dest, int x, int y) {
for (int ypos = 0; ypos < source->getHeight(); ypos++) {
memcpy(dest->getDataPtr() + (ypos + y) * dest->getRowSize() + BPP * x, source->getDataPtr() + ypos * source->getRowSize(), (size_t) source->getWidth() * BPP);
}
}
// Draws page pg from document doc using splashOut. Image is resized to fit box_w x box_h pixels. Resulting image is either written to stdout if
// canvas is NULL or drawn to its box on canvas based on page number.
static void drawPage(PDFDoc *doc, SplashOutputDev *splashOut, SplashBitmap *canvas, int pg, int box_w, int box_h, int box_x, int box_y, GBool useCropBox) {
double pg_w, pg_h;
if (useCropBox) {
pg_w = doc->getPageCropWidth(pg);
pg_h = doc->getPageCropHeight(pg);
} else {
pg_w = doc->getPageMediaWidth(pg);
pg_h = doc->getPageMediaHeight(pg);
}
// Find resolution that fits to box_w x box_h
double resolution = std::min((72.0 * box_w) / pg_w, (72.0 * box_h) / pg_h);
pg_w = pg_w * (resolution / 72.0);
pg_h = pg_h * (resolution / 72.0);
// Rotate page if needed
if ((doc->getPageRotate(pg) == 90) || (doc->getPageRotate(pg) == -90) || (doc->getPageRotate(pg) == 270)) {
double tmp = pg_w;
pg_w = pg_h;
pg_h = tmp;
}
// Round page dimensions and eliminate rounding errors.
int w = std::min((int) ceil(pg_w), box_w);
int h = std::min((int) ceil(pg_h), box_h);
doc->displayPageSlice(splashOut,
pg, resolution, resolution,
0,
!useCropBox, gFalse, gFalse,
0, 0, w, h
);
if (canvas == NULL)
splashOut->getBitmap()->writeImgFile(splashFormatJpeg, stdout, 300, 300);
else
drawBitmap(splashOut->getBitmap(), canvas, box_x * box_w + (box_w - w) / 2, box_y * box_h + (box_h - h) / 2);
}
int main(int argc, char *argv[]) {
GBool debug = gFalse;
SplashBitmap *canvas = NULL;
PDFDoc *doc;
SplashOutputDev *splashOut;
SplashColor paperColor = {255, 255, 255, 0};
int box_w, box_h, box_xnum;
int firstPage = 1;
int lastPage = -1;
// Poor man's argument parsing.
if (argc < 4) {
fprintf(stderr, "Usage:\n\tpdfpreview <box width> <box height> <box max x> [first page] [last page] [verbose 0|1]\n");
return 2;
}
box_w = atoi(argv[1]);
if (box_w <= 0) {
fprintf(stderr, "Box width must be positive number.\n");
return 2;
}
box_h = atoi(argv[2]);
if (box_h <= 0) {
fprintf(stderr, "Box height must be positive number.\n");
return 2;
}
box_xnum = atoi(argv[3]);
if (box_xnum <= 0) {
fprintf(stderr, "Box max x must be positive number.\n");
return 2;
}
if (argc > 4) {
firstPage = atoi(argv[4]);
}
if (argc > 5) {
lastPage = atoi(argv[5]);
}
if (argc > 6) {
debug = atoi(argv[6]) ? gTrue : gFalse;
}
if (debug)
fprintf(stderr, "fist_page: %d\nlast_page: %d\nbox_w: %d\nbox_h: %d\nbox_xnum: %d\n", firstPage, lastPage, box_w, box_h, box_xnum);
// Open pdf document.
globalParams = new GlobalParams(); // this must be created, or pdf render will crash
{
GooString *fileName = new GooString("fd://0"); // read from stdin
doc = PDFDocFactory().createPDFDoc(*fileName, NULL, NULL);
delete fileName;
}
if (!doc->isOk()) {
delete doc;
delete globalParams;
return 1;
}
// Check page ranges ( 1 <= firstPage <= lastPage <= pages in document )
if (firstPage < 1) {
fprintf(stderr, "Adjusted first page from %d to 1.\n", firstPage);
firstPage = 1;
}
if (lastPage < 1 || lastPage > doc->getNumPages()) {
if (lastPage != -1) // only warn when page count set
fprintf(stderr, "Adjusted last page from %d to %d.\n", lastPage, doc->getNumPages());
lastPage = doc->getNumPages();
}
if (lastPage < firstPage) {
fprintf(stderr, "Wrong page range given: the first page (%d) can not be after the last page (%d).\n", firstPage, lastPage);
delete doc;
delete globalParams;
return 2;
}
// Prepare for rendering.
splashOut = new SplashOutputDev(splashModeRGB8, 4, gFalse, paperColor, gTrue, splashThinLineDefault);
splashOut->setVectorAntialias(gTrue);
splashOut->startDoc(doc);
// Create resulting bitmap if needed
if (firstPage != lastPage) {
int box_ynum = (int) ceil((double) (lastPage - firstPage + 1) / box_xnum);
if (debug)
fprintf(stderr, "box_ynum: %d\n", box_ynum);
canvas = new SplashBitmap(box_w * box_xnum, box_h * box_ynum, 4, splashModeRGB8, gFalse);
#ifdef SQUARE_FILL
for (int x = 0; x < box_xnum; x++) {
for (int y = 0; y < box_ynum; y++) {
fillRect(canvas, x * box_w, y * box_h, box_w, box_h, (x + y * box_xnum) * 30);
}
}
#else
fillRect(canvas, 0, 0, canvas->getWidth(), canvas->getHeight(), 255);
#endif
}
if (debug) {
if (firstPage == lastPage) {
fprintf(stderr, "Processing only one page, will render to exact size\n");
} else {
fprintf(stderr, "Will process pages from %d to %d\n", firstPage, lastPage);
}
}
// Process pages.
for (int pg = firstPage; pg <= lastPage; ++pg) {
if (debug)
fprintf(stderr, "Processing page %d\n", pg);
drawPage(doc, splashOut, canvas, pg, box_w, box_h, ((pg - firstPage) % box_xnum), ((pg - firstPage) / box_xnum), gFalse);
}
// Write result.
if (canvas != NULL) {
canvas->writeImgFile(splashFormatJpeg, stdout, 300, 300);
delete canvas;
}
// Cleanup and exit.
delete splashOut;
delete doc;
delete globalParams;
return 0;
}