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.33 2007/05/28 14:05:39 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 int pages_in_file; 00100 /*! Column-to-column (X) resolution in pixels per metre. */ 00101 int x_resolution; 00102 /*! Row-to-row (Y) resolution in pixels per metre. */ 00103 int y_resolution; 00104 /*! Width of the current page, in pixels. */ 00105 int image_width; 00106 /*! Current pixel row number. */ 00107 int row; 00108 /*! Total pixel rows in the current page. */ 00109 int image_length; 00110 /*! The current number of consecutive bad rows. */ 00111 int curr_bad_row_run; 00112 /*! The longest run of consecutive bad rows seen in the current page. */ 00113 int longest_bad_row_run; 00114 /*! The total number of bad rows in the current page. */ 00115 int bad_rows; 00116 00117 /* Decode state */ 00118 uint32_t bits_to_date; 00119 int bits; 00120 00121 /*! \brief This variable is set if we are treating the current row as a 2D encoded 00122 one. */ 00123 int row_is_2d; 00124 int its_black; 00125 int row_len; 00126 /*! \brief This variable is used to record the fact we have seen at least one EOL 00127 since we started decoding. We will not try to interpret the received 00128 data as an image until we have seen the first EOL. */ 00129 int first_eol_seen; 00130 /*! \brief This variable is used to count the consecutive EOLS we have seen. If it 00131 reaches six, this is the end of the image. */ 00132 int consecutive_eols; 00133 00134 /*! \brief B&W runs for reference line */ 00135 uint32_t *ref_runs; 00136 /*! \brief B&W runs for current line */ 00137 uint32_t *cur_runs; 00138 00139 uint32_t *pa; 00140 uint32_t *pb; 00141 int a0; 00142 int b1; 00143 /*! \brief The length of the current run of black or white. */ 00144 int run_length; 00145 int black_white; 00146 00147 uint32_t data; 00148 int bit; 00149 00150 /*! \brief A point into the image buffer indicating where the last row begins */ 00151 int last_row_starts_at; 00152 /*! \brief A point into the image buffer indicating where the current row begins */ 00153 int row_starts_at; 00154 00155 /* Encode state */ 00156 00157 /*! Pointer to the buffer for the current pixel row. */ 00158 uint8_t *row_buf; 00159 00160 int bit_pos; 00161 int bit_ptr; 00162 00163 /*! \brief The reference pixel row for 2D encoding. */ 00164 uint8_t *ref_row_buf; 00165 /*! \brief The maximum contiguous rows that will be 2D encoded. */ 00166 int max_rows_to_next_1d_row; 00167 /*! \brief Number of rows left that can be 2D encoded, before a 1D encoded row 00168 must be used. */ 00169 int rows_to_next_1d_row; 00170 /*! \brief The minimum number of encoded bits per row. */ 00171 int min_row_bits; 00172 /*! \brief The current number of bits in the current encoded row. */ 00173 int row_bits; 00174 00175 /*! \brief Error and flow logging control */ 00176 logging_state_t logging; 00177 } t4_state_t; 00178 00179 /*! 00180 T.4 FAX compression/decompression statistics. 00181 */ 00182 typedef struct 00183 { 00184 /*! \brief The number of pages transferred so far. */ 00185 int pages_transferred; 00186 /*! \brief The number of pages in the file (<0 if unknown). */ 00187 int pages_in_file; 00188 /*! \brief The number of horizontal pixels in the most recent page. */ 00189 int width; 00190 /*! \brief The number of vertical pixels in the most recent page. */ 00191 int length; 00192 /*! \brief The number of bad pixel rows in the most recent page. */ 00193 int bad_rows; 00194 /*! \brief The largest number of bad pixel rows in a block in the most recent page. */ 00195 int longest_bad_row_run; 00196 /*! \brief The horizontal resolution of the page in pixels per metre */ 00197 int x_resolution; 00198 /*! \brief The vertical resolution of the page in pixels per metre */ 00199 int y_resolution; 00200 /*! \brief The type of compression used between the FAX machines */ 00201 int encoding; 00202 /*! \brief The size of the image, in bytes */ 00203 int image_size; 00204 } t4_stats_t; 00205 00206 #if defined(__cplusplus) 00207 extern "C" { 00208 #endif 00209 00210 /*! \brief Allocate a T.4 transmit handling context, and 00211 initialise it. 00212 \param file The name of the file to be received. 00213 \param output_encoding The output encoding. 00214 \return The T.4 context, or NULL. */ 00215 t4_state_t *t4_rx_create(const char *file, int output_encoding); 00216 00217 /*! \brief Prepare for reception of a document. 00218 \param s The T.4 context. 00219 \param file The name of the file to be received. 00220 \param output_encoding The output encoding. 00221 \return 0 for success, otherwise -1. */ 00222 int t4_rx_init(t4_state_t *s, const char *file, int output_encoding); 00223 00224 /*! \brief Prepare to receive the next page of the current document. 00225 \param s The T.4 context. 00226 \return zero for success, -1 for failure. */ 00227 int t4_rx_start_page(t4_state_t *s); 00228 00229 /*! \brief Put a bit of the current document page. 00230 \param s The T.4 context. 00231 \param bit The data bit. 00232 \return TRUE when the bit ends the document page, otherwise FALSE. */ 00233 int t4_rx_put_bit(t4_state_t *s, int bit); 00234 00235 /*! \brief Put a byte of the current document page. 00236 \param s The T.4 context. 00237 \param byte The data byte. 00238 \return TRUE when the byte ends the document page, otherwise FALSE. */ 00239 int t4_rx_put_byte(t4_state_t *s, uint8_t byte); 00240 00241 /*! \brief Put a byte of the current document page. 00242 \param s The T.4 context. 00243 \param buf The buffer containing the chunk. 00244 \param len The length of the chunk. 00245 \return TRUE when the byte ends the document page, otherwise FALSE. */ 00246 int t4_rx_put_chunk(t4_state_t *s, const uint8_t buf[], int len); 00247 00248 /*! \brief Complete the reception of a page. 00249 \param s The T.4 receive context. 00250 \return 0 for success, otherwise -1. */ 00251 int t4_rx_end_page(t4_state_t *s); 00252 00253 /*! \brief End reception of a document. Tidy up, close the file and 00254 free the context. This should be used to end T.4 reception 00255 started with t4_rx_create. 00256 \param s The T.4 receive context. 00257 \return 0 for success, otherwise -1. */ 00258 int t4_rx_delete(t4_state_t *s); 00259 00260 /*! \brief End reception of a document. Tidy up and close the file. 00261 This should be used to end T.4 reception started with 00262 t4_rx_init. 00263 \param s The T.4 context. 00264 \return 0 for success, otherwise -1. */ 00265 int t4_rx_end(t4_state_t *s); 00266 00267 /*! \brief Set the encoding for the received data. 00268 \param s The T.4 context. 00269 \param encoding The encoding. */ 00270 void t4_rx_set_rx_encoding(t4_state_t *s, int encoding); 00271 00272 /*! \brief Set the expected width of the received image, in pixel columns. 00273 \param s The T.4 context. 00274 \param columns The number of pixels across the image. */ 00275 void t4_rx_set_image_width(t4_state_t *s, int width); 00276 00277 /*! \brief Set the row-to-row (y) resolution to expect for a received image. 00278 \param s The T.4 context. 00279 \param resolution The resolution, in pixels per metre. */ 00280 void t4_rx_set_y_resolution(t4_state_t *s, int resolution); 00281 00282 /*! \brief Set the column-to-column (x) resolution to expect for a received image. 00283 \param s The T.4 context. 00284 \param resolution The resolution, in pixels per metre. */ 00285 void t4_rx_set_x_resolution(t4_state_t *s, int resolution); 00286 00287 /*! \brief Set the sub-address of the fax, for inclusion in the file. 00288 \param s The T.4 context. 00289 \param sub_address The sub-address string. */ 00290 void t4_rx_set_sub_address(t4_state_t *s, const char *sub_address); 00291 00292 /*! \brief Set the identity of the remote machine, for inclusion in the file. 00293 \param s The T.4 context. 00294 \param ident The identity string. */ 00295 void t4_rx_set_far_ident(t4_state_t *s, const char *ident); 00296 00297 /*! \brief Set the vendor of the remote machine, for inclusion in the file. 00298 \param s The T.4 context. 00299 \param vendor The vendor string, or NULL. */ 00300 void t4_rx_set_vendor(t4_state_t *s, const char *vendor); 00301 00302 /*! \brief Set the model of the remote machine, for inclusion in the file. 00303 \param s The T.4 context. 00304 \param model The model string, or NULL. */ 00305 void t4_rx_set_model(t4_state_t *s, const char *model); 00306 00307 /*! \brief Allocate a T.4 receive handling context, and 00308 initialise it. 00309 \param s The T.4 context. 00310 \param file The name of the file to be sent. 00311 \return 0 for success, otherwise -1. */ 00312 t4_state_t *t4_tx_create(const char *file, int start_page, int stop_page); 00313 00314 /*! \brief Prepare for transmission of a document. 00315 \param s The T.4 context. 00316 \param file The name of the file to be sent. 00317 \param start_page The first page to send. -1 for no restriction. 00318 \param stop_page The last page to send. -1 for no restriction. 00319 \return The T.4 context, or NULL. */ 00320 int t4_tx_init(t4_state_t *s, const char *file, int start_page, int stop_page); 00321 00322 /*! \brief Prepare to send the next page of the current document. 00323 \param s The T.4 context. 00324 \return zero for success, -1 for failure. */ 00325 int t4_tx_start_page(t4_state_t *s); 00326 00327 /*! \brief Prepare the current page for a resend. 00328 \param s The T.4 context. 00329 \return zero for success, -1 for failure. */ 00330 int t4_tx_restart_page(t4_state_t *s); 00331 00332 /*! \brief Check for the existance of the next page. This information can 00333 be needed before it is determined that the current page is finished with. 00334 \param s The T.4 context. 00335 \return zero for next page found, -1 for failure. */ 00336 int t4_tx_more_pages(t4_state_t *s); 00337 00338 /*! \brief Complete the sending of a page. 00339 \param s The T.4 context. 00340 \return zero for success, -1 for failure. */ 00341 int t4_tx_end_page(t4_state_t *s); 00342 00343 /*! \brief Get the next bit of the current document page. The document will 00344 be padded for the current minimum scan line time. If the 00345 file does not contain an RTC (return to control) code at 00346 the end of the page, one will be added where appropriate. 00347 \param s The T.4 context. 00348 \return The next bit (i.e. 0 or 1). For the last bit of data, bit 1 is 00349 set (i.e. the returned value is 2 or 3). */ 00350 int t4_tx_get_bit(t4_state_t *s); 00351 00352 /*! \brief Get the next byte of the current document page. The document will 00353 be padded for the current minimum scan line time. If the 00354 file does not contain an RTC (return to control) code at 00355 the end of the page, one will be added where appropriate. 00356 \param s The T.4 context. 00357 \return The next byte. For the last byte of data, bit 8 is 00358 set. In this case, one or more bits of the byte may be padded with 00359 zeros, to complete the byte. */ 00360 int t4_tx_get_byte(t4_state_t *s); 00361 00362 /*! \brief Get the next chunk of the current document page. The document will 00363 be padded for the current minimum scan line time. If the 00364 file does not contain an RTC (return to control) code at 00365 the end of the page, one will be added where appropriate. 00366 \param s The T.4 context. 00367 \param buf The buffer into which the chunk is to written. 00368 \param max_len The maximum length of the chunk. 00369 \return The actual length of the chunk. If this is less than max_len it 00370 indicates that the end of the document has been reached. */ 00371 int t4_tx_get_chunk(t4_state_t *s, uint8_t buf[], int max_len); 00372 00373 /*! \brief Return the next bit of the current document page, without actually 00374 moving forward in the buffer. The document will be padded for the 00375 current minimum scan line time. If the file does not contain an 00376 RTC (return to control) code at the end of the page, one will be 00377 added. 00378 \param s The T.4 context. 00379 \return The next bit (i.e. 0 or 1). For the last bit of data, bit 1 is 00380 set (i.e. the returned value is 2 or 3). */ 00381 int t4_tx_check_bit(t4_state_t *s); 00382 00383 /*! \brief End the transmission of a document. Tidy up, close the file and 00384 free the context. This should be used to end T.4 transmission 00385 started with t4_tx_create. 00386 \param s The T.4 context. 00387 \return 0 for success, otherwise -1. */ 00388 int t4_tx_delete(t4_state_t *s); 00389 00390 /*! \brief End the transmission of a document. Tidy up and close the file. 00391 This should be used to end T.4 transmission started with t4_tx_init. 00392 \param s The T.4 context. 00393 \return 0 for success, otherwise -1. */ 00394 int t4_tx_end(t4_state_t *s); 00395 00396 /*! \brief Set the encoding for the encoded data. 00397 \param s The T.4 context. 00398 \param encoding The encoding. */ 00399 void t4_tx_set_tx_encoding(t4_state_t *s, int encoding); 00400 00401 /*! \brief Set the minimum number of encoded bits per row. This allows the 00402 makes the encoding process to be set to comply with the minimum row 00403 time specified by a remote receiving machine. 00404 \param s The T.4 context. 00405 \param bits The minimum number of bits per row. */ 00406 void t4_tx_set_min_row_bits(t4_state_t *s, int bits); 00407 00408 /*! \brief Set the identity of the local machine, for inclusion in page headers. 00409 \param s The T.4 context. 00410 \param ident The identity string. */ 00411 void t4_tx_set_local_ident(t4_state_t *s, const char *ident); 00412 00413 /*! Set the info field, included in the header line included in each page of an encoded 00414 FAX. This is a string of up to 50 characters. Other information (date, local ident, etc.) 00415 are automatically included in the header. If the header info is set to NULL or a zero 00416 length string, no header lines will be added to the encoded FAX. 00417 \brief Set the header info. 00418 \param s The T.4 context. 00419 \param info A string, of up to 50 bytes, which will form the info field. */ 00420 void t4_tx_set_header_info(t4_state_t *s, const char *info); 00421 00422 /*! \brief Get the row-to-row (y) resolution of the current page. 00423 \param s The T.4 context. 00424 \return The resolution, in pixels per metre. */ 00425 int t4_tx_get_y_resolution(t4_state_t *s); 00426 00427 /*! \brief Get the column-to-column (x) resolution of the current page. 00428 \param s The T.4 context. 00429 \return The resolution, in pixels per metre. */ 00430 int t4_tx_get_x_resolution(t4_state_t *s); 00431 00432 /*! \brief Get the width of the current page, in pixel columns. 00433 \param s The T.4 context. 00434 \return The number of columns. */ 00435 int t4_tx_get_image_width(t4_state_t *s); 00436 00437 /*! \brief Get the number of pages in the file. 00438 \param s The T.4 context. 00439 \return The number of pages, or -1 if there is an error. */ 00440 int t4_tx_get_pages_in_file(t4_state_t *s); 00441 00442 /*! Get the current image transfer statistics. 00443 \brief Get the current transfer statistics. 00444 \param s The T.4 context. 00445 \param t A pointer to a statistics structure. */ 00446 void t4_get_transfer_statistics(t4_state_t *s, t4_stats_t *t); 00447 00448 /*! Get the short text name of an encoding format. 00449 \brief Get the short text name of an encoding format. 00450 \param encoding The encoding type. 00451 \return A pointer to the string. */ 00452 const char *t4_encoding_to_str(int encoding); 00453 00454 #if defined(__cplusplus) 00455 } 00456 #endif 00457 00458 #endif 00459 /*- End of file ------------------------------------------------------------*/