[poppler] utils/ImageOutputDev.cc utils/ImageOutputDev.h utils/pdfimages.1 utils/pdfimages.cc

Albert Astals Cid aacid at kemper.freedesktop.org
Wed Nov 10 15:43:27 PST 2010


 utils/ImageOutputDev.cc |   25 ++++++++++++++++++-------
 utils/ImageOutputDev.h  |   19 +++++++++++++++----
 utils/pdfimages.1       |    3 +++
 utils/pdfimages.cc      |    6 +++++-
 4 files changed, 41 insertions(+), 12 deletions(-)

New commits:
commit ce929cf33f4b6b6421f9d327b4bb792816d47aac
Author: Jakob Voss <jakob.voss at gbv.de>
Date:   Wed Nov 10 23:41:44 2010 +0000

    Add -p flag to pdfimages
    
    Adds the page the image is in to the image filename

diff --git a/utils/ImageOutputDev.cc b/utils/ImageOutputDev.cc
index 7bf843f..88305a1 100644
--- a/utils/ImageOutputDev.cc
+++ b/utils/ImageOutputDev.cc
@@ -19,6 +19,7 @@
 // Copyright (C) 2008 Vasile Gaburici <gaburici at cs.umd.edu>
 // Copyright (C) 2009 Carlos Garcia Campos <carlosgc at gnome.org>
 // Copyright (C) 2009 William Bader <williambader at hotmail.com>
+// Copyright (C) 2010 Jakob Voss <jakob.voss at gbv.de>
 //
 // 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,13 @@
 #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 +62,14 @@ ImageOutputDev::~ImageOutputDev() {
   gfree(fileRoot);
 }
 
+void ImageOutputDev::setFilename(const 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 +81,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 +103,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 +148,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 +171,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 +202,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..e5dacc9 100644
--- a/utils/ImageOutputDev.h
+++ b/utils/ImageOutputDev.h
@@ -16,6 +16,7 @@
 // Copyright (C) 2006 Rainer Keller <class321 at gmx.de>
 // Copyright (C) 2008 Timothy Lee <timothy.lee at siriushk.com>
 // Copyright (C) 2009 Carlos Garcia Campos <carlosgc at gnome.org>
+// Copyright (C) 2010 Jakob Voss <jakob.voss at gbv.de>
 //
 // 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
-  // <fileRoot>-NNN.<type>.  Normally, all images are written as PBM
-  // (.pbm) or PPM (.ppm) files.  If <dumpJPEG> is set, JPEG images are
-  // written as JPEG (.jpg) files.
-  ImageOutputDev(char *fileRootA, GBool dumpJPEGA);
+  // <fileRoot>-NNN.<type> or <fileRoot>-PPP-NNN.<type>, if 
+  // <pageNames> is set. Normally, all images are written as PBM
+  // (.pbm) or PPM (.ppm) files.  If <dumpJPEG> is set, JPEG images 
+  // are written as JPEG (.jpg) files.
+  ImageOutputDev(char *fileRootA, GBool pageNamesA, GBool dumpJPEGA);
 
   // Destructor.
   virtual ~ImageOutputDev();
@@ -63,6 +65,10 @@ public:
   // Does this device need non-text content?
   virtual GBool needNonText() { return gTrue; }
 
+  // Start a page
+  virtual void startPage(int pageNumA, GfxState *state) 
+			{ pageNum = pageNumA; }
+ 
   //---- get info about output device
 
   // Does this device use upside-down coordinates?
@@ -95,10 +101,15 @@ public:
 				   GBool maskInterpolate);
 
 private:
+  // Sets the output filename with a given file extension
+  void setFilename(const char *fileExt);
+
 
   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..d9d0d55 100644
--- a/utils/pdfimages.cc
+++ b/utils/pdfimages.cc
@@ -17,6 +17,7 @@
 //
 // Copyright (C) 2007-2008, 2010 Albert Astals Cid <aacid at kde.org>
 // Copyright (C) 2010 Hib Eris <hib at hiberis.nl>
+// Copyright (C) 2010 Jakob Voss <jakob.voss at gbv.de>
 //
 // 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
@@ -48,6 +49,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 +67,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 +161,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);


More information about the poppler mailing list