Blame contrib/ras/ras2tif.c

Packit 7838c8
#ifndef lint
Packit 7838c8
static char sccsid[] = "@(#)ras2tif.c 1.2 90/03/06";
Packit 7838c8
#endif
Packit 7838c8
/*-
Packit 7838c8
 * ras2tif.c - Converts from a Sun Rasterfile to a Tagged Image File.
Packit 7838c8
 *
Packit 7838c8
 * Copyright (c) 1990 by Sun Microsystems, Inc.
Packit 7838c8
 *
Packit 7838c8
 * Author: Patrick J. Naughton
Packit 7838c8
 * naughton@wind.sun.com
Packit 7838c8
 *
Packit 7838c8
 * Permission to use, copy, modify, and distribute this software and its
Packit 7838c8
 * documentation for any purpose and without fee is hereby granted,
Packit 7838c8
 * provided that the above copyright notice appear in all copies and that
Packit 7838c8
 * both that copyright notice and this permission notice appear in
Packit 7838c8
 * supporting documentation.
Packit 7838c8
 *
Packit 7838c8
 * This file is provided AS IS with no warranties of any kind.  The author
Packit 7838c8
 * shall have no liability with respect to the infringement of copyrights,
Packit 7838c8
 * trade secrets or any patents by this file or any part thereof.  In no
Packit 7838c8
 * event will the author be liable for any lost revenue or profits or
Packit 7838c8
 * other special, indirect and consequential damages.
Packit 7838c8
 *
Packit 7838c8
 * Comments and additions should be sent to the author:
Packit 7838c8
 *
Packit 7838c8
 *                     Patrick J. Naughton
Packit 7838c8
 *                     Sun Microsystems
Packit 7838c8
 *                     2550 Garcia Ave, MS 14-40
Packit 7838c8
 *                     Mountain View, CA 94043
Packit 7838c8
 *                     (415) 336-1080
Packit 7838c8
 *
Packit 7838c8
 * Revision History:
Packit 7838c8
 * 11-Jan-89: Created.
Packit 7838c8
 * 06-Mar-90: fix bug in SCALE() macro.
Packit 7838c8
 *	      got rid of xres and yres, (they weren't working anyways).
Packit 7838c8
 *	      fixed bpsl calculation.
Packit 7838c8
 * 25-Nov-99: y2k fix (year as 1900 + tm_year) <mike@onshore.com>
Packit 7838c8
 *
Packit 7838c8
 * Description:
Packit 7838c8
 *   This program takes a Sun Rasterfile [see rasterfile(5)] as input and
Packit 7838c8
 * writes a MicroSoft/Aldus "Tagged Image File Format" image or "TIFF" file.
Packit 7838c8
 * The input file may be standard input, but the output TIFF file must be a
Packit 7838c8
 * real file since seek(2) is used.
Packit 7838c8
 */
Packit 7838c8
Packit 7838c8
#include <stdio.h>
Packit 7838c8
#include <sys/time.h>
Packit 7838c8
#include <pixrect/pixrect_hs.h>
Packit 7838c8
#include "tiffio.h"
Packit 7838c8
Packit 7838c8
typedef int boolean;
Packit 7838c8
#define True (1)
Packit 7838c8
#define False (0)
Packit 7838c8
#define	SCALE(x)	(((x)*((1L<<16)-1))/255)
Packit 7838c8
Packit 7838c8
boolean     Verbose = False;
Packit 7838c8
boolean     dummyinput = False;
Packit 7838c8
char       *pname;		/* program name (used for error messages) */
Packit 7838c8
Packit 7838c8
void
Packit 7838c8
error(s1, s2)
Packit 7838c8
    char       *s1,
Packit 7838c8
               *s2;
Packit 7838c8
{
Packit 7838c8
    fprintf(stderr, s1, pname, s2);
Packit 7838c8
    exit(1);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
void
Packit 7838c8
usage()
Packit 7838c8
{
Packit 7838c8
    error("usage: %s -[vq] [-|rasterfile] TIFFfile\n", NULL);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
Packit 7838c8
main(argc, argv)
Packit 7838c8
    int         argc;
Packit 7838c8
    char       *argv[];
Packit 7838c8
{
Packit 7838c8
    char       *inf = NULL;
Packit 7838c8
    char       *outf = NULL;
Packit 7838c8
    FILE       *fp;
Packit 7838c8
    int         depth,
Packit 7838c8
                i;
Packit 7838c8
    long        row;
Packit 7838c8
    TIFF       *tif;
Packit 7838c8
    Pixrect    *pix;		/* The Sun Pixrect */
Packit 7838c8
    colormap_t  Colormap;	/* The Pixrect Colormap */
Packit 7838c8
    u_short     red[256],
Packit 7838c8
                green[256],
Packit 7838c8
                blue[256];
Packit 7838c8
    struct tm  *ct;
Packit 7838c8
    struct timeval tv;
Packit 7838c8
    long        width,
Packit 7838c8
                height;
Packit 7838c8
    long        rowsperstrip;
Packit 7838c8
    int         year; 
Packit 7838c8
    short       photometric;
Packit 7838c8
    short       samplesperpixel;
Packit 7838c8
    short       bitspersample;
Packit 7838c8
    int         bpsl;
Packit 7838c8
    static char *version = "ras2tif 1.0";
Packit 7838c8
    static char *datetime = "1990:01:01 12:00:00";
Packit 7838c8
Packit 7838c8
    gettimeofday(&tv, (struct timezone *) NULL);
Packit 7838c8
    ct = localtime(&tv.tv_sec);
Packit 7838c8
    year=1900 + ct->tm_year; 
Packit 7838c8
    sprintf(datetime, "%04d:%02d:%02d %02d:%02d:%02d",
Packit 7838c8
	    year, ct->tm_mon + 1, ct->tm_mday,
Packit 7838c8
	    ct->tm_hour, ct->tm_min, ct->tm_sec);
Packit 7838c8
Packit 7838c8
    setbuf(stderr, NULL);
Packit 7838c8
    pname = argv[0];
Packit 7838c8
Packit 7838c8
    while (--argc) {
Packit 7838c8
	if ((++argv)[0][0] == '-') {
Packit 7838c8
	    switch (argv[0][1]) {
Packit 7838c8
	    case 'v':
Packit 7838c8
		Verbose = True;
Packit 7838c8
		break;
Packit 7838c8
	    case 'q':
Packit 7838c8
		usage();
Packit 7838c8
		break;
Packit 7838c8
	    case '\0':
Packit 7838c8
		if (inf == NULL)
Packit 7838c8
		    dummyinput = True;
Packit 7838c8
		else
Packit 7838c8
		    usage();
Packit 7838c8
		break;
Packit 7838c8
	    default:
Packit 7838c8
		fprintf(stderr, "%s: illegal option -%c.\n", pname,
Packit 7838c8
			argv[0][1]);
Packit 7838c8
		exit(1);
Packit 7838c8
	    }
Packit 7838c8
	} else if (inf == NULL && !dummyinput) {
Packit 7838c8
	    inf = argv[0];
Packit 7838c8
	} else if (outf == NULL)
Packit 7838c8
	    outf = argv[0];
Packit 7838c8
	else
Packit 7838c8
	    usage();
Packit 7838c8
    }
Packit 7838c8
Packit 7838c8
    if (outf == NULL)
Packit 7838c8
	error("%s: can't write output file to a stream.\n", NULL);
Packit 7838c8
Packit 7838c8
    if (dummyinput || inf == NULL) {
Packit 7838c8
	inf = "Standard Input";
Packit 7838c8
	fp = stdin;
Packit 7838c8
    } else if ((fp = fopen(inf, "r")) == NULL)
Packit 7838c8
	error("%s: %s couldn't be opened.\n", inf);
Packit 7838c8
Packit 7838c8
    if (Verbose)
Packit 7838c8
	fprintf(stderr, "Reading rasterfile from %s...", inf);
Packit 7838c8
Packit 7838c8
    pix = pr_load(fp, &Colormap);
Packit 7838c8
    if (pix == NULL)
Packit 7838c8
	error("%s: %s is not a raster file.\n", inf);
Packit 7838c8
Packit 7838c8
    if (Verbose)
Packit 7838c8
	fprintf(stderr, "done.\n");
Packit 7838c8
Packit 7838c8
    if (Verbose)
Packit 7838c8
	fprintf(stderr, "Writing %s...", outf);
Packit 7838c8
Packit 7838c8
    tif = TIFFOpen(outf, "w");
Packit 7838c8
Packit 7838c8
    if (tif == NULL)
Packit 7838c8
	error("%s: error opening TIFF file %s", outf);
Packit 7838c8
Packit 7838c8
    width = pix->pr_width;
Packit 7838c8
    height = pix->pr_height;
Packit 7838c8
    depth = pix->pr_depth;
Packit 7838c8
Packit 7838c8
    switch (depth) {
Packit 7838c8
    case 1:
Packit 7838c8
	samplesperpixel = 1;
Packit 7838c8
	bitspersample = 1;
Packit 7838c8
	photometric = PHOTOMETRIC_MINISBLACK;
Packit 7838c8
	break;
Packit 7838c8
    case 8:
Packit 7838c8
	samplesperpixel = 1;
Packit 7838c8
	bitspersample = 8;
Packit 7838c8
	photometric = PHOTOMETRIC_PALETTE;
Packit 7838c8
	break;
Packit 7838c8
    case 24:
Packit 7838c8
	samplesperpixel = 3;
Packit 7838c8
	bitspersample = 8;
Packit 7838c8
	photometric = PHOTOMETRIC_RGB;
Packit 7838c8
	break;
Packit 7838c8
    case 32:
Packit 7838c8
	samplesperpixel = 4;
Packit 7838c8
	bitspersample = 8;
Packit 7838c8
	photometric = PHOTOMETRIC_RGB;
Packit 7838c8
	break;
Packit 7838c8
    default:
Packit 7838c8
	error("%s: bogus depth: %d\n", depth);
Packit 7838c8
    }
Packit 7838c8
Packit 7838c8
    bpsl = ((depth * width + 15) >> 3) & ~1;
Packit 7838c8
    rowsperstrip = (8 * 1024) / bpsl;
Packit 7838c8
Packit 7838c8
    TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
Packit 7838c8
    TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
Packit 7838c8
    TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bitspersample);
Packit 7838c8
    TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
Packit 7838c8
    TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
Packit 7838c8
    TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric);
Packit 7838c8
    TIFFSetField(tif, TIFFTAG_DOCUMENTNAME, inf);
Packit 7838c8
    TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, "converted Sun rasterfile");
