Coverage Report

Created: 2026-06-22 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xpdf-4.06/splash/SplashScreen.cc
Line
Count
Source
1
//========================================================================
2
//
3
// SplashScreen.cc
4
//
5
// Copyright 2003-2013 Glyph & Cog, LLC
6
//
7
//========================================================================
8
9
#include <aconf.h>
10
11
#include <stdlib.h>
12
#include <string.h>
13
#if HAVE_STD_SORT
14
#include <algorithm>
15
#endif
16
#include "gmem.h"
17
#include "gmempp.h"
18
#include "SplashMath.h"
19
#include "SplashScreen.h"
20
21
//------------------------------------------------------------------------
22
23
static SplashScreenParams defaultParams = {
24
  splashScreenDispersed,  // type
25
  2,        // size
26
  2,        // dotRadius
27
  1.0,        // gamma
28
  0.0,        // blackThreshold
29
  1.0       // whiteThreshold
30
};
31
32
//------------------------------------------------------------------------
33
34
struct SplashScreenPoint {
35
  int x, y;
36
  int dist;
37
};
38
39
#if HAVE_STD_SORT
40
41
struct cmpDistancesFunctor {
42
31.0M
  bool operator()(const SplashScreenPoint &p0, const SplashScreenPoint &p1) {
43
31.0M
    return p0.dist < p1.dist;
44
31.0M
  }
45
};
46
47
#else // HAVE_STD_SORT
48
49
static int cmpDistances(const void *p0, const void *p1) {
50
  return ((SplashScreenPoint *)p0)->dist - ((SplashScreenPoint *)p1)->dist;
51
}
52
53
#endif
54
55
//------------------------------------------------------------------------
56
// SplashScreen
57
//------------------------------------------------------------------------
58
59
// If <clustered> is true, this generates a 45 degree screen using a
60
// circular dot spot function.  DPI = resolution / ((size / 2) *
61
// sqrt(2)).  If <clustered> is false, this generates an optimal
62
// threshold matrix using recursive tesselation.  Gamma correction
63
// (gamma = 1 / 1.33) is also computed here.
64
110k
SplashScreen::SplashScreen(SplashScreenParams *params) {
65
110k
  Guchar u;
66
110k
  int black, white, i;
67
68
110k
  if (!params) {
69
1.69k
    params = &defaultParams;
70
1.69k
  }
71
72
  // size must be a power of 2, and at least 2
73
229k
  for (size = 2, log2Size = 1; size < params->size; size <<= 1, ++log2Size) ;
74
75
110k
  switch (params->type) {
76
77
108k
  case splashScreenDispersed:
78
108k
    mat = (Guchar *)gmallocn(size * size, sizeof(Guchar));
79
108k
    buildDispersedMatrix(size/2, size/2, 1, size/2, 1);
80
108k
    break;
81
82
0
  case splashScreenClustered:
83
0
    mat = (Guchar *)gmallocn(size * size, sizeof(Guchar));
84
0
    buildClusteredMatrix();
85
0
    break;
86
87
2.31k
  case splashScreenStochasticClustered:
88
    // size must be at least 2*r
89
2.31k
    while (size < (params->dotRadius << 1)) {
90
0
      size <<= 1;
91
0
      ++log2Size;
92
0
    }
93
2.31k
    mat = (Guchar *)gmallocn(size * size, sizeof(Guchar));
94
2.31k
    buildSCDMatrix(params->dotRadius);
95
2.31k
    break;
96
110k
  }
97
98
110k
  sizeM1 = size - 1;
99
100
  // do gamma correction and compute minVal/maxVal
101
110k
  minVal = 255;
102
110k
  maxVal = 0;
103
110k
  black = splashRound((SplashCoord)255.0 * params->blackThreshold);
104
110k
  if (black < 1) {
105
110k
    black = 1;
106
110k
  }
107
110k
  white = splashRound((SplashCoord)255.0 * params->whiteThreshold);
108
110k
  if (white > 255) {
109
0
    white = 255;
110
0
  }
111
11.3M
  for (i = 0; i < size * size; ++i) {
112
11.1M
    u = (Guchar)splashRound((SplashCoord)255.0 *
113
11.1M
          splashPow((SplashCoord)mat[i] / 255.0,
114
11.1M
              params->gamma));
115
11.1M
    if (u < black) {
116
0
      u = (Guchar)black;
117
11.1M
    } else if (u >= white) {
118
830k
      u = (Guchar)white;
119
830k
    }
120
11.1M
    mat[i] = u;
121
11.1M
    if (u < minVal) {
122
226k
      minVal = u;
123
10.9M
    } else if (u > maxVal) {
124
121k
      maxVal = u;
125
121k
    }
126
11.1M
  }
127
110k
}
128
129
void SplashScreen::buildDispersedMatrix(int i, int j, int val,
130
2.25M
          int delta, int offset) {
131
2.25M
  if (delta == 0) {
132
    // map values in [1, size^2] --> [1, 255]
133
1.71M
    mat[(i << log2Size) + j] =
134
1.71M
        (Guchar)(1 + (254 * (val - 1)) / (size * size - 1));
135
1.71M
  } else {
136
536k
    buildDispersedMatrix(i, j,
137
536k
       val, delta / 2, 4*offset);
138
536k
    buildDispersedMatrix((i + delta) % size, (j + delta) % size,
139
536k
       val + offset, delta / 2, 4*offset);
140
536k
    buildDispersedMatrix((i + delta) % size, j,
141
536k
       val + 2*offset, delta / 2, 4*offset);
142
536k
    buildDispersedMatrix((i + 2*delta) % size, (j + delta) % size,
143
536k
       val + 3*offset, delta / 2, 4*offset);
144
536k
  }
145
2.25M
}
146
147
0
void SplashScreen::buildClusteredMatrix() {
148
0
  SplashCoord *dist;
149
0
  SplashCoord u, v, d;
150
0
  Guchar val;
151
0
  int size2, x, y, x1, y1, i;
152
153
0
  size2 = size >> 1;
154
155
  // initialize the threshold matrix
156
0
  for (y = 0; y < size; ++y) {
157
0
    for (x = 0; x < size; ++x) {
158
0
      mat[(y << log2Size) + x] = 0;
159
0
    }
160
0
  }
161
162
  // build the distance matrix
163
0
  dist = (SplashCoord *)gmallocn(size * size2, sizeof(SplashCoord));
164
0
  for (y = 0; y < size2; ++y) {
165
0
    for (x = 0; x < size2; ++x) {
166
0
      if (x + y < size2 - 1) {
167
0
  u = (SplashCoord)x + 0.5 - 0;
168
0
  v = (SplashCoord)y + 0.5 - 0;
169
0
      } else {
170
0
  u = (SplashCoord)x + 0.5 - (SplashCoord)size2;
171
0
  v = (SplashCoord)y + 0.5 - (SplashCoord)size2;
172
0
      }
173
0
      dist[y * size2 + x] = u*u + v*v;
174
0
    }
175
0
  }
176
0
  for (y = 0; y < size2; ++y) {
177
0
    for (x = 0; x < size2; ++x) {
178
0
      if (x < y) {
179
0
  u = (SplashCoord)x + 0.5 - 0;
180
0
  v = (SplashCoord)y + 0.5 - (SplashCoord)size2;
181
0
      } else {
182
0
  u = (SplashCoord)x + 0.5 - (SplashCoord)size2;
183
0
  v = (SplashCoord)y + 0.5 - 0;
184
0
      }
185
0
      dist[(size2 + y) * size2 + x] = u*u + v*v;
186
0
    }
187
0
  }
188
189
  // build the threshold matrix
190
0
  x1 = y1 = 0; // make gcc happy
191
0
  for (i = 0; i < size * size2; ++i) {
192
0
    d = -1;
193
0
    for (y = 0; y < size; ++y) {
194
0
      for (x = 0; x < size2; ++x) {
195
0
  if (mat[(y << log2Size) + x] == 0 &&
196
0
      dist[y * size2 + x] > d) {
197
0
    x1 = x;
198
0
    y1 = y;
199
0
    d = dist[y1 * size2 + x1];
200
0
  }
201
0
      }
202
0
    }
203
    // map values in [0, 2*size*size2-1] --> [1, 255]
204
0
    val = (Guchar)(1 + (254 * (2*i)) / (2*size*size2 - 1));
205
0
    mat[(y1 << log2Size) + x1] = val;
206
0
    val = (Guchar)(1 + (254 * (2*i+1)) / (2*size*size2 - 1));
207
0
    if (y1 < size2) {
208
0
      mat[((y1 + size2) << log2Size) + x1 + size2] = val;
209
0
    } else {
210
0
      mat[((y1 - size2) << log2Size) + x1 + size2] = val;
211
0
    }
212
0
  }
213
214
0
  gfree(dist);
215
0
}
216
217
// Compute the distance between two points on a toroid.
218
2.96G
int SplashScreen::distance(int x0, int y0, int x1, int y1) {
219
2.96G
  int dx0, dx1, dx, dy0, dy1, dy;
220
221
2.96G
  dx0 = abs(x0 - x1);
222
2.96G
  dx1 = size - dx0;
223
2.96G
  dx = dx0 < dx1 ? dx0 : dx1;
224
2.96G
  dy0 = abs(y0 - y1);
225
2.96G
  dy1 = size - dy0;
226
2.96G
  dy = dy0 < dy1 ? dy0 : dy1;
227
2.96G
  return dx * dx + dy * dy;
228
2.96G
}
229
230
// Algorithm taken from:
231
// Victor Ostromoukhov and Roger D. Hersch, "Stochastic Clustered-Dot
232
// Dithering" in Color Imaging: Device-Independent Color, Color
233
// Hardcopy, and Graphic Arts IV, SPIE Vol. 3648, pp. 496-505, 1999.
234
2.31k
void SplashScreen::buildSCDMatrix(int r) {
235
2.31k
  SplashScreenPoint *dots, *pts;
236
2.31k
  int dotsLen, dotsSize;
237
2.31k
  char *tmpl;
238
2.31k
  char *grid;
239
2.31k
  int *region, *dist;
240
2.31k
  int x, y, xx, yy, x0, x1, y0, y1, i, j, d, iMin, dMin, n;
241
242
  //~ this should probably happen somewhere else
243
2.31k
  srand(123);
244
245
  // generate the random space-filling curve
246
2.31k
  pts = (SplashScreenPoint *)gmallocn(size * size, sizeof(SplashScreenPoint));
247
2.31k
  i = 0;
248
150k
  for (y = 0; y < size; ++y) {
249
9.62M
    for (x = 0; x < size; ++x) {
250
9.47M
      pts[i].x = x;
251
9.47M
      pts[i].y = y;
252
9.47M
      ++i;
253
9.47M
    }
254
148k
  }
255
9.48M
  for (i = 0; i < size * size; ++i) {
256
9.47M
    j = i + (int)((double)(size * size - i) *
257
9.47M
      (double)rand() / ((double)RAND_MAX + 1.0));
258
9.47M
    x = pts[i].x;
259
9.47M
    y = pts[i].y;
260
9.47M
    pts[i].x = pts[j].x;
261
9.47M
    pts[i].y = pts[j].y;
262
9.47M
    pts[j].x = x;
263
9.47M
    pts[j].y = y;
264
9.47M
  }
265
266
  // construct the circle template
267
2.31k
  tmpl = (char *)gmallocn((r+1)*(r+1), sizeof(char));
268
9.25k
  for (y = 0; y <= r; ++y) {
269
27.7k
    for (x = 0; x <= r; ++x) {
270
20.8k
      tmpl[y*(r+1) + x] = (x * y <= r * r) ? 1 : 0;
271
20.8k
    }
272
6.94k
  }
273
274
  // mark all grid cells as free
275
2.31k
  grid = (char *)gmallocn(size * size, sizeof(char));
276
150k
  for (y = 0; y < size; ++y) {
277
9.62M
    for (x = 0; x < size; ++x) {
278
9.47M
      grid[(y << log2Size) + x] = 0;
279
9.47M
    }
280
148k
  }
281
282
  // walk the space-filling curve, adding dots
283
2.31k
  dotsLen = 0;
284
2.31k
  dotsSize = 32;
285
2.31k
  dots = (SplashScreenPoint *)gmallocn(dotsSize, sizeof(SplashScreenPoint));
286
9.48M
  for (i = 0; i < size * size; ++i) {
287
9.47M
    x = pts[i].x;
288
9.47M
    y = pts[i].y;
289
9.47M
    if (!grid[(y << log2Size) + x]) {
290
721k
      if (dotsLen == dotsSize) {
291
9.25k
  dotsSize *= 2;
292
9.25k
  dots = (SplashScreenPoint *)greallocn(dots, dotsSize,
293
9.25k
                sizeof(SplashScreenPoint));
294
9.25k
      }
295
721k
      dots[dotsLen++] = pts[i];
296
2.88M
      for (yy = 0; yy <= r; ++yy) {
297
2.16M
  y0 = (y + yy) % size;
298
2.16M
  y1 = (y - yy + size) % size;
299
8.66M
  for (xx = 0; xx <= r; ++xx) {
300
6.49M
    if (tmpl[yy*(r+1) + xx]) {
301
6.49M
      x0 = (x + xx) % size;
302
6.49M
      x1 = (x - xx + size) % size;
303
6.49M
      grid[(y0 << log2Size) + x0] = 1;
304
6.49M
      grid[(y0 << log2Size) + x1] = 1;
305
6.49M
      grid[(y1 << log2Size) + x0] = 1;
306
6.49M
      grid[(y1 << log2Size) + x1] = 1;
307
6.49M
    }
308
6.49M
  }
309
2.16M
      }
310
721k
    }
311
9.47M
  }
312
313
2.31k
  gfree(tmpl);
314
2.31k
  gfree(grid);
315
316
  // assign each cell to a dot, compute distance to center of dot
317
2.31k
  region = (int *)gmallocn(size * size, sizeof(int));
318
2.31k
  dist = (int *)gmallocn(size * size, sizeof(int));
319
150k
  for (y = 0; y < size; ++y) {
320
9.62M
    for (x = 0; x < size; ++x) {
321
9.47M
      iMin = 0;
322
9.47M
      dMin = distance(dots[0].x, dots[0].y, x, y);
323
2.95G
      for (i = 1; i < dotsLen; ++i) {
324
2.94G
  d = distance(dots[i].x, dots[i].y, x, y);
325
2.94G
  if (d < dMin) {
326
48.5M
    iMin = i;
327
48.5M
    dMin = d;
328
48.5M
  }
329
2.94G
      }
330
9.47M
      region[(y << log2Size) + x] = iMin;
331
9.47M
      dist[(y << log2Size) + x] = dMin;
332
9.47M
    }
333
148k
  }
334
335
  // compute threshold values
336
724k
  for (i = 0; i < dotsLen; ++i) {
337
721k
    n = 0;
338
46.9M
    for (y = 0; y < size; ++y) {
339
3.00G
      for (x = 0; x < size; ++x) {
340
2.95G
  if (region[(y << log2Size) + x] == i) {
341
9.47M
    pts[n].x = x;
342
9.47M
    pts[n].y = y;
343
9.47M
    pts[n].dist = distance(dots[i].x, dots[i].y, x, y);
344
9.47M
    ++n;
345
9.47M
  }
346
2.95G
      }
347
46.2M
    }
348
721k
#if HAVE_STD_SORT
349
721k
    std::sort(pts, pts + n, cmpDistancesFunctor());
350
#else
351
    qsort(pts, n, sizeof(SplashScreenPoint), &cmpDistances);
352
#endif
353
10.2M
    for (j = 0; j < n; ++j) {
354
      // map values in [0 .. n-1] --> [255 .. 1]
355
9.47M
      mat[(pts[j].y << log2Size) + pts[j].x] =
356
9.47M
    (Guchar)(255 - (254 * j) / (n - 1));
357
9.47M
    }
358
721k
  }
359
360
2.31k
  gfree(pts);
361
2.31k
  gfree(region);
362
2.31k
  gfree(dist);
363
364
2.31k
  gfree(dots);
365
2.31k
}
366
367
2.40M
SplashScreen::SplashScreen(SplashScreen *screen) {
368
2.40M
  size = screen->size;
369
2.40M
  sizeM1 = screen->sizeM1;
370
2.40M
  log2Size = screen->log2Size;
371
2.40M
  mat = (Guchar *)gmallocn(size * size, sizeof(Guchar));
372
2.40M
  memcpy(mat, screen->mat, size * size * sizeof(Guchar));
373
2.40M
  minVal = screen->minVal;
374
2.40M
  maxVal = screen->maxVal;
375
2.40M
}
376
377
2.51M
SplashScreen::~SplashScreen() {
378
2.51M
  gfree(mat);
379
2.51M
}