~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/poppler/utils/pdftoppm.cc

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//========================================================================
 
2
//
 
3
// pdftoppm.cc
 
4
//
 
5
// Copyright 2003 Glyph & Cog, LLC
 
6
//
 
7
//========================================================================
 
8
 
 
9
//========================================================================
 
10
//
 
11
// Modified under the Poppler project - http://poppler.freedesktop.org
 
12
//
 
13
// All changes made under the Poppler project to this file are licensed
 
14
// under GPL version 2 or later
 
15
//
 
16
// Copyright (C) 2007 Ilmari Heikkinen <ilmari.heikkinen@gmail.com>
 
17
// Copyright (C) 2008 Richard Airlie <richard.airlie@maglabs.net>
 
18
// Copyright (C) 2009 Michael K. Johnson <a1237@danlj.org>
 
19
// Copyright (C) 2009 Shen Liang <shenzhuxi@gmail.com>
 
20
// Copyright (C) 2009 Stefan Thomas <thomas@eload24.com>
 
21
// Copyright (C) 2009, 2010 Albert Astals Cid <aacid@kde.org>
 
22
// Copyright (C) 2010 Adrian Johnson <ajohnson@redneon.com>
 
23
// Copyright (C) 2010 Hib Eris <hib@hiberis.nl>
 
24
// Copyright (C) 2010 Jonathan Liu <net147@gmail.com>
 
25
//
 
26
// To see a description of the changes please see the Changelog file that
 
27
// came with your tarball or type make ChangeLog if you are building from git
 
28
//
 
29
//========================================================================
 
30
 
 
31
#include "config.h"
 
32
#include <poppler-config.h>
 
33
#ifdef _WIN32
 
34
#include <fcntl.h> // for O_BINARY
 
35
#include <io.h>    // for setmode
 
36
#endif
 
37
#include <stdio.h>
 
38
#include <math.h>
 
39
#include "parseargs.h"
 
40
#include "goo/gmem.h"
 
41
#include "goo/GooString.h"
 
42
#include "GlobalParams.h"
 
43
#include "Object.h"
 
44
#include "PDFDoc.h"
 
45
#include "PDFDocFactory.h"
 
46
#include "splash/SplashBitmap.h"
 
47
#include "splash/Splash.h"
 
48
#include "SplashOutputDev.h"
 
49
 
 
50
#define PPM_FILE_SZ 512
 
51
 
 
52
static int firstPage = 1;
 
53
static int lastPage = 0;
 
54
static GBool printOnlyOdd = gFalse;
 
55
static GBool printOnlyEven = gFalse;
 
56
static double resolution = 0.0;
 
57
static double x_resolution = 150.0;
 
58
static double y_resolution = 150.0;
 
59
static int scaleTo = 0;
 
60
static int x_scaleTo = 0;
 
61
static int y_scaleTo = 0;
 
62
static int x = 0;
 
63
static int y = 0;
 
64
static int w = 0;
 
65
static int h = 0;
 
66
static int sz = 0;
 
67
static GBool useCropBox = gFalse;
 
68
static GBool mono = gFalse;
 
69
static GBool gray = gFalse;
 
70
static GBool png = gFalse;
 
71
static GBool jpeg = gFalse;
 
72
static char enableFreeTypeStr[16] = "";
 
73
static char antialiasStr[16] = "";
 
74
static char vectorAntialiasStr[16] = "";
 
75
static char ownerPassword[33] = "";
 
76
static char userPassword[33] = "";
 
77
static GBool quiet = gFalse;
 
78
static GBool printVersion = gFalse;
 
79
static GBool printHelp = gFalse;
 
