File: tiff.c

package info (click to toggle)
xloadimage 4.1-25
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,820 kB
  • sloc: ansic: 36,084; asm: 284; makefile: 282; sh: 280
file content (635 lines) | stat: -rw-r--r-- 17,809 bytes parent folder | download
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/* tiff.c:
 *
 * interface file for TIFF image format
 *
 * jim frost 09.05.93
 */

#include "image.h"
#include "options.h"
#ifdef HAVE_LIBTIFF

#include <tiffio.h>

#ifndef TIFF_VERSION_CLASSIC /* libtiff4 or libtiff5? */
#define TIFFHeaderCommon TIFFHeader
#endif

/* this structure contains all the information we care about WRT a TIFF
 * image.
 */
struct tiff_info {
  unsigned short type; /* little- or big-endian */
  int width;
  int height;
  int depth;
  unsigned short planarconfiguration;
  unsigned short photometric;
  unsigned short compression;
  unsigned short bitspersample;
  unsigned short samplesperpixel;
  unsigned short bytesperrow;
  char *title;
};

static TIFF *is_tiff(fullname, name, info)
     char *fullname;
     char *name;
     struct tiff_info *info;
{
  ZFILE *zf;
  TIFFHeaderClassic th;
  TIFF *tiff;

  zf = zopen(fullname);

  /* read TIFF header and see if it looks right
   */
  if ((zread(zf, (byte *)&th, sizeof(TIFFHeaderClassic)) == sizeof(TIFFHeaderClassic)) &&
      ((th.tiff_magic == TIFF_BIGENDIAN) ||
       (th.tiff_magic == TIFF_LITTLEENDIAN))) {

    /* definitely a tiff file
     */
    if (zf->type != ZSTANDARD) {
      printf("%s is a TIFF file, but TIFF files can't be read through pipes or filters, sorry.\n", name);
      return((TIFF *)-1);
    }

    info->type= th.tiff_magic;

    tiff = TIFFOpen(fullname, "r");
    if (!tiff)
      return(0);

    /* ok, start sucking in information about this file
     */
    /* find out gross information about this image
     */
    TIFFGetFieldDefaulted(tiff, TIFFTAG_IMAGEWIDTH, &info->width);
    TIFFGetFieldDefaulted(tiff, TIFFTAG_IMAGELENGTH, &info->height);
    TIFFGetFieldDefaulted(tiff, TIFFTAG_PLANARCONFIG, &info->planarconfiguration);
    TIFFGetFieldDefaulted(tiff, TIFFTAG_PHOTOMETRIC, &info->photometric);
    TIFFGetFieldDefaulted(tiff, TIFFTAG_COMPRESSION, &info->compression);
    TIFFGetFieldDefaulted(tiff, TIFFTAG_BITSPERSAMPLE, &info->bitspersample);
    TIFFGetFieldDefaulted(tiff, TIFFTAG_SAMPLESPERPIXEL, &info->samplesperpixel);
    info->bytesperrow= TIFFScanlineSize(tiff);

    /* get the "title" of the image
     */
    if (!TIFFGetField(tiff, TIFFTAG_DOCUMENTNAME, &info->title))
      if (!TIFFGetField(tiff, TIFFTAG_IMAGEDESCRIPTION, &info->title))
	info->title = NULL;
    /* make our own copy just to be safe
     */
    if (info->title)
      info->title = dupString(info->title);

#if 0 /* maybe it does */
    /* convert byte orders.  why the hell doesn't the TIFF stuff do this
     * for you?
     */
    switch (info->type) {
    case TIFF_BIGENDIAN:
#define convert(V) (V) = memToVal(&(V), sizeof(V))
      convert(info->width);
      convert(info->height);
      convert(info->planarconfiguration);
      convert(info->photometric);
      convert(info->compression);
      convert(info->bitspersample);
      convert(info->samplesperpixel);
#undef convert
      break;
    case TIFF_LITTLEENDIAN:
#define convert(V) (V) = memToValLSB(&(V), sizeof(V))
      convert(info->width);
      convert(info->height);
      convert(info->planarconfiguration);
      convert(info->photometric);
      convert(info->compression);
      convert(info->bitspersample);
      convert(info->samplesperpixel);
#undef convert
      break;
    }
#endif
    return(tiff);
  }

