SmartPDF - lightweight pdf viewer app for rosapps
[reactos.git] / rosapps / smartpdf / poppler / utils / pdftoppm.cc
1 //========================================================================
2 //
3 // pdftoppm.cc
4 //
5 // Copyright 2003 Glyph & Cog, LLC
6 //
7 //========================================================================
8
9 #include "config.h"
10 #include <poppler-config.h>
11 #include <stdio.h>
12 #include "parseargs.h"
13 #include "goo/gmem.h"
14 #include "goo/GooString.h"
15 #include "GlobalParams.h"
16 #include "Object.h"
17 #include "PDFDoc.h"
18 #include "splash/SplashBitmap.h"
19 #include "splash/Splash.h"
20 #include "SplashOutputDev.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 GooString *fileName;
82 char *ppmRoot;
83 char ppmFile[512];
84 GooString *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 GooString(argv[1]);
107 ppmRoot = argv[2];
108
109 // read config file
110 globalParams = new GlobalParams(cfgFileName);
111 if (enableT1libStr[0]) {
112 if (!globalParams->setEnableT1lib(enableT1libStr)) {
113 fprintf(stderr, "Bad '-t1lib' value on command line\n");
114 }
115 }
116 if (enableFreeTypeStr[0]) {
117 if (!globalParams->setEnableFreeType(enableFreeTypeStr)) {
118 fprintf(stderr, "Bad '-freetype' value on command line\n");
119 }
120 }
121 if (antialiasStr[0]) {
122 if (!globalParams->setAntialias(antialiasStr)) {
123 fprintf(stderr, "Bad '-aa' value on command line\n");
124 }
125 }
126 if (quiet) {
127 globalParams->setErrQuiet(quiet);
128 }
129
130 // open PDF file
131 if (ownerPassword[0]) {
132 ownerPW = new GooString(ownerPassword);
133 } else {
134 ownerPW = NULL;
135 }
136 if (userPassword[0]) {
137 userPW = new GooString(userPassword);
138 } else {
139 userPW = NULL;
140 }
141 doc = new PDFDoc(fileName, ownerPW, userPW);
142 if (userPW) {
143 delete userPW;
144 }
145 if (ownerPW) {
146 delete ownerPW;
147 }
148 if (!doc->isOk()) {
149 exitCode = 1;
150 goto err1;
151 }
152
153 // get page range
154 if (firstPage < 1)
155 firstPage = 1;
156 if (lastPage < 1 || lastPage > doc->getNumPages())
157 lastPage = doc->getNumPages();
158
159 // write PPM files
160 paperColor[0] = 255;
161 paperColor[1] = 255;
162 paperColor[2] = 255;
163 splashOut = new SplashOutputDev(mono ? splashModeMono1 :
164 gray ? splashModeMono8 :
165 splashModeRGB8, 4,
166 gFalse, paperColor);
167 splashOut->startDoc(doc->getXRef());
168 for (pg = firstPage; pg <= lastPage; ++pg) {
169 doc->displayPage(splashOut, pg, resolution, resolution, 0, gTrue, gFalse, gFalse);
170 sprintf(ppmFile, "%.*s-%06d.%s",
171 (int)sizeof(ppmFile) - 32, ppmRoot, pg,
172 mono ? "pbm" : gray ? "pgm" : "ppm");
173 splashOut->getBitmap()->writePNMFile(ppmFile);
174 }
175 delete splashOut;
176
177 exitCode = 0;
178
179 // clean up
180 err1:
181 delete doc;
182 delete globalParams;
183 err0:
184
185 // check for memory leaks
186 Object::memCheck(stderr);
187 gMemReport(stderr);
188
189 return exitCode;
190 }