roentgen b75cab
#ifndef lint
roentgen b75cab
static char id[] = "$Id: tif2ras.c,v 1.3 2010-06-08 18:55:15 bfriesen Exp $"; 
roentgen b75cab
#endif
roentgen b75cab
/*-
roentgen b75cab
 * tif2ras.c - Converts from a Tagged Image File Format image to a Sun Raster.
roentgen b75cab
 *
roentgen b75cab
 * Copyright (c) 1990 by Sun Microsystems, Inc.
roentgen b75cab
 *
roentgen b75cab
 * Author: Patrick J. Naughton
roentgen b75cab
 * naughton@wind.sun.com
roentgen b75cab
 *
roentgen b75cab
 * Permission to use, copy, modify, and distribute this software and its
roentgen b75cab
 * documentation for any purpose and without fee is hereby granted,
roentgen b75cab
 * provided that the above copyright notice appear in all copies and that
roentgen b75cab
 * both that copyright notice and this permission notice appear in
roentgen b75cab
 * supporting documentation.
roentgen b75cab
 *
roentgen b75cab
 * This file is provided AS IS with no warranties of any kind.  The author
roentgen b75cab
 * shall have no liability with respect to the infringement of copyrights,
roentgen b75cab
 * trade secrets or any patents by this file or any part thereof.  In no
roentgen b75cab
 * event will the author be liable for any lost revenue or profits or
roentgen b75cab
 * other special, indirect and consequential damages.
roentgen b75cab
 *
roentgen b75cab
 * Comments and additions should be sent to the author:
roentgen b75cab
 *
roentgen b75cab
 *                     Patrick J. Naughton
roentgen b75cab
 *                     Sun Microsystems
roentgen b75cab
 *                     2550 Garcia Ave, MS 14-40
roentgen b75cab
 *                     Mountain View, CA 94043
roentgen b75cab
 *                     (415) 336-1080
roentgen b75cab
 *
roentgen b75cab
 * Revision History:
roentgen b75cab
 * 10-Jan-89: Created.
roentgen b75cab
 * 06-Mar-90: Change to byte encoded rasterfiles.
roentgen b75cab
 *	      fix bug in call to ReadScanline().
roentgen b75cab
 *	      fix bug in CVT() macro.
roentgen b75cab
 *	      fix assignment of td, (missing &).
roentgen b75cab
 *
roentgen b75cab
 * Description:
roentgen b75cab
 *   This program takes a MicroSoft/Aldus "Tagged Image File Format" image or
roentgen b75cab
 * "TIFF" file as input and writes a Sun Rasterfile [see rasterfile(5)].  The
roentgen b75cab
 * output file may be standard output, but the input TIFF file must be a real
roentgen b75cab
 * file since seek(2) is used.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
#include <stdio.h></stdio.h>
roentgen b75cab
#include <pixrect pixrect_hs.h=""></pixrect>
roentgen b75cab
#include "tiffio.h"
roentgen b75cab
roentgen b75cab
typedef int boolean;
roentgen b75cab
#define True (1)
roentgen b75cab
#define False (0)
roentgen b75cab
#define	CVT(x)		(((x) * 255) / ((1L<<16)-1))
roentgen b75cab
roentgen b75cab
boolean     Verbose = False;
roentgen b75cab
char       *pname;		/* program name (used for error messages) */
roentgen b75cab
roentgen b75cab
void
roentgen b75cab
error(s1, s2)
roentgen b75cab
    char       *s1,
roentgen b75cab
               *s2;
