filters

ImageOutputDev.cc

00001 //========================================================================
00002 //
00003 // ImageOutputDev.cc
00004 //
00005 // Copyright 1998-2002 Glyph & Cog, LLC
00006 //
00007 //========================================================================
00008 
00009 #include <aconf.h>
00010 
00011 #ifdef USE_GCC_PRAGMAS
00012 #pragma implementation
00013 #endif
00014 
00015 #include <stdio.h>
00016 #include <stdlib.h>
00017 #include <stddef.h>
00018 #include <ctype.h>
00019 #include "gmem.h"
00020 #include "config.h"
00021 #include "Error.h"
00022 #include "GfxState.h"
00023 #include "Object.h"
00024 #include "Stream.h"
00025 #include "ImageOutputDev.h"
00026 
00027 ImageOutputDev::ImageOutputDev(char *fileRootA, GBool dumpJPEGA) {
00028   fileRoot = copyString(fileRootA);
00029   fileName = (char *)gmalloc(strlen(fileRoot) + 20);
00030   dumpJPEG = dumpJPEGA;
00031   imgNum = 0;
00032   ok = gTrue;
00033 }
00034 
00035 ImageOutputDev::~ImageOutputDev() {
00036   gfree(fileName);
00037   gfree(fileRoot);
00038 }
00039 
00040 void ImageOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
00041                    int width, int height, GBool invert,
00042                    GBool inlineImg) {
00043   FILE *f;
00044   int c;
00045   int size, i;
00046 
00047   // dump JPEG file
00048   if (dumpJPEG && str->getKind() == strDCT && !inlineImg) {
00049 
00050     // open the image file
00051     sprintf(fileName, "%s-%03d.jpg", fileRoot, imgNum);
00052     ++imgNum;
00053     if (!(f = fopen(fileName, "wb"))) {
00054       error(-1, "Couldn't open image file '%s'", fileName);
00055       return;
00056     }
00057 
00058     // initialize stream
00059     str = ((DCTStream *)str)->getRawStream();
00060     str->reset();
00061 
00062     // copy the stream
00063     while ((c = str->getChar()) != EOF)
00064       fputc(c, f);
00065 
00066     str->close();
00067     fclose(f);
00068 
00069   // dump PBM file
00070   } else {
00071 
00072     // open the image file and write the PBM header
00073     sprintf(fileName, "%s-%03d.pbm", fileRoot, imgNum);
00074     ++imgNum;
00075     if (!(f = fopen(fileName, "wb"))) {
00076       error(-1, "Couldn't open image file '%s'", fileName);
00077       return;
00078     }
00079     fprintf(f, "P4\n");
00080     fprintf(f, "%d %d\n", width, height);
00081 
00082     // initialize stream
00083     str->reset();
00084 
00085     // copy the stream
00086     size = height * ((width + 7) / 8);
00087     for (i = 0; i < size; ++i) {
00088       fputc(str->getChar(), f);
00089     }
00090 
00091     str->close();
00092     fclose(f);
00093   }
00094 }
00095 
00096 void ImageOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
00097                    int width, int height,
00098                    GfxImageColorMap *colorMap,
00099                    int *maskColors, GBool inlineImg) {
00100   FILE *f;
00101   ImageStream *imgStr;
00102   Guchar *p;
00103   GfxRGB rgb;
00104   int x, y;
00105   int c;
00106   int size, i;
00107 
00108   // dump JPEG file
00109   if (dumpJPEG && str->getKind() == strDCT &&
00110       colorMap->getNumPixelComps() == 3 &&
00111       !inlineImg) {
00112 
00113     // open the image file
00114     sprintf(fileName, "%s-%03d.jpg", fileRoot, imgNum);
00115     ++imgNum;
00116     if (!(f = fopen(fileName, "wb"))) {
00117       error(-1, "Couldn't open image file '%s'", fileName);
00118       return;
00119     }
00120 
00121     // initialize stream
00122     str = ((DCTStream *)str)->getRawStream();
00123     str->reset();
00124 
00125     // copy the stream
00126     while ((c = str->getChar()) != EOF)
00127       fputc(c, f);
00128 
00129     str->close();
00130     fclose(f);
00131 
00132   // dump PBM file
00133   } else if (colorMap->getNumPixelComps() == 1 &&
00134          colorMap->getBits() == 1) {
00135 
00136     // open the image file and write the PBM header
00137     sprintf(fileName, "%s-%03d.pbm", fileRoot, imgNum);
00138     ++imgNum;
00139     if (!(f = fopen(fileName, "wb"))) {
00140       error(-1, "Couldn't open image file '%s'", fileName);
00141       return;
00142     }
00143     fprintf(f, "P4\n");
00144     fprintf(f, "%d %d\n", width, height);
00145 
00146     // initialize stream
00147     str->reset();
00148 
00149     // copy the stream
00150     size = height * ((width + 7) / 8);
00151     for (i = 0; i < size; ++i) {
00152       fputc(str->getChar() ^ 0xff, f);
00153     }
00154 
00155     str->close();
00156     fclose(f);
00157 
00158   // dump PPM file
00159   } else {
00160 
00161     // open the image file and write the PPM header
00162     sprintf(fileName, "%s-%03d.ppm", fileRoot, imgNum);
00163     ++imgNum;
00164     if (!(f = fopen(fileName, "wb"))) {
00165       error(-1, "Couldn't open image file '%s'", fileName);
00166       return;
00167     }
00168     fprintf(f, "P6\n");
00169     fprintf(f, "%d %d\n", width, height);
00170     fprintf(f, "255\n");
00171 
00172     // initialize stream
00173     imgStr = new ImageStream(str, width, colorMap->getNumPixelComps(),
00174                  colorMap->getBits());
00175     imgStr->reset();
00176 
00177     // for each line...
00178     for (y = 0; y < height; ++y) {
00179 
00180       // write the line
00181       p = imgStr->getLine();
00182       for (x = 0; x < width; ++x) {
00183     colorMap->getRGB(p, &rgb);
00184     fputc((int)(rgb.r * 255 + 0.5), f);
00185     fputc((int)(rgb.g * 255 + 0.5), f);
00186     fputc((int)(rgb.b * 255 + 0.5), f);
00187     p += colorMap->getNumPixelComps();
00188       }
00189     }
00190     delete imgStr;
00191 
00192     fclose(f);
00193   }
00194 }
KDE Home | KDE Accessibility Home | Description of Access Keys