  zclose(zf);
  return(0);
}

static char *photometricName(info)
     struct tiff_info *info;
{
  static char buf[32];

  switch (info->photometric) {
  case PHOTOMETRIC_MINISBLACK:
    if (info->bitspersample > 1) {
      snprintf(buf, 31, "%d-bit greyscale ", info->bitspersample);
      return(buf);
    }
    else
      return "white-on-black ";
  case PHOTOMETRIC_MINISWHITE:
    if (info->bitspersample > 1) {
      snprintf(buf, 31, "%d-bit greyscale ", info->bitspersample);
      return(buf);
    }
    else
      return "black-on-white ";
  case PHOTOMETRIC_RGB:
    return "RGB ";
  case PHOTOMETRIC_PALETTE:
    return "colormap ";
  case PHOTOMETRIC_MASK:
    return "masked ";
  case PHOTOMETRIC_SEPARATED:
    return "color-separated ";
  case PHOTOMETRIC_YCBCR:
    return "YCBCR ";
  case PHOTOMETRIC_CIELAB:
    return "CIE L*a*b* ";
  default:
    return "";
  }
}

static char *compressionName(compression)
     int compression;
{
  switch (compression) {
  case COMPRESSION_NONE:
    return "standard ";
  case COMPRESSION_CCITTRLE:
    return "RLE ";
  case COMPRESSION_CCITTFAX3:
    return "G3FAX ";
  case COMPRESSION_CCITTFAX4:
    return "G4FAX ";
  case COMPRESSION_LZW:
    return "LZW ";
  case COMPRESSION_JPEG:
    return "JPEG ";
  case COMPRESSION_NEXT:
    return "NeXT ";
  case COMPRESSION_CCITTRLEW:
    return "RLEW ";
  case COMPRESSION_PACKBITS:
    return "Macintosh ";
  case COMPRESSION_THUNDERSCAN:
    return "Thunderscan ";
  default:
    return "";
  }
}

static char *planarConfigName(config)
     int config;
{
  switch (config) {
  case PLANARCONFIG_CONTIG:
    return "single-plane ";
  case PLANARCONFIG_SEPARATE:
    return "separate-plane ";
  default:
    return "";
  }
}

static void babble(name, info)
     char *name;
     struct tiff_info *info;
{
  switch (info->photometric) {
  case PHOTOMETRIC_MINISWHITE:
  case PHOTOMETRIC_MINISBLACK:
    printf("%s is a %dx%d %s%s%sTIFF image",
	   name, info->width, info->height,
	   planarConfigName(info->planarconfiguration),
	   photometricName(info),
	   compressionName(info->compression));
    break;
  default:
    printf("%s is a %dx%d %d-bit %s%s%sTIFF image",
	   name, info->width, info->height,
	   (info->bitspersample * info->samplesperpixel),
	   planarConfigName(info->planarconfiguration),
	   photometricName(info),
	   compressionName(info->compression));
  }
  if (info->title)
    printf("Titled \"%s\"", info->title);
  printf("\n");
}

int tiffIdent(fullname, name)
{
  TIFF *tiff;
  struct tiff_info info;

  tiff = is_tiff(fullname, name, &info);
  if (tiff == NULL)
    return(0);
  babble(name, &info);
  if (tiff == (TIFF *)-1) /* is TIFF, but can't open it */
    return(1);
  TIFFClose(tiff);

/*  babble(fullname, name, info); */
  return(1);
}

