1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
//========================================================================
//
// ImageOutputDev.cc
//
// Copyright 1998-2003 Glyph & Cog, LLC
//
//========================================================================
#include <aconf.h>
#ifdef USE_GCC_PRAGMAS
#pragma implementation
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <ctype.h>
#include "gmem.h"
#include "config.h"
#include "Error.h"
#include "GfxState.h"
#include "Object.h"
#include "Stream.h"
#include "ImageOutputDev.h"
ImageOutputDev::ImageOutputDev(char *fileRootA, GBool dumpJPEGA) {
fileRoot = copyString(fileRootA);
fileName = (char *)gmalloc((int)strlen(fileRoot) + 20);
dumpJPEG = dumpJPEGA;
imgNum = 0;
ok = gTrue;
}
ImageOutputDev::~ImageOutputDev() {
gfree(fileName);
gfree(fileRoot);
}
void ImageOutputDev::tilingPatternFill(GfxState *state, Gfx *gfx,
Object *strRef,
int paintType, Dict *resDict,
double *mat, double *bbox,
int x0, int y0, int x1, int y1,
double xStep, double yStep) {
// do nothing -- this avoids the potentially slow loop in Gfx.cc
}
void ImageOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
int width, int height, GBool invert,
GBool inlineImg, GBool interpolate) {
FILE *f;
char buf[4096];
int size, n, i;
// dump JPEG file
if (dumpJPEG && str->getKind() == strDCT && !inlineImg) {
// open the image file
sprintf(fileName, "%s-%04d.jpg", fileRoot, imgNum);
++imgNum;
if (!(f = fopen(fileName, "wb"))) {
error(errIO, -1, "Couldn't open image file '{0:s}'", fileName);
return;
}
// initialize stream
str = ((DCTStream *)str)->getRawStream();
str->reset();
// copy the stream
while ((n = str->getBlock(buf, sizeof(buf))) > 0) {
fwrite(buf, 1, n, f);
}
str->close();
fclose(f);
// dump PBM file
} else {
// open the image file and write the PBM header
sprintf(fileName, "%s-%04d.pbm", fileRoot, imgNum);
++imgNum;
if (!(f = fopen(fileName, "wb"))) {
error(errIO, -1, "Couldn't open image file '{0:s}'", fileName);
return;
}
fprintf(f, "P4\n");
fprintf(f, "%d %d\n", width, height);
// initialize stream
str->reset();
// copy the stream
size = height * ((width + 7) / 8);
while (size > 0) {
i = size < (int)sizeof(buf) ? size : (int)sizeof(buf);
n = str->getBlock(buf, i);
fwrite(buf, 1, n, f);
if (n < i) {
break;
}
size -= n;
}
str->close();
fclose(f);
}
}
void ImageOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
int width, int height,
GfxImageColorMap *colorMap,
int *maskColors, GBool inlineImg,
GBool interpolate) {
FILE *f;
ImageStream *imgStr;
Guchar *p;
GfxRGB rgb;
int x, y;
char buf[4096];
int size, n, i;
// dump JPEG file
if (dumpJPEG && str->getKind() == strDCT &&
(colorMap->getNumPixelComps() == 1 ||
colorMap->getNumPixelComps() == 3) &&
!inlineImg) {
// open the image file
sprintf(fileName, "%s-%04d.jpg", fileRoot, imgNum);
++imgNum;
if (!(f = fopen(fileName, "wb"))) {
error(errIO, -1, "Couldn't open image file '{0:s}'", fileName);
return;
}
// initialize stream
str = ((DCTStream *)str)->getRawStream();
str->reset();
// copy the stream
while ((n = str->getBlock(buf, sizeof(buf))) > 0) {
fwrite(buf, 1, n, f);
}
str->close();
fclose(f);
// dump PBM file
} else if (colorMap->getNumPixelComps() == 1 &&
colorMap->getBits() == 1) {
// open the image file and write the PBM header
sprintf(fileName, "%s-%04d.pbm", fileRoot, imgNum);
++imgNum;
if (!(f = fopen(fileName, "wb"))) {
error(errIO, -1, "Couldn't open image file '{0:s}'", fileName);
return;
}
fprintf(f, "P4\n");
fprintf(f, "%d %d\n", width, height);
// initialize stream
str->reset();
// copy the stream
size = height * ((width + 7) / 8);
while (size > 0) {
i = size < (int)sizeof(buf) ? size : (int)sizeof(buf);
n = str->getBlock(buf, i);
fwrite(buf, 1, n, f);
if (n < i) {
break;
}
size -= n;
}
str->close();
fclose(f);
// dump PPM file
} else {
// open the image file and write the PPM header
sprintf(fileName, "%s-%04d.ppm", fileRoot, imgNum);
++imgNum;
if (!(f = fopen(fileName, "wb"))) {
error(errIO, -1, "Couldn't open image file '{0:s}'", fileName);
return;
}
fprintf(f, "P6\n");
fprintf(f, "%d %d\n", width, height);
fprintf(f, "255\n");
// initialize stream
imgStr = new ImageStream(str, width, colorMap->getNumPixelComps(),
colorMap->getBits());
imgStr->reset();
// for each line...
for (y = 0; y < height; ++y) {
// write the line
if ((p = imgStr->getLine())) {
for (x = 0; x < width; ++x) {
colorMap->getRGB(p, &rgb);
fputc(colToByte(rgb.r), f);
fputc(colToByte(rgb.g), f);
fputc(colToByte(rgb.b), f);
p += colorMap->getNumPixelComps();
}
} else {
for (x = 0; x < width; ++x) {
fputc(0, f);
fputc(0, f);
fputc(0, f);
}
}
}
imgStr->close();
delete imgStr;
fclose(f);
}
}
void ImageOutputDev::drawMaskedImage(GfxState *state, Object *ref, Stream *str,
int width, int height,
GfxImageColorMap *colorMap,
Stream *maskStr,
int maskWidth, int maskHeight,
GBool maskInvert, GBool interpolate) {
drawImage(state, ref, str, width, height, colorMap,
NULL, gFalse, interpolate);
drawImageMask(state, ref, maskStr, maskWidth, maskHeight, maskInvert,
gFalse, interpolate);
}
void ImageOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref,
Stream *str,
int width, int height,
GfxImageColorMap *colorMap,
Stream *maskStr,
int maskWidth, int maskHeight,
GfxImageColorMap *maskColorMap,
GBool interpolate) {
drawImage(state, ref, str, width, height, colorMap,
NULL, gFalse, interpolate);
drawImage(state, ref, maskStr, maskWidth, maskHeight, maskColorMap,
NULL, gFalse, interpolate);
}
|