| 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 | Lucian Plesea (provided checksum code) |
| 23 | */ |
| 24 | |
| 25 | #include <climits> |
| 26 | #include <typeinfo> |
| 27 | #include "Defines.h" |
| 28 | #include "Lerc2.h" |
| 29 | #include "Huffman.h" |
| 30 | #include "RLE.h" |
| 31 | |
| 32 | USING_NAMESPACE_LERC |
| 33 | using namespace std; |
| 34 | |
| 35 | // -------------------------------------------------------------------------- ; |
| 36 | |
| 37 | Lerc2::Lerc2() |
| 38 | { |
| 39 | Init(); |
| 40 | } |
| 41 | |
| 42 | // -------------------------------------------------------------------------- ; |
| 43 | |
| 44 | Lerc2::Lerc2(int nDepth, int nCols, int nRows, const Byte* pMaskBits) |
| 45 | { |
| 46 | Init(); |
| 47 | Set(nDepth, nCols, nRows, pMaskBits); |
| 48 | } |
| 49 | |
| 50 | // -------------------------------------------------------------------------- ; |
| 51 | |
| 52 | bool Lerc2::SetEncoderToOldVersion(int version) |
| 53 | { |
| 54 | if (version < 2 || version > CurrentVersion()) |
| 55 | return false; |
| 56 | |
| 57 | if (version < 4 && m_headerInfo.nDepth > 1) |
| 58 | return false; |
| 59 | |
| 60 | m_headerInfo.version = version; |
| 61 | |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | // -------------------------------------------------------------------------- ; |
| 66 | |
| 67 | void Lerc2::Init() |
| 68 | { |
| 69 | m_microBlockSize = 8; |
| 70 | m_maxValToQuantize = 0; |
| 71 | m_encodeMask = true; |
| 72 | m_writeDataOneSweep = false; |
| 73 | m_minMaxSet = false; |
| 74 | m_imageEncodeMode = IEM_Tiling; |
| 75 | |
| 76 | m_headerInfo.RawInit(); |
| 77 | m_headerInfo.version = CurrentVersion(); |
| 78 | m_headerInfo.microBlockSize = m_microBlockSize; |
| 79 | } |
| 80 | |
| 81 | // -------------------------------------------------------------------------- ; |
| 82 | |
| 83 | bool Lerc2::Set(int nDepth, int nCols, int nRows, const Byte* pMaskBits) |
| 84 | { |
| 85 | if (nDepth > 1 && m_headerInfo.version < 4) |
| 86 | return false; |
| 87 | |
| 88 | if (!m_bitMask.SetSize(nCols, nRows)) |
| 89 | return false; |
| 90 | |
| 91 | if (pMaskBits) |
| 92 | { |
| 93 | memcpy(m_bitMask.Bits(), pMaskBits, m_bitMask.Size()); |
| 94 | |
| 95 | int64_t numValid = m_bitMask.CountValidBits(); |
| 96 | if (numValid < 0 || numValid > (int64_t)INT_MAX) |
| 97 | return false; |
| 98 | |
| 99 | m_headerInfo.numValidPixel = (int)numValid; |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | m_headerInfo.numValidPixel = nCols * nRows; |
| 104 | m_bitMask.SetAllValid(); |
| 105 | } |
| 106 | |
| 107 | m_headerInfo.nDepth = nDepth; |
| 108 | m_headerInfo.nCols = nCols; |
| 109 | m_headerInfo.nRows = nRows; |
| 110 | |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | // -------------------------------------------------------------------------- ; |
| 115 | |
| 116 | bool Lerc2::SetNoDataValues(bool bNeedsNoDataVal, double noDataVal, double noDataValOrig) |
| 117 | { |
| 118 | if (m_headerInfo.version < 6) |
| 119 | return false; |
| 120 | |
| 121 | m_headerInfo.bPassNoDataValues = bNeedsNoDataVal; |
| 122 | m_headerInfo.noDataVal = bNeedsNoDataVal ? noDataVal : 0; |
| 123 | m_headerInfo.noDataValOrig = bNeedsNoDataVal ? noDataValOrig : 0; |
| 124 | |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | // -------------------------------------------------------------------------- ; |
| 129 | |
| 130 | bool Lerc2::SetNumBlobsMoreToCome(int nBlobsMore) |
| 131 | { |
| 132 | if (m_headerInfo.version < 6) |
| 133 | return false; |
| 134 | |
| 135 | m_headerInfo.nBlobsMore = nBlobsMore; |
| 136 | |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | // -------------------------------------------------------------------------- ; |
| 141 | |
| 142 | bool Lerc2::SetIsAllInt(bool bIsAllInt) |
| 143 | { |
| 144 | if (m_headerInfo.version < 6) |
| 145 | return false; |
| 146 | |
| 147 | m_headerInfo.bIsInt = bIsAllInt ? 1 : 0; |
| 148 | |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | // -------------------------------------------------------------------------- ; |
| 153 | |
| 154 | bool Lerc2::SetMinMax(int nDepth, double minVal, double maxVal) |
| 155 | { |
| 156 | ClearMinMax(); |
| 157 | |
| 158 | if (nDepth != 1 || minVal > maxVal) |
| 159 | return false; |
| 160 | |
| 161 | m_zMinVec.assign(1, minVal); |
| 162 | m_zMaxVec.assign(1, maxVal); |
| 163 | m_minMaxSet = true; |
| 164 | |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | // -------------------------------------------------------------------------- ; |
| 169 | |
| 170 | void Lerc2::ClearMinMax() |
| 171 | { |
| 172 | m_zMinVec.clear(); |
| 173 | m_zMaxVec.clear(); |
| 174 | m_minMaxSet = false; |
| 175 | } |
| 176 | |
| 177 | // -------------------------------------------------------------------------- ; |
| 178 | |
| 179 | template<class T> |
| 180 | unsigned int Lerc2::ComputeNumBytesNeededToWrite(const T* arr, double maxZError, bool encodeMask) |
| 181 | { |
| 182 | if (!arr || !IsLittleEndianSystem()) |
| 183 | return 0; |
| 184 | |
| 185 | // header |
| 186 | unsigned int nBytesHeaderMask = ComputeNumBytesHeaderToWrite(m_headerInfo); |
| 187 | |
| 188 | // valid / invalid mask |
| 189 | int numValid = m_headerInfo.numValidPixel; |
| 190 | int numTotal = m_headerInfo.nCols * m_headerInfo.nRows; |
| 191 | |
| 192 | bool needMask = numValid > 0 && numValid < numTotal; |
| 193 | |
| 194 | m_encodeMask = encodeMask; |
| 195 | |
| 196 | nBytesHeaderMask += 1 * sizeof(int); // the mask encode numBytes |
| 197 | |
| 198 | if (needMask && encodeMask) |
| 199 | { |
| 200 | RLE rle; |
| 201 | size_t n = rle.computeNumBytesRLE((const Byte*)m_bitMask.Bits(), m_bitMask.Size()); |
| 202 | nBytesHeaderMask += (unsigned int)n; |
| 203 | } |
| 204 | |
| 205 | m_headerInfo.dt = GetDataType(arr[0]); |
| 206 | |
| 207 | if (m_headerInfo.dt == DT_Undefined) |
| 208 | return 0; |
| 209 | |
| 210 | if (maxZError == 777) // cheat code |
| 211 | maxZError = -0.01; |
| 212 | |
| 213 | if (m_headerInfo.dt < DT_Float) // integer types |
| 214 | { |
| 215 | // interpret a negative maxZError as bit plane epsilon; dflt = 0.01; |
| 216 | if (maxZError < 0 && (!TryBitPlaneCompression(arr, -maxZError, maxZError))) |
| 217 | maxZError = 0; |
| 218 | |
| 219 | maxZError = std::max(0.5, floor(maxZError)); |
| 220 | } |
| 221 | else // float types |
| 222 | { |
| 223 | if (maxZError < 0) // don't allow bit plane compression for float or double |
| 224 | return 0; |
| 225 | |
| 226 | double maxZErrorNew = maxZError; |
| 227 | if (maxZError > 0 && TryRaiseMaxZError(arr, maxZErrorNew)) |
| 228 | { |
| 229 | //printf("%f --> %f\n", maxZError, maxZErrorNew); |
| 230 | maxZError = maxZErrorNew; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | m_headerInfo.maxZError = maxZError; |
| 235 | m_headerInfo.zMin = 0; |
| 236 | m_headerInfo.zMax = 0; |
| 237 | m_headerInfo.microBlockSize = m_microBlockSize; |
| 238 | m_headerInfo.blobSize = nBytesHeaderMask; |
| 239 | |
| 240 | if (numValid == 0) |
| 241 | return nBytesHeaderMask; |
| 242 | |
| 243 | m_maxValToQuantize = GetMaxValToQuantize(m_headerInfo.dt); |
| 244 | |
| 245 | Byte* ptr = nullptr; // only emulate the writing and just count the bytes needed |
| 246 | int nBytesTiling = 0; |
| 247 | |
| 248 | if ((!m_minMaxSet || m_headerInfo.nDepth > 1) |
| 249 | && !ComputeMinMaxRanges(arr, m_zMinVec, m_zMaxVec)) // need this for diff encoding before WriteTiles() |
| 250 | return 0; |
| 251 | |
| 252 | m_headerInfo.zMin = *std::min_element(m_zMinVec.begin(), m_zMinVec.end()); |
| 253 | m_headerInfo.zMax = *std::max_element(m_zMaxVec.begin(), m_zMaxVec.end()); |
| 254 | |
| 255 | if (m_headerInfo.zMin == m_headerInfo.zMax) // image is const |
| 256 | return nBytesHeaderMask; |
| 257 | |
| 258 | int nDepth = m_headerInfo.nDepth; |
| 259 | |
| 260 | if (m_headerInfo.version >= 4) |
| 261 | { |
| 262 | // add the min max ranges behind the mask and before the main data; |
| 263 | // so we do not write it if no valid pixel or all same value const |
| 264 | |
| 265 | size_t currBlobSize = m_headerInfo.blobSize; |
| 266 | currBlobSize += sizeof(T) * nDepth * 2; |
| 267 | |
| 268 | if (currBlobSize > (size_t)INT_MAX) // limit Lerc blob size per band to 2 GB |
| 269 | return 0; |
| 270 | |
| 271 | m_headerInfo.blobSize = (int)currBlobSize; |
| 272 | |
| 273 | bool minMaxEqual = false; |
| 274 | if (!CheckMinMaxRanges(minMaxEqual)) |
| 275 | return 0; |
| 276 | |
| 277 | if (minMaxEqual) |
| 278 | return m_headerInfo.blobSize; // all nDepth bands are const |
| 279 | } |
| 280 | |
| 281 | // data |
| 282 | if (!WriteTiles(arr, &ptr, nBytesTiling) || nBytesTiling < 0) |
| 283 | return 0; |
| 284 | |
| 285 | m_imageEncodeMode = IEM_Tiling; |
| 286 | int nBytesData = nBytesTiling; |
| 287 | int nBytesHuffman = 0; |
| 288 | |
| 289 | if (m_headerInfo.TryHuffmanInt()) |
| 290 | { |
| 291 | ImageEncodeMode huffmanEncMode; |
| 292 | ComputeHuffmanCodes(arr, nBytesHuffman, huffmanEncMode, m_huffmanCodes); // save Huffman codes for later use |
| 293 | |
| 294 | if (nBytesHuffman < 0) |
| 295 | nBytesHuffman = INT_MAX; |
| 296 | |
| 297 | if (!m_huffmanCodes.empty() && nBytesHuffman < nBytesTiling) |
| 298 | { |
| 299 | m_imageEncodeMode = huffmanEncMode; |
| 300 | nBytesData = nBytesHuffman; |
| 301 | } |
| 302 | else |
| 303 | m_huffmanCodes.resize(0); |
| 304 | } |
| 305 | else if (m_headerInfo.TryHuffmanFlt()) |
| 306 | { |
| 307 | m_huffmanCodes.resize(0); |
| 308 | |
| 309 | bool rv = m_lfpc.ComputeHuffmanCodesFlt(arr, (m_headerInfo.dt == DT_Double), |
| 310 | m_headerInfo.nCols, m_headerInfo.nRows, m_headerInfo.nDepth); |
| 311 | |
| 312 | if (!rv) |
| 313 | return 0; |
| 314 | |
| 315 | if (rv) |
| 316 | { |
| 317 | nBytesHuffman = m_lfpc.compressedLength(); |
| 318 | |
| 319 | if (nBytesHuffman < 0) |
| 320 | nBytesHuffman = INT_MAX; |
| 321 | |
| 322 | if (nBytesHuffman < nBytesTiling * 0.9) // demand at least 10% better than not Huffman |
| 323 | { |
| 324 | nBytesData = nBytesHuffman; |
| 325 | m_imageEncodeMode = IEM_DeltaDeltaHuffman; |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | m_writeDataOneSweep = false; |
| 331 | size_t nBytesDataOneSweep = sizeof(T) * nDepth * numValid; |
| 332 | |
| 333 | { |
| 334 | // try with double block size to reduce block header overhead, if |
| 335 | if (((size_t)nBytesTiling * 8 < (size_t)numTotal * nDepth * 1.5) // resulting bit rate < x (2 bpp) |
| 336 | && ((size_t)nBytesTiling < 4 * nBytesDataOneSweep) // bit stuffing is effective |
| 337 | && (nBytesHuffman == 0 || (size_t)nBytesTiling < (size_t)2 * nBytesHuffman) // not much worse than huffman (otherwise huffman wins anyway) |
| 338 | && (m_headerInfo.nRows > m_microBlockSize || m_headerInfo.nCols > m_microBlockSize)) |
| 339 | { |
| 340 | m_headerInfo.microBlockSize = m_microBlockSize * 2; |
| 341 | |
| 342 | int nBytes2 = 0; |
| 343 | if (!WriteTiles(arr, &ptr, nBytes2) || nBytes2 < 0) // no huffman in here anymore |
| 344 | return 0; |
| 345 | |
| 346 | if (nBytes2 <= nBytesData) |
| 347 | { |
| 348 | nBytesData = nBytes2; |
| 349 | m_imageEncodeMode = IEM_Tiling; |
| 350 | m_huffmanCodes.resize(0); |
| 351 | } |
| 352 | else |
| 353 | { |
| 354 | m_headerInfo.microBlockSize = m_microBlockSize; // reset to orig |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | if (m_headerInfo.TryHuffmanInt() || m_headerInfo.TryHuffmanFlt()) |
| 360 | nBytesData += 1; // flag for image encode mode |
| 361 | |
| 362 | size_t totalBlobSize = m_headerInfo.blobSize; |
| 363 | |
| 364 | if (nBytesDataOneSweep <= (size_t)nBytesData) |
| 365 | { |
| 366 | m_writeDataOneSweep = true; // fallback: write data binary uncompressed in one sweep |
| 367 | totalBlobSize += 1 + nBytesDataOneSweep; // header, mask, min max ranges, flag, data one sweep |
| 368 | } |
| 369 | else |
| 370 | { |
| 371 | m_writeDataOneSweep = false; |
| 372 | totalBlobSize += 1 + nBytesData; // header, mask, min max ranges, flag(s), data |
| 373 | } |
| 374 | |
| 375 | if (totalBlobSize > (size_t)INT_MAX) // limit Lerc blob size per band to 2 GB |
| 376 | return 0; |
| 377 | |
| 378 | m_headerInfo.blobSize = (int)totalBlobSize; |
| 379 | |
| 380 | return m_headerInfo.blobSize; |
| 381 | } |
| 382 | |
| 383 | // -------------------------------------------------------------------------- ; |
| 384 | |
| 385 | template unsigned int Lerc2::ComputeNumBytesNeededToWrite<signed char>(const signed char* arr, double maxZError, bool encodeMask); |
| 386 | template unsigned int Lerc2::ComputeNumBytesNeededToWrite<Byte>(const Byte* arr, double maxZError, bool encodeMask); |
| 387 | template unsigned int Lerc2::ComputeNumBytesNeededToWrite<short>(const short* arr, double maxZError, bool encodeMask); |
| 388 | template unsigned int Lerc2::ComputeNumBytesNeededToWrite<unsigned short>(const unsigned short* arr, double maxZError, bool encodeMask); |
| 389 | template unsigned int Lerc2::ComputeNumBytesNeededToWrite<int>(const int* arr, double maxZError, bool encodeMask); |
| 390 | template unsigned int Lerc2::ComputeNumBytesNeededToWrite<unsigned int>(const unsigned int* arr, double maxZError, bool encodeMask); |
| 391 | template unsigned int Lerc2::ComputeNumBytesNeededToWrite<float>(const float* arr, double maxZError, bool encodeMask); |
| 392 | template unsigned int Lerc2::ComputeNumBytesNeededToWrite<double>(const double* arr, double maxZError, bool encodeMask); |
| 393 | |
| 394 | // -------------------------------------------------------------------------- ; |
| 395 | |
| 396 | template<class T> |
| 397 | bool Lerc2::Encode(const T* arr, Byte** ppByte) |
| 398 | { |
| 399 | if (!arr || !ppByte || !IsLittleEndianSystem()) |
| 400 | return false; |
| 401 | |
| 402 | Byte* ptrBlob = *ppByte; // keep a ptr to the start of the blob |
| 403 | |
| 404 | if (!WriteHeader(ppByte, m_headerInfo)) |
| 405 | return false; |
| 406 | |
| 407 | if (!WriteMask(ppByte)) |
| 408 | return false; |
| 409 | |
| 410 | if (m_headerInfo.numValidPixel == 0 || m_headerInfo.zMin == m_headerInfo.zMax) |
| 411 | { |
| 412 | return DoChecksOnEncode(ptrBlob, *ppByte); |
| 413 | } |
| 414 | |
| 415 | if (m_headerInfo.version >= 4) |
| 416 | { |
| 417 | if (!WriteMinMaxRanges(arr, ppByte)) |
| 418 | return false; |
| 419 | |
| 420 | bool minMaxEqual = false; |
| 421 | if (!CheckMinMaxRanges(minMaxEqual)) |
| 422 | return false; |
| 423 | |
| 424 | if (minMaxEqual) |
| 425 | return DoChecksOnEncode(ptrBlob, *ppByte); |
| 426 | } |
| 427 | |
| 428 | **ppByte = m_writeDataOneSweep ? 1 : 0; // write flag |
| 429 | (*ppByte)++; |
| 430 | |
| 431 | if (!m_writeDataOneSweep) |
| 432 | { |
| 433 | if (m_headerInfo.TryHuffmanInt() || m_headerInfo.TryHuffmanFlt()) |
| 434 | { |
| 435 | **ppByte = (Byte)m_imageEncodeMode; // Huffman or tiling encode mode |
| 436 | (*ppByte)++; |
| 437 | |
| 438 | if (m_imageEncodeMode != IEM_Tiling) |
| 439 | { |
| 440 | if (m_headerInfo.TryHuffmanFlt()) |
| 441 | { |
| 442 | if (!(m_imageEncodeMode == IEM_DeltaHuffman || m_imageEncodeMode == IEM_Huffman || m_imageEncodeMode == IEM_DeltaDeltaHuffman)) |
| 443 | return false; |
| 444 | |
| 445 | if (!m_lfpc.EncodeHuffmanFlt(ppByte)) |
| 446 | return false; |
| 447 | |
| 448 | return DoChecksOnEncode(ptrBlob, *ppByte); |
| 449 | } |
| 450 | |
| 451 | if (!m_huffmanCodes.empty()) // Huffman, no tiling |
| 452 | { |
| 453 | if (m_headerInfo.TryHuffmanInt()) |
| 454 | { |
| 455 | if (!(m_imageEncodeMode == IEM_DeltaHuffman || m_imageEncodeMode == IEM_Huffman)) |
| 456 | return false; |
| 457 | |
| 458 | if (!EncodeHuffman(arr, ppByte)) // data bit stuffed |
| 459 | return false; |
| 460 | } |
| 461 | else |
| 462 | return false; |
| 463 | |
| 464 | return DoChecksOnEncode(ptrBlob, *ppByte); |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | int numBytes = 0; |
| 470 | if (!WriteTiles(arr, ppByte, numBytes) || numBytes < 0) |
| 471 | return false; |
| 472 | } |
| 473 | else |
| 474 | { |
| 475 | if (!WriteDataOneSweep(arr, ppByte)) |
| 476 | return false; |
| 477 | } |
| 478 | |
| 479 | return DoChecksOnEncode(ptrBlob, *ppByte); |
| 480 | } |
| 481 | |
| 482 | // -------------------------------------------------------------------------- ; |
| 483 | |
| 484 | template bool Lerc2::Encode<signed char>(const signed char* arr, Byte** ppByte); |
| 485 | template bool Lerc2::Encode<Byte>(const Byte* arr, Byte** ppByte); |
| 486 | template bool Lerc2::Encode<short>(const short* arr, Byte** ppByte); |
| 487 | template bool Lerc2::Encode<unsigned short>(const unsigned short* arr, Byte** ppByte); |
| 488 | template bool Lerc2::Encode<int>(const int* arr, Byte** ppByte); |
| 489 | template bool Lerc2::Encode<unsigned int>(const unsigned int* arr, Byte** ppByte); |
| 490 | template bool Lerc2::Encode<float>(const float* arr, Byte** ppByte); |
| 491 | template bool Lerc2::Encode<double>(const double* arr, Byte** ppByte); |
| 492 | |
| 493 | // -------------------------------------------------------------------------- ; |
| 494 | |
| 495 | bool Lerc2::GetHeaderInfo(const Byte* pByte, size_t nBytesRemaining, struct HeaderInfo& hd, bool& bHasMask) |
| 496 | { |
| 497 | if (!pByte || !IsLittleEndianSystem()) |
| 498 | return false; |
| 499 | |
| 500 | if (!ReadHeader(&pByte, nBytesRemaining, hd)) |
| 501 | return false; |
| 502 | |
| 503 | int numBytesMask(0); |
| 504 | if (nBytesRemaining < sizeof(int) || !memcpy(&numBytesMask, pByte, sizeof(int)) || numBytesMask < 0) |
| 505 | return false; |
| 506 | |
| 507 | bHasMask = numBytesMask > 0; |
| 508 | |
| 509 | return true; |
| 510 | } |
| 511 | |
| 512 | // -------------------------------------------------------------------------- ; |
| 513 | |
| 514 | bool Lerc2::GetRanges(const Byte* pByte, size_t nBytesRemaining, double* pMins, double* pMaxs) |
| 515 | { |
| 516 | if (!pByte || !IsLittleEndianSystem() || m_headerInfo.version < 4 || !pMins || !pMaxs) |
| 517 | return false; |
| 518 | |
| 519 | if (!ReadHeader(&pByte, nBytesRemaining, m_headerInfo)) |
| 520 | return false; |
| 521 | |
| 522 | if (!ReadMask(&pByte, nBytesRemaining)) |
| 523 | return false; |
| 524 | |
| 525 | const int nDepth = m_headerInfo.nDepth; |
| 526 | |
| 527 | if (m_headerInfo.numValidPixel == 0) // image is empty |
| 528 | { |
| 529 | memset(pMins, 0, nDepth * sizeof(double)); // fill with 0 |
| 530 | memset(pMaxs, 0, nDepth * sizeof(double)); |
| 531 | |
| 532 | return true; |
| 533 | } |
| 534 | |
| 535 | if (m_headerInfo.zMin == m_headerInfo.zMax) // image is const |
| 536 | { |
| 537 | double val = m_headerInfo.zMin; |
| 538 | |
| 539 | for (int i = 0; i < nDepth; i++) // fill with const |
| 540 | pMins[i] = pMaxs[i] = val; |
| 541 | |
| 542 | return true; |
| 543 | } |
| 544 | |
| 545 | bool rv = false; |
| 546 | void* ptr = nullptr; |
| 547 | |
| 548 | switch (m_headerInfo.dt) |
| 549 | { |
| 550 | case DT_Char: rv = ReadMinMaxRanges(&pByte, nBytesRemaining, (signed char*) ptr); break; |
| 551 | case DT_Byte: rv = ReadMinMaxRanges(&pByte, nBytesRemaining, (Byte*) ptr); break; |
| 552 | case DT_Short: rv = ReadMinMaxRanges(&pByte, nBytesRemaining, (short*) ptr); break; |
| 553 | case DT_UShort: rv = ReadMinMaxRanges(&pByte, nBytesRemaining, (unsigned short*)ptr); break; |
| 554 | case DT_Int: rv = ReadMinMaxRanges(&pByte, nBytesRemaining, (int*) ptr); break; |
| 555 | case DT_UInt: rv = ReadMinMaxRanges(&pByte, nBytesRemaining, (unsigned int*) ptr); break; |
| 556 | case DT_Float : rv = ReadMinMaxRanges(&pByte, nBytesRemaining, (float*) ptr); break; |
| 557 | case DT_Double: rv = ReadMinMaxRanges(&pByte, nBytesRemaining, (double*) ptr); break; |
| 558 | |
| 559 | default: |
| 560 | return false; |
| 561 | } |
| 562 | |
| 563 | if (!rv) |
| 564 | return false; |
| 565 | |
| 566 | for (int i = 0; i < nDepth; i++) |
| 567 | { |
| 568 | pMins[i] = m_zMinVec[i]; |
| 569 | pMaxs[i] = m_zMaxVec[i]; |
| 570 | } |
| 571 | |
| 572 | return true; |
| 573 | } |
| 574 | |
| 575 | // -------------------------------------------------------------------------- ; |
| 576 | |
| 577 | template<class T> |
| 578 | bool Lerc2::Decode(const Byte** ppByte, size_t& nBytesRemaining, T* arr, Byte* pMaskBits) |
| 579 | { |
| 580 | if (!arr || !ppByte || !IsLittleEndianSystem()) |
| 581 | return false; |
| 582 | |
| 583 | const Byte* ptrBlob = *ppByte; // keep a ptr to the start of the blob |
| 584 | size_t nBytesRemaining00 = nBytesRemaining; |
| 585 | |
| 586 | if (!ReadHeader(ppByte, nBytesRemaining, m_headerInfo)) |
| 587 | return false; |
| 588 | |
| 589 | if (nBytesRemaining00 < (size_t)m_headerInfo.blobSize) |
| 590 | return false; |
| 591 | |
| 592 | if (m_headerInfo.version >= 3) |
| 593 | { |
| 594 | int nBytes = (int)(FileKey().length() + sizeof(int) + sizeof(unsigned int)); // start right after the checksum entry |
| 595 | if (m_headerInfo.blobSize < nBytes) |
| 596 | return false; |
| 597 | unsigned int checksum = ComputeChecksumFletcher32(ptrBlob + nBytes, m_headerInfo.blobSize - nBytes); |
| 598 | |
| 599 | if (checksum != m_headerInfo.checksum) |
| 600 | return false; |
| 601 | } |
| 602 | |
| 603 | if (!ReadMask(ppByte, nBytesRemaining)) |
| 604 | return false; |
| 605 | |
| 606 | if (pMaskBits) // return proper mask bits even if they were not stored |
| 607 | memcpy(pMaskBits, m_bitMask.Bits(), m_bitMask.Size()); |
| 608 | |
| 609 | memset(arr, 0, (size_t)m_headerInfo.nCols * m_headerInfo.nRows * m_headerInfo.nDepth * sizeof(T)); |
| 610 | |
| 611 | if (m_headerInfo.numValidPixel == 0) |
| 612 | return true; |
| 613 | |
| 614 | if (m_headerInfo.zMin == m_headerInfo.zMax) // image is const |
| 615 | { |
| 616 | if (!FillConstImage(arr)) |
| 617 | return false; |
| 618 | |
| 619 | return true; |
| 620 | } |
| 621 | |
| 622 | if (m_headerInfo.version >= 4) |
| 623 | { |
| 624 | if (!ReadMinMaxRanges(ppByte, nBytesRemaining, arr)) |
| 625 | return false; |
| 626 | |
| 627 | bool minMaxEqual = false; |
| 628 | if (!CheckMinMaxRanges(minMaxEqual)) |
| 629 | return false; |
| 630 | |
| 631 | if (minMaxEqual) // if all bands are const, fill outgoing and done |
| 632 | { |
| 633 | if (!FillConstImage(arr)) |
| 634 | return false; |
| 635 | |
| 636 | return true; // done |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | if (nBytesRemaining < 1) |
| 641 | return false; |
| 642 | |
| 643 | Byte readDataOneSweep = **ppByte; // read flag |
| 644 | (*ppByte)++; |
| 645 | nBytesRemaining--; |
| 646 | |
| 647 | if (!readDataOneSweep) |
| 648 | { |
| 649 | if (m_headerInfo.TryHuffmanInt() || m_headerInfo.TryHuffmanFlt()) |
| 650 | { |
| 651 | if (nBytesRemaining < 1) |
| 652 | return false; |
| 653 | |
| 654 | Byte flag = **ppByte; // read flag Huffman / Lerc2 |
| 655 | (*ppByte)++; |
| 656 | nBytesRemaining--; |
| 657 | |
| 658 | if (flag > 3 |
| 659 | || (flag > 2 && m_headerInfo.version < 6) |
| 660 | || (flag > 1 && m_headerInfo.version < 4)) |
| 661 | return false; |
| 662 | |
| 663 | m_imageEncodeMode = (ImageEncodeMode)flag; |
| 664 | |
| 665 | if (m_imageEncodeMode != IEM_Tiling) |
| 666 | { |
| 667 | if (m_headerInfo.TryHuffmanInt()) |
| 668 | { |
| 669 | if (m_imageEncodeMode == IEM_DeltaHuffman || (m_headerInfo.version >= 4 && m_imageEncodeMode == IEM_Huffman)) |
| 670 | return DecodeHuffman(ppByte, nBytesRemaining, arr); // done. |
| 671 | else |
| 672 | return false; |
| 673 | } |
| 674 | else if (m_headerInfo.TryHuffmanFlt() && m_imageEncodeMode == IEM_DeltaDeltaHuffman) |
| 675 | { |
| 676 | return LosslessFPCompression::DecodeHuffmanFlt(ppByte, nBytesRemaining, arr, |
| 677 | (m_headerInfo.dt == DT_Double), m_headerInfo.nCols, m_headerInfo.nRows, m_headerInfo.nDepth); |
| 678 | } |
| 679 | else |
| 680 | return false; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | if (!ReadTiles(ppByte, nBytesRemaining, arr)) |
| 685 | return false; |
| 686 | } |
| 687 | else |
| 688 | { |
| 689 | if (!ReadDataOneSweep(ppByte, nBytesRemaining, arr)) |
| 690 | return false; |
| 691 | } |
| 692 | |
| 693 | return true; |
| 694 | } |
| 695 | |
| 696 | // -------------------------------------------------------------------------- ; |
| 697 | |
| 698 | template bool Lerc2::Decode<signed char>(const Byte** ppByte, size_t& nBytesRemaining, signed char* arr, Byte* pMaskBits); |
| 699 | template bool Lerc2::Decode<Byte>(const Byte** ppByte, size_t& nBytesRemaining, Byte* arr, Byte* pMaskBits); |
| 700 | template bool Lerc2::Decode<short>(const Byte** ppByte, size_t& nBytesRemaining, short* arr, Byte* pMaskBits); |
| 701 | template bool Lerc2::Decode<unsigned short>(const Byte** ppByte, size_t& nBytesRemaining, unsigned short* arr, Byte* pMaskBits); |
| 702 | template bool Lerc2::Decode<int>(const Byte** ppByte, size_t& nBytesRemaining, int* arr, Byte* pMaskBits); |
| 703 | template bool Lerc2::Decode<unsigned int>(const Byte** ppByte, size_t& nBytesRemaining, unsigned int* arr, Byte* pMaskBits); |
| 704 | template bool Lerc2::Decode<float>(const Byte** ppByte, size_t& nBytesRemaining, float* arr, Byte* pMaskBits); |
| 705 | template bool Lerc2::Decode<double>(const Byte** ppByte, size_t& nBytesRemaining, double* arr, Byte* pMaskBits); |
| 706 | |
| 707 | // -------------------------------------------------------------------------- ; |
| 708 | // -------------------------------------------------------------------------- ; |
| 709 | |
| 710 | unsigned int Lerc2::ComputeNumBytesHeaderToWrite(const struct HeaderInfo& hd) |
| 711 | { |
| 712 | unsigned int numBytes = (unsigned int)FileKey().length(); |
| 713 | numBytes += 1 * sizeof(int); |
| 714 | numBytes += (hd.version >= 3 ? 1 : 0) * sizeof(unsigned int); |
| 715 | numBytes += (hd.version >= 4 ? 7 : 6) * sizeof(int); |
| 716 | numBytes += (hd.version >= 6 ? 1 : 0) * sizeof(int); |
| 717 | numBytes += (hd.version >= 6 ? 4 : 0) * sizeof(Byte); |
| 718 | numBytes += (hd.version >= 6 ? 5 : 3) * sizeof(double); |
| 719 | return numBytes; |
| 720 | } |
| 721 | |
| 722 | // -------------------------------------------------------------------------- ; |
| 723 | |
| 724 | bool Lerc2::WriteHeader(Byte** ppByte, const struct HeaderInfo& hd) |
| 725 | { |
| 726 | if (!ppByte) |
| 727 | return false; |
| 728 | |
| 729 | Byte* ptr = *ppByte; |
| 730 | |
| 731 | string fileKey = FileKey(); |
| 732 | size_t len = fileKey.length(); |
| 733 | memcpy(ptr, fileKey.c_str(), len); |
| 734 | ptr += len; |
| 735 | |
| 736 | memcpy(ptr, &hd.version, sizeof(int)); |
| 737 | ptr += sizeof(int); |
| 738 | |
| 739 | if (hd.version >= 3) |
| 740 | { |
| 741 | unsigned int checksum = 0; |
| 742 | memcpy(ptr, &checksum, sizeof(unsigned int)); // place holder to be filled by the real check sum later |
| 743 | ptr += sizeof(unsigned int); |
| 744 | } |
| 745 | |
| 746 | vector<int> intVec; |
| 747 | intVec.push_back(hd.nRows); |
| 748 | intVec.push_back(hd.nCols); |
| 749 | if (hd.version >= 4) intVec.push_back(hd.nDepth); |
| 750 | intVec.push_back(hd.numValidPixel); |
| 751 | intVec.push_back(hd.microBlockSize); |
| 752 | intVec.push_back(hd.blobSize); |
| 753 | intVec.push_back((int)hd.dt); |
| 754 | if (hd.version >= 6) intVec.push_back(hd.nBlobsMore); |
| 755 | |
| 756 | len = intVec.size() * sizeof(int); |
| 757 | memcpy(ptr, &intVec[0], len); |
| 758 | ptr += len; |
| 759 | |
| 760 | if (hd.version >= 6) |
| 761 | { |
| 762 | vector<Byte> byteVec; |
| 763 | byteVec.push_back(hd.bPassNoDataValues); |
| 764 | byteVec.push_back(hd.bIsInt); |
| 765 | byteVec.push_back(hd.bReserved3); |
| 766 | byteVec.push_back(hd.bReserved4); |
| 767 | |
| 768 | len = byteVec.size(); |
| 769 | memcpy(ptr, &byteVec[0], len); |
| 770 | ptr += len; |
| 771 | } |
| 772 | |
| 773 | vector<double> dblVec; |
| 774 | dblVec.push_back(hd.maxZError); |
| 775 | dblVec.push_back(hd.zMin); |
| 776 | dblVec.push_back(hd.zMax); |
| 777 | if (hd.version >= 6) dblVec.push_back(hd.noDataVal); |
| 778 | if (hd.version >= 6) dblVec.push_back(hd.noDataValOrig); |
| 779 | |
| 780 | len = dblVec.size() * sizeof(double); |
| 781 | memcpy(ptr, &dblVec[0], len); |
| 782 | ptr += len; |
| 783 | |
| 784 | *ppByte = ptr; |
| 785 | return true; |
| 786 | } |
| 787 | |
| 788 | // -------------------------------------------------------------------------- ; |
| 789 | |
| 790 | bool Lerc2::ReadHeader(const Byte** ppByte, size_t& nBytesRemainingInOut, struct HeaderInfo& hd) |
| 791 | { |
| 792 | if (!ppByte || !*ppByte) |
| 793 | return false; |
| 794 | |
| 795 | const Byte* ptr = *ppByte; |
| 796 | size_t nBytesRemaining = nBytesRemainingInOut; |
| 797 | |
| 798 | string fileKey = FileKey(); |
| 799 | size_t keyLen = fileKey.length(); |
| 800 | |
| 801 | hd.RawInit(); |
| 802 | |
| 803 | if (nBytesRemaining < keyLen || memcmp(ptr, fileKey.c_str(), keyLen)) |
| 804 | return false; |
| 805 | |
| 806 | ptr += keyLen; |
| 807 | nBytesRemaining -= keyLen; |
| 808 | |
| 809 | if (nBytesRemaining < sizeof(int) || !memcpy(&(hd.version), ptr, sizeof(int))) |
| 810 | return false; |
| 811 | |
| 812 | ptr += sizeof(int); |
| 813 | nBytesRemaining -= sizeof(int); |
| 814 | |
| 815 | if (hd.version < 0 || hd.version > CurrentVersion()) // this reader is outdated |
| 816 | return false; |
| 817 | |
| 818 | if (hd.version >= 3) |
| 819 | { |
| 820 | if (nBytesRemaining < sizeof(unsigned int) || !memcpy(&(hd.checksum), ptr, sizeof(unsigned int))) |
| 821 | return false; |
| 822 | |
| 823 | ptr += sizeof(unsigned int); |
| 824 | nBytesRemaining -= sizeof(unsigned int); |
| 825 | } |
| 826 | |
| 827 | int nInts = 6; |
| 828 | nInts += (hd.version >= 4) ? 1 : 0; |
| 829 | nInts += (hd.version >= 6) ? 1 : 0; |
| 830 | |
| 831 | int nBytes = 0; |
| 832 | nBytes += (hd.version >= 6) ? 4 : 0; |
| 833 | |
| 834 | int nDbls = 3; |
| 835 | nDbls += (hd.version >= 6) ? 2 : 0; |
| 836 | |
| 837 | vector<int> intVec(nInts, 0); |
| 838 | vector<Byte> byteVec(nBytes, 0); |
| 839 | vector<double> dblVec(nDbls, 0); |
| 840 | |
| 841 | size_t len = sizeof(int) * intVec.size(); |
| 842 | |
| 843 | if (nBytesRemaining < len || !memcpy(&intVec[0], ptr, len)) |
| 844 | return false; |
| 845 | |
| 846 | ptr += len; |
| 847 | nBytesRemaining -= len; |
| 848 | |
| 849 | if (hd.version >= 6) |
| 850 | { |
| 851 | len = byteVec.size(); |
| 852 | |
| 853 | if (nBytesRemaining < len || !memcpy(&byteVec[0], ptr, len)) |
| 854 | return false; |
| 855 | |
| 856 | ptr += len; |
| 857 | nBytesRemaining -= len; |
| 858 | } |
| 859 | |
| 860 | len = sizeof(double) * dblVec.size(); |
| 861 | |
| 862 | if (nBytesRemaining < len || !memcpy(&dblVec[0], ptr, len)) |
| 863 | return false; |
| 864 | |
| 865 | ptr += len; |
| 866 | nBytesRemaining -= len; |
| 867 | |
| 868 | int i = 0; |
| 869 | hd.nRows = intVec[i++]; |
| 870 | hd.nCols = intVec[i++]; |
| 871 | hd.nDepth = (hd.version >= 4) ? intVec[i++] : 1; |
| 872 | hd.numValidPixel = intVec[i++]; |
| 873 | hd.microBlockSize = intVec[i++]; |
| 874 | hd.blobSize = intVec[i++]; |
| 875 | const int dt = intVec[i++]; |
| 876 | |
| 877 | if (hd.nRows <= 0 || hd.nCols <= 0 || hd.nDepth <= 0 || hd.numValidPixel < 0 |
| 878 | || hd.microBlockSize <= 0 || hd.blobSize <= 0 || dt < DT_Char || dt > DT_Double) |
| 879 | return false; |
| 880 | |
| 881 | hd.dt = static_cast<DataType>(dt); |
| 882 | hd.nBlobsMore = (hd.version >= 6) ? intVec[i++] : 0; |
| 883 | |
| 884 | i = 0; |
| 885 | hd.bPassNoDataValues = (hd.version >= 6) ? byteVec[i++] : 0; |
| 886 | hd.bIsInt = (hd.version >= 6) ? byteVec[i++] : 0; |
| 887 | hd.bReserved3 = (hd.version >= 6) ? byteVec[i++] : 0; |
| 888 | hd.bReserved4 = (hd.version >= 6) ? byteVec[i++] : 0; |
| 889 | |
| 890 | i = 0; |
| 891 | hd.maxZError = dblVec[i++]; |
| 892 | hd.zMin = dblVec[i++]; |
| 893 | hd.zMax = dblVec[i++]; |
| 894 | hd.noDataVal = (hd.version >= 6) ? dblVec[i++] : 0; |
| 895 | hd.noDataValOrig = (hd.version >= 6) ? dblVec[i++] : 0; |
| 896 | |
| 897 | // here we guard against bogus input parameters and dimensions: |
| 898 | // we limit the size of input data per band to 2 GB, so any product [height * width * depth * sizeof(data element)] <= INT_MAX; |
| 899 | // the size of a compressed binary Lerc blob per band is <= 2 GB; |
| 900 | // the total size of a compressed binary Lerc blob over all bands is <= 4 GB (current Lerc API limitation); |
| 901 | // (see the corresponding size and dimension checks in Lerc::CheckDimensions(...)); |
| 902 | |
| 903 | const uint64_t numPixel = (uint64_t)hd.nRows * hd.nCols; |
| 904 | const uint64_t maxint32 = (uint64_t)INT_MAX; |
| 905 | const uint64_t nbpp = GetDataTypeSize(hd.dt); |
| 906 | |
| 907 | if (numPixel > maxint32 || (uint64_t)hd.numValidPixel > numPixel) |
| 908 | return false; |
| 909 | |
| 910 | if (hd.microBlockSize > 32 || nbpp * hd.nDepth > maxint32 || nbpp * hd.nDepth * numPixel > maxint32) |
| 911 | return false; |
| 912 | |
| 913 | *ppByte = ptr; |
| 914 | nBytesRemainingInOut = nBytesRemaining; |
| 915 | |
| 916 | return true; |
| 917 | } |
| 918 | |
| 919 | // -------------------------------------------------------------------------- ; |
| 920 | |
| 921 | bool Lerc2::WriteMask(Byte** ppByte) const |
| 922 | { |
| 923 | if (!ppByte) |
| 924 | return false; |
| 925 | |
| 926 | int numValid = m_headerInfo.numValidPixel; |
| 927 | int numTotal = m_headerInfo.nCols * m_headerInfo.nRows; |
| 928 | |
| 929 | bool needMask = numValid > 0 && numValid < numTotal; |
| 930 | |
| 931 | Byte* ptr = *ppByte; |
| 932 | |
| 933 | if (needMask && m_encodeMask) |
| 934 | { |
| 935 | Byte* pArrRLE; |
| 936 | size_t numBytesRLE; |
| 937 | RLE rle; |
| 938 | if (!rle.compress((const Byte*)m_bitMask.Bits(), m_bitMask.Size(), &pArrRLE, numBytesRLE, false)) |
| 939 | return false; |
| 940 | |
| 941 | int numBytesMask = (int)numBytesRLE; |
| 942 | memcpy(ptr, &numBytesMask, sizeof(int)); // num bytes for compressed mask |
| 943 | ptr += sizeof(int); |
| 944 | memcpy(ptr, pArrRLE, numBytesRLE); |
| 945 | ptr += numBytesRLE; |
| 946 | |
| 947 | delete[] pArrRLE; |
| 948 | } |
| 949 | else |
| 950 | { |
| 951 | memset(ptr, 0, sizeof(int)); // indicates no mask stored |
| 952 | ptr += sizeof(int); |
| 953 | } |
| 954 | |
| 955 | *ppByte = ptr; |
| 956 | return true; |
| 957 | } |
| 958 | |
| 959 | // -------------------------------------------------------------------------- ; |
| 960 | |
| 961 | bool Lerc2::ReadMask(const Byte** ppByte, size_t& nBytesRemainingInOut) |
| 962 | { |
| 963 | if (!ppByte) |
| 964 | return false; |
| 965 | |
| 966 | int numValid = m_headerInfo.numValidPixel; |
| 967 | int w = m_headerInfo.nCols; |
| 968 | int h = m_headerInfo.nRows; |
| 969 | |
| 970 | const Byte* ptr = *ppByte; |
| 971 | size_t nBytesRemaining = nBytesRemainingInOut; |
| 972 | |
| 973 | int numBytesMask; |
| 974 | if (nBytesRemaining < sizeof(int) || !memcpy(&numBytesMask, ptr, sizeof(int)) || numBytesMask < 0) |
| 975 | return false; |
| 976 | |
| 977 | ptr += sizeof(int); |
| 978 | nBytesRemaining -= sizeof(int); |
| 979 | |
| 980 | if ((numValid == 0 || numValid == w * h) && (numBytesMask != 0)) |
| 981 | return false; |
| 982 | |
| 983 | if (!m_bitMask.SetSize(w, h)) |
| 984 | return false; |
| 985 | |
| 986 | if (numValid == 0) |
| 987 | m_bitMask.SetAllInvalid(); |
| 988 | else if (numValid == w * h) |
| 989 | m_bitMask.SetAllValid(); |
| 990 | else if (numBytesMask > 0) // read it in |
| 991 | { |
| 992 | if (nBytesRemaining < static_cast<size_t>(numBytesMask)) |
| 993 | return false; |
| 994 | |
| 995 | RLE rle; |
| 996 | if (!rle.decompress(ptr, nBytesRemaining, m_bitMask.Bits(), m_bitMask.Size())) |
| 997 | return false; |
| 998 | |
| 999 | ptr += numBytesMask; |
| 1000 | nBytesRemaining -= numBytesMask; |
| 1001 | } |
| 1002 | // else use previous mask |
| 1003 | |
| 1004 | *ppByte = ptr; |
| 1005 | nBytesRemainingInOut = nBytesRemaining; |
| 1006 | |
| 1007 | return true; |
| 1008 | } |
| 1009 | |
| 1010 | // -------------------------------------------------------------------------- ; |
| 1011 | |
| 1012 | bool Lerc2::DoChecksOnEncode(Byte* pBlobBegin, Byte* pBlobEnd) const |
| 1013 | { |
| 1014 | if ((size_t)(pBlobEnd - pBlobBegin) != (size_t)m_headerInfo.blobSize) |
| 1015 | return false; |
| 1016 | |
| 1017 | if (m_headerInfo.version >= 3) |
| 1018 | { |
| 1019 | int blobSize = (int)(pBlobEnd - pBlobBegin); |
| 1020 | int nBytes = (int)(FileKey().length() + sizeof(int) + sizeof(unsigned int)); // start right after the checksum entry |
| 1021 | if (blobSize < nBytes) |
| 1022 | return false; |
| 1023 | unsigned int checksum = ComputeChecksumFletcher32(pBlobBegin + nBytes, blobSize - nBytes); |
| 1024 | |
| 1025 | nBytes -= sizeof(unsigned int); |
| 1026 | memcpy(pBlobBegin + nBytes, &checksum, sizeof(unsigned int)); |
| 1027 | } |
| 1028 | |
| 1029 | return true; |
| 1030 | } |
| 1031 | |
| 1032 | // -------------------------------------------------------------------------- ; |
| 1033 | |
| 1034 | // from https://en.wikipedia.org/wiki/Fletcher's_checksum |
| 1035 | // modified from ushorts to bytes (by Lucian Plesea) |
| 1036 | |
| 1037 | unsigned int Lerc2::ComputeChecksumFletcher32(const Byte* pByte, int len) |
| 1038 | { |
| 1039 | unsigned int sum1 = 0xffff, sum2 = 0xffff; |
| 1040 | unsigned int words = len / 2; |
| 1041 | |
| 1042 | while (words) |
| 1043 | { |
| 1044 | unsigned int tlen = (words >= 359) ? 359 : words; |
| 1045 | words -= tlen; |
| 1046 | do { |
| 1047 | sum1 += (*pByte++ << 8); |
| 1048 | sum2 += sum1 += *pByte++; |
| 1049 | } while (--tlen); |
| 1050 | |
| 1051 | sum1 = (sum1 & 0xffff) + (sum1 >> 16); |
| 1052 | sum2 = (sum2 & 0xffff) + (sum2 >> 16); |
| 1053 | } |
| 1054 | |
| 1055 | // add the straggler byte if it exists |
| 1056 | if (len & 1) |
| 1057 | sum2 += sum1 += (*pByte << 8); |
| 1058 | |
| 1059 | // second reduction step to reduce sums to 16 bits |
| 1060 | sum1 = (sum1 & 0xffff) + (sum1 >> 16); |
| 1061 | sum2 = (sum2 & 0xffff) + (sum2 >> 16); |
| 1062 | |
| 1063 | return sum2 << 16 | sum1; |
| 1064 | } |
| 1065 | |
| 1066 | // -------------------------------------------------------------------------- ; |
| 1067 | |
| 1068 | // for the theory and math, see |
| 1069 | // https://pdfs.semanticscholar.org/d064/2e2ad1a4c3b445b0d795770f604a5d9e269c.pdf |
| 1070 | |
| 1071 | template<class T> |
| 1072 | bool Lerc2::TryBitPlaneCompression(const T* data, double eps, double& newMaxZError) const |
| 1073 | { |
| 1074 | newMaxZError = 0; // lossless is the obvious fallback |
| 1075 | |
| 1076 | if (!data || eps <= 0) |
| 1077 | return false; |
| 1078 | |
| 1079 | const HeaderInfo& hd = m_headerInfo; |
| 1080 | const int nDepth = hd.nDepth; |
| 1081 | const int maxShift = 8 * GetDataTypeSize(hd.dt); |
| 1082 | const int minCnt = 5000; |
| 1083 | |
| 1084 | if (hd.numValidPixel < minCnt) // not enough data for good stats |
| 1085 | return false; |
| 1086 | |
| 1087 | std::vector<int> cntDiffVec((size_t)nDepth * maxShift, 0); |
| 1088 | int cnt = 0; |
| 1089 | |
| 1090 | if (nDepth == 1 && hd.numValidPixel == hd.nCols * hd.nRows) // special but common case |
| 1091 | { |
| 1092 | if (hd.dt == DT_Byte || hd.dt == DT_UShort || hd.dt == DT_UInt) // unsigned int |
| 1093 | { |
| 1094 | for (int i = 0; i < hd.nRows - 1; i++) |
| 1095 | for (int k = i * hd.nCols, j = 0; j < hd.nCols - 1; j++, k++) |
| 1096 | { |
| 1097 | unsigned int c = ((unsigned int)data[k]) ^ ((unsigned int)data[k + 1]); |
| 1098 | AddUIntToCounts(&cntDiffVec[0], c, maxShift); |
| 1099 | cnt++; |
| 1100 | c = ((unsigned int)data[k]) ^ ((unsigned int)data[k + hd.nCols]); |
| 1101 | AddUIntToCounts(&cntDiffVec[0], c, maxShift); |
| 1102 | cnt++; |
| 1103 | } |
| 1104 | } |
| 1105 | else if (hd.dt == DT_Char || hd.dt == DT_Short || hd.dt == DT_Int) // signed int |
| 1106 | { |
| 1107 | for (int i = 0; i < hd.nRows - 1; i++) |
| 1108 | for (int k = i * hd.nCols, j = 0; j < hd.nCols - 1; j++, k++) |
| 1109 | { |
| 1110 | int c = ((int)data[k]) ^ ((int)data[k + 1]); |
| 1111 | AddIntToCounts(&cntDiffVec[0], c, maxShift); |
| 1112 | cnt++; |
| 1113 | c = ((int)data[k]) ^ ((int)data[k + hd.nCols]); |
| 1114 | AddIntToCounts(&cntDiffVec[0], c, maxShift); |
| 1115 | cnt++; |
| 1116 | } |
| 1117 | } |
| 1118 | else |
| 1119 | return false; // unsupported data type |
| 1120 | } |
| 1121 | |
| 1122 | else // general case: nDepth > 1 or not all pixel valid |
| 1123 | { |
| 1124 | if (hd.dt == DT_Byte || hd.dt == DT_UShort || hd.dt == DT_UInt) // unsigned int |
| 1125 | { |
| 1126 | for (int k = 0, m0 = 0, i = 0; i < hd.nRows; i++) |
| 1127 | for (int j = 0; j < hd.nCols; j++, k++, m0 += nDepth) |
| 1128 | if (m_bitMask.IsValid(k)) |
| 1129 | { |
| 1130 | if (j < hd.nCols - 1 && m_bitMask.IsValid(k + 1)) // hori |
| 1131 | { |
| 1132 | for (int s0 = 0, m = 0; m < nDepth; m++, s0 += maxShift) |
| 1133 | { |
| 1134 | unsigned int c = ((unsigned int)data[m0 + m]) ^ ((unsigned int)data[m0 + m + nDepth]); |
| 1135 | AddUIntToCounts(&cntDiffVec[s0], c, maxShift); |
| 1136 | } |
| 1137 | cnt++; |
| 1138 | } |
| 1139 | if (i < hd.nRows - 1 && m_bitMask.IsValid(k + hd.nCols)) // vert |
| 1140 | { |
| 1141 | for (int s0 = 0, m = 0; m < nDepth; m++, s0 += maxShift) |
| 1142 | { |
| 1143 | unsigned int c = ((unsigned int)data[m0 + m]) ^ ((unsigned int)data[m0 + m + nDepth * hd.nCols]); |
| 1144 | AddUIntToCounts(&cntDiffVec[s0], c, maxShift); |
| 1145 | } |
| 1146 | cnt++; |
| 1147 | } |
| 1148 | } |
| 1149 | } |
| 1150 | else if (hd.dt == DT_Char || hd.dt == DT_Short || hd.dt == DT_Int) // signed int |
| 1151 | { |
| 1152 | for (int k = 0, m0 = 0, i = 0; i < hd.nRows; i++) |
| 1153 | for (int j = 0; j < hd.nCols; j++, k++, m0 += nDepth) |
| 1154 | if (m_bitMask.IsValid(k)) |
| 1155 | { |
| 1156 | if (j < hd.nCols - 1 && m_bitMask.IsValid(k + 1)) // hori |
| 1157 | { |
| 1158 | for (int s0 = 0, m = 0; m < nDepth; m++, s0 += maxShift) |
| 1159 | { |
| 1160 | int c = ((int)data[m0 + m]) ^ ((int)data[m0 + m + nDepth]); |
| 1161 | AddIntToCounts(&cntDiffVec[s0], c, maxShift); |
| 1162 | } |
| 1163 | cnt++; |
| 1164 | } |
| 1165 | if (i < hd.nRows - 1 && m_bitMask.IsValid(k + hd.nCols)) // vert |
| 1166 | { |
| 1167 | for (int s0 = 0, m = 0; m < nDepth; m++, s0 += maxShift) |
| 1168 | { |
| 1169 | int c = ((int)data[m0 + m]) ^ ((int)data[m0 + m + nDepth * hd.nCols]); |
| 1170 | AddIntToCounts(&cntDiffVec[s0], c, maxShift); |
| 1171 | } |
| 1172 | cnt++; |
| 1173 | } |
| 1174 | } |
| 1175 | } |
| 1176 | else |
| 1177 | return false; // unsupported data type |
| 1178 | } |
| 1179 | |
| 1180 | if (cnt < minCnt) // not enough data for good stats |
| 1181 | return false; |
| 1182 | |
| 1183 | int nCutFound = 0, lastPlaneKept = 0; |
| 1184 | //const bool printAll = false; |
| 1185 | |
| 1186 | for (int s = maxShift - 1; s >= 0; s--) |
| 1187 | { |
| 1188 | //if (printAll) printf("bit plane %2d: ", s); |
| 1189 | bool bCrit = true; |
| 1190 | |
| 1191 | for (int iDepth = 0; iDepth < nDepth; iDepth++) |
| 1192 | { |
| 1193 | double x = cntDiffVec[iDepth * maxShift + s]; |
| 1194 | double n = cnt; |
| 1195 | double m = x / n; |
| 1196 | //double stdDev = sqrt(x * x / n - m * m) / n; |
| 1197 | |
| 1198 | //printf(" %.4f +- %.4f ", (float)(2 * m), (float)(2 * stdDev)); |
| 1199 | //if (printAll) printf(" %.4f ", (float)(2 * m)); |
| 1200 | |
| 1201 | if (fabs(1 - 2 * m) >= eps) |
| 1202 | bCrit = false; |
| 1203 | } |
| 1204 | //if (printAll) printf("\n"); |
| 1205 | |
| 1206 | if (bCrit && nCutFound < 2) |
| 1207 | { |
| 1208 | if (nCutFound == 0) |
| 1209 | lastPlaneKept = s; |
| 1210 | |
| 1211 | if (nCutFound == 1 && s < lastPlaneKept - 1) |
| 1212 | { |
| 1213 | lastPlaneKept = s; |
| 1214 | nCutFound = 0; |
| 1215 | //if (printAll) printf(" reset "); |
| 1216 | } |
| 1217 | |
| 1218 | nCutFound++; |
| 1219 | //if (printAll && nCutFound == 1) printf("\n"); |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | lastPlaneKept = std::max(0, lastPlaneKept); |
| 1224 | //if (printAll) printf("%d \n", lastPlaneKept); |
| 1225 | |
| 1226 | newMaxZError = (1 << lastPlaneKept) >> 1; // turn lastPlaneKept into new maxZError |
| 1227 | |
| 1228 | return true; |
| 1229 | } |
| 1230 | |
| 1231 | // -------------------------------------------------------------------------- ; |
| 1232 | |
| 1233 | template<class T> |
| 1234 | bool Lerc2::TryRaiseMaxZError(const T* data, double& maxZError) const |
| 1235 | { |
| 1236 | if (!data || m_headerInfo.dt < DT_Float || m_headerInfo.numValidPixel == 0) |
| 1237 | return false; |
| 1238 | |
| 1239 | const HeaderInfo& hd = m_headerInfo; |
| 1240 | const int nDepth = hd.nDepth; |
| 1241 | |
| 1242 | std::vector<double> roundErr, zErr, zErrCand = { 1, 0.5, 0.1, 0.05, 0.01, 0.005, 0.001, 0.0005, 0.0001 }; |
| 1243 | std::vector<int> zFac, zFacCand = { 1, 2, 10, 20, 100, 200, 1000, 2000, 10000 }; |
| 1244 | |
| 1245 | for (size_t i = 0; i < zErrCand.size(); i++) |
| 1246 | if (zErrCand[i] / 2 > maxZError) |
| 1247 | { |
| 1248 | zErr.push_back(zErrCand[i] / 2); |
| 1249 | zFac.push_back(zFacCand[i]); |
| 1250 | roundErr.push_back(0); |
| 1251 | } |
| 1252 | |
| 1253 | if (zErr.empty()) |
| 1254 | return false; |
| 1255 | |
| 1256 | if (nDepth == 1 && hd.numValidPixel == hd.nCols * hd.nRows) // special but common case |
| 1257 | { |
| 1258 | for (int i = 0; i < hd.nRows; i++) |
| 1259 | { |
| 1260 | size_t nCand = zErr.size(); |
| 1261 | |
| 1262 | for (int k = i * hd.nCols, j = 0; j < hd.nCols; j++, k++) |
| 1263 | { |
| 1264 | double x = data[k]; |
| 1265 | |
| 1266 | for (size_t n = 0; n < nCand; n++) |
| 1267 | { |
| 1268 | double z = x * zFac[n]; |
| 1269 | if (z == (int)z) |
| 1270 | break; |
| 1271 | |
| 1272 | double delta = fabs(floor(z + 0.5) - z); |
| 1273 | roundErr[n] = std::max(roundErr[n], delta); |
| 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | if (!PruneCandidates(roundErr, zErr, zFac, maxZError)) |
| 1278 | return false; |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | else // general case: nDepth > 1 or not all pixel valid |
| 1283 | { |
| 1284 | for (int k = 0, m0 = 0, i = 0; i < hd.nRows; i++) |
| 1285 | { |
| 1286 | size_t nCand = zErr.size(); |
| 1287 | |
| 1288 | for (int j = 0; j < hd.nCols; j++, k++, m0 += nDepth) |
| 1289 | if (m_bitMask.IsValid(k)) |
| 1290 | for (int m = 0; m < nDepth; m++) |
| 1291 | { |
| 1292 | double x = data[m0 + m]; |
| 1293 | |
| 1294 | for (size_t n = 0; n < nCand; n++) |
| 1295 | { |
| 1296 | double z = x * zFac[n]; |
| 1297 | if (z == (int)z) |
| 1298 | break; |
| 1299 | |
| 1300 | double delta = fabs(floor(z + 0.5) - z); |
| 1301 | roundErr[n] = std::max(roundErr[n], delta); |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | if (!PruneCandidates(roundErr, zErr, zFac, maxZError)) |
| 1306 | return false; |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | for (size_t n = 0; n < zErr.size(); n++) |
| 1311 | if (roundErr[n] / zFac[n] <= maxZError / 2) |
| 1312 | { |
| 1313 | maxZError = zErr[n]; |
| 1314 | return true; |
| 1315 | } |
| 1316 | |
| 1317 | return false; |
| 1318 | } |
| 1319 | |
| 1320 | // -------------------------------------------------------------------------- ; |
| 1321 | |
| 1322 | bool Lerc2::PruneCandidates(std::vector<double>& roundErr, std::vector<double>& zErr, |
| 1323 | std::vector<int>& zFac, double maxZError) |
| 1324 | { |
| 1325 | size_t nCand = zErr.size(); |
| 1326 | |
| 1327 | if (nCand == 0 || roundErr.size() != nCand || zFac.size() != nCand || maxZError <= 0) |
| 1328 | return false; |
| 1329 | |
| 1330 | for (int n = (int)(nCand - 1); n >= 0; n--) |
| 1331 | if (roundErr[n] / zFac[n] > maxZError / 2) |
| 1332 | { |
| 1333 | roundErr.erase(roundErr.begin() + n); |
| 1334 | zErr.erase(zErr.begin() + n); |
| 1335 | zFac.erase(zFac.begin() + n); |
| 1336 | } |
| 1337 | |
| 1338 | return (!zErr.empty()); |
| 1339 | } |
| 1340 | |
| 1341 | // -------------------------------------------------------------------------- ; |
| 1342 | |
| 1343 | template<class T> |
| 1344 | bool Lerc2::WriteDataOneSweep(const T* data, Byte** ppByte) const |
| 1345 | { |
| 1346 | if (!data || !ppByte) |
| 1347 | return false; |
| 1348 | |
| 1349 | Byte* ptr = (*ppByte); |
| 1350 | const HeaderInfo& hd = m_headerInfo; |
| 1351 | int nDepth = hd.nDepth; |
| 1352 | int len = nDepth * sizeof(T); |
| 1353 | |
| 1354 | for (int k = 0, m0 = 0, i = 0; i < hd.nRows; i++) |
| 1355 | for (int j = 0; j < hd.nCols; j++, k++, m0 += nDepth) |
| 1356 | if (m_bitMask.IsValid(k)) |
| 1357 | { |
| 1358 | memcpy(ptr, &data[m0], len); |
| 1359 | ptr += len; |
| 1360 | } |
| 1361 | |
| 1362 | (*ppByte) = ptr; |
| 1363 | return true; |
| 1364 | } |
| 1365 | |
| 1366 | // -------------------------------------------------------------------------- ; |
| 1367 | |
| 1368 | template<class T> |
| 1369 | bool Lerc2::ReadDataOneSweep(const Byte** ppByte, size_t& nBytesRemaining, T* data) const |
| 1370 | { |
| 1371 | if (!data || !ppByte || !(*ppByte)) |
| 1372 | return false; |
| 1373 | |
| 1374 | const Byte* ptr = (*ppByte); |
| 1375 | const HeaderInfo& hd = m_headerInfo; |
| 1376 | int nDepth = hd.nDepth; |
| 1377 | size_t len = sizeof(T) * nDepth; |
| 1378 | |
| 1379 | int64_t numValid = m_bitMask.CountValidBits(); |
| 1380 | if (numValid < 0 || numValid >(int64_t)INT_MAX) |
| 1381 | return false; |
| 1382 | |
| 1383 | size_t nValidPix = (size_t)numValid; |
| 1384 | |
| 1385 | if (nBytesRemaining < nValidPix * len) |
| 1386 | return false; |
| 1387 | |
| 1388 | for (int k = 0, m0 = 0, i = 0; i < hd.nRows; i++) |
| 1389 | for (int j = 0; j < hd.nCols; j++, k++, m0 += nDepth) |
| 1390 | if (m_bitMask.IsValid(k)) |
| 1391 | { |
| 1392 | memcpy(&data[m0], ptr, len); |
| 1393 | ptr += len; |
| 1394 | } |
| 1395 | |
| 1396 | (*ppByte) = ptr; |
| 1397 | nBytesRemaining -= nValidPix * len; |
| 1398 | |
| 1399 | return true; |
| 1400 | } |
| 1401 | |
| 1402 | // -------------------------------------------------------------------------- ; |
| 1403 | |
| 1404 | template<class T> |
| 1405 | bool Lerc2::ComputeMinMaxRanges(const T* data, std::vector<double>& zMinVecA, std::vector<double>& zMaxVecA) const |
| 1406 | { |
| 1407 | if (!data || m_headerInfo.numValidPixel == 0) |
| 1408 | return false; |
| 1409 | |
| 1410 | const HeaderInfo& hd = m_headerInfo; |
| 1411 | const int nDepth = hd.nDepth; |
| 1412 | bool bInit = false; |
| 1413 | |
| 1414 | zMinVecA.resize(nDepth); |
| 1415 | zMaxVecA.resize(nDepth); |
| 1416 | |
| 1417 | std::vector<T> zMinVec(nDepth, 0), zMaxVec(nDepth, 0); |
| 1418 | |
| 1419 | if (hd.numValidPixel == hd.nRows * hd.nCols) // all valid, no mask |
| 1420 | { |
| 1421 | bInit = true; |
| 1422 | for (int m = 0; m < nDepth; m++) |
| 1423 | zMinVec[m] = zMaxVec[m] = data[m]; |
| 1424 | |
| 1425 | for (int m0 = 0, i = 0; i < hd.nRows; i++) |
| 1426 | for (int j = 0; j < hd.nCols; j++, m0 += nDepth) |
| 1427 | for (int m = 0; m < nDepth; m++) |
| 1428 | { |
| 1429 | T val = data[m0 + m]; |
| 1430 | |
| 1431 | if (val < zMinVec[m]) |
| 1432 | zMinVec[m] = val; |
| 1433 | else if (val > zMaxVec[m]) |
| 1434 | zMaxVec[m] = val; |
| 1435 | } |
| 1436 | } |
| 1437 | else |
| 1438 | { |
| 1439 | for (int k = 0, m0 = 0, i = 0; i < hd.nRows; i++) |
| 1440 | for (int j = 0; j < hd.nCols; j++, k++, m0 += nDepth) |
| 1441 | if (m_bitMask.IsValid(k)) |
| 1442 | { |
| 1443 | if (bInit) |
| 1444 | for (int m = 0; m < nDepth; m++) |
| 1445 | { |
| 1446 | T val = data[m0 + m]; |
| 1447 | |
| 1448 | if (val < zMinVec[m]) |
| 1449 | zMinVec[m] = val; |
| 1450 | else if (val > zMaxVec[m]) |
| 1451 | zMaxVec[m] = val; |
| 1452 | } |
| 1453 | else |
| 1454 | { |
| 1455 | bInit = true; |
| 1456 | for (int m = 0; m < nDepth; m++) |
| 1457 | zMinVec[m] = zMaxVec[m] = data[m0 + m]; |
| 1458 | } |
| 1459 | } |
| 1460 | } |
| 1461 | |
| 1462 | if (bInit) |
| 1463 | for (int m = 0; m < nDepth; m++) |
| 1464 | { |
| 1465 | zMinVecA[m] = zMinVec[m]; |
| 1466 | zMaxVecA[m] = zMaxVec[m]; |
| 1467 | } |
| 1468 | |
| 1469 | return bInit; |
| 1470 | } |
| 1471 | |
| 1472 | // -------------------------------------------------------------------------- ; |
| 1473 | |
| 1474 | template<class T> |
| 1475 | bool Lerc2::WriteTiles(const T* data, Byte** ppByte, int& numBytes) const |
| 1476 | { |
| 1477 | if (!data || !ppByte) |
| 1478 | return false; |
| 1479 | |
| 1480 | numBytes = 0; |
| 1481 | int numBytesLerc = 0; |
| 1482 | |
| 1483 | std::vector<unsigned int> quantVec, quantVecDiff; |
| 1484 | std::vector<std::pair<unsigned int, unsigned int> > sortedQuantVec, sortedQuantVecDiff; |
| 1485 | |
| 1486 | const HeaderInfo& hd = m_headerInfo; |
| 1487 | int mbSize = hd.microBlockSize; |
| 1488 | int nDepth = hd.nDepth; |
| 1489 | |
| 1490 | std::vector<T> dataVec((size_t)mbSize * mbSize, 0); |
| 1491 | T* dataBuf = &dataVec[0]; |
| 1492 | |
| 1493 | const bool bDtInt = (hd.dt < DT_Float); |
| 1494 | const bool bIntLossless = bDtInt && (hd.maxZError == 0.5); |
| 1495 | const bool bTryDiffEnc = (hd.version >= 5) && (nDepth > 1) && bIntLossless; // only for int lossless to avoid error propagation |
| 1496 | |
| 1497 | const bool bCheckForIntOverflow = NeedToCheckForIntOverflow(hd); |
| 1498 | const bool bCheckForFltRndErr = NeedToCheckForFltRndErr(hd); |
| 1499 | |
| 1500 | int mbDiff2 = bTryDiffEnc ? mbSize * mbSize : 0; |
| 1501 | std::vector<int> diffDataVecInt(mbDiff2, 0); // use fixed type (int) for difference of all int types |
| 1502 | std::vector<T> diffDataVecFlt(mbDiff2, 0), prevDataVec(mbDiff2, 0); |
| 1503 | |
| 1504 | int numTilesVert = (hd.nRows + mbSize - 1) / mbSize; |
| 1505 | int numTilesHori = (hd.nCols + mbSize - 1) / mbSize; |
| 1506 | |
| 1507 | for (int iTile = 0; iTile < numTilesVert; iTile++) |
| 1508 | { |
| 1509 | int tileH = mbSize; |
| 1510 | int i0 = iTile * tileH; |
| 1511 | if (iTile == numTilesVert - 1) |
| 1512 | tileH = hd.nRows - i0; |
| 1513 | |
| 1514 | for (int jTile = 0; jTile < numTilesHori; jTile++) |
| 1515 | { |
| 1516 | int tileW = mbSize; |
| 1517 | int j0 = jTile * tileW; |
| 1518 | if (jTile == numTilesHori - 1) |
| 1519 | tileW = hd.nCols - j0; |
| 1520 | |
| 1521 | for (int iDepth = 0; iDepth < nDepth; iDepth++) |
| 1522 | { |
| 1523 | T zMin = 0, zMax = 0; |
| 1524 | int numValidPixel = 0; |
| 1525 | bool bQuantizeDone = false; |
| 1526 | bool tryLut = false; |
| 1527 | |
| 1528 | if (!GetValidDataAndStats(data, i0, i0 + tileH, j0, j0 + tileW, iDepth, dataBuf, zMin, zMax, numValidPixel, tryLut)) |
| 1529 | return false; |
| 1530 | |
| 1531 | if (numValidPixel == 0 && !(*ppByte)) |
| 1532 | { |
| 1533 | numBytesLerc += nDepth; // 1 byte per empty block |
| 1534 | break; // iDepth loop |
| 1535 | } |
| 1536 | |
| 1537 | //tryLut = false; // always OFF |
| 1538 | //tryLut = NeedToQuantize(numValidPixel, zMin, zMax); // always ON |
| 1539 | |
| 1540 | // if needed, quantize the data here once |
| 1541 | if (((*ppByte && iDepth == 0) || tryLut) && NeedToQuantize(numValidPixel, zMin, zMax)) |
| 1542 | { |
| 1543 | Quantize(dataBuf, numValidPixel, zMin, quantVec); |
| 1544 | bQuantizeDone = true; |
| 1545 | if (tryLut) |
| 1546 | SortQuantArray(quantVec, sortedQuantVec); |
| 1547 | } |
| 1548 | |
| 1549 | BlockEncodeMode blockEncodeMode(BEM_RawBinary), blockEncodeModeDiff(BEM_RawBinary); |
| 1550 | int numBytesNeeded = NumBytesTile(numValidPixel, zMin, zMax, hd.dt, tryLut, blockEncodeMode, sortedQuantVec); |
| 1551 | |
| 1552 | int numBytesNeededDiff = numBytesNeeded + 1; |
| 1553 | int zMinDiffInt(0), zMaxDiffInt(0); |
| 1554 | T zMinDiffFlt(0), zMaxDiffFlt(0); |
| 1555 | double zMinDiff(0), zMaxDiff(0); |
| 1556 | bool bQuantizeDoneDiff = false, tryLutDiff = false; |
| 1557 | |
| 1558 | if (bTryDiffEnc && iDepth > 0 && numValidPixel > 0) |
| 1559 | { |
| 1560 | bool rv = bDtInt ? ComputeDiffSliceInt(&dataVec[0], &prevDataVec[0], numValidPixel, bCheckForIntOverflow, hd.maxZError, diffDataVecInt, zMinDiffInt, zMaxDiffInt, tryLutDiff) |
| 1561 | : ComputeDiffSliceFlt(&dataVec[0], &prevDataVec[0], numValidPixel, bCheckForFltRndErr, hd.maxZError, diffDataVecFlt, zMinDiffFlt, zMaxDiffFlt, tryLutDiff); |
| 1562 | |
| 1563 | zMinDiff = bDtInt ? zMinDiffInt : zMinDiffFlt; |
| 1564 | zMaxDiff = bDtInt ? zMaxDiffInt : zMaxDiffFlt; |
| 1565 | |
| 1566 | if (rv) // can be false due to int overflow, then fall back to abs / regular encode |
| 1567 | { |
| 1568 | // tryLutDiff = false; // for faster encode? |
| 1569 | |
| 1570 | if (tryLutDiff && NeedToQuantize(numValidPixel, zMinDiff, zMaxDiff)) |
| 1571 | { |
| 1572 | bDtInt ? Quantize(&diffDataVecInt[0], numValidPixel, zMinDiffInt, quantVecDiff) |
| 1573 | : Quantize(&diffDataVecFlt[0], numValidPixel, zMinDiffFlt, quantVecDiff); |
| 1574 | bQuantizeDoneDiff = true; |
| 1575 | SortQuantArray(quantVecDiff, sortedQuantVecDiff); |
| 1576 | } |
| 1577 | |
| 1578 | int nb = bDtInt ? NumBytesTile(numValidPixel, zMinDiffInt, zMaxDiffInt, DT_Int, tryLutDiff, blockEncodeModeDiff, sortedQuantVecDiff) |
| 1579 | : NumBytesTile(numValidPixel, zMinDiffFlt, zMaxDiffFlt, hd.dt, tryLutDiff, blockEncodeModeDiff, sortedQuantVecDiff); |
| 1580 | if (nb > 0) |
| 1581 | numBytesNeededDiff = nb; |
| 1582 | } |
| 1583 | } |
| 1584 | |
| 1585 | numBytesLerc += std::min(numBytesNeeded, numBytesNeededDiff); |
| 1586 | |
| 1587 | if (bTryDiffEnc && iDepth < (nDepth - 1) && numValidPixel > 0) |
| 1588 | { |
| 1589 | if (iDepth == 0) |
| 1590 | prevDataVec.resize(numValidPixel); |
| 1591 | |
| 1592 | if (!bIntLossless) // if not int lossless: do lossy quantize back and forth, then copy to buffer |
| 1593 | { |
| 1594 | double zMaxClamp = m_zMaxVec[iDepth]; |
| 1595 | bool bClampScaleBack = (zMax + 2 * hd.maxZError > zMaxClamp); |
| 1596 | |
| 1597 | if (iDepth == 0 || numBytesNeeded <= numBytesNeededDiff) // for regular or abs encode |
| 1598 | { |
| 1599 | if (bQuantizeDone || NeedToQuantize(numValidPixel, zMin, zMax)) |
| 1600 | { |
| 1601 | if (!bQuantizeDone) |
| 1602 | Quantize(dataBuf, numValidPixel, zMin, quantVec); |
| 1603 | |
| 1604 | bQuantizeDone = true; |
| 1605 | ScaleBack(&prevDataVec[0], quantVec, zMin, false, bClampScaleBack, zMaxClamp, hd.maxZError); |
| 1606 | } |
| 1607 | else if (zMin == zMax || ((hd.maxZError > 0) && (0 == (unsigned int)(ComputeMaxVal(zMin, zMax, hd.maxZError) + 0.5)))) // const block case |
| 1608 | prevDataVec.assign(numValidPixel, zMin); |
| 1609 | else |
| 1610 | copy(dataVec.begin(), dataVec.begin() + numValidPixel, prevDataVec.begin()); |
| 1611 | } |
| 1612 | else // for diff or relative encode |
| 1613 | { |
| 1614 | if (bQuantizeDoneDiff || NeedToQuantize(numValidPixel, zMinDiff, zMaxDiff)) |
| 1615 | { |
| 1616 | if (!bQuantizeDoneDiff) |
| 1617 | bDtInt ? Quantize(&diffDataVecInt[0], numValidPixel, zMinDiffInt, quantVecDiff) |
| 1618 | : Quantize(&diffDataVecFlt[0], numValidPixel, zMinDiffFlt, quantVecDiff); |
| 1619 | |
| 1620 | bQuantizeDoneDiff = true; |
| 1621 | ScaleBack(&prevDataVec[0], quantVecDiff, zMinDiff, true, bClampScaleBack, zMaxClamp, hd.maxZError); |
| 1622 | } |
| 1623 | else if (zMinDiff == zMaxDiff || ((hd.maxZError > 0) && (0 == (unsigned int)(ComputeMaxVal(zMinDiff, zMaxDiff, hd.maxZError) + 0.5)))) // const block case |
| 1624 | { |
| 1625 | ScaleBackConstBlock(&prevDataVec[0], numValidPixel, zMinDiff, bClampScaleBack, zMaxClamp); |
| 1626 | } |
| 1627 | else |
| 1628 | copy(dataVec.begin(), dataVec.begin() + numValidPixel, prevDataVec.begin()); |
| 1629 | } |
| 1630 | } |
| 1631 | else |
| 1632 | copy(dataVec.begin(), dataVec.begin() + numValidPixel, prevDataVec.begin()); |
| 1633 | } |
| 1634 | |
| 1635 | if (*ppByte) |
| 1636 | { |
| 1637 | int numBytesWritten = 0; |
| 1638 | bool rv = false; |
| 1639 | |
| 1640 | if (iDepth == 0 || numBytesNeeded <= numBytesNeededDiff) |
| 1641 | { |
| 1642 | if (!bQuantizeDone && NeedToQuantize(numValidPixel, zMin, zMax)) |
| 1643 | Quantize(dataBuf, numValidPixel, zMin, quantVec); |
| 1644 | |
| 1645 | rv = WriteTile(dataBuf, numValidPixel, ppByte, numBytesWritten, j0, zMin, zMax, hd.dt, false, quantVec, blockEncodeMode, sortedQuantVec); |
| 1646 | } |
| 1647 | else |
| 1648 | { |
| 1649 | if (!bQuantizeDoneDiff && NeedToQuantize(numValidPixel, zMinDiff, zMaxDiff)) |
| 1650 | { |
| 1651 | bDtInt ? Quantize(&diffDataVecInt[0], numValidPixel, zMinDiffInt, quantVecDiff) |
| 1652 | : Quantize(&diffDataVecFlt[0], numValidPixel, zMinDiffFlt, quantVecDiff); |
| 1653 | } |
| 1654 | rv = bDtInt |
| 1655 | ? WriteTile(&diffDataVecInt[0], numValidPixel, ppByte, numBytesWritten, j0, zMinDiffInt, zMaxDiffInt, DT_Int, true, quantVecDiff, blockEncodeModeDiff, sortedQuantVecDiff) |
| 1656 | : WriteTile(&diffDataVecFlt[0], numValidPixel, ppByte, numBytesWritten, j0, zMinDiffFlt, zMaxDiffFlt, hd.dt, true, quantVecDiff, blockEncodeModeDiff, sortedQuantVecDiff); |
| 1657 | } |
| 1658 | |
| 1659 | if (!rv || numBytesWritten != std::min(numBytesNeeded, numBytesNeededDiff)) |
| 1660 | return false; |
| 1661 | } |
| 1662 | } |
| 1663 | } |
| 1664 | } |
| 1665 | |
| 1666 | numBytes += numBytesLerc; |
| 1667 | return true; |
| 1668 | } |
| 1669 | |
| 1670 | // -------------------------------------------------------------------------- ; |
| 1671 | |
| 1672 | template<class T> |
| 1673 | bool Lerc2::ReadTiles(const Byte** ppByte, size_t& nBytesRemaining, T* data) const |
| 1674 | { |
| 1675 | if (!data || !ppByte || !(*ppByte)) |
| 1676 | return false; |
| 1677 | |
| 1678 | std::vector<unsigned int> bufferVec; |
| 1679 | |
| 1680 | const HeaderInfo& hd = m_headerInfo; |
| 1681 | int mbSize = hd.microBlockSize; |
| 1682 | int nDepth = hd.nDepth; |
| 1683 | |
| 1684 | if (mbSize > 32) |
| 1685 | return false; |
| 1686 | |
| 1687 | int numTilesVert = (hd.nRows + mbSize - 1) / mbSize; |
| 1688 | int numTilesHori = (hd.nCols + mbSize - 1) / mbSize; |
| 1689 | |
| 1690 | for (int iTile = 0; iTile < numTilesVert; iTile++) |
| 1691 | { |
| 1692 | int tileH = mbSize; |
| 1693 | int i0 = iTile * tileH; |
| 1694 | if (iTile == numTilesVert - 1) |
| 1695 | tileH = hd.nRows - i0; |
| 1696 | |
| 1697 | for (int jTile = 0; jTile < numTilesHori; jTile++) |
| 1698 | { |
| 1699 | int tileW = mbSize; |
| 1700 | int j0 = jTile * tileW; |
| 1701 | if (jTile == numTilesHori - 1) |
| 1702 | tileW = hd.nCols - j0; |
| 1703 | |
| 1704 | for (int iDepth = 0; iDepth < nDepth; iDepth++) |
| 1705 | { |
| 1706 | if (!ReadTile(ppByte, nBytesRemaining, data, i0, i0 + tileH, j0, j0 + tileW, iDepth, bufferVec)) |
| 1707 | return false; |
| 1708 | } |
| 1709 | } |
| 1710 | } |
| 1711 | |
| 1712 | return true; |
| 1713 | } |
| 1714 | |
| 1715 | // -------------------------------------------------------------------------- ; |
| 1716 | |
| 1717 | template<class T> |
| 1718 | bool Lerc2::GetValidDataAndStats(const T* data, int i0, int i1, int j0, int j1, int iDepth, |
| 1719 | T* dataBuf, T& zMin, T& zMax, int& numValidPixel, bool& tryLut) const |
| 1720 | { |
| 1721 | const HeaderInfo& hd = m_headerInfo; |
| 1722 | |
| 1723 | if (!data || i0 < 0 || j0 < 0 || i1 > hd.nRows || j1 > hd.nCols || i0 >= i1 || j0 >= j1 || iDepth < 0 || iDepth > hd.nDepth || !dataBuf) |
| 1724 | return false; |
| 1725 | |
| 1726 | zMin = zMax = 0; |
| 1727 | tryLut = false; |
| 1728 | |
| 1729 | T prevVal = 0; |
| 1730 | int cnt = 0, cntSameVal = 0; |
| 1731 | int nDepth = hd.nDepth; |
| 1732 | |
| 1733 | if (hd.numValidPixel == hd.nCols * hd.nRows) // all valid, no mask |
| 1734 | { |
| 1735 | int k0 = i0 * hd.nCols + j0; |
| 1736 | int m0 = k0 * nDepth + iDepth; |
| 1737 | zMin = zMax = data[m0]; // init |
| 1738 | |
| 1739 | for (int i = i0; i < i1; i++) |
| 1740 | { |
| 1741 | int k = i * hd.nCols + j0; |
| 1742 | int m = k * nDepth + iDepth; |
| 1743 | |
| 1744 | for (int j = j0; j < j1; j++, m += nDepth) |
| 1745 | { |
| 1746 | T val = data[m]; |
| 1747 | dataBuf[cnt] = val; |
| 1748 | |
| 1749 | if (val < zMin) |
| 1750 | zMin = val; |
| 1751 | else if (val > zMax) |
| 1752 | zMax = val; |
| 1753 | |
| 1754 | if (val == prevVal) |
| 1755 | cntSameVal++; |
| 1756 | |
| 1757 | prevVal = val; |
| 1758 | cnt++; |
| 1759 | } |
| 1760 | } |
| 1761 | } |
| 1762 | else // not all valid, use mask |
| 1763 | { |
| 1764 | for (int i = i0; i < i1; i++) |
| 1765 | { |
| 1766 | int k = i * hd.nCols + j0; |
| 1767 | int m = k * nDepth + iDepth; |
| 1768 | |
| 1769 | for (int j = j0; j < j1; j++, k++, m += nDepth) |
| 1770 | if (m_bitMask.IsValid(k)) |
| 1771 | { |
| 1772 | T val = data[m]; |
| 1773 | dataBuf[cnt] = val; |
| 1774 | |
| 1775 | if (cnt > 0) |
| 1776 | { |
| 1777 | if (val < zMin) |
| 1778 | zMin = val; |
| 1779 | else if (val > zMax) |
| 1780 | zMax = val; |
| 1781 | |
| 1782 | if (val == prevVal) |
| 1783 | cntSameVal++; |
| 1784 | } |
| 1785 | else |
| 1786 | zMin = zMax = val; // init |
| 1787 | |
| 1788 | prevVal = val; |
| 1789 | cnt++; |
| 1790 | } |
| 1791 | } |
| 1792 | } |
| 1793 | |
| 1794 | if (cnt > 4) |
| 1795 | tryLut = (zMax > zMin + 3 * hd.maxZError) && (2 * cntSameVal > cnt); |
| 1796 | |
| 1797 | numValidPixel = cnt; |
| 1798 | return true; |
| 1799 | } |
| 1800 | |
| 1801 | // -------------------------------------------------------------------------- ; |
| 1802 | |
| 1803 | template<class T> |
| 1804 | bool Lerc2::ComputeDiffSliceInt(const T* data, const T* prevData, int numValidPixel, |
| 1805 | bool bCheckForIntOverflow, double maxZError, |
| 1806 | std::vector<int>& diffDataVec, int& zMin, int& zMax, bool& tryLut) |
| 1807 | { |
| 1808 | if (numValidPixel <= 0) |
| 1809 | return false; |
| 1810 | |
| 1811 | diffDataVec.resize(numValidPixel); |
| 1812 | |
| 1813 | int prevVal(0), cnt(0), cntSameVal(0); |
| 1814 | |
| 1815 | if (!bCheckForIntOverflow) |
| 1816 | { |
| 1817 | zMin = zMax = (int)data[0] - (int)prevData[0]; // init |
| 1818 | |
| 1819 | for (int i = 0; i < numValidPixel; i++) |
| 1820 | { |
| 1821 | int val = (int)data[i] - (int)prevData[i]; |
| 1822 | |
| 1823 | diffDataVec[i] = val; |
| 1824 | |
| 1825 | if (val < zMin) |
| 1826 | zMin = val; |
| 1827 | else if (val > zMax) |
| 1828 | zMax = val; |
| 1829 | |
| 1830 | if (val == prevVal) |
| 1831 | cntSameVal++; |
| 1832 | |
| 1833 | prevVal = val; |
| 1834 | cnt++; |
| 1835 | } |
| 1836 | } |
| 1837 | else |
| 1838 | { |
| 1839 | zMin = zMax = (int)((double)data[0] - (double)prevData[0]); // init |
| 1840 | const double zIntMax = 0x7FFFFFFF; |
| 1841 | const double zIntMin = -zIntMax - 1; |
| 1842 | bool bOverflow = false; |
| 1843 | |
| 1844 | for (int i = 0; i < numValidPixel; i++) |
| 1845 | { |
| 1846 | double z = (double)data[i] - (double)prevData[i]; |
| 1847 | int val = (int)z; |
| 1848 | |
| 1849 | if (z < zIntMin || z > zIntMax) // check for int32 overflow |
| 1850 | bOverflow = true; |
| 1851 | |
| 1852 | diffDataVec[i] = val; |
| 1853 | |
| 1854 | if (val < zMin) |
| 1855 | zMin = val; |
| 1856 | else if (val > zMax) |
| 1857 | zMax = val; |
| 1858 | |
| 1859 | if (val == prevVal) |
| 1860 | cntSameVal++; |
| 1861 | |
| 1862 | prevVal = val; |
| 1863 | cnt++; |
| 1864 | } |
| 1865 | |
| 1866 | if (bOverflow) |
| 1867 | return false; |
| 1868 | } |
| 1869 | |
| 1870 | if (cnt > 4) |
| 1871 | tryLut = (zMax > zMin + 3 * maxZError) && (2 * cntSameVal > cnt); |
| 1872 | |
| 1873 | return true; |
| 1874 | } |
| 1875 | |
| 1876 | // -------------------------------------------------------------------------- ; |
| 1877 | |
| 1878 | template<class T> |
| 1879 | bool Lerc2::ComputeDiffSliceFlt(const T* data, const T* prevData, int numValidPixel, |
| 1880 | bool bCheckForFltRndErr, double maxZError, |
| 1881 | std::vector<T>& diffDataVec, T& zMin, T& zMax, bool& tryLut) |
| 1882 | { |
| 1883 | if (numValidPixel <= 0) |
| 1884 | return false; |
| 1885 | |
| 1886 | diffDataVec.resize(numValidPixel); |
| 1887 | |
| 1888 | zMin = zMax = (T)((double)data[0] - (double)prevData[0]); // init |
| 1889 | T prevVal(0); |
| 1890 | int cnt(0), cntSameVal(0); |
| 1891 | |
| 1892 | if (!bCheckForFltRndErr) |
| 1893 | { |
| 1894 | for (int i = 0; i < numValidPixel; i++) |
| 1895 | { |
| 1896 | T val = (T)((double)data[i] - (double)prevData[i]); |
| 1897 | |
| 1898 | diffDataVec[i] = val; |
| 1899 | |
| 1900 | if (val < zMin) |
| 1901 | zMin = val; |
| 1902 | else if (val > zMax) |
| 1903 | zMax = val; |
| 1904 | |
| 1905 | if (val == prevVal) |
| 1906 | cntSameVal++; |
| 1907 | |
| 1908 | prevVal = val; |
| 1909 | cnt++; |
| 1910 | } |
| 1911 | } |
| 1912 | else |
| 1913 | { |
| 1914 | double maxRoundErr = 0; |
| 1915 | |
| 1916 | for (int i = 0; i < numValidPixel; i++) |
| 1917 | { |
| 1918 | T val = (T)((double)data[i] - (double)prevData[i]); |
| 1919 | |
| 1920 | double testVal = (double)val + (double)prevData[i]; // compute flt pnt rounding error |
| 1921 | maxRoundErr = (std::max)(fabs(testVal - (double)data[i]), maxRoundErr); |
| 1922 | |
| 1923 | diffDataVec[i] = val; |
| 1924 | |
| 1925 | if (val < zMin) |
| 1926 | zMin = val; |
| 1927 | else if (val > zMax) |
| 1928 | zMax = val; |
| 1929 | |
| 1930 | if (val == prevVal) |
| 1931 | cntSameVal++; |
| 1932 | |
| 1933 | prevVal = val; |
| 1934 | cnt++; |
| 1935 | } |
| 1936 | |
| 1937 | if (maxRoundErr > maxZError / 8) |
| 1938 | return false; |
| 1939 | } |
| 1940 | |
| 1941 | if (cnt > 4) |
| 1942 | tryLut = (zMax > zMin + 3 * maxZError) && (2 * cntSameVal > cnt); |
| 1943 | |
| 1944 | return true; |
| 1945 | } |
| 1946 | |
| 1947 | // -------------------------------------------------------------------------- ; |
| 1948 | |
| 1949 | template<class T> |
| 1950 | bool Lerc2::WriteTile(const T* dataBuf, int num, Byte** ppByte, int& numBytesWritten, int j0, T zMin, T zMax, |
| 1951 | DataType dtZ, bool bDiffEnc, const std::vector<unsigned int>& quantVec, BlockEncodeMode blockEncodeMode, |
| 1952 | const std::vector<std::pair<unsigned int, unsigned int>>& sortedQuantVec) const |
| 1953 | { |
| 1954 | Byte* ptr = *ppByte; |
| 1955 | Byte comprFlag = ((j0 >> 3) & 15) << 2; // use bits 2345 for integrity check |
| 1956 | |
| 1957 | if (m_headerInfo.version >= 5) |
| 1958 | comprFlag = bDiffEnc ? (comprFlag | 4) : (comprFlag & (7 << 3)); // bit 2 now encodes diff encoding |
| 1959 | |
| 1960 | if (num == 0 || (zMin == 0 && zMax == 0)) // special cases |
| 1961 | { |
| 1962 | *ptr++ = comprFlag | 2; // set compression flag to 2 to mark tile as constant 0 |
| 1963 | numBytesWritten = 1; |
| 1964 | *ppByte = ptr; |
| 1965 | return true; |
| 1966 | } |
| 1967 | |
| 1968 | if (blockEncodeMode == BEM_RawBinary) |
| 1969 | { |
| 1970 | if (bDiffEnc) |
| 1971 | return false; // doesn't make sense, should not happen |
| 1972 | |
| 1973 | *ptr++ = comprFlag | 0; // write z's binary uncompressed |
| 1974 | |
| 1975 | memcpy(ptr, dataBuf, num * sizeof(T)); |
| 1976 | ptr += num * sizeof(T); |
| 1977 | } |
| 1978 | else |
| 1979 | { |
| 1980 | double maxVal = (m_headerInfo.maxZError > 0) ? ComputeMaxVal(zMin, zMax, m_headerInfo.maxZError) : 0; |
| 1981 | |
| 1982 | // write z's as int arr bit stuffed |
| 1983 | unsigned int maxElem = (unsigned int)(maxVal + 0.5); |
| 1984 | if (maxElem == 0) |
| 1985 | comprFlag |= 3; // set compression flag to 3 to mark tile as constant zMin |
| 1986 | else |
| 1987 | comprFlag |= 1; // use bit stuffing |
| 1988 | |
| 1989 | DataType dtReduced; |
| 1990 | int bits67 = ReduceDataType(zMin, dtZ, dtReduced); |
| 1991 | comprFlag |= bits67 << 6; |
| 1992 | |
| 1993 | *ptr++ = comprFlag; |
| 1994 | |
| 1995 | if (!WriteVariableDataType(&ptr, (double)zMin, dtReduced)) |
| 1996 | return false; |
| 1997 | |
| 1998 | if (maxElem > 0) |
| 1999 | { |
| 2000 | if ((int)quantVec.size() != num) |
| 2001 | return false; |
| 2002 | |
| 2003 | if (blockEncodeMode == BEM_BitStuffSimple) |
| 2004 | { |
| 2005 | if (!m_bitStuffer2.EncodeSimple(&ptr, quantVec, m_headerInfo.version)) |
| 2006 | return false; |
| 2007 | } |
| 2008 | else if (blockEncodeMode == BEM_BitStuffLUT) |
| 2009 | { |
| 2010 | if (!m_bitStuffer2.EncodeLut(&ptr, sortedQuantVec, m_headerInfo.version)) |
| 2011 | return false; |
| 2012 | } |
| 2013 | else |
| 2014 | return false; |
| 2015 | } |
| 2016 | } |
| 2017 | |
| 2018 | numBytesWritten = (int)(ptr - *ppByte); |
| 2019 | *ppByte = ptr; |
| 2020 | return true; |
| 2021 | } |
| 2022 | |
| 2023 | // -------------------------------------------------------------------------- ; |
| 2024 | |
| 2025 | template<class T> |
| 2026 | bool Lerc2::ReadTile(const Byte** ppByte, size_t& nBytesRemainingInOut, T* data, int i0, int i1, int j0, int j1, int iDepth, |
| 2027 | std::vector<unsigned int>& bufferVec) const |
| 2028 | { |
| 2029 | const Byte* ptr = *ppByte; |
| 2030 | size_t nBytesRemaining = nBytesRemainingInOut; |
| 2031 | |
| 2032 | if (nBytesRemaining < 1) |
| 2033 | return false; |
| 2034 | |
| 2035 | const HeaderInfo& hd = m_headerInfo; |
| 2036 | int nCols = hd.nCols; |
| 2037 | int nDepth = hd.nDepth; |
| 2038 | |
| 2039 | Byte comprFlag = *ptr++; |
| 2040 | nBytesRemaining--; |
| 2041 | |
| 2042 | const bool bDiffEnc = (hd.version >= 5) ? (comprFlag & 4) : false; // bit 2 now encodes diff or delta encoding |
| 2043 | const int pattern = (hd.version >= 5) ? 14 : 15; |
| 2044 | |
| 2045 | if (((comprFlag >> 2) & pattern) != ((j0 >> 3) & pattern)) // use bits 2345 for integrity check |
| 2046 | return false; |
| 2047 | |
| 2048 | if (bDiffEnc && iDepth == 0) |
| 2049 | return false; |
| 2050 | |
| 2051 | int bits67 = comprFlag >> 6; |
| 2052 | comprFlag &= 3; |
| 2053 | |
| 2054 | if (comprFlag == 2) // entire tile is constant 0 (all the valid pixels) |
| 2055 | { |
| 2056 | for (int i = i0; i < i1; i++) |
| 2057 | { |
| 2058 | int k = i * nCols + j0; |
| 2059 | int m = k * nDepth + iDepth; |
| 2060 | |
| 2061 | for (int j = j0; j < j1; j++, k++, m += nDepth) |
| 2062 | if (m_bitMask.IsValid(k)) |
| 2063 | data[m] = bDiffEnc ? data[m - 1] : 0; |
| 2064 | } |
| 2065 | |
| 2066 | *ppByte = ptr; |
| 2067 | nBytesRemainingInOut = nBytesRemaining; |
| 2068 | return true; |
| 2069 | } |
| 2070 | |
| 2071 | else if (comprFlag == 0) // read z's binary uncompressed |
| 2072 | { |
| 2073 | if (bDiffEnc) |
| 2074 | return false; // doesn't make sense, should not happen |
| 2075 | |
| 2076 | const T* srcPtr = (const T*)ptr; |
| 2077 | int cnt = 0; |
| 2078 | |
| 2079 | for (int i = i0; i < i1; i++) |
| 2080 | { |
| 2081 | int k = i * nCols + j0; |
| 2082 | int m = k * nDepth + iDepth; |
| 2083 | |
| 2084 | for (int j = j0; j < j1; j++, k++, m += nDepth) |
| 2085 | if (m_bitMask.IsValid(k)) |
| 2086 | { |
| 2087 | if (nBytesRemaining < sizeof(T)) |
| 2088 | return false; |
| 2089 | |
| 2090 | data[m] = *srcPtr++; |
| 2091 | nBytesRemaining -= sizeof(T); |
| 2092 | |
| 2093 | cnt++; |
| 2094 | } |
| 2095 | } |
| 2096 | |
| 2097 | ptr += cnt * sizeof(T); |
| 2098 | } |
| 2099 | else |
| 2100 | { |
| 2101 | // read z's as int arr bit stuffed |
| 2102 | DataType dtUsed = GetDataTypeUsed((bDiffEnc && hd.dt < DT_Float) ? DT_Int : hd.dt, bits67); |
| 2103 | if (dtUsed == DT_Undefined) |
| 2104 | return false; |
| 2105 | size_t n = GetDataTypeSize(dtUsed); |
| 2106 | if (nBytesRemaining < n) |
| 2107 | return false; |
| 2108 | |
| 2109 | double offset = ReadVariableDataType(&ptr, dtUsed); |
| 2110 | nBytesRemaining -= n; |
| 2111 | |
| 2112 | double zMax = (hd.version >= 4 && nDepth > 1) ? m_zMaxVec[iDepth] : hd.zMax; |
| 2113 | |
| 2114 | if (comprFlag == 3) // entire tile is constant zMin (all the valid pixels) |
| 2115 | { |
| 2116 | for (int i = i0; i < i1; i++) |
| 2117 | { |
| 2118 | int k = i * nCols + j0; |
| 2119 | int m = k * nDepth + iDepth; |
| 2120 | |
| 2121 | if (!bDiffEnc) |
| 2122 | { |
| 2123 | T val = (T)offset; |
| 2124 | for (int j = j0; j < j1; j++, k++, m += nDepth) |
| 2125 | if (m_bitMask.IsValid(k)) |
| 2126 | data[m] = val; |
| 2127 | } |
| 2128 | else |
| 2129 | { |
| 2130 | for (int j = j0; j < j1; j++, k++, m += nDepth) |
| 2131 | if (m_bitMask.IsValid(k)) |
| 2132 | { |
| 2133 | double z = offset + data[m - 1]; |
| 2134 | data[m] = (T)std::min(z, zMax); |
| 2135 | } |
| 2136 | } |
| 2137 | } |
| 2138 | } |
| 2139 | else |
| 2140 | { |
| 2141 | size_t maxElementCount = size_t(i1 - i0) * (j1 - j0); |
| 2142 | if (!m_bitStuffer2.Decode(&ptr, nBytesRemaining, bufferVec, maxElementCount, hd.version)) |
| 2143 | return false; |
| 2144 | |
| 2145 | double invScale = 2 * hd.maxZError; // for int types this is int |
| 2146 | const unsigned int* srcPtr = bufferVec.data(); |
| 2147 | |
| 2148 | if (bufferVec.size() == maxElementCount) // all valid |
| 2149 | { |
| 2150 | for (int i = i0; i < i1; i++) |
| 2151 | { |
| 2152 | int k = i * nCols + j0; |
| 2153 | int m = k * nDepth + iDepth; |
| 2154 | |
| 2155 | if (!bDiffEnc) |
| 2156 | { |
| 2157 | for (int j = j0; j < j1; j++, k++, m += nDepth) |
| 2158 | { |
| 2159 | double z = offset + *srcPtr++ * invScale; |
| 2160 | data[m] = (T)std::min(z, zMax); // make sure we stay in the orig range |
| 2161 | } |
| 2162 | } |
| 2163 | else |
| 2164 | { |
| 2165 | for (int j = j0; j < j1; j++, k++, m += nDepth) |
| 2166 | { |
| 2167 | double z = offset + *srcPtr++ * invScale + data[m - 1]; |
| 2168 | data[m] = (T)std::min(z, zMax); |
| 2169 | } |
| 2170 | } |
| 2171 | } |
| 2172 | } |
| 2173 | else // not all valid |
| 2174 | { |
| 2175 | if (hd.version > 2) |
| 2176 | { |
| 2177 | for (int i = i0; i < i1; i++) |
| 2178 | { |
| 2179 | int k = i * nCols + j0; |
| 2180 | int m = k * nDepth + iDepth; |
| 2181 | |
| 2182 | if (!bDiffEnc) |
| 2183 | { |
| 2184 | for (int j = j0; j < j1; j++, k++, m += nDepth) |
| 2185 | if (m_bitMask.IsValid(k)) |
| 2186 | { |
| 2187 | double z = offset + *srcPtr++ * invScale; |
| 2188 | data[m] = (T)std::min(z, zMax); // make sure we stay in the orig range |
| 2189 | } |
| 2190 | } |
| 2191 | else |
| 2192 | { |
| 2193 | for (int j = j0; j < j1; j++, k++, m += nDepth) |
| 2194 | if (m_bitMask.IsValid(k)) |
| 2195 | { |
| 2196 | double z = offset + *srcPtr++ * invScale + data[m - 1]; |
| 2197 | data[m] = (T)std::min(z, zMax); |
| 2198 | } |
| 2199 | } |
| 2200 | } |
| 2201 | } |
| 2202 | else // fail gracefully in case of corrupted blob for old version <= 2 which had no checksum |
| 2203 | { |
| 2204 | size_t bufferVecIdx = 0; |
| 2205 | |
| 2206 | for (int i = i0; i < i1; i++) |
| 2207 | { |
| 2208 | int k = i * nCols + j0; |
| 2209 | int m = k * nDepth + iDepth; |
| 2210 | |
| 2211 | for (int j = j0; j < j1; j++, k++, m += nDepth) |
| 2212 | if (m_bitMask.IsValid(k)) |
| 2213 | { |
| 2214 | if (bufferVecIdx == bufferVec.size()) // fail gracefully in case of corrupted blob for old version <= 2 which had no checksum |
| 2215 | return false; |
| 2216 | |
| 2217 | double z = offset + bufferVec[bufferVecIdx] * invScale; |
| 2218 | bufferVecIdx++; |
| 2219 | data[m] = (T)std::min(z, zMax); // make sure we stay in the orig range |
| 2220 | } |
| 2221 | } |
| 2222 | } |
| 2223 | } |
| 2224 | } |
| 2225 | } |
| 2226 | |
| 2227 | *ppByte = ptr; |
| 2228 | nBytesRemainingInOut = nBytesRemaining; |
| 2229 | return true; |
| 2230 | } |
| 2231 | |
| 2232 | // -------------------------------------------------------------------------- ; |
| 2233 | |
| 2234 | template<class T> |
| 2235 | Lerc2::DataType Lerc2::GetDataType(T z) |
| 2236 | { |
| 2237 | const std::type_info& ti = typeid(z); |
| 2238 | |
| 2239 | if (ti == typeid(signed char)) return DT_Char; |
| 2240 | else if (ti == typeid(Byte)) return DT_Byte; |
| 2241 | else if (ti == typeid(short)) return DT_Short; |
| 2242 | else if (ti == typeid(unsigned short)) return DT_UShort; |
| 2243 | else if (ti == typeid(int) && sizeof(int) == 4) return DT_Int; |
| 2244 | else if (ti == typeid(long) && sizeof(long) == 4) return DT_Int; |
| 2245 | else if (ti == typeid(unsigned int) && sizeof(unsigned int) == 4) return DT_UInt; |
| 2246 | else if (ti == typeid(unsigned long) && sizeof(unsigned long) == 4) return DT_UInt; |
| 2247 | else if (ti == typeid(float)) return DT_Float; |
| 2248 | else if (ti == typeid(double)) return DT_Double; |
| 2249 | else |
| 2250 | return DT_Undefined; |
| 2251 | } |
| 2252 | |
| 2253 | // -------------------------------------------------------------------------- ; |
| 2254 | |
| 2255 | void Lerc2::SortQuantArray(const vector<unsigned int>& quantVec, vector<pair<unsigned int, unsigned int> >& sortedQuantVec) |
| 2256 | { |
| 2257 | int numElem = (int)quantVec.size(); |
| 2258 | sortedQuantVec.resize(numElem); |
| 2259 | |
| 2260 | for (int i = 0; i < numElem; i++) |
| 2261 | sortedQuantVec[i] = pair<unsigned int, unsigned int>(quantVec[i], i); |
| 2262 | |
| 2263 | std::sort(sortedQuantVec.begin(), sortedQuantVec.end(), |
| 2264 | [](const pair<unsigned int, unsigned int>& p0, |
| 2265 | const pair<unsigned int, unsigned int>& p1) { return p0.first < p1.first; }); |
| 2266 | } |
| 2267 | |
| 2268 | // -------------------------------------------------------------------------- ; |
| 2269 | |
| 2270 | template<class T> |
| 2271 | void Lerc2::ComputeHuffmanCodes(const T* data, int& numBytes, ImageEncodeMode& imageEncodeMode, std::vector<std::pair<unsigned short, unsigned int> >& codes) const |
| 2272 | { |
| 2273 | std::vector<int> histo, deltaHisto; |
| 2274 | ComputeHistoForHuffman(data, histo, deltaHisto); |
| 2275 | |
| 2276 | int nBytes0 = 0, nBytes1 = 0; |
| 2277 | double avgBpp0 = 0, avgBpp1 = 0; |
| 2278 | Huffman huffman0, huffman1; |
| 2279 | |
| 2280 | if (m_headerInfo.version >= 4) |
| 2281 | { |
| 2282 | if (!huffman0.ComputeCodes(histo) || !huffman0.ComputeCompressedSize(histo, nBytes0, avgBpp0)) |
| 2283 | nBytes0 = 0; |
| 2284 | } |
| 2285 | |
| 2286 | if (!huffman1.ComputeCodes(deltaHisto) || !huffman1.ComputeCompressedSize(deltaHisto, nBytes1, avgBpp1)) |
| 2287 | nBytes1 = 0; |
| 2288 | |
| 2289 | if (nBytes0 > 0 && nBytes1 > 0) // regular case, pick the better of the two |
| 2290 | { |
| 2291 | imageEncodeMode = (nBytes0 <= nBytes1) ? IEM_Huffman : IEM_DeltaHuffman; |
| 2292 | codes = (nBytes0 <= nBytes1) ? huffman0.GetCodes() : huffman1.GetCodes(); |
| 2293 | numBytes = (std::min)(nBytes0, nBytes1); |
| 2294 | } |
| 2295 | else if (nBytes0 == 0 && nBytes1 == 0) // rare case huffman cannot handle, fall back to tiling |
| 2296 | { |
| 2297 | imageEncodeMode = IEM_Tiling; |
| 2298 | codes.resize(0); |
| 2299 | numBytes = 0; |
| 2300 | } |
| 2301 | else // rare also, pick the valid one, the other is 0 |
| 2302 | { |
| 2303 | imageEncodeMode = (nBytes0 > nBytes1) ? IEM_Huffman : IEM_DeltaHuffman; |
| 2304 | codes = (nBytes0 > nBytes1) ? huffman0.GetCodes() : huffman1.GetCodes(); |
| 2305 | numBytes = (std::max)(nBytes0, nBytes1); |
| 2306 | } |
| 2307 | } |
| 2308 | |
| 2309 | // -------------------------------------------------------------------------- ; |
| 2310 | |
| 2311 | template<class T> |
| 2312 | void Lerc2::ComputeHistoForHuffman(const T* data, std::vector<int>& histo, std::vector<int>& deltaHisto) const |
| 2313 | { |
| 2314 | histo.resize(256); |
| 2315 | deltaHisto.resize(256); |
| 2316 | |
| 2317 | memset(&histo[0], 0, histo.size() * sizeof(int)); |
| 2318 | memset(&deltaHisto[0], 0, deltaHisto.size() * sizeof(int)); |
| 2319 | |
| 2320 | int offset = (m_headerInfo.dt == DT_Char) ? 128 : 0; |
| 2321 | int height = m_headerInfo.nRows; |
| 2322 | int width = m_headerInfo.nCols; |
| 2323 | int nDepth = m_headerInfo.nDepth; |
| 2324 | |
| 2325 | if (m_headerInfo.numValidPixel == width * height) // all valid |
| 2326 | { |
| 2327 | for (int iDepth = 0; iDepth < nDepth; iDepth++) |
| 2328 | { |
| 2329 | T prevVal = 0; |
| 2330 | for (int m = iDepth, i = 0; i < height; i++) |
| 2331 | for (int j = 0; j < width; j++, m += nDepth) |
| 2332 | { |
| 2333 | T val = data[m]; |
| 2334 | T delta = val; |
| 2335 | |
| 2336 | if (j > 0) |
| 2337 | delta -= prevVal; // use overflow |
| 2338 | else if (i > 0) |
| 2339 | delta -= data[m - width * nDepth]; |
| 2340 | else |
| 2341 | delta -= prevVal; |
| 2342 | |
| 2343 | prevVal = val; |
| 2344 | |
| 2345 | histo[offset + (int)val]++; |
| 2346 | deltaHisto[offset + (int)delta]++; |
| 2347 | } |
| 2348 | } |
| 2349 | } |
| 2350 | else // not all valid |
| 2351 | { |
| 2352 | for (int iDepth = 0; iDepth < nDepth; iDepth++) |
| 2353 | { |
| 2354 | T prevVal = 0; |
| 2355 | for (int k = 0, m = iDepth, i = 0; i < height; i++) |
| 2356 | for (int j = 0; j < width; j++, k++, m += nDepth) |
| 2357 | if (m_bitMask.IsValid(k)) |
| 2358 | { |
| 2359 | T val = data[m]; |
| 2360 | T delta = val; |
| 2361 | |
| 2362 | if (j > 0 && m_bitMask.IsValid(k - 1)) |
| 2363 | { |
| 2364 | delta -= prevVal; // use overflow |
| 2365 | } |
| 2366 | else if (i > 0 && m_bitMask.IsValid(k - width)) |
| 2367 | { |
| 2368 | delta -= data[m - width * nDepth]; |
| 2369 | } |
| 2370 | else |
| 2371 | delta -= prevVal; |
| 2372 | |
| 2373 | prevVal = val; |
| 2374 | |
| 2375 | histo[offset + (int)val]++; |
| 2376 | deltaHisto[offset + (int)delta]++; |
| 2377 | } |
| 2378 | } |
| 2379 | } |
| 2380 | } |
| 2381 | |
| 2382 | // -------------------------------------------------------------------------- ; |
| 2383 | |
| 2384 | template<class T> |
| 2385 | bool Lerc2::EncodeHuffman(const T* data, Byte** ppByte) const |
| 2386 | { |
| 2387 | if (!data || !ppByte) |
| 2388 | return false; |
| 2389 | |
| 2390 | Huffman huffman; |
| 2391 | if (!huffman.SetCodes(m_huffmanCodes) || !huffman.WriteCodeTable(ppByte, m_headerInfo.version)) // header and code table |
| 2392 | return false; |
| 2393 | |
| 2394 | int offset = (m_headerInfo.dt == DT_Char) ? 128 : 0; |
| 2395 | int height = m_headerInfo.nRows; |
| 2396 | int width = m_headerInfo.nCols; |
| 2397 | int nDepth = m_headerInfo.nDepth; |
| 2398 | int bitPos = 0; |
| 2399 | |
| 2400 | if (m_imageEncodeMode == IEM_DeltaHuffman) |
| 2401 | { |
| 2402 | for (int iDepth = 0; iDepth < nDepth; iDepth++) |
| 2403 | { |
| 2404 | T prevVal = 0; |
| 2405 | for (int k = 0, m = iDepth, i = 0; i < height; i++) |
| 2406 | for (int j = 0; j < width; j++, k++, m += nDepth) |
| 2407 | if (m_bitMask.IsValid(k)) |
| 2408 | { |
| 2409 | T val = data[m]; |
| 2410 | T delta = val; |
| 2411 | |
| 2412 | if (j > 0 && m_bitMask.IsValid(k - 1)) |
| 2413 | { |
| 2414 | delta -= prevVal; // use overflow |
| 2415 | } |
| 2416 | else if (i > 0 && m_bitMask.IsValid(k - width)) |
| 2417 | { |
| 2418 | delta -= data[m - width * nDepth]; |
| 2419 | } |
| 2420 | else |
| 2421 | delta -= prevVal; |
| 2422 | |
| 2423 | prevVal = val; |
| 2424 | |
| 2425 | // bit stuff the huffman code for this delta |
| 2426 | int kBin = offset + (int)delta; |
| 2427 | int len = m_huffmanCodes[kBin].first; |
| 2428 | if (len <= 0) |
| 2429 | return false; |
| 2430 | |
| 2431 | unsigned int code = m_huffmanCodes[kBin].second; |
| 2432 | |
| 2433 | if (!Huffman::PushValue(ppByte, bitPos, code, len)) |
| 2434 | return false; |
| 2435 | } |
| 2436 | } |
| 2437 | } |
| 2438 | |
| 2439 | else if (m_imageEncodeMode == IEM_Huffman) |
| 2440 | { |
| 2441 | for (int k = 0, m0 = 0, i = 0; i < height; i++) |
| 2442 | for (int j = 0; j < width; j++, k++, m0 += nDepth) |
| 2443 | if (m_bitMask.IsValid(k)) |
| 2444 | for (int m = 0; m < nDepth; m++) |
| 2445 | { |
| 2446 | T val = data[m0 + m]; |
| 2447 | |
| 2448 | // bit stuff the huffman code for this val |
| 2449 | int kBin = offset + (int)val; |
| 2450 | int len = m_huffmanCodes[kBin].first; |
| 2451 | if (len <= 0) |
| 2452 | return false; |
| 2453 | |
| 2454 | unsigned int code = m_huffmanCodes[kBin].second; |
| 2455 | |
| 2456 | if (!Huffman::PushValue(ppByte, bitPos, code, len)) |
| 2457 | return false; |
| 2458 | } |
| 2459 | } |
| 2460 | |
| 2461 | else |
| 2462 | return false; |
| 2463 | |
| 2464 | size_t numUInts = (bitPos > 0 ? 1 : 0) + 1; // add one more as the decode LUT can read ahead |
| 2465 | *ppByte += numUInts * sizeof(unsigned int); |
| 2466 | |
| 2467 | return true; |
| 2468 | } |
| 2469 | |
| 2470 | // -------------------------------------------------------------------------- ; |
| 2471 | |
| 2472 | template<class T> |
| 2473 | bool Lerc2::DecodeHuffman(const Byte** ppByte, size_t& nBytesRemainingInOut, T* data) const |
| 2474 | { |
| 2475 | if (!data || !ppByte || !(*ppByte)) |
| 2476 | return false; |
| 2477 | |
| 2478 | Huffman huffman; |
| 2479 | if (!huffman.ReadCodeTable(ppByte, nBytesRemainingInOut, m_headerInfo.version)) // header and code table |
| 2480 | return false; |
| 2481 | |
| 2482 | int numBitsLUT = 0; |
| 2483 | if (!huffman.BuildTreeFromCodes(numBitsLUT)) |
| 2484 | return false; |
| 2485 | |
| 2486 | int offset = (m_headerInfo.dt == DT_Char) ? 128 : 0; |
| 2487 | int height = m_headerInfo.nRows; |
| 2488 | int width = m_headerInfo.nCols; |
| 2489 | int nDepth = m_headerInfo.nDepth; |
| 2490 | |
| 2491 | const Byte* ptr0 = *ppByte; |
| 2492 | const Byte* ptr = ptr0; |
| 2493 | |
| 2494 | int bitPos = 0; |
| 2495 | size_t nBytesRemaining = nBytesRemainingInOut; |
| 2496 | |
| 2497 | if (m_headerInfo.numValidPixel == width * height) // all valid |
| 2498 | { |
| 2499 | if (m_imageEncodeMode == IEM_DeltaHuffman) |
| 2500 | { |
| 2501 | for (int iDepth = 0; iDepth < nDepth; iDepth++) |
| 2502 | { |
| 2503 | T prevVal = 0; |
| 2504 | for (int m = iDepth, i = 0; i < height; i++) |
| 2505 | for (int j = 0; j < width; j++, m += nDepth) |
| 2506 | { |
| 2507 | int val = 0; |
| 2508 | if (!huffman.DecodeOneValue(&ptr, nBytesRemaining, bitPos, numBitsLUT, val)) |
| 2509 | return false; |
| 2510 | |
| 2511 | T delta = (T)(val - offset); |
| 2512 | |
| 2513 | if (j > 0) |
| 2514 | delta += prevVal; // use overflow |
| 2515 | else if (i > 0) |
| 2516 | delta += data[m - width * nDepth]; |
| 2517 | else |
| 2518 | delta += prevVal; |
| 2519 | |
| 2520 | data[m] = delta; |
| 2521 | prevVal = delta; |
| 2522 | } |
| 2523 | } |
| 2524 | } |
| 2525 | |
| 2526 | else if (m_imageEncodeMode == IEM_Huffman) |
| 2527 | { |
| 2528 | for (int k = 0, m0 = 0, i = 0; i < height; i++) |
| 2529 | for (int j = 0; j < width; j++, k++, m0 += nDepth) |
| 2530 | for (int m = 0; m < nDepth; m++) |
| 2531 | { |
| 2532 | int val = 0; |
| 2533 | if (!huffman.DecodeOneValue(&ptr, nBytesRemaining, bitPos, numBitsLUT, val)) |
| 2534 | return false; |
| 2535 | |
| 2536 | data[m0 + m] = (T)(val - offset); |
| 2537 | } |
| 2538 | } |
| 2539 | |
| 2540 | else |
| 2541 | return false; |
| 2542 | } |
| 2543 | |
| 2544 | else // not all valid |
| 2545 | { |
| 2546 | if (m_imageEncodeMode == IEM_DeltaHuffman) |
| 2547 | { |
| 2548 | for (int iDepth = 0; iDepth < nDepth; iDepth++) |
| 2549 | { |
| 2550 | T prevVal = 0; |
| 2551 | for (int k = 0, m = iDepth, i = 0; i < height; i++) |
| 2552 | for (int j = 0; j < width; j++, k++, m += nDepth) |
| 2553 | if (m_bitMask.IsValid(k)) |
| 2554 | { |
| 2555 | int val = 0; |
| 2556 | if (!huffman.DecodeOneValue(&ptr, nBytesRemaining, bitPos, numBitsLUT, val)) |
| 2557 | return false; |
| 2558 | |
| 2559 | T delta = (T)(val - offset); |
| 2560 | |
| 2561 | if (j > 0 && m_bitMask.IsValid(k - 1)) |
| 2562 | { |
| 2563 | delta += prevVal; // use overflow |
| 2564 | } |
| 2565 | else if (i > 0 && m_bitMask.IsValid(k - width)) |
| 2566 | { |
| 2567 | delta += data[m - width * nDepth]; |
| 2568 | } |
| 2569 | else |
| 2570 | delta += prevVal; |
| 2571 | |
| 2572 | data[m] = delta; |
| 2573 | prevVal = delta; |
| 2574 | } |
| 2575 | } |
| 2576 | } |
| 2577 | |
| 2578 | else if (m_imageEncodeMode == IEM_Huffman) |
| 2579 | { |
| 2580 | for (int k = 0, m0 = 0, i = 0; i < height; i++) |
| 2581 | for (int j = 0; j < width; j++, k++, m0 += nDepth) |
| 2582 | if (m_bitMask.IsValid(k)) |
| 2583 | for (int m = 0; m < nDepth; m++) |
| 2584 | { |
| 2585 | int val = 0; |
| 2586 | if (!huffman.DecodeOneValue(&ptr, nBytesRemaining, bitPos, numBitsLUT, val)) |
| 2587 | return false; |
| 2588 | |
| 2589 | data[m0 + m] = (T)(val - offset); |
| 2590 | } |
| 2591 | } |
| 2592 | |
| 2593 | else |
| 2594 | return false; |
| 2595 | } |
| 2596 | |
| 2597 | size_t numUInts = (bitPos > 0 ? 1 : 0) + 1; // add one more as the decode LUT can read ahead |
| 2598 | size_t len = (ptr - ptr0) + numUInts * sizeof(unsigned int); |
| 2599 | |
| 2600 | if (nBytesRemainingInOut < len) |
| 2601 | return false; |
| 2602 | |
| 2603 | *ppByte += len; |
| 2604 | nBytesRemainingInOut -= len; |
| 2605 | return true; |
| 2606 | } |
| 2607 | |
| 2608 | // -------------------------------------------------------------------------- ; |
| 2609 | |
| 2610 | template<class T> |
| 2611 | bool Lerc2::WriteMinMaxRanges(const T*, Byte** ppByte) const |
| 2612 | { |
| 2613 | if (!ppByte || !(*ppByte)) |
| 2614 | return false; |
| 2615 | |
| 2616 | //printf("write min / max = %f %f\n", m_zMinVec[0], m_zMaxVec[0]); |
| 2617 | |
| 2618 | int nDepth = m_headerInfo.nDepth; |
| 2619 | if (/* nDepth < 2 || */ (int)m_zMinVec.size() != nDepth || (int)m_zMaxVec.size() != nDepth) |
| 2620 | return false; |
| 2621 | |
| 2622 | std::vector<T> zVec(nDepth); |
| 2623 | size_t len = nDepth * sizeof(T); |
| 2624 | |
| 2625 | for (int i = 0; i < nDepth; i++) |
| 2626 | zVec[i] = (T)m_zMinVec[i]; |
| 2627 | |
| 2628 | memcpy(*ppByte, &zVec[0], len); |
| 2629 | (*ppByte) += len; |
| 2630 | |
| 2631 | for (int i = 0; i < nDepth; i++) |
| 2632 | zVec[i] = (T)m_zMaxVec[i]; |
| 2633 | |
| 2634 | memcpy(*ppByte, &zVec[0], len); |
| 2635 | (*ppByte) += len; |
| 2636 | |
| 2637 | return true; |
| 2638 | } |
| 2639 | |
| 2640 | // -------------------------------------------------------------------------- ; |
| 2641 | |
| 2642 | template<class T> |
| 2643 | bool Lerc2::ReadMinMaxRanges(const Byte** ppByte, size_t& nBytesRemaining, const T* /*data*/) |
| 2644 | { |
| 2645 | if (!ppByte || !(*ppByte)) |
| 2646 | return false; |
| 2647 | |
| 2648 | int nDepth = m_headerInfo.nDepth; |
| 2649 | |
| 2650 | m_zMinVec.resize(nDepth); |
| 2651 | m_zMaxVec.resize(nDepth); |
| 2652 | |
| 2653 | std::vector<T> zVec(nDepth); |
| 2654 | size_t len = nDepth * sizeof(T); |
| 2655 | |
| 2656 | if (nBytesRemaining < len || !memcpy(&zVec[0], *ppByte, len)) |
| 2657 | return false; |
| 2658 | |
| 2659 | (*ppByte) += len; |
| 2660 | nBytesRemaining -= len; |
| 2661 | |
| 2662 | for (int i = 0; i < nDepth; i++) |
| 2663 | m_zMinVec[i] = zVec[i]; |
| 2664 | |
| 2665 | if (nBytesRemaining < len || !memcpy(&zVec[0], *ppByte, len)) |
| 2666 | return false; |
| 2667 | |
| 2668 | (*ppByte) += len; |
| 2669 | nBytesRemaining -= len; |
| 2670 | |
| 2671 | for (int i = 0; i < nDepth; i++) |
| 2672 | m_zMaxVec[i] = zVec[i]; |
| 2673 | |
| 2674 | //printf("read min / max = %f %f\n", m_zMinVec[0], m_zMaxVec[0]); |
| 2675 | |
| 2676 | return true; |
| 2677 | } |
| 2678 | |
| 2679 | // -------------------------------------------------------------------------- ; |
| 2680 | |
| 2681 | template<class T> |
| 2682 | bool Lerc2::FillConstImage(T* data) const |
| 2683 | { |
| 2684 | if (!data) |
| 2685 | return false; |
| 2686 | |
| 2687 | const HeaderInfo& hd = m_headerInfo; |
| 2688 | int nCols = hd.nCols; |
| 2689 | int nRows = hd.nRows; |
| 2690 | int nDepth = hd.nDepth; |
| 2691 | T z0 = (T)hd.zMin; |
| 2692 | |
| 2693 | if (nDepth == 1) |
| 2694 | { |
| 2695 | for (int k = 0, i = 0; i < nRows; i++) |
| 2696 | for (int j = 0; j < nCols; j++, k++) |
| 2697 | if (m_bitMask.IsValid(k)) |
| 2698 | data[k] = z0; |
| 2699 | } |
| 2700 | else |
| 2701 | { |
| 2702 | std::vector<T> zBufVec(nDepth, z0); |
| 2703 | |
| 2704 | if (hd.zMin != hd.zMax) |
| 2705 | { |
| 2706 | if ((int)m_zMinVec.size() != nDepth) |
| 2707 | return false; |
| 2708 | |
| 2709 | for (int m = 0; m < nDepth; m++) |
| 2710 | zBufVec[m] = (T)m_zMinVec[m]; |
| 2711 | } |
| 2712 | |
| 2713 | int len = nDepth * sizeof(T); |
| 2714 | for (int k = 0, m = 0, i = 0; i < nRows; i++) |
| 2715 | for (int j = 0; j < nCols; j++, k++, m += nDepth) |
| 2716 | if (m_bitMask.IsValid(k)) |
| 2717 | memcpy(&data[m], &zBufVec[0], len); |
| 2718 | } |
| 2719 | |
| 2720 | return true; |
| 2721 | } |
| 2722 | |
| 2723 | // -------------------------------------------------------------------------- ; |
| 2724 |