Image *tiffLoad(fullname, name, verbose)
     char *fullname;
     char *name;
     int verbose;
{
  TIFF *tiff;
  struct tiff_info info;
  Image *image;
  byte *row, *dest_line;
  int dest_line_len;
  register int pixlen;
  register byte *dest;
  register int x, y;

  tiff = is_tiff(fullname, name, &info);
  if ((tiff == NULL) || (tiff == (TIFF *)-1))
    return(NULL);
  if (verbose)
    babble(name, &info);

  row = lmalloc(info.bytesperrow);

  /* decide which type of image to allocate based on photometric
   * information and set up the colormap as necessary
   */
  switch (info.photometric) {
  case PHOTOMETRIC_MINISWHITE: /* bitmap and greyscale image types */
  case PHOTOMETRIC_MINISBLACK:
    /* monochrome image
     */
    if (info.bitspersample == 1) {
      dest_line_len = (info.width / 8) + (info.width % 8 ? 1 : 0);
      if (dest_line_len > info.bytesperrow) /* just in case */
	dest_line_len = info.bytesperrow;

      image= newBitImage(info.width, info.height);

      /* default bit image colormap matches PHOTOMETRIC_MINISWHITE, so we
       * need to change it if it's the other type.
       */
      if (info.photometric == PHOTOMETRIC_MINISBLACK) {
	image->rgb.red[0]= image->rgb.green[0]= image->rgb.blue[0]= 0;
	image->rgb.red[1]= image->rgb.green[1]= image->rgb.blue[1]= 65535;
      }

      /* read the image data and set the pixels
       */
      dest = image->data;
      if (info.bitspersample == 1) {
	for (y = 0; y < info.height; y++) {
	  if (TIFFReadScanline(tiff, row, y, 0) < 0) {
	    fprintf(stderr, "%s: Short read in image data!\n", fullname);
	    break;
	  }

	  /* isn't it nice when it's in the same data format we use?
	   */
	  bcopy(row, dest, dest_line_len);
	  dest += dest_line_len;
	}
      }
      break;
    }
    else {
      /* need to build the scale for greyscale images
       */
      image = newRGBImage(info.width, info.height, info.bitspersample);
      for (x = 0; x < image->rgb.size; x++) {
	int value = (65535 * x) / (unsigned int)(image->rgb.size);
	if (info.photometric == PHOTOMETRIC_MINISWHITE)
	  value = 65535 - value;
	image->rgb.red[x]= image->rgb.green[x]= image->rgb.blue[x]= value;
      }
      image->rgb.used = image->rgb.size;
      goto read_pallette_data; /* ugly but expedient */
    }
    /* NOTREACHED */

  case PHOTOMETRIC_PALETTE:
    /* this is a close match with the IRGB-style Image.
     */
    image = newRGBImage(info.width, info.height, info.bitspersample);
    {
      unsigned short *red, *green, *blue;
 
      if (!TIFFGetField(tiff, TIFFTAG_COLORMAP, &red, &green, &blue)) {
	fprintf(stderr, "%s: Image has no colormap!\n", fullname);
	freeImage(image);
	image = NULL;
	break;
      }

      /* fill in our colormap with this data
       */
      switch (info.type) {
      case TIFF_BIGENDIAN:
	for (x = 0; x < image->rgb.size; x++) {
	  image->rgb.red[x] = memToVal(&red[x], 2);
	  image->rgb.green[x] = memToVal(&green[x], 2);
	  image->rgb.blue[x] = memToVal(&blue[x], 2);
	}
	break;
      case TIFF_LITTLEENDIAN:
	for (x = 0; x < image->rgb.size; x++) {
	  image->rgb.red[x] = memToValLSB(&red[x], 2);
	  image->rgb.green[x] = memToValLSB(&green[x], 2);
	  image->rgb.blue[x] = memToValLSB(&blue[x], 2);
	}
	break;
      }
      image->rgb.used = image->rgb.size; /* all filled up */
    }

  read_pallette_data:
    /* start reading image data
     */
    dest_line_len = info.width * image->pixlen;
    if (dest_line_len > info.bytesperrow) /* just in case */
      dest_line_len = info.bytesperrow;
    pixlen = image->pixlen;
    dest_line = image->data;

    switch (info.type) {
    case TIFF_LITTLEENDIAN:
      if (info.bitspersample > 8) {

	/* bummer, need to byte-swap
	 */
	for (y = 0; y < info.height; y++) {
	  if (TIFFReadScanline(tiff, row, y, 0) < 0) {
	    fprintf(stderr, "%s: Short read in image data!\n", fullname);
	    break;
	  }
	  dest = dest_line;
	  for (x = 0; x < info.width; x++) {
	    valToMem(memToValLSB(dest, image->pixlen), dest, pixlen);
	    dest += pixlen;
	  }
	  dest_line += dest_line_len;
	}
      }
      /* if the sample fits in 1 byte we don't need to byte swap so
       * we fall through into the big-endian reader which is much faster.
       */
      /* FALLTHRU */
    case TIFF_BIGENDIAN:
      /* bigendian is a nice match for our internal data format
       */
      for (y = 0; y < info.height; y++) {
	if (TIFFReadScanline(tiff, row, y, 0) < 0) {
	  fprintf(stderr, "%s: Short read in image data!\n", fullname);
	  break;
	}
	bcopy(row, dest_line, dest_line_len);
	dest_line += dest_line_len;
      }
      break;
    }
    break;

  case PHOTOMETRIC_RGB:
    /* this is a close match with the ITRUE-style Image.
     */
    if (info.samplesperpixel != 3) {
      fprintf(stderr,
	      "%s: Can't handle TIFF RGB images with %d samples per pixel, sorry\n",
	      fullname,
	      info.samplesperpixel);
      image = NULL;
      break;
    }
    image = newTrueImage(info.width, info.height);
    dest_line_len = info.width * image->pixlen;
    if (dest_line_len > info.bytesperrow) /* just in case */
      dest_line_len = info.bytesperrow;
    dest_line = image->data;

    /* quick-and-dirty copy if it's just what we're looking for
     */
    switch (info.planarconfiguration) {
    case PLANARCONFIG_CONTIG:
      if (info.bitspersample == 8) {
	for (y = 0; y < info.height; y++) {
	  if (TIFFReadScanline(tiff, row, y, 0) < 0) {
	    fprintf(stderr, "%s: Short read in image data!\n", fullname);
	    break;
	  }
	  bcopy(row, dest_line, dest_line_len);
	  dest_line += dest_line_len;
	}
      }
      else {
    case PLANARCONFIG_SEPARATE:
	fprintf(stderr, "%s: %s is an unsupported planar configuration.\n",
		fullname, planarConfigName(info.planarconfiguration));
	freeImage(image);
	image = NULL;
      }
    }
    break;

  case PHOTOMETRIC_MASK:
  case PHOTOMETRIC_SEPARATED:
  case PHOTOMETRIC_YCBCR:
  case PHOTOMETRIC_CIELAB:
  default:
    fprintf(stderr, "%s: %s is an unsupported TIFF photometric style, sorry.\n",
	    fullname, photometricName(&info));
    image = NULL;
  }

  if (image) {
    if (info.title)
      image->title = info.title;
    else
      image->title = dupString(name);
  }

  TIFFClose(tiff);
  lfree(row);
  return(image);
}

