From b3e235a85b364b58b33eeb68a2db73bd7f8d1ae8 Mon Sep 17 00:00:00 2001 From: Igor Slepchin Date: Fri, 3 Feb 2012 17:26:39 -0500 Subject: [PATCH 1/2] Get rid of static data members; merge duplicated jpeg dumping code. HtmlPage::pgNum is never used; imgNum is always equal to imgList->getLength()+1 imgList is now maintained in HtmlPage. --- utils/HtmlOutputDev.cc | 143 ++++++++++++++++++----------------------------- utils/HtmlOutputDev.h | 15 +++-- 2 files changed, 65 insertions(+), 93 deletions(-) diff --git a/utils/HtmlOutputDev.cc b/utils/HtmlOutputDev.cc index 50ceefe..cf6b5a7 100644 --- a/utils/HtmlOutputDev.cc +++ b/utils/HtmlOutputDev.cc @@ -85,10 +85,6 @@ public: // returns true if x is closer to y than x is to z static inline bool IS_CLOSER(float x, float y, float z) { return fabs((x)-(y)) < fabs((x)-(z)); } -int HtmlPage::pgNum=0; -int HtmlOutputDev::imgNum=1; -GooList *HtmlOutputDev::imgList=new GooList(); - extern GBool complexMode; extern GBool singleHtml; extern GBool ignore; @@ -273,6 +269,7 @@ HtmlPage::HtmlPage(GBool rawOrder, char *imgExtVal) { yxCur1 = yxCur2 = NULL; fonts=new HtmlFontAccu(); links=new HtmlLinks(); + imgList=new GooList(); pageWidth=0; pageHeight=0; fontsPageMarker = 0; @@ -283,10 +280,11 @@ HtmlPage::HtmlPage(GBool rawOrder, char *imgExtVal) { HtmlPage::~HtmlPage() { clear(); - if (DocName) delete DocName; - if (fonts) delete fonts; - if (links) delete links; - if (imgExt) delete imgExt; + delete DocName; + delete fonts; + delete links; + delete imgExt; + deleteGooList(imgList, HtmlImage); } void HtmlPage::updateFont(GfxState *state) { @@ -743,9 +741,9 @@ void HtmlPage::dumpAsXML(FILE* f,int page){ delete fontCSStyle; } - int listlen=HtmlOutputDev::imgList->getLength(); + int listlen=imgList->getLength(); for (int i = 0; i < listlen; i++) { - HtmlImage *img = (HtmlImage*)HtmlOutputDev::imgList->del(0); + HtmlImage *img = (HtmlImage*)imgList->del(0); fprintf(f,"yMin),xoutRound(img->xMin)); fprintf(f,"width=\"%d\" height=\"%d\" ",xoutRound(img->xMax-img->xMin),xoutRound(img->yMax-img->yMin)); fprintf(f,"src=\"%s\"/>\n",img->fName->getCString()); @@ -889,13 +887,12 @@ void HtmlPage::dump(FILE *f, int pageNum) { fprintf(f,"",pageNum); // Loop over the list of image names on this page - int listlen=HtmlOutputDev::imgList->getLength(); + int listlen=imgList->getLength(); for (int i = 0; i < listlen; i++) { - HtmlImage *img = (HtmlImage*)HtmlOutputDev::imgList->del(0); + HtmlImage *img = (HtmlImage*)imgList->del(0); fprintf(f,"
\n",img->fName->getCString()); delete img; } - HtmlOutputDev::imgNum=1; GooString* str; for(HtmlString *tmp=yxStrings;tmp;tmp=tmp->yxNext){ @@ -948,6 +945,11 @@ void HtmlPage::setDocName(char *fname){ DocName=new GooString(fname); } +void HtmlPage::addImage(GooString *fname, GfxState *state) { + HtmlImage *img = new HtmlImage(fname, state); + imgList->append(img); +} + //------------------------------------------------------------------------ // HtmlMetaVar //------------------------------------------------------------------------ @@ -1048,7 +1050,6 @@ HtmlOutputDev::HtmlOutputDev(char *fileName, char *title, this->rawOrder = rawOrder; this->doOutline = outline; ok = gFalse; - imgNum=1; //this->firstPage = firstPage; //pageNum=firstPage; // open file @@ -1259,6 +1260,41 @@ void HtmlOutputDev::drawChar(GfxState *state, double x, double y, pages->addChar(state, x, y, dx, dy, originX, originY, u, uLen); } +void HtmlOutputDev::drawJpegImage(GfxState *state, Stream *str) +{ + FILE *f1; + int c; + GooString *fName=new GooString(Docname); + fName->append("-"); + GooString *pgNum= GooString::fromInt(pageNum); + GooString *imgnum= GooString::fromInt(pages->getNumImages()+1); + + // open the image file + fName->append(pgNum)->append("_")->append(imgnum)->append(".jpg"); + delete pgNum; + delete imgnum; + + if (!(f1 = fopen(fName->getCString(), "wb"))) { + error(-1, "Couldn't open image file '%s'", fName->getCString()); + delete fName; + return; + } + + // initialize stream + str = str->getNextStream(); + str->reset(); + + // copy the stream + while ((c = str->getChar()) != EOF) + fputc(c, f1); + + fclose(f1); + + if (fName) { + pages->addImage(fName, state); + } +} + void HtmlOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str, int width, int height, GBool invert, GBool interpolate, GBool inlineImg) { @@ -1268,41 +1304,9 @@ void HtmlOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str, return; } - FILE *f1; - int c; - // dump JPEG file if (dumpJPEG && str->getKind() == strDCT) { - GooString *fName=new GooString(Docname); - fName->append("-"); - GooString *pgNum=GooString::fromInt(pageNum); - GooString *imgnum=GooString::fromInt(imgNum); - // open the image file - fName->append(pgNum)->append("_")->append(imgnum)->append(".jpg"); - delete pgNum; - delete imgnum; - - ++imgNum; - if (!(f1 = fopen(fName->getCString(), "wb"))) { - error(-1, "Couldn't open image file '%s'", fName->getCString()); - delete fName; - return; - } - - // initialize stream - str = str->getNextStream(); - str->reset(); - - // copy the stream - while ((c = str->getChar()) != EOF) - fputc(c, f1); - - fclose(f1); - - if (fName) { - HtmlImage *img = new HtmlImage(fName, state); - imgList->append(img); - } + drawJpegImage(state, str); } else { OutputDev::drawImageMask(state, ref, str, width, height, invert, interpolate, inlineImg); @@ -1318,51 +1322,18 @@ void HtmlOutputDev::drawImage(GfxState *state, Object *ref, Stream *str, maskColors, inlineImg); return; } - - FILE *f1; - int c; /*if( !globalParams->getErrQuiet() ) printf("image stream of kind %d\n", str->getKind());*/ // dump JPEG file if (dumpJPEG && str->getKind() == strDCT) { - GooString *fName=new GooString(Docname); - fName->append("-"); - GooString *pgNum= GooString::fromInt(pageNum); - GooString *imgnum= GooString::fromInt(imgNum); - - // open the image file - fName->append(pgNum)->append("_")->append(imgnum)->append(".jpg"); - delete pgNum; - delete imgnum; - - ++imgNum; - - if (!(f1 = fopen(fName->getCString(), "wb"))) { - error(-1, "Couldn't open image file '%s'", fName->getCString()); - delete fName; - return; - } - - // initialize stream - str = str->getNextStream(); - str->reset(); - - // copy the stream - while ((c = str->getChar()) != EOF) - fputc(c, f1); - - fclose(f1); - - if (fName) { - HtmlImage *img = new HtmlImage(fName, state); - imgList->append(img); - } + drawJpegImage(state, str); } else { #ifdef ENABLE_LIBPNG // Dump the image as a PNG file. Much of the PNG code // comes from an example by Guillaume Cottenceau. + FILE *f1; Guchar *p; GfxRGB rgb; png_byte *row = (png_byte *) malloc(3 * width); // 3 bytes/pixel: RGB @@ -1372,7 +1343,7 @@ void HtmlOutputDev::drawImage(GfxState *state, Object *ref, Stream *str, GooString *fName=new GooString(Docname); fName->append("-"); GooString *pgNum= GooString::fromInt(pageNum); - GooString *imgnum= GooString::fromInt(imgNum); + GooString *imgnum= GooString::fromInt(pages->getNumImages()+1); fName->append(pgNum)->append("_")->append(imgnum)->append(".png"); delete pgNum; delete imgnum; @@ -1423,9 +1394,7 @@ void HtmlOutputDev::drawImage(GfxState *state, Object *ref, Stream *str, fclose(f1); free(row); - HtmlImage *img = new HtmlImage(fName, state); - imgList->append(img); - ++imgNum; + pages->addImage(fName, state); imgStr->close(); delete imgStr; #else @@ -1673,7 +1642,6 @@ GBool HtmlOutputDev::newOutlineLevel(FILE *output, GooList *outlines, Catalog* c GooString *linkName = NULL;; LinkAction *action = item->getAction(); LinkGoTo *link = NULL; - Object dest; if (action && action->getKind() == actionGoTo) link = dynamic_cast(action); if (link && link->isOk()) { @@ -1715,7 +1683,6 @@ GBool HtmlOutputDev::newOutlineLevel(FILE *output, GooList *outlines, Catalog* c delete str; } } - dest.free(); fputs("
  • ",output); if (linkName) diff --git a/utils/HtmlOutputDev.h b/utils/HtmlOutputDev.h index acf3c1e..4943bd9 100644 --- a/utils/HtmlOutputDev.h +++ b/utils/HtmlOutputDev.h @@ -155,7 +155,13 @@ public: links->AddLink(x); } - void dump(FILE *f, int pageNum); + // add an image to the current page + void addImage(GooString *fname, GfxState *state); + + // number of images on the current page + int getNumImages() { return imgList->getLength(); } + + void dump(FILE *f, int pageNum); // Clear the page. void clear(); @@ -182,12 +188,12 @@ private: int fontsPageMarker; HtmlFontAccu *fonts; HtmlLinks *links; + GooList *imgList; GooString *DocName; GooString *imgExt; int pageWidth; int pageHeight; - static int pgNum; int firstPage; // used to begin the numeration of pages friend class HtmlOutputDev; @@ -300,7 +306,7 @@ public: int getPageWidth() { return maxPageWidth; } int getPageHeight() { return maxPageHeight; } - GBool dumpDocOutline(PDFDoc* catalog); + GBool dumpDocOutline(PDFDoc* doc); private: // convert encoding into a HTML standard, or encoding->getCString if not @@ -311,6 +317,7 @@ private: void dumpMetaVars(FILE *); void doFrame(int firstPage); GBool newOutlineLevel(FILE *output, GooList *outlines, Catalog* catalog, int level = 1); + void drawJpegImage(GfxState *state, Stream *str); FILE *fContentsFrame; FILE *page; // html file @@ -325,8 +332,6 @@ private: int pageNum; int maxPageWidth; int maxPageHeight; - static int imgNum; - static GooList *imgList; GooString *Docname; GooString *docTitle; GooList *glMetaVars; -- 1.7.7.6