80
 
 
81
static const ArgDesc argDesc[] = {
 
82
  {"-f",      argInt,      &firstPage,     0,
 
83
   "first page to print"},
 
84
  {"-l",      argInt,      &lastPage,      0,
 
85
   "last page to print"},
 
86
  {"-o",      argFlag,      &printOnlyOdd, 0,
 
87
   "print only odd pages"},
 
88
  {"-e",      argFlag,      &printOnlyEven, 0,
 
89
   "print only even pages"},
 
90
 
 
91
  {"-r",      argFP,       &resolution,    0,
 
92
   "resolution, in DPI (default is 150)"},
 
93
  {"-rx",      argFP,       &x_resolution,    0,
 
94
   "X resolution, in DPI (default is 150)"},
 
95
  {"-ry",      argFP,       &y_resolution,    0,
 
96
   "Y resolution, in DPI (default is 150)"},
 
97
  {"-scale-to",      argInt,       &scaleTo,    0,
 
98
   "scales each page to fit within scale-to*scale-to pixel box"},
 
99
  {"-scale-to-x",      argInt,       &x_scaleTo,    0,
 
100
   "scales each page horizontally to fit in scale-to-x pixels"},
 
101
  {"-scale-to-y",      argInt,       &y_scaleTo,    0,
 
102
   "scales each page vertically to fit in scale-to-y pixels"},
 
103
 
 
104
  {"-x",      argInt,      &x,             0,
 
105
   "x-coordinate of the crop area top left corner"},
 
106
  {"-y",      argInt,      &y,             0,
 
107
   "y-coordinate of the crop area top left corner"},
 
108
  {"-W",      argInt,      &w,             0,
 
109
   "width of crop area in pixels (default is 0)"},
 
110
  {"-H",      argInt,      &h,             0,
 
111
   "height of crop area in pixels (default is 0)"},
 
112
  {"-sz",     argInt,      &sz,            0,
 
113
   "size of crop square in pixels (sets W and H)"},
 
114
  {"-cropbox",argFlag,     &useCropBox,    0,
 
115
   "use the crop box rather than media box"},
 
116
 
 
117
  {"-mono",   argFlag,     &mono,          0,
 
118
   "generate a monochrome PBM file"},
 
119
  {"-gray",   argFlag,     &gray,          0,
 
120
   "generate a grayscale PGM file"},
 
121
#if ENABLE_LIBPNG
 
122
  {"-png",    argFlag,     &png,           0,
 
123
   "generate a PNG file"},
 
124
#endif
 
125
#if ENABLE_LIBJPEG
 
126
  {"-jpeg",    argFlag,     &jpeg,           0,
 
127
   "generate a JPEG file"},
 
128
#endif
 
129
#if HAVE_FREETYPE_FREETYPE_H | HAVE_FREETYPE_H
 
130
  {"-freetype",   argString,      enableFreeTypeStr, sizeof(enableFreeTypeStr),
 
131
   "enable FreeType font rasterizer: yes, no"},
 
132
#endif
 
133
  
 
134
  {"-aa",         argString,      antialiasStr,   sizeof(antialiasStr),
 
135
   "enable font anti-aliasing: yes, no"},
 
136
  {"-aaVector",   argString,      vectorAntialiasStr, sizeof(vectorAntialiasStr),
 
137
   "enable vector anti-aliasing: yes, no"},
 
138
  
 
139
  {"-opw",    argString,   ownerPassword,  sizeof(ownerPassword),
 
140
   "owner password (for encrypted files)"},
 
141
  {"-upw",    argString,   userPassword,   sizeof(userPassword),
 
142
   "user password (for encrypted files)"},
 
143
  
 
144
  {"-q",      argFlag,     &quiet,         0,
 
145
   "don't print any messages or errors"},
 
146
  {"-v",      argFlag,     &printVersion,  0,
 
147
   "print copyright and version info"},
 
148
  {"-h",      argFlag,     &printHelp,     0,
 
149
   "print usage information"},
 
150
  {"-help",   argFlag,     &printHelp,     0,
 
151
   "print usage information"},
 
152
  {"--help",  argFlag,     &printHelp,     0,
 
153
   "print usage information"},
 
154
  {"-?",      argFlag,     &printHelp,     0,
 
155
   "print usage information"},
 
156
  {NULL}
 
157
};
 
