Coverage Report

Created: 2026-03-15 06:39

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
83.5k
#define splashBitmapMemCacheThreshold 4000000
25
26
12.8k
SplashBitmapMemCache::SplashBitmapMemCache() {
27
64.1k
  for (int i = 0; i < splashBitmapMemCacheSize; ++i) {
28
51.3k
    cache[i].data = NULL;
29
51.3k
    cache[i].height = 0;
30
51.3k
    cache[i].rowSize = 0;
31
51.3k
  }
32
12.8k
}
33
34
12.8k
SplashBitmapMemCache::~SplashBitmapMemCache() {
35
64.0k
  for (int i = 0; i < splashBitmapMemCacheSize; ++i) {
36
51.2k
    if (cache[i].data) {
37
19
      gfree(cache[i].data);
38
19
      cache[i].data = NULL;
39
19
      cache[i].height = 0;
40
19
      cache[i].rowSize = 0;
41
19
    }
42
51.2k
  }
43
12.8k
}
44
45
41.8k
void *SplashBitmapMemCache::alloc(int height, SplashBitmapRowSize rowSize) {
46
  // if the block is small, just allocate it
47
41.8k
  if (rowSize < splashBitmapMemCacheThreshold / height) {
48
41.7k
    return gmallocn64(height, rowSize);
49
41.7k
  }
50
51
  // if a match is found in the cache, return it
52
160
  for (int i = splashBitmapMemCacheSize - 1; i >= 0; --i) {
53
132
    if (cache[i].data &&
54
15
  cache[i].height == height &&
55
15
  cache[i].rowSize == rowSize) {
56
10
      void *data = cache[i].data;
57
20
      for (int j = i; j < splashBitmapMemCacheSize - 1; ++j) {
58
10
  cache[j] = cache[j + 1];
59
10
      }
60
10
      cache[splashBitmapMemCacheSize - 1].data = NULL;
61
10
      cache[splashBitmapMemCacheSize - 1].height = 0;
62
10
      cache[splashBitmapMemCacheSize - 1].rowSize = 0;
63
10
      return data;
64
10
    }
65
132
  }
66
67
  // if no match was found, clear the cache and allcoate the block
68
140
  for (int i = 0; i < splashBitmapMemCacheSize; ++i) {
69
112
    if (cache[i].data) {
70
0
      gfree(cache[i].data);
71
0
      cache[i].data = NULL;
72
0
      cache[i].height = 0;
73
0
      cache[i].rowSize = 0;
74
0
    }
75
112
  }
76
28
  return gmallocn64(height, rowSize);
77
38
}
78
79
void SplashBitmapMemCache::free(void *data, int height,
80
41.7k
        SplashBitmapRowSize rowSize) {
81
41.7k
  if (rowSize < splashBitmapMemCacheThreshold / height) {
82
41.7k
    gfree(data);
83
41.7k
    return;
84
41.7k
  }
85
29
  if (cache[0].data) {
86
0
    gfree(cache[0].data);
87
0
  }
88
116
  for (int i = 0; i < splashBitmapMemCacheSize - 1; ++i) {
89
87
    cache[i] = cache[i + 1];
90
87
  }
91
29
  cache[splashBitmapMemCacheSize - 1].data = data;
92
29
  cache[splashBitmapMemCacheSize - 1].height = height;
93
29
  cache[splashBitmapMemCacheSize - 1].rowSize = rowSize;
94
29
}
95
96
//------------------------------------------------------------------------
97
// SplashBitmap
98
//------------------------------------------------------------------------
99
100
SplashBitmap::SplashBitmap(int widthA, int heightA, int rowPad,
101
         SplashColorMode modeA, GBool alphaA,
102
22.3k
         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
22.3k
  width = widthA;
107
22.3k
  height = heightA;
108
22.3k
  mode = modeA;
109
22.3k
  rowSize = 0; // make gcc happy
110
22.3k
  switch (mode) {
111
129
  case splashModeMono1:
112
129
    if (width <= 0) {
113
0
      gMemError("invalid bitmap width");
114
0
    }
115
129
    rowSize = (width + 7) >> 3;
116
129
    break;
117
3.51k
  case splashModeMono8:
118
3.51k
    if (width <= 0) {
119
0
      gMemError("invalid bitmap width");
120
0
    }
121
3.51k
    rowSize = width;
122
3.51k
    break;
123
18.5k
  case splashModeRGB8:
124
18.5k
  case splashModeBGR8:
125
18.5k
    if (width <= 0 || width > INT_MAX / 3) {
126
10
      gMemError("invalid bitmap width");
127
10
    }
128
18.5k
    rowSize = (SplashBitmapRowSize)width * 3;
129
18.5k
    break;
130
0
#if SPLASH_CMYK
131
118
  case splashModeCMYK8:
132
118
    if (width <= 0 || width > INT_MAX / 4) {
133
0
      gMemError("invalid bitmap width");
134
0
    }
135
118
    rowSize = (SplashBitmapRowSize)width * 4;
136
118
    break;
137
22.3k
#endif
138
22.3k
  }
139
22.3k
  rowSize += rowPad - 1;
140
22.3k
  rowSize -= rowSize % rowPad;
141
142
22.3k
  traceAlloc(this, "alloc bitmap: %d x %d x %d %s -> %lld bytes",
143
22.3k
       width, height, splashColorModeNComps[mode],
144
22.3k
       alphaA ? "with alpha" : "without alpha",
145
22.3k
       height * rowSize + (alphaA ? height * width : 0));
146
147
22.3k
  cache = cacheA;
148
22.3k
  if (cache) {
149
22.3k
    data = (SplashColorPtr)cache->alloc(height, rowSize);
150
22.3k
  } else {
151
0
    data = (SplashColorPtr)gmallocn64(height, rowSize);
152
0
  }
153
22.3k
  if (!topDown) {
154
0
    data += (height - 1) * rowSize;
155
0
    rowSize = -rowSize;
156
0
  }
157
22.3k
  if (alphaA) {
158
19.4k
    alphaRowSize = width;
159
19.4k
    if (cache) {
160
19.4k
      alpha = (Guchar *)cache->alloc(height, alphaRowSize);
161
19.4k
    } else {
162
0
      alpha = (Guchar *)gmallocn64(height, alphaRowSize);
163
0
    }
164
19.4k
  } else {
165
2.93k
    alphaRowSize = 0;
166
2.93k
    alpha = NULL;
167
2.93k
  }
168
22.3k
}
169
170
22.3k
SplashBitmap::~SplashBitmap() {
171
22.3k
  traceFree(this, "free bitmap");
172
22.3k
  if (data && rowSize < 0) {
173
0
    rowSize = -rowSize;
174
0
    data -= (height - 1) * rowSize;
175
0
  }
176
22.3k
  if (cache) {
177
22.3k
    cache->free(data, height, rowSize);
178
22.3k
    if (alpha) {
179
19.3k
      cache->free(alpha, height, alphaRowSize);
180
19.3k
    }
181
22.3k
  } else {
182
0
    gfree(data);
183
0
    gfree(alpha);
184
0
  }
185
22.3k
}
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
74
             SplashBitmapMemCache *cacheA) {
351
74
  width = widthA;
352
74
  height = heightA;
353
74
  alphaRowSize = width;
354
355
74
  traceAlloc(this, "alloc alpha bitmap: %d x %d -> %lld bytes",
356
74
       width, height, height * alphaRowSize);
357
358
74
  cache = cacheA;
359
74
  if (cache) {
360
74
    alpha = (Guchar *)cache->alloc(height, alphaRowSize);
361
74
  } else {
362
0
    alpha = (Guchar *)gmallocn64(height, alphaRowSize);
363
0
  }
364
74
}
365
366
74
SplashAlphaBitmap::~SplashAlphaBitmap() {
367
74
  traceFree(this, "free alpha bitmap");
368
74
  if (cache) {
369
74
    cache->free(alpha, height, alphaRowSize);
370
74
  } else {
371
0
    gfree(alpha);
372
0
  }
373
74
}