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

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/tools/RCS/sgi2tiff.c,v 1.8 93/08/26 15:09:38 sam Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 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. #include <gl/image.h>
  33.  
  34. #include "tiffio.h"
  35.  
  36. #define    streq(a,b)    (strcmp(a,b) == 0)
  37.  
  38. static    short config = PLANARCONFIG_CONTIG;
  39. static    uint16 compression = COMPRESSION_LZW;
  40. static    uint16 predictor = 0;
  41. static    uint16 fillorder = 0;
  42. static    uint32 rowsperstrip = (uint32) -1;
  43.  
  44. static    void usage(void);
  45. static    int cpContig(IMAGE*, TIFF*);
  46. static    int cpSeparate(IMAGE*, TIFF*);
  47.  
  48. /* XXX image library has no prototypes */
  49. extern    IMAGE* iopen(const char*, const char*);
  50. extern    void iclose(IMAGE*);
  51. extern    void getrow(IMAGE*, short*, int, int);
  52.  
  53. void
  54. main(int argc, char* argv[])
  55. {
  56.     IMAGE *in;
  57.     TIFF *out;
  58.  
  59.     argc--, argv++;
  60.     if (argc < 2)
  61.         usage();
  62.     for (; argc > 2 && argv[0][0] == '-'; argc--, argv++) {
  63.         if (streq(argv[0], "-none")) {
  64.             compression = COMPRESSION_NONE;
  65.             continue;
  66.         }
  67.         if (streq(argv[0], "-packbits")) {
  68.             compression = COMPRESSION_PACKBITS;
  69.             continue;
  70.         }
  71.         if (streq(argv[0], "-lzw")) {
  72.             compression = COMPRESSION_LZW;
  73.             continue;
  74.         }
  75.         if (streq(argv[0], "-contig")) {
  76.             config = PLANARCONFIG_CONTIG;
  77.             continue;
  78.         }
  79.         if (streq(argv[0], "-separate")) {
  80.             config = PLANARCONFIG_SEPARATE;
  81.             continue;
  82.         }
  83.         if (streq(argv[0], "-lsb2msb")) {
  84.             fillorder = FILLORDER_LSB2MSB;
  85.             continue;
  86.         }
  87.         if (streq(argv[0], "-msb2lsb")) {
  88.             fillorder = FILLORDER_MSB2LSB;
  89.             continue;
  90.         }
  91.         if (streq(argv[0], "-rowsperstrip")) {
  92.             argc--, argv++;
  93.             rowsperstrip = atoi(argv[0]);
  94.             continue;
  95.         }
  96.         if (streq(argv[0], "-predictor")) {
  97.             argc--, argv++;
  98.             predictor = atoi(argv[0]);
  99.             continue;
  100.         }
  101.         usage();
  102.     }
  103.     in = iopen(argv[0], "r");
  104.     if (in == NULL)
  105.         exit(-1);
  106.     out = TIFFOpen(argv[1], "w");
  107.     if (out == NULL)
  108.         exit(-2);
  109.     TIFFSetField(out, TIFFTAG_IMAGEWIDTH, (uint32) in->xsize);
  110.     TIFFSetField(out, TIFFTAG_IMAGELENGTH, (uint32) in->ysize);
  111.     TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
  112.     TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
  113.     if (predictor != 0)
  114.         TIFFSetField(out, TIFFTAG_PREDICTOR, predictor);
  115.     TIFFSetField(out, TIFFTAG_PHOTOMETRIC,
  116.         in->zsize == 1 ? PHOTOMETRIC_MINISBLACK : PHOTOMETRIC_RGB);
  117.     if (fillorder != 0)
  118.         TIFFSetField(out, TIFFTAG_FILLORDER, fillorder);
  119.     TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
  120.     TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, in->zsize);
  121.     TIFFSetField(out, TIFFTAG_MINSAMPLEVALUE, (uint16) in->min);
  122.     TIFFSetField(out, TIFFTAG_MAXSAMPLEVALUE, (uint16) in->max);
  123.     TIFFSetField(out, TIFFTAG_PLANARCONFIG, config);
  124.     if (config != PLANARCONFIG_SEPARATE) {
  125.         if (rowsperstrip <= 0)
  126.             rowsperstrip = (8*1024)/TIFFScanlineSize(out);
  127.         if (rowsperstrip == 0)
  128.             rowsperstrip == 1;
  129.         TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
  130.     } else            /* force 1 row/strip for library limitation */
  131.         TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, 1L);
  132.     if (in->name[0] != '\0')
  133.         TIFFSetField(out, TIFFTAG_IMAGEDESCRIPTION, in->name);
  134.     if (config == PLANARCONFIG_CONTIG)
  135.         cpContig(in, out);
  136.     else
  137.         cpSeparate(in, out);
  138.     (void) iclose(in);
  139.     (void) TIFFClose(out);
  140.     exit(0);
  141. }
  142.  
  143. static int
  144. cpContig(IMAGE* in, TIFF* out)
  145. {
  146.     unsigned char *buf = (unsigned char *)malloc(TIFFScanlineSize(out));
  147.     short *r = NULL;
  148.     int x, y;
  149.  
  150.     if (in->zsize == 3) {
  151.         short *g, *b;
  152.  
  153.         r = (short *)malloc(3 * in->xsize * sizeof (short));
  154.         g = r + in->xsize;
  155.         b = g + in->xsize;
  156.         for (y = in->ysize-1; y >= 0; y--) {
  157.             unsigned char* pp = buf;
  158.  
  159.             getrow(in, r, y, 0);
  160.             getrow(in, g, y, 1);
  161.             getrow(in, b, y, 2);
  162.             for (x = 0; x < in->xsize; x++) {
  163.                 *pp++ = r[x];
  164.                 *pp++ = g[x];
  165.                 *pp++ = b[x];
  166.             }
  167.             if (TIFFWriteScanline(out, buf, in->ysize-y-1, 0) < 0)
  168.                 goto bad;
  169.         }
  170.     } else {
  171.         r = (short *)malloc(in->xsize * sizeof (short));
  172.         for (y = in->ysize-1; y >= 0; y--) {
  173.             getrow(in, r, y, 0);
  174.             for (x = 0; x < in->xsize; x++)
  175.                 buf[x] = r[x];
  176.             if (TIFFWriteScanline(out, buf, in->ysize-y-1, 0) < 0)
  177.                 goto bad;
  178.         }
  179.     }
  180. done:
  181.     if (r)
  182.         free(r);
  183.     free(buf);
  184.     return (1);
  185. bad:
  186.     if (r)
  187.         free(r);
  188.     free(buf);
  189.     return (0);
  190. }
  191.  
  192. static int
  193. cpSeparate(IMAGE* in, TIFF* out)
  194. {
  195.     unsigned char *buf = (unsigned char *)malloc(TIFFScanlineSize(out));
  196.     short *r = (short *)malloc(in->xsize * sizeof (short));
  197.     int x, y, z;
  198.  
  199.     for (z = 0; z < in->zsize; z++) {
  200.         for (y = in->ysize-1; y >= 0; y--) {
  201.             getrow(in, r, y, z);
  202.             for (x = 0; x < in->xsize; x++)
  203.                 buf[x] = r[x];
  204.             if (TIFFWriteScanline(out, buf, in->ysize-y-1, z) < 0)
  205.                 goto bad;
  206.         }
  207.     }
  208. done:
  209.     free(r);
  210.     free(buf);
  211.     return (1);
  212. bad:
  213.     free(r);
  214.     free(buf);
  215.     return (0);
  216. }
  217.  
  218. static void
  219. usage(void)
  220. {
  221.     fprintf(stderr, "usage: sgi2tiff [options] input output\n");
  222.     fprintf(stderr, "where options are:\n");
  223.     fprintf(stderr,
  224.         " -contig\tpack samples contiguously (e.g. RGBRGB...)\n");
  225.     fprintf(stderr,
  226.         " -separate\tstore samples separately (e.g. RRR...GGG...BBB...)\n");
  227.     fprintf(stderr, "\n");
  228.     fprintf(stderr,
  229.         " -lzw\t\tcompress output with Lempel-Ziv & Welch encoding\n");
  230.     fprintf(stderr,
  231.         " -packbits\tcompress output with packbits encoding\n");
  232.     fprintf(stderr,
  233.         " -none\t\tuse no compression algorithm on output\n");
  234.     fprintf(stderr, "\n");
  235.     fprintf(stderr,
  236.         " -rowsperstrip #\tmake each strip have no more than # rows\n");
  237.     exit(-1);
  238. }
  239.