Debian Codegraph @

texk/xdvipsk/thirdparty/libtiff/contrib/dbs/tiff-grayscale.c

raw
1 /*
2 * tiff-grayscale.c -- create a Class G (grayscale) TIFF file
3 * with a gray response curve in linear optical density
4 *
5 * Copyright 1990 by Digital Equipment Corporation, Maynard, Massachusetts.
6 *
7 * All Rights Reserved
8 *
9 * Permission to use, copy, modify, and distribute this software and its
10 * documentation for any purpose and without fee is hereby granted,
11 * provided that the above copyright notice appear in all copies and that
12 * both that copyright notice and this permission notice appear in
13 * supporting documentation, and that the name of Digital not be
14 * used in advertising or publicity pertaining to distribution of the
15 * software without specific, written prior permission.
16 *
17 * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
18 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
19 * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
20 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
22 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
23 * SOFTWARE.
24 */
25
26 #include <math.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "tiffio.h"
32
33 #define WIDTH 512
34 #define HEIGHT WIDTH
35
36 char *programName;
37 void Usage();
38
39 int main(int argc, char **argv)
40 {
41 int bits_per_pixel = 8, cmsize, i, j, k, gray_index, chunk_size = 32,
42 nchunks = 16;
43 unsigned char *scan_line;
44 uint16_t *gray;
45 float refblackwhite[2 * 1];
46 TIFF *tif;
47
48 programName = argv[0];
49
50 if (argc != 4)
51 Usage();
52
53 if (!strcmp(argv[1], "-depth"))
54 bits_per_pixel = atoi(argv[2]);
55 else
56 Usage();
57
58 switch (bits_per_pixel)
59 {
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_t *)malloc(cmsize * sizeof(uint16_t));
78
79 gray[0] = 3000;
80 for (i = 1; i < cmsize; i++)
81 gray[i] = (uint16_t)(-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 {
88 fprintf(stderr, "can't open %s as a TIFF file\n", argv[3]);
89 free(gray);
90 return 0;
91 }
92
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);
104
105 scan_line = (unsigned char *)malloc(WIDTH / (8 / bits_per_pixel));
106
107 for (i = 0; i < HEIGHT; i++)
108 {
109 for (j = 0, k = 0; j < WIDTH;)
110 {
111 gray_index = (j / chunk_size) + ((i / chunk_size) * nchunks);
112
113 switch (bits_per_pixel)
114 {
115 case 8:
116 scan_line[k++] = gray_index;
117 j++;
118 break;
119 case 4:
120 scan_line[k++] = (gray_index << 4) + gray_index;
121 j += 2;
122 break;
123 case 2:
124 scan_line[k++] = (gray_index << 6) + (gray_index << 4) +
125 (gray_index << 2) + gray_index;
126 j += 4;
127 break;
128 }
129 }
130 TIFFWriteScanline(tif, scan_line, i, 0);
131 }
132
133 free(scan_line);
134 TIFFClose(tif);
135 return 0;
136 }
137
138 void Usage()
139 {
140 fprintf(stderr, "Usage: %s -depth (8 | 4 | 2) tiff-image\n", programName);
141 exit(0);
142 }