]>
Commit | Line | Data |
---|---|---|
8414a40c VZ |
1 | |
2 | /* | |
3 | * tiff-palette.c -- create a Class P (palette) TIFF file | |
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 <stdio.h> | |
27 | #include <stdlib.h> | |
28 | #include <string.h> | |
29 | ||
30 | #include "tiffio.h" | |
31 | ||
32 | #define WIDTH 512 | |
33 | #define HEIGHT WIDTH | |
34 | #define SCALE(x) ((x) * 257L) | |
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, | |
42 | cmap_index, chunk_size = 32, nchunks = 16; | |
43 | unsigned char * scan_line; | |
44 | uint16 *red, *green, *blue; | |
45 | TIFF * tif; | |
46 | ||
47 | programName = argv[0]; | |
48 | ||
49 | if (argc != 4) | |
50 | Usage(); | |
51 | ||
52 | if (!strcmp(argv[1], "-depth")) | |
53 | bits_per_pixel = atoi(argv[2]); | |
54 | else | |
55 | Usage(); | |
56 | ||
57 | switch (bits_per_pixel) { | |
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 | cmsize = nchunks * nchunks; | |
80 | } else { | |
81 | cmsize = 2; | |
82 | } | |
83 | red = (uint16 *) malloc(cmsize * sizeof(uint16)); | |
84 | green = (uint16 *) malloc(cmsize * sizeof(uint16)); | |
85 | blue = (uint16 *) malloc(cmsize * sizeof(uint16)); | |
86 | ||
87 | switch (bits_per_pixel) { | |
88 | case 8: | |
89 | for (i = 0; i < cmsize; i++) { | |
90 | if (i < 32) | |
91 | red[i] = 0; | |
92 | else if (i < 64) | |
93 | red[i] = SCALE(36); | |
94 | else if (i < 96) | |
95 | red[i] = SCALE(73); | |
96 | else if (i < 128) | |
97 | red[i] = SCALE(109); | |
98 | else if (i < 160) | |
99 | red[i] = SCALE(146); | |
100 | else if (i < 192) | |
101 | red[i] = SCALE(182); | |
102 | else if (i < 224) | |
103 | red[i] = SCALE(219); | |
104 | else if (i < 256) | |
105 | red[i] = SCALE(255); | |
106 | ||
107 | if ((i % 32) < 4) | |
108 | green[i] = 0; | |
109 | else if (i < 8) | |
110 | green[i] = SCALE(36); | |
111 | else if ((i % 32) < 12) | |
112 | green[i] = SCALE(73); | |
113 | else if ((i % 32) < 16) | |
114 | green[i] = SCALE(109); | |
115 | else if ((i % 32) < 20) | |
116 | green[i] = SCALE(146); | |
117 | else if ((i % 32) < 24) | |
118 | green[i] = SCALE(182); | |
119 | else if ((i % 32) < 28) | |
120 | green[i] = SCALE(219); | |
121 | else if ((i % 32) < 32) | |
122 | green[i] = SCALE(255); | |
123 | ||
124 | if ((i % 4) == 0) | |
125 | blue[i] = SCALE(0); | |
126 | else if ((i % 4) == 1) | |
127 | blue[i] = SCALE(85); | |
128 | else if ((i % 4) == 2) | |
129 | blue[i] = SCALE(170); | |
130 | else if ((i % 4) == 3) | |
131 | blue[i] = SCALE(255); | |
132 | } | |
133 | break; | |
134 | case 4: | |
135 | red[0] = SCALE(255); | |
136 | green[0] = 0; | |
137 | blue[0] = 0; | |
138 | ||
139 | red[1] = 0; | |
140 | green[1] = SCALE(255); | |
141 | blue[1] = 0; | |
142 | ||
143 | red[2] = 0; | |
144 | green[2] = 0; | |
145 | blue[2] = SCALE(255); | |
146 | ||
147 | red[3] = SCALE(255); | |
148 | green[3] = SCALE(255); | |
149 | blue[3] = SCALE(255); | |
150 | ||
151 | red[4] = 0; | |
152 | green[4] = SCALE(255); | |
153 | blue[4] = SCALE(255); | |
154 | ||
155 | red[5] = SCALE(255); | |
156 | green[5] = 0; | |
157 | blue[5] = SCALE(255); | |
158 | ||
159 | red[6] = SCALE(255); | |
160 | green[6] = SCALE(255); | |
161 | blue[6] = 0; | |
162 | ||
163 | red[7] = 0; | |
164 | green[7] = 0; | |
165 | blue[7] = 0; | |
166 | ||
167 | red[8] = SCALE(176); | |
168 | green[8] = SCALE(224); | |
169 | blue[8] = SCALE(230); | |
170 | red[9] = SCALE(100); | |
171 | green[9] = SCALE(149); | |
172 | blue[9] = SCALE(237); | |
173 | red[10] = SCALE(46); | |
174 | green[10] = SCALE(139); | |
175 | blue[10] = SCALE(87); | |
176 | red[11] = SCALE(160); | |
177 | green[11] = SCALE(82); | |
178 | blue[11] = SCALE(45); | |
179 | red[12] = SCALE(238); | |
180 | green[12] = SCALE(130); | |
181 | blue[12] = SCALE(238); | |
182 | red[13] = SCALE(176); | |
183 | green[13] = SCALE(48); | |
184 | blue[13] = SCALE(96); | |
185 | red[14] = SCALE(50); | |
186 | green[14] = SCALE(205); | |
187 | blue[14] = SCALE(50); | |
188 | red[15] = SCALE(240); | |
189 | green[15] = SCALE(152); | |
190 | blue[15] = SCALE(35); | |
191 | break; | |
192 | case 2: | |
193 | red[0] = SCALE(255); | |
194 | green[0] = 0; | |
195 | blue[0] = 0; | |
196 | ||
197 | red[1] = 0; | |
198 | green[1] = SCALE(255); | |
199 | blue[1] = 0; | |
200 | ||
201 | red[2] = 0; | |
202 | green[2] = 0; | |
203 | blue[2] = SCALE(255); | |
204 | red[3] = SCALE(255); | |
205 | green[3] = SCALE(255); | |
206 | blue[3] = SCALE(255); | |
207 | break; | |
208 | case 1: | |
209 | red[0] = 0; | |
210 | green[0] = 0; | |
211 | blue[0] = 0; | |
212 | ||
213 | red[1] = SCALE(255); | |
214 | green[1] = SCALE(255); | |
215 | blue[1] = SCALE(255); | |
216 | break; | |
217 | } | |
218 | ||
219 | if ((tif = TIFFOpen(argv[3], "w")) == NULL) { | |
220 | fprintf(stderr, "can't open %s as a TIFF file\n", argv[3]); | |
80ed523f | 221 | free(red);free(green);free(blue); |
8414a40c VZ |
222 | return 0; |
223 | } | |
224 | ||
225 | TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, WIDTH); | |
226 | TIFFSetField(tif, TIFFTAG_IMAGELENGTH, HEIGHT); | |
227 | TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bits_per_pixel); | |
228 | TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE); | |
229 | TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE); | |
230 | TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1); | |
231 | TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1); | |
232 | TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); | |
233 | TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_NONE); | |
234 | TIFFSetField(tif, TIFFTAG_COLORMAP, red, green, blue); | |
235 | ||
236 | scan_line = (unsigned char *) malloc(WIDTH / (8 / bits_per_pixel)); | |
237 | ||
238 | for (i = 0; i < HEIGHT; i++) { | |
239 | for (j = 0, k = 0; j < WIDTH;) { | |
240 | cmap_index = (j / chunk_size) + ((i / chunk_size) * nchunks); | |
241 | ||
242 | switch (bits_per_pixel) { | |
243 | case 8: | |
244 | scan_line[k++] = cmap_index; | |
245 | j++; | |
246 | break; | |
247 | case 4: | |
248 | scan_line[k++] = (cmap_index << 4) + cmap_index; | |
249 | j += 2; | |
250 | break; | |
251 | case 2: | |
252 | scan_line[k++] = (cmap_index << 6) + (cmap_index << 4) | |
253 | + (cmap_index << 2) + cmap_index; | |
254 | j += 4; | |
255 | break; | |
256 | case 1: | |
257 | scan_line[k++] = | |
258 | ((j / chunk_size) == (i / chunk_size)) ? 0x00 : 0xff; | |
259 | j += 8; | |
260 | break; | |
261 | } | |
262 | } | |
263 | TIFFWriteScanline(tif, scan_line, i, 0); | |
264 | } | |
265 | ||
266 | free(scan_line); | |
267 | TIFFClose(tif); | |
268 | return 0; | |
269 | } | |
270 | ||
271 | void | |
272 | Usage() | |
273 | { | |
274 | fprintf(stderr, "Usage: %s -depth (8 | 4 | 2 | 1) tiff-image\n", programName); | |
275 | exit(0); | |
276 | } | |
80ed523f VZ |
277 | /* |
278 | * Local Variables: | |
279 | * mode: c | |
280 | * c-basic-offset: 8 | |
281 | * fill-column: 78 | |
282 | * End: | |
283 | */ |