Kicking it Olde Sküül! Burgerlib on Github Follow Olde Sküül on Twitter Burgerbecky on LinkedIn Burgerbecky on LinkedIn
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Private Types | Private Attributes | Static Private Attributes | List of all members
Burger::GIFEncoder Class Reference

GIF style LZW compressor. More...

Collaboration diagram for Burger::GIFEncoder:
Collaboration graph
[legend]

Public Member Functions

 GIFEncoder ()
 Init the encoder to an inert state.
 
uint_t GetClearCode (void) const
 Return the LZW Clear code.
 
uint_t GetEOFCode (void) const
 Return the LZW End of File code.
 
void Init (OutputMemoryStream *pOutput, uint_t uDepth)
 Given a palette depth, create a GIF LZW encoder.
 
void ClearHash (void)
 Erase the GIF LZW hash.
 
uint_t LookupHash (uint_t uInput)
 Given a code, create a hash index.
 
void AddHashEntry (uint_t uKey, uint_t uCode)
 Insert a new code into the hash.
 
void WriteByte (uint_t uInput)
 Write a byte of data to the GIF byte stream.
 
void WriteCode (uint_t uCode)
 Given an LZW code, encode it into the bit stream.
 
void WritePixels (const uint8_t *pInput, uintptr_t uInputLength)
 Encode a scan line of pixels.
 
void Flush (void)
 Encoding is complete, flush the buffer.
 

Static Public Member Functions

static GIFEncoderNew (void)
 Allocate and initialize a GIFEncoder.
 
static uint_t GetHashKey (uint_t uInput)
 Create a hash key.
 
static uint_t GetKey (uint_t uInput)
 Return the LZW key from a hash.
 
static uint_t PutKey (uint_t uInput)
 Prepare a LZW key to form a key.
 
static uint_t GetCode (uint_t uInput)
 Return the LZW code from a hash.
 
static uint_t PutCode (uint_t uInput)
 Prepare a LZW code to form a key.
 

Private Types

enum  eEncodingState { ENCODE_INPROGRESS , ENCODE_FINAL }
 

Private Attributes

OutputMemoryStreamm_pOutput
 Pointer to the stream to store the compressed data.
 
uint_t m_uColorDepth
 Number of bits of color data (8 is the default)
 
uint_t m_uRunningCode
 Current LZW code.
 
uint_t m_uRunningBits
 Number of valid bits in the current LZW code.
 
uint_t m_uMaximumRunningCode
 If the current LZW matches this valid, increase bit size.
 
uint_t m_uCode
 Value being encoded.
 
uint_t m_uShiftState
 Number of bits in the bit bucket.
 
uint_t m_uShiftData
 Bit bucket.
 
eEncodingState m_eEncodingState
 Flag for determining if a flush is needed on destruction.
 
uint_t m_uBytesInBuffer
 Number of LZW bytes cached before a data flush.
 
uint8_t m_Buffer [256]
 Buffer of encoded LZW bytes.
 
uint_t m_HashTable [cHashSize]
 Hash for quick match in the LZW buffer for encoding strings.
 

Static Private Attributes

static const uint_t cHashSize = 1U<<13U
 8192 entry (13 bit) hash table
 
static const uint_t cHashKeyMask = cHashSize-1U
 Mask for the hash size.
 
static const uint_t cLZBits = 12U
 Number of bits for an LZW token.
 
static const uint_t cLZLastCode = (1U<<cLZBits)-1U
 Largest allowable LZW token.
 
static const uint_t cStartingCode = (1U<<cLZBits)
 Impossible LZW token for alerting encoding hasn't started.
 

Detailed Description

GIF style LZW compressor.


This class will compress pixel data into using GIF LZW format

See also
FileGIF or GIFDecoder

Member Enumeration Documentation

◆ eEncodingState

Enumerator
ENCODE_INPROGRESS 

Encoding is in progress.

ENCODE_FINAL 

Encoding is complete, don't output to the stream anymore.

Constructor & Destructor Documentation

◆ GIFEncoder()

Burger::GIFEncoder::GIFEncoder ( )

Init the encoder to an inert state.


Member Function Documentation

◆ AddHashEntry()

void Burger::GIFEncoder::AddHashEntry ( uint_t uKey,
uint_t uCode )

Insert a new code into the hash.


Given a hash key, look for an empty entry in the hash and insert the LZW token into it.

Parameters
uKeyHash key (Index into the hash)
uCodeLZW code

◆ ClearHash()

void Burger::GIFEncoder::ClearHash ( void )

Erase the GIF LZW hash.


◆ Flush()

void Burger::GIFEncoder::Flush ( void )

Encoding is complete, flush the buffer.


At the conclusion of compression, call this function to perform cleanup and flush out any cached data

◆ GetClearCode()

uint_t Burger::GIFEncoder::GetClearCode ( void ) const
inline

Return the LZW Clear code.


Based on the bit depth, return the code to clear the LZW cache

Returns
LZW clear code

◆ GetCode()

uint_t Burger::GIFEncoder::GetCode ( uint_t uInput)
inlinestatic

Return the LZW code from a hash.


Parameters
uInputHash value
Returns
LZW code

◆ GetEOFCode()

uint_t Burger::GIFEncoder::GetEOFCode ( void ) const
inline

Return the LZW End of File code.


Based on the bit depth, return the code to end decompression of LZW data

Returns
LZW EOF code