/* this is not what I'd call a well-designed TIFF dumping function but it
 * does do the job.
 */
void tiffDump(image, options, file, verbose)
     Image *image;
     char *options;
     char *file;
     int verbose;
{
  TIFF *out;
  char *name, *value;
  int compression = COMPRESSION_LZW;
  int y;
  int srclinelen;
  byte *srcptr;

  out = TIFFOpen(file, "w");
  TIFFSetField(out, TIFFTAG_IMAGEWIDTH, (unsigned long) image->width);
  TIFFSetField(out, TIFFTAG_IMAGELENGTH, (unsigned long) image->height);
  TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
  TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, 1);

  /* this sets the TIFF resolution, necessary for some packages.  since
   * we have no concept of resolution (most input files don't retain that
   * information) we set it for 1:1 pixel resolution.
   */
  TIFFSetField(out, TIFFTAG_RESOLUTIONUNIT, RESUNIT_NONE);
  TIFFSetField(out, TIFFTAG_XRESOLUTION, (float)1.0);
  TIFFSetField(out, TIFFTAG_YRESOLUTION, (float)1.0);

  /* process image options
   */
  while (getNextTypeOption(&options, &name, &value) > 0) {
    if (!strncmp("compression", name, strlen(name))) {
      if (!strncmp("none", value, strlen(value)))
	compression = COMPRESSION_NONE;
      else if (!strncmp("rle", value, strlen(value)))
	compression = COMPRESSION_CCITTRLE;
      else if (!strncmp("g3fax", value, strlen(value)))
	compression = COMPRESSION_CCITTFAX3;
      else if (!strncmp("g4fax", value, strlen(value)))
	compression = COMPRESSION_CCITTFAX4;
      else if (!strncmp("lzw", value, strlen(value)))
	compression = COMPRESSION_LZW; /* default */
      else if (!strncmp("jpeg", value, strlen(value)))
	compression = COMPRESSION_JPEG;
      else if (!strncmp("next", value, strlen(value)))
	compression = COMPRESSION_NEXT;
      else if (!strncmp("rlew", value, strlen(value)))
	compression = COMPRESSION_CCITTRLEW;
      else if (!strncmp("packbits", value, strlen(value)) ||
	       !strncmp("mac", value, strlen(value)))
	compression = COMPRESSION_PACKBITS;
      else if (!strncmp("thunderscan", value, strlen(value)))
	compression = COMPRESSION_THUNDERSCAN;
      else
	fprintf(stderr, "tiffDump: Unknown compression type '%s'.\n",
		value);
    }
    else
      fprintf(stderr, "tiffDump: Unknown option '%s'\n", name);
  }
	
  TIFFSetField(out, TIFFTAG_COMPRESSION, compression);

  switch (image->type) {
  case IBITMAP:
    /* should use pixel intensity but this will usually work */
    if (image->rgb.red[0] > image->rgb.red[1]) {
      if (verbose) {
	printf("Dumping black-on-white ");
	if (compression != COMPRESSION_NONE)
	  printf("%s", compressionName(compression));
	printf("TIFF image to %s.\n", file);
      }
      TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
    }
    else {
      if (verbose) {
	printf("Dumping white-on-black ");
	if (compression != COMPRESSION_NONE)
	  printf("%s", compressionName(compression));
	printf("TIFF image to %s.\n", file);
      }
      TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
    }
    TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 1);
    TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 1);

    srclinelen = (image->width / 8) + (image->width % 8 ? 1 : 0);
    srcptr = image->data;
    for (y = 0; y < image->height; y++) {
      TIFFWriteScanline(out, srcptr, y, 0);
      srcptr += srclinelen;
    }
    break;

  case IRGB:
    TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 1);
    TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8 * image->pixlen);
    TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);

    if (verbose) {
      printf("Dumping colormap ");
      if (compression != COMPRESSION_NONE)
	printf("%s", compressionName(compression));
      printf("TIFF image to %s.\n", file);
    }

    /* save the colormap
     */
    {
      unsigned short *red, *green, *blue;

      red = (unsigned short *)lmalloc(image->rgb.size * sizeof(unsigned short));
      green = (unsigned short *)lmalloc(image->rgb.size * sizeof(unsigned short));
      blue = (unsigned short *)lmalloc(image->rgb.size * sizeof(unsigned short));

      for (y = 0; y < image->rgb.used; y++) {
	valToMem(image->rgb.red[y], &red[y], 2);
	valToMem(image->rgb.green[y], &green[y], 2);
	valToMem(image->rgb.blue[y], &blue[y], 2);
      }
      while (y < image->rgb.size) {
	valToMem(0, &red[y], 2);
	valToMem(0, &green[y], 2);
	valToMem(0, &blue[y], 2);
	y++;
      }
      TIFFSetField(out, TIFFTAG_COLORMAP, red, green, blue);
      lfree((byte *)red);
      lfree((byte *)green);
      lfree((byte *)blue);
    }
    srclinelen = image->width * image->pixlen;
    srcptr = image->data;
    for (y = 0; y < image->height; y++) {
      TIFFWriteScanline(out, srcptr, y, 0);
      srcptr += srclinelen;
    }
    break;

  case ITRUE:
    if (verbose) {
      printf("Dumping RGB ");
      if (compression != COMPRESSION_NONE)
	printf("%s", compressionName(compression));
      printf("TIFF image to %s.\n", file);
    }
    TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 3);
    TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
    TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
    srclinelen = image->width * image->pixlen;
    srcptr = image->data;
    for (y = 0; y < image->height; y++) {
      TIFFWriteScanline(out, srcptr, y, 0);
      srcptr += srclinelen;
    }
    break;
  }
  TIFFClose(out);
}

#else /* !HAVE_LIBTIFF */
static int unused;
#endif /* !HAVE_LIBTIFF */