t4.h

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

Generated on Fri Sep 7 23:31:47 2007 for libspandsp by  doxygen 1.5.2