5
|
1 /*
|
|
2 * SpanDSP - a series of DSP components for telephony
|
|
3 *
|
|
4 * generate_dithered_tif.c - Create a fine checkerboard TIFF file for test purposes.
|
|
5 *
|
|
6 * Written by Steve Underwood <steveu@coppice.org>
|
|
7 *
|
|
8 * Copyright (C) 2006 Steve Underwood
|
|
9 *
|
|
10 * All rights reserved.
|
|
11 *
|
|
12 * This program is free software; you can redistribute it and/or modify
|
|
13 * it under the terms of the GNU General Public License version 2, as
|
|
14 * published by the Free Software Foundation.
|
|
15 *
|
|
16 * This program is distributed in the hope that it will be useful,
|
|
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
19 * GNU General Public License for more details.
|
|
20 *
|
|
21 * You should have received a copy of the GNU General Public License
|
|
22 * along with this program; if not, write to the Free Software
|
|
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
24 *
|
|
25 * $Id: generate_dithered_tif.c,v 1.3 2006/11/20 00:00:27 steveu Exp $
|
|
26 */
|
|
27
|
|
28 /*! \file */
|
|
29
|
|
30 /*
|
|
31 This program generates an A4 sized FAX image of a fine checkerboard. This doesn't
|
|
32 compress well, so it results in a rather large file for a single page. This is
|
|
33 good for testing the handling of extreme pages.
|
|
34
|
|
35 Note that due to a bug in FAX image handling, versions of libtiff up to 3.8.2 fail
|
|
36 to handle this complex image properly, if 2-D compression is used. The bug should
|
|
37 be fixed in CVS at the time of writing, and so should be fixed in released versions
|
|
38 after 3.8.2. This code uses 1-D compression to avoid the issue.
|
|
39 */
|
|
40
|
|
41 #ifdef HAVE_CONFIG_H
|
|
42 #include "config.h"
|
|
43 #endif
|
|
44
|
|
45 #define _GNU_SOURCE
|
|
46
|
|
47 #include <stdio.h>
|
|
48 #include <inttypes.h>
|
|
49 #include <limits.h>
|
|
50 #include <unistd.h>
|
|
51 #include <fcntl.h>
|
|
52 #include <stdlib.h>
|
|
53 #include <time.h>
|
|
54 #include <memory.h>
|
|
55 #include <string.h>
|
|
56 #if defined(HAVE_TGMATH_H)
|
|
57 #include <tgmath.h>
|
|
58 #endif
|
|
59 #if defined(HAVE_MATH_H)
|
|
60 #include <math.h>
|
|
61 #endif
|
|
62 #include <tiffio.h>
|
|
63
|
|
64 #define T4_X_RESOLUTION_R4 4019
|
|
65 #define T4_X_RESOLUTION_R8 8037
|
|
66 #define T4_X_RESOLUTION_R16 16074
|
|
67
|
|
68 #define T4_Y_RESOLUTION_STANDARD 3850
|
|
69 #define T4_Y_RESOLUTION_FINE 7700
|
|
70 #define T4_Y_RESOLUTION_SUPERFINE 15400
|
|
71
|
|
72 int main(int argc, char *argv[])
|
|
73 {
|
|
74 int image_width;
|
|
75 int row;
|
|
76 int resunit;
|
|
77 int output_compression;
|
|
78 int output_t4_options;
|
|
79 uint8_t image_buffer[1024];
|
|
80 TIFF *tiff_file;
|
|
81 struct tm *tm;
|
|
82 time_t now;
|
|
83 char buf[133];
|
|
84 float x_resolution;
|
|
85 float y_resolution;
|
|
86 int x_res;
|
|
87 int y_res;
|
|
88 int image_length;
|
|
89
|
|
90 if ((tiff_file = TIFFOpen("dithered.tif", "w")) == NULL)
|
|
91 exit(2);
|
|
92
|
|
93 output_compression = COMPRESSION_CCITT_T4;
|
|
94 /* Use 1-D compression until a fixed libtiff is the norm. */
|
|
95 //output_t4_options = GROUP3OPT_FILLBITS | GROUP3OPT_2DENCODING;
|
|
96 output_t4_options = GROUP3OPT_FILLBITS;
|
|
97
|
|
98 x_res = T4_X_RESOLUTION_R8;
|
|
99 y_res = T4_Y_RESOLUTION_FINE;
|
|
100 image_width = 1728;
|
|
101 image_length = 2200;
|
|
102
|
|
103 /* Prepare the directory entry fully before writing the image, or libtiff complains */
|
|
104 TIFFSetField(tiff_file, TIFFTAG_COMPRESSION, output_compression);
|
|
105 if (output_compression == COMPRESSION_CCITT_T4)
|
|
106 {
|
|
107 TIFFSetField(tiff_file, TIFFTAG_T4OPTIONS, output_t4_options);
|
|
108 TIFFSetField(tiff_file, TIFFTAG_FAXMODE, FAXMODE_CLASSF);
|
|
109 }
|
|
110 TIFFSetField(tiff_file, TIFFTAG_IMAGEWIDTH, image_width);
|
|
111 TIFFSetField(tiff_file, TIFFTAG_BITSPERSAMPLE, 1);
|
|
112 TIFFSetField(tiff_file, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
|
|
113 TIFFSetField(tiff_file, TIFFTAG_SAMPLESPERPIXEL, 1);
|
|
114 TIFFSetField(tiff_file, TIFFTAG_ROWSPERSTRIP, -1L);
|
|
115 TIFFSetField(tiff_file, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
|
|
116 TIFFSetField(tiff_file, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
|
|
117 TIFFSetField(tiff_file, TIFFTAG_FILLORDER, FILLORDER_LSB2MSB);
|
|
118
|
|
119 x_resolution = x_res/100.0f;
|
|
120 y_resolution = y_res/100.0f;
|
|
121 TIFFSetField(tiff_file, TIFFTAG_XRESOLUTION, floorf(x_resolution*2.54f + 0.5f));
|
|
122 TIFFSetField(tiff_file, TIFFTAG_YRESOLUTION, floorf(y_resolution*2.54f + 0.5f));
|
|
123 resunit = RESUNIT_INCH;
|
|
124 TIFFSetField(tiff_file, TIFFTAG_RESOLUTIONUNIT, resunit);
|
|
125
|
|
126 TIFFSetField(tiff_file, TIFFTAG_SOFTWARE, "spandsp");
|
|
127 if (gethostname(buf, sizeof(buf)) == 0)
|
|
128 TIFFSetField(tiff_file, TIFFTAG_HOSTCOMPUTER, buf);
|
|
129
|
|
130 TIFFSetField(tiff_file, TIFFTAG_IMAGEDESCRIPTION, "Checkerboard or dithered ones");
|
|
131 TIFFSetField(tiff_file, TIFFTAG_MAKE, "soft-switch.org");
|
|
132 TIFFSetField(tiff_file, TIFFTAG_MODEL, "test data");
|
|
133
|
|
134 time(&now);
|
|
135 tm = localtime(&now);
|
|
136 sprintf(buf,
|
|
137 "%4d/%02d/%02d %02d:%02d:%02d",
|
|
138 tm->tm_year + 1900,
|
|
139 tm->tm_mon + 1,
|
|
140 tm->tm_mday,
|
|
141 tm->tm_hour,
|
|
142 tm->tm_min,
|
|
143 tm->tm_sec);
|
|
144 TIFFSetField(tiff_file, TIFFTAG_DATETIME, buf);
|
|
145
|
|
146 TIFFSetField(tiff_file, TIFFTAG_IMAGELENGTH, image_length);
|
|
147 TIFFSetField(tiff_file, TIFFTAG_PAGENUMBER, 0, 1);
|
|
148 TIFFSetField(tiff_file, TIFFTAG_CLEANFAXDATA, CLEANFAXDATA_CLEAN);
|
|
149 TIFFSetField(tiff_file, TIFFTAG_IMAGEWIDTH, image_width);
|
|
150
|
|
151 /* Write the image first.... */
|
|
152 for (row = 0; row < image_length; row++)
|
|
153 {
|
|
154 if ((row & 1) == 0)
|
|
155 memset(image_buffer, 0xAA, image_width/8 + 1);
|
|
156 else
|
|
157 memset(image_buffer, 0x55, image_width/8 + 1);
|
|
158 if (TIFFWriteScanline(tiff_file, image_buffer, row, 0) < 0)
|
|
159 {
|
|
160 printf("Write error at row %d.\n", row);
|
|
161 exit(2);
|
|
162 }
|
|
163 }
|
|
164 /* ....then the directory entry, and libtiff is happy. */
|
|
165 TIFFWriteDirectory(tiff_file);
|
|
166 TIFFClose(tiff_file);
|
|
167 return 0;
|
|
168 }
|
|
169 /*- End of function --------------------------------------------------------*/
|
|
170 /*- End of file ------------------------------------------------------------*/
|