| 1 | /* |
| 2 | Copyright 2015 - 2026 Esri |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | |
| 16 | A local copy of the license and additional notices are located with the |
| 17 | source distribution at: |
| 18 | |
| 19 | http://github.com/Esri/lerc/ |
| 20 | |
| 21 | Contributors: Thomas Maurer |
| 22 | */ |
| 23 | |
| 24 | #include <climits> |
| 25 | #include <cstring> |
| 26 | #include <algorithm> |
| 27 | #include "CntZImage.h" |
| 28 | #include "BitStuffer.h" |
| 29 | #include "../BitMask.h" |
| 30 | #include "../RLE.h" |
| 31 | |
| 32 | using namespace std; |
| 33 | using namespace LercNS; |
| 34 | |
| 35 | // -------------------------------------------------------------------------- ; |
| 36 | |
| 37 | CntZImage::CntZImage() |
| 38 | { |
| 39 | type_ = CNT_Z; |
| 40 | m_bDecoderCanIgnoreMask = false; |
| 41 | |
| 42 | memset(&m_infoFromComputeNumBytes, 0, sizeof(m_infoFromComputeNumBytes)); |
| 43 | }; |
| 44 | |
| 45 | // -------------------------------------------------------------------------- ; |
| 46 | |
| 47 | bool CntZImage::resizeFill0(int width, int height) |
| 48 | { |
| 49 | if (!resize(width, height)) |
| 50 | return false; |
| 51 | |
| 52 | memset(getData(), 0, sizeof(CntZ) * width * height); |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | // -------------------------------------------------------------------------- ; |
| 57 | |
| 58 | unsigned int CntZImage::computeNumBytesNeededToReadHeader(bool onlyZPart) |
| 59 | { |
| 60 | CntZImage zImg; |
| 61 | unsigned int cnt = (unsigned int)zImg.getTypeString().length(); // "CntZImage ", 10 bytes |
| 62 | cnt += 4 * sizeof(int); // version, type, width, height |
| 63 | cnt += 1 * sizeof(double); // maxZError |
| 64 | if (!onlyZPart) |
| 65 | cnt += 3 * sizeof(int) + sizeof(float); // cnt part |
| 66 | cnt += 3 * sizeof(int) + sizeof(float); // z part |
| 67 | cnt += 1; |
| 68 | return cnt; |
| 69 | } |
| 70 | |
| 71 | // -------------------------------------------------------------------------- ; |
| 72 | |
| 73 | bool CntZImage::read(const Byte** ppByte, const Byte* bArr_end, double maxZError, bool onlyHeader, bool onlyZPart) |
| 74 | { |
| 75 | if (!ppByte || !*ppByte) |
| 76 | return false; |
| 77 | |
| 78 | size_t len = getTypeString().length(); |
| 79 | |
| 80 | if (*ppByte + len > bArr_end) |
| 81 | return false; |
| 82 | |
| 83 | string typeStr(len, '0'); |
| 84 | memcpy(&typeStr[0], *ppByte, len); |
| 85 | *ppByte += len; |
| 86 | |
| 87 | if (typeStr != getTypeString()) |
| 88 | return false; |
| 89 | |
| 90 | int version = 0, type = 0, width = 0, height = 0; |
| 91 | double maxZErrorInFile = 0; |
| 92 | |
| 93 | const Byte* ptr = *ppByte; |
| 94 | |
| 95 | if (ptr + 4 * sizeof(int) + sizeof(double) > bArr_end) |
| 96 | return false; |
| 97 | |
| 98 | memcpy(&version, ptr, sizeof(int)); ptr += sizeof(int); |
| 99 | memcpy(&type, ptr, sizeof(int)); ptr += sizeof(int); |
| 100 | memcpy(&height, ptr, sizeof(int)); ptr += sizeof(int); |
| 101 | memcpy(&width, ptr, sizeof(int)); ptr += sizeof(int); |
| 102 | memcpy(&maxZErrorInFile, ptr, sizeof(double)); ptr += sizeof(double); |
| 103 | |
| 104 | *ppByte = ptr; |
| 105 | |
| 106 | SWAP_4(version); |
| 107 | SWAP_4(type); |
| 108 | SWAP_4(height); |
| 109 | SWAP_4(width); |
| 110 | SWAP_8(maxZErrorInFile); |
| 111 | |
| 112 | if (version != 11 || type != type_) |
| 113 | return false; |
| 114 | |
| 115 | if (height < 0 || width < 0 || height > 40000 || width > 40000) // guard against bogus numbers; size limitation for old Lerc1 |
| 116 | return false; |
| 117 | |
| 118 | if (sizeof(CntZ) * height * width > (size_t)INT_MAX) |
| 119 | return false; |
| 120 | |
| 121 | if (maxZErrorInFile > maxZError) |
| 122 | return false; |
| 123 | |
| 124 | if (onlyHeader) |
| 125 | return true; |
| 126 | |
| 127 | if (!onlyZPart && !resizeFill0(width, height)) // init with (0,0), used below |
| 128 | return false; |
| 129 | |
| 130 | m_bDecoderCanIgnoreMask = false; |
| 131 | |
| 132 | for (int iPart = 0; iPart < 2; iPart++) |
| 133 | { |
| 134 | bool zPart = iPart ? true : false; // first cnt part, then z part |
| 135 | |
| 136 | if (!zPart && onlyZPart) |
| 137 | continue; |
| 138 | |
| 139 | int numTilesVert = 0, numTilesHori = 0, numBytes = 0; |
| 140 | float maxValInImg = 0; |
| 141 | |
| 142 | const Byte* ptr = *ppByte; |
| 143 | |
| 144 | if (ptr + 3 * sizeof(int) + sizeof(float) > bArr_end) |
| 145 | return false; |
| 146 | |
| 147 | memcpy(&numTilesVert, ptr, sizeof(int)); ptr += sizeof(int); |
| 148 | memcpy(&numTilesHori, ptr, sizeof(int)); ptr += sizeof(int); |
| 149 | memcpy(&numBytes, ptr, sizeof(int)); ptr += sizeof(int); |
| 150 | memcpy(&maxValInImg, ptr, sizeof(float)); ptr += sizeof(float); |
| 151 | |
| 152 | *ppByte = ptr; |
| 153 | const Byte* bArr = ptr; |
| 154 | |
| 155 | SWAP_4(numTilesVert); |
| 156 | SWAP_4(numTilesHori); |
| 157 | SWAP_4(numBytes); |
| 158 | SWAP_4(maxValInImg); |
| 159 | |
| 160 | if (numBytes < 0 || ptr + numBytes > bArr_end) |
| 161 | return false; |
| 162 | |
| 163 | if (!zPart && numTilesVert == 0 && numTilesHori == 0) // no tiling for this cnt part |
| 164 | { |
| 165 | if (numBytes == 0) // cnt part is const |
| 166 | { |
| 167 | CntZ* dstPtr = getData(); |
| 168 | for (int i = 0; i < height_; i++) |
| 169 | for (int j = 0; j < width_; j++) |
| 170 | { |
| 171 | dstPtr->cnt = maxValInImg; |
| 172 | dstPtr++; |
| 173 | } |
| 174 | |
| 175 | if (maxValInImg > 0) |
| 176 | m_bDecoderCanIgnoreMask = true; |
| 177 | } |
| 178 | |
| 179 | if (numBytes > 0) // cnt part is binary mask, use fast RLE class |
| 180 | { |
| 181 | // decompress to bit mask |
| 182 | BitMask bitMask(width_, height_); |
| 183 | RLE rle; |
| 184 | if (!rle.decompress(bArr, (size_t)width_ * height_ * 2, (Byte*)bitMask.Bits(), bitMask.Size())) |
| 185 | return false; |
| 186 | |
| 187 | CntZ* dstPtr = getData(); |
| 188 | for (int k = 0, i = 0; i < height_; i++) |
| 189 | for (int j = 0; j < width_; j++, k++, dstPtr++) |
| 190 | dstPtr->cnt = bitMask.IsValid(k) ? 1.0f : 0.0f; |
| 191 | } |
| 192 | } |
| 193 | else if (!readTiles(zPart, maxZErrorInFile, numTilesVert, numTilesHori, maxValInImg, bArr)) |
| 194 | return false; |
| 195 | |
| 196 | *ppByte += numBytes; |
| 197 | } |
| 198 | |
| 199 | m_tmpDataVec.clear(); |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | // -------------------------------------------------------------------------- ; |
| 204 | |
| 205 | bool CntZImage::readTiles(bool zPart, double maxZErrorInFile, int numTilesVert, int numTilesHori, |
| 206 | float maxValInImg, const Byte* bArr) |
| 207 | { |
| 208 | if (numTilesVert <= 0 || numTilesHori <= 0 |
| 209 | || numTilesVert > height_ || numTilesHori > width_) |
| 210 | return false; |
| 211 | |
| 212 | const Byte* ptr = bArr; |
| 213 | |
| 214 | for (int iTile = 0; iTile <= numTilesVert; iTile++) |
| 215 | { |
| 216 | int tileH = height_ / numTilesVert; |
| 217 | int i0 = iTile * tileH; |
| 218 | if (iTile == numTilesVert) |
| 219 | tileH = height_ % numTilesVert; |
| 220 | |
| 221 | if (tileH == 0) |
| 222 | continue; |
| 223 | |
| 224 | for (int jTile = 0; jTile <= numTilesHori; jTile++) |
| 225 | { |
| 226 | int tileW = width_ / numTilesHori; |
| 227 | int j0 = jTile * tileW; |
| 228 | if (jTile == numTilesHori) |
| 229 | tileW = width_ % numTilesHori; |
| 230 | |
| 231 | if (tileW == 0) |
| 232 | continue; |
| 233 | |
| 234 | bool rv = zPart ? readZTile( &ptr, i0, i0 + tileH, j0, j0 + tileW, maxZErrorInFile, maxValInImg) : |
| 235 | readCntTile(&ptr, i0, i0 + tileH, j0, j0 + tileW); |
| 236 | |
| 237 | if (!rv) |
| 238 | return false; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | // -------------------------------------------------------------------------- ; |
| 246 | |
| 247 | bool CntZImage::readCntTile(const Byte** ppByte, int i0, int i1, int j0, int j1) |
| 248 | { |
| 249 | if (i0 >= i1 || j0 >= j1) |
| 250 | return false; |
| 251 | |
| 252 | const Byte* ptr = *ppByte; |
| 253 | Byte comprFlag = *ptr++; |
| 254 | |
| 255 | if (comprFlag == 2) // entire tile is constant 0 (invalid) |
| 256 | { // here we depend on resizeFill0() |
| 257 | *ppByte = ptr; |
| 258 | return true; |
| 259 | } |
| 260 | |
| 261 | if (comprFlag == 3 || comprFlag == 4) // entire tile is constant -1 (invalid) or 1 (valid) |
| 262 | { |
| 263 | CntZ cz1m = {-1, 0}; |
| 264 | CntZ cz1p = { 1, 0}; |
| 265 | CntZ cz1 = (comprFlag == 3) ? cz1m : cz1p; |
| 266 | |
| 267 | for (int i = i0; i < i1; i++) |
| 268 | { |
| 269 | CntZ* dstPtr = getData() + i * width_ + j0; |
| 270 | for (int j = j0; j < j1; j++) |
| 271 | *dstPtr++ = cz1; |
| 272 | } |
| 273 | |
| 274 | *ppByte = ptr; |
| 275 | return true; |
| 276 | } |
| 277 | |
| 278 | if ((comprFlag & 63) > 4) |
| 279 | return false; |
| 280 | |
| 281 | if (comprFlag == 0) |
| 282 | { |
| 283 | // read cnt's as flt arr uncompressed |
| 284 | for (int i = i0; i < i1; i++) |
| 285 | { |
| 286 | CntZ* dstPtr = getData() + i * width_ + j0; |
| 287 | for (int j = j0; j < j1; j++) |
| 288 | { |
| 289 | memcpy(&dstPtr->cnt, ptr, 4); ptr += 4; |
| 290 | SWAP_4(dstPtr->cnt); |
| 291 | dstPtr++; |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | else |
| 296 | { |
| 297 | // read cnt's as int arr bit stuffed |
| 298 | int bits67 = comprFlag >> 6; |
| 299 | int n = (bits67 == 0) ? 4 : 3 - bits67; |
| 300 | |
| 301 | float offset = 0; |
| 302 | if (!readFlt(&ptr, offset, n)) |
| 303 | return false; |
| 304 | |
| 305 | int numPixel = (i1 - i0) * (j1 - j0); |
| 306 | BitStuffer bitStuffer; |
| 307 | if (!bitStuffer.read(&ptr, m_tmpDataVec) || m_tmpDataVec.size() < (size_t)numPixel) |
| 308 | return false; |
| 309 | |
| 310 | unsigned int* srcPtr = &m_tmpDataVec[0]; |
| 311 | |
| 312 | for (int i = i0; i < i1; i++) |
| 313 | { |
| 314 | CntZ* dstPtr = getData() + i * width_ + j0; |
| 315 | for (int j = j0; j < j1; j++) |
| 316 | { |
| 317 | dstPtr->cnt = offset + (float)(*srcPtr++); |
| 318 | dstPtr++; |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | *ppByte = ptr; |
| 324 | return true; |
| 325 | } |
| 326 | |
| 327 | // -------------------------------------------------------------------------- ; |
| 328 | |
| 329 | bool CntZImage::readZTile(const Byte** ppByte, int i0, int i1, int j0, int j1, double maxZErrorInFile, float maxZInImg) |
| 330 | { |
| 331 | const Byte* ptr = *ppByte; |
| 332 | Byte comprFlag = *ptr++; |
| 333 | int bits67 = comprFlag >> 6; |
| 334 | comprFlag &= 63; |
| 335 | |
| 336 | if (comprFlag == 2) // entire zTile is constant 0 (if valid or invalid doesn't matter) |
| 337 | { |
| 338 | for (int i = i0; i < i1; i++) |
| 339 | { |
| 340 | CntZ* dstPtr = getData() + i * width_ + j0; |
| 341 | for (int j = j0; j < j1; j++) |
| 342 | { |
| 343 | if (dstPtr->cnt > 0) |
| 344 | dstPtr->z = 0; |
| 345 | dstPtr++; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | *ppByte = ptr; |
| 350 | return true; |
| 351 | } |
| 352 | |
| 353 | if (comprFlag > 3) |
| 354 | return false; |
| 355 | |
| 356 | if (comprFlag == 0) |
| 357 | { |
| 358 | // read z's as flt arr uncompressed |
| 359 | for (int i = i0; i < i1; i++) |
| 360 | { |
| 361 | CntZ* dstPtr = getData() + i * width_ + j0; |
| 362 | for (int j = j0; j < j1; j++) |
| 363 | { |
| 364 | if (dstPtr->cnt > 0) |
| 365 | { |
| 366 | memcpy(&dstPtr->z, ptr, 4); ptr += 4; |
| 367 | SWAP_4(dstPtr->z); |
| 368 | } |
| 369 | dstPtr++; |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | else |
| 374 | { |
| 375 | // read z's as int arr bit stuffed |
| 376 | int n = (bits67 == 0) ? 4 : 3 - bits67; |
| 377 | float offset = 0; |
| 378 | if (!readFlt(&ptr, offset, n)) |
| 379 | return false; |
| 380 | |
| 381 | if (comprFlag == 3) |
| 382 | { |
| 383 | for (int i = i0; i < i1; i++) |
| 384 | { |
| 385 | CntZ* dstPtr = getData() + i * width_ + j0; |
| 386 | for (int j = j0; j < j1; j++) |
| 387 | { |
| 388 | if (dstPtr->cnt > 0) |
| 389 | dstPtr->z = offset; |
| 390 | dstPtr++; |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | else |
| 395 | { |
| 396 | vector<unsigned int>& dataVec = m_tmpDataVec; |
| 397 | BitStuffer bitStuffer; |
| 398 | if (!bitStuffer.read(&ptr, dataVec)) |
| 399 | return false; |
| 400 | |
| 401 | double invScale = 2 * maxZErrorInFile; |
| 402 | unsigned int* srcPtr = &dataVec[0]; |
| 403 | |
| 404 | if (m_bDecoderCanIgnoreMask) |
| 405 | { |
| 406 | for (int i = i0; i < i1; i++) |
| 407 | { |
| 408 | CntZ* dstPtr = getData() + i * width_ + j0; |
| 409 | for (int j = j0; j < j1; j++) |
| 410 | { |
| 411 | float z = (float)(offset + *srcPtr++ * invScale); |
| 412 | dstPtr->z = std::min(z, maxZInImg); // make sure we stay in the orig range |
| 413 | dstPtr++; |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | else |
| 418 | { |
| 419 | for (int i = i0; i < i1; i++) |
| 420 | { |
| 421 | CntZ* dstPtr = getData() + i * width_ + j0; |
| 422 | for (int j = j0; j < j1; j++) |
| 423 | { |
| 424 | if (dstPtr->cnt > 0) |
| 425 | { |
| 426 | float z = (float)(offset + *srcPtr++ * invScale); |
| 427 | dstPtr->z = std::min(z, maxZInImg); // make sure we stay in the orig range |
| 428 | } |
| 429 | dstPtr++; |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | *ppByte = ptr; |
| 437 | return true; |
| 438 | } |
| 439 | |
| 440 | // -------------------------------------------------------------------------- ; |
| 441 | |
| 442 | int CntZImage::numBytesFlt(float z) |
| 443 | { |
| 444 | short s = (short)z; |
| 445 | char c = (char)s; |
| 446 | return ((float)c == z) ? 1 : ((float)s == z) ? 2 : 4; |
| 447 | } |
| 448 | |
| 449 | // -------------------------------------------------------------------------- ; |
| 450 | |
| 451 | bool CntZImage::readFlt(const Byte** ppByte, float& z, int numBytes) |
| 452 | { |
| 453 | const Byte* ptr = *ppByte; |
| 454 | |
| 455 | if (numBytes == 1) |
| 456 | { |
| 457 | char c = *((char*)ptr); |
| 458 | z = c; |
| 459 | } |
| 460 | else if (numBytes == 2) |
| 461 | { |
| 462 | short s; |
| 463 | memcpy(&s, ptr, sizeof(short)); |
| 464 | SWAP_2(s); |
| 465 | z = s; |
| 466 | } |
| 467 | else if (numBytes == 4) |
| 468 | { |
| 469 | memcpy(&z, ptr, sizeof(float)); |
| 470 | SWAP_4(z); |
| 471 | } |
| 472 | else |
| 473 | return false; |
| 474 | |
| 475 | *ppByte = ptr + numBytes; |
| 476 | return true; |
| 477 | } |
| 478 | |
| 479 | // -------------------------------------------------------------------------- ; |
| 480 |