t4_rx.c File Reference
`#include <stdlib.h>`
`#include <inttypes.h>`
`#include <limits.h>`
`#include <stdio.h>`
`#include <fcntl.h>`
`#include <unistd.h>`
`#include <time.h>`
`#include <memory.h>`
`#include <string.h>`
`#include "floating_fudge.h"`
`#include <tiffio.h>`
`#include "spandsp/telephony.h"`
`#include "spandsp/logging.h"`
`#include "spandsp/bit_operations.h"`
`#include "spandsp/async.h"`
`#include "spandsp/timezone.h"`
`#include "spandsp/t4_rx.h"`
`#include "spandsp/t4_tx.h"`
`#include "spandsp/t4_t6_decode.h"`
`#include "spandsp/t4_t6_encode.h"`
`#include "spandsp/version.h"`
`#include "spandsp/private/logging.h"`
`#include "spandsp/private/t4_t6_decode.h"`
`#include "spandsp/private/t4_t6_encode.h"`
`#include "spandsp/private/t4_rx.h"`
`#include "spandsp/private/t4_tx.h"`
`#include "t4_t6_decode_states.h"`

## Macros

#define | CM_PER_INCH 2.54f
#define | EOLS_TO_END_ANY_RX_PAGE 6
#define | EOLS_TO_END_T4_RX_PAGE 5
#define | EOLS_TO_END_T6_RX_PAGE 2
#define | STATE_TRACE(...)

## Functions

int | t4_rx_end_page (t4_rx_state_t *s)
| Complete the reception of a page.
int | t4_rx_put_bit (t4_rx_state_t *s, int bit)
| Put a bit of the current document page.
int | t4_rx_put_byte (t4_rx_state_t *s, uint8_t byte)
| Put a byte of the current document page.
int | t4_rx_put_chunk (t4_rx_state_t *s, const uint8_t buf[], int len)
| Put a byte of the current document page.
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.
int | t4_rx_start_page (t4_rx_state_t *s)
| Prepare to receive the next page of the current document.
void | t4_rx_set_rx_encoding (t4_rx_state_t *s, int encoding)
| Set the encoding for the received data.
void | t4_rx_set_image_width (t4_rx_state_t *s, int width)
| Set the expected width of the received image, in pixel columns.
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.
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.
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.
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.
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.
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.
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.
void | t4_rx_get_transfer_statistics (t4_rx_state_t *s, t4_stats_t *t)
| Get the current transfer statistics.
const char * | t4_encoding_to_str (int encoding)
| Get the short text name of an encoding format.
t4_rx_state_t * | t4_rx_init (t4_rx_state_t *s, const char *file, int output_encoding)
| Prepare for reception of a document.
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.4 reception started with t4_rx_init.
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 T.4 reception started with t4_rx_init.

## Macro Definition Documentation

## CM_PER_INCH

#define CM_PER_INCH 2.54f

The number of centimetres in one inch

## EOLS_TO_END_ANY_RX_PAGE

#define EOLS_TO_END_ANY_RX_PAGE 6

The number of EOLs to expect at the end of a T.4 page

Referenced by t4_rx_end_page().

## EOLS_TO_END_T4_RX_PAGE

#define EOLS_TO_END_T4_RX_PAGE 5

The number of EOLs to check at the end of a T.4 page

## EOLS_TO_END_T6_RX_PAGE

#define EOLS_TO_END_T6_RX_PAGE 2

The number of EOLs to check at the end of a T.6 page

## Function Documentation

## t4_encoding_to_str()

const char * t4_encoding_to_str | ( | int | encoding | ) |

Get the short text name of an encoding format.

Get the short text name of an encoding format.

Parameters

  encoding | The encoding type.

Returns
  A pointer to the string.

