| 1 | |
| 2 | #include <cstring> |
| 3 | #include <cstdlib> |
| 4 | #include <cmath> |
| 5 | #include <vector> |
| 6 | #include <iostream> |
| 7 | #include <algorithm> |
| 8 | #include <chrono> |
| 9 | #include <cfloat> |
| 10 | #include "../LercLib/include/Lerc_c_api.h" |
| 11 | #include "../LercLib/include/Lerc_types.h" // see for error codes, data types, etc |
| 12 | |
| 13 | using namespace std; |
| 14 | using namespace std::chrono; |
| 15 | |
| 16 | //#define TestLegacyData |
| 17 | //#define DumpDecodedTiles |
| 18 | //#define CompareAgainstDumpedTiles |
| 19 | |
| 20 | typedef unsigned char Byte; // convenience |
| 21 | typedef unsigned int uint32; |
| 22 | enum lerc_DataType { dt_char = 0, dt_uchar, dt_short, dt_ushort, dt_int, dt_uint, dt_float, dt_double }; // shorter |
| 23 | |
| 24 | //----------------------------------------------------------------------------- |
| 25 | // some helper functions |
| 26 | //----------------------------------------------------------------------------- |
| 27 | |
| 28 | void BlobInfo_Print(const uint32* infoArr); |
| 29 | bool BlobInfo_Equal(const uint32* infoArr, uint32 nDepth, uint32 nCols, uint32 nRows, uint32 nBands, uint32 dataType); |
| 30 | |
| 31 | #ifdef TestLegacyData |
| 32 | bool ReadListFile(const string& listFn, vector<string>& fnVec); |
| 33 | bool ReadFile(const string& fn, Byte* pBuffer, size_t bufferSize, size_t& nBytesRead); |
| 34 | bool WriteDecodedTile(const string& fn, int nMasks, const Byte* pValidBytes, int nDepth, int w, int h, int nBands, int bpp, const void* pArr); |
| 35 | bool ReadDecodedTile(const string& fn, int& nMasks, Byte* pValidBytes, int& nDepth, int& w, int& h, int& nBands, int& bpp, void* pArr); |
| 36 | |
| 37 | bool CompareTwoTiles(int nMasks, const Byte* pValidBytes, int nDepth, int w, int h, int nBands, int bpp, const void* pArr, |
| 38 | int nMasks2, const Byte* pValidBytes2, int nDepth2, int w2, int h2, int nBands2, int bpp2, const void* pArr2, int dt); |
| 39 | #endif |
| 40 | |
| 41 | static void Failed(const char* fctName, int& cntFailures) |
| 42 | { |
| 43 | std::cout << fctName << " failed" << endl; |
| 44 | cntFailures++; |
| 45 | } |
| 46 | |
| 47 | //----------------------------------------------------------------------------- |
| 48 | // main |
| 49 | //----------------------------------------------------------------------------- |
| 50 | |
| 51 | int main(int argc, char* arcv[]) |
| 52 | { |
| 53 | const int infoArrSize = (int)LercNS::InfoArrOrder::_last; |
| 54 | const int dataRangeArrSize = (int)LercNS::DataRangeArrOrder::_last; |
| 55 | |
| 56 | lerc_status hr(0); |
| 57 | int cntFailures(0); |
| 58 | uint32 infoArr[infoArrSize]; |
| 59 | double dataRangeArr[dataRangeArrSize]; |
| 60 | high_resolution_clock::time_point t0, t1; |
| 61 | |
| 62 | #ifndef TestLegacyData |
| 63 | |
| 64 | //--------------------------------------------------------------------------- |
| 65 | |
| 66 | // Sample 1: float image, 1 band, with some pixels set to invalid / void, maxZError = 0.1 |
| 67 | { |
| 68 | int h = 512; |
| 69 | int w = 512; |
| 70 | |
| 71 | float* zImg = new float[w * h]; |
| 72 | memset(zImg, 0, w * h * sizeof(float)); |
| 73 | |
| 74 | Byte* maskByteImg = new Byte[w * h]; |
| 75 | memset(maskByteImg, 1, w * h); |
| 76 | |
| 77 | for (int k = 0, i = 0; i < h; i++) |
| 78 | for (int j = 0; j < w; j++, k++) |
| 79 | { |
| 80 | zImg[k] = sqrt((float)(i * i + j * j)); // smooth surface |
| 81 | zImg[k] += rand() % 20; // add some small amplitude noise |
| 82 | |
| 83 | //if (i == 99 && j == 99) // to test presence of NaN |
| 84 | // zImg[k] = NAN; |
| 85 | |
| 86 | if (j % 100 == 0 || i % 100 == 0) // set some void pixels |
| 87 | maskByteImg[k] = 0; |
| 88 | } |
| 89 | |
| 90 | // compress into byte arr |
| 91 | |
| 92 | double maxZErrorWanted = 0.1; |
| 93 | double eps = 0.0001; // safety margin (optional), to account for finite floating point accuracy |
| 94 | double maxZError = maxZErrorWanted - eps; |
| 95 | |
| 96 | uint32 numBytesNeeded = 0; |
| 97 | uint32 numBytesWritten = 0; |
| 98 | |
| 99 | // optional, can call encode() directly with large enough blob buffer |
| 100 | |
| 101 | hr = lerc_computeCompressedSize((void*)zImg, // raw image data, row by row, band by band |
| 102 | (uint32)dt_float, 1, w, h, 1, |
| 103 | 1, // nMasks, added in Lerc 3.0 to allow for different masks per band |
| 104 | maskByteImg, // can pass nullptr if all pixels are valid and nMasks = 0 |
| 105 | maxZError, // max coding error per pixel, or precision |
| 106 | &numBytesNeeded); // size of outgoing Lerc blob |
| 107 | |
| 108 | if (hr) |
| 109 | Failed("lerc_computeCompressedSize(...)", cntFailures); |
| 110 | |
| 111 | uint32 numBytesBlob = numBytesNeeded; |
| 112 | Byte* pLercBlob = new Byte[numBytesBlob]; |
| 113 | |
| 114 | t0 = high_resolution_clock::now(); |
| 115 | |
| 116 | hr = lerc_encode((void*)zImg, // raw image data, row by row, band by band |
| 117 | (uint32)dt_float, 1, w, h, 1, |
| 118 | 1, // nMasks, added in Lerc 3.0 to allow for different masks per band |
| 119 | maskByteImg, // can pass nullptr if all pixels are valid and nMasks = 0 |
| 120 | maxZError, // max coding error per pixel, or precision |
| 121 | pLercBlob, // buffer to write to, function will fail if buffer too small |
| 122 | numBytesBlob, // buffer size |
| 123 | &numBytesWritten); // num bytes written to buffer |
| 124 | |
| 125 | if (hr) |
| 126 | Failed("lerc_encode(...)", cntFailures); |
| 127 | |
| 128 | t1 = high_resolution_clock::now(); |
| 129 | auto duration = duration_cast<milliseconds>(t1 - t0).count(); |
| 130 | |
| 131 | double ratio = w * h * (0.125 + sizeof(float)) / numBytesBlob; |
| 132 | std::cout << "sample 1 compression ratio = " << ratio << ", encode time = " << duration << " ms" << endl; |
| 133 | |
| 134 | // decompress |
| 135 | if ((hr = lerc_getBlobInfo(pLercBlob, numBytesBlob, infoArr, dataRangeArr, infoArrSize, dataRangeArrSize))) |
| 136 | Failed("lerc_getBlobInfo(...)", cntFailures); |
| 137 | |
| 138 | BlobInfo_Print(infoArr); |
| 139 | |
| 140 | if (!BlobInfo_Equal(infoArr, 1, w, h, 1, (uint32)dt_float)) |
| 141 | { |
| 142 | std::cout << "got wrong lerc info" << endl; |
| 143 | cntFailures++; |
| 144 | } |
| 145 | |
| 146 | // new empty data storage |
| 147 | float* zImg2 = new float[w * h]; |
| 148 | memset(zImg2, 0, w * h * sizeof(float)); |
| 149 | |
| 150 | Byte* maskByteImg2 = new Byte[w * h]; |
| 151 | memset(maskByteImg2, 0, w * h); |
| 152 | |
| 153 | t0 = high_resolution_clock::now(); |
| 154 | |
| 155 | hr = lerc_decode(pLercBlob, numBytesBlob, |
| 156 | 1, // nMasks, added in Lerc 3.0 to allow for different masks per band (get it from infoArr via lerc_getBlobInfo(...)) |
| 157 | maskByteImg2, 1, w, h, 1, (uint32)dt_float, (void*)zImg2); |
| 158 | |
| 159 | if (hr) |
| 160 | Failed("lerc_decode(...)", cntFailures); |
| 161 | |
| 162 | t1 = high_resolution_clock::now(); |
| 163 | duration = duration_cast<milliseconds>(t1 - t0).count(); |
| 164 | |
| 165 | // compare to orig |
| 166 | |
| 167 | bool bDiffDetected(false); |
| 168 | double maxDelta = 0; |
| 169 | for (int k = 0, i = 0; i < h; i++) |
| 170 | for (int j = 0; j < w; j++, k++) |
| 171 | { |
| 172 | if (maskByteImg2[k] != maskByteImg[k]) |
| 173 | bDiffDetected = true; |
| 174 | |
| 175 | if (maskByteImg2[k]) |
| 176 | { |
| 177 | double delta = abs((double)zImg2[k] - (double)zImg[k]); |
| 178 | if (delta > maxDelta) |
| 179 | maxDelta = delta; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | if (bDiffDetected) |
| 184 | { |
| 185 | std::cout << "Error in main: decoded valid bytes differ from encoded valid bytes" << endl; |
| 186 | cntFailures++; |
| 187 | } |
| 188 | |
| 189 | std::cout << "max z error per pixel = " << maxDelta << ", decode time = " << duration << " ms" << endl; |
| 190 | std::cout << endl; |
| 191 | |
| 192 | delete[] zImg; |
| 193 | delete[] zImg2; |
| 194 | delete[] maskByteImg; |
| 195 | delete[] maskByteImg2; |
| 196 | delete[] pLercBlob; |
| 197 | } |
| 198 | |
| 199 | //--------------------------------------------------------------------------- |
| 200 | |
| 201 | // Sample 2: random byte image, nDepth = 3, all pixels valid, maxZError = 0 (lossless) |
| 202 | { |
| 203 | int h = 713; |
| 204 | int w = 257; |
| 205 | |
| 206 | Byte* byteImg = new Byte[3 * w * h]; |
| 207 | memset(byteImg, 0, 3 * w * h); |
| 208 | |
| 209 | for (int k = 0, i = 0; i < h; i++) |
| 210 | for (int j = 0; j < w; j++, k++) |
| 211 | for (int m = 0; m < 3; m++) |
| 212 | byteImg[k * 3 + m] = rand() % 30; |
| 213 | |
| 214 | // encode |
| 215 | uint32 numBytesWritten = 0; |
| 216 | uint32 numBytesBlob = (3 * w * h) * 2; // should be enough |
| 217 | Byte* pLercBlob = new Byte[numBytesBlob]; |
| 218 | |
| 219 | t0 = high_resolution_clock::now(); |
| 220 | |
| 221 | hr = lerc_encode((void*)byteImg, // raw image data: nDepth values per pixel, row by row, band by band |
| 222 | (uint32)dt_uchar, 3, w, h, 1, |
| 223 | 0, // nMasks, added in Lerc 3.0 to allow for different masks per band |
| 224 | nullptr, // can pass nullptr if all pixels are valid and nMasks = 0 |
| 225 | 0, // max coding error per pixel |
| 226 | pLercBlob, // buffer to write to, function will fail if buffer too small |
| 227 | numBytesBlob, // buffer size |
| 228 | &numBytesWritten); // num bytes written to buffer |
| 229 | |
| 230 | if (hr) |
| 231 | Failed("lerc_encode(...)", cntFailures); |
| 232 | |
| 233 | t1 = high_resolution_clock::now(); |
| 234 | auto duration = duration_cast<milliseconds>(t1 - t0).count(); |
| 235 | |
| 236 | numBytesBlob = numBytesWritten; |
| 237 | double ratio = 3 * w * h / (double)numBytesBlob; |
| 238 | std::cout << "sample 2 compression ratio = " << ratio << ", encode time = " << duration << " ms" << endl; |
| 239 | |
| 240 | // decode |
| 241 | |
| 242 | if ((hr = lerc_getBlobInfo(pLercBlob, numBytesBlob, infoArr, dataRangeArr, infoArrSize, dataRangeArrSize))) |
| 243 | Failed("lerc_getBlobInfo(...)", cntFailures); |
| 244 | |
| 245 | BlobInfo_Print(infoArr); |
| 246 | |
| 247 | if (!BlobInfo_Equal(infoArr, 3, w, h, 1, (uint32)dt_uchar)) |
| 248 | { |
| 249 | std::cout << "got wrong lerc info" << endl; |
| 250 | cntFailures++; |
| 251 | } |
| 252 | |
| 253 | vector<double> zMinVec(3, 0), zMaxVec(3, 0); |
| 254 | if ((hr = lerc_getDataRanges(pLercBlob, numBytesBlob, 3, 1, &zMinVec[0], &zMaxVec[0]))) |
| 255 | Failed("lerc_getDataRanges(...)", cntFailures); |
| 256 | |
| 257 | // new data storage |
| 258 | Byte* byteImg2 = new Byte[3 * w * h]; |
| 259 | memset(byteImg2, 0, 3 * w * h); |
| 260 | |
| 261 | t0 = high_resolution_clock::now(); |
| 262 | |
| 263 | if ((hr = lerc_decode(pLercBlob, numBytesBlob, 0, nullptr, 3, w, h, 1, (uint32)dt_uchar, (void*)byteImg2))) |
| 264 | Failed("lerc_decode(...)", cntFailures); |
| 265 | |
| 266 | t1 = high_resolution_clock::now(); |
| 267 | duration = duration_cast<milliseconds>(t1 - t0).count(); |
| 268 | |
| 269 | // compare to orig |
| 270 | |
| 271 | int maxDelta = 0; |
| 272 | for (int k = 0, i = 0; i < h; i++) |
| 273 | for (int j = 0; j < w; j++, k++) |
| 274 | for (int m = 0; m < 3; m++) |
| 275 | { |
| 276 | int delta = abs((int)byteImg2[k * 3 + m] - (int)byteImg[k * 3 + m]); |
| 277 | if (delta > maxDelta) |
| 278 | maxDelta = delta; |
| 279 | } |
| 280 | |
| 281 | std::cout << "max z error per pixel = " << maxDelta << ", decode time = " << duration << " ms" << endl; |
| 282 | std::cout << endl; |
| 283 | |
| 284 | delete[] byteImg; |
| 285 | delete[] byteImg2; |
| 286 | delete[] pLercBlob; |
| 287 | } |
| 288 | |
| 289 | //--------------------------------------------------------------------------- |
| 290 | |
| 291 | // Sample 3: random float image, nBands = 4, no input mask, throw in some NaN pixels, maxZError = 0 (lossless) |
| 292 | // Lerc will replace the NaN as invalid pixels using byte masks, one mask per band, as needed |
| 293 | { |
| 294 | int h = 128; |
| 295 | int w = 257; |
| 296 | |
| 297 | float* fImg = new float[4 * w * h]; |
| 298 | memset(fImg, 0, 4 * w * h * sizeof(float)); |
| 299 | |
| 300 | for (int iBand = 0; iBand < 4; iBand++) |
| 301 | { |
| 302 | float* arr = &fImg[iBand * w * h]; |
| 303 | |
| 304 | for (int k = 0, i = 0; i < h; i++) |
| 305 | for (int j = 0; j < w; j++, k++) |
| 306 | { |
| 307 | arr[k] = sqrt((float)(i * i + j * j)); // smooth surface |
| 308 | arr[k] += rand() % 20; // add some small amplitude noise |
| 309 | |
| 310 | if (iBand != 2 && rand() > 0.5 * RAND_MAX) // set some NaN's but not for band 2 |
| 311 | arr[k] = NAN; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | // encode |
| 316 | uint32 numBytesNeeded = 0; |
| 317 | uint32 numBytesWritten = 0; |
| 318 | |
| 319 | // optional, can call encode directly with large enough buffer, |
| 320 | // but can be used to decide which method is best, Lerc or method bla |
| 321 | |
| 322 | hr = lerc_computeCompressedSize((void*)fImg, // raw image data: nDepth values per pixel, row by row, band by band |
| 323 | (uint32)dt_float, 1, w, h, 4, |
| 324 | 0, // nMasks, added in Lerc 3.0 to allow for different masks per band |
| 325 | nullptr, // can pass nullptr if all pixels are valid and nMasks = 0 |
| 326 | 0, // max coding error per pixel |
| 327 | &numBytesNeeded); // size of outgoing Lerc blob |
| 328 | |
| 329 | if (hr) |
| 330 | Failed("lerc_computeCompressedSize(...)", cntFailures); |
| 331 | |
| 332 | uint32 numBytesBlob = numBytesNeeded; |
| 333 | Byte* pLercBlob = new Byte[numBytesBlob]; |
| 334 | |
| 335 | t0 = high_resolution_clock::now(); |
| 336 | |
| 337 | hr = lerc_encode((void*)fImg, // raw image data: nDepth values per pixel, row by row, band by band |
| 338 | (uint32)dt_float, 1, w, h, 4, |
| 339 | 0, // nMasks, added in Lerc 3.0 to allow for different masks per band |
| 340 | nullptr, // can pass nullptr if all pixels are valid and nMasks = 0 |
| 341 | 0, // max coding error per pixel |
| 342 | pLercBlob, // buffer to write to, function will fail if buffer too small |
| 343 | numBytesBlob, // buffer size |
| 344 | &numBytesWritten); // num bytes written to buffer |
| 345 | |
| 346 | if (hr) |
| 347 | Failed("lerc_encode(...)", cntFailures); |
| 348 | |
| 349 | t1 = high_resolution_clock::now(); |
| 350 | auto duration = duration_cast<milliseconds>(t1 - t0).count(); |
| 351 | |
| 352 | double ratio = 4 * w * h * sizeof(float) / (double)numBytesBlob; |
| 353 | std::cout << "sample 3 compression ratio = " << ratio << ", encode time = " << duration << " ms" << endl; |
| 354 | |
| 355 | // decode |
| 356 | |
| 357 | if ((hr = lerc_getBlobInfo(pLercBlob, numBytesBlob, infoArr, dataRangeArr, infoArrSize, dataRangeArrSize))) |
| 358 | Failed("lerc_getBlobInfo(...)", cntFailures); |
| 359 | |
| 360 | BlobInfo_Print(infoArr); |
| 361 | |
| 362 | if (!BlobInfo_Equal(infoArr, 1, w, h, 4, (uint32)dt_float)) |
| 363 | { |
| 364 | std::cout << "got wrong lerc info" << endl; |
| 365 | cntFailures++; |
| 366 | } |
| 367 | |
| 368 | // new data storage |
| 369 | float* fImg2 = new float[4 * w * h]; |
| 370 | memset(fImg2, 0, 4 * w * h * sizeof(float)); |
| 371 | |
| 372 | t0 = high_resolution_clock::now(); |
| 373 | |
| 374 | int nMasks = infoArr[(int)LercNS::InfoArrOrder::nMasks]; // get the number of valid / invalid byte masks from infoArr |
| 375 | |
| 376 | Byte* maskByteImg2 = nullptr; |
| 377 | |
| 378 | if (nMasks > 0) // possible values are 0, 1, or nBands |
| 379 | { |
| 380 | maskByteImg2 = new Byte[nMasks * w * h]; |
| 381 | memset(maskByteImg2, 0, nMasks * w * h); |
| 382 | } |
| 383 | |
| 384 | if ((hr = lerc_decode(pLercBlob, numBytesBlob, nMasks, maskByteImg2, 1, w, h, 4, (uint32)dt_float, (void*)fImg2))) |
| 385 | Failed("lerc_decode(...)", cntFailures); |
| 386 | |
| 387 | t1 = high_resolution_clock::now(); |
| 388 | duration = duration_cast<milliseconds>(t1 - t0).count(); |
| 389 | |
| 390 | // compare to orig |
| 391 | |
| 392 | double maxDelta = 0; |
| 393 | for (int iBand = 0; iBand < 4; iBand++) |
| 394 | { |
| 395 | const float* arr = &fImg[iBand * w * h]; |
| 396 | const float* arr2 = &fImg2[iBand * w * h]; |
| 397 | const Byte* mask = (nMasks == 4) ? &maskByteImg2[iBand * w * h] |
| 398 | : ((nMasks == 1) ? maskByteImg2 : nullptr); |
| 399 | |
| 400 | for (int k = 0, i = 0; i < h; i++) |
| 401 | for (int j = 0; j < w; j++, k++) |
| 402 | if (!mask || mask[k]) |
| 403 | { |
| 404 | double delta = abs((double)arr2[k] - (double)arr[k]); |
| 405 | if (delta > maxDelta || std::isnan(delta)) |
| 406 | maxDelta = delta; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | std::cout << "max z error per pixel = " << maxDelta << ", decode time = " << duration << " ms" << endl; |
| 411 | std::cout << endl; |
| 412 | |
| 413 | delete[] fImg; |
| 414 | delete[] fImg2; |
| 415 | delete[] maskByteImg2; |
| 416 | delete[] pLercBlob; |
| 417 | } |
| 418 | |
| 419 | //--------------------------------------------------------------------------- |
| 420 | |
| 421 | // Sample 4: float image, nBands = 2, nDepth = 2, pass noData value to band 0, maxZError = 0.001, |
| 422 | // produce special mixed case of valid and invalid values at the same pixel; |
| 423 | // so here we need to use a noData value as the 2D byte masks cannot cover this case. |
| 424 | |
| 425 | // this example shows how to call the new Lerc API 4.0 functions which enable working with such mixed valid / invalid cases, |
| 426 | // and allow to pass a noData value for that, one per band; |
| 427 | { |
| 428 | int h = 128; |
| 429 | int w = 65; |
| 430 | const int nBands = 2; |
| 431 | const int nDepth = 2; // might be a complex image |
| 432 | int nValues = nDepth * w * h * nBands; |
| 433 | |
| 434 | float noDataVal = FLT_MAX; |
| 435 | double maxZErr = 0.001; |
| 436 | |
| 437 | // here, the 1st band uses a noData value, the 2nd band does not; |
| 438 | // noData value can be passed in addition to a byte mask, it is not one or the other; |
| 439 | Byte bUsesNoDataArr[nBands] = { 1, 0 }; |
| 440 | double noDataArr[nBands] = { noDataVal, 0 }; |
| 441 | |
| 442 | float* fImg = new float[nValues]; |
| 443 | memset(fImg, 0, nValues * sizeof(float)); |
| 444 | |
| 445 | for (int iBand = 0; iBand < nBands; iBand++) |
| 446 | { |
| 447 | float* arr = &fImg[nDepth * w * h * iBand]; |
| 448 | |
| 449 | for (int k = 0, i = 0; i < h; i++) |
| 450 | for (int j = 0; j < w; j++, k++) |
| 451 | { |
| 452 | int m = k * nDepth; |
| 453 | arr[m] = sqrt((float)(i * i + j * j)); // smooth surface |
| 454 | arr[m] += rand() % 20; // add some small amplitude noise |
| 455 | |
| 456 | arr[m + 1] = arr[m]; |
| 457 | |
| 458 | if (iBand == 0 && rand() > 0.5 * RAND_MAX) // set some values to noData for band 0 |
| 459 | arr[m] = noDataVal; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | // encode |
| 464 | uint32 numBytesNeeded = 0; |
| 465 | uint32 numBytesWritten = 0; |
| 466 | |
| 467 | hr = lerc_computeCompressedSize_4D((void*)fImg, // raw image data: nDepth values per pixel, row by row, band by band |
| 468 | (uint32)dt_float, nDepth, w, h, nBands, |
| 469 | 0, // nMasks, added in Lerc 3.0 to allow for different masks per band |
| 470 | nullptr, // can pass nullptr if all pixels are valid and nMasks = 0 |
| 471 | maxZErr, // max coding error per pixel |
| 472 | &numBytesNeeded, // size of outgoing Lerc blob |
| 473 | bUsesNoDataArr, // array of size nBands, set to 1 for each band that uses noData value |
| 474 | noDataArr); // array of size nBands, set noData value for each band that uses it |
| 475 | |
| 476 | if (hr) |
| 477 | Failed("lerc_computeCompressedSize(...)", cntFailures); |
| 478 | |
| 479 | uint32 numBytesBlob = numBytesNeeded; |
| 480 | Byte* pLercBlob = new Byte[numBytesBlob]; |
| 481 | |
| 482 | t0 = high_resolution_clock::now(); |
| 483 | |
| 484 | hr = lerc_encode_4D((void*)fImg, // raw image data: nDepth values per pixel, row by row, band by band |
| 485 | (uint32)dt_float, nDepth, w, h, nBands, |
| 486 | 0, // nMasks, added in Lerc 3.0 to allow for different masks per band |
| 487 | nullptr, // can pass nullptr if all pixels are valid and nMasks = 0 |
| 488 | maxZErr, // max coding error per pixel |
| 489 | pLercBlob, // buffer to write to, function will fail if buffer too small |
| 490 | numBytesBlob, // buffer size |
| 491 | &numBytesWritten, // num bytes written to buffer |
| 492 | bUsesNoDataArr, // array of size nBands, set to 1 for each band that uses noData value |
| 493 | noDataArr); // array of size nBands, set noData value for each band that uses it |
| 494 | |
| 495 | if (hr) |
| 496 | Failed("lerc_encode(...)", cntFailures); |
| 497 | |
| 498 | t1 = high_resolution_clock::now(); |
| 499 | auto duration = duration_cast<milliseconds>(t1 - t0).count(); |
| 500 | |
| 501 | double ratio = nValues * sizeof(float) / (double)numBytesBlob; |
| 502 | std::cout << "sample 4 compression ratio = " << ratio << ", encode time = " << duration << " ms" << endl; |
| 503 | |
| 504 | // decode |
| 505 | |
| 506 | if ((hr = lerc_getBlobInfo(pLercBlob, numBytesBlob, infoArr, dataRangeArr, infoArrSize, dataRangeArrSize))) |
| 507 | Failed("lerc_getBlobInfo(...)", cntFailures); |
| 508 | |
| 509 | BlobInfo_Print(infoArr); |
| 510 | |
| 511 | if (!BlobInfo_Equal(infoArr, nDepth, w, h, nBands, (uint32)dt_float)) |
| 512 | { |
| 513 | std::cout << "got wrong lerc info" << endl; |
| 514 | cntFailures++; |
| 515 | } |
| 516 | |
| 517 | // new data storage |
| 518 | float* fImg2 = new float[nValues]; |
| 519 | memset(fImg2, 0, nValues * sizeof(float)); |
| 520 | |
| 521 | t0 = high_resolution_clock::now(); |
| 522 | |
| 523 | // Lerc converts noData values to byte mask wherever possible |
| 524 | int nMasks = infoArr[(int)LercNS::InfoArrOrder::nMasks]; // get the number of valid / invalid byte masks from infoArr |
| 525 | int nUsesNoData = infoArr[(int)LercNS::InfoArrOrder::nUsesNoDataValue]; // 0 - noData value is not used, nBands - noData value used in one or more bands |
| 526 | |
| 527 | Byte* maskByteImg2 = nullptr; |
| 528 | |
| 529 | if (nMasks > 0) |
| 530 | { |
| 531 | maskByteImg2 = new Byte[nMasks * w * h]; |
| 532 | memset(maskByteImg2, 0, nMasks * w * h); |
| 533 | } |
| 534 | |
| 535 | vector<Byte> bUsesNoDataVec(nBands, 0); |
| 536 | vector<double> noDataVec(nBands, 0); |
| 537 | |
| 538 | hr = lerc_decode_4D(pLercBlob, numBytesBlob, nMasks, maskByteImg2, nDepth, w, h, nBands, (uint32)dt_float, (void*)fImg2, |
| 539 | &bUsesNoDataVec[0], &noDataVec[0]); |
| 540 | |
| 541 | if (hr) |
| 542 | Failed("lerc_decode(...)", cntFailures); |
| 543 | |
| 544 | t1 = high_resolution_clock::now(); |
| 545 | duration = duration_cast<milliseconds>(t1 - t0).count(); |
| 546 | |
| 547 | // compare to orig |
| 548 | |
| 549 | double maxDelta = 0; |
| 550 | bool bNoDataValueHasChanged = false; |
| 551 | |
| 552 | for (int iBand = 0; iBand < nBands; iBand++) |
| 553 | { |
| 554 | if (bUsesNoDataVec[iBand]) // this band uses a noData value; only possible if nDepth > 1 and some pixel has a mix of valid and invalid values |
| 555 | { |
| 556 | if (!bUsesNoDataArr[iBand]) // that would be a bug |
| 557 | printf("Error: band %d changed from not using noData to using noData!\n", iBand); |
| 558 | |
| 559 | if (noDataVec[iBand] != noDataArr[iBand]) |
| 560 | printf("Error: noData value changed for band %d!\n", iBand); |
| 561 | } |
| 562 | |
| 563 | const float* arr = &fImg[nDepth * w * h * iBand]; |
| 564 | const float* arr2 = &fImg2[nDepth * w * h * iBand]; |
| 565 | |
| 566 | const Byte* mask = (nMasks == nBands) ? &maskByteImg2[iBand * w * h] |
| 567 | : ((nMasks == 1) ? maskByteImg2 : nullptr); |
| 568 | |
| 569 | for (int k = 0, i = 0; i < h; i++) |
| 570 | for (int j = 0; j < w; j++, k++) |
| 571 | if (!mask || mask[k]) |
| 572 | { |
| 573 | int m0 = k * nDepth; |
| 574 | for (int m = m0; m < m0 + nDepth; m++) |
| 575 | { |
| 576 | double delta = abs((double)arr2[m] - (double)arr[m]); |
| 577 | |
| 578 | if (bUsesNoDataVec[iBand] && (arr[m] == noDataVal) && delta > 0) |
| 579 | bNoDataValueHasChanged = true; |
| 580 | |
| 581 | if (delta > maxDelta) |
| 582 | maxDelta = delta; |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | if (bNoDataValueHasChanged) |
| 588 | { |
| 589 | std::cout << "Error: some noData value has changed!" << endl; |
| 590 | cntFailures++; |
| 591 | } |
| 592 | |
| 593 | std::cout << "max z error per pixel = " << maxDelta << ", decode time = " << duration << " ms" << endl; |
| 594 | std::cout << endl; |
| 595 | |
| 596 | delete[] fImg; |
| 597 | delete[] fImg2; |
| 598 | delete[] maskByteImg2; |
| 599 | delete[] pLercBlob; |
| 600 | } |
| 601 | |
| 602 | //--------------------------------------------------------------------------- |
| 603 | //--------------------------------------------------------------------------- |
| 604 | |
| 605 | #endif // TestLegacyData |
| 606 | |
| 607 | #ifdef TestLegacyData |
| 608 | |
| 609 | size_t lercBufferSize = 8 * 1024 * 1024; // 8 MB |
| 610 | Byte* pLercBuffer = new Byte[lercBufferSize]; |
| 611 | |
| 612 | double totalDecodeTime = 0; |
| 613 | |
| 614 | vector<string> fnVec; |
| 615 | string path = "D:/GitHub/LercOpenSource_v2.5/testData/"; // windows |
| 616 | string pathDecoded = "C:/Temp/LercDecodedTiles/"; |
| 617 | //string path = "/home/john/Documents/Code/TestData/"; // ubuntu |
| 618 | //string pathDecoded = "/home/john/Documents/Code/DecodedTiles/"; |
| 619 | #ifdef USE_EMSCRIPTEN |
| 620 | path = "PreLoadedFiles/LercBlobs/"; |
| 621 | pathDecoded = "PreLoadedFiles/DecodedTiles/"; |
| 622 | #endif |
| 623 | |
| 624 | string listFn = path + "_list.txt"; |
| 625 | if (!ReadListFile(listFn, fnVec)) |
| 626 | cout << "Read file " << listFn << " failed." << endl; |
| 627 | |
| 628 | //fnVec.clear(); |
| 629 | //fnVec.push_back("ShortBlob/broken_l2v2.lerc22"); |
| 630 | |
| 631 | for (size_t n = 0; n < fnVec.size(); n++) |
| 632 | { |
| 633 | string fn = path; |
| 634 | fn += fnVec[n]; |
| 635 | |
| 636 | size_t fileSize = 0; |
| 637 | if (!ReadFile(fn, pLercBuffer, lercBufferSize, fileSize)) |
| 638 | { |
| 639 | cout << "Read file " << fn << " failed." << endl; |
| 640 | continue; |
| 641 | } |
| 642 | |
| 643 | if ((hr = lerc_getBlobInfo(pLercBuffer, (uint32)fileSize, infoArr, dataRangeArr, infoArrSize, dataRangeArrSize))) |
| 644 | Failed("lerc_getBlobInfo(...)", cntFailures); |
| 645 | |
| 646 | { |
| 647 | int codecVersion = infoArr[0]; |
| 648 | uint32 dt = infoArr[1]; |
| 649 | int nDepth = infoArr[2]; |
| 650 | int w = infoArr[3]; |
| 651 | int h = infoArr[4]; |
| 652 | int nBands = infoArr[5]; |
| 653 | |
| 654 | int nMasks = infoArr[(int)LercNS::InfoArrOrder::nMasks]; // get the number of valid / invalid byte masks from infoArr |
| 655 | int nUsesNoData = infoArr[(int)LercNS::InfoArrOrder::nUsesNoDataValue]; // if > 0, noData value must be honored in addition to the byte mask |
| 656 | |
| 657 | double zMin = dataRangeArr[0]; |
| 658 | double zMax = dataRangeArr[1]; |
| 659 | |
| 660 | const int bpp[] = { 1, 1, 2, 2, 4, 4, 4, 8 }; |
| 661 | |
| 662 | Byte* pDstArr = new Byte[nBands * w * h * nDepth * bpp[dt]]; |
| 663 | Byte* pValidBytes = new Byte[nMasks * w * h]; |
| 664 | |
| 665 | // allow for noData values, one per band |
| 666 | Byte* pUsesNoData = new Byte[nBands]; |
| 667 | double* pNoDataVal = new double[nBands]; |
| 668 | |
| 669 | t0 = high_resolution_clock::now(); |
| 670 | |
| 671 | string resultMsg = "ok"; |
| 672 | if ((hr = lerc_decode_4D(pLercBuffer, (uint32)fileSize, nMasks, pValidBytes, nDepth, w, h, nBands, dt, (void*)pDstArr, pUsesNoData, pNoDataVal))) |
| 673 | resultMsg = "FAILED"; |
| 674 | |
| 675 | //memset(pDstArr, 5, nBands * w * h * nDepth * bpp[dt]); // to double check on the CompareTwoTiles() function |
| 676 | |
| 677 | t1 = high_resolution_clock::now(); |
| 678 | auto duration = duration_cast<milliseconds>(t1 - t0).count(); |
| 679 | totalDecodeTime += duration; |
| 680 | |
| 681 | printf("codec = %1d, nDepth = %1d, w = %4d, h = %4d, nBands = %1d, nMasks = %1d, usesNoData = %1d, dt = %1d, min = %10.4lf, max = %16.4lf, time = %4d ms, %s : %s\n", |
| 682 | codecVersion, nDepth, w, h, nBands, nMasks, nUsesNoData ? 1 : 0, dt, zMin, zMax, (int)duration, resultMsg.c_str(), fnVec[n].c_str()); |
| 683 | |
| 684 | string fnDec = pathDecoded; |
| 685 | string s = fnVec[n]; |
| 686 | replace(s.begin(), s.end(), '/', '_'); |
| 687 | fnDec += s; |
| 688 | |
| 689 | #ifdef DumpDecodedTiles |
| 690 | if (!WriteDecodedTile(fnDec, nMasks, pValidBytes, nDepth, w, h, nBands, bpp[dt], pDstArr)) |
| 691 | { |
| 692 | printf("Write decoded Lerc tile failed for %s\n", fnDec.c_str()); |
| 693 | cntFailures++; |
| 694 | } |
| 695 | #endif |
| 696 | |
| 697 | #ifdef CompareAgainstDumpedTiles |
| 698 | Byte* pDstArr2 = new Byte[nBands * w * h * nDepth * bpp[dt]]; |
| 699 | Byte* pValidBytes2 = new Byte[nMasks * w * h]; |
| 700 | |
| 701 | int nMasks2(0), nDepth2(0), w2(0), h2(0), nBands2(0), bpp2(0); |
| 702 | if (!ReadDecodedTile(fnDec, nMasks2, pValidBytes2, nDepth2, w2, h2, nBands2, bpp2, pDstArr2)) |
| 703 | { |
| 704 | printf("Read decoded Lerc tile failed for %s\n", fnDec.c_str()); |
| 705 | cntFailures++; |
| 706 | } |
| 707 | |
| 708 | if (!CompareTwoTiles(nMasks, pValidBytes, nDepth, w, h, nBands, bpp[dt], pDstArr, |
| 709 | nMasks2, pValidBytes2, nDepth2, w2, h2, nBands2, bpp2, pDstArr2, dt)) |
| 710 | { |
| 711 | printf("Compare two Lerc tiles failed for %s vs %s\n", fnVec[n].c_str(), fnDec.c_str()); |
| 712 | cntFailures++; |
| 713 | } |
| 714 | |
| 715 | delete[] pDstArr2; |
| 716 | delete[] pValidBytes2; |
| 717 | #endif |
| 718 | |
| 719 | delete[] pDstArr; |
| 720 | delete[] pValidBytes; |
| 721 | |
| 722 | delete[] pUsesNoData; |
| 723 | delete[] pNoDataVal; |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | delete[] pLercBuffer; |
| 728 | |
| 729 | printf("Total decode time = %d ms", (int)totalDecodeTime); |
| 730 | |
| 731 | #endif |
| 732 | |
| 733 | if (cntFailures) |
| 734 | printf("\nSUMMARY: %d failures.\n", cntFailures); |
| 735 | else |
| 736 | printf("\nSUMMARY: all good.\n"); |
| 737 | |
| 738 | if (!getenv("LERCTEST_NONINTERACTIVE")) |
| 739 | { |
| 740 | printf("\npress ENTER\n"); |
| 741 | getchar(); |
| 742 | } |
| 743 | |
| 744 | return cntFailures ? 1 : 0; |
| 745 | } |
| 746 | |
| 747 | //----------------------------------------------------------------------------- |
| 748 | //----------------------------------------------------------------------------- |
| 749 | |
| 750 | void BlobInfo_Print(const uint32* infoArr) |
| 751 | { |
| 752 | const uint32* ia = infoArr; |
| 753 | printf("version = %d, dataType = %d, nDepth = %d, nCols = %d, nRows = %d, nBands = %d, \ |
| 754 | nValidPixels = %d, blobSize = %d, nMasks = %d, nDepth = %d, nUsesNoDataValue = %d\n", |
| 755 | ia[0], ia[1], ia[2], ia[3], ia[4], ia[5], ia[6], ia[7], ia[8], ia[9], ia[10]); |
| 756 | } |
| 757 | |
| 758 | bool BlobInfo_Equal(const uint32* infoArr, uint32 nDepth, uint32 nCols, uint32 nRows, uint32 nBands, uint32 dataType) |
| 759 | { |
| 760 | const uint32* ia = infoArr; |
| 761 | return ia[1] == dataType && ia[2] == nDepth && ia[3] == nCols && ia[4] == nRows && ia[5] == nBands; |
| 762 | } |
| 763 | |
| 764 | //----------------------------------------------------------------------------- |
| 765 | |
| 766 | #ifdef TestLegacyData |
| 767 | |
| 768 | bool ReadListFile(const string& listFn, vector<string>& fnVec) |
| 769 | { |
| 770 | FILE* fp = fopen(listFn.c_str(), "r"); |
| 771 | if (!fp) |
| 772 | { |
| 773 | printf("Cannot open file %s\n", listFn.c_str()); |
| 774 | return false; |
| 775 | } |
| 776 | |
| 777 | char buffer[1024]; |
| 778 | int num = 0; |
| 779 | int rv = fscanf(fp, "%d", &num); |
| 780 | for (int i = 0; i < num; i++) |
| 781 | { |
| 782 | rv = fscanf(fp, "%s", buffer); |
| 783 | string s = buffer; |
| 784 | fnVec.push_back(s); |
| 785 | } |
| 786 | |
| 787 | fclose(fp); |
| 788 | return true; |
| 789 | } |
| 790 | |
| 791 | bool ReadFile(const string& fn, Byte* pBuffer, size_t bufferSize, size_t& nBytesRead) |
| 792 | { |
| 793 | nBytesRead = 0; |
| 794 | FILE* fp = fopen(fn.c_str(), "rb"); |
| 795 | if (!fp) |
| 796 | { |
| 797 | printf("Cannot open file %s\n", fn.c_str()); |
| 798 | return false; |
| 799 | } |
| 800 | |
| 801 | fseek(fp, 0, SEEK_END); |
| 802 | int rv = ftell(fp); // get the file size |
| 803 | if (rv <= 0) |
| 804 | { |
| 805 | fclose(fp); |
| 806 | return false; |
| 807 | } |
| 808 | |
| 809 | size_t fileSize = (size_t)rv; |
| 810 | |
| 811 | if (fileSize > bufferSize) |
| 812 | { |
| 813 | printf("Lerc blob size %d > buffer size of %d\n", (uint32)fileSize, (uint32)bufferSize); |
| 814 | fclose(fp); |
| 815 | return false; |
| 816 | } |
| 817 | |
| 818 | rewind(fp); |
| 819 | nBytesRead = fread(pBuffer, 1, fileSize, fp); // read Lerc blob into buffer |
| 820 | fclose(fp); |
| 821 | return nBytesRead == fileSize; |
| 822 | } |
| 823 | |
| 824 | bool WriteDecodedTile(const string& fn, int nMasks, const Byte* pValidBytes, int nDepth, int w, int h, int nBands, int bpp, const void* pArr) |
| 825 | { |
| 826 | FILE* fp = fopen(fn.c_str(), "wb"); |
| 827 | if (!fp) |
| 828 | { |
| 829 | printf("Cannot open file %s\n", fn.c_str()); |
| 830 | return false; |
| 831 | } |
| 832 | |
| 833 | int dimensions[] = { nMasks, nDepth, w, h, nBands, bpp }; |
| 834 | fwrite(dimensions, 1, 6 * sizeof(int), fp); |
| 835 | |
| 836 | if (nMasks > 0) |
| 837 | fwrite(pValidBytes, 1, nMasks * w * h, fp); |
| 838 | |
| 839 | fwrite(pArr, 1, nBands * w * h * nDepth * bpp, fp); |
| 840 | fclose(fp); |
| 841 | return true; |
| 842 | } |
| 843 | |
| 844 | bool ReadDecodedTile(const string& fn, int& nMasks, Byte* pValidBytes, int& nDepth, int& w, int& h, int& nBands, int& bpp, void* pArr) |
| 845 | { |
| 846 | FILE* fp = fopen(fn.c_str(), "rb"); |
| 847 | if (!fp) |
| 848 | { |
| 849 | printf("Cannot open file %s\n", fn.c_str()); |
| 850 | return false; |
| 851 | } |
| 852 | |
| 853 | int dims[6] = { 0 }; |
| 854 | size_t nbRead = fread(dims, 1, 6 * sizeof(int), fp); |
| 855 | nMasks = dims[0]; |
| 856 | nDepth = dims[1]; |
| 857 | w = dims[2]; |
| 858 | h = dims[3]; |
| 859 | nBands = dims[4]; |
| 860 | bpp = dims[5]; |
| 861 | |
| 862 | if (nMasks > 0) |
| 863 | nbRead += fread(pValidBytes, 1, nMasks * w * h, fp); |
| 864 | |
| 865 | nbRead += fread(pArr, 1, nBands * w * h * nDepth * bpp, fp); |
| 866 | fclose(fp); |
| 867 | return true; |
| 868 | } |
| 869 | |
| 870 | bool CompareTwoTiles(int nMasks, const Byte* pValidBytes, int nDepth, int w, int h, int nBands, int bpp, const void* pArr, |
| 871 | int nMasks2, const Byte* pValidBytes2, int nDepth2, int w2, int h2, int nBands2, int bpp2, const void* pArr2, int dt) |
| 872 | { |
| 873 | if (nMasks2 != nMasks || nDepth2 != nDepth || w2 != w || h2 != h || nBands2 != nBands || bpp2 != bpp) |
| 874 | { |
| 875 | printf("Tile dimensions differ.\n"); |
| 876 | return false; |
| 877 | } |
| 878 | if (0 != memcmp(pValidBytes2, pValidBytes, nMasks * w * h)) |
| 879 | { |
| 880 | printf("Valid byte masks differ.\n"); |
| 881 | return false; |
| 882 | } |
| 883 | |
| 884 | double maxDelta(0); |
| 885 | |
| 886 | for (int iBand = 0; iBand < nBands; iBand++) |
| 887 | { |
| 888 | const Byte* mask = (nMasks == nBands) ? &pValidBytes[iBand * w * h] : (nMasks == 1 ? pValidBytes : nullptr); |
| 889 | |
| 890 | for (int k = 0, i = 0; i < h; i++) |
| 891 | { |
| 892 | for (int j = 0; j < w; j++, k++) |
| 893 | { |
| 894 | if (!mask || mask[k]) |
| 895 | { |
| 896 | for (int m = 0; m < nDepth; m++) |
| 897 | { |
| 898 | int index = (iBand * w * h + k) * nDepth + m; |
| 899 | double delta = 0; |
| 900 | |
| 901 | const Byte* ptr = (const Byte*)pArr + index * bpp; |
| 902 | const Byte* ptr2 = (const Byte*)pArr2 + index * bpp; |
| 903 | |
| 904 | if (dt <= 5) |
| 905 | { |
| 906 | if (0 != memcmp(ptr, ptr2, bpp)) |
| 907 | { |
| 908 | printf("Data differ.\n"); |
| 909 | return false; |
| 910 | } |
| 911 | } |
| 912 | else if (dt == 6) |
| 913 | { |
| 914 | float x(0), x2(0); |
| 915 | memcpy(&x, ptr, bpp); |
| 916 | memcpy(&x2, ptr2, bpp); |
| 917 | delta = fabs(x2 - x); |
| 918 | } |
| 919 | else if (dt == 7) |
| 920 | { |
| 921 | double x(0), x2(0); |
| 922 | memcpy(&x, ptr, bpp); |
| 923 | memcpy(&x2, ptr2, bpp); |
| 924 | delta = fabs(x2 - x); |
| 925 | } |
| 926 | |
| 927 | if (delta > maxDelta) |
| 928 | maxDelta = delta; |
| 929 | } |
| 930 | } |
| 931 | } |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | if (maxDelta > 0) |
| 936 | { |
| 937 | printf("Valid pixel values differ, max delta = %lf.\n", maxDelta); |
| 938 | return false; |
| 939 | } |
| 940 | |
| 941 | return true; |
| 942 | } |
| 943 | |
| 944 | #endif |
| 945 | |
| 946 | //----------------------------------------------------------------------------- |
| 947 |