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

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/tools/RCS/ras2tiff.c,v 1.12 93/08/26 15:08:53 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. #include "rasterfile.h"
  33. #include "tiffio.h"
  34.  
  35. #define    howmany(x, y)    (((x)+((y)-1))/(y))
  36. #define    streq(a,b)    (strcmp(a,b) == 0)
  37.  
  38. uint16    config = PLANARCONFIG_CONTIG;
  39. uint16    compression = -1;
  40. uint32    rowsperstrip = (uint32) -1;
  41.  
  42. void    usage(void);
  43.  
  44. void
  45. main(int argc, char* argv[])
  46. {
  47.     unsigned char* buf;
  48.     uint32 row;
  49.     tsize_t linebytes, scanline;
  50.     TIFF *out;
  51.     FILE *in;
  52.     struct rasterfile h;
  53.  
  54.     argc--, argv++;
  55.     if (argc < 2)
  56.         usage();
  57.     for (; argc > 2 && argv[0][0] == '-'; argc--, argv++) {
  58.         if (streq(argv[0], "-none")) {
  59.             compression = COMPRESSION_NONE;
  60.             continue;
  61.         }
  62.         if (streq(argv[0], "-packbits")) {
  63.             compression = COMPRESSION_PACKBITS;
  64.             continue;
  65.         }
  66.         if (streq(argv[0], "-lzw")) {
  67.             compression = COMPRESSION_LZW;
  68.             continue;
  69.         }
  70.         if (streq(argv[0], "-rowsperstrip")) {
  71.             argc--, argv++;
  72.             rowsperstrip = atoi(argv[0]);
  73.             continue;
  74.         }
  75.         usage();
  76.     }
  77.     in = fopen(argv[0], "r");
  78.     if (in == NULL) {
  79.         fprintf(stderr, "%s: Can not open.\n", argv[0]);
  80.         exit(-1);
  81.     }
  82.     if (fread(&h, sizeof (h), 1, in) != 1) {
  83.         fprintf(stderr, "%s: Can not read header.\n", argv[0]);
  84.         exit(-2);
  85.     }
  86.     if (h.ras_magic != RAS_MAGIC) {
  87.         fprintf(stderr, "%s: Not a rasterfile.\n", argv[0]);
  88.         exit(-3);
  89.     }
  90.     out = TIFFOpen(argv[1], "w");
  91.     if (out == NULL)
  92.         exit(-4);
  93.     TIFFSetField(out, TIFFTAG_IMAGEWIDTH, (uint32) h.ras_width);
  94.     TIFFSetField(out, TIFFTAG_IMAGELENGTH, (uint32) h.ras_height);
  95.     TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
  96.     TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, h.ras_depth > 8 ? 3 : 1);
  97.     TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, h.ras_depth > 1 ? 8 : 1);
  98.     TIFFSetField(out, TIFFTAG_PLANARCONFIG, config);
  99.     if (h.ras_maptype != RMT_NONE) {
  100.         uint16* red;
  101.         register uint16* map;
  102.         register int i, j;
  103.         int mapsize;
  104.  
  105.         buf = (unsigned char *)malloc(h.ras_maplength);
  106.         if (buf == NULL) {
  107.             fprintf(stderr, "No space to read in colormap.\n");
  108.             exit(-5);
  109.         }
  110.         if (fread(buf, h.ras_maplength, 1, in) != 1) {
  111.             fprintf(stderr, "%s: Read error on colormap.\n",
  112.                 argv[0]);
  113.             exit(-6);
  114.         }
  115.         mapsize = 1<<h.ras_depth; 
  116.         if (h.ras_maplength > mapsize*3) {
  117.             fprintf(stderr,
  118.                 "%s: Huh, %d colormap entries, should be %d?\n",
  119.                 argv[0], h.ras_maplength, mapsize*3);
  120.             exit(-7);
  121.         }
  122.         red = (uint16*)malloc(mapsize * 3 * sizeof (uint16));
  123.         if (red == NULL) {
  124.             fprintf(stderr, "No space for colormap.\n");
  125.             exit(-8);
  126.         }
  127.         map = red;
  128.         for (j = 0; j < 3; j++) {
  129. #define    SCALE(x)    (((x)*((1L<<16)-1))/255)
  130.             for (i = h.ras_maplength/3; i-- > 0;)
  131.                 *map++ = SCALE(*buf++);
  132.             if ((i = h.ras_maplength/3) < mapsize) {
  133.                 i = mapsize - i;
  134.                 memset(map, 0, i*sizeof (uint16));
  135.                 map += i;
  136.             }
  137.         }
  138.         TIFFSetField(out, TIFFTAG_COLORMAP,
  139.              red, red + mapsize, red + 2*mapsize);
  140.         TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
  141.         if (compression == (uint16)-1)
  142.             compression = COMPRESSION_PACKBITS;
  143.         TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
  144.     } else {
  145.         /* XXX this is bogus... */
  146.         TIFFSetField(out, TIFFTAG_PHOTOMETRIC, h.ras_depth == 24 ?
  147.             PHOTOMETRIC_RGB : PHOTOMETRIC_MINISBLACK);
  148.         if (compression == (uint16)-1)
  149.             compression = COMPRESSION_LZW;
  150.         TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
  151.     }
  152.     linebytes = ((h.ras_depth*h.ras_width+15) >> 3) &~ 1;
  153.     scanline = TIFFScanlineSize(out);
  154.     if (scanline > linebytes) {
  155.         buf = (unsigned char *)malloc(scanline);
  156.         memset(buf+linebytes, 0, scanline-linebytes);
  157.     } else
  158.         buf = (unsigned char *)malloc(linebytes);
  159.     if (rowsperstrip == (uint32)-1)
  160.         rowsperstrip = (8*1024)/scanline;
  161.     if (rowsperstrip == 0)
  162.         rowsperstrip = 1;
  163.     TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
  164.     for (row = 0; row < h.ras_height; row++) {
  165.         if (fread(buf, linebytes, 1, in) != 1) {
  166.             fprintf(stderr, "%s: scanline %lu: Read error.\n",
  167.                 argv[0], (unsigned long) row);
  168.             break;
  169.         }
  170.         if (TIFFWriteScanline(out, buf, row, 0) < 0)
  171.             break;
  172.     }
  173.     (void) TIFFClose(out);
  174. }
  175.  
  176. void
  177. usage(void)
  178. {
  179.     fprintf(stderr, "usage: ras2tif [options] input output\n");
  180.     fprintf(stderr, "where options are:\n");
  181.     fprintf(stderr,
  182.         " -lzw\t\tcompress output with Lempel-Ziv & Welch encoding\n");
  183.     fprintf(stderr,
  184.         " -packbits\tcompress output with packbits encoding\n");
  185.     fprintf(stderr,
  186.         " -none\t\tuse no compression algorithm on output\n");
  187.     fprintf(stderr, "\n");
  188.     fprintf(stderr,
  189.         " -rowsperstrip #\tmake each strip have no more than # rows\n");
  190.     exit(-1);
  191. }
  192.