]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/ImageOutputDev.cc
Synched with Xpdf 1.01
[evince.git] / pdf / xpdf / ImageOutputDev.cc
1 //========================================================================
2 //
3 // ImageOutputDev.cc
4 //
5 // Copyright 1998-2002 Glyph & Cog, LLC
6 //
7 //========================================================================
8
9 #ifdef __GNUC__
10 #pragma implementation
11 #endif
12
13 #include <aconf.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <stddef.h>
17 #include <ctype.h>
18 #include "gmem.h"
19 #include "config.h"
20 #include "Error.h"
21 #include "GfxState.h"
22 #include "Object.h"
23 #include "Stream.h"
24 #include "ImageOutputDev.h"
25
26 ImageOutputDev::ImageOutputDev(char *fileRootA, GBool dumpJPEGA) {
27   fileRoot = copyString(fileRootA);
28   fileName = (char *)gmalloc(strlen(fileRoot) + 20);
29   dumpJPEG = dumpJPEGA;
30   imgNum = 0;
31   ok = gTrue;
32 }
33
34 ImageOutputDev::~ImageOutputDev() {
35   gfree(fileName);
36   gfree(fileRoot);
37 }
38
39 void ImageOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
40                                    int width, int height, GBool invert,
41                                    GBool inlineImg) {
42   FILE *f;
43   int c;
44   int size, i;
45
46   // dump JPEG file
47   if (dumpJPEG && str->getKind() == strDCT && !inlineImg) {
48
49     // open the image file
50     sprintf(fileName, "%s-%03d.jpg", fileRoot, imgNum);
51     ++imgNum;
52     if (!(f = fopen(fileName, "wb"))) {
53       error(-1, "Couldn't open image file '%s'", fileName);
54       return;
55     }
56
57     // initialize stream
58     str = ((DCTStream *)str)->getRawStream();
59     str->reset();
60
61     // copy the stream
62     while ((c = str->getChar()) != EOF)
63       fputc(c, f);
64
65     str->close();
66     fclose(f);
67
68   // dump PBM file
69   } else {
70
71     // open the image file and write the PBM header
72     sprintf(fileName, "%s-%03d.pbm", fileRoot, imgNum);
73     ++imgNum;
74     if (!(f = fopen(fileName, "wb"))) {
75       error(-1, "Couldn't open image file '%s'", fileName);
76       return;
77     }
78     fprintf(f, "P4\n");
79     fprintf(f, "%d %d\n", width, height);
80
81     // initialize stream
82     str->reset();
83
84     // copy the stream
85     size = height * ((width + 7) / 8);
86     for (i = 0; i < size; ++i) {
87       fputc(str->getChar(), f);
88     }
89
90     str->close();
91     fclose(f);
92   }
93 }
94
95 void ImageOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
96                                int width, int height,
97                                GfxImageColorMap *colorMap,
98                                int *maskColors, GBool inlineImg) {
99   FILE *f;
100   ImageStream *imgStr;
101   Guchar pixBuf[4];
102   GfxRGB rgb;
103   int x, y;
104   int c;
105   int size, i;
106
107   // dump JPEG file
108   if (dumpJPEG && str->getKind() == strDCT &&
109       colorMap->getNumPixelComps() == 3 &&
110       !inlineImg) {
111
112     // open the image file
113     sprintf(fileName, "%s-%03d.jpg", fileRoot, imgNum);
114     ++imgNum;
115     if (!(f = fopen(fileName, "wb"))) {
116       error(-1, "Couldn't open image file '%s'", fileName);
117       return;
118     }
119
120     // initialize stream
121     str = ((DCTStream *)str)->getRawStream();
122     str->reset();
123
124     // copy the stream
125     while ((c = str->getChar()) != EOF)
126       fputc(c, f);
127
128     str->close();
129     fclose(f);
130
131   // dump PBM file
132   } else if (colorMap->getNumPixelComps() == 1 &&
133              colorMap->getBits() == 1) {
134
135     // open the image file and write the PBM header
136     sprintf(fileName, "%s-%03d.pbm", fileRoot, imgNum);
137     ++imgNum;
138     if (!(f = fopen(fileName, "wb"))) {
139       error(-1, "Couldn't open image file '%s'", fileName);
140       return;
141     }
142     fprintf(f, "P4\n");
143     fprintf(f, "%d %d\n", width, height);
144
145     // initialize stream
146     str->reset();
147
148     // copy the stream
149     size = height * ((width + 7) / 8);
150     for (i = 0; i < size; ++i) {
151       fputc(str->getChar(), f);
152     }
153
154     str->close();
155     fclose(f);
156
157   // dump PPM file
158   } else {
159
160     // open the image file and write the PPM header
161     sprintf(fileName, "%s-%03d.ppm", fileRoot, imgNum);
162     ++imgNum;
163     if (!(f = fopen(fileName, "wb"))) {
164       error(-1, "Couldn't open image file '%s'", fileName);
165       return;
166     }
167     fprintf(f, "P6\n");
168     fprintf(f, "%d %d\n", width, height);
169     fprintf(f, "255\n");
170
171     // initialize stream
172     imgStr = new ImageStream(str, width, colorMap->getNumPixelComps(),
173                              colorMap->getBits());
174     imgStr->reset();
175
176     // for each line...
177     for (y = 0; y < height; ++y) {
178
179       // write the line
180       for (x = 0; x < width; ++x) {
181         imgStr->getPixel(pixBuf);
182         colorMap->getRGB(pixBuf, &rgb);
183         fputc((int)(rgb.r * 255 + 0.5), f);
184         fputc((int)(rgb.g * 255 + 0.5), f);
185         fputc((int)(rgb.b * 255 + 0.5), f);
186       }
187     }
188     delete imgStr;
189
190     fclose(f);
191   }
192 }