2012-11-26 18:58:24 +00:00
|
|
|
/*
|
|
|
|
===========================================================================
|
|
|
|
|
|
|
|
Doom 3 BFG Edition GPL Source Code
|
2012-11-28 15:47:07 +00:00
|
|
|
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
|
|
|
|
|
|
|
|
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
|
|
|
|
|
|
|
|
===========================================================================
|
|
|
|
*/
|
|
|
|
#pragma hdrstop
|
2012-12-02 21:37:32 +00:00
|
|
|
#include "../idlib/precompiled.h"
|
2012-11-26 18:58:24 +00:00
|
|
|
#include "LightweightCompression.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
HashIndex
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
static int HashIndex( int w, int k )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return ( w ^ k ) & idLZWCompressor::HASH_MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idLZWCompressor::Start
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idLZWCompressor::Start( uint8* data_, int maxSize_, bool append )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// Clear hash
|
|
|
|
ClearHash();
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
if( append )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( lzwData->nextCode > LZW_FIRST_CODE );
|
|
|
|
|
|
|
|
int originalNextCode = lzwData->nextCode;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
lzwData->nextCode = LZW_FIRST_CODE;
|
|
|
|
|
|
|
|
// If we are appending, then fill up the hash
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = LZW_FIRST_CODE; i < originalNextCode; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
AddToDict( lzwData->dictionaryW[i], lzwData->dictionaryK[i] );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( originalNextCode == lzwData->nextCode );
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( int i = 0; i < LZW_FIRST_CODE; i++ )
|
|
|
|
{
|
|
|
|
lzwData->dictionaryK[i] = ( uint8 )i;
|
2012-11-26 18:58:24 +00:00
|
|
|
lzwData->dictionaryW[i] = 0xFFFF;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
lzwData->nextCode = LZW_FIRST_CODE;
|
|
|
|
lzwData->codeBits = LZW_START_BITS;
|
|
|
|
lzwData->codeWord = -1;
|
|
|
|
lzwData->tempValue = 0;
|
|
|
|
lzwData->tempBits = 0;
|
|
|
|
lzwData->bytesWritten = 0;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
oldCode = -1; // Used by DecompressBlock
|
|
|
|
data = data_;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
blockSize = 0;
|
|
|
|
blockIndex = 0;
|
|
|
|
|
|
|
|
bytesRead = 0;
|
|
|
|
|
|
|
|
maxSize = maxSize_;
|
|
|
|
overflowed = false;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
savedBytesWritten = 0;
|
|
|
|
savedCodeWord = 0;
|
|
|
|
saveCodeBits = 0;
|
|
|
|
savedTempValue = 0;
|
|
|
|
savedTempBits = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idLZWCompressor::ReadBits
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLZWCompressor::ReadBits( int bits )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int bitsToRead = bits - lzwData->tempBits;
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
while( bitsToRead > 0 )
|
|
|
|
{
|
|
|
|
if( bytesRead >= maxSize )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
lzwData->tempValue |= ( uint64 )data[bytesRead++] << lzwData->tempBits;
|
2012-11-26 18:58:24 +00:00
|
|
|
lzwData->tempBits += 8;
|
|
|
|
bitsToRead -= 8;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
int value = ( int )lzwData->tempValue & ( ( 1 << bits ) - 1 );
|
2012-11-26 18:58:24 +00:00
|
|
|
lzwData->tempValue >>= bits;
|
|
|
|
lzwData->tempBits -= bits;
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idLZWCompressor::WriteBits
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idLZWCompressor::WriteBits( uint32 value, int bits )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
// Queue up bits into temp value
|
2012-11-28 15:47:07 +00:00
|
|
|
lzwData->tempValue |= ( uint64 )value << lzwData->tempBits;
|
2012-11-26 18:58:24 +00:00
|
|
|
lzwData->tempBits += bits;
|
|
|
|
|
|
|
|
// Flush 8 bits (1 byte) at a time ( leftovers will get caught in idLZWCompressor::End() )
|
2012-11-28 15:47:07 +00:00
|
|
|
while( lzwData->tempBits >= 8 )
|
|
|
|
{
|
|
|
|
if( lzwData->bytesWritten >= maxSize )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
overflowed = true;
|
|
|
|
return;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
data[lzwData->bytesWritten++] = ( uint8 )( lzwData->tempValue & 255 );
|
2012-11-26 18:58:24 +00:00
|
|
|
lzwData->tempValue >>= 8;
|
|
|
|
lzwData->tempBits -= 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idLZWCompressor::WriteChain
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
The chain is stored backwards, so we have to write it to a buffer then output the buffer in
|
|
|
|
reverse.
|
2012-11-26 18:58:24 +00:00
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLZWCompressor::WriteChain( int code )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
byte chain[lzwCompressionData_t::LZW_DICT_SIZE];
|
|
|
|
int firstChar = 0;
|
|
|
|
int i = 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
do
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( i < lzwCompressionData_t::LZW_DICT_SIZE && code < lzwCompressionData_t::LZW_DICT_SIZE && code >= 0 );
|
2012-11-28 15:47:07 +00:00
|
|
|
chain[i++] = ( byte )lzwData->dictionaryK[code];
|
2012-11-26 18:58:24 +00:00
|
|
|
code = lzwData->dictionaryW[code];
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
while( code != 0xFFFF );
|
2012-11-26 18:58:24 +00:00
|
|
|
firstChar = chain[--i];
|
2012-11-28 15:47:07 +00:00
|
|
|
for( ; i >= 0; i-- )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
block[blockSize++] = chain[i];
|
|
|
|
}
|
|
|
|
return firstChar;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idLZWCompressor::DecompressBlock
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idLZWCompressor::DecompressBlock()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( blockIndex == blockSize ); // Make sure we've read all we can
|
|
|
|
|
|
|
|
blockIndex = 0;
|
|
|
|
blockSize = 0;
|
|
|
|
|
|
|
|
int firstChar = -1;
|
2012-11-28 15:47:07 +00:00
|
|
|
while( blockSize < LZW_BLOCK_SIZE - lzwCompressionData_t::LZW_DICT_SIZE )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( lzwData->codeBits <= lzwCompressionData_t::LZW_DICT_BITS );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
int code = ReadBits( lzwData->codeBits );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( code == -1 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( oldCode == -1 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( code < 256 );
|
2012-11-28 15:47:07 +00:00
|
|
|
block[blockSize++] = ( uint8 )code;
|
2012-11-26 18:58:24 +00:00
|
|
|
oldCode = code;
|
|
|
|
firstChar = code;
|
|
|
|
continue;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( code >= lzwData->nextCode )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( code == lzwData->nextCode );
|
|
|
|
firstChar = WriteChain( oldCode );
|
2012-11-28 15:47:07 +00:00
|
|
|
block[blockSize++] = ( uint8 )firstChar;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
firstChar = WriteChain( code );
|
|
|
|
}
|
|
|
|
AddToDict( oldCode, firstChar );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( BumpBits() )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
oldCode = -1;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
oldCode = code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idLZWCompressor::ReadByte
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLZWCompressor::ReadByte( bool ignoreOverflow )
|
|
|
|
{
|
|
|
|
if( blockIndex == blockSize )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
DecompressBlock();
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( blockIndex == blockSize ) //-V581 DecompressBlock() updates these values, the if() isn't redundant
|
|
|
|
{
|
|
|
|
if( !ignoreOverflow )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
overflowed = true;
|
|
|
|
assert( !"idLZWCompressor::ReadByte overflowed!" );
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return block[blockIndex++];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idLZWCompressor::WriteByte
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idLZWCompressor::WriteByte( uint8 value )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int code = Lookup( lzwData->codeWord, value );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( code >= 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
lzwData->codeWord = code;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
WriteBits( lzwData->codeWord, lzwData->codeBits );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !BumpBits() )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
AddToDict( lzwData->codeWord, value );
|
|
|
|
}
|
|
|
|
lzwData->codeWord = value;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( lzwData->bytesWritten >= maxSize - ( lzwData->codeBits + lzwData->tempBits + 7 ) / 8 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
overflowed = true; // At any point, if we can't perform an End call, then trigger an overflow
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
2012-11-28 15:47:07 +00:00
|
|
|
idLZWCompressor::Lookup
|
2012-11-26 18:58:24 +00:00
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLZWCompressor::Lookup( int w, int k )
|
|
|
|
{
|
|
|
|
if( w == -1 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return k;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i = HashIndex( w, k );
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int j = hash[i]; j != 0xFFFF; j = nextHash[j] )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( j < lzwCompressionData_t::LZW_DICT_SIZE );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( lzwData->dictionaryK[j] == k && lzwData->dictionaryW[j] == w )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
2012-11-28 15:47:07 +00:00
|
|
|
idLZWCompressor::AddToDict
|
2012-11-26 18:58:24 +00:00
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLZWCompressor::AddToDict( int w, int k )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( w < 0xFFFF - 1 );
|
|
|
|
assert( k < 256 );
|
|
|
|
assert( lzwData->nextCode < lzwCompressionData_t::LZW_DICT_SIZE );
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
lzwData->dictionaryK[lzwData->nextCode] = ( uint8 )k;
|
|
|
|
lzwData->dictionaryW[lzwData->nextCode] = ( uint16 )w;
|
2012-11-26 18:58:24 +00:00
|
|
|
int i = HashIndex( w, k );
|
|
|
|
nextHash[lzwData->nextCode] = hash[i];
|
2012-11-28 15:47:07 +00:00
|
|
|
hash[i] = ( uint16 )lzwData->nextCode;
|
2012-11-26 18:58:24 +00:00
|
|
|
return lzwData->nextCode++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idLZWCompressor::BumpBits
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
Possibly increments codeBits.
|
2012-11-28 15:47:07 +00:00
|
|
|
return: bool - true, if the dictionary was cleared.
|
2012-11-26 18:58:24 +00:00
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
bool idLZWCompressor::BumpBits()
|
|
|
|
{
|
|
|
|
if( lzwData->nextCode == ( 1 << lzwData->codeBits ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
lzwData->codeBits ++;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( lzwData->codeBits > lzwCompressionData_t::LZW_DICT_BITS )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
lzwData->nextCode = LZW_FIRST_CODE;
|
|
|
|
lzwData->codeBits = LZW_START_BITS;
|
|
|
|
ClearHash();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idLZWCompressor::End
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLZWCompressor::End()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( lzwData->tempBits < 8 );
|
|
|
|
assert( lzwData->bytesWritten < maxSize - ( lzwData->codeBits + lzwData->tempBits + 7 ) / 8 );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( ( Length() > 0 ) == ( lzwData->codeWord != -1 ) );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( lzwData->codeWord != -1 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
WriteBits( lzwData->codeWord, lzwData->codeBits );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( lzwData->tempBits > 0 )
|
|
|
|
{
|
|
|
|
if( lzwData->bytesWritten >= maxSize )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
overflowed = true;
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
data[lzwData->bytesWritten++] = ( uint8 )lzwData->tempValue & ( ( 1 << lzwData->tempBits ) - 1 );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Length() > 0 ? Length() : -1; // Total bytes written (or failure)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idLZWCompressor::Save
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idLZWCompressor::Save()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( !overflowed );
|
|
|
|
// Check and make sure we are at a good spot (can call End)
|
|
|
|
assert( lzwData->bytesWritten < maxSize - ( lzwData->codeBits + lzwData->tempBits + 7 ) / 8 );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
savedBytesWritten = lzwData->bytesWritten;
|
|
|
|
savedCodeWord = lzwData->codeWord;
|
|
|
|
saveCodeBits = lzwData->codeBits;
|
|
|
|
savedTempValue = lzwData->tempValue;
|
|
|
|
savedTempBits = lzwData->tempBits;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idLZWCompressor::Restore
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idLZWCompressor::Restore()
|
|
|
|
{
|
|
|
|
lzwData->bytesWritten = savedBytesWritten;
|
2012-11-26 18:58:24 +00:00
|
|
|
lzwData->codeWord = savedCodeWord;
|
|
|
|
lzwData->codeBits = saveCodeBits;
|
|
|
|
lzwData->tempValue = savedTempValue;
|
|
|
|
lzwData->tempBits = savedTempBits;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idLZWCompressor::ClearHash
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idLZWCompressor::ClearHash()
|
|
|
|
{
|
|
|
|
memset( hash, 0xFF, sizeof( hash ) );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idZeroRunLengthCompressor
|
|
|
|
Simple zero based run length encoder/decoder
|
|
|
|
========================
|
|
|
|
*/
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
void idZeroRunLengthCompressor::Start( uint8* dest_, idLZWCompressor* comp_, int maxSize_ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
zeroCount = 0;
|
|
|
|
dest = dest_;
|
|
|
|
comp = comp_;
|
|
|
|
compressed = 0;
|
|
|
|
maxSize = maxSize_;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
bool idZeroRunLengthCompressor::WriteRun()
|
|
|
|
{
|
|
|
|
if( zeroCount > 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( zeroCount <= 255 );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( compressed + 2 > maxSize )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
maxSize = -1;
|
|
|
|
return false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( comp != NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
comp->WriteByte( 0 );
|
2012-11-28 15:47:07 +00:00
|
|
|
comp->WriteByte( ( uint8 )zeroCount );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
*dest++ = 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
*dest++ = ( uint8 )zeroCount;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
compressed += 2;
|
|
|
|
zeroCount = 0;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
bool idZeroRunLengthCompressor::WriteByte( uint8 value )
|
|
|
|
{
|
|
|
|
if( value != 0 || zeroCount >= 255 )
|
|
|
|
{
|
|
|
|
if( !WriteRun() )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
maxSize = -1;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
if( value != 0 )
|
|
|
|
{
|
|
|
|
if( compressed + 1 > maxSize )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
maxSize = -1;
|
|
|
|
return false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( comp != NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
comp->WriteByte( value );
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*dest++ = value;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
compressed++;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
zeroCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
byte idZeroRunLengthCompressor::ReadByte()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// See if we need to possibly read more data
|
2012-11-28 15:47:07 +00:00
|
|
|
if( zeroCount == 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int value = ReadInternal();
|
2012-11-28 15:47:07 +00:00
|
|
|
if( value == -1 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( 0 );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( value != 0 )
|
|
|
|
{
|
|
|
|
return ( byte )value; // Return non zero values immediately
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
// Read the number of zeroes
|
|
|
|
zeroCount = ReadInternal();
|
|
|
|
}
|
|
|
|
|
|
|
|
assert( zeroCount > 0 );
|
|
|
|
|
|
|
|
zeroCount--;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
void idZeroRunLengthCompressor::ReadBytes( byte* dest, int count )
|
|
|
|
{
|
|
|
|
for( int i = 0; i < count; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
*dest++ = ReadByte();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
void idZeroRunLengthCompressor::WriteBytes( uint8* src, int count )
|
|
|
|
{
|
|
|
|
for( int i = 0; i < count; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
WriteByte( *src++ );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
int idZeroRunLengthCompressor::End()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
WriteRun();
|
2012-11-28 15:47:07 +00:00
|
|
|
if( maxSize == -1 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return compressed;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
int idZeroRunLengthCompressor::ReadInternal()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
compressed++;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( comp != NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return comp->ReadByte();
|
|
|
|
}
|
|
|
|
return *dest++;
|
|
|
|
}
|