f354726346e7a4eb4a667f8a0c66906f90a71cf6
[swftools.git] / lib / pdf / pdf2jpeg.c
1 /* pdf2jpeg.c
2    Converts a pdf page to a jpeg.
3
4    Copyright (c) 2010 Matthias Kramm
5    Copyright (c) 1998-2009 Derek Noonburg
6
7    Swftools is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    Swftools is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with swftools; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
20
21 #include <aconf.h>
22 #include <stdio.h>
23 #include "parseargs.h"
24 #include "gmem.h"
25 #include "GString.h"
26 #include "GlobalParams.h"
27 #include "Object.h"
28 #include "PDFDoc.h"
29 #include "SplashBitmap.h"
30 #include "Splash.h"
31 #include "SplashOutputDev.h"
32 #include "config.h"
33 #include "../jpeg.h"
34
35 static int page = 1;
36 static int width = 0;
37 static int resolution = 150;
38 static int quality = 95;
39 static char ownerPassword[33] = "";
40 static char userPassword[33] = "";
41 static char cfgFileName[256] = "";
42 static GBool printVersion = gFalse;
43 static GBool printHelp = gFalse;
44 static char output[256] = "output.jpg";
45
46 static ArgDesc argDesc[] = {
47   {"-p",      argInt,      &page,     0,
48    "first page to print"},
49   {"-r",      argInt,      &resolution,    0,
50    "resolution, in DPI (default is 150)"},
51   {"-q",      argInt,      &quality,    0,
52    "jpeg conversion quality"},
53   {"-w",      argInt,      &width,    0,
54    "zoom to this width"},
55   {"-o",      argString,   &output,    sizeof(output),
56    "resolution, in DPI (default is 150)"},
57   {"-opw",    argString,   ownerPassword,  sizeof(ownerPassword),
58    "owner password (for encrypted files)"},
59   {"-upw",    argString,   userPassword,   sizeof(userPassword),
60    "user password (for encrypted files)"},
61   {"-cfg",        argString,      cfgFileName,    sizeof(cfgFileName),
62    "configuration file to use in place of .xpdfrc"},
63   {"-v",      argFlag,     &printVersion,  0,
64    "print copyright and version info"},
65   {"-h",      argFlag,     &printHelp,     0,
66    "print usage information"},
67   {"-help",   argFlag,     &printHelp,     0,
68    "print usage information"},
69   {"--help",  argFlag,     &printHelp,     0,
70    "print usage information"},
71   {"-?",      argFlag,     &printHelp,     0,
72    "print usage information"},
73   {NULL}
74 };
75
76 int main(int argc, char *argv[]) {
77   PDFDoc *doc;
78   GString *fileName;
79   GString *ownerPW, *userPW;
80   SplashColor paperColor;
81   SplashOutputDev *splashOut;
82   SplashBitmap*bitmap = 0;
83   GBool ok;
84   int exitCode;
85   int pg;
86   double r;
87
88   exitCode = 99;
89
90   // parse args
91   ok = parseArgs(argDesc, &argc, argv);
92   
93   if (!ok || argc != 2 || printVersion || printHelp) {
94     fprintf(stderr, "pdf2jpeg version %s\n", xpdfVersion);
95     fprintf(stderr, "%s\n", xpdfCopyright);
96     if (!printVersion) {
97       printUsage("pdf2jpeg", "<PDF-file> -o <jpegfile>", argDesc);
98     }
99     goto err0;
100   }
101   fileName = new GString(argv[1]);
102
103   // read config file
104   globalParams = new GlobalParams(cfgFileName);
105   globalParams->setupBaseFonts(NULL);
106   
107   // open PDF file
108   if (ownerPassword[0]) {
109     ownerPW = new GString(ownerPassword);
110   } else {
111     ownerPW = NULL;
112   }
113   if (userPassword[0]) {
114     userPW = new GString(userPassword);
115   } else {
116     userPW = NULL;
117   }
118   doc = new PDFDoc(fileName, ownerPW, userPW);
119   if (userPW) {
120     delete userPW;
121   }
122   if (ownerPW) {
123     delete ownerPW;
124   }
125   if (!doc->isOk()) {
126     exitCode = 1;
127     goto err1;
128   }
129
130   paperColor[0] = paperColor[1] = paperColor[2] = 0xff;
131   splashOut = new SplashOutputDev(splashModeRGB8, 1, gFalse, paperColor);
132   
133   splashOut->startDoc(doc->getXRef());
134
135   r = resolution;
136   if(width) {
137     int old_width = doc->getPageCropWidth(page);
138     r = 72.0*width/old_width;
139   }
140
141   doc->displayPage(splashOut, page, r, r, 0, gFalse, gTrue, gFalse);
142   bitmap = splashOut->getBitmap();
143   if(bitmap) {
144     Guchar*rgb = bitmap->getDataPtr();
145     int width = bitmap->getWidth();
146     int height = bitmap->getHeight();
147     jpeg_save(rgb, width, height, quality, output);
148   }
149   delete splashOut;
150  
151
152   exitCode = 0;
153
154   // clean up
155  err1:
156   delete doc;
157   delete globalParams;
158  err0:
159
160   // check for memory leaks
161   Object::memCheck(stderr);
162   gMemReport(stderr);
163
164   return exitCode;
165 }