diff options
author | Julien Nabet <serval2412@yahoo.fr> | 2022-05-15 12:09:02 +0200 |
---|---|---|
committer | Julien Nabet <serval2412@yahoo.fr> | 2022-05-15 14:40:12 +0200 |
commit | 63078e284fb088a5ddd81824dc962775588cade9 (patch) | |
tree | d266b1b498970712390fbe3802598f2fb133bdf5 /vcl | |
parent | 203609a393cd6f60bbb6b9e62023619c6bac30d8 (diff) |
vcl/tiff-import-lzw various changes
Seen during tdf#119686 investigation:
- Prefix private members + add some spaces for readability
- Pragma once
- Declare constexpr CODE_CLEAR, CODE_EOI and CODE_FIRSTMULTICHAR
- Rename m_nTableSize to m_nTableCurrentId
- Use constexpr instead of #define for a const
- m_bFirst useless
- Reinitialize at each StartDecompression call
Change-Id: I6342f3b25bef94950a152186db07aa11ecc6cd5e
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134340
Tested-by: Jenkins
Reviewed-by: Julien Nabet <serval2412@yahoo.fr>
Diffstat (limited to 'vcl')
-rw-r--r-- | vcl/source/filter/itiff/lzwdecom.cxx | 186 | ||||
-rw-r--r-- | vcl/source/filter/itiff/lzwdecom.hxx | 27 |
2 files changed, 106 insertions, 107 deletions
diff --git a/vcl/source/filter/itiff/lzwdecom.cxx b/vcl/source/filter/itiff/lzwdecom.cxx index 0f8483708179..cf2ed67abc4e 100644 --- a/vcl/source/filter/itiff/lzwdecom.cxx +++ b/vcl/source/filter/itiff/lzwdecom.cxx @@ -22,82 +22,84 @@ #include <sal/log.hxx> #include <tools/stream.hxx> +constexpr sal_Int16 CODE_CLEAR = 256; +constexpr sal_Int16 CODE_EOI = 257; +constexpr sal_Int16 CODE_FIRSTMULTICHAR = 258; + LZWDecompressor::LZWDecompressor() - : pIStream(nullptr) - , aTable{{}} - , nTableSize(0) - , bEOIFound(false) - , bInvert(false) - , bFirst(true) - , nOldCode(0) - , pOutBufData(nullptr) - , nOutBufDataLen(0) - , nInputBitsBuf(0) - , nInputBitsBufSize(0) + : m_pIStream(nullptr) + , m_aTable{{}} + , m_nTableCurrentId(0) + , m_bEOIFound(false) + , m_bInvert(false) + , m_nOldCode(0) + , m_pOutBufData(nullptr) + , m_nOutBufDataLen(0) + , m_nInputBitsBuf(0) + , m_nInputBitsBufSize(0) { - for (sal_uInt16 i=0; i<MAX_TABLE_SIZE; i++) - { - aTable[i].nPrevCode=0; - aTable[i].nDataCount=1; - aTable[i].nData=static_cast<sal_uInt8>(i); - } } void LZWDecompressor::StartDecompression(SvStream & rIStream) { - pIStream=&rIStream; + for (sal_uInt16 i=0; i<MAX_TABLE_SIZE; i++) + { + m_aTable[i].nPrevCode = 0; + m_aTable[i].nDataCount = 1; + m_aTable[i].nData = static_cast<sal_uInt8>(i); + } + m_pIStream = &rIStream; - nTableSize=258; + m_nTableCurrentId = CODE_FIRSTMULTICHAR; - bEOIFound=false; + m_bEOIFound = false; - nOutBufDataLen=0; + m_nOldCode = 0; + m_pOutBufData = nullptr; - pIStream->ReadUChar( nInputBitsBuf ); + m_nOutBufDataLen = 0; - nInputBitsBufSize=8; + m_pIStream->ReadUChar( m_nInputBitsBuf ); - if ( bFirst ) + m_nInputBitsBufSize=8; + + m_bInvert = m_nInputBitsBuf == 1; + + if ( m_bInvert ) { - bInvert = nInputBitsBuf == 1; - bFirst = false; + m_nInputBitsBuf = ( ( m_nInputBitsBuf & 1 ) << 7 ) | ( ( m_nInputBitsBuf & 2 ) << 5 ) | ( ( m_nInputBitsBuf & 4 ) << 3 ) | ( ( m_nInputBitsBuf & 8 ) << 1 ) | ( ( m_nInputBitsBuf & 16 ) >> 1 ) | ( ( m_nInputBitsBuf & 32 ) >> 3 ) | ( ( m_nInputBitsBuf & 64 ) >> 5 ) | ( (m_nInputBitsBuf & 128 ) >> 7 ); } - - if ( bInvert ) - nInputBitsBuf = ( ( nInputBitsBuf & 1 ) << 7 ) | ( ( nInputBitsBuf & 2 ) << 5 ) | ( ( nInputBitsBuf & 4 ) << 3 ) | ( ( nInputBitsBuf & 8 ) << 1 ) | ( ( nInputBitsBuf & 16 ) >> 1 ) | ( ( nInputBitsBuf & 32 ) >> 3 ) | ( ( nInputBitsBuf & 64 ) >> 5 ) | ( (nInputBitsBuf & 128 ) >> 7 ); } sal_uInt64 LZWDecompressor::Decompress(sal_uInt8 * pTarget, sal_uInt32 nMaxCount) { - sal_uInt32 nCount; + if (m_pIStream == nullptr) return 0; - if (pIStream==nullptr) return 0; - - nCount=0; + sal_uInt32 nCount=0; for (;;) { - if (pIStream->GetError()) break; + if (m_pIStream->GetError()) break; - if (nOutBufDataLen>=nMaxCount) { - nOutBufDataLen = nOutBufDataLen - static_cast<sal_uInt16>(nMaxCount); - nCount+=nMaxCount; - while (nMaxCount>0) { - *(pTarget++)=*(pOutBufData++); + if (m_nOutBufDataLen >= nMaxCount) { + m_nOutBufDataLen = m_nOutBufDataLen - static_cast<sal_uInt16>(nMaxCount); + nCount += nMaxCount; + while (nMaxCount > 0) { + *(pTarget++)=*(m_pOutBufData++); nMaxCount--; } break; } - nMaxCount-=static_cast<sal_uInt32>(nOutBufDataLen); - nCount+=nOutBufDataLen; - while (nOutBufDataLen>0) { - *(pTarget++)=*(pOutBufData++); - nOutBufDataLen--; + nMaxCount -= static_cast<sal_uInt32>(m_nOutBufDataLen); + nCount += m_nOutBufDataLen; + while (m_nOutBufDataLen>0) { + *(pTarget++) = *(m_pOutBufData++); + m_nOutBufDataLen--; } - if (bEOIFound) break; + if (m_bEOIFound) break; DecompressSome(); @@ -109,32 +111,34 @@ sal_uInt64 LZWDecompressor::Decompress(sal_uInt8 * pTarget, sal_uInt32 nMaxCount sal_uInt16 LZWDecompressor::GetNextCode() { - sal_uInt16 nBits,nCode; + sal_uInt16 nBits; - if (nTableSize<511) nBits=9; - else if (nTableSize<1023) nBits=10; - else if (nTableSize<2047) nBits=11; - else nBits=12; + if (m_nTableCurrentId<511) nBits = 9; + else if (m_nTableCurrentId<1023) nBits = 10; + else if (m_nTableCurrentId<2047) nBits = 11; + else nBits = 12; - nCode=0; + sal_uInt16 nCode = 0; do { - if (nInputBitsBufSize<=nBits) + if (m_nInputBitsBufSize <= nBits) { - nCode=(nCode<<nInputBitsBufSize) | nInputBitsBuf; - nBits = nBits - nInputBitsBufSize; - pIStream->ReadUChar( nInputBitsBuf ); - if ( bInvert ) - nInputBitsBuf = ( ( nInputBitsBuf & 1 ) << 7 ) | ( ( nInputBitsBuf & 2 ) << 5 ) | ( ( nInputBitsBuf & 4 ) << 3 ) | ( ( nInputBitsBuf & 8 ) << 1 ) | ( ( nInputBitsBuf & 16 ) >> 1 ) | ( ( nInputBitsBuf & 32 ) >> 3 ) | ( ( nInputBitsBuf & 64 ) >> 5 ) | ( (nInputBitsBuf & 128 ) >> 7 ); - nInputBitsBufSize=8; + nCode = (nCode << m_nInputBitsBufSize) | m_nInputBitsBuf; + nBits = nBits - m_nInputBitsBufSize; + m_pIStream->ReadUChar( m_nInputBitsBuf ); + if ( m_bInvert ) + { + m_nInputBitsBuf = ( ( m_nInputBitsBuf & 1 ) << 7 ) | ( ( m_nInputBitsBuf & 2 ) << 5 ) | ( ( m_nInputBitsBuf & 4 ) << 3 ) | ( ( m_nInputBitsBuf & 8 ) << 1 ) | ( ( m_nInputBitsBuf & 16 ) >> 1 ) | ( ( m_nInputBitsBuf & 32 ) >> 3 ) | ( ( m_nInputBitsBuf & 64 ) >> 5 ) | ( (m_nInputBitsBuf & 128 ) >> 7 ); + } + m_nInputBitsBufSize=8; } else { - nCode=(nCode<<nBits) | (nInputBitsBuf>>(nInputBitsBufSize-nBits)); - nInputBitsBufSize = nInputBitsBufSize - nBits; - nInputBitsBuf&=0x00ff>>(8-nInputBitsBufSize); - nBits=0; + nCode = (nCode<<nBits) | ( m_nInputBitsBuf >> (m_nInputBitsBufSize-nBits) ); + m_nInputBitsBufSize = m_nInputBitsBufSize - nBits; + m_nInputBitsBuf &= 0x00ff >> (8 - m_nInputBitsBufSize); + nBits = 0; } - } while (nBits>0); + } while (nBits > 0); return nCode; } @@ -142,70 +146,68 @@ sal_uInt16 LZWDecompressor::GetNextCode() void LZWDecompressor::AddToTable(sal_uInt16 nPrevCode, sal_uInt16 nCodeFirstData) { - if (nTableSize >= MAX_TABLE_SIZE) + if (m_nTableCurrentId >= MAX_TABLE_SIZE) { //It might be possible to force emit a 256 to flush the buffer and try //to continue later? SAL_WARN("filter.tiff", "Too much data at scanline"); - bEOIFound = true; + m_bEOIFound = true; return; } unsigned char aSeenIndexes[MAX_TABLE_SIZE] = {0}; - while (aTable[nCodeFirstData].nDataCount>1) + while (m_aTable[nCodeFirstData].nDataCount > 1) { if (aSeenIndexes[nCodeFirstData]) { SAL_WARN("filter.tiff", "Loop in chain"); - bEOIFound = true; + m_bEOIFound = true; return; } aSeenIndexes[nCodeFirstData] = 1; - nCodeFirstData=aTable[nCodeFirstData].nPrevCode; + nCodeFirstData = m_aTable[nCodeFirstData].nPrevCode; } - aTable[nTableSize].nPrevCode=nPrevCode; - aTable[nTableSize].nDataCount=aTable[nPrevCode].nDataCount+1; - aTable[nTableSize].nData=aTable[nCodeFirstData].nData; + m_aTable[m_nTableCurrentId].nPrevCode = nPrevCode; + m_aTable[m_nTableCurrentId].nDataCount = m_aTable[nPrevCode].nDataCount+1; + m_aTable[m_nTableCurrentId].nData = m_aTable[nCodeFirstData].nData; - nTableSize++; + m_nTableCurrentId++; } void LZWDecompressor::DecompressSome() { - sal_uInt16 i,nCode; - - nCode=GetNextCode(); - if (nCode==256) + sal_uInt16 nCode = GetNextCode(); + if (nCode == CODE_CLEAR) { - nTableSize=258; - nCode=GetNextCode(); - if (nCode==257) + m_nTableCurrentId = CODE_FIRSTMULTICHAR; + nCode = GetNextCode(); + if (nCode == CODE_EOI) { - bEOIFound=true; + m_bEOIFound = true; } } - else if (nCode<nTableSize) - AddToTable(nOldCode,nCode); - else if (nCode==nTableSize) - AddToTable(nOldCode,nOldCode); + else if (nCode < m_nTableCurrentId) + AddToTable(m_nOldCode, nCode); + else if (nCode == m_nTableCurrentId) + AddToTable(m_nOldCode, m_nOldCode); else { - bEOIFound=true; + m_bEOIFound = true; } - if (bEOIFound) + if (m_bEOIFound) return; - nOldCode=nCode; + m_nOldCode = nCode; - nOutBufDataLen=aTable[nCode].nDataCount; - pOutBufData=pOutBuf.data()+nOutBufDataLen; - for (i=0; i<nOutBufDataLen; i++) + m_nOutBufDataLen = m_aTable[nCode].nDataCount; + m_pOutBufData = m_pOutBuf.data() + m_nOutBufDataLen; + for (sal_uInt16 i = 0; i < m_nOutBufDataLen; i++) { - *(--pOutBufData)=aTable[nCode].nData; - nCode=aTable[nCode].nPrevCode; + *(--m_pOutBufData) = m_aTable[nCode].nData; + nCode = m_aTable[nCode].nPrevCode; } } diff --git a/vcl/source/filter/itiff/lzwdecom.hxx b/vcl/source/filter/itiff/lzwdecom.hxx index 2104a4e49f5a..c2a04bdeecb3 100644 --- a/vcl/source/filter/itiff/lzwdecom.hxx +++ b/vcl/source/filter/itiff/lzwdecom.hxx @@ -17,13 +17,12 @@ * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ -#ifndef INCLUDED_FILTER_SOURCE_GRAPHICFILTER_ITIFF_LZWDECOM_HXX -#define INCLUDED_FILTER_SOURCE_GRAPHICFILTER_ITIFF_LZWDECOM_HXX +#pragma once #include <sal/types.h> #include <array> -#define MAX_TABLE_SIZE 4096 +constexpr sal_Int16 MAX_TABLE_SIZE = 4096; struct LZWTableEntry { @@ -50,23 +49,21 @@ private: void AddToTable(sal_uInt16 nPrevCode, sal_uInt16 nCodeFirstData); void DecompressSome(); - SvStream* pIStream; + SvStream* m_pIStream; - std::array<LZWTableEntry, MAX_TABLE_SIZE> aTable; - sal_uInt16 nTableSize; + std::array<LZWTableEntry, MAX_TABLE_SIZE> m_aTable; + sal_uInt16 m_nTableCurrentId; - bool bEOIFound, bInvert, bFirst; + bool m_bEOIFound, m_bInvert; - sal_uInt16 nOldCode; + sal_uInt16 m_nOldCode; - std::array<sal_uInt8, MAX_TABLE_SIZE> pOutBuf; - sal_uInt8* pOutBufData; - sal_uInt16 nOutBufDataLen; + std::array<sal_uInt8, MAX_TABLE_SIZE> m_pOutBuf; + sal_uInt8* m_pOutBufData; + sal_uInt16 m_nOutBufDataLen; - sal_uInt8 nInputBitsBuf; - sal_uInt16 nInputBitsBufSize; + sal_uInt8 m_nInputBitsBuf; + sal_uInt16 m_nInputBitsBufSize; }; -#endif - /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |