| 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 |
bool operator()(const SplashScreenPoint &p0, const SplashScreenPoint &p1) { |
| 43 |
return p0.dist < p1.dist; |
| 44 |
} |
| 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 |
SplashScreen::SplashScreen(SplashScreenParams *params) { |
| 65 |
Guchar u; |
| 66 |
int black, white, i; |
| 67 |
|
| 68 |
if (!params) { |
| 69 |
params = &defaultParams; |
| 70 |
} |
| 71 |
|
| 72 |
// size must be a power of 2, and at least 2 |
| 73 |
for (size = 2, log2Size = 1; size < params->size; size <<= 1, ++log2Size) ; |
| 74 |
|
| 75 |
switch (params->type) { |
| 76 |
|
| 77 |
case splashScreenDispersed: |
| 78 |
mat = (Guchar *)gmallocn(size * size, sizeof(Guchar)); |
| 79 |
buildDispersedMatrix(size/2, size/2, 1, size/2, 1); |
| 80 |
break; |
| 81 |
|
| 82 |
case splashScreenClustered: |
| 83 |
mat = (Guchar *)gmallocn(size * size, sizeof(Guchar)); |
| 84 |
buildClusteredMatrix(); |
| 85 |
break; |
| 86 |
|
| 87 |
case splashScreenStochasticClustered: |
| 88 |
// size must be at least 2*r |
| 89 |
while (size < (params->dotRadius << 1)) { |
| 90 |
size <<= 1; |
| 91 |
++log2Size; |
| 92 |
} |
| 93 |
mat = (Guchar *)gmallocn(size * size, sizeof(Guchar)); |
| 94 |
buildSCDMatrix(params->dotRadius); |
| 95 |
break; |
| 96 |
} |
| 97 |
|
| 98 |
sizeM1 = size - 1; |
| 99 |
|
| 100 |
// do gamma correction and compute minVal/maxVal |
| 101 |
minVal = 255; |
| 102 |
maxVal = 0; |
| 103 |
black = splashRound((SplashCoord)255.0 * params->blackThreshold); |
| 104 |
if (black < 1) { |
| 105 |
black = 1; |
| 106 |
} |
| 107 |
white = splashRound((SplashCoord)255.0 * params->whiteThreshold); |
| 108 |
if (white > 255) { |
| 109 |
white = 255; |
| 110 |
} |
| 111 |
for (i = 0; i < size * size; ++i) { |
| 112 |
u = (Guchar)splashRound((SplashCoord)255.0 * |
| 113 |
splashPow((SplashCoord)mat[i] / 255.0, |
| 114 |
params->gamma)); |
| 115 |
if (u < black) { |
| 116 |
u = (Guchar)black; |
| 117 |
} else if (u >= white) { |
| 118 |
u = (Guchar)white; |
| 119 |
} |
| 120 |
mat[i] = u; |
| 121 |
if (u < minVal) { |
| 122 |
minVal = u; |
| 123 |
} else if (u > maxVal) { |
| 124 |
maxVal = u; |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
void SplashScreen::buildDispersedMatrix(int i, int j, int val, |
| 130 |
int delta, int offset) { |
| 131 |
if (delta == 0) { |
| 132 |
// map values in [1, size^2] --> [1, 255] |
| 133 |
mat[(i << log2Size) + j] = |
| 134 |
(Guchar)(1 + (254 * (val - 1)) / (size * size - 1)); |
| 135 |
} else { |
| 136 |
buildDispersedMatrix(i, j, |
| 137 |
val, delta / 2, 4*offset); |
| 138 |
buildDispersedMatrix((i + delta) % size, (j + delta) % size, |
| 139 |
val + offset, delta / 2, 4*offset); |
| 140 |
buildDispersedMatrix((i + delta) % size, j, |
| 141 |
val + 2*offset, delta / 2, 4*offset); |
| 142 |
buildDispersedMatrix((i + 2*delta) % size, (j + delta) % size, |
| 143 |
val + 3*offset, delta / 2, 4*offset); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
void SplashScreen::buildClusteredMatrix() { |
| 148 |
SplashCoord *dist; |
| 149 |
SplashCoord u, v, d; |
| 150 |
Guchar val; |
| 151 |
int size2, x, y, x1, y1, i; |
| 152 |
|
| 153 |
size2 = size >> 1; |
| 154 |
|
| 155 |
// initialize the threshold matrix |
| 156 |
for (y = 0; y < size; ++y) { |
| 157 |
for (x = 0; x < size; ++x) { |
| 158 |
mat[(y << log2Size) + x] = 0; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
// build the distance matrix |
| 163 |
dist = (SplashCoord *)gmallocn(size * size2, sizeof(SplashCoord)); |
| 164 |
for (y = 0; y < size2; ++y) { |
| 165 |
for (x = 0; x < size2; ++x) { |
| 166 |
if (x + y < size2 - 1) { |
| 167 |
u = (SplashCoord)x + 0.5 - 0; |
| 168 |
v = (SplashCoord)y + 0.5 - 0; |
| 169 |
} else { |
| 170 |
u = (SplashCoord)x + 0.5 - (SplashCoord)size2; |
| 171 |
v = (SplashCoord)y + 0.5 - (SplashCoord)size2; |
| 172 |
} |
| 173 |
dist[y * size2 + x] = u*u + v*v; |
| 174 |
} |
| 175 |
} |
| 176 |
for (y = 0; y < size2; ++y) { |
| 177 |
for (x = 0; x < size2; ++x) { |
| 178 |
if (x < y) { |
| 179 |
u = (SplashCoord)x + 0.5 - 0; |
| 180 |
v = (SplashCoord)y + 0.5 - (SplashCoord)size2; |
| 181 |
} else { |
| 182 |
u = (SplashCoord)x + 0.5 - (SplashCoord)size2; |
| 183 |
v = (SplashCoord)y + 0.5 - 0; |
| 184 |
} |
| 185 |
dist[(size2 + y) * size2 + x] = u*u + v*v; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
// build the threshold matrix |
| 190 |
x1 = y1 = 0; // make gcc happy |
| 191 |
for (i = 0; i < size * size2; ++i) { |
| 192 |
d = -1; |
| 193 |
for (y = 0; y < size; ++y) { |
| 194 |
for (x = 0; x < size2; ++x) { |
| 195 |
if (mat[(y << log2Size) + x] == 0 && |
| 196 |
dist[y * size2 + x] > d) { |
| 197 |
x1 = x; |
| 198 |
y1 = y; |
| 199 |
d = dist[y1 * size2 + x1]; |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
// map values in [0, 2*size*size2-1] --> [1, 255] |
| 204 |
val = (Guchar)(1 + (254 * (2*i)) / (2*size*size2 - 1)); |
| 205 |
mat[(y1 << log2Size) + x1] = val; |
| 206 |
val = (Guchar)(1 + (254 * (2*i+1)) / (2*size*size2 - 1)); |
| 207 |
if (y1 < size2) { |
| 208 |
mat[((y1 + size2) << log2Size) + x1 + size2] = val; |
| 209 |
} else { |
| 210 |
mat[((y1 - size2) << log2Size) + x1 + size2] = val; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
gfree(dist); |
| 215 |
} |
| 216 |
|
| 217 |
// Compute the distance between two points on a toroid. |
| 218 |
int SplashScreen::distance(int x0, int y0, int x1, int y1) { |
| 219 |
int dx0, dx1, dx, dy0, dy1, dy; |
| 220 |
|
| 221 |
dx0 = abs(x0 - x1); |
| 222 |
dx1 = size - dx0; |
| 223 |
dx = dx0 < dx1 ? dx0 : dx1; |
| 224 |
dy0 = abs(y0 - y1); |
| 225 |
dy1 = size - dy0; |
| 226 |
dy = dy0 < dy1 ? dy0 : dy1; |
| 227 |
return dx * dx + dy * dy; |
| 228 |
} |
| 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 |
void SplashScreen::buildSCDMatrix(int r) { |
| 235 |
SplashScreenPoint *dots, *pts; |
| 236 |
int dotsLen, dotsSize; |
| 237 |
char *tmpl; |
| 238 |
char *grid; |
| 239 |
int *region, *dist; |
| 240 |
int x, y, xx, yy, x0, x1, y0, y1, i, j, d, iMin, dMin, n; |
| 241 |
|
| 242 |
//~ this should probably happen somewhere else |
| 243 |
srand(123); |
| 244 |
|
| 245 |
// generate the random space-filling curve |
| 246 |
pts = (SplashScreenPoint *)gmallocn(size * size, sizeof(SplashScreenPoint)); |
| 247 |
i = 0; |
| 248 |
for (y = 0; y < size; ++y) { |
| 249 |
for (x = 0; x < size; ++x) { |
| 250 |
pts[i].x = x; |
| 251 |
pts[i].y = y; |
| 252 |
++i; |
| 253 |
} |
| 254 |
} |
| 255 |
for (i = 0; i < size * size; ++i) { |
| 256 |
j = i + (int)((double)(size * size - i) * |
| 257 |
(double)rand() / ((double)RAND_MAX + 1.0)); |
| 258 |
x = pts[i].x; |
| 259 |
y = pts[i].y; |
| 260 |
pts[i].x = pts[j].x; |
| 261 |
pts[i].y = pts[j].y; |
| 262 |
pts[j].x = x; |
| 263 |
pts[j].y = y; |
| 264 |
} |
| 265 |
|
| 266 |
// construct the circle template |
| 267 |
tmpl = (char *)gmallocn((r+1)*(r+1), sizeof(char)); |
| 268 |
for (y = 0; y <= r; ++y) { |
| 269 |
for (x = 0; x <= r; ++x) { |
| 270 |
tmpl[y*(r+1) + x] = (x * y <= r * r) ? 1 : 0; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
// mark all grid cells as free |
| 275 |
grid = (char *)gmallocn(size * size, sizeof(char)); |
| 276 |
for (y = 0; y < size; ++y) { |
| 277 |
for (x = 0; x < size; ++x) { |
| 278 |
grid[(y << log2Size) + x] = 0; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
// walk the space-filling curve, adding dots |
| 283 |
dotsLen = 0; |
| 284 |
dotsSize = 32; |
| 285 |
dots = (SplashScreenPoint *)gmallocn(dotsSize, sizeof(SplashScreenPoint)); |
| 286 |
for (i = 0; i < size * size; ++i) { |
| 287 |
x = pts[i].x; |
| 288 |
y = pts[i].y; |
| 289 |
if (!grid[(y << log2Size) + x]) { |
| 290 |
if (dotsLen == dotsSize) { |
| 291 |
dotsSize *= 2; |
| 292 |
dots = (SplashScreenPoint *)greallocn(dots, dotsSize, |
| 293 |
sizeof(SplashScreenPoint)); |
| 294 |
} |
| 295 |
dots[dotsLen++] = pts[i]; |
| 296 |
for (yy = 0; yy <= r; ++yy) { |
| 297 |
y0 = (y + yy) % size; |
| 298 |
y1 = (y - yy + size) % size; |
| 299 |
for (xx = 0; xx <= r; ++xx) { |
| 300 |
if (tmpl[yy*(r+1) + xx]) { |
| 301 |
x0 = (x + xx) % size; |
| 302 |
x1 = (x - xx + size) % size; |
| 303 |
grid[(y0 << log2Size) + x0] = 1; |
| 304 |
grid[(y0 << log2Size) + x1] = 1; |
| 305 |
grid[(y1 << log2Size) + x0] = 1; |
| 306 |
grid[(y1 << log2Size) + x1] = 1; |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
gfree(tmpl); |
| 314 |
gfree(grid); |
| 315 |
|
| 316 |
// assign each cell to a dot, compute distance to center of dot |
| 317 |
region = (int *)gmallocn(size * size, sizeof(int)); |
| 318 |
dist = (int *)gmallocn(size * size, sizeof(int)); |
| 319 |
for (y = 0; y < size; ++y) { |
| 320 |
for (x = 0; x < size; ++x) { |
| 321 |
iMin = 0; |
| 322 |
dMin = distance(dots[0].x, dots[0].y, x, y); |
| 323 |
for (i = 1; i < dotsLen; ++i) { |
| 324 |
d = distance(dots[i].x, dots[i].y, x, y); |
| 325 |
if (d < dMin) { |
| 326 |
iMin = i; |
| 327 |
dMin = d; |
| 328 |
} |
| 329 |
} |
| 330 |
region[(y << log2Size) + x] = iMin; |
| 331 |
dist[(y << log2Size) + x] = dMin; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
// compute threshold values |
| 336 |
for (i = 0; i < dotsLen; ++i) { |
| 337 |
n = 0; |
| 338 |
for (y = 0; y < size; ++y) { |
| 339 |
for (x = 0; x < size; ++x) { |
| 340 |
if (region[(y << log2Size) + x] == i) { |
| 341 |
pts[n].x = x; |
| 342 |
pts[n].y = y; |
| 343 |
pts[n].dist = distance(dots[i].x, dots[i].y, x, y); |
| 344 |
++n; |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
#if HAVE_STD_SORT |
| 349 |
std::sort(pts, pts + n, cmpDistancesFunctor()); |
| 350 |
#else |
| 351 |
qsort(pts, n, sizeof(SplashScreenPoint), &cmpDistances); |
| 352 |
#endif |
| 353 |
for (j = 0; j < n; ++j) { |
| 354 |
// map values in [0 .. n-1] --> [255 .. 1] |
| 355 |
mat[(pts[j].y << log2Size) + pts[j].x] = |
| 356 |
(Guchar)(255 - (254 * j) / (n - 1)); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
gfree(pts); |
| 361 |
gfree(region); |
| 362 |
gfree(dist); |
| 363 |
|
| 364 |
gfree(dots); |
| 365 |
} |
| 366 |
|
| 367 |
SplashScreen::SplashScreen(SplashScreen *screen) { |
| 368 |
size = screen->size; |
| 369 |
sizeM1 = screen->sizeM1; |
| 370 |
log2Size = screen->log2Size; |
| 371 |
mat = (Guchar *)gmallocn(size * size, sizeof(Guchar)); |
| 372 |
memcpy(mat, screen->mat, size * size * sizeof(Guchar)); |
| 373 |
minVal = screen->minVal; |
| 374 |
maxVal = screen->maxVal; |
| 375 |
} |
| 376 |
|
| 377 |
SplashScreen::~SplashScreen() { |
| 378 |
gfree(mat); |
| 379 |
} |