158
 
 
159
static void savePageSlice(PDFDoc *doc,
 
160
                   SplashOutputDev *splashOut, 
 
161
                   int pg, int x, int y, int w, int h, 
 
162
                   double pg_w, double pg_h, 
 
163
                   char *ppmFile) {
 
164
  if (w == 0) w = (int)ceil(pg_w);
 
165
  if (h == 0) h = (int)ceil(pg_h);
 
166
  w = (x+w > pg_w ? (int)ceil(pg_w-x) : w);
 
167
  h = (y+h > pg_h ? (int)ceil(pg_h-y) : h);
 
168
  doc->displayPageSlice(splashOut, 
 
169
    pg, x_resolution, y_resolution, 
 
170
    0,
 
171
    !useCropBox, gFalse, gFalse,
 
172
    x, y, w, h
 
173
  );
 
174
 
 
175
  SplashBitmap *bitmap = splashOut->getBitmap();
 
176
  
 
177
  if (ppmFile != NULL) {
 
178
    if (png) {
 
179
      bitmap->writeImgFile(splashFormatPng, ppmFile, x_resolution, y_resolution);
 
180
    } else if (jpeg) {
 
181
      bitmap->writeImgFile(splashFormatJpeg, ppmFile, x_resolution, y_resolution);
 
182
    } else {
 
183
      bitmap->writePNMFile(ppmFile);
 
184
    }
 
185
  } else {
 
186
#ifdef _WIN32
 
187
    setmode(fileno(stdout), O_BINARY);
 
188
#endif
 
189
 
 
190
    if (png) {
 
191
      bitmap->writeImgFile(splashFormatPng, stdout, x_resolution, y_resolution);
 
192
    } else if (jpeg) {
 
193
      bitmap->writeImgFile(splashFormatJpeg, stdout, x_resolution, y_resolution);
 
194
    } else {
 
195
      bitmap->writePNMFile(stdout);
 
196
    }
 
197
  }
 
198
}
 
199
 
 
200
static int numberOfCharacters(unsigned int n)
 
201
{
 
202
  int charNum = 0;
 
203
  while (n >= 10)
 
204
  {
 
205
    n = n / 10;
 
206
    charNum++;
 
207
  }
 
208
  charNum++;
 
209
  return charNum;
 
210
}
 
