From 421b1bd0f1f397baa682fc9e0e5cde4713595cf1 Mon Sep 17 00:00:00 2001 From: Matthias Kramm Date: Mon, 15 Mar 2010 18:27:46 -0700 Subject: [PATCH] added internal pdf2jpeg utility --- lib/jpeg.h | 4 +- lib/pdf/Makefile.in | 4 +- lib/pdf/pdf2jpeg.c | 165 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 170 insertions(+), 3 deletions(-) create mode 100644 lib/pdf/pdf2jpeg.c diff --git a/lib/jpeg.h b/lib/jpeg.h index 54b67d0..090d147 100644 --- a/lib/jpeg.h +++ b/lib/jpeg.h @@ -3,7 +3,7 @@ #include -#ifdef __CPLUSPLUS__ +#ifdef __cplusplus extern "C" { #endif @@ -14,7 +14,7 @@ int jpeg_save_to_mem(unsigned char*data, int width, int height, int quality, uns int jpeg_load(const char*filename, unsigned char**dest, int*width, int*height); int jpeg_load_from_mem(unsigned char*_data, int size, unsigned char*dest, int width, int height); -#ifdef __CPLUSPLUS__ +#ifdef __cplusplus } #endif diff --git a/lib/pdf/Makefile.in b/lib/pdf/Makefile.in index fd1852b..bebd630 100644 --- a/lib/pdf/Makefile.in +++ b/lib/pdf/Makefile.in @@ -4,7 +4,7 @@ srcdir = @srcdir@ top_srcdir = @top_srcdir@ include ../../Makefile.common -all: ../libgfxpdf$(A) pdf2swf$(E) +all: ../libgfxpdf$(A) pdf2swf$(E) pdf2jpeg$(E) libgfxpdf: ../libgfxpdf$(A) @@ -183,6 +183,8 @@ pdfinfo$(E): $(XPDFOK) xpdf/pdfinfo.$(O) xpdf/parseargs.$(O) $(xpdf_objects) gfx_objects = ../libgfxswf$(A) ../libgfx$(A) ../librfxswf$(A) ../libbase$(A) gfx_objects2 = $(gfx_objects) ../devices/lrf.$(O) ../libocr$(A) +pdf2jpeg$(E): $(XPDFOK) pdf2jpeg.c ../libbase$(A) xpdf/parseargs.$(O) $(xpdf_objects) $(splash_objects) + $(LL) $(CPPFLAGS) -DXPDFEXE -Ixpdf -I. -g pdf2jpeg.c xpdf/parseargs.$(O) ../libbase$(A) $(xpdf_objects) $(splash_objects) -o pdf2jpeg$(E) $(LIBS) pdf2swf$(E): $(XPDFOK) ../../src/pdf2swf.c $(libgfxpdf_objects) $(xpdf_in_source) $(splash_in_source) $(gfx_objects) $(LL) $(CPPFLAGS) -g ../../src/pdf2swf.c $(libgfxpdf_objects) $(xpdf_in_source) $(splash_in_source) $(gfx_objects) -o pdf2swf$(E) $(LIBS) pdf2pdf$(E): $(XPDFOK) ../../src/pdf2pdf.c $(libgfxpdf_objects) $(xpdf_in_source) $(splash_in_source) $(gfx_objects) diff --git a/lib/pdf/pdf2jpeg.c b/lib/pdf/pdf2jpeg.c new file mode 100644 index 0000000..f354726 --- /dev/null +++ b/lib/pdf/pdf2jpeg.c @@ -0,0 +1,165 @@ +/* pdf2jpeg.c + Converts a pdf page to a jpeg. + + Copyright (c) 2010 Matthias Kramm + Copyright (c) 1998-2009 Derek Noonburg + + Swftools is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + Swftools is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with swftools; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +#include +#include +#include "parseargs.h" +#include "gmem.h" +#include "GString.h" +#include "GlobalParams.h" +#include "Object.h" +#include "PDFDoc.h" +#include "SplashBitmap.h" +#include "Splash.h" +#include "SplashOutputDev.h" +#include "config.h" +#include "../jpeg.h" + +static int page = 1; +static int width = 0; +static int resolution = 150; +static int quality = 95; +static char ownerPassword[33] = ""; +static char userPassword[33] = ""; +static char cfgFileName[256] = ""; +static GBool printVersion = gFalse; +static GBool printHelp = gFalse; +static char output[256] = "output.jpg"; + +static ArgDesc argDesc[] = { + {"-p", argInt, &page, 0, + "first page to print"}, + {"-r", argInt, &resolution, 0, + "resolution, in DPI (default is 150)"}, + {"-q", argInt, &quality, 0, + "jpeg conversion quality"}, + {"-w", argInt, &width, 0, + "zoom to this width"}, + {"-o", argString, &output, sizeof(output), + "resolution, in DPI (default is 150)"}, + {"-opw", argString, ownerPassword, sizeof(ownerPassword), + "owner password (for encrypted files)"}, + {"-upw", argString, userPassword, sizeof(userPassword), + "user password (for encrypted files)"}, + {"-cfg", argString, cfgFileName, sizeof(cfgFileName), + "configuration file to use in place of .xpdfrc"}, + {"-v", argFlag, &printVersion, 0, + "print copyright and version info"}, + {"-h", argFlag, &printHelp, 0, + "print usage information"}, + {"-help", argFlag, &printHelp, 0, + "print usage information"}, + {"--help", argFlag, &printHelp, 0, + "print usage information"}, + {"-?", argFlag, &printHelp, 0, + "print usage information"}, + {NULL} +}; + +int main(int argc, char *argv[]) { + PDFDoc *doc; + GString *fileName; + GString *ownerPW, *userPW; + SplashColor paperColor; + SplashOutputDev *splashOut; + SplashBitmap*bitmap = 0; + GBool ok; + int exitCode; + int pg; + double r; + + exitCode = 99; + + // parse args + ok = parseArgs(argDesc, &argc, argv); + + if (!ok || argc != 2 || printVersion || printHelp) { + fprintf(stderr, "pdf2jpeg version %s\n", xpdfVersion); + fprintf(stderr, "%s\n", xpdfCopyright); + if (!printVersion) { + printUsage("pdf2jpeg", " -o ", argDesc); + } + goto err0; + } + fileName = new GString(argv[1]); + + // read config file + globalParams = new GlobalParams(cfgFileName); + globalParams->setupBaseFonts(NULL); + + // open PDF file + if (ownerPassword[0]) { + ownerPW = new GString(ownerPassword); + } else { + ownerPW = NULL; + } + if (userPassword[0]) { + userPW = new GString(userPassword); + } else { + userPW = NULL; + } + doc = new PDFDoc(fileName, ownerPW, userPW); + if (userPW) { + delete userPW; + } + if (ownerPW) { + delete ownerPW; + } + if (!doc->isOk()) { + exitCode = 1; + goto err1; + } + + paperColor[0] = paperColor[1] = paperColor[2] = 0xff; + splashOut = new SplashOutputDev(splashModeRGB8, 1, gFalse, paperColor); + + splashOut->startDoc(doc->getXRef()); + + r = resolution; + if(width) { + int old_width = doc->getPageCropWidth(page); + r = 72.0*width/old_width; + } + + doc->displayPage(splashOut, page, r, r, 0, gFalse, gTrue, gFalse); + bitmap = splashOut->getBitmap(); + if(bitmap) { + Guchar*rgb = bitmap->getDataPtr(); + int width = bitmap->getWidth(); + int height = bitmap->getHeight(); + jpeg_save(rgb, width, height, quality, output); + } + delete splashOut; + + + exitCode = 0; + + // clean up + err1: + delete doc; + delete globalParams; + err0: + + // check for memory leaks + Object::memCheck(stderr); + gMemReport(stderr); + + return exitCode; +} -- 1.7.10.4