New comit of SDL2
[supertux.git] / src / SDL2 / external / tiff-4.0.3 / contrib / dbs / tiff-grayscale.c
1 /* $Id: tiff-grayscale.c,v 1.6 2010-06-08 18:55:15 bfriesen Exp $ */
2
3 /*
4  * tiff-grayscale.c -- create a Class G (grayscale) TIFF file
5  *      with a gray response curve in linear optical density
6  *
7  * Copyright 1990 by Digital Equipment Corporation, Maynard, Massachusetts.
8  *
9  *                        All Rights Reserved
10  *
11  * Permission to use, copy, modify, and distribute this software and its
12  * documentation for any purpose and without fee is hereby granted,
13  * provided that the above copyright notice appear in all copies and that
14  * both that copyright notice and this permission notice appear in
15  * supporting documentation, and that the name of Digital not be
16  * used in advertising or publicity pertaining to distribution of the
17  * software without specific, written prior permission.
18  *
19  * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
20  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
21  * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
22  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
23  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
25  * SOFTWARE.
26  */
27
28 #include <math.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "tiffio.h"
34
35 #define WIDTH       512
36 #define HEIGHT      WIDTH
37
38 char *              programName;
39 void                Usage();
40
41 int main(int argc, char **argv)
42 {
43     int             bits_per_pixel = 8, cmsize, i, j, k,
44                     gray_index, chunk_size = 32, nchunks = 16;
45     unsigned char * scan_line;
46     uint16 *        gray;
47     float           refblackwhite[2*1];
48     TIFF *          tif;
49
50     programName = argv[0];
51
52     if (argc != 4)
53         Usage();
54
55     if (!strcmp(argv[1], "-depth"))
56          bits_per_pixel = atoi(argv[2]);
57     else
58          Usage();
59
60     switch (bits_per_pixel) {
61         case 8:
62             nchunks = 16;
63             chunk_size = 32;
64             break;
65         case 4:
66             nchunks = 4;
67             chunk_size = 128;
68             break;
69         case 2:
70             nchunks = 2;
71             chunk_size = 256;
72             break;
73         default:
74             Usage();
75     }
76
77     cmsize = nchunks * nchunks;
78     gray = (uint16 *) malloc(cmsize * sizeof(uint16));
79
80     gray[0] = 3000;
81     for (i = 1; i < cmsize; i++)
82         gray[i] = (uint16) (-log10((double) i / (cmsize - 1)) * 1000);
83
84     refblackwhite[0] = 0.0;
85     refblackwhite[1] = (float)((1L<<bits_per_pixel) - 1);
86
87     if ((tif = TIFFOpen(argv[3], "w")) == NULL) {
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         for (j = 0, k = 0; j < WIDTH;) {
109             gray_index = (j / chunk_size) + ((i / chunk_size) * nchunks);
110
111             switch (bits_per_pixel) {
112             case 8:
113                 scan_line[k++] = gray_index;
114                 j++;
115                 break;
116             case 4:
117                 scan_line[k++] = (gray_index << 4) + gray_index;
118                 j += 2;
119                 break;
120             case 2:
121                 scan_line[k++] = (gray_index << 6) + (gray_index << 4)
122                     + (gray_index << 2) + gray_index;
123                 j += 4;
124                 break;
125             }
126         }
127         TIFFWriteScanline(tif, scan_line, i, 0);
128     }
129
130     free(scan_line);
131     TIFFClose(tif);
132     return 0;
133 }
134
135 void
136 Usage()
137 {
138     fprintf(stderr, "Usage: %s -depth (8 | 4 | 2) tiff-image\n", programName);
139     exit(0);
140 }
141 /*
142  * Local Variables:
143  * mode: c
144  * c-basic-offset: 8
145  * fill-column: 78
146  * End:
147  */