spandsp: t4_rx.h Source File

spandsp 0.0.6

t4_rx.h
Go to the documentation of this file.
1/*
2 * SpanDSP - a series of DSP components for telephony
3 *
4 * t4_rx.h - definitions for T.4 FAX receive processing
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 Lesser General Public License version 2.1,
14 * as 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 Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26/*! \file */
27
28#if !defined(_SPANDSP_T4_RX_H_)
29#define _SPANDSP_T4_RX_H_
30
31/*! \page t4_page T.4 image compression and decompression
32
33\section t4_page_sec_1 What does it do?
34The T.4 image compression and decompression routines implement the 1D and 2D
35encoding methods defined in ITU specification T.4. They also implement the pure
362D encoding method defined in T.6. These are image compression algorithms used
37for FAX transmission.
38
39\section t4_page_sec_1 How does it work?
40*/
41
42/*! This function is a callback from the image decoders, to write the decoded bi-level image,
43 row by row. It is called for each row, with len set to the number of bytes per row. At the
44 end of the image it is called with len set to zero, to indicate the end of image condition.
45 \return 0 for OK, or non-zero for a problem that requires the image be interrupted. */
46 typedef int (*t4_row_write_handler_t)(void *user_data, const uint8_t buf[], size_t len);
47
48/*! Supported compression modes. */
49 typedef enum
50{
51 /*! No compression */
52 T4_COMPRESSION_NONE = 0,
53 /*! T.1 1D compression */
54 T4_COMPRESSION_ITU_T4_1D = 1,
55 /*! T.4 2D compression */
56 T4_COMPRESSION_ITU_T4_2D = 2,
57 /*! T.6 2D compression */
58 T4_COMPRESSION_ITU_T6 = 3,
59 /*! T.85 monochrome JBIG coding with L0 fixed. */
60 T4_COMPRESSION_ITU_T85 = 4,
61 /*! T.85 monochrome JBIG coding with L0 variable. */
62 T4_COMPRESSION_ITU_T85_L0 = 5,
63 /*! T.43 colour JBIG coding */
64 T4_COMPRESSION_ITU_T43 = 6,
65 /*! T.45 run length colour compression */
66 T4_COMPRESSION_ITU_T45 = 7,
67 /*! T.42 + T.81 + T.30 Annex E colour JPEG coding */
68 T4_COMPRESSION_ITU_T42 = 8,
69 /*! T.42 + T.81 + T.30 Annex K colour sYCC-JPEG coding */
70 T4_COMPRESSION_ITU_SYCC_T42 = 9
71} t4_image_compression_t;
72
73/*! Image type */
74 typedef enum
75{
76 T4_IMAGE_TYPE_BILEVEL = 0,
77 T4_IMAGE_TYPE_GRAY_8BIT = 1,
78 T4_IMAGE_TYPE_GRAY_12BIT = 2,
79 T4_IMAGE_TYPE_COLOUR_8BIT = 3,
80 T4_IMAGE_TYPE_COLOUR_12BIT = 4
81} t4_image_types_t;
82
83/*! Supported X resolutions, in pixels per metre. */
84 typedef enum
85{
86 T4_X_RESOLUTION_R4 = 4016,
87 T4_X_RESOLUTION_R8 = 8031,
88 T4_X_RESOLUTION_300 = 11811,
89 T4_X_RESOLUTION_R16 = 16063,
90 T4_X_RESOLUTION_600 = 23622,
91 T4_X_RESOLUTION_800 = 31496,
92 T4_X_RESOLUTION_1200 = 47244
93} t4_image_x_resolution_t;
94
95/*! Supported Y resolutions, in pixels per metre. */
96 typedef enum
97{
98 T4_Y_RESOLUTION_STANDARD = 3850,
99 T4_Y_RESOLUTION_FINE = 7700,
100 T4_Y_RESOLUTION_300 = 11811,
101 T4_Y_RESOLUTION_SUPERFINE = 15400, /* 400 is 15748 */
102 T4_Y_RESOLUTION_600 = 23622,
103 T4_Y_RESOLUTION_800 = 31496,
104 T4_Y_RESOLUTION_1200 = 47244
105} t4_image_y_resolution_t;
106
107/*!
108 Exact widths in PELs for the difference resolutions, and page widths.
109 Note:
110 The A4 widths also apply to North American letter and legal.
111 The R4 resolution widths are not supported in recent versions of T.30
112 Only images of exactly these widths are acceptable for FAX transmisson.
113
114 R4 864 pels/215mm for ISO A4, North American Letter and Legal
115 R4 1024 pels/255mm for ISO B4
116 R4 1216 pels/303mm for ISO A3
117 R8 1728 pels/215mm for ISO A4, North American Letter and Legal
118 R8 2048 pels/255mm for ISO B4
119 R8 2432 pels/303mm for ISO A3
120 R16 3456 pels/215mm for ISO A4, North American Letter and Legal
121 R16 4096 pels/255mm for ISO B4
122 R16 4864 pels/303mm for ISO A3
123*/
124 typedef enum
125{
126 T4_WIDTH_R4_A4 = 864,
127 T4_WIDTH_R4_B4 = 1024,
128 T4_WIDTH_R4_A3 = 1216,
129 T4_WIDTH_R8_A4 = 1728,
130 T4_WIDTH_R8_B4 = 2048,
131 T4_WIDTH_R8_A3 = 2432,
132 T4_WIDTH_300_A4 = 2592,
133 T4_WIDTH_300_B4 = 3072,
134 T4_WIDTH_300_A3 = 3648,
135 T4_WIDTH_R16_A4 = 3456,
136 T4_WIDTH_R16_B4 = 4096,
137 T4_WIDTH_R16_A3 = 4864,
138 T4_WIDTH_600_A4 = 5184,
139 T4_WIDTH_600_B4 = 6144,
140 T4_WIDTH_600_A3 = 7296,
141 T4_WIDTH_1200_A4 = 10368,
142 T4_WIDTH_1200_B4 = 12288,
143 T4_WIDTH_1200_A3 = 14592
144} t4_image_width_t;
145
146/*!
147 Length of the various supported paper sizes, in pixels at the various Y resolutions.
148 Paper sizes are
149 A4 (215mm x 297mm)
150 B4 (255mm x 364mm)
151 A3 (303mm x 418.56mm)
152 North American Letter (215.9mm x 279.4mm)
153 North American Legal (215.9mm x 355.6mm)
154 Unlimited
155
156 T.4 does not accurately define the maximum number of scan lines in a page. A wide
157 variety of maximum row counts are used in the real world. It is important not to
158 set our sending limit too high, or a receiving machine might split pages. It is
159 important not to set it too low, or we might clip pages.
160
161 Values seen for standard resolution A4 pages include 1037, 1045, 1109, 1126 and 1143.
162 1109 seems the most-popular. At fine res 2150, 2196, 2200, 2237, 2252-2262, 2264,
163 2286, and 2394 are used. 2255 seems the most popular. We try to use balanced choices
164 here.
165*/
166 typedef enum
167{
168 /* A4 is 297mm long */
169 T4_LENGTH_STANDARD_A4 = 1143,
170 T4_LENGTH_FINE_A4 = 2286,
171 T4_LENGTH_300_A4 = 4665,
172 T4_LENGTH_SUPERFINE_A4 = 4573,
173 T4_LENGTH_600_A4 = 6998,
174 T4_LENGTH_800_A4 = 9330,
175 T4_LENGTH_1200_A4 = 13996,
176 /* B4 is 364mm long */
177 T4_LENGTH_STANDARD_B4 = 1401,
178 T4_LENGTH_FINE_B4 = 2802,
179 T4_LENGTH_300_B4 = 0,
180 T4_LENGTH_SUPERFINE_B4 = 5605,
181 T4_LENGTH_600_B4 = 0,
182 T4_LENGTH_800_B4 = 0,
183 T4_LENGTH_1200_B4 = 0,
184 /* North American letter is 279.4mm long */
185 T4_LENGTH_STANDARD_US_LETTER = 1075,
186 T4_LENGTH_FINE_US_LETTER = 2151,
187 T4_LENGTH_300_US_LETTER = 0,
188 T4_LENGTH_SUPERFINE_US_LETTER = 4302,
189 T4_LENGTH_600_US_LETTER = 0,
190 T4_LENGTH_800_US_LETTER = 0,
191 T4_LENGTH_1200_US_LETTER = 0,
192 /* North American legal is 355.6mm long */
193 T4_LENGTH_STANDARD_US_LEGAL = 1369,
194 T4_LENGTH_FINE_US_LEGAL = 2738,
195 T4_LENGTH_300_US_LEGAL = 0,
196 T4_LENGTH_SUPERFINE_US_LEGAL = 5476,
197 T4_LENGTH_600_US_LEGAL = 0,
198 T4_LENGTH_800_US_LEGAL = 0,
199 T4_LENGTH_1200_US_LEGAL = 0
200} t4_image_length_t;
201
202/*!
203 T.4 FAX compression/decompression descriptor. This defines the working state
204 for a single instance of a T.4 FAX compression or decompression channel.
205*/
206 typedef struct t4_state_s t4_state_t;
207typedef struct t4_state_s t4_rx_state_t;
208
209/*!
210 T.4 FAX compression/decompression statistics.
211*/
212 typedef struct
213{
214 /*! \brief The number of pages transferred so far. */
215 int pages_transferred;
216 /*! \brief The number of pages in the file (<0 if unknown). */
217 int pages_in_file;
218 /*! \brief The number of horizontal pixels in the most recent page. */
219 int width;
220 /*! \brief The number of vertical pixels in the most recent page. */
221 int length;
222 /*! \brief The number of bad pixel rows in the most recent page. */
223 int bad_rows;
224 /*! \brief The largest number of bad pixel rows in a block in the most recent page. */
225 int longest_bad_row_run;
226 /*! \brief The horizontal resolution of the page in pixels per metre */
227 int x_resolution;
228 /*! \brief The vertical resolution of the page in pixels per metre */
229 int y_resolution;
230 /*! \brief The type of compression used between the FAX machines */
231 int encoding;
232 /*! \brief The size of the image on the line, in bytes */
233 int line_image_size;
234} t4_stats_t;
235
236#if defined(__cplusplus)
237extern "C" {
238#endif
239
240/*! \brief Prepare to receive the next page of the current document.
241 \param s The T.4 context.
242 \return zero for success, -1 for failure. */
243SPAN_DECLARE(int) t4_rx_start_page(t4_rx_state_t *s);
244
245/*! \brief Put a bit of the current document page.
246 \param s The T.4 context.
247 \param bit The data bit.
248 \return TRUE when the bit ends the document page, otherwise FALSE. */
249SPAN_DECLARE(int) t4_rx_put_bit(t4_rx_state_t *s, int bit);
250
251/*! \brief Put a byte of the current document page.
252 \param s The T.4 context.
253 \param byte The data byte.
254 \return TRUE when the byte ends the document page, otherwise FALSE. */
255SPAN_DECLARE(int) t4_rx_put_byte(t4_rx_state_t *s, uint8_t byte);
256
257/*! \brief Put a byte of the current document page.
258 \param s The T.4 context.
259 \param buf The buffer containing the chunk.
260 \param len The length of the chunk.
261 \return TRUE when the byte ends the document page, otherwise FALSE. */
262SPAN_DECLARE(int) t4_rx_put_chunk(t4_rx_state_t *s, const uint8_t buf[], int len);
263
264/*! \brief Complete the reception of a page.
265 \param s The T.4 receive context.
266 \return 0 for success, otherwise -1. */
267SPAN_DECLARE(int) t4_rx_end_page(t4_rx_state_t *s);
268
269/*! \brief Set the row write handler for a T.4 receive context.
270 \param s The T.4 receive context.
271 \param handler A pointer to the handler routine.
272 \param user_data An opaque pointer passed to the handler routine.
273 \return 0 for success, otherwise -1. */
274SPAN_DECLARE(int) t4_rx_set_row_write_handler(t4_rx_state_t *s, t4_row_write_handler_t handler, void *user_data);
275
276/*! \brief Set the encoding for the received data.
277 \param s The T.4 context.
278 \param encoding The encoding. */
279SPAN_DECLARE(void) t4_rx_set_rx_encoding(t4_rx_state_t *s, int encoding);
280
281/*! \brief Set the expected width of the received image, in pixel columns.
282 \param s The T.4 context.
283 \param width The number of pixels across the image. */
284SPAN_DECLARE(void) t4_rx_set_image_width(t4_rx_state_t *s, int width);
285
286/*! \brief Set the row-to-row (y) resolution to expect for a received image.
287 \param s The T.4 context.
288 \param resolution The resolution, in pixels per metre. */
289SPAN_DECLARE(void) t4_rx_set_y_resolution(t4_rx_state_t *s, int resolution);
290
291/*! \brief Set the column-to-column (x) resolution to expect for a received image.
292 \param s The T.4 context.
293 \param resolution The resolution, in pixels per metre. */
294SPAN_DECLARE(void) t4_rx_set_x_resolution(t4_rx_state_t *s, int resolution);
295
296/*! \brief Set the DCS information of the fax, for inclusion in the file.
297 \param s The T.4 context.
298 \param dcs The DCS information, formatted as an ASCII string. */
299SPAN_DECLARE(void) t4_rx_set_dcs(t4_rx_state_t *s, const char *dcs);
300
301/*! \brief Set the sub-address of the fax, for inclusion in the file.
302 \param s The T.4 context.
303 \param sub_address The sub-address string. */
304SPAN_DECLARE(void) t4_rx_set_sub_address(t4_rx_state_t *s, const char *sub_address);
305
306/*! \brief Set the identity of the remote machine, for inclusion in the file.
307 \param s The T.4 context.
308 \param ident The identity string. */
309SPAN_DECLARE(void) t4_rx_set_far_ident(t4_rx_state_t *s, const char *ident);
310
311/*! \brief Set the vendor of the remote machine, for inclusion in the file.
312 \param s The T.4 context.
313 \param vendor The vendor string, or NULL. */
314SPAN_DECLARE(void) t4_rx_set_vendor(t4_rx_state_t *s, const char *vendor);
315
316/*! \brief Set the model of the remote machine, for inclusion in the file.
317 \param s The T.4 context.
318 \param model The model string, or NULL. */
319SPAN_DECLARE(void) t4_rx_set_model(t4_rx_state_t *s, const char *model);
320
321/*! Get the current image transfer statistics.
322 \brief Get the current transfer statistics.
323 \param s The T.4 context.
324 \param t A pointer to a statistics structure. */
325SPAN_DECLARE(void) t4_rx_get_transfer_statistics(t4_rx_state_t *s, t4_stats_t *t);
326
327/*! Get the short text name of an encoding format.
328 \brief Get the short text name of an encoding format.
329 \param encoding The encoding type.
330 \return A pointer to the string. */
331SPAN_DECLARE(const char *) t4_encoding_to_str(int encoding);
332
333/*! \brief Prepare for reception of a document.
334 \param s The T.4 context.
335 \param file The name of the file to be received.
336 \param output_encoding The output encoding.
337 \return A pointer to the context, or NULL if there was a problem. */
338SPAN_DECLARE(t4_rx_state_t *) t4_rx_init(t4_rx_state_t *s, const char *file, int output_encoding);
339
340/*! \brief End reception of a document. Tidy up and close the file.
341 This should be used to end T.4 reception started with
342 t4_rx_init.
343 \param s The T.4 receive context.
344 \return 0 for success, otherwise -1. */
345SPAN_DECLARE(int) t4_rx_release(t4_rx_state_t *s);
346
347/*! \brief End reception of a document. Tidy up, close the file and
348 free the context. This should be used to end T.4 reception
349 started with t4_rx_init.
350 \param s The T.4 receive context.
351 \return 0 for success, otherwise -1. */
352SPAN_DECLARE(int) t4_rx_free(t4_rx_state_t *s);
353
354#if defined(__cplusplus)
355}
356#endif
357
358#endif
359/*- End of file ------------------------------------------------------------*/
t4_state_s
Definition private/t4_tx.h:36
t4_stats_t
Definition t4_rx.h:213
t4_stats_t::pages_transferred
int pages_transferred
The number of pages transferred so far.
Definition t4_rx.h:215
t4_stats_t::length
int length
The number of vertical pixels in the most recent page.
Definition t4_rx.h:221
t4_stats_t::pages_in_file
int pages_in_file
The number of pages in the file (<0 if unknown).
Definition t4_rx.h:217
t4_stats_t::y_resolution
int y_resolution
The vertical resolution of the page in pixels per metre.
Definition t4_rx.h:229
t4_stats_t::bad_rows
int bad_rows
The number of bad pixel rows in the most recent page.
Definition t4_rx.h:223
t4_stats_t::encoding
int encoding
The type of compression used between the FAX machines.
Definition t4_rx.h:231
t4_stats_t::x_resolution
int x_resolution
The horizontal resolution of the page in pixels per metre.
Definition t4_rx.h:227
t4_stats_t::longest_bad_row_run
int longest_bad_row_run
The largest number of bad pixel rows in a block in the most recent page.
Definition t4_rx.h:225
t4_stats_t::line_image_size
int line_image_size
The size of the image on the line, in bytes.
Definition t4_rx.h:233
t4_stats_t::width
int width
The number of horizontal pixels in the most recent page.
Definition t4_rx.h:219
t4_rx_set_sub_address
void t4_rx_set_sub_address(t4_rx_state_t *s, const char *sub_address)
Set the sub-address of the fax, for inclusion in the file.
Definition t4_rx.c:1109
t4_image_compression_t
t4_image_compression_t
Definition t4_rx.h:50
T4_COMPRESSION_ITU_T85
@ T4_COMPRESSION_ITU_T85
Definition t4_rx.h:60
T4_COMPRESSION_ITU_T43
@ T4_COMPRESSION_ITU_T43
Definition t4_rx.h:64
T4_COMPRESSION_ITU_T6
@ T4_COMPRESSION_ITU_T6
Definition t4_rx.h:58
T4_COMPRESSION_ITU_T85_L0
@ T4_COMPRESSION_ITU_T85_L0
Definition t4_rx.h:62
T4_COMPRESSION_ITU_T4_2D
@ T4_COMPRESSION_ITU_T4_2D
Definition t4_rx.h:56
T4_COMPRESSION_NONE
@ T4_COMPRESSION_NONE
Definition t4_rx.h:52
T4_COMPRESSION_ITU_T42
@ T4_COMPRESSION_ITU_T42
Definition t4_rx.h:68
T4_COMPRESSION_ITU_T4_1D
@ T4_COMPRESSION_ITU_T4_1D
Definition t4_rx.h:54
T4_COMPRESSION_ITU_T45
@ T4_COMPRESSION_ITU_T45
Definition t4_rx.h:66
T4_COMPRESSION_ITU_SYCC_T42
@ T4_COMPRESSION_ITU_SYCC_T42
Definition t4_rx.h:70
t4_rx_put_byte
int t4_rx_put_byte(t4_rx_state_t *s, uint8_t byte)
Put a byte of the current document page.
Definition t4_rx.c:981
t4_rx_start_page
int t4_rx_start_page(t4_rx_state_t *s)
Prepare to receive the next page of the current document.
Definition t4_rx.c:1010
t4_rx_set_row_write_handler
int t4_rx_set_row_write_handler(t4_rx_state_t *s, t4_row_write_handler_t handler, void *user_data)
Set the row write handler for a T.4 receive context.
Definition t4_rx.c:1002
t4_rx_set_far_ident
void t4_rx_set_far_ident(t4_rx_state_t *s, const char *ident)
Set the identity of the remote machine, for inclusion in the file.
Definition t4_rx.c:1115
t4_image_x_resolution_t
t4_image_x_resolution_t
Definition t4_rx.h:85
t4_state_t
struct t4_state_s t4_state_t
Definition t4_rx.h:206
t4_image_width_t
t4_image_width_t
Definition t4_rx.h:125
t4_image_types_t
t4_image_types_t
Definition t4_rx.h:75
t4_rx_put_chunk
int t4_rx_put_chunk(t4_rx_state_t *s, const uint8_t buf[], int len)
Put a byte of the current document page.
Definition t4_rx.c:987
t4_rx_set_x_resolution
void t4_rx_set_x_resolution(t4_rx_state_t *s, int resolution)
Set the column-to-column (x) resolution to expect for a received image.
Definition t4_rx.c:1097
t4_rx_set_model
void t4_rx_set_model(t4_rx_state_t *s, const char *model)
Set the model of the remote machine, for inclusion in the file.
Definition t4_rx.c:1127
t4_encoding_to_str
const char * t4_encoding_to_str(int encoding)
Get the short text name of an encoding format.
Definition t4_rx.c:1148
t4_rx_free
int t4_rx_free(t4_rx_state_t *s)
End reception of a document. Tidy up, close the file and free the context. This should be used to end...
Definition t4_rx.c:1246
t4_rx_set_rx_encoding
void t4_rx_set_rx_encoding(t4_rx_state_t *s, int encoding)
Set the encoding for the received data.
Definition t4_rx.c:1079
t4_image_length_t
t4_image_length_t
Definition t4_rx.h:167
t4_rx_set_image_width
void t4_rx_set_image_width(t4_rx_state_t *s, int width)
Set the expected width of the received image, in pixel columns.
Definition t4_rx.c:1085
t4_rx_get_transfer_statistics
void t4_rx_get_transfer_statistics(t4_rx_state_t *s, t4_stats_t *t)
Get the current transfer statistics.
Definition t4_rx.c:1133
t4_image_y_resolution_t
t4_image_y_resolution_t
Definition t4_rx.h:97
t4_rx_put_bit
int t4_rx_put_bit(t4_rx_state_t *s, int bit)
Put a bit of the current document page.
Definition t4_rx.c:975
t4_rx_release
int t4_rx_release(t4_rx_state_t *s)
End reception of a document. Tidy up and close the file. This should be used to end T....
Definition t4_rx.c:1235
t4_rx_init
t4_rx_state_t * t4_rx_init(t4_rx_state_t *s, const char *file, int output_encoding)
Prepare for reception of a document.
Definition t4_rx.c:1177
t4_row_write_handler_t
int(* t4_row_write_handler_t)(void *user_data, const uint8_t buf[], size_t len)
Definition t4_rx.h:46
t4_rx_set_vendor
void t4_rx_set_vendor(t4_rx_state_t *s, const char *vendor)
Set the vendor of the remote machine, for inclusion in the file.
Definition t4_rx.c:1121
t4_rx_set_dcs
void t4_rx_set_dcs(t4_rx_state_t *s, const char *dcs)
Set the DCS information of the fax, for inclusion in the file.
Definition t4_rx.c:1103
t4_rx_set_y_resolution
void t4_rx_set_y_resolution(t4_rx_state_t *s, int resolution)
Set the row-to-row (y) resolution to expect for a received image.
Definition t4_rx.c:1091
t4_rx_end_page
int t4_rx_end_page(t4_rx_state_t *s)
Complete the reception of a page.
Definition t4_rx.c:581