Coverage Report

Created: 2026-10-13 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xpdf-4.06/splash/SplashBitmap.cc
Line
Count
Source
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
// SplashBitmapMemCache
22
//------------------------------------------------------------------------
23
24
45.8k
#define splashBitmapMemCacheThreshold 4000000
25
26
9.16k
SplashBitmapMemCache::SplashBitmapMemCache() {
27
45.8k
  for (int i = 0; i < splashBitmapMemCacheSize; ++i) {
28
36.6k
    cache[i].data = NULL;
29
36.6k
    cache[i].height = 0;
30
36.6k
    cache[i].rowSize = 0;
31
36.6k
  }
32
9.16k
}
33
34
9.14k
SplashBitmapMemCache::~SplashBitmapMemCache() {
35
45.7k
  for (int i = 0; i < splashBitmapMemCacheSize; ++i) {
36
36.5k
    if (cache[i].data) {
37
89
      gfree(cache[i].data);
38
89
      cache[i].data = NULL;
39
89
      cache[i].height = 0;
40
89
      cache[i].rowSize = 0;
41
89
    }
42
36.5k
  }
43
9.14k
}
44
45
22.9k
void *SplashBitmapMemCache::alloc(int height, SplashBitmapRowSize rowSize) {
46
  // if the block is small, just allocate it
47
22.9k
  if (rowSize < splashBitmapMemCacheThreshold / height) {
48
22.6k
    return gmallocn64(height, rowSize);
49
22.6k
  }
50
51
  // if a match is found in the cache, return it
52
928
  for (int i = splashBitmapMemCacheSize - 1; i >= 0; --i) {
53
776
    if (cache[i].data &&
54
188
  cache[i].height == height &&
55
183
  cache[i].rowSize == rowSize) {
56
94
      void *data = cache[i].data;
57
168
      for (int j = i; j < splashBitmapMemCacheSize - 1; ++j) {
58
74
  cache[j] = cache[j + 1];
59
74
      }
60
94
      cache[splashBitmapMemCacheSize - 1].data = NULL;
61
94
      cache[splashBitmapMemCacheSize - 1].height = 0;
62
94
      cache[splashBitmapMemCacheSize - 1].rowSize = 0;
63
94
      return data;
64
94
    }
65
776
  }
66
67
  // if no match was found, clear the cache and allcoate the block
68
760
  for (int i = 0; i < splashBitmapMemCacheSize; ++i) {
69
608
    if (cache[i].data) {
70
57
      gfree(cache[i].data);
71
57
      cache[i].data = NULL;
72
57
      cache[i].height = 0;
73
57
      cache[i].rowSize = 0;
74
57
    }
75
608
  }
76
152
  return gmallocn64(height, rowSize);
77
246
}
78
79
void SplashBitmapMemCache::free(void *data, int height,
80
22.8k
        SplashBitmapRowSize rowSize) {
81
22.8k
  if (rowSize < splashBitmapMemCacheThreshold / height) {
82
22.6k
    gfree(data);
83
22.6k
    return;
84
22.6k
  }
85
240
  if (cache[0].data) {
86
0
    gfree(cache[0].data);
87
0
  }
88
960
  for (int i = 0; i < splashBitmapMemCacheSize - 1; ++i) {
89
720
    cache[i] = cache[i + 1];
90
720
  }
91
240
  cache[splashBitmapMemCacheSize - 1].data = data;
92
240
  cache[splashBitmapMemCacheSize - 1].height = height;
93
240
  cache[splashBitmapMemCacheSize - 1].rowSize = rowSize;
94
240
}
95
96
//------------------------------------------------------------------------
97
// SplashBitmap
98
//------------------------------------------------------------------------
99
100
SplashBitmap::SplashBitmap(int widthA, int heightA, int rowPad,
101
         SplashColorMode modeA, GBool alphaA,
102
11.9k
         GBool topDown, SplashBitmapMemCache *cacheA) {
103
  // NB: this code checks that rowSize fits in a signed 32-bit
104
  // integer, because some code (outside this class) makes that
105
  // assumption
106
11.9k
  width = widthA;
107
11.9k
  height = heightA;
108
11.9k
  mode = modeA;
109
11.9k
  rowSize = 0; // make gcc happy
110
11.9k
  switch (mode) {
111
0
  case splashModeMono1:
112
0
    if (width <= 0) {
113
0
      gMemError("invalid bitmap width");
114
0
    }
115
0
    rowSize = (width + 7) >> 3;
116
0
    break;
117
1.14k
  case splashModeMono8:
118
1.14k
    if (width <= 0) {
119
0
      gMemError("invalid bitmap width");
120
0
    }
121
1.14k
    rowSize = width;
122
1.14k
    break;
123
10.7k
  case splashModeRGB8:
124
10.7k
  case splashModeBGR8:
125
10.7k
    if (width <= 0 || width > INT_MAX / 3) {
126
11
      gMemError("invalid bitmap width");
127
11
    }
128
10.7k
    rowSize = (SplashBitmapRowSize)width * 3;
129
10.7k
    break;
130
0
#if SPLASH_CMYK
131
30
  case splashModeCMYK8:
132
30
    if (width <= 0 || width > INT_MAX / 4) {
133
0
      gMemError("invalid bitmap width");
134
0
    }
135
30
    rowSize = (SplashBitmapRowSize)width * 4;
136
30
    break;
137
11.9k
#endif
138
11.9k
  }
139
11.9k
  rowSize += rowPad - 1;
140
11.9k
  rowSize -= rowSize % rowPad;
141
142
11.9k
  traceAlloc(this, "alloc bitmap: %d x %d x %d %s -> %lld bytes",
143
11.9k
       width, height, splashColorModeNComps[mode],
144
11.9k
       alphaA ? "with alpha" : "without alpha",
145
11.9k
       height * rowSize + (alphaA ? height * width : 0));
146
147
11.9k
  cache = cacheA;
148
11.9k
  if (cache) {
149
11.9k
    data = (SplashColorPtr)cache->alloc(height, rowSize);
150
11.9k
  } else {
151
0
    data = (SplashColorPtr)gmallocn64(height, rowSize);
152
0
  }
153
11.9k
  if (!topDown) {
154
0
    data += (height - 1) * rowSize;
155
0
    rowSize = -rowSize;
156
0
  }
157
11.9k
  if (alphaA) {
158
10.9k
    alphaRowSize = width;
159
10.9k
    if (cache) {
160
10.9k
      alpha = (Guchar *)cache->alloc(height, alphaRowSize);
161
10.9k
    } else {
162
0
      alpha = (Guchar *)gmallocn64(height, alphaRowSize);
163
0
    }
164
10.9k
  } else {
165
941
    alphaRowSize = 0;
166
941
    alpha = NULL;
167
941
  }
168
11.9k
}
169
170
11.9k
SplashBitmap::~SplashBitmap() {
171
11.9k
  traceFree(this, "free bitmap");
172
11.9k
  if (data && rowSize < 0) {
173
0
    rowSize = -rowSize;
174
0
    data -= (height - 1) * rowSize;
175
0
  }
176
11.9k
  if (cache) {
177
11.9k
    cache->free(data, height, rowSize);
178
11.9k
    if (alpha) {
179
10.9k
      cache->free(alpha, height, alphaRowSize);
180
10.9k
    }
181
11.9k
  } else {
182
0
    gfree(data);
183
0
    gfree(alpha);
184
0
  }
185
11.9k
}
186
187
0
SplashError SplashBitmap::writePNMFile(char *fileName) {
188
0
  FILE *f;
189
0
  SplashError err;
190
191
0
  if (!(f = openFile(fileName, "wb"))) {
192
0
    return splashErrOpenFile;
193
0
  }
194
0
  err = writePNMFile(f);
195
0
  fclose(f);
196
0
  return err;
197
0
}
198
199
0
SplashError SplashBitmap::writePNMFile(FILE *f) {
200
0
  SplashColorPtr row, p;
201
0
  int x, y;
202
203
0
  switch (mode) {
204
205
0
  case splashModeMono1:
206
0
    fprintf(f, "P4\n%d %d\n", width, height);
207
0
    row = data;
208
0
    for (y = 0; y < height; ++y) {
209
0
      p = row;
210
0
      for (x = 0; x < width; x += 8) {
211
0
  fputc(*p ^ 0xff, f);
212
0
  ++p;
213
0
      }
214
0
      row += rowSize;
215
0
    }
216
0
    break;
217
218
0
  case splashModeMono8:
219
0
    fprintf(f, "P5\n%d %d\n255\n", width, height);
220
0
    row = data;
221
0
    for (y = 0; y < height; ++y) {
222
0
      fwrite(row, 1, width, f);
223
0
      row += rowSize;
224
0
    }
225
0
    break;
226
227
0
  case splashModeRGB8:
228
0
    fprintf(f, "P6\n%d %d\n255\n", width, height);
229
0
    row = data;
230
0
    for (y = 0; y < height; ++y) {
231
0
      fwrite(row, 1, 3 * width, f);
232
0
      row += rowSize;
233
0
    }
234
0
    break;
235
236
0
  case splashModeBGR8:
237
0
    fprintf(f, "P6\n%d %d\n255\n", width, height);
238
0
    row = data;
239
0
    for (y = 0; y < height; ++y) {
240
0
      p = row;
241
0
      for (x = 0; x < width; ++x) {
242
0
  fputc(splashBGR8R(p), f);
243
0
  fputc(splashBGR8G(p), f);
244
0
  fputc(splashBGR8B(p), f);
245
0
  p += 3;
246
0
      }
247
0
      row += rowSize;
248
0
    }
249
0
    break;
250
251
0
#if SPLASH_CMYK
252
0
  case splashModeCMYK8:
253
0
    fprintf(f, "P7\n");
254
0
    fprintf(f, "WIDTH %d\n", width);
255
0
    fprintf(f, "HEIGHT %d\n", height);
256
0
    fprintf(f, "DEPTH 4\n");
257
0
    fprintf(f, "MAXVAL 255\n");
258
0
    fprintf(f, "TUPLTYPE CMYK\n");
259
0
    fprintf(f, "ENDHDR\n");
260
0
    row = data;
261
0
    for (y = 0; y < height; ++y) {
262
0
      fwrite(row, 1, 4 * width, f);
263
0
      row += rowSize;
264
0
    }
265
0
    break;
266
0
#endif
267
268
0
  }
269
270
0
  return splashOk;
271
0
}
272
273
0
SplashError SplashBitmap::writeAlphaPGMFile(char *fileName) {
274
0
  FILE *f;
275
276
0
  if (!alpha) {
277
0
    return splashErrModeMismatch;
278
0
  }
279
0
  if (!(f = openFile(fileName, "wb"))) {
280
0
    return splashErrOpenFile;
281
0
  }
282
0
  fprintf(f, "P5\n%d %d\n255\n", width, height);
283
0
  fwrite(alpha, 1, width * height, f);
284
0
  fclose(f);
285
0
  return splashOk;
286
0
}
287
288
289
0
void SplashBitmap::getPixel(int x, int y, SplashColorPtr pixel) {
290
0
  SplashColorPtr p;
291
292
0
  if (y < 0 || y >= height || x < 0 || x >= width) {
293
0
    return;
294
0
  }
295
0
  switch (mode) {
296
0
  case splashModeMono1:
297
0
    p = &data[y * rowSize + (x >> 3)];
298
0
    pixel[0] = (p[0] & (0x80 >> (x & 7))) ? 0xff : 0x00;
299
0
    break;
300
0
  case splashModeMono8:
301
0
    p = &data[y * rowSize + x];
302
0
    pixel[0] = p[0];
303
0
    break;
304
0
  case splashModeRGB8:
305
0
    p = &data[y * rowSize + 3 * x];
306
0
    pixel[0] = p[0];
307
0
    pixel[1] = p[1];
308
0
    pixel[2] = p[2];
309
0
    break;
310
0
  case splashModeBGR8:
311
0
    p = &data[y * rowSize + 3 * x];
312
0
    pixel[0] = p[2];
313
0
    pixel[1] = p[1];
314
0
    pixel[2] = p[0];
315
0
    break;
316
0
#if SPLASH_CMYK
317
0
  case splashModeCMYK8:
318
0
    p = &data[y * rowSize + 4 * x];
319
0
    pixel[0] = p[0];
320
0
    pixel[1] = p[1];
321
0
    pixel[2] = p[2];
322
0
    pixel[3] = p[3];
323
0
    break;
324
0
#endif
325
0
  }
326
0
}
327
328
0
Guchar SplashBitmap::getAlpha(int x, int y) {
329
0
  return alpha[y * (size_t)width + x];
330
0
}
331
332
0
SplashColorPtr SplashBitmap::takeData() {
333
0
  SplashColorPtr data2;
334
335
0
  data2 = data;
336
0
  data = NULL;
337
0
  return data2;
338
0
}
339
340
0
void SplashBitmap::doNotCache() {
341
0
  cache = NULL;
342
0
}
343
344
345
//------------------------------------------------------------------------
346
// SplashAlphaBitmap
347
//------------------------------------------------------------------------
348
349
SplashAlphaBitmap::SplashAlphaBitmap(int widthA, int heightA,
350
1
             SplashBitmapMemCache *cacheA) {
351
1
  width = widthA;
352
1
  height = heightA;
353
1
  alphaRowSize = width;
354
355
1
  traceAlloc(this, "alloc alpha bitmap: %d x %d -> %lld bytes",
356
1
       width, height, height * alphaRowSize);
357
358
1
  cache = cacheA;
359
1
  if (cache) {
360
1
    alpha = (Guchar *)cache->alloc(height, alphaRowSize);
361
1
  } else {
362
0
    alpha = (Guchar *)gmallocn64(height, alphaRowSize);
363
0
  }
364
1
}
365
366
1
SplashAlphaBitmap::~SplashAlphaBitmap() {
367
1
  traceFree(this, "free alpha bitmap");
368
1
  if (cache) {
369
1
    cache->free(alpha, height, alphaRowSize);
370
1
  } else {
371
0
    gfree(alpha);
372
0
  }
373
1
}