| 1 | /* |
| 2 | Copyright 2015 - 2026 Esri |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | |
| 16 | A local copy of the license and additional notices are located with the |
| 17 | source distribution at: |
| 18 | |
| 19 | http://github.com/Esri/lerc/ |
| 20 | |
| 21 | Contributors: Thomas Maurer |
| 22 | */ |
| 23 | |
| 24 | #ifndef LERC2_H |
| 25 | #define LERC2_H |
| 26 | |
| 27 | #include <cfloat> |
| 28 | #include <cmath> |
| 29 | #include <climits> |
| 30 | #include <algorithm> |
| 31 | #include <string> |
| 32 | #include "BitMask.h" |
| 33 | #include "BitStuffer2.h" |
| 34 | #include "fpl_Lerc2Ext.h" |
| 35 | |
| 36 | NAMESPACE_LERC_START |
| 37 | |
| 38 | /** Lerc2 v1 |
| 39 | * |
| 40 | * -- allow for lossless compression of all common data types |
| 41 | * -- avoid data type conversions and copies |
| 42 | * -- optimized compression for segmented rasters (10-15x lossless) |
| 43 | * -- micro block is 8x8 fixed, only gets doubled to 16x16 if bit rate < 1 bpp |
| 44 | * -- cnt is replaced by bit mask |
| 45 | * -- Lerc blob header has data range [min, max] |
| 46 | * -- harden consistency checks to detect if the byte blob has been tampered with |
| 47 | * -- drop support for big endian, this is legacy now |
| 48 | * |
| 49 | * Lerc2 v2 |
| 50 | * |
| 51 | * -- add Huffman coding for better lossless compression of 8 bit data types Char, Byte |
| 52 | * |
| 53 | * Lerc2 v3 |
| 54 | * |
| 55 | * -- add checksum for the entire byte blob, for more rigorous detection of compressed data corruption |
| 56 | * -- for the main bit stuffing routine, use an extra uint buffer for guaranteed memory alignment |
| 57 | * -- this also allows to drop the NumExtraBytesToAllocate functions |
| 58 | * |
| 59 | * Lerc2 v4 |
| 60 | * |
| 61 | * -- allow array per pixel, nDepth values per pixel. Such as RGB, complex number, or larger arrays per pixel |
| 62 | * -- extend Huffman coding for 8 bit data types from delta only to trying both delta and orig |
| 63 | * -- for integer data types, allow to drop bit planes containing only random noise |
| 64 | * |
| 65 | * Lerc2 v5 |
| 66 | * -- for float data (as it might be lower precision like %.2f), try raise maxZError if possible w/o extra loss |
| 67 | * -- add delta encoding of a block iDepth relative to previous block (iDepth - 1) |
| 68 | * |
| 69 | */ |
| 70 | |
| 71 | class Lerc2 |
| 72 | { |
| 73 | public: |
| 74 | Lerc2(); |
| 75 | Lerc2(int nDepth, int nCols, int nRows, const Byte* pMaskBits = nullptr); // valid / invalid bits as byte array |
| 76 | ~Lerc2() {} |
| 77 | |
| 78 | static int CurrentVersion() { return 6; } |
| 79 | |
| 80 | bool SetEncoderToOldVersion(int version); // call this to encode compatible to an old decoder |
| 81 | |
| 82 | bool Set(int nDepth, int nCols, int nRows, const Byte* pMaskBits = nullptr); // set mask and dimensions |
| 83 | |
| 84 | // v6 |
| 85 | bool SetNoDataValues(bool bNeedsNoDataVal, double noDataVal, double noDataValOrig); |
| 86 | bool SetNumBlobsMoreToCome(int nBlobsMore); // for safer decode of multi-band blobs |
| 87 | bool SetIsAllInt(bool bIsAllInt); |
| 88 | |
| 89 | bool SetMinMax(int nDepth, double minVal, double maxVal); // set min / max but only for nDepth = 1 |
| 90 | void ClearMinMax(); |
| 91 | |
| 92 | template<class T> |
| 93 | unsigned int ComputeNumBytesNeededToWrite(const T* arr, double maxZError, bool encodeMask); |
| 94 | |
| 95 | /// dst buffer already allocated; byte ptr is moved like a file pointer |
| 96 | template<class T> |
| 97 | bool Encode(const T* arr, Byte** ppByte); |
| 98 | |
| 99 | // data types supported by Lerc2 |
| 100 | enum DataType {DT_Char = 0, DT_Byte, DT_Short, DT_UShort, DT_Int, DT_UInt, DT_Float, DT_Double, DT_Undefined}; |
| 101 | |
| 102 | struct HeaderInfo |
| 103 | { |
| 104 | int version; |
| 105 | unsigned int checksum; |
| 106 | int nRows, |
| 107 | nCols, |
| 108 | nDepth, |
| 109 | numValidPixel, |
| 110 | microBlockSize, |
| 111 | blobSize, |
| 112 | nBlobsMore; // for multi-band Lerc blob, how many more blobs or bands appended to this one |
| 113 | |
| 114 | Byte bPassNoDataValues, // 1 - pass noData values to decoder, 0 - don't pass, ignore |
| 115 | bIsInt, // 1 - float or double data is all integer numbers, 0 - not |
| 116 | bReserved3, |
| 117 | bReserved4; |
| 118 | |
| 119 | DataType dt; |
| 120 | |
| 121 | double maxZError, |
| 122 | zMin, // if nDepth > 1, this is the overall range |
| 123 | zMax, |
| 124 | noDataVal, // temp noData value used for nDepth > 1 if bit mask cannot cover it |
| 125 | noDataValOrig; // orig noData value to map to in decode |
| 126 | |
| 127 | void RawInit() { memset(this, 0, sizeof(struct HeaderInfo)); } |
| 128 | |
| 129 | bool TryHuffmanInt() const { return (version >= 2) && (dt == DT_Byte || dt == DT_Char) && (maxZError == 0.5); } |
| 130 | bool TryHuffmanFlt() const { return (version >= 6) && (dt == DT_Float || dt == DT_Double) && (maxZError == 0); } |
| 131 | }; |
| 132 | |
| 133 | static bool GetHeaderInfo(const Byte* pByte, size_t nBytesRemaining, struct HeaderInfo& headerInfo, bool& bHasMask); |
| 134 | |
| 135 | bool GetRanges(const Byte* pByte, size_t nBytesRemaining, double* pMins, double* pMaxs); |
| 136 | |
| 137 | /// dst buffer already allocated; byte ptr is moved like a file pointer |
| 138 | template<class T> |
| 139 | bool Decode(const Byte** ppByte, size_t& nBytesRemaining, T* arr, Byte* pMaskBits = nullptr); // if mask ptr is not 0, mask bits are returned (even if all valid or same as previous) |
| 140 | |
| 141 | private: |
| 142 | |
| 143 | enum ImageEncodeMode { IEM_Tiling = 0, IEM_DeltaHuffman, IEM_Huffman, IEM_DeltaDeltaHuffman }; |
| 144 | enum BlockEncodeMode { BEM_RawBinary = 0, BEM_BitStuffSimple, BEM_BitStuffLUT }; |
| 145 | |
| 146 | int m_microBlockSize, |
| 147 | m_maxValToQuantize; |
| 148 | BitMask m_bitMask; |
| 149 | HeaderInfo m_headerInfo; |
| 150 | BitStuffer2 m_bitStuffer2; |
| 151 | bool m_encodeMask, |
| 152 | m_writeDataOneSweep, |
| 153 | m_minMaxSet; |
| 154 | ImageEncodeMode m_imageEncodeMode; |
| 155 | |
| 156 | std::vector<double> m_zMinVec, m_zMaxVec; |
| 157 | std::vector<std::pair<unsigned short, unsigned int> > m_huffmanCodes; // <= 256 codes, 1.5 kB |
| 158 | |
| 159 | LosslessFPCompression m_lfpc; |
| 160 | |
| 161 | private: |
| 162 | static std::string FileKey() { return "Lerc2 "; } |
| 163 | static bool IsLittleEndianSystem() { int n = 1; return (1 == *((Byte*)&n)) && (4 == sizeof(int)); } |
| 164 | void Init(); |
| 165 | |
| 166 | static unsigned int ComputeNumBytesHeaderToWrite(const struct HeaderInfo& hd); |
| 167 | static bool WriteHeader(Byte** ppByte, const struct HeaderInfo& hd); |
| 168 | static bool ReadHeader(const Byte** ppByte, size_t& nBytesRemaining, struct HeaderInfo& hd); |
| 169 | |
| 170 | bool WriteMask(Byte** ppByte) const; |
| 171 | bool ReadMask(const Byte** ppByte, size_t& nBytesRemaining); |
| 172 | |
| 173 | bool DoChecksOnEncode(Byte* pBlobBegin, Byte* pBlobEnd) const; |
| 174 | static unsigned int ComputeChecksumFletcher32(const Byte* pByte, int len); |
| 175 | |
| 176 | static void AddUIntToCounts(int* pCounts, unsigned int val, int nBits); |
| 177 | static void AddIntToCounts(int* pCounts, int val, int nBits); |
| 178 | |
| 179 | template<class T> |
| 180 | bool TryBitPlaneCompression(const T* data, double eps, double& newMaxZError) const; |
| 181 | |
| 182 | template<class T> |
| 183 | bool TryRaiseMaxZError(const T* data, double& maxZError) const; |
| 184 | |
| 185 | static bool PruneCandidates(std::vector<double>& roundErr, std::vector<double>& zErr, |
| 186 | std::vector<int>& zFac, double maxZError); |
| 187 | |
| 188 | template<class T> |
| 189 | bool WriteDataOneSweep(const T* data, Byte** ppByte) const; |
| 190 | |
| 191 | template<class T> |
| 192 | bool ReadDataOneSweep(const Byte** ppByte, size_t& nBytesRemaining, T* data) const; |
| 193 | |
| 194 | template<class T> |
| 195 | bool ComputeMinMaxRanges(const T* data, std::vector<double>& zMinVec, std::vector<double>& zMaxVec) const; |
| 196 | |
| 197 | template<class T> |
| 198 | bool WriteTiles(const T* data, Byte** ppByte, int& numBytes) const; |
| 199 | |
| 200 | template<class T> |
| 201 | bool ReadTiles(const Byte** ppByte, size_t& nBytesRemaining, T* data) const; |
| 202 | |
| 203 | template<class T> |
| 204 | bool GetValidDataAndStats(const T* data, int i0, int i1, int j0, int j1, int iDepth, |
| 205 | T* dataBuf, T& zMin, T& zMax, int& numValidPixel, bool& tryLut) const; |
| 206 | |
| 207 | template<class T> |
| 208 | static bool ComputeDiffSliceInt(const T* data, const T* prevData, int numValidPixel, bool bCheckForIntOverflow, |
| 209 | double maxZError, std::vector<int>& diffDataVec, int& zMin, int& zMax, bool& tryLut); |
| 210 | |
| 211 | template<class T> |
| 212 | static bool ComputeDiffSliceFlt(const T* data, const T* prevData, int numValidPixel, bool bCheckForFltRndErr, |
| 213 | double maxZError, std::vector<T>& diffDataVec, T& zMin, T& zMax, bool& tryLut); |
| 214 | |
| 215 | static bool NeedToCheckForIntOverflow(const HeaderInfo& hd); |
| 216 | static bool NeedToCheckForFltRndErr(const HeaderInfo& hd); |
| 217 | |
| 218 | static double ComputeMaxVal(double zMin, double zMax, double maxZError); |
| 219 | |
| 220 | template<class T> |
| 221 | bool NeedToQuantize(int numValidPixel, T zMin, T zMax) const; |
| 222 | |
| 223 | template<class T> |
| 224 | void Quantize(const T* dataBuf, int num, T zMin, std::vector<unsigned int>& quantVec) const; |
| 225 | |
| 226 | template<class T> |
| 227 | static void ScaleBack(T* dataBuf, const std::vector<unsigned int>& quantVec, |
| 228 | double zMin, bool bDiff, bool bClamp, double zMaxClamp, double maxZError); |
| 229 | |
| 230 | template<class T> |
| 231 | static void ScaleBackConstBlock(T* dataBuf, int num, double zMin, bool bClamp, double zMaxClamp); |
| 232 | |
| 233 | template<class T> |
| 234 | int NumBytesTile(int numValidPixel, T zMin, T zMax, DataType dtZ, bool tryLut, BlockEncodeMode& blockEncodeMode, |
| 235 | const std::vector<std::pair<unsigned int, unsigned int> >& sortedQuantVec) const; |
| 236 | |
| 237 | template<class T> |
| 238 | bool WriteTile(const T* dataBuf, int num, Byte** ppByte, int& numBytesWritten, int j0, T zMin, T zMax, |
| 239 | DataType dtZ, bool bDiffEnc, const std::vector<unsigned int>& quantVec, BlockEncodeMode blockEncodeMode, |
| 240 | const std::vector<std::pair<unsigned int, unsigned int> >& sortedQuantVec) const; |
| 241 | |
| 242 | template<class T> |
| 243 | bool ReadTile(const Byte** ppByte, size_t& nBytesRemaining, T* data, int i0, int i1, int j0, int j1, int iDepth, |
| 244 | std::vector<unsigned int>& bufferVec) const; |
| 245 | |
| 246 | template<class T> |
| 247 | static int ReduceDataType(T z, DataType dt, DataType& dtReduced); |
| 248 | |
| 249 | static DataType GetDataTypeUsed(DataType dt, int reducedTypeCode); |
| 250 | |
| 251 | static DataType ValidateDataType(int dt); |
| 252 | |
| 253 | static bool WriteVariableDataType(Byte** ppByte, double z, DataType dtUsed); |
| 254 | |
| 255 | static double ReadVariableDataType(const Byte** ppByte, DataType dtUsed); |
| 256 | |
| 257 | template<class T> |
| 258 | static DataType GetDataType(T z); |
| 259 | |
| 260 | static unsigned int GetMaxValToQuantize(DataType dt); |
| 261 | |
| 262 | static unsigned int GetDataTypeSize(DataType dt); |
| 263 | |
| 264 | static void SortQuantArray(const std::vector<unsigned int>& quantVec, |
| 265 | std::vector<std::pair<unsigned int, unsigned int> >& sortedQuantVec); |
| 266 | |
| 267 | template<class T> |
| 268 | void ComputeHuffmanCodes(const T* data, int& numBytes, ImageEncodeMode& imageEncodeMode, |
| 269 | std::vector<std::pair<unsigned short, unsigned int> >& codes) const; |
| 270 | |
| 271 | template<class T> |
| 272 | void ComputeHistoForHuffman(const T* data, std::vector<int>& histo, std::vector<int>& deltaHisto) const; |
| 273 | |
| 274 | template<class T> |
| 275 | bool EncodeHuffman(const T* data, Byte** ppByte) const; |
| 276 | |
| 277 | template<class T> |
| 278 | bool DecodeHuffman(const Byte** ppByte, size_t& nBytesRemaining, T* data) const; |
| 279 | |
| 280 | template<class T> |
| 281 | bool WriteMinMaxRanges(const T* data, Byte** ppByte) const; |
| 282 | |
| 283 | template<class T> |
| 284 | bool ReadMinMaxRanges(const Byte** ppByte, size_t& nBytesRemaining, const T* data); |
| 285 | |
| 286 | bool CheckMinMaxRanges(bool& minMaxEqual) const; |
| 287 | |
| 288 | template<class T> |
| 289 | bool FillConstImage(T* data) const; |
| 290 | }; |
| 291 | |
| 292 | // -------------------------------------------------------------------------- ; |
| 293 | // -------------------------------------------------------------------------- ; |
| 294 | |
| 295 | inline void Lerc2::AddUIntToCounts(int* pCounts, unsigned int val, int nBits) |
| 296 | { |
| 297 | pCounts[0] += val & 1; |
| 298 | for (int i = 1; i < nBits; i++) |
| 299 | pCounts[i] += (val >>= 1) & 1; |
| 300 | } |
| 301 | |
| 302 | // -------------------------------------------------------------------------- ; |
| 303 | |
| 304 | inline void Lerc2::AddIntToCounts(int* pCounts, int val, int nBits) |
| 305 | { |
| 306 | pCounts[0] += val & 1; |
| 307 | for (int i = 1; i < nBits; i++) |
| 308 | pCounts[i] += (val >>= 1) & 1; |
| 309 | } |
| 310 | |
| 311 | // -------------------------------------------------------------------------- ; |
| 312 | |
| 313 | inline bool Lerc2::NeedToCheckForIntOverflow(const HeaderInfo& hd) |
| 314 | { |
| 315 | return (hd.dt == DT_Int || hd.dt == DT_UInt) && (hd.zMax - hd.zMin >= 0x7FFFFFFF); |
| 316 | } |
| 317 | |
| 318 | // -------------------------------------------------------------------------- ; |
| 319 | |
| 320 | inline bool Lerc2::NeedToCheckForFltRndErr(const HeaderInfo& hd) |
| 321 | { |
| 322 | if (hd.dt != DT_Float) |
| 323 | return false; |
| 324 | |
| 325 | if (hd.zMax - hd.zMin > 100000) // prevent the below test falls through with extreme numbers like 10^38 |
| 326 | return true; |
| 327 | |
| 328 | float diff = (float)(hd.zMax - hd.zMin); |
| 329 | double testMax = (double)diff + hd.zMin; |
| 330 | double fltRndErr = fabs(testMax - hd.zMax); |
| 331 | |
| 332 | return (fltRndErr > hd.maxZError / 8); |
| 333 | } |
| 334 | |
| 335 | // -------------------------------------------------------------------------- ; |
| 336 | |
| 337 | inline double Lerc2::ComputeMaxVal(double zMin, double zMax, double maxZError) |
| 338 | { |
| 339 | double fac = 1 / (2 * maxZError); // must match the code in Decode(), don't touch it |
| 340 | return (zMax - zMin) * fac; |
| 341 | } |
| 342 | |
| 343 | // -------------------------------------------------------------------------- ; |
| 344 | |
| 345 | template<class T> |
| 346 | inline bool Lerc2::NeedToQuantize(int numValidPixel, T zMin, T zMax) const |
| 347 | { |
| 348 | if (numValidPixel == 0 || m_headerInfo.maxZError == 0) |
| 349 | return false; |
| 350 | |
| 351 | double maxVal = ComputeMaxVal(zMin, zMax, m_headerInfo.maxZError); |
| 352 | return !(maxVal > m_maxValToQuantize || (unsigned int)(maxVal + 0.5) == 0); |
| 353 | } |
| 354 | |
| 355 | // -------------------------------------------------------------------------- ; |
| 356 | |
| 357 | template<class T> |
| 358 | inline void Lerc2::Quantize(const T* dataBuf, int num, T zMin, std::vector<unsigned int>& quantVec) const |
| 359 | { |
| 360 | quantVec.resize(num); |
| 361 | |
| 362 | if (m_headerInfo.dt < DT_Float && m_headerInfo.maxZError == 0.5) // int lossless |
| 363 | { |
| 364 | for (int i = 0; i < num; i++) |
| 365 | quantVec[i] = (unsigned int)(dataBuf[i] - zMin); // ok: char, short get promoted to int by C++ integral promotion rule |
| 366 | } |
| 367 | else // float and/or lossy |
| 368 | { |
| 369 | double scale = 1 / (2 * m_headerInfo.maxZError); |
| 370 | double zMinDbl = (double)zMin; |
| 371 | |
| 372 | for (int i = 0; i < num; i++) |
| 373 | quantVec[i] = (unsigned int)(((double)dataBuf[i] - zMinDbl) * scale + 0.5); // ok, consistent with ComputeMaxVal(...) |
| 374 | //quantVec[i] = (unsigned int)((dataBuf[i] - zMin) * scale + 0.5); // bad, not consistent with ComputeMaxVal(...) |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | // -------------------------------------------------------------------------- ; |
| 379 | |
| 380 | template<class T> |
| 381 | inline void Lerc2::ScaleBack(T* dataBuf, const std::vector<unsigned int>& quantVec, |
| 382 | double zMin, bool bDiff, bool bClamp, double zMaxClamp, double maxZError) |
| 383 | { |
| 384 | double invScale = 2 * maxZError; // for int types this is int |
| 385 | int num = (int)quantVec.size(); |
| 386 | |
| 387 | if (!bClamp) |
| 388 | for (int i = 0; i < num; i++) |
| 389 | { |
| 390 | double z = zMin + quantVec[i] * invScale + (bDiff ? dataBuf[i] : 0); |
| 391 | dataBuf[i] = (T)z; |
| 392 | } |
| 393 | else |
| 394 | for (int i = 0; i < num; i++) |
| 395 | { |
| 396 | double z = zMin + quantVec[i] * invScale + (bDiff ? dataBuf[i] : 0); |
| 397 | dataBuf[i] = (T)std::min(z, zMaxClamp); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | // -------------------------------------------------------------------------- ; |
| 402 | |
| 403 | template<class T> |
| 404 | inline void Lerc2::ScaleBackConstBlock(T* dataBuf, int num, double zMin, bool bClamp, double zMaxClamp) |
| 405 | { |
| 406 | if (!bClamp) |
| 407 | for (int i = 0; i < num; i++) |
| 408 | dataBuf[i] = (T)(zMin + dataBuf[i]); |
| 409 | else |
| 410 | for (int i = 0; i < num; i++) |
| 411 | dataBuf[i] = (T)std::min(zMin + dataBuf[i], zMaxClamp); |
| 412 | } |
| 413 | |
| 414 | // -------------------------------------------------------------------------- ; |
| 415 | |
| 416 | template<class T> |
| 417 | inline int Lerc2::NumBytesTile(int numValidPixel, T zMin, T zMax, DataType dtZ, bool tryLut, |
| 418 | BlockEncodeMode& blockEncodeMode, const std::vector<std::pair<unsigned int, unsigned int> >& sortedQuantVec) const |
| 419 | { |
| 420 | blockEncodeMode = BEM_RawBinary; |
| 421 | |
| 422 | if (numValidPixel == 0 || (zMin == 0 && zMax == 0)) |
| 423 | return 1; |
| 424 | |
| 425 | double maxVal = 0, maxZError = m_headerInfo.maxZError; |
| 426 | int nBytesRaw = (int)(1 + numValidPixel * sizeof(T)); |
| 427 | |
| 428 | if ((maxZError == 0 && zMax > zMin) |
| 429 | || (maxZError > 0 && (maxVal = ComputeMaxVal(zMin, zMax, maxZError)) > m_maxValToQuantize)) |
| 430 | { |
| 431 | return nBytesRaw; |
| 432 | } |
| 433 | else |
| 434 | { |
| 435 | DataType dtReduced; |
| 436 | ReduceDataType(zMin, dtZ, dtReduced); |
| 437 | int nBytes = 1 + GetDataTypeSize(dtReduced); |
| 438 | |
| 439 | unsigned int maxElem = (unsigned int)(maxVal + 0.5); |
| 440 | if (maxElem > 0) |
| 441 | { |
| 442 | nBytes += (!tryLut) ? BitStuffer2::ComputeNumBytesNeededSimple(numValidPixel, maxElem) |
| 443 | : BitStuffer2::ComputeNumBytesNeededLut(sortedQuantVec, tryLut); |
| 444 | } |
| 445 | |
| 446 | if (nBytes < nBytesRaw) |
| 447 | blockEncodeMode = (!tryLut || maxElem == 0) ? BEM_BitStuffSimple : BEM_BitStuffLUT; |
| 448 | else |
| 449 | nBytes = nBytesRaw; |
| 450 | |
| 451 | return nBytes; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | // -------------------------------------------------------------------------- ; |
| 456 | |
| 457 | template<class T> |
| 458 | inline int Lerc2::ReduceDataType(T z, DataType dt, DataType& dtReduced) |
| 459 | { |
| 460 | // clamp to dst range first to guarantee portable code, see https://www.cplusplus.com/doc/tutorial/typecasting/ |
| 461 | |
| 462 | Byte b = (z >= 0 && z <= 255) ? (Byte)z : 0; |
| 463 | switch (dt) |
| 464 | { |
| 465 | case DT_Short: |
| 466 | { |
| 467 | signed char c = (z >= (double)-128 && z <= 127) ? (signed char)z : 0; // avoid signed / unsigned warnings on some compilers |
| 468 | int tc = (T)c == z ? 2 : (T)b == z ? 1 : 0; |
| 469 | dtReduced = (DataType)(dt - tc); |
| 470 | return tc; |
| 471 | } |
| 472 | case DT_UShort: |
| 473 | { |
| 474 | int tc = (T)b == z ? 1 : 0; |
| 475 | dtReduced = (DataType)(dt - 2 * tc); |
| 476 | return tc; |
| 477 | } |
| 478 | case DT_Int: |
| 479 | { |
| 480 | short s = (z >= (double)SHRT_MIN && z <= SHRT_MAX) ? (short)z : 0; |
| 481 | unsigned short us = (z >= 0 && z <= USHRT_MAX) ? (unsigned short)z : 0; |
| 482 | int tc = (T)b == z ? 3 : (T)s == z ? 2 : (T)us == z ? 1 : 0; |
| 483 | dtReduced = (DataType)(dt - tc); |
| 484 | return tc; |
| 485 | } |
| 486 | case DT_UInt: |
| 487 | { |
| 488 | unsigned short us = (z >= 0 && z <= USHRT_MAX) ? (unsigned short)z : 0; |
| 489 | int tc = (T)b == z ? 2 : (T)us == z ? 1 : 0; |
| 490 | dtReduced = (DataType)(dt - 2 * tc); |
| 491 | return tc; |
| 492 | } |
| 493 | case DT_Float: |
| 494 | { |
| 495 | short s = (z >= (float)SHRT_MIN && z <= SHRT_MAX) ? (short)z : 0; |
| 496 | int tc = (T)b == z ? 2 : (T)s == z ? 1 : 0; |
| 497 | dtReduced = tc == 0 ? dt : (tc == 1 ? DT_Short : DT_Byte); |
| 498 | return tc; |
| 499 | } |
| 500 | case DT_Double: |
| 501 | { |
| 502 | short s = (z >= (double)SHRT_MIN && z <= SHRT_MAX) ? (short)z : 0; |
| 503 | int l = (z >= (double)INT_MIN && z <= (double)INT_MAX) ? (int)z : 0; |
| 504 | float f = (z >= -FLT_MAX && z <= FLT_MAX) ? (float)z : 0; |
| 505 | int tc = (T)s == z ? 3 : (T)l == z ? 2 : (T)f == z ? 1 : 0; |
| 506 | dtReduced = tc == 0 ? dt : (DataType)(dt - 2 * tc + 1); |
| 507 | return tc; |
| 508 | } |
| 509 | default: |
| 510 | { |
| 511 | dtReduced = dt; |
| 512 | return 0; |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | // -------------------------------------------------------------------------- ; |
| 518 | |
| 519 | inline Lerc2::DataType Lerc2::ValidateDataType(int dt) |
| 520 | { |
| 521 | if (dt >= DT_Char && dt <= DT_Double) |
| 522 | return static_cast<DataType>(dt); |
| 523 | return DT_Undefined; |
| 524 | } |
| 525 | |
| 526 | // -------------------------------------------------------------------------- ; |
| 527 | |
| 528 | inline |
| 529 | Lerc2::DataType Lerc2::GetDataTypeUsed(DataType dt, int tc) |
| 530 | { |
| 531 | switch (dt) |
| 532 | { |
| 533 | case DT_Short: |
| 534 | case DT_Int: return ValidateDataType(dt - tc); |
| 535 | case DT_UShort: |
| 536 | case DT_UInt: return ValidateDataType(dt - 2 * tc); |
| 537 | case DT_Float: return tc == 0 ? dt : (tc == 1 ? DT_Short : DT_Byte); |
| 538 | case DT_Double: return tc == 0 ? dt : ValidateDataType(dt - 2 * tc + 1); |
| 539 | default: |
| 540 | return dt; |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | // -------------------------------------------------------------------------- ; |
| 545 | |
| 546 | inline |
| 547 | bool Lerc2::WriteVariableDataType(Byte** ppByte, double z, DataType dtUsed) |
| 548 | { |
| 549 | Byte* ptr = *ppByte; |
| 550 | |
| 551 | switch (dtUsed) |
| 552 | { |
| 553 | case DT_Char: |
| 554 | { |
| 555 | *((signed char*)ptr) = (signed char)z; |
| 556 | ptr++; |
| 557 | break; |
| 558 | } |
| 559 | case DT_Byte: |
| 560 | { |
| 561 | *((Byte*)ptr) = (Byte)z; |
| 562 | ptr++; |
| 563 | break; |
| 564 | } |
| 565 | case DT_Short: |
| 566 | { |
| 567 | short s = (short)z; |
| 568 | memcpy(ptr, &s, sizeof(short)); |
| 569 | ptr += 2; |
| 570 | break; |
| 571 | } |
| 572 | case DT_UShort: |
| 573 | { |
| 574 | unsigned short us = (unsigned short)z; |
| 575 | memcpy(ptr, &us, sizeof(unsigned short)); |
| 576 | ptr += 2; |
| 577 | break; |
| 578 | } |
| 579 | case DT_Int: |
| 580 | { |
| 581 | int i = (int)z; |
| 582 | memcpy(ptr, &i, sizeof(int)); |
| 583 | ptr += 4; |
| 584 | break; |
| 585 | } |
| 586 | case DT_UInt: |
| 587 | { |
| 588 | unsigned int n = (unsigned int)z; |
| 589 | memcpy(ptr, &n, sizeof(unsigned int)); |
| 590 | ptr += 4; |
| 591 | break; |
| 592 | } |
| 593 | case DT_Float: |
| 594 | { |
| 595 | float f = (float)z; |
| 596 | memcpy(ptr, &f, sizeof(float)); |
| 597 | ptr += 4; |
| 598 | break; |
| 599 | } |
| 600 | case DT_Double: |
| 601 | { |
| 602 | memcpy(ptr, &z, sizeof(double)); |
| 603 | ptr += 8; |
| 604 | break; |
| 605 | } |
| 606 | |
| 607 | default: |
| 608 | return false; |
| 609 | } |
| 610 | |
| 611 | *ppByte = ptr; |
| 612 | return true; |
| 613 | } |
| 614 | |
| 615 | // -------------------------------------------------------------------------- ; |
| 616 | |
| 617 | inline |
| 618 | double Lerc2::ReadVariableDataType(const Byte** ppByte, DataType dtUsed) |
| 619 | { |
| 620 | const Byte* ptr = *ppByte; |
| 621 | |
| 622 | switch (dtUsed) |
| 623 | { |
| 624 | case DT_Char: |
| 625 | { |
| 626 | signed char c = *((signed char*)ptr); |
| 627 | *ppByte = ptr + 1; |
| 628 | return c; |
| 629 | } |
| 630 | case DT_Byte: |
| 631 | { |
| 632 | Byte b = *((Byte*)ptr); |
| 633 | *ppByte = ptr + 1; |
| 634 | return b; |
| 635 | } |
| 636 | case DT_Short: |
| 637 | { |
| 638 | short s; |
| 639 | memcpy(&s, ptr, sizeof(short)); |
| 640 | *ppByte = ptr + 2; |
| 641 | return s; |
| 642 | } |
| 643 | case DT_UShort: |
| 644 | { |
| 645 | unsigned short us; |
| 646 | memcpy(&us, ptr, sizeof(unsigned short)); |
| 647 | *ppByte = ptr + 2; |
| 648 | return us; |
| 649 | } |
| 650 | case DT_Int: |
| 651 | { |
| 652 | int i; |
| 653 | memcpy(&i, ptr, sizeof(int)); |
| 654 | *ppByte = ptr + 4; |
| 655 | return i; |
| 656 | } |
| 657 | case DT_UInt: |
| 658 | { |
| 659 | unsigned int n; |
| 660 | memcpy(&n, ptr, sizeof(unsigned int)); |
| 661 | *ppByte = ptr + 4; |
| 662 | return n; |
| 663 | } |
| 664 | case DT_Float: |
| 665 | { |
| 666 | float f; |
| 667 | memcpy(&f, ptr, sizeof(float)); |
| 668 | *ppByte = ptr + 4; |
| 669 | return f; |
| 670 | } |
| 671 | case DT_Double: |
| 672 | { |
| 673 | double d; |
| 674 | memcpy(&d, ptr, sizeof(double)); |
| 675 | *ppByte = ptr + 8; |
| 676 | return d; |
| 677 | } |
| 678 | default: |
| 679 | return 0; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | // -------------------------------------------------------------------------- ; |
| 684 | |
| 685 | inline |
| 686 | unsigned int Lerc2::GetMaxValToQuantize(DataType dt) |
| 687 | { |
| 688 | switch (dt) |
| 689 | { |
| 690 | case DT_Char: |
| 691 | case DT_Byte: //return (1 << 7) - 1; // disabled: allow LUT mode for 8 bit segmented |
| 692 | case DT_Short: |
| 693 | case DT_UShort: return (1 << 15) - 1; |
| 694 | |
| 695 | case DT_Int: |
| 696 | case DT_UInt: |
| 697 | case DT_Float: |
| 698 | case DT_Double: return (1 << 30) - 1; |
| 699 | |
| 700 | default: |
| 701 | return 0; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | // -------------------------------------------------------------------------- ; |
| 706 | |
| 707 | inline |
| 708 | unsigned int Lerc2::GetDataTypeSize(DataType dt) |
| 709 | { |
| 710 | switch (dt) |
| 711 | { |
| 712 | case DT_Char: |
| 713 | case DT_Byte: return 1; |
| 714 | case DT_Short: |
| 715 | case DT_UShort: return 2; |
| 716 | case DT_Int: |
| 717 | case DT_UInt: |
| 718 | case DT_Float: return 4; |
| 719 | case DT_Double: return 8; |
| 720 | |
| 721 | default: |
| 722 | return 0; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | // -------------------------------------------------------------------------- ; |
| 727 | |
| 728 | inline |
| 729 | bool Lerc2::CheckMinMaxRanges(bool& minMaxEqual) const |
| 730 | { |
| 731 | int nDepth = m_headerInfo.nDepth; |
| 732 | if ((int)m_zMinVec.size() != nDepth || (int)m_zMaxVec.size() != nDepth) |
| 733 | return false; |
| 734 | |
| 735 | minMaxEqual = (0 == memcmp(&m_zMinVec[0], &m_zMaxVec[0], nDepth * sizeof(m_zMinVec[0]))); |
| 736 | return true; |
| 737 | } |
| 738 | |
| 739 | // -------------------------------------------------------------------------- ; |
| 740 | |
| 741 | NAMESPACE_LERC_END |
| 742 | #endif |