From 4bb4f4c97bd1b5fc97152bc2c3b2dc3c1d9cfe32 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Wed, 8 Jan 2025 17:43:22 +0100 Subject: [PATCH] LIBERTIFF: use GDALExpandPackedBitsToByteAt0Or255() and GDALExpandPackedBitsToByteAt0Or1() --- frmts/libertiff/libertiffdataset.cpp | 64 +++------------------------- 1 file changed, 6 insertions(+), 58 deletions(-) diff --git a/frmts/libertiff/libertiffdataset.cpp b/frmts/libertiff/libertiffdataset.cpp index 6e88b572423b..9bfb72ec90e4 100644 --- a/frmts/libertiff/libertiffdataset.cpp +++ b/frmts/libertiff/libertiffdataset.cpp @@ -1166,28 +1166,6 @@ FloatingPointHorizPredictorDecode(std::vector &tmpBuffer, return true; } -/************************************************************************/ -/* ExtractBitAndConvertTo255() */ -/************************************************************************/ - -#if defined(__GNUC__) || defined(_MSC_VER) -// Signedness of char implementation dependent, so be explicit. -// Assumes 2-complement integer types and sign extension of right shifting -// GCC guarantees such: -// https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html#Integers-implementation -static inline GByte ExtractBitAndConvertTo255(GByte byVal, int nBit) -{ - return static_cast(static_cast(byVal << (7 - nBit)) >> - 7); -} -#else -// Portable way -static inline GByte ExtractBitAndConvertTo255(GByte byVal, int nBit) -{ - return (byVal & (1 << nBit)) ? 255 : 0; -} -#endif - /************************************************************************/ /* ReadBlock() */ /************************************************************************/ @@ -1582,51 +1560,21 @@ bool LIBERTIFFDataset::ReadBlock(GByte *pabyBlockData, int nBlockXOff, if (m_image->bitsPerSample() == 1) { const GByte *CPL_RESTRICT pabySrc = abyDecompressedStrile.data(); - const GByte val = m_bExpand1To255 ? 255 : 1; GByte *CPL_RESTRICT pabyDst = bufferForOneBitExpansion.data(); for (int iY = 0; iY < nBlockActualYSize; ++iY) { - int iX = 0; if (m_bExpand1To255) { - for (; iX + 7 < nBlockXSize; - iX += 8, ++pabySrc, pabyDst += 8) - { - const GByte srcByte = *pabySrc; - pabyDst[0] = ExtractBitAndConvertTo255(srcByte, 7); - pabyDst[1] = ExtractBitAndConvertTo255(srcByte, 6); - pabyDst[2] = ExtractBitAndConvertTo255(srcByte, 5); - pabyDst[3] = ExtractBitAndConvertTo255(srcByte, 4); - pabyDst[4] = ExtractBitAndConvertTo255(srcByte, 3); - pabyDst[5] = ExtractBitAndConvertTo255(srcByte, 2); - pabyDst[6] = ExtractBitAndConvertTo255(srcByte, 1); - pabyDst[7] = ExtractBitAndConvertTo255(srcByte, 0); - } + GDALExpandPackedBitsToByteAt0Or255(pabySrc, pabyDst, + nBlockXSize); } else { - for (; iX + 7 < nBlockXSize; - iX += 8, ++pabySrc, pabyDst += 8) - { - const int srcByte = *pabySrc; - pabyDst[0] = (srcByte >> 7) & 1; - pabyDst[1] = (srcByte >> 6) & 1; - pabyDst[2] = (srcByte >> 5) & 1; - pabyDst[3] = (srcByte >> 4) & 1; - pabyDst[4] = (srcByte >> 3) & 1; - pabyDst[5] = (srcByte >> 2) & 1; - pabyDst[6] = (srcByte >> 1) & 1; - pabyDst[7] = (srcByte >> 0) & 1; - } - } - if (iX < nBlockXSize) - { - for (; iX < nBlockXSize; ++iX, ++pabyDst) - { - *pabyDst = (*pabySrc & (0x80 >> (iX % 8))) ? val : 0; - } - ++pabySrc; + GDALExpandPackedBitsToByteAt0Or1(pabySrc, pabyDst, + nBlockXSize); } + pabySrc += (nBlockXSize + 7) / 8; + pabyDst += nBlockXSize; } std::swap(abyDecompressedStrile, bufferForOneBitExpansion);