roentgen b75cab
/* $Id: tiff-grayscale.c,v 1.6 2010-06-08 18:55:15 bfriesen Exp $ */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * tiff-grayscale.c -- create a Class G (grayscale) TIFF file
roentgen b75cab
 *      with a gray response curve in linear optical density
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 WIDTH       512
roentgen b75cab
#define HEIGHT      WIDTH
roentgen b75cab
roentgen b75cab
char *              programName;
roentgen b75cab
void                Usage();
roentgen b75cab
roentgen b75cab
int main(int argc, char **argv)
roentgen b75cab
{
roentgen b75cab
    int             bits_per_pixel = 8, cmsize, i, j, k,
roentgen b75cab
                    gray_index, chunk_size = 32, nchunks = 16;
roentgen b75cab
    unsigned char * scan_line;
roentgen b75cab
    uint16 *        gray;
roentgen b75cab
    float           refblackwhite[2*1];
roentgen b75cab
    TIFF *          tif;
roentgen b75cab
roentgen b75cab
    programName = argv[0];
roentgen b75cab
roentgen b75cab
    if (argc != 4)
roentgen b75cab
        Usage();
roentgen b75cab
roentgen b75cab
    if (!strcmp(argv[1], "-depth"))
roentgen b75cab
         bits_per_pixel = atoi(argv[2]);
roentgen b75cab
    else
roentgen b75cab
         Usage();
roentgen b75cab
roentgen b75cab
    switch (bits_per_pixel) {
roentgen b75cab
        case 8:
roentgen b75cab
            nchunks = 16;
roentgen b75cab
            chunk_size = 32;
roentgen b75cab
            break;
roentgen b75cab
        case 4:
roentgen b75cab
            nchunks = 4;
roentgen b75cab
            chunk_size = 128;
roentgen b75cab
            break;
roentgen b75cab
        case 2:
roentgen b75cab
            nchunks = 2;
roentgen b75cab
            chunk_size = 256;
roentgen b75cab
            break;
roentgen b75cab
        default:
roentgen b75cab
            Usage();
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    cmsize = nchunks * nchunks;
roentgen b75cab
    gray = (uint16 *) malloc(cmsize * sizeof(uint16));
roentgen b75cab
roentgen b75cab
    gray[0] = 3000;
roentgen b75cab
    for (i = 1; i < cmsize; i++)
roentgen b75cab
        gray[i] = (uint16) (-log10((double) i / (cmsize - 1)) * 1000);
roentgen b75cab
roentgen b75cab
    refblackwhite[0] = 0.0;
roentgen b75cab
    refblackwhite[1] = (float)((1L<
roentgen b75cab
roentgen b75cab
    if ((tif = TIFFOpen(argv[3], "w")) == NULL) {
roentgen b75cab
        fprintf(stderr, "can't open %s as a TIFF file\n", argv[3]);
roentgen b75cab
		free(gray);
roentgen b75cab
        return 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, bits_per_pixel);
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_REFERENCEBLACKWHITE, refblackwhite);
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_TRANSFERFUNCTION, gray);
roentgen b75cab
    TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_NONE);
roentgen b75cab
roentgen b75cab
    scan_line = (unsigned char *) malloc(WIDTH / (8 / bits_per_pixel));
roentgen b75cab
roentgen b75cab
    for (i = 0; i < HEIGHT; i++) {
roentgen b75cab
        for (j = 0, k = 0; j < WIDTH;) {
roentgen b75cab
            gray_index = (j / chunk_size) + ((i / chunk_size) * nchunks);
roentgen b75cab
roentgen b75cab
            switch (bits_per_pixel) {
roentgen b75cab
            case 8:
roentgen b75cab
                scan_line[k++] = gray_index;
roentgen b75cab
                j++;
roentgen b75cab
                break;
roentgen b75cab
            case 4:
roentgen b75cab
                scan_line[k++] = (gray_index << 4) + gray_index;
roentgen b75cab
                j += 2;
roentgen b75cab
                break;
roentgen b75cab
            case 2:
roentgen b75cab
                scan_line[k++] = (gray_index << 6) + (gray_index << 4)
roentgen b75cab
                    + (gray_index << 2) + gray_index;
roentgen b75cab
                j += 4;
roentgen b75cab
                break;
roentgen b75cab
            }
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
    return 0;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
void
roentgen b75cab
Usage()
roentgen b75cab
{
roentgen b75cab
    fprintf(stderr, "Usage: %s -depth (8 | 4 | 2) 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
 */