From 2d7b4f3843e803a04b19bd0b84ac0bb87a17f695 Mon Sep 17 00:00:00 2001 From: Jakob Voss Date: Wed, 10 Nov 2010 22:53:42 +0100 Subject: [PATCH] added command line parameter -p to pdfimages --- utils/ImageOutputDev.cc | 26 +++++++++++++++++++------- utils/ImageOutputDev.h | 19 +++++++++++++++---- utils/pdfimages.1 | 3 +++ utils/pdfimages.cc | 5 ++++- 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/utils/ImageOutputDev.cc b/utils/ImageOutputDev.cc index 7bf843f..2723155 100644 --- a/utils/ImageOutputDev.cc +++ b/utils/ImageOutputDev.cc @@ -19,6 +19,7 @@ // Copyright (C) 2008 Vasile Gaburici // Copyright (C) 2009 Carlos Garcia Campos // Copyright (C) 2009 William Bader +// Copyright (C) 2010 Jakob Voss // // To see a description of the changes please see the Changelog file that // came with your tarball or type make ChangeLog if you are building from git @@ -46,11 +47,14 @@ #endif #include "ImageOutputDev.h" -ImageOutputDev::ImageOutputDev(char *fileRootA, GBool dumpJPEGA) { +ImageOutputDev::ImageOutputDev(char *fileRootA, + GBool pageNamesA, GBool dumpJPEGA) { fileRoot = copyString(fileRootA); - fileName = (char *)gmalloc(strlen(fileRoot) + 20); + fileName = (char *)gmalloc(strlen(fileRoot) + 45); dumpJPEG = dumpJPEGA; + pageNames = pageNamesA; imgNum = 0; + pageNum = 0; ok = gTrue; } @@ -59,6 +63,14 @@ ImageOutputDev::~ImageOutputDev() { gfree(fileRoot); } +void ImageOutputDev::setFilename(char *fileExt) { + if (pageNames) { + sprintf(fileName, "%s-%03d-%03d.%s", fileRoot, pageNum, imgNum, fileExt); + } else { + sprintf(fileName, "%s-%03d.%s", fileRoot, imgNum, fileExt); + } +} + void ImageOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str, int width, int height, GBool invert, GBool interpolate, GBool inlineImg) { @@ -70,7 +82,7 @@ void ImageOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str, if (dumpJPEG && str->getKind() == strDCT && !inlineImg) { // open the image file - sprintf(fileName, "%s-%03d.jpg", fileRoot, imgNum); + setFilename("jpg"); ++imgNum; if (!(f = fopen(fileName, "wb"))) { error(-1, "Couldn't open image file '%s'", fileName); @@ -92,7 +104,7 @@ void ImageOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str, } else { // open the image file and write the PBM header - sprintf(fileName, "%s-%03d.pbm", fileRoot, imgNum); + setFilename("pbm"); ++imgNum; if (!(f = fopen(fileName, "wb"))) { error(-1, "Couldn't open image file '%s'", fileName); @@ -137,7 +149,7 @@ void ImageOutputDev::drawImage(GfxState *state, Object *ref, Stream *str, !inlineImg) { // open the image file - sprintf(fileName, "%s-%03d.jpg", fileRoot, imgNum); + setFilename("jpg"); ++imgNum; if (!(f = fopen(fileName, "wb"))) { error(-1, "Couldn't open image file '%s'", fileName); @@ -160,7 +172,7 @@ void ImageOutputDev::drawImage(GfxState *state, Object *ref, Stream *str, colorMap->getBits() == 1) { // open the image file and write the PBM header - sprintf(fileName, "%s-%03d.pbm", fileRoot, imgNum); + setFilename("pbm"); ++imgNum; if (!(f = fopen(fileName, "wb"))) { error(-1, "Couldn't open image file '%s'", fileName); @@ -191,7 +203,7 @@ void ImageOutputDev::drawImage(GfxState *state, Object *ref, Stream *str, } else { // open the image file and write the PPM header - sprintf(fileName, "%s-%03d.ppm", fileRoot, imgNum); + setFilename("ppm"); ++imgNum; if (!(f = fopen(fileName, "wb"))) { error(-1, "Couldn't open image file '%s'", fileName); diff --git a/utils/ImageOutputDev.h b/utils/ImageOutputDev.h index f2beab2..dc89920 100644 --- a/utils/ImageOutputDev.h +++ b/utils/ImageOutputDev.h @@ -16,6 +16,7 @@ // Copyright (C) 2006 Rainer Keller // Copyright (C) 2008 Timothy Lee // Copyright (C) 2009 Carlos Garcia Campos +// Copyright (C) 2010 Jakob Voss // // To see a description of the changes please see the Changelog file that // came with your tarball or type make ChangeLog if you are building from git @@ -45,10 +46,11 @@ class ImageOutputDev: public OutputDev { public: // Create an OutputDev which will write images to files named - // -NNN.. Normally, all images are written as PBM - // (.pbm) or PPM (.ppm) files. If is set, JPEG images are - // written as JPEG (.jpg) files. - ImageOutputDev(char *fileRootA, GBool dumpJPEGA); + // -NNN. or -PPP-NNN., if + // is set. Normally, all images are written as PBM + // (.pbm) or PPM (.ppm) files. If is set, JPEG images + // are written as JPEG (.jpg) files. + ImageOutputDev(char *fileRootA, GBool pageNamesA, GBool dumpJPEGA); // Destructor. virtual ~ImageOutputDev(); @@ -63,6 +65,13 @@ public: // Does this device need non-text content? virtual GBool needNonText() { return gTrue; } + // Start a page + virtual void startPage(int pageNum, GfxState *state) + { this->pageNum = pageNum; } + + // Sets the output filename with a given file extension + virtual void setFilename(char *fileExt); + //---- get info about output device // Does this device use upside-down coordinates? @@ -99,6 +108,8 @@ private: char *fileRoot; // root of output file names char *fileName; // buffer for output file names GBool dumpJPEG; // set to dump native JPEG files + GBool pageNames; // set to include page number in file names + int pageNum; // current page number int imgNum; // current image number GBool ok; // set up ok? }; diff --git a/utils/pdfimages.1 b/utils/pdfimages.1 index ff31d85..986a296 100644 --- a/utils/pdfimages.1 +++ b/utils/pdfimages.1 @@ -42,6 +42,9 @@ bypass all security restrictions. .BI \-upw " password" Specify the user password for the PDF file. .TP +.B \-p +Include page numbers in output file names. +.TP .B \-q Don't print any messages or errors. .TP diff --git a/utils/pdfimages.cc b/utils/pdfimages.cc index ffa7991..a68aa81 100644 --- a/utils/pdfimages.cc +++ b/utils/pdfimages.cc @@ -48,6 +48,7 @@ static int firstPage = 1; static int lastPage = 0; static GBool dumpJPEG = gFalse; +static GBool pageNames = gFalse; static char ownerPassword[33] = "\001"; static char userPassword[33] = "\001"; static GBool quiet = gFalse; @@ -65,6 +66,8 @@ static const ArgDesc argDesc[] = { "owner password (for encrypted files)"}, {"-upw", argString, userPassword, sizeof(userPassword), "user password (for encrypted files)"}, + {"-p", argFlag, &pageNames, 0, + "include page numbers in output file names"}, {"-q", argFlag, &quiet, 0, "don't print any messages or errors"}, {"-v", argFlag, &printVersion, 0, @@ -157,7 +160,7 @@ int main(int argc, char *argv[]) { lastPage = doc->getNumPages(); // write image files - imgOut = new ImageOutputDev(imgRoot, dumpJPEG); + imgOut = new ImageOutputDev(imgRoot, pageNames, dumpJPEG); if (imgOut->isOk()) { doc->displayPages(imgOut, firstPage, lastPage, 72, 72, 0, gTrue, gFalse, gFalse); -- 1.7.0.4