home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / libtiff / lbtif3_3.tar / tools / tiffinfo.c < prev    next >
C/C++ Source or Header  |  1993-08-26  |  8KB  |  309 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/tools/RCS/tiffinfo.c,v 1.14 93/08/26 15:12:44 sam Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1988, 1989, 1990, 1991, 1992 Sam Leffler
  7.  * Copyright (c) 1991, 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32.  
  33. #include "tiffio.h"
  34.  
  35. int    showdata = 0;            /* show data */
  36. int    readdata = 0;            /* read data in file */
  37. int    stoponerr = 0;            /* stop on first read error */
  38.  
  39. void    TIFFReadData(TIFF*);
  40.  
  41. void
  42. usage(int code)
  43. {
  44.     fprintf(stderr, "Usage: tiffinfo [-cdDSjs] [-#] TIFF-files\n");
  45.     fprintf(stderr, "-D    read data\n");
  46.     fprintf(stderr, "-S    stop reading data on 1st read error\n");
  47.     fprintf(stderr, "-d    show data\n");
  48.     fprintf(stderr, "-c    show color/gray response curves and colormap\n");
  49.     fprintf(stderr, "-s    show data strip offsets and byte counts\n");
  50.     fprintf(stderr, "-j    show JPEG tables\n");
  51.     fprintf(stderr, "-#    show directory number #\n");
  52.     exit(code);
  53. }
  54.  
  55. void
  56. main(int argc, char* argv[])
  57. {
  58.     int dirnum = -1, multiplefiles, c;
  59.     int verbose = 0;
  60.     uint16 order = 0;
  61.     char *cp;
  62.     TIFF *tif;
  63.     extern int optind;
  64.     long flags = 0;
  65.  
  66.     while ((c = getopt(argc, argv, "cdDSjlmsv0123456789")) != -1)
  67.         switch (c) {
  68.         case '0': case '1': case '2': case '3':
  69.         case '4': case '5': case '6': case '7':
  70.         case '8': case '9':
  71.             dirnum = atoi(&argv[optind-1][1]);
  72.             break;
  73.         case 'd':
  74.             showdata++;
  75.             /* fall thru... */
  76.         case 'D':
  77.             readdata++;
  78.             break;
  79.         case 'S':
  80.             stoponerr = 1;
  81.             break;
  82.         case 'c':
  83.             flags |= TIFFPRINT_COLORMAP | TIFFPRINT_CURVES;
  84.             break;
  85.         case 'l':
  86.             order = FILLORDER_LSB2MSB;
  87.             break;
  88.         case 'm':
  89.             order = FILLORDER_MSB2LSB;
  90.             break;
  91.         case 's':
  92.             flags |= TIFFPRINT_STRIPS;
  93.             break;
  94.         case 'j':
  95.             flags |= TIFFPRINT_JPEGQTABLES |
  96.                  TIFFPRINT_JPEGACTABLES |
  97.                  TIFFPRINT_JPEGDCTABLES;
  98.             break;
  99.         case 'v':
  100.             verbose = 1;
  101.             break;
  102.         case '?':
  103.             usage(-1);
  104.             /*NOTREACHED*/
  105.         }
  106.     if (optind >= argc)
  107.         usage(-2);
  108.     multiplefiles = (argc - optind > 1);
  109.     for (; optind < argc; optind++) {
  110.         if (multiplefiles || verbose)
  111.             printf("%s:\n", argv[optind]);
  112.         tif = TIFFOpen(argv[optind], "r");
  113.         if (tif != NULL) {
  114.             if (dirnum == -1) {
  115.                 do {
  116.                     if (order)
  117.                         TIFFSetField(tif, TIFFTAG_FILLORDER, order);
  118.                     TIFFPrintDirectory(tif, stdout, flags);
  119.                     if (readdata)
  120.                         TIFFReadData(tif);
  121.                 } while (TIFFReadDirectory(tif));
  122.             } else {
  123.                 if (TIFFSetDirectory(tif, dirnum)) {
  124.                     if (order)
  125.                         TIFFSetField(tif, TIFFTAG_FILLORDER, order);
  126.                     TIFFPrintDirectory(tif, stdout, flags);
  127.                     if (readdata)
  128.                         TIFFReadData(tif);
  129.                 }
  130.             }
  131.             TIFFClose(tif);
  132.         }
  133.     }
  134.     exit(0);
  135. }
  136.  
  137. static void
  138. ShowStrip(tstrip_t strip, unsigned char* pp, uint32 nrow, tsize_t scanline)
  139. {
  140.     register tsize_t cc;
  141.  
  142.     printf("Strip %lu:\n", (unsigned long) strip);
  143.     while (nrow-- > 0) {
  144.         for (cc = 0; cc < scanline; cc++) {
  145.             printf(" %02x", *pp++);
  146.             if (((cc+1) % 24) == 0)
  147.                 putchar('\n');
  148.         }
  149.         putchar('\n');
  150.     }
  151. }
  152.  
  153. void
  154. TIFFReadContigStripData(TIFF* tif)
  155. {
  156.     unsigned char *buf;
  157.     tsize_t scanline = TIFFScanlineSize(tif);
  158.  
  159.     buf = (unsigned char *)malloc(TIFFStripSize(tif));
  160.     if (buf) {
  161.         uint32 row, h;
  162.         uint32 rowsperstrip = (uint32)-1;
  163.  
  164.         TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
  165.         TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
  166.         for (row = 0; row < h; row += rowsperstrip) {
  167.             uint32 nrow = (row+rowsperstrip > h ?
  168.                 h-row : rowsperstrip);
  169.             tstrip_t strip = TIFFComputeStrip(tif, row, 0);
  170.             if (TIFFReadEncodedStrip(tif, strip, buf, nrow*scanline) < 0) {
  171.                 if (stoponerr)
  172.                     break;
  173.             } else if (showdata)
  174.                 ShowStrip(strip, buf, nrow, scanline);
  175.         }
  176.         free(buf);
  177.     }
  178. }
  179.  
  180. void
  181. TIFFReadSeparateStripData(TIFF* tif)
  182. {
  183.     unsigned char *buf;
  184.     tsize_t scanline = TIFFScanlineSize(tif);
  185.  
  186.     buf = (unsigned char *)malloc(TIFFStripSize(tif));
  187.     if (buf) {
  188.         uint32 row, h;
  189.         uint32 rowsperstrip = (uint32)-1;
  190.         tsample_t s, samplesperpixel;
  191.  
  192.         TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
  193.         TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
  194.         TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
  195.         for (row = 0; row < h; row += rowsperstrip) {
  196.             for (s = 0; s < samplesperpixel; s++) {
  197.                 uint32 nrow = (row+rowsperstrip > h ?
  198.                     h-row : rowsperstrip);
  199.                 tstrip_t strip = TIFFComputeStrip(tif, row, s);
  200.                 if (TIFFReadEncodedStrip(tif, strip, buf, nrow*scanline) < 0) {
  201.                     if (stoponerr)
  202.                         break;
  203.                 } else if (showdata)
  204.                     ShowStrip(strip, buf, nrow, scanline);
  205.             }
  206.         }
  207.         free(buf);
  208.     }
  209. }
  210.  
  211. static void
  212. ShowTile(uint32 row, uint32 col, tsample_t sample,
  213.     unsigned char* pp, uint32 nrow, uint32 rowsize)
  214. {
  215.     register tsize_t cc;
  216.  
  217.     printf("Tile (%lu,%lu", (unsigned long) row, (unsigned long) col);
  218.     if (sample > 0)
  219.         printf(",%u", sample);
  220.     printf("):\n");
  221.     while (nrow-- > 0) {
  222.         for (cc = 0; cc < rowsize; cc++) {
  223.             printf(" %02x", *pp++);
  224.             if (((cc+1) % 24) == 0)
  225.                 putchar('\n');
  226.         }
  227.         putchar('\n');
  228.     }
  229. }
  230.  
  231. void
  232. TIFFReadContigTileData(TIFF* tif)
  233. {
  234.     unsigned char *buf;
  235.     tsize_t rowsize = TIFFTileRowSize(tif);
  236.  
  237.     buf = (unsigned char *)malloc(TIFFTileSize(tif));
  238.     if (buf) {
  239.         uint32 tw, th, w, h;
  240.         uint32 row, col;
  241.  
  242.         TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
  243.         TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
  244.         TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
  245.         TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
  246.         for (row = 0; row < h; row += th) {
  247.             for (col = 0; col < w; col += tw) {
  248.                 if (TIFFReadTile(tif, buf, col, row, 0, 0) < 0) {
  249.                     if (stoponerr)
  250.                         break;
  251.                 } else if (showdata)
  252.                     ShowTile(row, col, -1, buf, th, rowsize);
  253.             }
  254.         }
  255.         free(buf);
  256.     }
  257. }
  258.  
  259. void
  260. TIFFReadSeparateTileData(TIFF* tif)
  261. {
  262.     unsigned char *buf;
  263.     tsize_t rowsize = TIFFTileRowSize(tif);
  264.  
  265.     buf = (unsigned char *)malloc(TIFFTileSize(tif));
  266.     if (buf) {
  267.         uint32 tw, th, w, h;
  268.         uint32 row, col;
  269.         tsample_t s, samplesperpixel;
  270.  
  271.         TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
  272.         TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
  273.         TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
  274.         TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
  275.         TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
  276.         for (row = 0; row < h; row += th) {
  277.             for (col = 0; col < w; col += tw) {
  278.                 for (s = 0; s < samplesperpixel; s++) {
  279.                     if (TIFFReadTile(tif, buf, col, row, 0, 0) < 0) {
  280.                         if (stoponerr)
  281.                             break;
  282.                     } else if (showdata)
  283.                         ShowTile(row, col, s, buf, th, rowsize);
  284.                 }
  285.             }
  286.         }
  287.         free(buf);
  288.     }
  289. }
  290.  
  291. void
  292. TIFFReadData(TIFF* tif)
  293. {
  294.     uint16 config;
  295.  
  296.     TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &config);
  297.     if (TIFFIsTiled(tif)) {
  298.         if (config == PLANARCONFIG_CONTIG)
  299.             TIFFReadContigTileData(tif);
  300.         else
  301.             TIFFReadSeparateTileData(tif);
  302.     } else {
  303.         if (config == PLANARCONFIG_CONTIG)
  304.             TIFFReadContigStripData(tif);
  305.         else
  306.             TIFFReadSeparateStripData(tif);
  307.     }
  308. }
  309.