roentgen b75cab
/* $Id: tiff-rgb.c,v 1.4 2010-06-08 18:55:15 bfriesen Exp $ */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * tiff-rgb.c -- create a 24-bit Class R (rgb) TIFF file
roentgen b75cab
 *
roentgen b75cab
 * Copyright 1990 by Digital Equipment Corporation, Maynard, Massachusetts.
roentgen b75cab
 *
roentgen b75cab
 *                        All Rights Reserved
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, and that the name of Digital not be
roentgen b75cab
 * used in advertising or publicity pertaining to distribution of the
roentgen b75cab
 * software without specific, written prior permission.
roentgen b75cab
 *
roentgen b75cab
 * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
roentgen b75cab
 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
roentgen b75cab
 * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
roentgen b75cab
 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
roentgen b75cab
 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
roentgen b75cab
 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
roentgen b75cab
 * SOFTWARE.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
#include <math.h></math.h>
roentgen b75cab
#include <stdio.h></stdio.h>
roentgen b75cab
#include <stdlib.h></stdlib.h>
roentgen b75cab
#include <string.h></string.h>
roentgen b75cab
roentgen b75cab
#include "tiffio.h"
roentgen b75cab
roentgen b75cab
#define ROUND(x)    (uint16) ((x) + 0.5)
roentgen b75cab
#define CMSIZE      256
roentgen b75cab
#define WIDTH       525
roentgen b75cab
#define HEIGHT      512
roentgen b75cab
#define TIFF_GAMMA  2.2
roentgen b75cab
roentgen b75cab
void                Usage();
roentgen b75cab
char *              programName;
roentgen b75cab
roentgen b75cab
int main(int argc, char **argv)
roentgen b75cab
{
roentgen b75cab
    char *          input_file = NULL;
roentgen b75cab
    double          image_gamma = TIFF_GAMMA;
roentgen b75cab
    int             i, j;
roentgen b75cab
    TIFF *          tif;
roentgen b75cab
    unsigned char * scan_line;
roentgen b75cab
    uint16          red[CMSIZE], green[CMSIZE], blue[CMSIZE];
roentgen b75cab
    float	    refblackwhite[2*3];
roentgen b75cab
roentgen b75cab
    programName = argv[0];
roentgen b75cab
roentgen b75cab
    switch (argc) {
roentgen b75cab
    case 2:
roentgen b75cab
        image_gamma = TIFF_GAMMA;
roentgen b75cab
        input_file = argv[1];
roentgen b75cab
        break;
roentgen b75cab
    case 4:
roentgen b75cab
        if (!strcmp(argv[1], "-gamma")) {
roentgen b75cab
            image_gamma = atof(argv[2]);
roentgen b75cab
            input_file = argv[3];
roentgen b75cab
        } else
roentgen b75cab
            Usage();
roentgen b75cab
        break;
roentgen b75cab
    default:
roentgen b75cab
        Usage();
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    for (i = 0; i < CMSIZE; i++) {
roentgen b75cab
        if (i == 0)
roentgen b75cab
            red[i] = green[i] = blue[i] = 0;
roentgen b75cab
        else {
roentgen b75cab
            red[i] = ROUND((pow(i / 255.0, 1.0 / image_gamma) * 65535.0));
roentgen b75cab
            green[i] = ROUND((pow(i / 255.0, 1.0 / image_gamma) * 65535.0));
roentgen b75cab
            blue[i] = ROUND((pow(i / 255.0, 1.0 / image_gamma) * 65535.0));
roentgen b75cab
        }
roentgen b75cab
    }
roentgen b75cab
    refblackwhite[0] = 0.0; refblackwhite[1] = 255.0;
roentgen b75cab
    refblackwhite[2] = 0.0; refblackwhite[3] = 255.0;
roentgen b75cab
    refblackwhite[4] = 0.0; refblackwhite[5] = 255.0;
roentgen b75cab
roentgen b75cab
    if ((tif = TIFFOpen(input_file, "w")) == NULL) {
roentgen b75cab
        fprintf(stderr, "can't open %s as a TIFF file\n", input_file);
roentgen b75cab
        exit(0);
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, WIDTH);
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_IMAGELENGTH, HEIGHT);
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_NONE);
roentgen b75cab
#ifdef notdef
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_WHITEPOINT, whitex, whitey);
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_PRIMARYCHROMATICITIES, primaries);
roentgen b75cab
#endif
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_REFERENCEBLACKWHITE, refblackwhite);
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_TRANSFERFUNCTION, red, green, blue);
roentgen b75cab
roentgen b75cab
    scan_line = (unsigned char *) malloc(WIDTH * 3);
