]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/pdftoppm.cc
9be5c6486c7f0f923875155d3ca7cff1adb2a65a
[evince.git] / pdf / xpdf / pdftoppm.cc
1 //========================================================================
2 //
3 // pdftoppm.cc
4 //
5 // Copyright 2003 Glyph & Cog, LLC
6 //
7 //========================================================================
8
9 #include <aconf.h>
10 #include <stdio.h>
11 #include "parseargs.h"
12 #include "gmem.h"
13 #include "GString.h"
14 #include "GlobalParams.h"
15 #include "Object.h"
16 #include "PDFDoc.h"
17 #include "SplashBitmap.h"
18 #include "Splash.h"
19 #include "SplashOutputDev.h"
20 #include "config.h"
21
22 static int firstPage = 1;
23 static int lastPage = 0;
24 static int resolution = 150;
25 static GBool mono = gFalse;
26 static GBool gray = gFalse;
27 static char enableT1libStr[16] = "";
28 static char enableFreeTypeStr[16] = "";
29 static char antialiasStr[16] = "";
30 static char ownerPassword[33] = "";
31 static char userPassword[33] = "";
32 static GBool quiet = gFalse;
33 static char cfgFileName[256] = "";
34 static GBool printVersion = gFalse;
35 static GBool printHelp = gFalse;
36
37 static ArgDesc argDesc[] = {
38   {"-f",      argInt,      &firstPage,     0,
39    "first page to print"},
40   {"-l",      argInt,      &lastPage,      0,
41    "last page to print"},
42   {"-r",      argInt,      &resolution,    0,
43    "resolution, in DPI (default is 150)"},
44   {"-mono",   argFlag,     &mono,          0,
45    "generate a monochrome PBM file"},
46   {"-gray",   argFlag,     &gray,          0,
47    "generate a grayscale PGM file"},
48 #if HAVE_T1LIB_H
49   {"-t1lib",      argString,      enableT1libStr, sizeof(enableT1libStr),
50    "enable t1lib font rasterizer: yes, no"},
51 #endif
52 #if HAVE_FREETYPE_FREETYPE_H | HAVE_FREETYPE_H
53   {"-freetype",   argString,      enableFreeTypeStr, sizeof(enableFreeTypeStr),
54    "enable FreeType font rasterizer: yes, no"},
55 #endif
56   {"-aa",         argString,      antialiasStr,   sizeof(antialiasStr),
57    "enable font anti-aliasing: yes, no"},
58   {"-opw",    argString,   ownerPassword,  sizeof(ownerPassword),
59    "owner password (for encrypted files)"},
60   {"-upw",    argString,   userPassword,   sizeof(userPassword),
61    "user password (for encrypted files)"},
62   {"-q",      argFlag,     &quiet,         0,
63    "don't print any messages or errors"},
64   {"-cfg",        argString,      cfgFileName,    sizeof(cfgFileName),
65    "configuration file to use in place of .xpdfrc"},
66   {"-v",      argFlag,     &printVersion,  0,
67    "print copyright and version info"},
68   {"-h",      argFlag,     &printHelp,     0,
69    "print usage information"},
70   {"-help",   argFlag,     &printHelp,     0,
71    "print usage information"},
72   {"--help",  argFlag,     &printHelp,     0,
73    "print usage information"},
74   {"-?",      argFlag,     &printHelp,     0,
75    "print usage information"},
76   {NULL}
77 };
78
79 int main(int argc, char *argv[]) {
80   PDFDoc *doc;
81   GString *fileName;
82   char *ppmRoot;
83   char ppmFile[512];
84   GString *ownerPW, *userPW;
85   SplashColor paperColor;
86   SplashOutputDev *splashOut;
87   GBool ok;
88   int exitCode;
89   int pg;
90
91   exitCode = 99;
92
93   // parse args
94   ok = parseArgs(argDesc, &argc, argv);
95   if (mono && gray) {
96     ok = gFalse;
97   }
98   if (!ok || argc != 3 || printVersion || printHelp) {
99     fprintf(stderr, "pdftoppm version %s\n", xpdfVersion);
100     fprintf(stderr, "%s\n", xpdfCopyright);
101     if (!printVersion) {
102       printUsage("pdftoppm", "<PDF-file> <PPM-root>", argDesc);
103     }
104     goto err0;
105   }
106   fileName = new GString(argv[1]);
107   ppmRoot = argv[2];
108
109   // read config file
110   globalParams = new GlobalParams(cfgFileName);
111   globalParams->setupBaseFonts(NULL);
112   if (enableT1libStr[0]) {
113     if (!globalParams->setEnableT1lib(enableT1libStr)) {
114       fprintf(stderr, "Bad '-t1lib' value on command line\n");
115     }
116   }
117   if (enableFreeTypeStr[0]) {
118     if (!globalParams->setEnableFreeType(enableFreeTypeStr)) {
119       fprintf(stderr, "Bad '-freetype' value on command line\n");
120     }
121   }
122   if (antialiasStr[0]) {
123     if (!globalParams->setAntialias(antialiasStr)) {
124       fprintf(stderr, "Bad '-aa' value on command line\n");
125     }
126   }
127   if (quiet) {
128     globalParams->setErrQuiet(quiet);
129   }
130
131   // open PDF file
132   if (ownerPassword[0]) {
133     ownerPW = new GString(ownerPassword);
134   } else {
135     ownerPW = NULL;
136   }
137   if (userPassword[0]) {
138     userPW = new GString(userPassword);
139   } else {
140     userPW = NULL;
141   }
142   doc = new PDFDoc(fileName, ownerPW, userPW);
143   if (userPW) {
144     delete userPW;
145   }
146   if (ownerPW) {
147     delete ownerPW;
148   }
149   if (!doc->isOk()) {
150     exitCode = 1;
151     goto err1;
152   }
153
154   // get page range
155   if (firstPage < 1)
156     firstPage = 1;
157   if (lastPage < 1 || lastPage > doc->getNumPages())
158     lastPage = doc->getNumPages();
159
160   // write PPM files
161   paperColor.rgb8 = splashMakeRGB8(255, 255, 255);
162   splashOut = new SplashOutputDev(mono ? splashModeMono1 :
163                                     gray ? splashModeMono8 :
164                                              splashModeRGB8,
165                                   gFalse, paperColor);
166   splashOut->startDoc(doc->getXRef());
167   for (pg = firstPage; pg <= lastPage; ++pg) {
168     doc->displayPage(splashOut, pg, resolution, resolution, 0, gTrue, gFalse);
169     sprintf(ppmFile, "%.*s-%06d.%s",
170             (int)sizeof(ppmFile) - 32, ppmRoot, pg,
171             mono ? "pbm" : gray ? "pgm" : "ppm");
172     splashOut->getBitmap()->writePNMFile(ppmFile);
173   }
174   delete splashOut;
175
176   exitCode = 0;
177
178   // clean up
179  err1:
180   delete doc;
181   delete globalParams;
182  err0:
183
184   // check for memory leaks
185   Object::memCheck(stderr);
186   gMemReport(stderr);
187
188   return exitCode;
189 }