roentgen b75cab
{
roentgen b75cab
    fprintf(stderr, s1, pname, s2);
roentgen b75cab
    exit(1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
void
roentgen b75cab
usage()
roentgen b75cab
{
roentgen b75cab
    error("usage: %s -[vq] TIFFfile [rasterfile]\n", NULL);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
roentgen b75cab
main(argc, argv)
roentgen b75cab
    int         argc;
roentgen b75cab
    char       *argv[];
roentgen b75cab
{
roentgen b75cab
    char       *inf = NULL;
roentgen b75cab
    char       *outf = NULL;
roentgen b75cab
    FILE       *fp;
roentgen b75cab
    long        width,
roentgen b75cab
                height;
roentgen b75cab
    int         depth,
roentgen b75cab
                numcolors;
roentgen b75cab
    register TIFF *tif;
roentgen b75cab
    TIFFDirectory *td;
roentgen b75cab
    register u_char *inp,
roentgen b75cab
               *outp;
roentgen b75cab
    register int col,
roentgen b75cab
                i;
roentgen b75cab
    register long row;
roentgen b75cab
    u_char     *Map = NULL;
roentgen b75cab
    u_char     *buf;
roentgen b75cab
    short	bitspersample;
roentgen b75cab
    short	samplesperpixel;
roentgen b75cab
    short	photometric;
roentgen b75cab
    u_short    *redcolormap,
roentgen b75cab
	       *bluecolormap,
roentgen b75cab
	       *greencolormap;
roentgen b75cab
roentgen b75cab
    Pixrect    *pix;		/* The Sun Pixrect */
roentgen b75cab
    colormap_t  Colormap;	/* The Pixrect Colormap */
roentgen b75cab
    u_char      red[256],
roentgen b75cab
                green[256],
roentgen b75cab
                blue[256];
roentgen b75cab
roentgen b75cab
    setbuf(stderr, NULL);
roentgen b75cab
    pname = argv[0];
roentgen b75cab
roentgen b75cab
    while (--argc) {
roentgen b75cab
	if ((++argv)[0][0] == '-')
roentgen b75cab
	    switch (argv[0][1]) {
roentgen b75cab
	    case 'v':
roentgen b75cab
		Verbose = True;
roentgen b75cab
		break;
roentgen b75cab
	    case 'q':
roentgen b75cab
		usage();
roentgen b75cab
		break;
roentgen b75cab
	    default:
roentgen b75cab
		fprintf(stderr, "%s: illegal option -%c.\n", pname,
roentgen b75cab
			argv[0][1]);
roentgen b75cab
		exit(1);
roentgen b75cab
	    }
roentgen b75cab
	else if (inf == NULL)
roentgen b75cab
	    inf = argv[0];
roentgen b75cab
	else if (outf == NULL)
roentgen b75cab
	    outf = argv[0];
roentgen b75cab
	else
roentgen b75cab
	    usage();
roentgen b75cab
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    if (inf == NULL)
roentgen b75cab
	error("%s: can't read input file from a stream.\n", NULL);
roentgen b75cab
roentgen b75cab
    if (Verbose)
roentgen b75cab
	fprintf(stderr, "Reading %s...", inf);
roentgen b75cab
roentgen b75cab
    tif = TIFFOpen(inf, "r");
roentgen b75cab
roentgen b75cab
    if (tif == NULL)
roentgen b75cab
	error("%s: error opening TIFF file %s", inf);
roentgen b75cab
roentgen b75cab
    if (Verbose)
roentgen b75cab
	TIFFPrintDirectory(tif, stderr, True, False, False);
roentgen b75cab
    TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bitspersample);
roentgen b75cab
    if (bitspersample > 8)
roentgen b75cab
	error("%s: can't handle more than 8-bits per sample\n", NULL);
roentgen b75cab
roentgen b75cab
    TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
roentgen b75cab
    switch (samplesperpixel) {
roentgen b75cab
    case 1:
roentgen b75cab
	if (bitspersample == 1)
roentgen b75cab
	    depth = 1;
roentgen b75cab
	else
roentgen b75cab
	    depth = 8;
roentgen b75cab
	break;
roentgen b75cab
    case 3:
roentgen b75cab
    case 4:
roentgen b75cab
	depth = 24;
roentgen b75cab
	break;
roentgen b75cab
    default:
roentgen b75cab
	error("%s: only handle 1-channel gray scale or 3-channel color\n");
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
roentgen b75cab
    TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
roentgen b75cab
roentgen b75cab
    if (Verbose)
roentgen b75cab
	fprintf(stderr, "%dx%dx%d image, ", width, height, depth);
roentgen b75cab
    if (Verbose)
roentgen b75cab
	fprintf(stderr, "%d bits/sample, %d samples/pixel, ",
roentgen b75cab
		bitspersample, samplesperpixel);
roentgen b75cab
roentgen b75cab
    pix = mem_create(width, height, depth);
roentgen b75cab
    if (pix == (Pixrect *) NULL)
roentgen b75cab
	error("%s: can't allocate memory for output pixrect...\n", NULL);
roentgen b75cab
roentgen b75cab
    numcolors = (1 << bitspersample);
roentgen b75cab
roentgen b75cab
    TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric);
roentgen b75cab
    if (numcolors == 2) {
roentgen b75cab
	if (Verbose)
roentgen b75cab
	    fprintf(stderr, "monochrome ");
roentgen b75cab
	Colormap.type = RMT_NONE;
roentgen b75cab
	Colormap.length = 0;
roentgen b75cab
	Colormap.map[0] = Colormap.map[1] = Colormap.map[2] = NULL;
roentgen b75cab
    } else {
roentgen b75cab
	switch (photometric) {
roentgen b75cab
	case PHOTOMETRIC_MINISBLACK:
roentgen b75cab
	    if (Verbose)
roentgen b75cab
		fprintf(stderr, "%d graylevels (min=black), ", numcolors);
roentgen b75cab
	    Map = (u_char *) malloc(numcolors * sizeof(u_char));
roentgen b75cab
	    for (i = 0; i < numcolors; i++)
roentgen b75cab
		Map[i] = (255 * i) / numcolors;
roentgen b75cab
	    Colormap.type = RMT_EQUAL_RGB;
roentgen b75cab
	    Colormap.length = numcolors;
roentgen b75cab
	    Colormap.map[0] = Colormap.map[1] = Colormap.map[2] = Map;
roentgen b75cab
	    break;
roentgen b75cab
	case PHOTOMETRIC_MINISWHITE:
roentgen b75cab
	    if (Verbose)
roentgen b75cab
		fprintf(stderr, "%d graylevels (min=white), ", numcolors);
roentgen b75cab
	    Map = (u_char *) malloc(numcolors * sizeof(u_char));
roentgen b75cab
	    for (i = 0; i < numcolors; i++)
roentgen b75cab
		Map[i] = 255 - ((255 * i) / numcolors);
roentgen b75cab
	    Colormap.type = RMT_EQUAL_RGB;
roentgen b75cab
	    Colormap.length = numcolors;
roentgen b75cab
	    Colormap.map[0] = Colormap.map[1] = Colormap.map[2] = Map;
roentgen b75cab
	    break;
roentgen b75cab
	case PHOTOMETRIC_RGB:
roentgen b75cab
	    if (Verbose)
roentgen b75cab
		fprintf(stderr, "truecolor ");
roentgen b75cab
	    Colormap.type = RMT_NONE;
roentgen b75cab
	    Colormap.length = 0;
roentgen b75cab
	    Colormap.map[0] = Colormap.map[1] = Colormap.map[2] = NULL;
roentgen b75cab
	    break;
roentgen b75cab
	case PHOTOMETRIC_PALETTE:
roentgen b75cab
	    if (Verbose)
roentgen b75cab
		fprintf(stderr, "colormapped ");
roentgen b75cab
	    Colormap.type = RMT_EQUAL_RGB;
roentgen b75cab
	    Colormap.length = numcolors;
roentgen b75cab
	    memset(red, 0, sizeof(red));
roentgen b75cab
	    memset(green, 0, sizeof(green));
roentgen b75cab
	    memset(blue, 0, sizeof(blue));
roentgen b75cab
	    TIFFGetField(tif, TIFFTAG_COLORMAP,
roentgen b75cab
		&redcolormap, &greencolormap, &bluecolormap);
roentgen b75cab
	    for (i = 0; i < numcolors; i++) {
roentgen b75cab
		red[i] = (u_char) CVT(redcolormap[i]);
roentgen b75cab
		green[i] = (u_char) CVT(greencolormap[i]);
roentgen b75cab
		blue[i] = (u_char) CVT(bluecolormap[i]);
roentgen b75cab
	    }
roentgen b75cab
	    Colormap.map[0] = red;
roentgen b75cab
	    Colormap.map[1] = green;
roentgen b75cab
	    Colormap.map[2] = blue;
roentgen b75cab
	    break;
roentgen b75cab
	case PHOTOMETRIC_MASK:
roentgen b75cab
	    error("%s: Don't know how to handle PHOTOMETRIC_MASK\n");
roentgen b75cab
	    break;
roentgen b75cab
	case PHOTOMETRIC_DEPTH:
roentgen b75cab
	    error("%s: Don't know how to handle PHOTOMETRIC_DEPTH\n");
roentgen b75cab
	    break;
roentgen b75cab
	default:
roentgen b75cab
	    error("%s: unknown photometric (cmap): %d\n", photometric);
roentgen b75cab
	}
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    buf = (u_char *) malloc(TIFFScanlineSize(tif));
roentgen b75cab
    if (buf == NULL)
roentgen b75cab
	error("%s: can't allocate memory for scanline buffer...\n", NULL);
roentgen b75cab
roentgen b75cab
    for (row = 0; row < height; row++) {
roentgen b75cab
	if (TIFFReadScanline(tif, buf, row, 0) < 0)
roentgen b75cab
	    error("%s: bad data read on line: %d\n", row);
roentgen b75cab
	inp = buf;
roentgen b75cab
	outp = (u_char *) mprd_addr(mpr_d(pix), 0, row);
roentgen b75cab
	switch (photometric) {
roentgen b75cab
	case PHOTOMETRIC_RGB:
roentgen b75cab
	    if (samplesperpixel == 4)
roentgen b75cab
		for (col = 0; col < width; col++) {
roentgen b75cab
		    *outp++ = *inp++;	/* Blue */
roentgen b75cab
		    *outp++ = *inp++;	/* Green */
roentgen b75cab
		    *outp++ = *inp++;	/* Red */
roentgen b75cab
		    inp++;	/* skip alpha channel */
roentgen b75cab
		}
roentgen b75cab
	    else
roentgen b75cab
		for (col = 0; col < width; col++) {
roentgen b75cab
		    *outp++ = *inp++;	/* Blue */
roentgen b75cab
		    *outp++ = *inp++;	/* Green */
roentgen b75cab
		    *outp++ = *inp++;	/* Red */
roentgen b75cab
		}
roentgen b75cab
	    break;
roentgen b75cab
	case PHOTOMETRIC_MINISWHITE:
roentgen b75cab
	case PHOTOMETRIC_MINISBLACK:
roentgen b75cab
	    switch (bitspersample) {
roentgen b75cab
	    case 1:
roentgen b75cab
		for (col = 0; col < ((width + 7) / 8); col++)
roentgen b75cab
		    *outp++ = *inp++;
roentgen b75cab
		break;
roentgen b75cab
	    case 2:
roentgen b75cab
		for (col = 0; col < ((width + 3) / 4); col++) {
roentgen b75cab
		    *outp++ = (*inp >> 6) & 3;
roentgen b75cab
		    *outp++ = (*inp >> 4) & 3;
roentgen b75cab
		    *outp++ = (*inp >> 2) & 3;
roentgen b75cab
		    *outp++ = *inp++ & 3;
roentgen b75cab
		}
roentgen b75cab
		break;
roentgen b75cab
	    case 4:
roentgen b75cab
		for (col = 0; col < width / 2; col++) {
roentgen b75cab
		    *outp++ = *inp >> 4;
roentgen b75cab
		    *outp++ = *inp++ & 0xf;
roentgen b75cab
		}
roentgen b75cab
		break;
roentgen b75cab
	    case 8:
roentgen b75cab
		for (col = 0; col < width; col++)
roentgen b75cab
		    *outp++ = *inp++;
roentgen b75cab
		break;
roentgen b75cab
	    default:
roentgen b75cab
		error("%s: bad bits/sample: %d\n", bitspersample);
roentgen b75cab
	    }
roentgen b75cab
	    break;
roentgen b75cab
	case PHOTOMETRIC_PALETTE:
roentgen b75cab
	    memcpy(outp, inp, width);
roentgen b75cab
	    break;
roentgen b75cab
	default:
roentgen b75cab
	    error("%s: unknown photometric (write): %d\n", photometric);
roentgen b75cab
	}
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    free((char *) buf);
roentgen b75cab
roentgen b75cab
    if (Verbose)
roentgen b75cab
	fprintf(stderr, "done.\n");
roentgen b75cab
roentgen b75cab
    if (outf == NULL || strcmp(outf, "Standard Output") == 0) {
roentgen b75cab
	outf = "Standard Output";
roentgen b75cab
	fp = stdout;
roentgen b75cab
    } else {
roentgen b75cab
	if (!(fp = fopen(outf, "w")))
roentgen b75cab
	    error("%s: %s couldn't be opened for writing.\n", outf);
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    if (Verbose)
roentgen b75cab
	fprintf(stderr, "Writing rasterfile in %s...", outf);
roentgen b75cab
roentgen b75cab
    if (pr_dump(pix, fp, &Colormap, RT_BYTE_ENCODED, 0) == PIX_ERR)
roentgen b75cab
	error("%s: error writing Sun Rasterfile: %s\n", outf);
roentgen b75cab
roentgen b75cab
    if (Verbose)
roentgen b75cab
	fprintf(stderr, "done.\n");
roentgen b75cab
roentgen b75cab
    pr_destroy(pix);
roentgen b75cab
roentgen b75cab
    if (fp != stdout)
roentgen b75cab
	fclose(fp);
roentgen b75cab
roentgen b75cab
    exit(0);
roentgen b75cab
}
roentgen b75cab
/*
roentgen b75cab
 * Local Variables:
roentgen b75cab
 * mode: c
roentgen b75cab
 * c-basic-offset: 8
roentgen b75cab
 * fill-column: 78
roentgen b75cab
 * End:
roentgen b75cab
 */