fix build with recent changes/gcc 6.3.0
[AROS-Contrib.git] / arospdf / xpdf / pdftoppm.cc
blob091b303ba2b1cfd9b45a5aea11d432b3e5c6429d
1 //========================================================================
2 //
3 // pdftoppm.cc
4 //
5 // Copyright 2003 Glyph & Cog, LLC
6 //
7 //========================================================================
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"
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 vectorAntialiasStr[16] = "";
31 static char ownerPassword[33] = "";
32 static char userPassword[33] = "";
33 static GBool quiet = gFalse;
34 static char cfgFileName[256] = "";
35 static GBool printVersion = gFalse;
36 static GBool printHelp = gFalse;
38 static ArgDesc argDesc[] = {
39 {"-f", argInt, &firstPage, 0,
40 "first page to print"},
41 {"-l", argInt, &lastPage, 0,
42 "last page to print"},
43 {"-r", argInt, &resolution, 0,
44 "resolution, in DPI (default is 150)"},
45 {"-mono", argFlag, &mono, 0,
46 "generate a monochrome PBM file"},
47 {"-gray", argFlag, &gray, 0,
48 "generate a grayscale PGM file"},
49 #if HAVE_T1LIB_H
50 {"-t1lib", argString, enableT1libStr, sizeof(enableT1libStr),
51 "enable t1lib font rasterizer: yes, no"},
52 #endif
53 #if HAVE_FREETYPE_FREETYPE_H | HAVE_FREETYPE_H
54 {"-freetype", argString, enableFreeTypeStr, sizeof(enableFreeTypeStr),
55 "enable FreeType font rasterizer: yes, no"},
56 #endif
57 {"-aa", argString, antialiasStr, sizeof(antialiasStr),
58 "enable font anti-aliasing: yes, no"},
59 {"-aaVector", argString, vectorAntialiasStr, sizeof(vectorAntialiasStr),
60 "enable vector anti-aliasing: yes, no"},
61 {"-opw", argString, ownerPassword, sizeof(ownerPassword),
62 "owner password (for encrypted files)"},
63 {"-upw", argString, userPassword, sizeof(userPassword),
64 "user password (for encrypted files)"},
65 {"-q", argFlag, &quiet, 0,
66 "don't print any messages or errors"},
67 {"-cfg", argString, cfgFileName, sizeof(cfgFileName),
68 "configuration file to use in place of .xpdfrc"},
69 {"-v", argFlag, &printVersion, 0,
70 "print copyright and version info"},
71 {"-h", argFlag, &printHelp, 0,
72 "print usage information"},
73 {"-help", argFlag, &printHelp, 0,
74 "print usage information"},
75 {"--help", argFlag, &printHelp, 0,
76 "print usage information"},
77 {"-?", argFlag, &printHelp, 0,
78 "print usage information"},
79 {NULL}
82 int main(int argc, char *argv[]) {
83 PDFDoc *doc;
84 GString *fileName;
85 char *ppmRoot;
86 char ppmFile[512];
87 GString *ownerPW, *userPW;
88 SplashColor paperColor;
89 SplashOutputDev *splashOut;
90 GBool ok;
91 int exitCode;
92 int pg;
94 exitCode = 99;
96 // parse args
97 ok = parseArgs(argDesc, &argc, argv);
98 if (mono && gray) {
99 ok = gFalse;
101 if (!ok || argc != 3 || printVersion || printHelp) {
102 fprintf(stderr, "pdftoppm version %s\n", xpdfVersion);
103 fprintf(stderr, "%s\n", xpdfCopyright);
104 if (!printVersion) {
105 printUsage("pdftoppm", "<PDF-file> <PPM-root>", argDesc);
107 goto err0;
109 fileName = new GString(argv[1]);
110 ppmRoot = argv[2];
112 // read config file
113 globalParams = new GlobalParams(cfgFileName);
114 globalParams->setupBaseFonts(NULL);
115 if (enableT1libStr[0]) {
116 if (!globalParams->setEnableT1lib(enableT1libStr)) {
117 fprintf(stderr, "Bad '-t1lib' value on command line\n");
120 if (enableFreeTypeStr[0]) {
121 if (!globalParams->setEnableFreeType(enableFreeTypeStr)) {
122 fprintf(stderr, "Bad '-freetype' value on command line\n");
125 if (antialiasStr[0]) {
126 if (!globalParams->setAntialias(antialiasStr)) {
127 fprintf(stderr, "Bad '-aa' value on command line\n");
130 if (vectorAntialiasStr[0]) {
131 if (!globalParams->setVectorAntialias(vectorAntialiasStr)) {
132 fprintf(stderr, "Bad '-aaVector' value on command line\n");
135 if (quiet) {
136 globalParams->setErrQuiet(quiet);
139 // open PDF file
140 if (ownerPassword[0]) {
141 ownerPW = new GString(ownerPassword);
142 } else {
143 ownerPW = NULL;
145 if (userPassword[0]) {
146 userPW = new GString(userPassword);
147 } else {
148 userPW = NULL;
150 doc = new PDFDoc(fileName, ownerPW, userPW);
151 if (userPW) {
152 delete userPW;
154 if (ownerPW) {
155 delete ownerPW;
157 if (!doc->isOk()) {
158 exitCode = 1;
159 goto err1;
162 // get page range
163 if (firstPage < 1)
164 firstPage = 1;
165 if (lastPage < 1 || lastPage > doc->getNumPages())
166 lastPage = doc->getNumPages();
168 // write PPM files
169 if (mono) {
170 paperColor[0] = 0xff;
171 splashOut = new SplashOutputDev(splashModeMono1, 1, gFalse, paperColor);
172 } else if (gray) {
173 paperColor[0] = 0xff;
174 splashOut = new SplashOutputDev(splashModeMono8, 1, gFalse, paperColor);
175 } else {
176 paperColor[0] = paperColor[1] = paperColor[2] = 0xff;
177 splashOut = new SplashOutputDev(splashModeRGB8, 1, gFalse, paperColor);
179 splashOut->startDoc(doc->getXRef());
180 for (pg = firstPage; pg <= lastPage; ++pg) {
181 doc->displayPage(splashOut, pg, resolution, resolution, 0,
182 gFalse, gTrue, gFalse);
183 sprintf(ppmFile, "%.*s-%06d.%s",
184 (int)sizeof(ppmFile) - 32, ppmRoot, pg,
185 mono ? "pbm" : gray ? "pgm" : "ppm");
186 splashOut->getBitmap()->writePNMFile(ppmFile);
188 delete splashOut;
190 exitCode = 0;
192 // clean up
193 err1:
194 delete doc;
195 delete globalParams;
196 err0:
198 // check for memory leaks
199 xObject::memCheck(stderr);
200 gMemReport(stderr);
202 return exitCode;