]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/ImageOutputDev.cc
9cd128002ea2f0fb2d140ab2667b0ce15e983489
[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, Object *ref, 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, Object *ref, Stream *str,
90                                int width, int height,
91                                GfxImageColorMap *colorMap, GBool inlineImg) {
92   FILE *f;
93   ImageStream *imgStr;
94   Guchar pixBuf[4];
95   GfxRGB rgb;
96   int x, y;
97   int c;
98
99   // dump JPEG file
100   if (dumpJPEG && str->getKind() == strDCT &&
101       colorMap->getNumPixelComps() == 3) {
102
103     // open the image file
104     sprintf(fileName, "%s-%03d.jpg", fileRoot, imgNum);
105     ++imgNum;
106     if (!(f = fopen(fileName, "wb"))) {
107       error(-1, "Couldn't open image file '%s'", fileName);
108       return;
109     }
110
111     // initialize stream
112     str = ((DCTStream *)str)->getRawStream();
113     str->reset();
114
115     // copy the stream
116     while ((c = str->getChar()) != EOF)
117       fputc(c, f);
118
119     fclose(f);
120
121   // dump PPM file
122   } else {
123
124     // open the image file and write the PPM header
125     sprintf(fileName, "%s-%03d.ppm", fileRoot, imgNum);
126     ++imgNum;
127     if (!(f = fopen(fileName, "wb"))) {
128       error(-1, "Couldn't open image file '%s'", fileName);
129       return;
130     }
131     fprintf(f, "P6\n");
132     fprintf(f, "%d %d\n", width, height);
133     fprintf(f, "255\n");
134
135     // initialize stream
136     imgStr = new ImageStream(str, width, colorMap->getNumPixelComps(),
137                              colorMap->getBits());
138     imgStr->reset();
139
140     // for each line...
141     for (y = 0; y < height; ++y) {
142
143       // write the line
144       for (x = 0; x < width; ++x) {
145         imgStr->getPixel(pixBuf);
146         colorMap->getRGB(pixBuf, &rgb);
147         fputc((int)(rgb.r * 255 + 0.5), f);
148         fputc((int)(rgb.g * 255 + 0.5), f);
149         fputc((int)(rgb.b * 255 + 0.5), f);
150       }
151     }
152     delete imgStr;
153
154     fclose(f);
155   }
156 }