Mercurial > hg > audiostuff
comparison spandsp-0.0.3/spandsp-0.0.3/tests/t4_tests.c @ 5:f762bf195c4b
import spandsp-0.0.3
author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
---|---|
date | Fri, 25 Jun 2010 16:00:21 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
4:26cd8f1ef0b1 | 5:f762bf195c4b |
---|---|
1 /* | |
2 * SpanDSP - a series of DSP components for telephony | |
3 * | |
4 * t4_tests.c - ITU T.4 FAX image to and from TIFF file tests | |
5 * | |
6 * Written by Steve Underwood <steveu@coppice.org> | |
7 * | |
8 * Copyright (C) 2003 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: t4_tests.c,v 1.34 2006/11/23 15:48:09 steveu Exp $ | |
26 */ | |
27 | |
28 /*! \file */ | |
29 | |
30 /*! \page t4_tests_page T.4 tests | |
31 \section t4_tests_page_sec_1 What does it do | |
32 These tests exercise the image compression and decompression methods defined | |
33 in ITU specifications T.4 and T.6. | |
34 */ | |
35 | |
36 #ifdef HAVE_CONFIG_H | |
37 #include "config.h" | |
38 #endif | |
39 | |
40 #include <stdio.h> | |
41 #include <stdlib.h> | |
42 #include <inttypes.h> | |
43 #include <fcntl.h> | |
44 #include <memory.h> | |
45 #if defined(HAVE_TGMATH_H) | |
46 #include <tgmath.h> | |
47 #endif | |
48 #if defined(HAVE_MATH_H) | |
49 #include <math.h> | |
50 #endif | |
51 | |
52 #include <tiffio.h> | |
53 | |
54 #include "spandsp.h" | |
55 | |
56 #define IN_FILE_NAME "../itutests/fax/itutests.tif" | |
57 #define OUT_FILE_NAME "t4_tests_receive.tif" | |
58 | |
59 #define XSIZE 1728 | |
60 | |
61 t4_state_t send_state; | |
62 t4_state_t receive_state; | |
63 | |
64 int main(int argc, char* argv[]) | |
65 { | |
66 int sends; | |
67 int page_no; | |
68 int bit; | |
69 int end_of_page; | |
70 int end_marks; | |
71 int i; | |
72 int decode_test; | |
73 int compression; | |
74 int compression_step; | |
75 int add_page_headers; | |
76 int min_row_bits; | |
77 int restart_pages; | |
78 char buf[512]; | |
79 t4_stats_t stats; | |
80 const char *in_file_name; | |
81 | |
82 i = 1; | |
83 decode_test = FALSE; | |
84 compression = -1; | |
85 add_page_headers = FALSE; | |
86 restart_pages = FALSE; | |
87 in_file_name = IN_FILE_NAME; | |
88 min_row_bits = 0; | |
89 while (i < argc) | |
90 { | |
91 if (strcmp(argv[i], "-d") == 0) | |
92 decode_test = TRUE; | |
93 else if (strcmp(argv[i], "-1") == 0) | |
94 compression = T4_COMPRESSION_ITU_T4_1D; | |
95 else if (strcmp(argv[i], "-2") == 0) | |
96 compression = T4_COMPRESSION_ITU_T4_2D; | |
97 else if (strcmp(argv[i], "-6") == 0) | |
98 compression = T4_COMPRESSION_ITU_T6; | |
99 else if (strcmp(argv[i], "-h") == 0) | |
100 add_page_headers = TRUE; | |
101 else if (strcmp(argv[i], "-r") == 0) | |
102 restart_pages = TRUE; | |
103 else if (strcmp(argv[i], "-i") == 0) | |
104 in_file_name = argv[++i]; | |
105 else if (strcmp(argv[i], "-m") == 0) | |
106 min_row_bits = atoi(argv[++i]); | |
107 i++; | |
108 } | |
109 /* Create a send and a receive end */ | |
110 memset(&send_state, 0, sizeof(send_state)); | |
111 memset(&receive_state, 0, sizeof(receive_state)); | |
112 | |
113 if (decode_test) | |
114 { | |
115 if (compression < 0) | |
116 compression = T4_COMPRESSION_ITU_T4_1D; | |
117 /* Receive end puts TIFF to a new file. We assume the receive width here. */ | |
118 if (t4_rx_init(&receive_state, OUT_FILE_NAME, T4_COMPRESSION_ITU_T4_2D)) | |
119 { | |
120 printf("Failed to init\n"); | |
121 exit(2); | |
122 } | |
123 | |
124 t4_rx_set_rx_encoding(&receive_state, compression); | |
125 t4_rx_set_x_resolution(&receive_state, T4_X_RESOLUTION_R8); | |
126 t4_rx_set_y_resolution(&receive_state, T4_Y_RESOLUTION_FINE); | |
127 t4_rx_set_image_width(&receive_state, XSIZE); | |
128 | |
129 page_no = 1; | |
130 t4_rx_start_page(&receive_state); | |
131 while (fgets(buf, 511, stdin)) | |
132 { | |
133 if (sscanf(buf, "Rx bit %*d - %d", &bit) == 1) | |
134 { | |
135 if ((end_of_page = t4_rx_put_bit(&receive_state, bit))) | |
136 { | |
137 printf("End of page detected\n"); | |
138 break; | |
139 } | |
140 } | |
141 } | |
142 #if 0 | |
143 /* Dump the entire image as text 'X's and spaces */ | |
144 s = receive_state.image_buffer; | |
145 for (i = 0; i < receive_state.rows; i++) | |
146 { | |
147 for (j = 0; j < receive_state.bytes_per_row; j++) | |
148 { | |
149 for (k = 0; k < 8; k++) | |
150 { | |
151 printf((receive_state.image_buffer[i*receive_state.bytes_per_row + j] & (0x80 >> k)) ? "X" : " "); | |
152 } | |
153 } | |
154 printf("\n"); | |
155 } | |
156 #endif | |
157 t4_rx_end_page(&receive_state); | |
158 t4_get_transfer_statistics(&receive_state, &stats); | |
159 printf("Pages = %d\n", stats.pages_transferred); | |
160 printf("Image size = %d x %d\n", stats.width, stats.length); | |
161 printf("Image resolution = %d x %d\n", stats.x_resolution, stats.y_resolution); | |
162 printf("Bad rows = %d\n", stats.bad_rows); | |
163 printf("Longest bad row run = %d\n", stats.longest_bad_row_run); | |
164 t4_rx_end(&receive_state); | |
165 } | |
166 else | |
167 { | |
168 /* Send end gets TIFF from a file */ | |
169 if (t4_tx_init(&send_state, in_file_name, -1, -1)) | |
170 { | |
171 printf("Failed to init TIFF send\n"); | |
172 exit(2); | |
173 } | |
174 | |
175 /* Receive end puts TIFF to a new file. */ | |
176 if (t4_rx_init(&receive_state, OUT_FILE_NAME, T4_COMPRESSION_ITU_T4_2D)) | |
177 { | |
178 printf("Failed to init\n"); | |
179 exit(2); | |
180 } | |
181 | |
182 t4_tx_set_min_row_bits(&send_state, min_row_bits); | |
183 | |
184 t4_rx_set_x_resolution(&receive_state, t4_tx_get_x_resolution(&send_state)); | |
185 t4_rx_set_y_resolution(&receive_state, t4_tx_get_y_resolution(&send_state)); | |
186 t4_rx_set_image_width(&receive_state, t4_tx_get_image_width(&send_state)); | |
187 | |
188 /* Now send and receive all the pages in the source TIFF file */ | |
189 page_no = 1; | |
190 t4_tx_set_local_ident(&send_state, "852 2666 0542"); | |
191 sends = 0; | |
192 /* Select whether we step round the compression schemes, or use a single specified one. */ | |
193 compression_step = (compression < 0) ? 0 : -1; | |
194 for (;;) | |
195 { | |
196 end_marks = 0; | |
197 /* Add a header line to alternate pages, if required */ | |
198 if (add_page_headers && (sends & 2)) | |
199 t4_tx_set_header_info(&send_state, "Header"); | |
200 else | |
201 t4_tx_set_header_info(&send_state, NULL); | |
202 if (restart_pages && (sends & 1)) | |
203 { | |
204 /* Use restart, to send the page a second time */ | |
205 if (t4_tx_restart_page(&send_state)) | |
206 break; | |
207 } | |
208 else | |
209 { | |
210 switch (compression_step) | |
211 { | |
212 case 0: | |
213 compression = T4_COMPRESSION_ITU_T4_1D; | |
214 compression_step++; | |
215 break; | |
216 case 1: | |
217 compression = T4_COMPRESSION_ITU_T4_2D; | |
218 compression_step++; | |
219 break; | |
220 case 2: | |
221 compression = T4_COMPRESSION_ITU_T6; | |
222 compression_step = 0; | |
223 break; | |
224 } | |
225 t4_tx_set_tx_encoding(&send_state, compression); | |
226 t4_rx_set_rx_encoding(&receive_state, compression); | |
227 | |
228 if (t4_tx_start_page(&send_state)) | |
229 break; | |
230 } | |
231 t4_rx_start_page(&receive_state); | |
232 do | |
233 { | |
234 bit = t4_tx_get_bit(&send_state); | |
235 #if 0 | |
236 if (--next_hit <= 0) | |
237 { | |
238 do | |
239 next_hit = rand() & 0x3FF; | |
240 while (next_hit < 20); | |
241 bit ^= (rand() & 1); | |
242 } | |
243 #endif | |
244 if (bit == PUTBIT_END_OF_DATA) | |
245 { | |
246 /* T.6 data does not contain an image termination sequence. | |
247 T.4 1D and 2D do, and should locate that sequence. */ | |
248 if (compression == T4_COMPRESSION_ITU_T6) | |
249 break; | |
250 if (++end_marks > 50) | |
251 { | |
252 printf("Receiver missed the end of page mark\n"); | |
253 exit(2); | |
254 } | |
255 } | |
256 end_of_page = t4_rx_put_bit(&receive_state, bit & 1); | |
257 } | |
258 while (!end_of_page); | |
259 t4_get_transfer_statistics(&receive_state, &stats); | |
260 printf("Pages = %d\n", stats.pages_transferred); | |
261 printf("Image size = %d x %d\n", stats.width, stats.length); | |
262 printf("Image resolution = %d x %d\n", stats.x_resolution, stats.y_resolution); | |
263 printf("Bad rows = %d\n", stats.bad_rows); | |
264 printf("Longest bad row run = %d\n", stats.longest_bad_row_run); | |
265 if (!restart_pages || (sends & 1)) | |
266 t4_tx_end_page(&send_state); | |
267 t4_rx_end_page(&receive_state); | |
268 sends++; | |
269 } | |
270 /* And we should now have a matching received TIFF file. Note this will only match | |
271 at the image level. TIFF files allow a lot of ways to express the same thing, | |
272 so bit matching of the files is not the normal case. */ | |
273 t4_tx_end(&send_state); | |
274 t4_rx_end(&receive_state); | |
275 } | |
276 } | |
277 /*- End of function --------------------------------------------------------*/ | |
278 /*- End of file ------------------------------------------------------------*/ |