00001 /* 00002 * SpanDSP - a series of DSP components for telephony 00003 * 00004 * t4.h - definitions for T.4 fax processing 00005 * 00006 * Written by Steve Underwood <steveu@coppice.org> 00007 * 00008 * Copyright (C) 2003 Steve Underwood 00009 * 00010 * All rights reserved. 00011 * 00012 * This program is free software; you can redistribute it and/or modify 00013 * it under the terms of the GNU General Public License version 2, as 00014 * published by the Free Software Foundation. 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU General Public License for more details. 00020 * 00021 * You should have received a copy of the GNU General Public License 00022 * along with this program; if not, write to the Free Software 00023 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00024 * 00025 * $Id: t4.h,v 1.31 2007/04/05 19:20:50 steveu Exp $ 00026 */ 00027 00028 /*! \file */ 00029 00030 #if !defined(_SPANDSP_T4_H_) 00031 #define _SPANDSP_T4_H_ 00032 00033 /*! \page t4_page T.4 image compression and decompression 00034 00035 \section t4_page_sec_1 What does it do? 00036 The T.4 image compression and decompression routines implement the 1D and 2D 00037 encoding methods defined in ITU specification T.4. They also implement the pure 00038 2D encoding method defined in T.6. These are image compression algorithms used 00039 for FAX transmission. 00040 00041 \section t4_page_sec_1 How does it work? 00042 */ 00043 00044 #define T4_COMPRESSION_ITU_T4_1D 1 00045 #define T4_COMPRESSION_ITU_T4_2D 2 00046 #define T4_COMPRESSION_ITU_T6 3 00047 00048 #define T4_X_RESOLUTION_R4 4019 00049 #define T4_X_RESOLUTION_R8 8037 00050 #define T4_X_RESOLUTION_R16 16074 00051 00052 #define T4_Y_RESOLUTION_STANDARD 3850 00053 #define T4_Y_RESOLUTION_FINE 7700 00054 #define T4_Y_RESOLUTION_SUPERFINE 15400 00055 00056 /*! 00057 T.4 FAX compression/decompression descriptor. This defines the working state 00058 for a single instance of a T.4 FAX compression or decompression channel. 00059 */ 00060 typedef struct 00061 { 00062 /* "Background" information about the FAX, which can be stored in a TIFF file. */ 00063 /*! \brief The vendor of the machine which produced the TIFF file. */ 00064 const char *vendor; 00065 /*! \brief The model of machine which produced the TIFF file. */ 00066 const char *model; 00067 /*! \brief The local ident string. */ 00068 const char *local_ident; 00069 /*! \brief The remote end's ident string. */ 00070 const char *far_ident; 00071 /*! \brief The FAX sub-address. */ 00072 const char *sub_address; 00073 /*! \brief The text which will be used in FAX page header. No text results 00074 in no header line. */ 00075 const char *header_info; 00076 00077 /*! \brief The type of compression used between the FAX machines. */ 00078 int line_encoding; 00079 /*! \brief The minimum number of bits per scan row. This is a timing thing 00080 for hardware FAX machines. */ 00081 int min_scan_line_bits; 00082 00083 int output_compression; 00084 int output_t4_options; 00085 00086 time_t page_start_time; 00087 00088 int bytes_per_row; 00089 int image_size; 00090 int image_buffer_size; 00091 uint8_t *image_buffer; 00092 00093 TIFF *tiff_file; 00094 const char *file; 00095 int start_page; 00096 int stop_page; 00097 00098 int pages_transferred; 00099 /*! Column-to-column (X) resolution in pixels per metre. */ 00100 int x_resolution; 00101 /*! Row-to-row (Y) resolution in pixels per metre. */ 00102 int y_resolution; 00103 /*! Width of the current page, in pixels. */ 00104 int image_width; 00105 /*! Current pixel row number. */ 00106 int row; 00107 /*! Total pixel rows in the current page. */ 00108 int image_length; 00109 /*! The current number of consecutive bad rows. */ 00110 int curr_bad_row_run; 00111 /*! The longest run of consecutive bad rows seen in the current page. */ 00112 int longest_bad_row_run; 00113 /*! The total number of bad rows in the current page. */ 00114 int bad_rows; 00115 00116 /* Decode state */ 00117 uint32_t bits_to_date; 00118 int bits; 00119 00120 /*! \brief This variable is set if we are treating the current row as a 2D encoded 00121 one. */ 00122 int row_is_2d; 00123 int its_black; 00124 int row_len; 00125 /*! \brief This variable is used to record the fact we have seen at least one EOL 00126 since we started decoding. We will not try to interpret the received 00127 data as an image until we have seen the first EOL. */ 00128 int first_eol_seen; 00129 /*! \brief This variable is used to count the consecutive EOLS we have seen. If it 00130 reaches six, this is the end of the image. */ 00131 int consecutive_eols; 00132 00133 /*! \brief B&W runs for reference line */ 00134 uint32_t *ref_runs; 00135 /*! \brief B&W runs for current line */ 00136 uint32_t *cur_runs; 00137 00138 uint32_t *pa; 00139 uint32_t *pb; 00140 int a0; 00141 int b1; 00142 /*! \brief The length of the current run of black or white. */ 00143 int run_length; 00144 int black_white; 00145 00146 uint32_t data; 00147 int bit; 00148 00149 /*! \brief A point into the image buffer indicating where the last row begins */ 00150 int last_row_starts_at; 00151 /*! \brief A point into the image buffer indicating where the current row begins */ 00152 int row_starts_at; 00153 00154 /* Encode state */ 00155 00156 /*! Pointer to the buffer for the current pixel row. */ 00157 uint8_t *row_buf; 00158 00159 int bit_pos; 00160 int bit_ptr; 00161 00162 /*! \brief The reference pixel row for 2D encoding. */ 00163 uint8_t *ref_row_buf; 00164 /*! \brief The maximum contiguous rows that will be 2D encoded. */ 00165 int max_rows_to_next_1d_row; 00166 /*! \brief Number of rows left that can be 2D encoded, before a 1D encoded row 00167 must be used. */ 00168 int rows_to_next_1d_row; 00169 /*! \brief The minimum number of encoded bits per row. */ 00170 int min_row_bits; 00171 /*! \brief The current number of bits in the current encoded row. */ 00172 int row_bits; 00173 00174 /*! \brief Error and flow logging control */ 00175 logging_state_t logging; 00176 } t4_state_t; 00177 00178 /*! 00179 T.4 FAX compression/decompression statistics. 00180 */ 00181 typedef struct 00182 { 00183 /*! \brief The number of pages transferred so far. */ 00184 int pages_transferred; 00185 /*! \brief The number of horizontal pixels in the most recent page. */ 00186 int width; 00187 /*! \brief The number of vertical pixels in the most recent page. */ 00188 int length; 00189 /*! \brief The number of bad pixel rows in the most recent page. */ 00190 int bad_rows; 00191 /*! \brief The largest number of bad pixel rows in a block in the most recent page. */ 00192 int longest_bad_row_run; 00193 /*! \brief The horizontal resolution of the page in pixels per metre */ 00194 int x_resolution; 00195 /*! \brief The vertical resolution of the page in pixels per metre */ 00196 int y_resolution; 00197 /*! \brief The type of compression used between the FAX machines */ 00198 int encoding; 00199 /*! \brief The size of the image, in bytes */ 00200 int image_size; 00201 } t4_stats_t; 00202 00203 #ifdef __cplusplus 00204 extern "C" { 00205 #endif 00206 00207 /*! \brief Allocate a T.4 transmit handling context, and 00208 initialise it. 00209 \param file The name of the file to be received. 00210 \param output_encoding The output encoding. 00211 \return The T.4 context, or NULL. */ 00212 t4_state_t *t4_rx_create(const char *file, int output_encoding); 00213 00214 /*! \brief Prepare for reception of a document. 00215 \param s The T.4 context. 00216 \param file The name of the file to be received. 00217 \param output_encoding The output encoding. 00218 \return 0 for success, otherwise -1. */ 00219 int t4_rx_init(t4_state_t *s, const char *file, int output_encoding); 00220 00221 /*! \brief Prepare to receive the next page of the current document. 00222 \param s The T.4 context. 00223 \return zero for success, -1 for failure. */ 00224 int t4_rx_start_page(t4_state_t *s); 00225 00226 /*! \brief Put a bit of the current document page. 00227 \param s The T.4 context. 00228 \param bit The data bit. 00229 \return TRUE when the bit ends the document page, otherwise FALSE. */ 00230 int t4_rx_put_bit(t4_state_t *s, int bit); 00231 00232 /*! \brief Put a byte of the current document page. 00233 \param s The T.4 context. 00234 \param byte The data byte. 00235 \return TRUE when the byte ends the document page, otherwise FALSE. */ 00236 int t4_rx_put_byte(t4_state_t *s, uint8_t byte); 00237 00238 /*! \brief Put a byte of the current document page. 00239 \param s The T.4 context. 00240 \param buf The buffer containing the chunk. 00241 \param len The length of the chunk. 00242 \return TRUE when the byte ends the document page, otherwise FALSE. */ 00243 int t4_rx_put_chunk(t4_state_t *s, const uint8_t buf[], int len); 00244 00245 /*! \brief Complete the reception of a page. 00246 \param s The T.4 receive context. 00247 \return 0 for success, otherwise -1. */ 00248 int t4_rx_end_page(t4_state_t *s); 00249 00250 /*! \brief End reception of a document. Tidy up, close the file and 00251 free the context. This should be used to end T.4 reception 00252 started with t4_rx_create. 00253 \param s The T.4 receive context. 00254 \return 0 for success, otherwise -1. */ 00255 int t4_rx_delete(t4_state_t *s); 00256 00257 /*! \brief End reception of a document. Tidy up and close the file. 00258 This should be used to end T.4 reception started with 00259 t4_rx_init. 00260 \param s The T.4 context. 00261 \return 0 for success, otherwise -1. */ 00262 int t4_rx_end(t4_state_t *s); 00263 00264 /*! \brief Set the encoding for the received data. 00265 \param s The T.4 context. 00266 \param encoding The encoding. */ 00267 void t4_rx_set_rx_encoding(t4_state_t *s, int encoding); 00268 00269 /*! \brief Set the expected width of the received image, in pixel columns. 00270 \param s The T.4 context. 00271 \param columns The number of pixels across the image. */ 00272 void t4_rx_set_image_width(t4_state_t *s, int width); 00273 00274 /*! \brief Set the row-to-row (y) resolution to expect for a received image. 00275 \param s The T.4 context. 00276 \param resolution The resolution, in pixels per metre. */ 00277 void t4_rx_set_y_resolution(t4_state_t *s, int resolution); 00278 00279 /*! \brief Set the column-to-column (x) resolution to expect for a received image. 00280 \param s The T.4 context. 00281 \param resolution The resolution, in pixels per metre. */ 00282 void t4_rx_set_x_resolution(t4_state_t *s, int resolution); 00283 00284 /*! \brief Set the sub-address of the fax, for inclusion in the file. 00285 \param s The T.4 context. 00286 \param sub_address The sub-address string. */ 00287 void t4_rx_set_sub_address(t4_state_t *s, const char *sub_address); 00288 00289 /*! \brief Set the identity of the remote machine, for inclusion in the file. 00290 \param s The T.4 context. 00291 \param ident The identity string. */ 00292 void t4_rx_set_far_ident(t4_state_t *s, const char *ident); 00293 00294 /*! \brief Set the vendor of the remote machine, for inclusion in the file. 00295 \param s The T.4 context. 00296 \param vendor The vendor string, or NULL. */ 00297 void t4_rx_set_vendor(t4_state_t *s, const char *vendor); 00298 00299 /*! \brief Set the model of the remote machine, for inclusion in the file. 00300 \param s The T.4 context. 00301 \param model The model string, or NULL. */ 00302 void t4_rx_set_model(t4_state_t *s, const char *model); 00303 00304 /*! \brief Allocate a T.4 receive handling context, and 00305 initialise it. 00306 \param s The T.4 context. 00307 \param file The name of the file to be sent. 00308 \return 0 for success, otherwise -1. */ 00309 t4_state_t *t4_tx_create(const char *file, int start_page, int stop_page); 00310 00311 /*! \brief Prepare for transmission of a document. 00312 \param s The T.4 context. 00313 \param file The name of the file to be sent. 00314 \param start_page The first page to send. -1 for no restriction. 00315 \param stop_page The last page to send. -1 for no restriction. 00316 \return The T.4 context, or NULL. */ 00317 int t4_tx_init(t4_state_t *s, const char *file, int start_page, int stop_page); 00318 00319 /*! \brief Prepare to send the next page of the current document. 00320 \param s The T.4 context. 00321 \return zero for success, -1 for failure. */ 00322 int t4_tx_start_page(t4_state_t *s); 00323 00324 /*! \brief Prepare the current page for a resend. 00325 \param s The T.4 context. 00326 \return zero for success, -1 for failure. */ 00327 int t4_tx_restart_page(t4_state_t *s); 00328 00329 /*! \brief Check for the existance of the next page. This information can 00330 be needed before it is determined that the current page is finished with. 00331 \param s The T.4 context. 00332 \return zero for next page found, -1 for failure. */ 00333 int t4_tx_more_pages(t4_state_t *s); 00334 00335 /*! \brief Complete the sending of a page. 00336 \param s The T.4 context. 00337 \return zero for success, -1 for failure. */ 00338 int t4_tx_end_page(t4_state_t *s); 00339 00340 /*! \brief Get the next bit of the current document page. The document will 00341 be padded for the current minimum scan line time. If the 00342 file does not contain an RTC (return to control) code at 00343 the end of the page, one will be added where appropriate. 00344 \param s The T.4 context. 00345 \return The next bit (i.e. 0 or 1). For the last bit of data, bit 1 is 00346 set (i.e. the returned value is 2 or 3). */ 00347 int t4_tx_get_bit(t4_state_t *s); 00348 00349 /*! \brief Get the next byte of the current document page. The document will 00350 be padded for the current minimum scan line time. If the 00351 file does not contain an RTC (return to control) code at 00352 the end of the page, one will be added where appropriate. 00353 \param s The T.4 context. 00354 \return The next byte. For the last byte of data, bit 8 is 00355 set. In this case, one or more bits of the byte may be padded with 00356 zeros, to complete the byte. */ 00357 int t4_tx_get_byte(t4_state_t *s); 00358 00359 /*! \brief Get the next chunk of the current document page. The document will 00360 be padded for the current minimum scan line time. If the 00361 file does not contain an RTC (return to control) code at 00362 the end of the page, one will be added where appropriate. 00363 \param s The T.4 context. 00364 \param buf The buffer into which the chunk is to written. 00365 \param max_len The maximum length of the chunk. 00366 \return The actual length of the chunk. If this is less than max_len it 00367 indicates that the end of the document has been reached. */ 00368 int t4_tx_get_chunk(t4_state_t *s, uint8_t buf[], int max_len); 00369 00370 /*! \brief Return the next bit of the current document page, without actually 00371 moving forward in the buffer. The document will be padded for the 00372 current minimum scan line time. If the file does not contain an 00373 RTC (return to control) code at the end of the page, one will be 00374 added. 00375 \param s The T.4 context. 00376 \return The next bit (i.e. 0 or 1). For the last bit of data, bit 1 is 00377 set (i.e. the returned value is 2 or 3). */ 00378 int t4_tx_check_bit(t4_state_t *s); 00379 00380 /*! \brief End the transmission of a document. Tidy up, close the file and 00381 free the context. This should be used to end T.4 transmission 00382 started with t4_tx_create. 00383 \param s The T.4 context. 00384 \return 0 for success, otherwise -1. */ 00385 int t4_tx_delete(t4_state_t *s); 00386 00387 /*! \brief End the transmission of a document. Tidy up and close the file. 00388 This should be used to end T.4 transmission started with t4_tx_init. 00389 \param s The T.4 context. 00390 \return 0 for success, otherwise -1. */ 00391 int t4_tx_end(t4_state_t *s); 00392 00393 /*! \brief Set the encoding for the encoded data. 00394 \param s The T.4 context. 00395 \param encoding The encoding. */ 00396 void t4_tx_set_tx_encoding(t4_state_t *s, int encoding); 00397 00398 /*! \brief Set the minimum number of encoded bits per row. This allows the 00399 makes the encoding process to be set to comply with the minimum row 00400 time specified by a remote receiving machine. 00401 \param s The T.4 context. 00402 \param bits The minimum number of bits per row. */ 00403 void t4_tx_set_min_row_bits(t4_state_t *s, int bits); 00404 00405 /*! \brief Set the identity of the local machine, for inclusion in page headers. 00406 \param s The T.4 context. 00407 \param ident The identity string. */ 00408 void t4_tx_set_local_ident(t4_state_t *s, const char *ident); 00409 00410 /*! Set the info field, included in the header line included in each page of an encoded 00411 FAX. This is a string of up to 50 characters. Other information (date, local ident, etc.) 00412 are automatically included in the header. If the header info is set to NULL or a zero 00413 length string, no header lines will be added to the encoded FAX. 00414 \brief Set the header info. 00415 \param s The T.4 context. 00416 \param info A string, of up to 50 bytes, which will form the info field. */ 00417 void t4_tx_set_header_info(t4_state_t *s, const char *info); 00418 00419 /*! \brief Get the row-to-row (y) resolution of the current page. 00420 \param s The T.4 context. 00421 \return The resolution, in pixels per metre. */ 00422 int t4_tx_get_y_resolution(t4_state_t *s); 00423 00424 /*! \brief Get the column-to-column (x) resolution of the current page. 00425 \param s The T.4 context. 00426 \return The resolution, in pixels per metre. */ 00427 int t4_tx_get_x_resolution(t4_state_t *s); 00428 00429 /*! \brief Get the width of the current page, in pixel columns. 00430 \param s The T.4 context. 00431 \return The number of columns. */ 00432 int t4_tx_get_image_width(t4_state_t *s); 00433 00434 /*! Get the current image transfer statistics. 00435 \brief Get the current transfer statistics. 00436 \param s The T.4 context. 00437 \param t A pointer to a statistics structure. */ 00438 void t4_get_transfer_statistics(t4_state_t *s, t4_stats_t *t); 00439 00440 /*! Get the short text name of an encoding format. 00441 \brief Get the short text name of an encoding format. 00442 \param encoding The encoding type. 00443 \return A pointer to the string. */ 00444 const char *t4_encoding_to_str(int encoding); 00445 00446 #ifdef __cplusplus 00447 } 00448 #endif 00449 00450 #endif 00451 /*- End of file ------------------------------------------------------------*/