Debian Codegraph @

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

raw
1 /*
2 * tiff-palette.c -- create a Class P (palette) TIFF file
3 *
4 * Copyright 1990 by Digital Equipment Corporation, Maynard, Massachusetts.
5 *
6 * All Rights Reserved
7 *
8 * Permission to use, copy, modify, and distribute this software and its
9 * documentation for any purpose and without fee is hereby granted,
10 * provided that the above copyright notice appear in all copies and that
11 * both that copyright notice and this permission notice appear in
12 * supporting documentation, and that the name of Digital not be
13 * used in advertising or publicity pertaining to distribution of the
14 * software without specific, written prior permission.
15 *
16 * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
17 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
18 * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
19 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
21 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
22 * SOFTWARE.
23 */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "tiffio.h"
30
31 #define WIDTH 512
32 #define HEIGHT WIDTH
33 #define SCALE(x) ((x)*257L)
34
35 char *programName;
36 void Usage();
37
38 int main(int argc, char **argv)
39 {
40 int bits_per_pixel = 8, cmsize, i, j, k, cmap_index, chunk_size = 32,
41 nchunks = 16;
42 unsigned char *scan_line;
43 uint16_t *red, *green, *blue;
44 TIFF *tif;
45
46 programName = argv[0];
47
48 if (argc != 4)
49 Usage();
50
51 if (!strcmp(argv[1], "-depth"))
52 bits_per_pixel = atoi(argv[2]);
53 else
54 Usage();
55
56 switch (bits_per_pixel)
57 {
58 case 8:
59 nchunks = 16;
60 chunk_size = 32;
61 break;
62 case 4:
63 nchunks = 4;
64 chunk_size = 128;
65 break;
66 case 2:
67 nchunks = 2;
68 chunk_size = 256;
69 break;
70 case 1:
71 nchunks = 2;
72 chunk_size = 256;
73 break;
74 default:
75 Usage();
76 }
77
78 if (bits_per_pixel != 1)
79 {
80 cmsize = nchunks * nchunks;
81 }
82 else
83 {
84 cmsize = 2;
85 }
86 red = (uint16_t *)malloc(cmsize * sizeof(uint16_t));
87 green = (uint16_t *)malloc(cmsize * sizeof(uint16_t));
88 blue = (uint16_t *)malloc(cmsize * sizeof(uint16_t));
89
90 switch (bits_per_pixel)
91 {
92 case 8:
93 for (i = 0; i < cmsize; i++)
94 {
95 if (i < 32)
96 red[i] = 0;
97 else if (i < 64)
98 red[i] = SCALE(36);
99 else if (i < 96)
100 red[i] = SCALE(73);
101 else if (i < 128)
102 red[i] = SCALE(109);
103 else if (i < 160)
104 red[i] = SCALE(146);
105 else if (i < 192)
106 red[i] = SCALE(182);
107 else if (i < 224)
108 red[i] = SCALE(219);
109 else if (i < 256)
110 red[i] = SCALE(255);
111
112 if ((i % 32) < 4)
113 green[i] = 0;
114 else if (i < 8)
115 green[i] = SCALE(36);
116 else if ((i % 32) < 12)
117 green[i] = SCALE(73);
118 else if ((i % 32) < 16)
119 green[i] = SCALE(109);
120 else if ((i % 32) < 20)
121 green[i] = SCALE(146);
122 else if ((i % 32) < 24)
123 green[i] = SCALE(182);
124 else if ((i % 32) < 28)
125 green[i] = SCALE(219);
126 else if ((i % 32) < 32)
127 green[i] = SCALE(255);
128
129 if ((i % 4) == 0)
130 blue[i] = SCALE(0);
131 else if ((i % 4) == 1)
132 blue[i] = SCALE(85);
133 else if ((i % 4) == 2)
134 blue[i] = SCALE(170);
135 else if ((i % 4) == 3)
136 blue[i] = SCALE(255);
137 }
138 break;
139 case 4:
140 red[0] = SCALE(255);
141 green[0] = 0;
142 blue[0] = 0;
143
144 red[1] = 0;
145 green[1] = SCALE(255);
146 blue[1] = 0;
147
148 red[2] = 0;
149 green[2] = 0;
150 blue[2] = SCALE(255);
151
152 red[3] = SCALE(255);
153 green[3] = SCALE(255);
154 blue[3] = SCALE(255);
155
156 red[4] = 0;
157 green[4] = SCALE(255);
158 blue[4] = SCALE(255);
159
160 red[5] = SCALE(255);
161 green[5] = 0;
162 blue[5] = SCALE(255);
163
164 red[6] = SCALE(255);
165 green[6] = SCALE(255);
166 blue[6] = 0;
167
168 red[7] = 0;
169 green[7] = 0;
170 blue[7] = 0;
171
172 red[8] = SCALE(176);
173 green[8] = SCALE(224);
174 blue[8] = SCALE(230);
175 red[9] = SCALE(100);
176 green[9] = SCALE(149);
177 blue[9] = SCALE(237);
178 red[10] = SCALE(46);
179 green[10] = SCALE(139);
180 blue[10] = SCALE(87);
181 red[11] = SCALE(160);
182 green[11] = SCALE(82);
183 blue[11] = SCALE(45);
184 red[12] = SCALE(238);
185 green[12] = SCALE(130);
186 blue[12] = SCALE(238);
187 red[13] = SCALE(176);
188 green[13] = SCALE(48);
189 blue[13] = SCALE(96);
190 red[14] = SCALE(50);
191 green[14] = SCALE(205);
192 blue[14] = SCALE(50);
193 red[15] = SCALE(240);
194 green[15] = SCALE(152);
195 blue[15] = SCALE(35);
196 break;
197 case 2:
198 red[0] = SCALE(255);
199 green[0] = 0;
200 blue[0] = 0;
201
202 red[1] = 0;
203 green[1] = SCALE(255);
204 blue[1] = 0;
205
206 red[2] = 0;
207 green[2] = 0;
208 blue[2] = SCALE(255);
209 red[3] = SCALE(255);
210 green[3] = SCALE(255);
211 blue[3] = SCALE(255);
212 break;
213 case 1:
214 red[0] = 0;
215 green[0] = 0;
216 blue[0] = 0;
217
218 red[1] = SCALE(255);
219 green[1] = SCALE(255);
220 blue[1] = SCALE(255);
221 break;
222 }
223
224 if ((tif = TIFFOpen(argv[3], "w")) == NULL)
225 {
226 fprintf(stderr, "can't open %s as a TIFF file\n", argv[3]);
227 free(red);
228 free(green);
229 free(blue);
230 return 0;
231 }
232
233 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, WIDTH);
234 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, HEIGHT);
235 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bits_per_pixel);
236 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
237 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
238 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
239 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
240 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
241 TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_NONE);
242 TIFFSetField(tif, TIFFTAG_COLORMAP, red, green, blue);
243
244 scan_line = (unsigned char *)malloc(WIDTH / (8 / bits_per_pixel));
245
246 for (i = 0; i < HEIGHT; i++)
247 {
248 for (j = 0, k = 0; j < WIDTH;)
249 {
250 cmap_index = (j / chunk_size) + ((i / chunk_size) * nchunks);
251
252 switch (bits_per_pixel)
253 {
254 case 8:
255 scan_line[k++] = cmap_index;
256 j++;
257 break;
258 case 4:
259 scan_line[k++] = (cmap_index << 4) + cmap_index;
260 j += 2;
261 break;
262 case 2:
263 scan_line[k++] = (cmap_index << 6) + (cmap_index << 4) +
264 (cmap_index << 2) + cmap_index;
265 j += 4;
266 break;
267 case 1:
268 scan_line[k++] =
269 ((j / chunk_size) == (i / chunk_size)) ? 0x00 : 0xff;
270 j += 8;
271 break;
272 }
273 }
274 TIFFWriteScanline(tif, scan_line, i, 0);
275 }
276
277 free(scan_line);
278 TIFFClose(tif);
279 return 0;
280 }
281
282 void Usage()
283 {
284 fprintf(stderr, "Usage: %s -depth (8 | 4 | 2 | 1) tiff-image\n",
285 programName);
286 exit(0);
287 }