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