roentgen b75cab
roentgen b75cab
    for (i = 0; i < 255; i++) {
roentgen b75cab
        for (j = 0; j < 75; j++) {
roentgen b75cab
             scan_line[j * 3] = 255;
roentgen b75cab
             scan_line[(j * 3) + 1] = 255 - i;
roentgen b75cab
             scan_line[(j * 3) + 2] = 255 - i;
roentgen b75cab
        }
roentgen b75cab
        for (j = 75; j < 150; j++) {
roentgen b75cab
             scan_line[j * 3] = 255 - i;
roentgen b75cab
             scan_line[(j * 3) + 1] = 255;
roentgen b75cab
             scan_line[(j * 3) + 2] = 255 - i;
roentgen b75cab
        }
roentgen b75cab
        for (j = 150; j < 225; j++) {
roentgen b75cab
             scan_line[j * 3] = 255 - i;
roentgen b75cab
             scan_line[(j * 3) + 1] = 255 - i;
roentgen b75cab
             scan_line[(j * 3) + 2] = 255;
roentgen b75cab
        }
roentgen b75cab
        for (j = 225; j < 300; j++) {
roentgen b75cab
             scan_line[j * 3] = (i - 1) / 2;
roentgen b75cab
             scan_line[(j * 3) + 1] = (i - 1) / 2;
roentgen b75cab
             scan_line[(j * 3) + 2] = (i - 1) / 2;
roentgen b75cab
        }
roentgen b75cab
        for (j = 300; j < 375; j++) {
roentgen b75cab
             scan_line[j * 3] = 255 - i;
roentgen b75cab
             scan_line[(j * 3) + 1] = 255;
roentgen b75cab
             scan_line[(j * 3) + 2] = 255;
roentgen b75cab
        }
roentgen b75cab
        for (j = 375; j < 450; j++) {
roentgen b75cab
             scan_line[j * 3] = 255;
roentgen b75cab
             scan_line[(j * 3) + 1] = 255 - i;
roentgen b75cab
             scan_line[(j * 3) + 2] = 255;
roentgen b75cab
        }
roentgen b75cab
        for (j = 450; j < 525; j++) {
roentgen b75cab
             scan_line[j * 3] = 255;
roentgen b75cab
             scan_line[(j * 3) + 1] = 255;
roentgen b75cab
             scan_line[(j * 3) + 2] = 255 - i;
roentgen b75cab
        }
roentgen b75cab
        TIFFWriteScanline(tif, scan_line, i, 0);
roentgen b75cab
    }
roentgen b75cab
    for (i = 255; i < 512; i++) {
roentgen b75cab
        for (j = 0; j < 75; j++) {
roentgen b75cab
             scan_line[j * 3] = i;
roentgen b75cab
             scan_line[(j * 3) + 1] = 0;
roentgen b75cab
             scan_line[(j * 3) + 2] = 0;
roentgen b75cab
        }
roentgen b75cab
        for (j = 75; j < 150; j++) {
roentgen b75cab
             scan_line[j * 3] = 0;
roentgen b75cab
             scan_line[(j * 3) + 1] = i;
roentgen b75cab
             scan_line[(j * 3) + 2] = 0;
roentgen b75cab
        }
roentgen b75cab
        for (j = 150; j < 225; j++) {
roentgen b75cab
             scan_line[j * 3] = 0;
roentgen b75cab
             scan_line[(j * 3) + 1] = 0;
roentgen b75cab
             scan_line[(j * 3) + 2] = i;
roentgen b75cab
        }
roentgen b75cab
        for (j = 225; j < 300; j++) {
roentgen b75cab
             scan_line[j * 3] = (i - 1) / 2;
roentgen b75cab
             scan_line[(j * 3) + 1] = (i - 1) / 2;
roentgen b75cab
             scan_line[(j * 3) + 2] = (i - 1) / 2;
roentgen b75cab
        }
roentgen b75cab
        for (j = 300; j < 375; j++) {
roentgen b75cab
             scan_line[j * 3] = 0;
roentgen b75cab
             scan_line[(j * 3) + 1] = i;
roentgen b75cab
             scan_line[(j * 3) + 2] = i;
roentgen b75cab
        }
roentgen b75cab
        for (j = 375; j < 450; j++) {
roentgen b75cab
             scan_line[j * 3] = i;
roentgen b75cab
             scan_line[(j * 3) + 1] = 0;
roentgen b75cab
             scan_line[(j * 3) + 2] = i;
roentgen b75cab
        }
roentgen b75cab
        for (j = 450; j < 525; j++) {
roentgen b75cab
             scan_line[j * 3] = i;
roentgen b75cab
             scan_line[(j * 3) + 1] = i;
roentgen b75cab
             scan_line[(j * 3) + 2] = 0;
roentgen b75cab
        }
roentgen b75cab
        TIFFWriteScanline(tif, scan_line, i, 0);
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    free(scan_line);
roentgen b75cab
    TIFFClose(tif);
roentgen b75cab
    exit(0);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
void
roentgen b75cab
Usage()
roentgen b75cab
{
roentgen b75cab
    fprintf(stderr, "Usage: %s -gamma gamma tiff-image\n", programName);
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
 */