Coverage Report

Created: 2025-07-11 07:47

/src/xpdf-4.05/splash/SplashBitmap.cc
Line
Count
Source (jump to first uncovered line)
1
//========================================================================
2
//
3
// SplashBitmap.cc
4
//
5
// Copyright 2003-2013 Glyph & Cog, LLC
6
//
7
//========================================================================
8
9
#include <aconf.h>
10
11
#include <stdio.h>
12
#include <limits.h>
13
#include "gmem.h"
14
#include "gmempp.h"
15
#include "gfile.h"
16
#include "Trace.h"
17
#include "SplashErrorCodes.h"
18
#include "SplashBitmap.h"
19
20
//------------------------------------------------------------------------
21
// SplashBitmap
22
//------------------------------------------------------------------------
23
24
SplashBitmap::SplashBitmap(int widthA, int heightA, int rowPad,
25
         SplashColorMode modeA, GBool alphaA,
26
0
         GBool topDown, SplashBitmap *parentA) {
27
  // NB: this code checks that rowSize fits in a signed 32-bit
28
  // integer, because some code (outside this class) makes that
29
  // assumption
30
0
  width = widthA;
31
0
  height = heightA;
32
0
  mode = modeA;
33
0
  switch (mode) {
34
0
  case splashModeMono1:
35
0
    if (width <= 0) {
36
0
      gMemError("invalid bitmap width");
37
0
    }
38
0
    rowSize = (width + 7) >> 3;
39
0
    break;
40
0
  case splashModeMono8:
41
0
    if (width <= 0) {
42
0
      gMemError("invalid bitmap width");
43
0
    }
44
0
    rowSize = width;
45
0
    break;
46
0
  case splashModeRGB8:
47
0
  case splashModeBGR8:
48
0
    if (width <= 0 || width > INT_MAX / 3) {
49
0
      gMemError("invalid bitmap width");
50
0
    }
51
0
    rowSize = (SplashBitmapRowSize)width * 3;
52
0
    break;
53
0
#if SPLASH_CMYK
54
0
  case splashModeCMYK8:
55
0
    if (width <= 0 || width > INT_MAX / 4) {
56
0
      gMemError("invalid bitmap width");
57
0
    }
58
0
    rowSize = (SplashBitmapRowSize)width * 4;
59
0
    break;
60
0
#endif
61
0
  }
62
0
  rowSize += rowPad - 1;
63
0
  rowSize -= rowSize % rowPad;
64
65
0
  traceAlloc(this, "alloc bitmap: %d x %d x %d %s -> %lld bytes",
66
0
       width, height, splashColorModeNComps[mode],
67
0
       alphaA ? "with alpha" : "without alpha",
68
0
       height * rowSize + (alphaA ? height * width : 0));
69
70
0
  parent = parentA;
71
0
  oldData = NULL;
72
0
  oldAlpha = NULL;
73
0
  oldRowSize = 0;
74
0
  oldAlphaRowSize = 0;
75
0
  oldHeight = 0;
76
0
  if (parent && parent->oldData &&
77
0
      parent->oldRowSize == rowSize &&
78
0
      parent->oldHeight == height) {
79
0
    data = parent->oldData;
80
0
    parent->oldData = NULL;
81
0
    traceMessage("reusing bitmap memory");
82
0
  } else {
83
0
    data = (SplashColorPtr)gmallocn64(height, rowSize);
84
0
    traceMessage("not reusing bitmap memory"
85
0
     " (parent=%p parent->oldData=%p same-size=%d)",
86
0
     parent, parent ? parent->oldData : NULL,
87
0
     parent ? (parent->oldRowSize == rowSize &&
88
0
         parent->oldHeight == height) : 0);
89
0
  }
90
0
  if (!topDown) {
91
0
    data += (height - 1) * rowSize;
92
0
    rowSize = -rowSize;
93
0
  }
94
0
  if (alphaA) {
95
0
    alphaRowSize = width;
96
0
    if (parent && parent->oldAlpha &&
97
0
  parent->oldAlphaRowSize == alphaRowSize &&
98
0
  parent->oldHeight == height) {
99
0
      alpha = parent->oldAlpha;
100
0
      parent->oldAlpha = NULL;
101
0
    } else {
102
0
      alpha = (Guchar *)gmallocn64(height, alphaRowSize);
103
0
    }
104
0
  } else {
105
0
    alphaRowSize = 0;
106
0
    alpha = NULL;
107
0
  }
108
0
}
109
110
0
SplashBitmap::~SplashBitmap() {
111
0
  traceFree(this, "free bitmap");
112
0
  if (data && rowSize < 0) {
113
0
    rowSize = -rowSize;
114
0
    data -= (height - 1) * rowSize;
115
0
  }
116
0
  if (parent && rowSize > 4000000 / height) {
117
0
    gfree(parent->oldData);
118
0
    gfree(parent->oldAlpha);
119
0
    parent->oldData = data;
120
0
    parent->oldAlpha = alpha;
121
0
    parent->oldRowSize = rowSize;
122
0
    parent->oldAlphaRowSize = alphaRowSize;
123
0
    parent->oldHeight = height;
124
0
  } else {
125
0
    gfree(data);
126
0
    gfree(alpha);
127
0
  }
128
0
  gfree(oldData);
129
0
  gfree(oldAlpha);
130
0
}
131
132
0
SplashError SplashBitmap::writePNMFile(char *fileName) {
133
0
  FILE *f;
134
0
  SplashError err;
135
136
0
  if (!(f = openFile(fileName, "wb"))) {
137
0
    return splashErrOpenFile;
138
0
  }
139
0
  err = writePNMFile(f);
140
0
  fclose(f);
141
0
  return err;
142
0
}
143
144
0
SplashError SplashBitmap::writePNMFile(FILE *f) {
145
0
  SplashColorPtr row, p;
146
0
  int x, y;
147
148
0
  switch (mode) {
149
150
0
  case splashModeMono1:
151
0
    fprintf(f, "P4\n%d %d\n", width, height);
152
0
    row = data;
153
0
    for (y = 0; y < height; ++y) {
154
0
      p = row;
155
0
      for (x = 0; x < width; x += 8) {
156
0
  fputc(*p ^ 0xff, f);
157
0
  ++p;
158
0
      }
159
0
      row += rowSize;
160
0
    }
161
0
    break;
162
163
0
  case splashModeMono8:
164
0
    fprintf(f, "P5\n%d %d\n255\n", width, height);
165
0
    row = data;
166
0
    for (y = 0; y < height; ++y) {
167
0
      fwrite(row, 1, width, f);
168
0
      row += rowSize;
169
0
    }
170
0
    break;
171
172
0
  case splashModeRGB8:
173
0
    fprintf(f, "P6\n%d %d\n255\n", width, height);
174
0
    row = data;
175
0
    for (y = 0; y < height; ++y) {
176
0
      fwrite(row, 1, 3 * width, f);
177
0
      row += rowSize;
178
0
    }
179
0
    break;
180
181
0
  case splashModeBGR8:
182
0
    fprintf(f, "P6\n%d %d\n255\n", width, height);
183
0
    row = data;
184
0
    for (y = 0; y < height; ++y) {
185
0
      p = row;
186
0
      for (x = 0; x < width; ++x) {
187
0
  fputc(splashBGR8R(p), f);
188
0
  fputc(splashBGR8G(p), f);
189
0
  fputc(splashBGR8B(p), f);
190
0
  p += 3;
191
0
      }
192
0
      row += rowSize;
193
0
    }
194
0
    break;
195
196
0
#if SPLASH_CMYK
197
0
  case splashModeCMYK8:
198
0
    fprintf(f, "P7\n");
199
0
    fprintf(f, "WIDTH %d\n", width);
200
0
    fprintf(f, "HEIGHT %d\n", height);
201
0
    fprintf(f, "DEPTH 4\n");
202
0
    fprintf(f, "MAXVAL 255\n");
203
0
    fprintf(f, "TUPLTYPE CMYK\n");
204
0
    fprintf(f, "ENDHDR\n");
205
0
    row = data;
206
0
    for (y = 0; y < height; ++y) {
207
0
      fwrite(row, 1, 4 * width, f);
208
0
      row += rowSize;
209
0
    }
210
0
    break;
211
0
#endif
212
213
0
  }
214
215
0
  return splashOk;
216
0
}
217
218
0
SplashError SplashBitmap::writeAlphaPGMFile(char *fileName) {
219
0
  FILE *f;
220
221
0
  if (!alpha) {
222
0
    return splashErrModeMismatch;
223
0
  }
224
0
  if (!(f = openFile(fileName, "wb"))) {
225
0
    return splashErrOpenFile;
226
0
  }
227
0
  fprintf(f, "P5\n%d %d\n255\n", width, height);
228
0
  fwrite(alpha, 1, width * height, f);
229
0
  fclose(f);
230
0
  return splashOk;
231
0
}
232
233
234
0
void SplashBitmap::getPixel(int x, int y, SplashColorPtr pixel) {
235
0
  SplashColorPtr p;
236
237
0
  if (y < 0 || y >= height || x < 0 || x >= width) {
238
0
    return;
239
0
  }
240
0
  switch (mode) {
241
0
  case splashModeMono1:
242
0
    p = &data[y * rowSize + (x >> 3)];
243
0
    pixel[0] = (p[0] & (0x80 >> (x & 7))) ? 0xff : 0x00;
244
0
    break;
245
0
  case splashModeMono8:
246
0
    p = &data[y * rowSize + x];
247
0
    pixel[0] = p[0];
248
0
    break;
249
0
  case splashModeRGB8:
250
0
    p = &data[y * rowSize + 3 * x];
251
0
    pixel[0] = p[0];
252
0
    pixel[1] = p[1];
253
0
    pixel[2] = p[2];
254
0
    break;
255
0
  case splashModeBGR8:
256
0
    p = &data[y * rowSize + 3 * x];
257
0
    pixel[0] = p[2];
258
0
    pixel[1] = p[1];
259
0
    pixel[2] = p[0];
260
0
    break;
261
0
#if SPLASH_CMYK
262
0
  case splashModeCMYK8:
263
0
    p = &data[y * rowSize + 4 * x];
264
0
    pixel[0] = p[0];
265
0
    pixel[1] = p[1];
266
0
    pixel[2] = p[2];
267
0
    pixel[3] = p[3];
268
0
    break;
269
0
#endif
270
0
  }
271
0
}
272
273
0
Guchar SplashBitmap::getAlpha(int x, int y) {
274
0
  return alpha[y * (size_t)width + x];
275
0
}
276
277
0
SplashColorPtr SplashBitmap::takeData() {
278
0
  SplashColorPtr data2;
279
280
0
  data2 = data;
281
0
  data = NULL;
282
0
  return data2;
283
0
}
284