Packit 7838c8
    TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);
Packit 7838c8
    TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
Packit 7838c8
    TIFFSetField(tif, TIFFTAG_STRIPBYTECOUNTS, height / rowsperstrip);
Packit 7838c8
    TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
Packit 7838c8
    TIFFSetField(tif, TIFFTAG_SOFTWARE, version);
Packit 7838c8
    TIFFSetField(tif, TIFFTAG_DATETIME, datetime);
Packit 7838c8
Packit 7838c8
    memset(red, 0, sizeof(red));
Packit 7838c8
    memset(green, 0, sizeof(green));
Packit 7838c8
    memset(blue, 0, sizeof(blue));
Packit 7838c8
    if (depth == 8) {
Packit 7838c8
	TIFFSetField(tif, TIFFTAG_COLORMAP, red, green, blue);
Packit 7838c8
	for (i = 0; i < Colormap.length; i++) {
Packit 7838c8
	    red[i] = SCALE(Colormap.map[0][i]);
Packit 7838c8
	    green[i] = SCALE(Colormap.map[1][i]);
Packit 7838c8
	    blue[i] = SCALE(Colormap.map[2][i]);
Packit 7838c8
	}
Packit 7838c8
    }
Packit 7838c8
    if (Verbose)
Packit 7838c8
	fprintf(stderr, "%dx%dx%d image, ", width, height, depth);
Packit 7838c8
Packit 7838c8
    for (row = 0; row < height; row++)
Packit 7838c8
	if (TIFFWriteScanline(tif,
Packit 7838c8
			      (u_char *) mprd_addr(mpr_d(pix), 0, row),
Packit 7838c8
			      row, 0) < 0) {
Packit 7838c8
	    fprintf("failed a scanline write (%d)\n", row);
Packit 7838c8
	    break;
Packit 7838c8
	}
Packit 7838c8
    TIFFFlushData(tif);
Packit 7838c8
    TIFFClose(tif);
Packit 7838c8
Packit 7838c8
    if (Verbose)
Packit 7838c8
	fprintf(stderr, "done.\n");
Packit 7838c8
Packit 7838c8
    pr_destroy(pix);
Packit 7838c8
Packit 7838c8
    exit(0);
Packit 7838c8
}
Packit 7838c8
/*
Packit 7838c8
 * Local Variables:
Packit 7838c8
 * mode: c
Packit 7838c8
 * c-basic-offset: 8
Packit 7838c8
 * fill-column: 78
Packit 7838c8
 * End:
Packit 7838c8
 */