1 //========================================================================
5 // Copyright 1998-2003 Glyph & Cog, LLC
7 //========================================================================
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
20 #include "xpdfconfig.h"
25 #include "ImageOutputDev.h"
27 ImageOutputDev::ImageOutputDev(char *fileRootA, GBool dumpJPEGA) {
28 fileRoot = copyString(fileRootA);
29 fileName = (char *)gmalloc(strlen(fileRoot) + 20);
35 ImageOutputDev::~ImageOutputDev() {
40 void ImageOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
41 int width, int height, GBool invert,
48 if (dumpJPEG && str->getKind() == strDCT && !inlineImg) {
50 // open the image file
51 sprintf(fileName, "%s-%03d.jpg", fileRoot, imgNum);
53 if (!(f = fopen(fileName, "wb"))) {
54 error(-1, "Couldn't open image file '%s'", fileName);
59 str = ((DCTStream *)str)->getRawStream();
63 while ((c = str->getChar()) != EOF)
72 // open the image file and write the PBM header
73 sprintf(fileName, "%s-%03d.pbm", fileRoot, imgNum);
75 if (!(f = fopen(fileName, "wb"))) {
76 error(-1, "Couldn't open image file '%s'", fileName);
80 fprintf(f, "%d %d\n", width, height);
86 size = height * ((width + 7) / 8);
87 for (i = 0; i < size; ++i) {
88 fputc(str->getChar(), f);
96 void ImageOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
97 int width, int height,
98 GfxImageColorMap *colorMap,
99 int *maskColors, GBool inlineImg) {
109 if (dumpJPEG && str->getKind() == strDCT &&
110 colorMap->getNumPixelComps() == 3 &&
113 // open the image file
114 sprintf(fileName, "%s-%03d.jpg", fileRoot, imgNum);
116 if (!(f = fopen(fileName, "wb"))) {
117 error(-1, "Couldn't open image file '%s'", fileName);
122 str = ((DCTStream *)str)->getRawStream();
126 while ((c = str->getChar()) != EOF)
133 } else if (colorMap->getNumPixelComps() == 1 &&
134 colorMap->getBits() == 1) {
136 // open the image file and write the PBM header
137 sprintf(fileName, "%s-%03d.pbm", fileRoot, imgNum);
139 if (!(f = fopen(fileName, "wb"))) {
140 error(-1, "Couldn't open image file '%s'", fileName);
144 fprintf(f, "%d %d\n", width, height);
150 size = height * ((width + 7) / 8);
151 for (i = 0; i < size; ++i) {
152 fputc(str->getChar() ^ 0xff, f);
161 // open the image file and write the PPM header
162 sprintf(fileName, "%s-%03d.ppm", fileRoot, imgNum);
164 if (!(f = fopen(fileName, "wb"))) {
165 error(-1, "Couldn't open image file '%s'", fileName);
169 fprintf(f, "%d %d\n", width, height);
173 imgStr = new ImageStream(str, width, colorMap->getNumPixelComps(),
174 colorMap->getBits());
178 for (y = 0; y < height; ++y) {
181 p = imgStr->getLine();
182 for (x = 0; x < width; ++x) {
183 colorMap->getRGB(p, &rgb);
184 fputc((int)(rgb.r * 255 + 0.5), f);
185 fputc((int)(rgb.g * 255 + 0.5), f);
186 fputc((int)(rgb.b * 255 + 0.5), f);
187 p += colorMap->getNumPixelComps();