211
 
 
212
int main(int argc, char *argv[]) {
 
213
  PDFDoc *doc;
 
214
  GooString *fileName = NULL;
 
215
  char *ppmRoot = NULL;
 
216
  char ppmFile[PPM_FILE_SZ];
 
217
  GooString *ownerPW, *userPW;
 
218
  SplashColor paperColor;
 
219
  SplashOutputDev *splashOut;
 
220
  GBool ok;
 
221
  int exitCode;
 
222
  int pg, pg_num_len;
 
223
  double pg_w, pg_h, tmp;
 
224
 
 
225
  exitCode = 99;
 
226
 
 
227
  // parse args
 
228
  ok = parseArgs(argDesc, &argc, argv);
 
229
  if (mono && gray) {
 
230
    ok = gFalse;
 
231
  }
 
232
  if ( resolution != 0.0 &&
 
233
       (x_resolution == 150.0 ||
 
234
        y_resolution == 150.0)) {
 
235
    x_resolution = resolution;
 
236
    y_resolution = resolution;
 
237
  }
 
238
  if (!ok || argc > 3 || printVersion || printHelp) {
 
239
    fprintf(stderr, "pdftoppm version %s\n", PACKAGE_VERSION);
 
240
    fprintf(stderr, "%s\n", popplerCopyright);
 
241
    fprintf(stderr, "%s\n", xpdfCopyright);
 
242
    if (!printVersion) {
 
243
      printUsage("pdftoppm", "[PDF-file [PPM-file-prefix]]", argDesc);
 
244
    }
 
245
    if (printVersion || printHelp)
 
246
      exitCode = 0;
 
247
    goto err0;
 
248
  }
 
249
  if (argc > 1) fileName = new GooString(argv[1]);
 
250
  if (argc == 3) ppmRoot = argv[2];
 
251
 
 
252
  // read config file
 
253
  globalParams = new GlobalParams();
 
254
  if (enableFreeTypeStr[0]) {
 
255
    if (!globalParams->setEnableFreeType(enableFreeTypeStr)) {
 
256
      fprintf(stderr, "Bad '-freetype' value on command line\n");
 
257
    }
 
258
  }
 
259
  if (antialiasStr[0]) {
 
260
    if (!globalParams->setAntialias(antialiasStr)) {
 
261
      fprintf(stderr, "Bad '-aa' value on command line\n");
 
262
    }
 
263
  }
 
264
  if (vectorAntialiasStr[0]) {
 
265
    if (!globalParams->setVectorAntialias(vectorAntialiasStr)) {
 
266
      fprintf(stderr, "Bad '-aaVector' value on command line\n");
 
267
    }
 
268
  }
 
269
  if (quiet) {
 
270
    globalParams->setErrQuiet(quiet);
 
271
  }
 
272
 
 
273
  // open PDF file
 
274
  if (ownerPassword[0]) {
 
275
    ownerPW = new GooString(ownerPassword);
 
276
  } else {
 
277
    ownerPW = NULL;
 
278
  }
 
279
  if (userPassword[0]) {
 
280
    userPW = new GooString(userPassword);
 
281
  } else {
 
282
    userPW = NULL;
 
283
  }
 
284
 
 
285
  if (fileName == NULL) {
 
286
    fileName = new GooString("fd://0");
 
287
  }
 
288
  if (fileName->cmp("-") == 0) {
 
289
    delete fileName;
 
290
    fileName = new GooString("fd://0");
 
291
  }
 
292
  doc = PDFDocFactory().createPDFDoc(*fileName, ownerPW, userPW);
 
293
  delete fileName;
 
294
 
 
295
  if (userPW) {
 
296
    delete userPW;
 
297
  }
 
298
  if (ownerPW) {
 
299
    delete ownerPW;
 
300
  }
 
301
  if (!doc->isOk()) {
 
302
    exitCode = 1;
 
303
    goto err1;
 
304
  }
 
305
 
 
306
  // get page range
 
307
  if (firstPage < 1)
 
308
    firstPage = 1;
 
309
  if (lastPage < 1 || lastPage > doc->getNumPages())
 
310
    lastPage = doc->getNumPages();
 
311
 
 
312
  // write PPM files
 
313
  paperColor[0] = 255;
 
314
  paperColor[1] = 255;
 
315
  paperColor[2] = 255;
 
316
  splashOut = new SplashOutputDev(mono ? splashModeMono1 :
 
317
                                    gray ? splashModeMono8 :
 
318
                                             splashModeRGB8, 4,
 
319
                                  gFalse, paperColor);
 
320
  splashOut->startDoc(doc->getXRef());
 
321
  if (sz != 0) w = h = sz;
 
322
  pg_num_len = numberOfCharacters(doc->getNumPages());
 
323
  for (pg = firstPage; pg <= lastPage; ++pg) {
 
324
    if (printOnlyEven && pg % 2 == 0) continue;
 
325
    if (printOnlyOdd && pg % 2 == 1) continue;
 
326
    if (useCropBox) {
 
327
      pg_w = doc->getPageCropWidth(pg);
 
328
      pg_h = doc->getPageCropHeight(pg);
 
329
    } else {
 
330
      pg_w = doc->getPageMediaWidth(pg);
 
331
      pg_h = doc->getPageMediaHeight(pg);
 
332
    }
 
333
 
 
334
    if (scaleTo != 0) {
 
335
      resolution = (72.0 * scaleTo) / (pg_w > pg_h ? pg_w : pg_h);
 
336
      x_resolution = y_resolution = resolution;
 
337
    } else {
 
338
      if (x_scaleTo != 0) {
 
339
        x_resolution = (72.0 * x_scaleTo) / pg_w;
 
340
      }
 
341
      if (y_scaleTo != 0) {
 
342
        y_resolution = (72.0 * y_scaleTo) / pg_h;
 
343
      }
 
344
    }
 
345
    pg_w = pg_w * (x_resolution / 72.0);
 
346
    pg_h = pg_h * (y_resolution / 72.0);
 
347
    if ((doc->getPageRotate(pg) == 90) || (doc->getPageRotate(pg) == 270)) {
 
348
      tmp = pg_w;
 
349
      pg_w = pg_h;
 
350
      pg_h = tmp;
 
351
    }
 
352
    if (ppmRoot != NULL) {
 
353
      snprintf(ppmFile, PPM_FILE_SZ, "%.*s-%0*d.%s",
 
354
              PPM_FILE_SZ - 32, ppmRoot, pg_num_len, pg,
 
355
              png ? "png" : jpeg ? "jpg" : mono ? "pbm" : gray ? "pgm" : "ppm");
 
356
      savePageSlice(doc, splashOut, pg, x, y, w, h, pg_w, pg_h, ppmFile);
 
357
    } else {
 
358
      savePageSlice(doc, splashOut, pg, x, y, w, h, pg_w, pg_h, NULL);
 
359
    }
 
360
  }
 
361
  delete splashOut;
 
362
 
 
363
  exitCode = 0;
 
364
 
 
365
  // clean up
 
366
 err1:
 
367
  delete doc;
 
368
  delete globalParams;
 
369
 err0:
 
370
 
 
371
  // check for memory leaks
 
372
  Object::memCheck(stderr);
 
373
  gMemReport(stderr);
 
374
 
 
375
  return exitCode;
 
376
}