◆ GetHashKey()

uint_t Burger::GIFEncoder::GetHashKey ( uint_t uInput)
inlinestatic

Create a hash key.


Given a 16 bit value of the current and previous pixel, return a hash value to index into the hash table

Parameters
uInputValue with LZW code shifted left by 8 bits and the pixel in the lower 8 bits
Returns
Hash of the key

◆ GetKey()

uint_t Burger::GIFEncoder::GetKey ( uint_t uInput)
inlinestatic

Return the LZW key from a hash.


Parameters
uInputHash value
Returns
LZW Key

◆ Init()

void Burger::GIFEncoder::Init ( Burger::OutputMemoryStream * pOutput,
uint_t uDepth )

Given a palette depth, create a GIF LZW encoder.


Call this function before any encoding is performed.

Parameters
pOutputPointer to a valid OutputMemoryStream to store the compressed data
uDepthDepth of the pixel data in bits (2-8)

◆ LookupHash()

uint_t Burger::GIFEncoder::LookupHash ( uint_t uInput)

Given a code, create a hash index.


Scan the hash table for a LZW token or return BURGER_MAXUINT if there is no matching entry.

Parameters
uInputEncoded pixel and previous pixel
Returns
BURGER_MAXUINT on error or a valid 12 bit hash index

◆ New()

Burger::GIFEncoder *BURGER_API Burger::GIFEncoder::New ( void )
static

Allocate and initialize a GIFEncoder.


Returns
A pointer to a default GIFEncoder class or NULL if out of memory
See also
Delete(const T *)

◆ PutCode()

uint_t Burger::GIFEncoder::PutCode ( uint_t uInput)
inlinestatic

Prepare a LZW code to form a key.


Parameters
uInputLZW code
Returns
Code shifted to a position for a key

◆ PutKey()

uint_t Burger::GIFEncoder::PutKey ( uint_t uInput)
inlinestatic

Prepare a LZW key to form a key.


Parameters
uInputLZW token
Returns
Token shifted to a position for a key

◆ WriteByte()

void Burger::GIFEncoder::WriteByte ( uint_t uInput)

Write a byte of data to the GIF byte stream.


Cache a byte of output data and send it out as a "pascal" string with a length followed by a stream of data. Only write to the stream when 255 bytes have been accumulated

Parameters
uInput8 bit data to write to the byte stream

◆ WriteCode()

void Burger::GIFEncoder::WriteCode ( uint_t uCode)

Given an LZW code, encode it into the bit stream.


Write out an LZW code using bit packing.

Parameters
uCodeBit pattern for LZW code

◆ WritePixels()

void Burger::GIFEncoder::WritePixels ( const uint8_t * pInput,
uintptr_t uInputLength )

Encode a scan line of pixels.


Given a scan line of pixels, compress using LZW. Assume the pixels are bytes.

Parameters
pInputPointer to an array of pixels for a single scan line
uInputLengthNumber of pixels to encode

Member Data Documentation

◆ cHashKeyMask

const uint_t Burger::GIFEncoder::cHashKeyMask = cHashSize-1U
staticprivate

Mask for the hash size.

◆ cHashSize

const uint_t Burger::GIFEncoder::cHashSize = 1U<<13U
staticprivate

8192 entry (13 bit) hash table

◆ cLZBits

const uint_t Burger::GIFEncoder::cLZBits = 12U
staticprivate

Number of bits for an LZW token.

◆ cLZLastCode

const uint_t Burger::GIFEncoder::cLZLastCode = (1U<<cLZBits)-1U
staticprivate

Largest allowable LZW token.

◆ cStartingCode

const uint_t Burger::GIFEncoder::cStartingCode = (1U<<cLZBits)
staticprivate

Impossible LZW token for alerting encoding hasn't started.

◆ m_Buffer

uint8_t Burger::GIFEncoder::m_Buffer[256]
private

Buffer of encoded LZW bytes.

◆ m_eEncodingState

eEncodingState Burger::GIFEncoder::m_eEncodingState
private

Flag for determining if a flush is needed on destruction.

◆ m_HashTable

uint_t Burger::GIFEncoder::m_HashTable[cHashSize]
private

Hash for quick match in the LZW buffer for encoding strings.

◆ m_pOutput

OutputMemoryStream* Burger::GIFEncoder::m_pOutput
private

Pointer to the stream to store the compressed data.

◆ m_uBytesInBuffer

uint_t Burger::GIFEncoder::m_uBytesInBuffer
private

Number of LZW bytes cached before a data flush.

◆ m_uCode

uint_t Burger::GIFEncoder::m_uCode
private

Value being encoded.

◆ m_uColorDepth

uint_t Burger::GIFEncoder::m_uColorDepth
private

Number of bits of color data (8 is the default)

◆ m_uMaximumRunningCode

uint_t Burger::GIFEncoder::m_uMaximumRunningCode
private

If the current LZW matches this valid, increase bit size.

◆ m_uRunningBits

uint_t Burger::GIFEncoder::m_uRunningBits
private

Number of valid bits in the current LZW code.

◆ m_uRunningCode

uint_t Burger::GIFEncoder::m_uRunningCode
private

Current LZW code.

◆ m_uShiftData

uint_t Burger::GIFEncoder::m_uShiftData
private

Bit bucket.

◆ m_uShiftState

uint_t Burger::GIFEncoder::m_uShiftState
private

Number of bits in the bit bucket.