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