References T4_COMPRESSION_ITU_SYCC_T42, T4_COMPRESSION_ITU_T42, T4_COMPRESSION_ITU_T43, T4_COMPRESSION_ITU_T45, T4_COMPRESSION_ITU_T4_1D, T4_COMPRESSION_ITU_T4_2D, T4_COMPRESSION_ITU_T6, T4_COMPRESSION_ITU_T85, T4_COMPRESSION_ITU_T85_L0, and T4_COMPRESSION_NONE.

## t4_rx_end_page()

int t4_rx_end_page | ( | t4_rx_state_t * | s | ) |

Complete the reception of a page.

Parameters

  s | The T.4 receive context.

Returns
  0 for success, otherwise -1.

References t4_state_s::bytes_per_row, t4_t6_decode_state_s::consecutive_eols, t4_t6_decode_state_s::curr_bad_row_run, EOLS_TO_END_ANY_RX_PAGE, t4_state_s::image_buffer, t4_state_s::image_length, t4_state_s::image_size, t4_state_s::line_encoding, t4_state_s::logging, t4_t6_decode_state_s::longest_bad_row_run, t4_t6_decode_state_s::row_write_handler, t4_t6_decode_state_s::row_write_user_data, t4_t6_decode_state_s::rx_bits, t4_t6_decode_state_s::rx_bitstream, t4_t6_decode_state_s::rx_skip_bits, span_log(), T4_COMPRESSION_ITU_T6, and t4_rx_put_bit().

## 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 T.4 reception started with t4_rx_init.

Parameters

  s | The T.4 receive context.

Returns
  0 for success, otherwise -1.

References t4_rx_release().

## 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.

Get the current image transfer statistics.

Parameters

  s | The T.4 context.
  t | A pointer to a statistics structure.

References t4_stats_t::bad_rows, t4_t6_decode_state_s::bad_rows, t4_state_s::current_page, t4_stats_t::encoding, t4_state_s::image_length, t4_state_s::image_width, t4_stats_t::length, t4_state_s::line_encoding, t4_state_s::line_image_size, t4_stats_t::line_image_size, t4_stats_t::longest_bad_row_run, t4_t6_decode_state_s::longest_bad_row_run, t4_stats_t::pages_in_file, t4_tiff_state_t::pages_in_file, t4_stats_t::pages_transferred, t4_tiff_state_t::start_page, t4_state_s::tiff, t4_stats_t::width, t4_state_s::x_resolution, t4_stats_t::x_resolution, t4_state_s::y_resolution, and t4_stats_t::y_resolution.

Referenced by t30_get_transfer_statistics().

## 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.

Parameters

  s | The T.4 context.
  file | The name of the file to be received.
  output_encoding | The output encoding.

Returns
  A pointer to the context, or NULL if there was a problem.

References t4_state_s::bytes_per_row, t4_state_s::current_page, t4_tiff_state_t::file, t4_state_s::image_buffer, t4_state_s::image_buffer_size, t4_state_s::image_width, t4_state_s::logging, t4_tiff_state_t::output_compression, t4_tiff_state_t::output_t4_options, t4_tiff_state_t::pages_in_file, t4_state_s::rx, span_log(), t4_tiff_state_t::start_page, t4_tiff_state_t::stop_page, T4_COMPRESSION_ITU_T4_1D, T4_COMPRESSION_ITU_T4_2D, T4_COMPRESSION_ITU_T6, t4_state_s::tiff, t4_state_s::x_resolution, and t4_state_s::y_resolution.

## t4_rx_put_bit()

int t4_rx_put_bit | ( | t4_rx_state_t * | s,
|  | int | bit )

Put a bit of the current document page.

Parameters

  s | The T.4 context.
  bit | The data bit.

Returns
  TRUE when the bit ends the document page, otherwise FALSE.

Referenced by t4_rx_end_page().

## 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.

Parameters

  s | The T.4 context.
  byte | The data byte.

Returns
  TRUE when the byte ends the document page, otherwise FALSE.

