/
convert.c
464 lines (390 loc) · 13.2 KB
/
convert.c
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
// GIGO Copyright (C) 2012 Robert Kooima
//
// This program 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 3 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 <getopt.h>
#include <tiffio.h>
#include <stdlib.h>
#include <stdio.h>
#include "img.h"
#include "err.h"
#include "etc.h"
#include "icc.h"
//------------------------------------------------------------------------------
// Test the file extension of the given argument to determine if it's a TIFF.
static bool istif(const char *arg)
{
return isext(arg, ".tif")
|| isext(arg, ".TIF")
|| isext(arg, ".tiff")
|| isext(arg, ".TIFF");
}
// Open a new source TIFF file. Check for a compatible format.
static TIFF *tifopenr(const char *tif, // TIFF file name
bool *c, // return is complex?
int *l, // return log2 tile size
int *n, // return log2 height
int *m, // return log2 width
int *p) // return pixel size
{
TIFF *T;
if ((T = TIFFOpen(tif, "r")))
{
uint32 W = 0;
uint32 L = 0;
uint32 S = 0;
uint16 P = 0;
uint16 B = 0;
uint16 G = 0;
uint16 F = 0;
TIFFGetField(T, TIFFTAG_IMAGEWIDTH, &W);
TIFFGetField(T, TIFFTAG_IMAGELENGTH, &L);
TIFFGetField(T, TIFFTAG_TILEWIDTH, &S);
TIFFGetField(T, TIFFTAG_SAMPLESPERPIXEL, &P);
TIFFGetField(T, TIFFTAG_BITSPERSAMPLE, &B);
TIFFGetField(T, TIFFTAG_PLANARCONFIG, &G);
TIFFGetField(T, TIFFTAG_SAMPLEFORMAT, &F);
if (G == PLANARCONFIG_CONTIG)
{
if (F == SAMPLEFORMAT_IEEEFP)
{
if (B == 32)
{
if (ispow2(W) && ispow2(L))
{
*c = (P % 2) ? false : true;
*p = (P % 2) ? P : P / 2;
*l = log2i(S);
*n = log2i(L);
*m = log2i(W);
return T;
}
else apperr("TIFF image size must be power of 2");
}
else apperr("TIFF must have 32 bits per sample");
}
else apperr("TIFF must have floating point sample format");
}
else apperr("TIFF must have contiguous planar configuration");
TIFFClose(T);
}
return 0;
}
// Open a new destination TIFF file.
static TIFF *tifopenw(const char *tif, // TIFF file name
bool c, // is complex?
int n, // log2 height
int m, // log2 width
int p) // pixel size
{
TIFF *T;
if ((T = TIFFOpen(tif, "w")))
{
TIFFSetField(T, TIFFTAG_IMAGEWIDTH, 1 << m);
TIFFSetField(T, TIFFTAG_IMAGELENGTH, 1 << n);
TIFFSetField(T, TIFFTAG_SAMPLESPERPIXEL, c ? 2 * p : p);
TIFFSetField(T, TIFFTAG_BITSPERSAMPLE, 32);
TIFFSetField(T, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
TIFFSetField(T, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(T, TIFFTAG_COMPRESSION, COMPRESSION_ADOBE_DEFLATE);
if (p == 1)
{
TIFFSetField(T, TIFFTAG_ICCPROFILE, sizeof (grayicc), grayicc);
TIFFSetField(T, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
}
if (p == 3)
{
TIFFSetField(T, TIFFTAG_ICCPROFILE, sizeof (srgbicc), srgbicc);
TIFFSetField(T, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
}
}
return T;
}
//------------------------------------------------------------------------------
static inline void ctor(float *dst, const float complex *src, int c)
{
for (int k = 0; k < c; k++)
dst[k ] = cabsf(src[k]);
}
static inline void ctop(float *dst, const float complex *src, int c)
{
for (int k = 0; k < c; k++)
{
dst[k ] = cabsf(src[k]);
dst[k + c] = cargf(src[k]);
}
}
static inline void rtoc(float complex *dst, const float *src, int c)
{
for (int k = 0; k < c; k++)
dst[k] = src[k];
}
static inline void ptoc(float complex *dst, const float *src, int c)
{
for (int k = 0; k < c; k++)
dst[k] = src[k] * cisf(src[k + c]);
}
//------------------------------------------------------------------------------
// Copy one tile of real TIFF data into a complex image cache.
static void tiletoimgr(int y, // destination row
int x, // destination column
img *d, // destination image
int s, // source tile size
const float *p) // source buffer
{
int i = 0;
for (int r = y; r < y + s; r++)
for (int c = x; c < x + s; c++, i++)
rtoc(imgz(d, r, c), p + d->p * i, d->p);
}
// Copy one scanline of real TIFF data into a complex image cache.
static void linetoimgr(int r, // destination row
img *d, // destination image
const float *p) // source buffer
{
for (int c = 0; c < 1 << d->m; c++)
rtoc(imgz(d, r, c), p + d->p * c, d->p);
}
// Copy one scanline of real TIFF data from a complex image cache.
static void imgtoliner(int r, // source row
img *d, // source image
float *p) // destination buffer
{
for (int c = 0; c < 1 << d->m; c++)
ctor(p + d->p * c, imgz(d, r, c), d->p);
}
//------------------------------------------------------------------------------
// Copy one tile of complex TIFF data into a complex image cache.
static void tiletoimgz(int y, // destination row
int x, // destination column
img *d, // destination image
int s, // source tile size
const float *p) // source buffer
{
int i = 0;
for (int r = y; r < y + s; r++)
for (int c = x; c < x + s; c++, i++)
ptoc(imgz(d, r, c), p + 2 * d->p * i, d->p);
}
// Copy one scanline of complex TIFF data into a complex image cache.
static void linetoimgz(int r, // destination row
img *d, // destination image
const float *p) // source buffer
{
for (int c = 0; c < 1 << d->m; c++)
ptoc(imgz(d, r, c), p + 2 * d->p * c, d->p);
}
// Copy one scanline of complex TIFF data from a complex image cache.
static void imgtolinez(int r, // source row
img *d, // source image
float *p) // destination buffer
{
for (int c = 0; c < 1 << d->m; c++)
ctop(p + 2 * d->p * c, imgz(d, r, c), d->p);
}
//------------------------------------------------------------------------------
static void reverse(float *p, int c, int n)
{
int j = n - 1;
for (int i = 0; i < j; i++, j--)
for (int k = 0; k < c; k++)
{
float temp = p[i * c + k];
p[i * c + k] = p[j * c + k];
p[j * c + k] = temp;
}
}
// Copy a scanline-based TIFF to an image cache.
static bool scantoimg(img *d, TIFF *T, bool c, bool e)
{
int n = 1 << (d->n - e);
int m = 1 << d->m;
int r = 0;
float *p;
if ((p = (float *) malloc(TIFFScanlineSize(T))))
{
for (r = 0; r < n; r++)
{
if (TIFFReadScanline(T, p, r, 0) == -1)
return false;
if (c) linetoimgz(r, d, p);
else linetoimgr(r, d, p);
if (e)
{
reverse(p, c ? d->p * 2 : d->p, m);
if (c) linetoimgz(2 * n - r - 1, d, p);
else linetoimgr(2 * n - r - 1, d, p);
}
}
free(p);
}
return (r == n);
}
// Copy a tile-based TIFF to an image cache.
static bool tiletoimg(img *d, TIFF *T, bool c, int e, int k)
{
int n = 1 << (d->n - e);
int m = 1 << d->m;
int s = 1 << k;
int y = 0;
int x = 0;
float *p;
if ((p = (float *) malloc(TIFFTileSize(T))))
{
for (y = 0; y < n; y += s)
for (x = 0; x < m; x += s)
{
if (TIFFReadTile(T, p, x, y, 0, 0) == -1)
return false;
if (c) tiletoimgz(y, x, d, s, p);
else tiletoimgr(y, x, d, s, p);
if (e)
{
reverse(p, c ? d->p * 2 : d->p, s * s);
if (c) tiletoimgz(2 * n - y - s, m - x - s, d, s, p);
else tiletoimgr(2 * n - y - s, m - x - s, d, s, p);
}
}
free(p);
}
return (y == n && x == m);
}
//------------------------------------------------------------------------------
// Convert a TIFF to an image cache file.
static bool tiftoimg(bool v, // verbose?
int e, // destination is extended?
int l, // log2 tile size
const char *tif, // source TIFF image file name
const char *bin) // destination image cache file name
{
bool ok = false;
TIFF *T;
img *d;
bool c;
int k = 0;
int n = 0;
int m = 0;
int p = 0;
if ((T = tifopenr(tif, &c, &k, &n, &m, &p)))
{
if (imginit(bin, l, n + e, m, p, 0))
{
if ((d = imgopen(bin, l, n + e, m, p)))
{
if (k)
ok = tiletoimg(d, T, c, e, k);
else
ok = scantoimg(d, T, c, e);
imgclose(d);
}
}
TIFFClose(T);
}
if (v) printf("%s %d %d %d %d\n", bin, l, n + e, m, p);
return ok;
}
// Convert an image cache file to a TIFF.
static bool imgtotif(bool c, // destination is complex?
int e, // source is extended?
int l, // source log2 tile size
int n, // source log2 height
int m, // source log2 width
int p, // source pixel size
const char *bin, // source image cache file name
const char *tif) // destination TIFF image file name
{
img *d;
TIFF *T;
void *buf;
int r = 0;
if ((n && m && p) || imgargs(bin, &n, &m, &p))
{
if ((d = imgopen(bin, l, n, m, p)))
{
if ((T = tifopenw(tif, c, n - e, m, p)))
{
if ((buf = malloc(TIFFScanlineSize(T))))
{
for (r = 0; r < 1 << (n - e); r++)
{
if (c)
imgtolinez(r, d, (float *) buf);
else
imgtoliner(r, d, (float *) buf);
if (TIFFWriteScanline(T, buf, r, 0) == -1)
break;
}
free(buf);
}
TIFFClose(T);
}
imgclose(d);
}
}
else apperr("Failed to guess image parameters", bin);
return (r == 1 << (n - e));
}
//------------------------------------------------------------------------------
static int usage(const char *exe)
{
fprintf(stderr, "Usage:\t%s [-tve] input.tif output.bin\n", exe);
fprintf(stderr, "\t%s [-tr] "
"[-l size] "
"[-n height] "
"[-m width] "
"[-p samples] input.bin output.tif\n", exe);
return EXIT_FAILURE;
}
int main(int argc, char **argv)
{
bool ok = false;
bool t = false;
bool c = true;
bool v = false;
int l = 5;
int n = 0;
int m = 0;
int p = 0;
int e = 0;
int o;
// Parse the command line options.
while ((o = getopt(argc, argv, "l:n:m:p:terv")) != -1)
switch (o)
{
case 't': t = true; break;
case 'r': c = false; break;
case 'v': v = true; break;
case 'l': l = strtol(optarg, 0, 0); break;
case 'n': n = strtol(optarg, 0, 0); break;
case 'm': m = strtol(optarg, 0, 0); break;
case 'p': p = strtol(optarg, 0, 0); break;
case 'e': e = 1; break;
case '?':
default : return usage(argv[0]);
}
// Confirm the arguments and run the process.
setexe(argv[0]);
struct timeval t0;
struct timeval t1;
gettimeofday(&t0, 0);
{
if (optind + 2 == argc)
{
if (istif(argv[optind]))
ok = tiftoimg(v, e, l, argv[optind], argv[optind + 1]);
else
ok = imgtotif(c, e, l, n, m, p, argv[optind], argv[optind + 1]);
}
else return usage(argv[0]);
}
gettimeofday(&t1, 0);
if (t) printtime(&t0, &t1);
return ok ? EXIT_SUCCESS : EXIT_FAILURE;
}