CAPS Universe documentation  1.0.4
All you need to know to be successful
Macros | Functions
encode_method_1027.c File Reference

Implementation of the '1027' encoding mode used by Brother printers. More...

Macros

#define pDEBUG(x, ...)
 
#define WHITE_BYTE   0x00
 

Functions

static void hl_c1027_line_append (struct cm1027_line *c, uint16_t word)
 
static void hl_c1027_sequence_append (struct cm1027_line *c, const uint16_t *pattern, size_t cnt)
 
static uint16_t hl_encoding_vertical (unsigned count)
 
static uint16_t hl_encoding_repeat_word (unsigned count)
 
static uint16_t hl_encoding_repeat_byte (unsigned count, unsigned data)
 
static uint16_t hl_encoding_repeat_nibble (unsigned count, unsigned data)
 
static uint16_t hl_encoding_copy (unsigned count)
 
bool hl_c1027_line_check_if_empty (unsigned cnt, const uint16_t line[cnt])
 
static bool c1027_upper_lower_byte_match (uint16_t data)
 
static bool c1027_all_nibbles_match (uint16_t data)
 
static void hl_1027_plain_data_copy (struct cm1027_line *cline, const uint16_t *pattern, unsigned start, unsigned cnt)
 
void hl_c1027_line_encode (struct cm1027_line *cline, unsigned cnt, const uint16_t line[cnt], const uint16_t *prev)
 
int hl_c1027_block_flush (struct cm1027_block *block, FILE *outstream)
 
int hl_c1027_block_line_add (struct cm1027_block *block, const struct cm1027_line *line, FILE *outstream)
 
int hl_c1027_block_line_twin_add (struct cm1027_block *block, const struct cm1027_line *line, unsigned cnt, FILE *outstream)
 
void hl_c1027_block_init (struct cm1027_block *block, unsigned llength)
 
void hl_c1027_block_exit (struct cm1027_block *block)
 

Detailed Description

Read the corresponding header file for further information about this encoding method.

The printer wants the data to be print as:

Macro Definition Documentation

◆ pDEBUG

#define pDEBUG (   x,
  ... 
)

◆ WHITE_BYTE

#define WHITE_BYTE   0x00

Define the byte value of eight white dots (e.g. not to print)

Function Documentation

◆ hl_c1027_line_append()

static void hl_c1027_line_append ( struct cm1027_line c,
uint16_t  word 
)
static

Append a single word to the end of the line data buffer

Parameters
[in,out]cCompression output buffer
[in]wordThe word to append to the end of the line data buffer

◆ hl_c1027_sequence_append()

static void hl_c1027_sequence_append ( struct cm1027_line c,
const uint16_t *  pattern,
size_t  cnt 
)
static

Append a pattern to the encoded output buffer

Parameters
[in,out]cThe buffer to add the pattern to
[in]patternThe pattern to copy
[in]cntCount of words to copy
Todo:
Do we need to keep the pattern as is? Or do we need to honor the endianess here as well?

◆ hl_encoding_vertical()

static uint16_t hl_encoding_vertical ( unsigned  count)
static

Generate a vertical copy entry

Parameters
[in]countCopy counter
Returns
The encoded compression code word

Copy count words from the previous line

The generated entry is defined to (MSB left):

76543210 76543210 111ccccc cccccccc

With:

ccccccccccccc 13 bit of count

Precondition
count < 8192
Todo:
What does count == 0x00 mean?

◆ hl_encoding_repeat_word()

static uint16_t hl_encoding_repeat_word ( unsigned  count)
static

Generate a horizontal word repeat entry

Parameters
[in]countRepeat counter
Returns
The encoded compression code word

Repeat the next 16 bit word count times

The generated entry is defined to (MSB left):

76543210 76543210 100ccccc cccccccc <word>

With:

ccccccccccccc 13 bit of count

Precondition
count < 8192
Todo:
What does count == 0x00 mean?

◆ hl_encoding_repeat_byte()

static uint16_t hl_encoding_repeat_byte ( unsigned  count,
unsigned  data 
)
static

Generate a horizontal byte repeat entry

Parameters
[in]countRepeat counter
[in]dataRepeat byte twice count times
Returns
The encoded compression code word

Repeat 8 bit data count * 2 times

The generated entry is defined to (MSB left):

76543210 76543210 110ccccc dddddddd

With:

dddddddd data ccccc 6 bit of count

Precondition
count < 32
Todo:
What does a count == 0 mean?

◆ hl_encoding_repeat_nibble()

static uint16_t hl_encoding_repeat_nibble ( unsigned  count,
unsigned  data 
)
static

Generate a horizontal nibble repeat entry

Parameters
[in]countRepeat counter
[in]dataRepeat lower nibble quad count times
Returns
The encoded compression code word

Repeat data nibble count * 4 times

The generated entry is defined to (MSB left):

76543210 76543210 101ddddc cccccccc

With:

