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