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

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/tools/RCS/sgisv.c,v 1.12 93/08/26 15:10:24 sam Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 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 <gl.h>
  33.  
  34. #include "tiffio.h"
  35.  
  36. #define    streq(a,b)    (strcmp(a,b) == 0)
  37.  
  38. typedef unsigned char u_char;
  39. typedef unsigned long u_long;
  40.  
  41. uint32    rowsperstrip = (u_long) -1;
  42. uint16    compression = COMPRESSION_LZW;
  43. uint16    config = PLANARCONFIG_CONTIG;
  44. int    xmaxscreen;
  45. int    ymaxscreen;
  46. uint16    photometric = PHOTOMETRIC_RGB;
  47.  
  48. static    void usage(void);
  49. static    void tiffsv(char*, int, int, int, int);
  50.  
  51. void
  52. main(int argc, char* argv[])
  53. {
  54.  
  55.     argc--, argv++;
  56.     for (; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
  57.         if (streq(argv[0], "-none")) {
  58.             compression = COMPRESSION_NONE;
  59.             continue;
  60.         }
  61.         if (streq(argv[0], "-packbits")) {
  62.             compression = COMPRESSION_PACKBITS;
  63.             continue;
  64.         }
  65.         if (streq(argv[0], "-lzw")) {
  66.             compression = COMPRESSION_LZW;
  67.             continue;
  68.         }
  69.         if (streq(argv[0], "-contig")) {
  70.             config = PLANARCONFIG_CONTIG;
  71.             continue;
  72.         }
  73.         if (streq(argv[0], "-separate")) {
  74.             config = PLANARCONFIG_SEPARATE;
  75.             continue;
  76.         }
  77.         if (streq(argv[0], "-rowsperstrip")) {
  78.             argc--, argv++;
  79.             rowsperstrip = atoi(argv[0]);
  80.             continue;
  81.         }
  82.         usage();
  83.     }
  84.     if (strcmp(argv[argc-1], "-b") == 0) {
  85.         argc--;
  86.         photometric = PHOTOMETRIC_MINISBLACK;
  87.     }
  88.     if (argc != 1 && argc != 5)
  89.         usage();
  90.     xmaxscreen = getgdesc(GD_XPMAX)-1;
  91.     ymaxscreen = getgdesc(GD_YPMAX)-1;
  92.     foreground();
  93.     noport();
  94.     winopen("tiffsv");
  95.     if (argc == 5)
  96.         tiffsv(argv[0],
  97.             atoi(argv[1]), atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
  98.     else
  99.         tiffsv(argv[0], 0, xmaxscreen, 0, ymaxscreen);
  100.     exit(0);
  101. }
  102.  
  103. static void
  104. usage(void)
  105. {
  106.  
  107.     fprintf(stderr, "usage: tiffsv [options] outimage [x1 x2 y1 y2] [-b]\n");
  108.     fprintf(stderr, "where options are:\n");
  109.     fprintf(stderr,
  110.         " -contig\tpack samples contiguously (e.g. RGBRGB...)\n");
  111.     fprintf(stderr,
  112.         " -separate\tstore samples separately (e.g. RRR...GGG...BBB...)\n");
  113.     fprintf(stderr, "\n");
  114.     fprintf(stderr,
  115.         " -lzw\t\tcompress output with Lempel-Ziv & Welch encoding\n");
  116.     fprintf(stderr,
  117.         " -packbits\tcompress output with packbits encoding\n");
  118.     fprintf(stderr,
  119.         " -none\t\tuse no compression algorithm on output\n");
  120.     fprintf(stderr, "\n");
  121.     fprintf(stderr,
  122.         " -rowsperstrip #\tmake each strip have no more than # rows\n");
  123.     exit(-1);
  124. }
  125.  
  126. static void
  127. svRGBSeparate(TIFF* tif, u_long* ss, int xsize, int ysize)
  128. {
  129.     tsize_t stripsize = TIFFStripSize(tif);
  130.     u_char *rbuf = (u_char *)malloc(3*stripsize);
  131.     u_char *gbuf = rbuf + stripsize;
  132.     u_char *bbuf = gbuf + stripsize;
  133.     register int y;
  134.  
  135.     for (y = 0; y <= ysize; y += rowsperstrip) {
  136.         u_char *rp, *gp, *bp;
  137.         register int x;
  138.         register uint32 n;
  139.  
  140.         n = rowsperstrip;
  141.         if (n > ysize-y+1)
  142.             n = ysize-y+1;
  143.         rp = rbuf; gp = gbuf; bp = bbuf;
  144.         do {
  145.             for (x = 0; x <= xsize; x++) {
  146.                 u_long v = ss[x];
  147.                 rp[x] = v;
  148.                 gp[x] = v >> 8;
  149.                 bp[x] = v >> 16;
  150.             }
  151.             rp += xsize+1, gp += xsize+1, bp += xsize+1;
  152.             ss += xsize+1;
  153.         } while (--n);
  154.         if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif,y,0),
  155.             rbuf, stripsize) < 0)
  156.             break;
  157.         if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif,y,1),
  158.             gbuf, stripsize) < 0)
  159.             break;
  160.         if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif,y,2),
  161.             bbuf, stripsize) < 0)
  162.             break;
  163.     }
  164.     free(rbuf);
  165. }
  166.  
  167. static void
  168. svRGBContig(TIFF* tif, u_long* ss, int xsize, int ysize)
  169. {
  170.     register int x, y;
  171.     tsize_t stripsize = TIFFStripSize(tif);
  172.     u_char *strip = (u_char *)malloc(stripsize);
  173.  
  174.     for (y = 0; y <= ysize; y += rowsperstrip) {
  175.         register u_char *pp = strip;
  176.         register uint32 n;
  177.  
  178.         n = rowsperstrip;
  179.         if (n > ysize-y+1)
  180.             n = ysize-y+1;
  181.         do {
  182.             for (x = 0; x <= xsize; x++) {
  183.                 u_long v = ss[x];
  184.                 pp[0] = v;
  185.                 pp[1] = v >> 8;
  186.                 pp[2] = v >> 16;
  187.                 pp += 3;
  188.             }
  189.             ss += xsize+1;
  190.         } while (--n);
  191.         if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif,y,0),
  192.             strip, stripsize) < 0)
  193.             break;
  194.     }
  195.     free(strip);
  196. }
  197.  
  198. #undef RED
  199. #undef GREEN
  200. #undef BLUE
  201. #define    CVT(x)    (((x)*255)/100)
  202. #define    RED    CVT(28)        /* 28% */
  203. #define    GREEN    CVT(59)        /* 59% */
  204. #define    BLUE    CVT(11)        /* 11% */
  205.  
  206. static void
  207. svGrey(TIFF* tif, u_long* ss, int xsize, int ysize)
  208. {
  209.     register int x, y;
  210.     u_char *buf = (u_char *)malloc(TIFFScanlineSize(tif));
  211.  
  212.     for (y = 0; y <= ysize; y++) {
  213.         for (x = 0; x <= xsize; x++) {
  214.             u_char *cp = (u_char *)&ss[x];
  215.             buf[x] = (RED*cp[3] + GREEN*cp[2] + BLUE*cp[1]) >> 8;
  216.         }
  217.         if (TIFFWriteScanline(tif, buf, (uint32) y, 0) < 0)
  218.             break;
  219.         ss += xsize+1;
  220.     }
  221.     free(buf);
  222. }
  223.  
  224. #define    MIN(a,b)    ((a)<(b)?(a):(b))
  225. #define    ABS(x)        ((x)<0?-(x):(x))
  226.  
  227. static void
  228. tiffsv(char* name, int x1, int x2, int y1, int y2)
  229. {
  230.     TIFF *tif;
  231.     int xsize, ysize;
  232.     int xorg, yorg;
  233.     int temp, y, i;
  234.     int pos, togo, n;
  235.     u_long *scrbuf, *ss;
  236.  
  237.     xorg = MIN(x1,x2);
  238.     yorg = MIN(y1,y2);
  239.     if (xorg<0)
  240.         xorg = 0;
  241.     if (yorg<0)
  242.         yorg = 0;
  243.     xsize = ABS(x2-x1);
  244.     ysize = ABS(y2-y1);
  245.     if (xorg+xsize > xmaxscreen)
  246.         xsize = xmaxscreen-xorg;
  247.     if (yorg+ysize > ymaxscreen)
  248.         ysize = ymaxscreen-yorg;
  249.     tif = TIFFOpen(name, "w");
  250.     TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32) (xsize+1));
  251.     TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32) (ysize+1));
  252.     TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
  253.     TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL,
  254.         photometric == PHOTOMETRIC_RGB ? 3 : 1);
  255.     TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric);
  256.     TIFFSetField(tif, TIFFTAG_PLANARCONFIG, config);
  257.     TIFFSetField(tif, TIFFTAG_COMPRESSION, compression);
  258.     TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_BOTLEFT);
  259.     if ((long)rowsperstrip <= 0)
  260.         rowsperstrip = (8*1024)/TIFFScanlineSize(tif);
  261.     if (rowsperstrip == 0)
  262.         rowsperstrip = 1L;
  263.     TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
  264.     scrbuf = (u_long *)malloc((xsize+1)*(ysize+1)*sizeof (u_long));
  265.     readdisplay(xorg, yorg, xorg+xsize, yorg+ysize, scrbuf, RD_FREEZE);
  266.     ss = scrbuf;
  267.     if (photometric == PHOTOMETRIC_RGB) {
  268.         if (config == PLANARCONFIG_SEPARATE)
  269.             svRGBSeparate(tif, scrbuf, xsize, ysize);
  270.         else
  271.             svRGBContig(tif, scrbuf, xsize, ysize);
  272.     } else
  273.         svGrey(tif, scrbuf, xsize, ysize);
  274.     (void) TIFFClose(tif);
  275.     free((char *)scrbuf);
  276. }
  277.