1 /* $Id: tiff-grayscale.c,v 1.6 2010-06-08 18:55:15 bfriesen Exp $ */
4 * tiff-grayscale.c -- create a Class G (grayscale) TIFF file
5 * with a gray response curve in linear optical density
7 * Copyright 1990 by Digital Equipment Corporation, Maynard, Massachusetts.
11 * Permission to use, copy, modify, and distribute this software and its
12 * documentation for any purpose and without fee is hereby granted,
13 * provided that the above copyright notice appear in all copies and that
14 * both that copyright notice and this permission notice appear in
15 * supporting documentation, and that the name of Digital not be
16 * used in advertising or publicity pertaining to distribution of the
17 * software without specific, written prior permission.
19 * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
20 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
21 * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
22 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
23 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
41 int main(int argc, char **argv)
43 int bits_per_pixel = 8, cmsize, i, j, k,
44 gray_index, chunk_size = 32, nchunks = 16;
45 unsigned char * scan_line;
47 float refblackwhite[2*1];
50 programName = argv[0];
55 if (!strcmp(argv[1], "-depth"))
56 bits_per_pixel = atoi(argv[2]);
60 switch (bits_per_pixel) {
77 cmsize = nchunks * nchunks;
78 gray = (uint16 *) malloc(cmsize * sizeof(uint16));
81 for (i = 1; i < cmsize; i++)
82 gray[i] = (uint16) (-log10((double) i / (cmsize - 1)) * 1000);
84 refblackwhite[0] = 0.0;
85 refblackwhite[1] = (float)((1L<<bits_per_pixel) - 1);
87 if ((tif = TIFFOpen(argv[3], "w")) == NULL) {
88 fprintf(stderr, "can't open %s as a TIFF file\n", argv[3]);
93 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, WIDTH);
94 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, HEIGHT);
95 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bits_per_pixel);
96 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
97 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
98 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
99 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
100 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
101 TIFFSetField(tif, TIFFTAG_REFERENCEBLACKWHITE, refblackwhite);
102 TIFFSetField(tif, TIFFTAG_TRANSFERFUNCTION, gray);
103 TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_NONE);
105 scan_line = (unsigned char *) malloc(WIDTH / (8 / bits_per_pixel));
107 for (i = 0; i < HEIGHT; i++) {
108 for (j = 0, k = 0; j < WIDTH;) {
109 gray_index = (j / chunk_size) + ((i / chunk_size) * nchunks);
111 switch (bits_per_pixel) {
113 scan_line[k++] = gray_index;
117 scan_line[k++] = (gray_index << 4) + gray_index;
121 scan_line[k++] = (gray_index << 6) + (gray_index << 4)
122 + (gray_index << 2) + gray_index;
127 TIFFWriteScanline(tif, scan_line, i, 0);
138 fprintf(stderr, "Usage: %s -depth (8 | 4 | 2) tiff-image\n", programName);