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

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/tools/RCS/ppm2tiff.c,v 1.7 93/08/26 15:08:34 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.  
  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    compression = -1;
  39. uint16    photometric;
  40. uint32    rowsperstrip = (uint32) -1;
  41. double    resolution = -1;
  42.  
  43. void    usage(void);
  44.  
  45. static void
  46. BadPPM(char* file)
  47. {
  48.     fprintf(stderr, "%s: Not a PPM file.\n", file);
  49.     exit(-2);
  50. }
  51.  
  52. void
  53. main(int argc, char* argv[])
  54. {
  55.     unsigned char *buf;
  56.     uint32 row;
  57.     tsize_t linebytes;
  58.     uint16 spp;
  59.     TIFF *out;
  60.     FILE *in;
  61.     uint32 w, h;
  62.     int c, prec;
  63.     char *infile;
  64.  
  65.     argc--, argv++;
  66.     if (argc < 1)
  67.         usage();
  68.     for (; argc > 1 && argv[0][0] == '-'; argc--, argv++) {
  69.         if (streq(argv[0], "-none")) {
  70.             compression = COMPRESSION_NONE;
  71.             continue;
  72.         }
  73.         if (streq(argv[0], "-packbits")) {
  74.             compression = COMPRESSION_PACKBITS;
  75.             continue;
  76.         }
  77.         if (streq(argv[0], "-lzw")) {
  78.             compression = COMPRESSION_LZW;
  79.             continue;
  80.         }
  81.         if (streq(argv[0], "-rowsperstrip")) {
  82.             argc--, argv++;
  83.             rowsperstrip = atoi(argv[0]);
  84.             continue;
  85.         }
  86.         if (streq(argv[0], "-resolution")) {
  87.             argc--, argv++;
  88.             resolution = atof(argv[0]);
  89.             continue;
  90.         }
  91.         usage();
  92.     }
  93.  
  94.     /*
  95.      * If only one file is specified, read input from
  96.      * stdin; otherwise usage is: ppm2tiff input output.
  97.      */
  98.     if (argc > 1) {
  99.         infile = argv[0];
  100.         argv++;
  101.         in = fopen(infile, "r");
  102.         if (in == NULL) {
  103.             fprintf(stderr, "%s: Can not open.\n", infile);
  104.             exit(-1);
  105.         }
  106.     } else {
  107.         infile = "<stdin>";
  108.         in = stdin;
  109.     }
  110.  
  111.     if (getc(in) != 'P')
  112.         BadPPM(infile);
  113.     switch (c = getc(in)) {
  114.     case '5':            /* it's a PGM file */
  115.         spp = 1;
  116.         photometric = PHOTOMETRIC_MINISBLACK;
  117.         break;
  118.     case '6':            /* it's a PPM file */
  119.         spp = 3;
  120.         photometric = PHOTOMETRIC_RGB;
  121.         break;
  122.     default:
  123.         BadPPM(infile);
  124.     }
  125.     if (fscanf(in, " %d %d %d", &w, &h, &prec) != 3)
  126.         BadPPM(infile);
  127.     if (getc(in) != '\n' || w <= 0 || h <= 0 || prec != 255)
  128.         BadPPM(infile);
  129.  
  130.     out = TIFFOpen(argv[0], "w");
  131.     if (out == NULL)
  132.         exit(-4);
  133.     TIFFSetField(out, TIFFTAG_IMAGEWIDTH,  w);
  134.     TIFFSetField(out, TIFFTAG_IMAGELENGTH, h);
  135.     TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
  136.     TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, spp);
  137.     TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
  138.     TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  139.     TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric);
  140.     if (compression == (uint16)-1)
  141.         compression = COMPRESSION_LZW;
  142.     TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
  143.     linebytes = spp * w;
  144.     if (TIFFScanlineSize(out) > linebytes)
  145.         buf = (unsigned char *)malloc(linebytes);
  146.     else
  147.         buf = (unsigned char *)malloc(TIFFScanlineSize(out));
  148.     if (rowsperstrip == (uint32)-1)
  149.         rowsperstrip = (8*1024)/linebytes;
  150.     if (rowsperstrip == 0)
  151.         rowsperstrip = 1;
  152.     TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
  153.     if (resolution > 0) {
  154.         TIFFSetField(out, TIFFTAG_XRESOLUTION, resolution);
  155.         TIFFSetField(out, TIFFTAG_YRESOLUTION, resolution);
  156.         TIFFSetField(out, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
  157.     }
  158.     for (row = 0; row < h; row++) {
  159.         if (fread(buf, linebytes, 1, in) != 1) {
  160.             fprintf(stderr, "%s: scanline %lu: Read error.\n",
  161.                 infile, (unsigned long) row);
  162.             break;
  163.         }
  164.         if (TIFFWriteScanline(out, buf, row, 0) < 0)
  165.             break;
  166.     }
  167.     (void) TIFFClose(out);
  168. }
  169.  
  170. void
  171. usage(void)
  172. {
  173.     fprintf(stderr, "usage: ppm2tif [options] [input] output\n");
  174.     fprintf(stderr, "where options are:\n");
  175.     fprintf(stderr,
  176.         " -lzw\t\tcompress output with Lempel-Ziv & Welch encoding\n");
  177.     fprintf(stderr, " -packbits\tcompress output with packbits encoding\n");
  178.     fprintf(stderr, " -none\t\tuse no compression algorithm on output\n");
  179.     fprintf(stderr, "\n");
  180.     fprintf(stderr,
  181.         " -rowsperstrip #\tmake each strip have no more than # rows\n");
  182.     fprintf(stderr, "\n");
  183.     fprintf(stderr, " -resolution #\tset x&y resolution (dpi)\n");
  184.     exit(-1);
  185. }
  186.