"Fossies" - the Fresh Open Source Software Archive

Member "tiff-4.6.0/contrib/ras/ras2tif.c" (22 May 2023, 7241 Bytes) of package /linux/misc/tiff-4.6.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ras2tif.c" see the Fossies "Dox" file reference documentation.

    1 #ifndef lint
    2 static char sccsid[] = "@(#)ras2tif.c 1.2 90/03/06";
    3 #endif
    4 /*-
    5  * ras2tif.c - Converts from a Sun Rasterfile to a Tagged Image File.
    6  *
    7  * Copyright (c) 1990 by Sun Microsystems, Inc.
    8  *
    9  * Author: Patrick J. Naughton
   10  * naughton@wind.sun.com
   11  *
   12  * Permission to use, copy, modify, and distribute this software and its
   13  * documentation for any purpose and without fee is hereby granted,
   14  * provided that the above copyright notice appear in all copies and that
   15  * both that copyright notice and this permission notice appear in
   16  * supporting documentation.
   17  *
   18  * This file is provided AS IS with no warranties of any kind.  The author
   19  * shall have no liability with respect to the infringement of copyrights,
   20  * trade secrets or any patents by this file or any part thereof.  In no
   21  * event will the author be liable for any lost revenue or profits or
   22  * other special, indirect and consequential damages.
   23  *
   24  * Comments and additions should be sent to the author:
   25  *
   26  *                     Patrick J. Naughton
   27  *                     Sun Microsystems
   28  *                     2550 Garcia Ave, MS 14-40
   29  *                     Mountain View, CA 94043
   30  *                     (415) 336-1080
   31  *
   32  * Revision History:
   33  * 11-Jan-89: Created.
   34  * 06-Mar-90: fix bug in SCALE() macro.
   35  *        got rid of xres and yres, (they weren't working anyways).
   36  *        fixed bpsl calculation.
   37  * 25-Nov-99: y2k fix (year as 1900 + tm_year) <mike@onshore.com>
   38  *
   39  * Description:
   40  *   This program takes a Sun Rasterfile [see rasterfile(5)] as input and
   41  * writes a MicroSoft/Aldus "Tagged Image File Format" image or "TIFF" file.
   42  * The input file may be standard input, but the output TIFF file must be a
   43  * real file since seek(2) is used.
   44  */
   45 
   46 #include "tiffio.h"
   47 #include <pixrect/pixrect_hs.h>
   48 #include <stdio.h>
   49 #include <sys/time.h>
   50 
   51 typedef int boolean;
   52 #define True (1)
   53 #define False (0)
   54 #define SCALE(x) (((x) * ((1L << 16) - 1)) / 255)
   55 
   56 boolean Verbose = False;
   57 boolean dummyinput = False;
   58 char *pname; /* program name (used for error messages) */
   59 
   60 void error(s1, s2) char *s1, *s2;
   61 {
   62     fprintf(stderr, s1, pname, s2);
   63     exit(1);
   64 }
   65 
   66 void usage() { error("usage: %s -[vq] [-|rasterfile] TIFFfile\n", NULL); }
   67 
   68 main(argc, argv) int argc;
   69 char *argv[];
   70 {
   71     char *inf = NULL;
   72     char *outf = NULL;
   73     FILE *fp;
   74     int depth, i;
   75     long row;
   76     TIFF *tif;
   77     Pixrect *pix;        /* The Sun Pixrect */
   78     colormap_t Colormap; /* The Pixrect Colormap */
   79     u_short red[256], green[256], blue[256];
   80     struct tm *ct;
   81     struct timeval tv;
   82     long width, height;
   83     long rowsperstrip;
   84     int year;
   85     short photometric;
   86     short samplesperpixel;
   87     short bitspersample;
   88     int bpsl;
   89     static char *version = "ras2tif 1.0";
   90     static char *datetime = "1990:01:01 12:00:00";
   91 
   92     gettimeofday(&tv, (struct timezone *)NULL);
   93     ct = localtime(&tv.tv_sec);
   94     year = 1900 + ct->tm_year;
   95     sprintf(datetime, "%04d:%02d:%02d %02d:%02d:%02d", year, ct->tm_mon + 1,
   96             ct->tm_mday, ct->tm_hour, ct->tm_min, ct->tm_sec);
   97 
   98     setbuf(stderr, NULL);
   99     pname = argv[0];
  100 
  101     while (--argc)
  102     {
  103         if ((++argv)[0][0] == '-')
  104         {
  105             switch (argv[0][1])
  106             {
  107                 case 'v':
  108                     Verbose = True;
  109                     break;
  110                 case 'q':
  111                     usage();
  112                     break;
  113                 case '\0':
  114                     if (inf == NULL)
  115                         dummyinput = True;
  116                     else
  117                         usage();
  118                     break;
  119                 default:
  120                     fprintf(stderr, "%s: illegal option -%c.\n", pname,
  121                             argv[0][1]);
  122                     exit(1);
  123             }
  124         }
  125         else if (inf == NULL && !dummyinput)
  126         {
  127             inf = argv[0];
  128         }
  129         else if (outf == NULL)
  130             outf = argv[0];
  131         else
  132             usage();
  133     }
  134 
  135     if (outf == NULL)
  136         error("%s: can't write output file to a stream.\n", NULL);
  137 
  138     if (dummyinput || inf == NULL)
  139     {
  140         inf = "Standard Input";
  141         fp = stdin;
  142     }
  143     else if ((fp = fopen(inf, "r")) == NULL)
  144         error("%s: %s couldn't be opened.\n", inf);
  145 
  146     if (Verbose)
  147         fprintf(stderr, "Reading rasterfile from %s...", inf);
  148 
  149     pix = pr_load(fp, &Colormap);
  150     if (pix == NULL)
  151         error("%s: %s is not a raster file.\n", inf);
  152 
  153     if (Verbose)
  154         fprintf(stderr, "done.\n");
  155 
  156     if (Verbose)
  157         fprintf(stderr, "Writing %s...", outf);
  158 
  159     tif = TIFFOpen(outf, "w");
  160 
  161     if (tif == NULL)
  162         error("%s: error opening TIFF file %s", outf);
  163 
  164     width = pix->pr_width;
  165     height = pix->pr_height;
  166     depth = pix->pr_depth;
  167 
  168     switch (depth)
  169     {
  170         case 1:
  171             samplesperpixel = 1;
  172             bitspersample = 1;
  173             photometric = PHOTOMETRIC_MINISBLACK;
  174             break;
  175         case 8:
  176             samplesperpixel = 1;
  177             bitspersample = 8;
  178             photometric = PHOTOMETRIC_PALETTE;
  179             break;
  180         case 24:
  181             samplesperpixel = 3;
  182             bitspersample = 8;
  183             photometric = PHOTOMETRIC_RGB;
  184             break;
  185         case 32:
  186             samplesperpixel = 4;
  187             bitspersample = 8;
  188             photometric = PHOTOMETRIC_RGB;
  189             break;
  190         default:
  191             error("%s: bogus depth: %d\n", depth);
  192     }
  193 
  194     bpsl = ((depth * width + 15) >> 3) & ~1;
  195     rowsperstrip = (8 * 1024) / bpsl;
  196 
  197     TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
  198     TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
  199     TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bitspersample);
  200     TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
  201     TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
  202     TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric);
  203     TIFFSetField(tif, TIFFTAG_DOCUMENTNAME, inf);
  204     TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, "converted Sun rasterfile");
  205     TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);
  206     TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
  207     TIFFSetField(tif, TIFFTAG_STRIPBYTECOUNTS, height / rowsperstrip);
  208     TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  209     TIFFSetField(tif, TIFFTAG_SOFTWARE, version);
  210     TIFFSetField(tif, TIFFTAG_DATETIME, datetime);
  211 
  212     memset(red, 0, sizeof(red));
  213     memset(green, 0, sizeof(green));
  214     memset(blue, 0, sizeof(blue));
  215     if (depth == 8)
  216     {
  217         TIFFSetField(tif, TIFFTAG_COLORMAP, red, green, blue);
  218         for (i = 0; i < Colormap.length; i++)
  219         {
  220             red[i] = SCALE(Colormap.map[0][i]);
  221             green[i] = SCALE(Colormap.map[1][i]);
  222             blue[i] = SCALE(Colormap.map[2][i]);
  223         }
  224     }
  225     if (Verbose)
  226         fprintf(stderr, "%dx%dx%d image, ", width, height, depth);
  227 
  228     for (row = 0; row < height; row++)
  229         if (TIFFWriteScanline(tif, (u_char *)mprd_addr(mpr_d(pix), 0, row), row,
  230                               0) < 0)
  231         {
  232             fprintf("failed a scanline write (%d)\n", row);
  233             break;
  234         }
  235     TIFFFlushData(tif);
  236     TIFFClose(tif);
  237 
  238     if (Verbose)
  239         fprintf(stderr, "done.\n");
  240 
  241     pr_destroy(pix);
  242 
  243     exit(0);
  244 }