Remove all lines containing cvs/svn "$Id$" keyword.
[wxWidgets.git] / src / tiff / contrib / dbs / tiff-grayscale.c
1
2 /*
3 * tiff-grayscale.c -- create a Class G (grayscale) TIFF file
4 * with a gray response curve in linear optical density
5 *
6 * Copyright 1990 by Digital Equipment Corporation, Maynard, Massachusetts.
7 *
8 * All Rights Reserved
9 *
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.
17 *
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
24 * SOFTWARE.
25 */
26
27 #include <math.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "tiffio.h"
33
34 #define WIDTH 512
35 #define HEIGHT WIDTH
36
37 char * programName;
38 void Usage();
39
40 int main(int argc, char **argv)
41 {
42 int bits_per_pixel = 8, cmsize, i, j, k,
43 gray_index, chunk_size = 32, nchunks = 16;
44 unsigned char * scan_line;
45 uint16 * gray;
46 float refblackwhite[2*1];
47 TIFF * tif;
48
49 programName = argv[0];
50
51 if (argc != 4)
52 Usage();
53
54 if (!strcmp(argv[1], "-depth"))
55 bits_per_pixel = atoi(argv[2]);
56 else
57 Usage();
58
59 switch (bits_per_pixel) {
60 case 8:
61 nchunks = 16;
62 chunk_size = 32;
63 break;
64 case 4:
65 nchunks = 4;
66 chunk_size = 128;
67 break;
68 case 2:
69 nchunks = 2;
70 chunk_size = 256;
71 break;
72 default:
73 Usage();
74 }
75
76 cmsize = nchunks * nchunks;
77 gray = (uint16 *) malloc(cmsize * sizeof(uint16));
78
79 gray[0] = 3000;
80 for (i = 1; i < cmsize; i++)
81 gray[i] = (uint16) (-log10((double) i / (cmsize - 1)) * 1000);
82
83 refblackwhite[0] = 0.0;
84 refblackwhite[1] = (float)((1L<<bits_per_pixel) - 1);
85
86 if ((tif = TIFFOpen(argv[3], "w")) == NULL) {
87 fprintf(stderr, "can't open %s as a TIFF file\n", argv[3]);
88 free(gray);
89 return 0;
90 }
91
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);
103
104 scan_line = (unsigned char *) malloc(WIDTH / (8 / bits_per_pixel));
105
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);
109
110 switch (bits_per_pixel) {
111 case 8:
112 scan_line[k++] = gray_index;
113 j++;
114 break;
115 case 4:
116 scan_line[k++] = (gray_index << 4) + gray_index;
117 j += 2;
118 break;
119 case 2:
120 scan_line[k++] = (gray_index << 6) + (gray_index << 4)
121 + (gray_index << 2) + gray_index;
122 j += 4;
123 break;
124 }
125 }
126 TIFFWriteScanline(tif, scan_line, i, 0);
127 }
128
129 free(scan_line);
130 TIFFClose(tif);
131 return 0;
132 }
133
134 void
135 Usage()
136 {
137 fprintf(stderr, "Usage: %s -depth (8 | 4 | 2) tiff-image\n", programName);
138 exit(0);
139 }
140 /*
141 * Local Variables:
142 * mode: c
143 * c-basic-offset: 8
144 * fill-column: 78
145 * End:
146 */