Referenced by t30_non_ecm_put_byte().

## 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.

Parameters

  s | The T.4 context.
  buf | The buffer containing the chunk.
  len | The length of the chunk.

Returns
  TRUE when the byte ends the document page, otherwise FALSE.

Referenced by t30_non_ecm_put_chunk().

## 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.4 reception started with t4_rx_init.

Parameters

  s | The T.4 receive context.

Returns
  0 for success, otherwise -1.

References t4_state_s::rx, t4_state_s::tiff, and t4_tiff_state_t::tiff_file.

Referenced by t4_rx_free().

## 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.

Parameters

  s | The T.4 context.
  dcs | The DCS information, formatted as an ASCII string.

References t4_rx_metadata_t::dcs, and t4_state_s::tiff.

## 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.

Parameters

  s | The T.4 context.
  ident | The identity string.

References t4_rx_metadata_t::far_ident, and t4_state_s::tiff.

## 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.

Parameters

  s | The T.4 context.
  width | The number of pixels across the image.

References t4_state_s::image_width.

## 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.

Parameters

  s | The T.4 context.
  model | The model string, or NULL.

References t4_rx_metadata_t::model, and t4_state_s::tiff.

## 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.

Parameters

  s | The T.4 receive context.
  handler | A pointer to the handler routine.
  user_data | An opaque pointer passed to the handler routine.

Returns
  0 for success, otherwise -1.

References t4_t6_decode_state_s::row_write_handler, and t4_t6_decode_state_s::row_write_user_data.

## 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.

Parameters

  s | The T.4 context.
  encoding | The encoding.

References t4_state_s::line_encoding.

## 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.

Parameters

  s | The T.4 context.
  sub_address | The sub-address string.

References t4_rx_metadata_t::sub_address, and t4_state_s::tiff.

## 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.

Parameters

  s | The T.4 context.
  vendor | The vendor string, or NULL.

References t4_state_s::tiff, and t4_rx_metadata_t::vendor.

## 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.

Parameters

  s | The T.4 context.
  resolution | The resolution, in pixels per metre.

References t4_state_s::x_resolution.

## 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.

Parameters

  s | The T.4 context.
  resolution | The resolution, in pixels per metre.

References t4_state_s::y_resolution.

## t4_rx_start_page()

int t4_rx_start_page | ( | t4_rx_state_t * | s | ) |

Prepare to receive the next page of the current document.

Parameters

  s | The T.4 context.

Returns
  zero for success, -1 for failure.

References t4_t6_decode_state_s::a0, t4_t6_decode_state_s::a_cursor, t4_t6_decode_state_s::b1, t4_t6_decode_state_s::b_cursor, t4_t6_decode_state_s::bad_rows, t4_t6_decode_state_s::black_white, t4_state_s::bytes_per_row, t4_t6_decode_state_s::consecutive_eols, t4_state_s::cur_runs, t4_t6_decode_state_s::curr_bad_row_run, t4_state_s::image_length, t4_state_s::image_size, t4_state_s::image_width, t4_t6_decode_state_s::its_black, t4_t6_decode_state_s::last_row_starts_at, t4_state_s::line_encoding, t4_state_s::line_image_size, t4_state_s::logging, t4_t6_decode_state_s::longest_bad_row_run, t4_state_s::max_row_bits, t4_state_s::min_row_bits, t4_state_s::page_start_time, t4_state_s::ref_runs, t4_state_s::row_bits, t4_state_s::row_is_2d, t4_state_s::row_len, t4_t6_decode_state_s::run_length, t4_t6_decode_state_s::rx_bits, t4_t6_decode_state_s::rx_bitstream, t4_t6_decode_state_s::rx_skip_bits, span_log(), T4_COMPRESSION_ITU_T6, t4_state_s::tiff, t4_tiff_state_t::tiff_file, t4_state_s::tx_bits, and t4_state_s::tx_bitstream.