Skip to content

Latest commit

 

History

History
537 lines (503 loc) · 14.8 KB

tiff2bw.c

File metadata and controls

537 lines (503 loc) · 14.8 KB
 
Oct 22, 2017
Oct 22, 2017
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
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that (i) the above copyright notices and this permission notice appear in
* all copies of the software and related documentation, and (ii) the names of
* Sam Leffler and Silicon Graphics may not be used in any advertising or
* publicity relating to the software without the specific, prior written
* permission of Sam Leffler and Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#include "tif_config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef NEED_LIBPORT
# include "libport.h"
#endif
#include "tiffio.h"
Nov 6, 2019
Nov 6, 2019
41
#include "tiffiop.h"
Oct 22, 2017
Oct 22, 2017
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
/* x% weighting -> fraction of full color */
#define PCT(x) (((x)*256+50)/100)
int RED = PCT(30); /* 30% */
int GREEN = PCT(59); /* 59% */
int BLUE = PCT(11); /* 11% */
static void usage(void);
static int processCompressOptions(char*);
static void
compresscontig(unsigned char* out, unsigned char* rgb, uint32 n)
{
register int v, red = RED, green = GREEN, blue = BLUE;
while (n-- > 0) {
v = red*(*rgb++);
v += green*(*rgb++);
v += blue*(*rgb++);
*out++ = v>>8;
}
}
static void
compresssep(unsigned char* out,
unsigned char* r, unsigned char* g, unsigned char* b, uint32 n)
{
register uint32 red = RED, green = GREEN, blue = BLUE;
while (n-- > 0)
*out++ = (unsigned char)
((red*(*r++) + green*(*g++) + blue*(*b++)) >> 8);
}
static int
checkcmap(TIFF* tif, int n, uint16* r, uint16* g, uint16* b)
{
while (n-- > 0)
if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
return (16);
TIFFWarning(TIFFFileName(tif), "Assuming 8-bit colormap");
return (8);
}
static void
compresspalette(unsigned char* out, unsigned char* data, uint32 n, uint16* rmap, uint16* gmap, uint16* bmap)
{
register int v, red = RED, green = GREEN, blue = BLUE;
while (n-- > 0) {
unsigned int ix = *data++;
v = red*rmap[ix];
v += green*gmap[ix];
v += blue*bmap[ix];
*out++ = v>>8;
}
}
static uint16 compression = (uint16) -1;
static uint16 predictor = 0;
static int jpegcolormode = JPEGCOLORMODE_RGB;
static int quality = 75; /* JPEG quality */
static void cpTags(TIFF* in, TIFF* out);
int
main(int argc, char* argv[])
{
uint32 rowsperstrip = (uint32) -1;
TIFF *in, *out;
uint32 w, h;
uint16 samplesperpixel;
uint16 bitspersample;
uint16 config;
uint16 photometric;
uint16* red;
uint16* green;
uint16* blue;
tsize_t rowsize;
register uint32 row;
register tsample_t s;
unsigned char *inbuf, *outbuf;
char thing[1024];
int c;
#if !HAVE_DECL_OPTARG
extern int optind;
extern char *optarg;
#endif
Nov 4, 2018
Nov 4, 2018
130
131
132
133
134
in = (TIFF *) NULL;
out = (TIFF *) NULL;
inbuf = (unsigned char *) NULL;
outbuf = (unsigned char *) NULL;
Oct 22, 2017
Oct 22, 2017
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
while ((c = getopt(argc, argv, "c:r:R:G:B:")) != -1)
switch (c) {
case 'c': /* compression scheme */
if (!processCompressOptions(optarg))
usage();
break;
case 'r': /* rows/strip */
rowsperstrip = atoi(optarg);
break;
case 'R':
RED = PCT(atoi(optarg));
break;
case 'G':
GREEN = PCT(atoi(optarg));
break;
case 'B':
BLUE = PCT(atoi(optarg));
break;
case '?':
usage();
/*NOTREACHED*/
}
if (argc - optind < 2)
usage();
in = TIFFOpen(argv[optind], "r");
if (in == NULL)
return (-1);
photometric = 0;
TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &photometric);
if (photometric != PHOTOMETRIC_RGB && photometric != PHOTOMETRIC_PALETTE ) {
fprintf(stderr,
"%s: Bad photometric; can only handle RGB and Palette images.\n",
argv[optind]);
Nov 4, 2018
Nov 4, 2018
169
goto tiff2bw_error;
Oct 22, 2017
Oct 22, 2017
170
171
172
173
174
}
TIFFGetField(in, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
if (samplesperpixel != 1 && samplesperpixel != 3) {
fprintf(stderr, "%s: Bad samples/pixel %u.\n",
argv[optind], samplesperpixel);
Nov 4, 2018
Nov 4, 2018
175
goto tiff2bw_error;
Oct 22, 2017
Oct 22, 2017
176
177
178
179
}
if( photometric == PHOTOMETRIC_RGB && samplesperpixel != 3) {
fprintf(stderr, "%s: Bad samples/pixel %u for PHOTOMETRIC_RGB.\n",
argv[optind], samplesperpixel);
Nov 4, 2018
Nov 4, 2018
180
goto tiff2bw_error;
Oct 22, 2017
Oct 22, 2017
181
182
183
184
185
}
TIFFGetField(in, TIFFTAG_BITSPERSAMPLE, &bitspersample);
if (bitspersample != 8) {
fprintf(stderr,
" %s: Sorry, only handle 8-bit samples.\n", argv[optind]);
Nov 4, 2018
Nov 4, 2018
186
goto tiff2bw_error;
Oct 22, 2017
Oct 22, 2017
187
188
189
190
191
192
193
194
}
TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &w);
TIFFGetField(in, TIFFTAG_IMAGELENGTH, &h);
TIFFGetField(in, TIFFTAG_PLANARCONFIG, &config);
out = TIFFOpen(argv[optind+1], "w");
if (out == NULL)
{
Nov 4, 2018
Nov 4, 2018
195
goto tiff2bw_error;
Oct 22, 2017
Oct 22, 2017
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
}
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, w);
TIFFSetField(out, TIFFTAG_IMAGELENGTH, h);
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 1);
TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
cpTags(in, out);
if (compression != (uint16) -1) {
TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
switch (compression) {
case COMPRESSION_JPEG:
TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality);
TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode);
break;
case COMPRESSION_LZW:
case COMPRESSION_DEFLATE:
if (predictor != 0)
TIFFSetField(out, TIFFTAG_PREDICTOR, predictor);
break;
}
}
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
snprintf(thing, sizeof(thing), "B&W version of %s", argv[optind]);
TIFFSetField(out, TIFFTAG_IMAGEDESCRIPTION, thing);
TIFFSetField(out, TIFFTAG_SOFTWARE, "tiff2bw");
outbuf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(out));
Nov 6, 2019
Nov 6, 2019
222
223
224
225
226
if( !outbuf )
{
fprintf(stderr, "Out of memory\n");
goto tiff2bw_error;
}
Oct 22, 2017
Oct 22, 2017
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
TIFFDefaultStripSize(out, rowsperstrip));
#define pack(a,b) ((a)<<8 | (b))
switch (pack(photometric, config)) {
case pack(PHOTOMETRIC_PALETTE, PLANARCONFIG_CONTIG):
case pack(PHOTOMETRIC_PALETTE, PLANARCONFIG_SEPARATE):
TIFFGetField(in, TIFFTAG_COLORMAP, &red, &green, &blue);
/*
* Convert 16-bit colormap to 8-bit (unless it looks
* like an old-style 8-bit colormap).
*/
if (checkcmap(in, 1<<bitspersample, red, green, blue) == 16) {
int i;
#define CVT(x) (((x) * 255L) / ((1L<<16)-1))
for (i = (1<<bitspersample)-1; i >= 0; i--) {
red[i] = CVT(red[i]);
green[i] = CVT(green[i]);
blue[i] = CVT(blue[i]);
}
#undef CVT
}
inbuf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(in));
Nov 6, 2019
Nov 6, 2019
250
251
252
253
254
if( !inbuf )
{
fprintf(stderr, "Out of memory\n");
goto tiff2bw_error;
}
Oct 22, 2017
Oct 22, 2017
255
256
257
258
259
260
261
262
263
264
for (row = 0; row < h; row++) {
if (TIFFReadScanline(in, inbuf, row, 0) < 0)
break;
compresspalette(outbuf, inbuf, w, red, green, blue);
if (TIFFWriteScanline(out, outbuf, row, 0) < 0)
break;
}
break;
case pack(PHOTOMETRIC_RGB, PLANARCONFIG_CONTIG):
inbuf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(in));
Nov 6, 2019
Nov 6, 2019
265
266
267
268
269
if( !inbuf )
{
fprintf(stderr, "Out of memory\n");
goto tiff2bw_error;
}
Oct 22, 2017
Oct 22, 2017
270
271
272
273
274
275
276
277
278
for (row = 0; row < h; row++) {
if (TIFFReadScanline(in, inbuf, row, 0) < 0)
break;
compresscontig(outbuf, inbuf, w);
if (TIFFWriteScanline(out, outbuf, row, 0) < 0)
break;
}
break;
case pack(PHOTOMETRIC_RGB, PLANARCONFIG_SEPARATE):
Nov 6, 2019
Nov 6, 2019
279
280
{
tmsize_t inbufsize;
Oct 22, 2017
Oct 22, 2017
281
rowsize = TIFFScanlineSize(in);
Nov 6, 2019
Nov 6, 2019
282
283
284
285
286
287
288
inbufsize = TIFFSafeMultiply(tmsize_t, 3, rowsize);
inbuf = (unsigned char *)_TIFFmalloc(inbufsize);
if( !inbuf )
{
fprintf(stderr, "Out of memory\n");
goto tiff2bw_error;
}
Oct 22, 2017
Oct 22, 2017
289
290
291
292
for (row = 0; row < h; row++) {
for (s = 0; s < 3; s++)
if (TIFFReadScanline(in,
inbuf+s*rowsize, row, s) < 0)
Nov 4, 2018
Nov 4, 2018
293
goto tiff2bw_error;
Oct 22, 2017
Oct 22, 2017
294
295
296
297
298
299
compresssep(outbuf,
inbuf, inbuf+rowsize, inbuf+2*rowsize, w);
if (TIFFWriteScanline(out, outbuf, row, 0) < 0)
break;
}
break;
Nov 6, 2019
Nov 6, 2019
300
}
Oct 22, 2017
Oct 22, 2017
301
302
}
#undef pack
Nov 4, 2018
Nov 4, 2018
303
304
305
306
307
if (inbuf)
_TIFFfree(inbuf);
if (outbuf)
_TIFFfree(outbuf);
TIFFClose(in);
Oct 22, 2017
Oct 22, 2017
308
309
TIFFClose(out);
return (0);
Nov 4, 2018
Nov 4, 2018
310
311
312
313
314
315
316
317
318
319
320
tiff2bw_error:
if (inbuf)
_TIFFfree(inbuf);
if (outbuf)
_TIFFfree(outbuf);
if (out)
TIFFClose(out);
if (in)
TIFFClose(in);
return (-1);
Oct 22, 2017
Oct 22, 2017
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
}
static int
processCompressOptions(char* opt)
{
if (streq(opt, "none"))
compression = COMPRESSION_NONE;
else if (streq(opt, "packbits"))
compression = COMPRESSION_PACKBITS;
else if (strneq(opt, "jpeg", 4)) {
char* cp = strchr(opt, ':');
compression = COMPRESSION_JPEG;
while( cp )
{
if (isdigit((int)cp[1]))
quality = atoi(cp+1);
else if (cp[1] == 'r' )
jpegcolormode = JPEGCOLORMODE_RAW;
else
usage();
cp = strchr(cp+1,':');
}
} else if (strneq(opt, "lzw", 3)) {
char* cp = strchr(opt, ':');
if (cp)
predictor = atoi(cp+1);
compression = COMPRESSION_LZW;
} else if (strneq(opt, "zip", 3)) {
char* cp = strchr(opt, ':');
if (cp)
predictor = atoi(cp+1);
compression = COMPRESSION_DEFLATE;
} else
return (0);
return (1);
}
#define CopyField(tag, v) \
if (TIFFGetField(in, tag, &v)) TIFFSetField(out, tag, v)
#define CopyField2(tag, v1, v2) \
if (TIFFGetField(in, tag, &v1, &v2)) TIFFSetField(out, tag, v1, v2)
#define CopyField3(tag, v1, v2, v3) \
if (TIFFGetField(in, tag, &v1, &v2, &v3)) TIFFSetField(out, tag, v1, v2, v3)
#define CopyField4(tag, v1, v2, v3, v4) \
if (TIFFGetField(in, tag, &v1, &v2, &v3, &v4)) TIFFSetField(out, tag, v1, v2, v3, v4)
static void
cpTag(TIFF* in, TIFF* out, uint16 tag, uint16 count, TIFFDataType type)
{
switch (type) {
case TIFF_SHORT:
if (count == 1) {
uint16 shortv;
CopyField(tag, shortv);
} else if (count == 2) {
uint16 shortv1, shortv2;
CopyField2(tag, shortv1, shortv2);
} else if (count == 4) {
uint16 *tr, *tg, *tb, *ta;
CopyField4(tag, tr, tg, tb, ta);
} else if (count == (uint16) -1) {
uint16 shortv1;
uint16* shortav;
CopyField2(tag, shortv1, shortav);
}
break;
case TIFF_LONG:
{ uint32 longv;
CopyField(tag, longv);
}
break;
case TIFF_RATIONAL:
if (count == 1) {
float floatv;
CopyField(tag, floatv);
} else if (count == (uint16) -1) {
float* floatav;
CopyField(tag, floatav);
}
break;
case TIFF_ASCII:
{ char* stringv;
CopyField(tag, stringv);
}
break;
case TIFF_DOUBLE:
if (count == 1) {
double doublev;
CopyField(tag, doublev);
} else if (count == (uint16) -1) {
double* doubleav;
CopyField(tag, doubleav);
}
break;
default:
TIFFError(TIFFFileName(in),
"Data type %d is not supported, tag %d skipped.",
tag, type);
}
}
#undef CopyField4
#undef CopyField3
#undef CopyField2
#undef CopyField
static struct cpTag {
uint16 tag;
uint16 count;
TIFFDataType type;
} tags[] = {
{ TIFFTAG_SUBFILETYPE, 1, TIFF_LONG },
{ TIFFTAG_THRESHHOLDING, 1, TIFF_SHORT },
{ TIFFTAG_DOCUMENTNAME, 1, TIFF_ASCII },
{ TIFFTAG_IMAGEDESCRIPTION, 1, TIFF_ASCII },
{ TIFFTAG_MAKE, 1, TIFF_ASCII },
{ TIFFTAG_MODEL, 1, TIFF_ASCII },
{ TIFFTAG_MINSAMPLEVALUE, 1, TIFF_SHORT },
{ TIFFTAG_MAXSAMPLEVALUE, 1, TIFF_SHORT },
{ TIFFTAG_XRESOLUTION, 1, TIFF_RATIONAL },
{ TIFFTAG_YRESOLUTION, 1, TIFF_RATIONAL },
{ TIFFTAG_PAGENAME, 1, TIFF_ASCII },
{ TIFFTAG_XPOSITION, 1, TIFF_RATIONAL },
{ TIFFTAG_YPOSITION, 1, TIFF_RATIONAL },
{ TIFFTAG_RESOLUTIONUNIT, 1, TIFF_SHORT },
{ TIFFTAG_SOFTWARE, 1, TIFF_ASCII },
{ TIFFTAG_DATETIME, 1, TIFF_ASCII },
{ TIFFTAG_ARTIST, 1, TIFF_ASCII },
{ TIFFTAG_HOSTCOMPUTER, 1, TIFF_ASCII },
{ TIFFTAG_WHITEPOINT, 2, TIFF_RATIONAL },
{ TIFFTAG_PRIMARYCHROMATICITIES,(uint16) -1,TIFF_RATIONAL },
{ TIFFTAG_HALFTONEHINTS, 2, TIFF_SHORT },
{ TIFFTAG_INKSET, 1, TIFF_SHORT },
{ TIFFTAG_DOTRANGE, 2, TIFF_SHORT },
{ TIFFTAG_TARGETPRINTER, 1, TIFF_ASCII },
{ TIFFTAG_SAMPLEFORMAT, 1, TIFF_SHORT },
{ TIFFTAG_YCBCRCOEFFICIENTS, (uint16) -1,TIFF_RATIONAL },
{ TIFFTAG_YCBCRSUBSAMPLING, 2, TIFF_SHORT },
{ TIFFTAG_YCBCRPOSITIONING, 1, TIFF_SHORT },
{ TIFFTAG_REFERENCEBLACKWHITE, (uint16) -1,TIFF_RATIONAL },
{ TIFFTAG_EXTRASAMPLES, (uint16) -1, TIFF_SHORT },
{ TIFFTAG_SMINSAMPLEVALUE, 1, TIFF_DOUBLE },
{ TIFFTAG_SMAXSAMPLEVALUE, 1, TIFF_DOUBLE },
{ TIFFTAG_STONITS, 1, TIFF_DOUBLE },
};
#define NTAGS (sizeof (tags) / sizeof (tags[0]))
static void
cpTags(TIFF* in, TIFF* out)
{
struct cpTag *p;
for (p = tags; p < &tags[NTAGS]; p++)
Nov 4, 2018
Nov 4, 2018
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
{
if( p->tag == TIFFTAG_GROUP3OPTIONS )
{
uint16 compression;
if( !TIFFGetField(in, TIFFTAG_COMPRESSION, &compression) ||
compression != COMPRESSION_CCITTFAX3 )
continue;
}
if( p->tag == TIFFTAG_GROUP4OPTIONS )
{
uint16 compression;
if( !TIFFGetField(in, TIFFTAG_COMPRESSION, &compression) ||
compression != COMPRESSION_CCITTFAX4 )
continue;
}
cpTag(in, out, p->tag, p->count, p->type);
}
Oct 22, 2017
Oct 22, 2017
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
}
#undef NTAGS
char* stuff[] = {
"usage: tiff2bw [options] input.tif output.tif",
"where options are:",
" -R % use #% from red channel",
" -G % use #% from green channel",
" -B % use #% from blue channel",
"",
" -r # make each strip have no more than # rows",
"",
" -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding",
" -c zip[:opts] compress output with deflate encoding",
" -c packbits compress output with packbits encoding",
" -c g3[:opts] compress output with CCITT Group 3 encoding",
" -c g4 compress output with CCITT Group 4 encoding",
" -c none use no compression algorithm on output",
"",
"LZW and deflate options:",
" # set predictor value",
"For example, -c lzw:2 to get LZW-encoded data with horizontal differencing",
NULL
};
static void
usage(void)
{
char buf[BUFSIZ];
int i;
setbuf(stderr, buf);
fprintf(stderr, "%s\n\n", TIFFGetVersion());
for (i = 0; stuff[i] != NULL; i++)
fprintf(stderr, "%s\n", stuff[i]);
exit(-1);
}
/* vim: set ts=8 sts=8 sw=8 noet: */
/*
* Local Variables:
* mode: c
* c-basic-offset: 8
* fill-column: 78
* End:
*/