-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathscm-file.cpp
More file actions
608 lines (509 loc) · 19.2 KB
/
scm-file.cpp
File metadata and controls
608 lines (509 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
// Copyright (C) 2011-2012 Robert Kooima
//
// LIBSCM is free software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation; either version 2 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITH-
// OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include "util3d/math3d.h"
#include "scm-index.hpp"
#include "scm-cache.hpp"
#include "scm-file.hpp"
#include "scm-path.hpp"
#include "scm-log.hpp"
//------------------------------------------------------------------------------
/// Construct a file table entry
///
/// Open the TIFF briefly to determine its format and cache its meta-data.
///
/// @param name TIFF file name
/// @param path Fully resolved path and name of TIFF file
scm_file::scm_file(const std::string& name,
const std::string& path) :
name(name),
path(path),
needs(32),
active(true),
sampler(0),
w(256), h(256), c(1), b(8),
xv(0), xc(0),
ov(0), oc(0),
av(0), ac(0),
zv(0), zc(0)
{
// Attempt to find and load the located TIFF.
if (!path.empty())
{
if (TIFF *T = TIFFOpen(path.c_str(), "r"))
{
uint64 n = 0;
void *p = 0;
// Cache the image parameters.
TIFFGetField(T, TIFFTAG_IMAGEWIDTH, &w);
TIFFGetField(T, TIFFTAG_IMAGELENGTH, &h);
TIFFGetField(T, TIFFTAG_BITSPERSAMPLE, &b);
TIFFGetField(T, TIFFTAG_SAMPLESPERPIXEL, &c);
// Preload all metadata.
if (TIFFGetField(T, 0xFFB1, &n, &p))
{
if ((xv = (uint64 *) malloc(size_t(n) * sizeof (uint64))))
{
memcpy(xv, p, size_t(n) * sizeof (uint64));
xc = n;
}
}
if (TIFFGetField(T, 0xFFB2, &n, &p))
{
if ((ov = (uint64 *) malloc(size_t(n) * sizeof (uint64))))
{
memcpy(ov, p, size_t(n) * sizeof (uint64));
oc = n;
}
}
if (TIFFGetField(T, 0xFFB3, &n, &p))
{
if ((av = malloc(size_t(n) * size_t(b) / 8)))
{
memcpy(av, p, size_t(n) * size_t(b) / 8);
ac = n;
}
}
if (TIFFGetField(T, 0xFFB4, &n, &p))
{
if ((zv = malloc(size_t(n) * size_t(b) / 8)))
{
memcpy(zv, p, size_t(n) * size_t(b) / 8);
zc = n;
}
}
TIFFClose(T);
}
}
scm_log("scm_file constructor %s", path.c_str());
}
scm_file::~scm_file()
{
scm_log("scm_file destructor %s", path.c_str());
// If we are not already exiting, signal the intention to do so.
if (is_active()) deactivate();
// Await the exit of each loader.
int s = 0;
for (thread_i i = threads.begin(); i != threads.end(); ++i)
SDL_WaitThread(*i, &s);
// Release all resources.
if (sampler) delete sampler;
free(zv);
free(av);
free(ov);
free(xv);
}
//------------------------------------------------------------------------------
/// Launch all loader threads for this file.
void scm_file::activate(scm_cache *cache)
{
this->cache = cache;
// Launch the loader threads.
int loader(void *);
for (int i = 0; i < 2; ++i)
threads.push_back(SDL_CreateThread(loader, "scm-loader", this));
}
/// Command all loader threads to exit.
void scm_file::deactivate()
{
// Notify the loaders that they may disregard their tasks.
active.set(false);
// A non-empty queue ensures that each loader unblocks.
int t = 1;
for (thread_i i = threads.begin(); i != threads.end(); ++i, ++t)
{
scm_task junk(-t, -t);
needs.try_insert(junk);
}
}
/// Return true if loader threads are active on this file.
bool scm_file::is_active() const
{
return active.get();
}
/// Insert a new loader task into the needs queue.
bool scm_file::add_need(scm_task& task)
{
return needs.try_insert(task);
}
//------------------------------------------------------------------------------
// Determine whether page i is given by this file. If no catalog exists then
// return true to indicate that data will be synthesized.
bool scm_file::get_page_status(uint64 i) const
{
if (xc)
{
if (toindex(i) < xc)
return true;
else
return false;
}
return true;
}
// Seek page i in the page catalog and return its file offset. Return zero
// on failure. If this file is synthesizing data, return a non-zero value.
uint64 scm_file::get_page_offset(uint64 i) const
{
if (oc)
{
uint64 oj;
if ((oj = toindex(i)) < oc)
{
return ov[oj];
}
return 0;
}
return (uint64) (-1);
}
// Determine the min and max values of page i. Seek it in the page catalog and
// reference the corresponding page in the min and max caches. If page i is not
// represented, assume its parent provides a useful bound and iterate up.
void scm_file::get_page_bounds(uint64 i, float& r0, float& r1) const
{
if (ac && zc)
{
uint64 aj = (uint64) (-1);
uint64 zj = (uint64) (-1);
while (aj >= ac || zj >= zc)
{
uint64 j = toindex(i);
if (aj >= ac) aj = j;
if (zj >= zc) zj = j;
if (i < 6)
break;
else
i = scm_page_parent(i);
}
r0 = (aj < ac) ? tofloat(av, aj * c) : 1.f;
r1 = (zj < zc) ? tofloat(zv, zj * c) : 1.f;
}
else
{
r0 = 0.5f;
r1 = 0.5f;
}
}
// Sample this file along vector v using linear filtering.
float scm_file::get_page_sample(const double *v)
{
if (xc)
{
if (sampler == 0)
sampler = new scm_sample(this);
return sampler ? sampler->get(v) : 1.f;
}
return 0.5f;
}
//------------------------------------------------------------------------------
// Compare two uint64s, for use by bsearch and qsort.
static int xcmp(const void *p, const void *q)
{
const uint64 *a = (const uint64 *) p;
const uint64 *b = (const uint64 *) q;
if (a[0] < b[0]) return -1;
else if (a[0] > b[0]) return +1;
else return 0;
}
// Determine where SCM index i appears in the sorted index list xv. This will
// indicate where the file offset and extrema appear in ov, av, and zv.
uint64 scm_file::toindex(uint64 i) const
{
void *p;
if (xc)
{
if ((p = bsearch(&i, xv, size_t(xc), sizeof (uint64), xcmp)))
{
return (uint64) ((uint64 *) p - xv);
}
}
return (uint64) (-1);
}
// Return sample i of the given buffer as a float.
float scm_file::tofloat(const void *v, uint64 i) const
{
switch (b)
{
case 8: return ((unsigned char *) v)[i] / 255.f;
case 16: return ((unsigned short *) v)[i] / 65535.f;
case 32: return (( float *) v)[i];
default: return 0.f;
}
}
// Set sample i of the given buffer to a float.
void scm_file::fromfloat(const void *v, uint64 i, float f) const
{
switch (b)
{
case 8: ((unsigned char *) v)[i] = (unsigned char) (f * 255.f); break;
case 16: ((unsigned short *) v)[i] = (unsigned short) (f * 65535.f); break;
case 32: (( float *) v)[i] = ( float) (f ); break;
}
}
//------------------------------------------------------------------------------
/// Seek the deepest page at this location (x, y) of root page a. Return the
/// file offset of this page and convert (x, y) to local coordinates there.
uint64 scm_file::find_page(long long a, double& y, double& x) const
{
long long n = 1;
long long l = 1;
uint64 j = 0;
uint64 o = ov[a];
while ((j = toindex(scm_page_index(a, l, int(2 * n * y),
int(2 * n * x)))) < oc)
if (ov[j])
{
o = ov[j];
l = l + 1;
n = n * 2;
}
else break;
x = (x * n) - floor(x * n);
y = (y * n) - floor(y * n);
return o;
}
//------------------------------------------------------------------------------
// This defines an 8x8 bitmap font used to write text directly to images.
static const uint8 bitfont[96][8] = {
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x18, 0x00 }, // !
{ 0x6c, 0x6c, 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00 }, // "
{ 0x6c, 0x6c, 0xfe, 0x6c, 0xfe, 0x6c, 0x6c, 0x00 }, // #
{ 0x18, 0x7e, 0xc0, 0x7c, 0x06, 0xfc, 0x18, 0x00 }, // $
{ 0x00, 0xc6, 0xcc, 0x18, 0x30, 0x66, 0xc6, 0x00 }, // %
{ 0x38, 0x6c, 0x38, 0x76, 0xdc, 0xcc, 0x76, 0x00 }, // &
{ 0x30, 0x30, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00 }, // '
{ 0x0c, 0x18, 0x30, 0x30, 0x30, 0x18, 0x0c, 0x00 }, // (
{ 0x30, 0x18, 0x0c, 0x0c, 0x0c, 0x18, 0x30, 0x00 }, // )
{ 0x00, 0x66, 0x3c, 0xff, 0x3c, 0x66, 0x00, 0x00 }, // *
{ 0x00, 0x18, 0x18, 0x7e, 0x18, 0x18, 0x00, 0x00 }, // +
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x30 }, // ,
{ 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x00 }, // -
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x00 }, // .
{ 0x06, 0x0c, 0x18, 0x30, 0x60, 0xc0, 0x80, 0x00 }, // /
{ 0x7c, 0xce, 0xde, 0xf6, 0xe6, 0xc6, 0x7c, 0x00 }, // 0
{ 0x18, 0x38, 0x18, 0x18, 0x18, 0x18, 0x3c, 0x00 }, // 1
{ 0x7c, 0xc6, 0x06, 0x7c, 0xc0, 0xc0, 0xfe, 0x00 }, // 2
{ 0x7c, 0xc6, 0x06, 0x3c, 0x06, 0xc6, 0x7c, 0x00 }, // 3
{ 0x0c, 0xcc, 0xcc, 0xcc, 0xfe, 0x0c, 0x0c, 0x00 }, // 4
{ 0xfe, 0xc0, 0xfc, 0x06, 0x06, 0xc6, 0x7c, 0x00 }, // 5
{ 0x7c, 0xc0, 0xfc, 0xc6, 0xc6, 0xc6, 0x7c, 0x00 }, // 6
{ 0xfe, 0x06, 0x06, 0x0c, 0x18, 0x30, 0x30, 0x00 }, // 7
{ 0x7c, 0xc6, 0xc6, 0x7c, 0xc6, 0xc6, 0x7c, 0x00 }, // 8
{ 0x7c, 0xc6, 0xc6, 0x7e, 0x06, 0x06, 0x7c, 0x00 }, // 9
{ 0x00, 0x18, 0x18, 0x00, 0x00, 0x18, 0x18, 0x00 }, // :
{ 0x00, 0x18, 0x18, 0x00, 0x00, 0x18, 0x18, 0x30 }, // ;
{ 0x0c, 0x18, 0x30, 0x60, 0x30, 0x18, 0x0c, 0x00 }, // <
{ 0x00, 0x00, 0x7e, 0x00, 0x7e, 0x00, 0x00, 0x00 }, // =
{ 0x30, 0x18, 0x0c, 0x06, 0x0c, 0x18, 0x30, 0x00 }, // >
{ 0x3c, 0x66, 0x0c, 0x18, 0x18, 0x00, 0x18, 0x00 }, // ?
{ 0x7c, 0xc6, 0xde, 0xde, 0xde, 0xc0, 0x7e, 0x00 }, // @
{ 0x38, 0x6c, 0xc6, 0xc6, 0xfe, 0xc6, 0xc6, 0x00 }, // A
{ 0xfc, 0xc6, 0xc6, 0xfc, 0xc6, 0xc6, 0xfc, 0x00 }, // B
{ 0x7c, 0xc6, 0xc0, 0xc0, 0xc0, 0xc6, 0x7c, 0x00 }, // C
{ 0xf8, 0xcc, 0xc6, 0xc6, 0xc6, 0xcc, 0xf8, 0x00 }, // D
{ 0xfe, 0xc0, 0xc0, 0xf8, 0xc0, 0xc0, 0xfe, 0x00 }, // E
{ 0xfe, 0xc0, 0xc0, 0xf8, 0xc0, 0xc0, 0xc0, 0x00 }, // F
{ 0x7c, 0xc6, 0xc0, 0xce, 0xc6, 0xc6, 0x7c, 0x00 }, // G
{ 0xc6, 0xc6, 0xc6, 0xfe, 0xc6, 0xc6, 0xc6, 0x00 }, // H
{ 0x7e, 0x18, 0x18, 0x18, 0x18, 0x18, 0x7e, 0x00 }, // I
{ 0x06, 0x06, 0x06, 0x06, 0x06, 0xc6, 0x7c, 0x00 }, // J
{ 0xc6, 0xcc, 0xd8, 0xf0, 0xd8, 0xcc, 0xc6, 0x00 }, // K
{ 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xfe, 0x00 }, // L
{ 0xc6, 0xee, 0xfe, 0xfe, 0xd6, 0xc6, 0xc6, 0x00 }, // M
{ 0xc6, 0xe6, 0xf6, 0xde, 0xce, 0xc6, 0xc6, 0x00 }, // N
{ 0x7c, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0x7c, 0x00 }, // O
{ 0xfc, 0xc6, 0xc6, 0xfc, 0xc0, 0xc0, 0xc0, 0x00 }, // P
{ 0x7c, 0xc6, 0xc6, 0xc6, 0xd6, 0xde, 0x7c, 0x06 }, // Q
{ 0xfc, 0xc6, 0xc6, 0xfc, 0xd8, 0xcc, 0xc6, 0x00 }, // R
{ 0x7c, 0xc6, 0xc0, 0x7c, 0x06, 0xc6, 0x7c, 0x00 }, // S
{ 0x7e, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00 }, // T
{ 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0x7c, 0x00 }, // U
{ 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0x6c, 0x38, 0x00 }, // V
{ 0xc6, 0xc6, 0xc6, 0xc6, 0xd6, 0xfe, 0x6c, 0x00 }, // W
{ 0xc6, 0xc6, 0x6c, 0x38, 0x6c, 0xc6, 0xc6, 0x00 }, // X
{ 0xc6, 0xc6, 0xc6, 0x7c, 0x18, 0x30, 0x60, 0x00 }, // Y
{ 0xfe, 0x0c, 0x18, 0x30, 0x60, 0xc0, 0xfe, 0x00 }, // Z
{ 0x3c, 0x30, 0x30, 0x30, 0x30, 0x30, 0x3c, 0x00 }, // [
{ 0xc0, 0x60, 0x30, 0x18, 0x0c, 0x06, 0x02, 0x00 }, //
{ 0x3c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x3c, 0x00 }, // ]
{ 0x10, 0x38, 0x6c, 0xc6, 0x00, 0x00, 0x00, 0x00 }, // ^
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff }, // _
{ 0x18, 0x18, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00 }, // `
{ 0x00, 0x00, 0x7c, 0x06, 0x7e, 0xc6, 0x7e, 0x00 }, // a
{ 0xc0, 0xc0, 0xfc, 0xc6, 0xc6, 0xc6, 0xfc, 0x00 }, // b
{ 0x00, 0x00, 0x7c, 0xc6, 0xc0, 0xc6, 0x7c, 0x00 }, // c
{ 0x06, 0x06, 0x7e, 0xc6, 0xc6, 0xc6, 0x7e, 0x00 }, // d
{ 0x00, 0x00, 0x7c, 0xc6, 0xfe, 0xc0, 0x7c, 0x00 }, // e
{ 0x1c, 0x36, 0x30, 0x78, 0x30, 0x30, 0x30, 0x00 }, // f
{ 0x00, 0x00, 0x7e, 0xc6, 0xc6, 0x7e, 0x06, 0xfc }, // g
{ 0xc0, 0xc0, 0xfc, 0xc6, 0xc6, 0xc6, 0xc6, 0x00 }, // h
{ 0x18, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00 }, // i
{ 0x06, 0x00, 0x06, 0x06, 0x06, 0x06, 0xc6, 0x7c }, // j
{ 0xc0, 0xc0, 0xc6, 0xcc, 0xf8, 0xcc, 0xc6, 0x00 }, // k
{ 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00 }, // l
{ 0x00, 0x00, 0xcc, 0xfe, 0xd6, 0xd6, 0xd6, 0x00 }, // m
{ 0x00, 0x00, 0xfc, 0xc6, 0xc6, 0xc6, 0xc6, 0x00 }, // n
{ 0x00, 0x00, 0x7c, 0xc6, 0xc6, 0xc6, 0x7c, 0x00 }, // o
{ 0x00, 0x00, 0xfc, 0xc6, 0xc6, 0xfc, 0xc0, 0xc0 }, // p
{ 0x00, 0x00, 0x7e, 0xc6, 0xc6, 0x7e, 0x06, 0x06 }, // q
{ 0x00, 0x00, 0xfc, 0xc6, 0xc0, 0xc0, 0xc0, 0x00 }, // r
{ 0x00, 0x00, 0x7e, 0xc0, 0x7c, 0x06, 0xfc, 0x00 }, // s
{ 0x18, 0x18, 0x7e, 0x18, 0x18, 0x18, 0x0e, 0x00 }, // t
{ 0x00, 0x00, 0xc6, 0xc6, 0xc6, 0xc6, 0x7e, 0x00 }, // u
{ 0x00, 0x00, 0xc6, 0xc6, 0xc6, 0x7c, 0x38, 0x00 }, // v
{ 0x00, 0x00, 0xc6, 0xc6, 0xd6, 0xfe, 0x6c, 0x00 }, // w
{ 0x00, 0x00, 0xc6, 0x6c, 0x38, 0x6c, 0xc6, 0x00 }, // x
{ 0x00, 0x00, 0xc6, 0xc6, 0xc6, 0x7e, 0x06, 0xfc }, // y
{ 0x00, 0x00, 0xfe, 0x0c, 0x38, 0x60, 0xfe, 0x00 }, // z
{ 0x0e, 0x18, 0x18, 0x70, 0x18, 0x18, 0x0e, 0x00 }, // {
{ 0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x18, 0x00 }, // |
{ 0x70, 0x18, 0x18, 0x0e, 0x18, 0x18, 0x70, 0x00 }, // }
{ 0x76, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // ~
{ 0x72, 0x6c, 0x6b, 0x40, 0x4C, 0x53, 0x55, 0x00 },
};
//------------------------------------------------------------------------------
static void set_pixel(float v, int x, int y, int w, int c, int b, void *p)
{
const int d = (y * w + x) * c;
for (int k = 0; k < c; ++k)
switch (b)
{
case 8: ((uint8 *) p)[d + k] = (uint8 ) (v * 255); break;
case 16: ((uint16 *) p)[d + k] = (uint16) (v * 65535); break;
case 32: ((float *) p)[d + k] = (float ) (v * 1); break;
}
}
static void set_text(const char *s, int x, int y,
int w, int h, int c, int b, void *p)
{
while (s[0])
{
for (int i = 0; i < 8; ++i)
for (int j = 0; j < 8; ++j)
if (32 <= s[0] && s[0] < 127 && 0 <= x + j && x + j < w
&& 0 <= y + i && y + i < h)
{
bool d = (bitfont[s[0] - 32][i] & (1 << (7 - j))) != 0;
set_pixel(d ? 1.f : 0.f, x + j, y + i, w, c, b, p);
set_pixel(d ? 1.f : 0.f, x + j, h - y - i, w, c, b, p);
}
s += 1;
x += 8;
}
}
//------------------------------------------------------------------------------
/// Write a message with diagnostics to the given image buffer.
void scm_page_text(const char *mesg,
const char *name,
long long i, int w, int h, int c, int b, void *p)
{
char diag[256];
// Fill the image with black.
memset(p, 0, w * h * c * b / 8);
// Fill the borders with white.
for (int j = 0; j < h; ++j)
{
set_pixel(1.f, 0, j, w, c, b, p);
set_pixel(1.f, 1, j, w, c, b, p);
set_pixel(1.f, w - 1, j, w, c, b, p);
set_pixel(1.f, w - 2, j, w, c, b, p);
set_pixel(1.f, j, 0, w, c, b, p);
set_pixel(1.f, j, 1, w, c, b, p);
set_pixel(1.f, j, h - 1, w, c, b, p);
set_pixel(1.f, j, h - 2, w, c, b, p);
}
// Draw the text. Unsafe under Windows due to shoddy standards compliance.
#ifdef WIN32
sprintf(diag, "%d %d %d %d %d", int(i), w, h, c, b);
#else
snprintf(diag, 256, "%d %d %d %d %d", int(i), w, h, c, b);
#endif
int msz = strlen(mesg);
int nsz = strlen(name);
int dsz = strlen(diag);
set_text(mesg, w / 2 - msz * 4, h / 3 - 14, w, h, c, b, p);
set_text(name, w / 2 - nsz * 4, h / 3 - 4, w, h, c, b, p);
set_text(diag, w / 2 - dsz * 4, h / 3 + 6, w, h, c, b, p);
}
/// Load a page from a TIFF file
///
/// Confirm the image parameters and return success.
/// @param name TIFF name
/// @param i Page index
/// @param T TIFF file
/// @param o TIFF offset
/// @param w Page width
/// @param h Page height
/// @param c Page channels per pixel
/// @param b Page bits per channel
/// @param p Destination pixel buffer
bool scm_load_page(const char *name, long long i,
TIFF *T, uint64 o, int w, int h, int c, int b, void *p)
{
if (T)
{
if (TIFFSetSubDirectory(T, o))
{
uint32 W, H;
uint16 C, B;
TIFFGetField(T, TIFFTAG_IMAGEWIDTH, &W);
TIFFGetField(T, TIFFTAG_IMAGELENGTH, &H);
TIFFGetField(T, TIFFTAG_BITSPERSAMPLE, &B);
TIFFGetField(T, TIFFTAG_SAMPLESPERPIXEL, &C);
if (int(W) == w && int(H) == h && int(B) == b && int(C) == c)
{
tsize_t N = TIFFNumberOfStrips(T);
tsize_t S = TIFFStripSize(T);
for (int l = 0; l < N; ++l)
{
if (TIFFReadEncodedStrip(T, l, (uint8 *) p + l * S, -1) == -1)
{
scm_page_text("Page read failure", name, i, W, H, C, B, p);
break;
}
}
}
else scm_page_text("Bad page format", name, i, W, H, C, B, p);
}
else scm_page_text("Page not found", name, i, w, h, c, b, p);
}
else scm_page_text("File not found", name, i, w, h, c, b, p);
return true;
}
/// Service page load requests
///
/// This function is the entry point for loader threads. The void data pointer
/// gives an scm_file structure with pointers to a needs queue and a cache with
/// a loads queue. An invalid file request represents an order to shut down.
int loader(void *data)
{
scm_file *file = (scm_file *) data;
scm_task task;
scm_log("loader thread begin %s", file->path.c_str());
{
const char *name = file->path.c_str();
TIFF *tiff = TIFFOpen(name, "r");
while ((task = file->needs.remove()).f >= 0)
if (file->is_active())
{
task.load_page(name, tiff);
file->cache->add_load(task);
}
else break;
if (tiff) TIFFClose(tiff);
}
scm_log("loader thread end %s", file->path.c_str());
return 0;
}