dddd lower nibble of data ccccccccc 9 bit of count

Precondition
count < 512

◆ hl_encoding_copy()

static uint16_t hl_encoding_copy ( unsigned  count)
static

No compression, count words of data follow

Parameters
[in]countCount of data words follows
Returns
The encoded compression code word

The generated entry is defined to (MSB left):

76543210 76543210 000ccccc ccccxxxx <word>…

With:

ccccccccc 9 bit of count

Precondition
count < 512

◆ hl_c1027_line_check_if_empty()

bool hl_c1027_line_check_if_empty ( unsigned  cnt,
const uint16_t  line[cnt] 
)

Check if a line is empty, e.g. nothing to print

Parameters
[in]lineThe line data to check
[in]cntCount of words in *line
Return values
trueNothing to print
falseLine contains something to print

If all bits in the line are '0' the line contains nothing to print and can be skipped instead. Empty lines can maybe skipped entirely if they are leading a block.

◆ c1027_upper_lower_byte_match()

static bool c1027_upper_lower_byte_match ( uint16_t  data)
static

Check if upper and lower bytes are the same

Parameters
[in]dataWord to check
Return values
trueIf the upper and lower byte of the word matches (e.g. 0xfefe for example)
falseIf they don't match

◆ c1027_all_nibbles_match()

static bool c1027_all_nibbles_match ( uint16_t  data)
static

Check if all four nibbles are the same

Parameters
[in]dataWord to check
Return values
trueif all nibbles in the word are equal (e.g. 0xeeee for example)
falseif not equal

This function works correctly, if the word has two equal bytes. The algorithm ensure this, by first calling c1027_upper_lower_byte_match() and in the next step this function to check in more detail.

◆ hl_1027_plain_data_copy()

static void hl_1027_plain_data_copy ( struct cm1027_line cline,
const uint16_t *  pattern,
unsigned  start,
unsigned  cnt 
)
static

Append plain pattern to the encoded output buffer

Parameters
[in,out]clineThe buffer to add the pattern to
[in]patternWhere to copy the plain pattern from
[in]startIndex where to start copy plain data from pattern
[in]cntWord count to copy beginning with start from pattern

◆ hl_c1027_line_encode()

void hl_c1027_line_encode ( struct cm1027_line cline,
unsigned  cnt,
const uint16_t  line[cnt],
const uint16_t *  prev 
)

Encode one line into the printer's wire format according to the 1027 method

Parameters
[in,out]clineWhere to store the encoded result
[in]cntCount of words in line
[in]lineThe current line to encode
[in]prevThe previous line for reference (can be NULL)
Precondition
line and prev must be independent

◆ hl_c1027_block_flush()

int hl_c1027_block_flush ( struct cm1027_block block,
FILE *  outstream 
)

Flush out the current block and prepare for the next block

Parameters
[in]blockPrinter data block info
[in,out]outstreamThe stream to send the printer data to
Return values
0on success
-errnoif the data flushing to the printer has failed

This flushes the data out to the printer's tream with a leading PCL command of the form:

  • the ESC char
  • "*b"
  • 'block->bused_cnt * 2 + header length' as an integer ASCII number (variable length)
  • the char 'W' (one byte)
  • the c0127_band_header structure content
  • and then appends the whole block data at once.
Attention
The block, e.g. cm1027_block::block_started gets cleared if called!
Note
'Flushing' here means only to write the accumulated line encoding data including the required data header into the printer's stream. Then everything gets prepared to start the next block. It doesn't mean, the printer receives this data immediately (but it can, if the stream's buffer is already full). The real stream flush should happen when processing of the current page finishes.

◆ hl_c1027_block_line_add()

int hl_c1027_block_line_add ( struct cm1027_block block,
const struct cm1027_line line,
FILE *  outstream 
)
Precondition
A block should never cross a band's boundaries

◆ hl_c1027_block_line_twin_add()

int hl_c1027_block_line_twin_add ( struct cm1027_block block,
const struct cm1027_line line,
unsigned  cnt,
FILE *  outstream 
)

Add one line and a twin of it to the encoded data

Parameters
[in,out]blockBlock info
[in]lineThe line to add to this block
[in]cntFinal length in words of one fully decoded line
[in]outstreamThe IO stream to send the printer data to (on demand)

This function adds two lines to the block for the HQ1200 mode, where the vertical resolution is still 600 DPI but the printer expects the lines for 1200 DPI.

The first line is the line and the second one is a twin of it, encoded as a reuse of the full line content.

◆ hl_c1027_block_init()

void hl_c1027_block_init ( struct cm1027_block block,
unsigned  llength 
)

Init the printer data block as a preparation of hl_c1027_block_line_add()

Parameters
[in,out]blockPrinter data block info
[in]llengthMax line length in count of words

◆ hl_c1027_block_exit()

void hl_c1027_block_exit ( struct cm1027_block block)

Free resources allocated when processing

Parameters
[in]blockPrinter data block info