Debian Codegraph @

src/LercLib/Lerc.cpp

raw symbols SCIP
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 "Defines.h"cxx . . $ `<file>/src/LercLib/Defines.h`/
25 #include "Lerc.h"cxx . . $ `<file>/src/LercLib/Lerc.h`/
26 #include "Lerc2.h"cxx . . $ `<file>/src/LercLib/Lerc2.h`/
27 #include <cstring>
28 #include <functional>
29 #include <limits>
30 #include <typeinfo>
31
32 #ifdef HAVE_LERC1_DECODEcxx . . $ `src/LercLib/Defines.h:39:9`!
33 #include "Lerc1Decode/CntZImage.h"cxx . . $ `<file>/src/LercLib/Lerc1Decode/CntZImage.h`/
34 #endif
35
36 using namespace std;
37 USING_NAMESPACE_LERCcxx . . $ `src/LercLib/Defines.h:37:9`!
38
39 // -------------------------------------------------------------------------- ;
40
41 ErrCodecxx . . $ LercNS/ErrCode# Lerccxx . . $ LercNS/Lerc#::ComputeCompressedSizecxx . . $ LercNS/Lerc#ComputeCompressedSize(907a79ad428f5081).(const void* pData, int version, DataTypecxx . . $ LercNS/Lerc#DataType# dt, int nDepth, int nCols, int nRows, int nBands,
42 int nMasks, const Bytecxx . . $ LercNS/Byte#* pValidBytes, double maxZErr, unsigned int& numBytesNeeded, const unsigned char* pUsesNoData, const double* noDataValues)
43 {
44 #define LERC_ARG_1cxx . . $ `src/LercLib/Lerc.cpp:44:9`! version, nDepth, nCols, nRows, nBands, nMasks, pValidBytes, maxZErr, numBytesNeeded, pUsesNoData, noDataValues
45
46 switch (dt)
47 {
48 case DT_Charcxx . . $ LercNS/Lerc#DT_Char.: return ComputeCompressedSizeTemplcxx . . $ LercNS/Lerc#ComputeCompressedSizeTempl(40cc20dd7dade3bf).((const signed char*)pData, LERC_ARG_1cxx . . $ `src/LercLib/Lerc.cpp:44:9`!);
49 case DT_Bytecxx . . $ LercNS/Lerc#DT_Byte.: return ComputeCompressedSizeTemplcxx . . $ LercNS/Lerc#ComputeCompressedSizeTempl(40cc20dd7dade3bf).((const Bytecxx . . $ LercNS/Byte#*)pData, LERC_ARG_1cxx . . $ `src/LercLib/Lerc.cpp:44:9`!);
50 case DT_Shortcxx . . $ LercNS/Lerc#DT_Short.: return ComputeCompressedSizeTemplcxx . . $ LercNS/Lerc#ComputeCompressedSizeTempl(40cc20dd7dade3bf).((const short*)pData, LERC_ARG_1cxx . . $ `src/LercLib/Lerc.cpp:44:9`!);
51 case DT_UShortcxx . . $ LercNS/Lerc#DT_UShort.: return ComputeCompressedSizeTemplcxx . . $ LercNS/Lerc#ComputeCompressedSizeTempl(40cc20dd7dade3bf).((const unsigned short*)pData, LERC_ARG_1cxx . . $ `src/LercLib/Lerc.cpp:44:9`!);
52 case DT_Intcxx . . $ LercNS/Lerc#DT_Int.: return ComputeCompressedSizeTemplcxx . . $ LercNS/Lerc#ComputeCompressedSizeTempl(40cc20dd7dade3bf).((const int*)pData, LERC_ARG_1cxx . . $ `src/LercLib/Lerc.cpp:44:9`!);
53 case DT_UIntcxx . . $ LercNS/Lerc#DT_UInt.: return ComputeCompressedSizeTemplcxx . . $ LercNS/Lerc#ComputeCompressedSizeTempl(40cc20dd7dade3bf).((const unsigned int*)pData, LERC_ARG_1cxx . . $ `src/LercLib/Lerc.cpp:44:9`!);
54 case DT_Floatcxx . . $ LercNS/Lerc#DT_Float.: return ComputeCompressedSizeTemplcxx . . $ LercNS/Lerc#ComputeCompressedSizeTempl(40cc20dd7dade3bf).((const float*)pData, LERC_ARG_1cxx . . $ `src/LercLib/Lerc.cpp:44:9`!);
55 case DT_Doublecxx . . $ LercNS/Lerc#DT_Double.: return ComputeCompressedSizeTemplcxx . . $ LercNS/Lerc#ComputeCompressedSizeTempl(40cc20dd7dade3bf).((const double*)pData, LERC_ARG_1cxx . . $ `src/LercLib/Lerc.cpp:44:9`!);
56
57 default:
58 return ErrCodecxx . . $ LercNS/ErrCode#::WrongParamcxx . . $ LercNS/ErrCode#WrongParam.;
59 }
60
61 #undef LERC_ARG_1cxx . . $ `src/LercLib/Lerc.cpp:44:9`!
62 }
63
64 // -------------------------------------------------------------------------- ;
65
66 ErrCodecxx . . $ LercNS/ErrCode# Lerccxx . . $ LercNS/Lerc#::Encodecxx . . $ LercNS/Lerc#Encode(5806e23373902463).(const void* pData, int version, DataTypecxx . . $ LercNS/Lerc#DataType# dt, int nDepth, int nCols, int nRows, int nBands,
67 int nMasks, const Bytecxx . . $ LercNS/Byte#* pValidBytes, double maxZErr, Bytecxx . . $ LercNS/Byte#* pBuffer, unsigned int numBytesBuffer,
68 unsigned int& numBytesWritten, const unsigned char* pUsesNoData, const double* noDataValues)
69 {
70 #define LERC_ARG_2cxx . . $ `src/LercLib/Lerc.cpp:70:9`! version, nDepth, nCols, nRows, nBands, nMasks, pValidBytes, maxZErr, pBuffer, numBytesBuffer, numBytesWritten, pUsesNoData, noDataValues
71
72 switch (dt)
73 {
74 case DT_Charcxx . . $ LercNS/Lerc#DT_Char.: return EncodeTemplcxx . . $ LercNS/Lerc#EncodeTempl(402103a7adc54e8c).((const signed char*)pData, LERC_ARG_2cxx . . $ `src/LercLib/Lerc.cpp:70:9`!);
75 case DT_Bytecxx . . $ LercNS/Lerc#DT_Byte.: return EncodeTemplcxx . . $ LercNS/Lerc#EncodeTempl(402103a7adc54e8c).((const Bytecxx . . $ LercNS/Byte#*)pData, LERC_ARG_2cxx . . $ `src/LercLib/Lerc.cpp:70:9`!);
76 case DT_Shortcxx . . $ LercNS/Lerc#DT_Short.: return EncodeTemplcxx . . $ LercNS/Lerc#EncodeTempl(402103a7adc54e8c).((const short*)pData, LERC_ARG_2cxx . . $ `src/LercLib/Lerc.cpp:70:9`!);
77 case DT_UShortcxx . . $ LercNS/Lerc#DT_UShort.: return EncodeTemplcxx . . $ LercNS/Lerc#EncodeTempl(402103a7adc54e8c).((const unsigned short*)pData, LERC_ARG_2cxx . . $ `src/LercLib/Lerc.cpp:70:9`!);
78 case DT_Intcxx . . $ LercNS/Lerc#DT_Int.: return EncodeTemplcxx . . $ LercNS/Lerc#EncodeTempl(402103a7adc54e8c).((const int*)pData, LERC_ARG_2cxx . . $ `src/LercLib/Lerc.cpp:70:9`!);
79 case DT_UIntcxx . . $ LercNS/Lerc#DT_UInt.: return EncodeTemplcxx . . $ LercNS/Lerc#EncodeTempl(402103a7adc54e8c).((const unsigned int*)pData, LERC_ARG_2cxx . . $ `src/LercLib/Lerc.cpp:70:9`!);
80 case DT_Floatcxx . . $ LercNS/Lerc#DT_Float.: return EncodeTemplcxx . . $ LercNS/Lerc#EncodeTempl(402103a7adc54e8c).((const float*)pData, LERC_ARG_2cxx . . $ `src/LercLib/Lerc.cpp:70:9`!);
81 case DT_Doublecxx . . $ LercNS/Lerc#DT_Double.: return EncodeTemplcxx . . $ LercNS/Lerc#EncodeTempl(402103a7adc54e8c).((const double*)pData, LERC_ARG_2cxx . . $ `src/LercLib/Lerc.cpp:70:9`!);
82
83 default:
84 return ErrCodecxx . . $ LercNS/ErrCode#::WrongParamcxx . . $ LercNS/ErrCode#WrongParam.;
85 }
86
87 #undef LERC_ARG_2cxx . . $ `src/LercLib/Lerc.cpp:70:9`!
88 }
89
90 // -------------------------------------------------------------------------- ;
91
92 ErrCodecxx . . $ LercNS/ErrCode# Lerccxx . . $ LercNS/Lerc#::GetLercInfocxx . . $ LercNS/Lerc#GetLercInfo(db35de8f40228c29).(const Bytecxx . . $ LercNS/Byte#* pLercBlob, unsigned int numBytesBlob, struct LercInfocxx . . $ LercNS/Lerc#LercInfo#& lercInfo, double* pMins, double* pMaxs, size_t nElem)
93 {
94 lercInfo.RawInitcxx . . $ LercNS/Lerc#LercInfo#RawInit(49f6e7a06ebc5aa8).();
95
96 // first try Lerc2
97 struct Lerc2cxx . . $ LercNS/Lerc2#::HeaderInfocxx . . $ LercNS/Lerc2#HeaderInfo# lerc2Info;
98 bool bHasMask = false;
99 int nMasks = 0;
100
101 if (Lerc2cxx . . $ LercNS/Lerc2#::GetHeaderInfocxx . . $ LercNS/Lerc2#GetHeaderInfo(5bafa162245ba3da).(pLercBlob, numBytesBlob, lerc2Info, bHasMask))
102 {
103 if (lerc2Info.blobSizecxx . . $ LercNS/Lerc2#HeaderInfo#blobSize. < 0)
104 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
105
106 lercInfo.versioncxx . . $ LercNS/Lerc#LercInfo#version. = lerc2Info.versioncxx . . $ LercNS/Lerc2#HeaderInfo#version.;
107 lercInfo.nDepthcxx . . $ LercNS/Lerc#LercInfo#nDepth. = lerc2Info.nDepthcxx . . $ LercNS/Lerc2#HeaderInfo#nDepth.;
108 lercInfo.nColscxx . . $ LercNS/Lerc#LercInfo#nCols. = lerc2Info.nColscxx . . $ LercNS/Lerc2#HeaderInfo#nCols.;
109 lercInfo.nRowscxx . . $ LercNS/Lerc#LercInfo#nRows. = lerc2Info.nRowscxx . . $ LercNS/Lerc2#HeaderInfo#nRows.;
110 lercInfo.numValidPixelcxx . . $ LercNS/Lerc#LercInfo#numValidPixel. = lerc2Info.numValidPixelcxx . . $ LercNS/Lerc2#HeaderInfo#numValidPixel.; // for 1st band
111 lercInfo.blobSizecxx . . $ LercNS/Lerc#LercInfo#blobSize. = (unsigned int)lerc2Info.blobSizecxx . . $ LercNS/Lerc2#HeaderInfo#blobSize.; // blob size per band is in [0 .. 2 GB]
112 lercInfo.dtcxx . . $ LercNS/Lerc#LercInfo#dt. = (DataTypecxx . . $ LercNS/Lerc#DataType#)lerc2Info.dtcxx . . $ LercNS/Lerc2#HeaderInfo#dt.;
113 lercInfo.zMincxx . . $ LercNS/Lerc#LercInfo#zMin. = lerc2Info.zMincxx . . $ LercNS/Lerc2#HeaderInfo#zMin.;
114 lercInfo.zMaxcxx . . $ LercNS/Lerc#LercInfo#zMax. = lerc2Info.zMaxcxx . . $ LercNS/Lerc2#HeaderInfo#zMax.;
115 lercInfo.maxZErrorcxx . . $ LercNS/Lerc#LercInfo#maxZError. = lerc2Info.maxZErrorcxx . . $ LercNS/Lerc2#HeaderInfo#maxZError.;
116 lercInfo.nUsesNoDataValuecxx . . $ LercNS/Lerc#LercInfo#nUsesNoDataValue. = lerc2Info.bPassNoDataValuescxx . . $ LercNS/Lerc2#HeaderInfo#bPassNoDataValues. ? 1 : 0;
117
118 bool bTryNextBlob = (lerc2Info.versioncxx . . $ LercNS/Lerc2#HeaderInfo#version. <= 5) || (lerc2Info.nBlobsMorecxx . . $ LercNS/Lerc2#HeaderInfo#nBlobsMore. > 0);
119
120 if (bHasMask || lercInfo.numValidPixelcxx . . $ LercNS/Lerc#LercInfo#numValidPixel. == 0)
121 nMasks = 1;
122
123 if (pMins && pMaxs)
124 {
125 ErrCodecxx . . $ LercNS/ErrCode# errCode = GetRangescxx . . $ LercNS/Lerc#GetRanges(771fda4212732f67).(pLercBlob, numBytesBlob, 0, lerc2Info, pMins, pMaxs, nElem); // band 0
126 if (errCode != ErrCodecxx . . $ LercNS/ErrCode#::Okcxx . . $ LercNS/ErrCode#Ok.)
127 return errCode;
128 }
129
130 lercInfo.nBandscxx . . $ LercNS/Lerc#LercInfo#nBands. = 1;
131
132 if (lercInfo.blobSizecxx . . $ LercNS/Lerc#LercInfo#blobSize. > numBytesBlob) // truncated blob, we won't be able to read this band
133 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
134
135 struct Lerc2cxx . . $ LercNS/Lerc2#::HeaderInfocxx . . $ LercNS/Lerc2#HeaderInfo# hdInfo;
136 while (bTryNextBlob && Lerc2cxx . . $ LercNS/Lerc2#::GetHeaderInfocxx . . $ LercNS/Lerc2#GetHeaderInfo(5bafa162245ba3da).(pLercBlob + lercInfo.blobSizecxx . . $ LercNS/Lerc#LercInfo#blobSize., numBytesBlob - lercInfo.blobSizecxx . . $ LercNS/Lerc#LercInfo#blobSize., hdInfo, bHasMask))
137 {
138 if (hdInfo.nDepthcxx . . $ LercNS/Lerc2#HeaderInfo#nDepth. != lercInfo.nDepthcxx . . $ LercNS/Lerc#LercInfo#nDepth.
139 || hdInfo.nColscxx . . $ LercNS/Lerc2#HeaderInfo#nCols. != lercInfo.nColscxx . . $ LercNS/Lerc#LercInfo#nCols.
140 || hdInfo.nRowscxx . . $ LercNS/Lerc2#HeaderInfo#nRows. != lercInfo.nRowscxx . . $ LercNS/Lerc#LercInfo#nRows.
141 || (int)hdInfo.dtcxx . . $ LercNS/Lerc2#HeaderInfo#dt. != (int)lercInfo.dtcxx . . $ LercNS/Lerc#LercInfo#dt.
142 || hdInfo.blobSizecxx . . $ LercNS/Lerc2#HeaderInfo#blobSize. < 0)
143 {
144 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
145 }
146
147 bTryNextBlob = (hdInfo.versioncxx . . $ LercNS/Lerc2#HeaderInfo#version. <= 5) || (hdInfo.nBlobsMorecxx . . $ LercNS/Lerc2#HeaderInfo#nBlobsMore. > 0);
148
149 if (hdInfo.bPassNoDataValuescxx . . $ LercNS/Lerc2#HeaderInfo#bPassNoDataValues.)
150 lercInfo.nUsesNoDataValuecxx . . $ LercNS/Lerc#LercInfo#nUsesNoDataValue.++;
151
152 if (bHasMask || hdInfo.numValidPixelcxx . . $ LercNS/Lerc2#HeaderInfo#numValidPixel. != lercInfo.numValidPixelcxx . . $ LercNS/Lerc#LercInfo#numValidPixel.) // support mask per band
153 nMasks = 2;
154
155 if ((size_t)lercInfo.blobSizecxx . . $ LercNS/Lerc#LercInfo#blobSize. > (size_t)UINT_MAXcxx . . $ `<external>/2419331716647297546/limits.h:124:9`! - hdInfo.blobSizecxx . . $ LercNS/Lerc2#HeaderInfo#blobSize.) // guard against overflow
156 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
157
158 if ((size_t)lercInfo.blobSizecxx . . $ LercNS/Lerc#LercInfo#blobSize. + hdInfo.blobSizecxx . . $ LercNS/Lerc2#HeaderInfo#blobSize. > (size_t)numBytesBlob) // truncated blob, we won't be able to read this band
159 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
160
161 lercInfo.zMincxx . . $ LercNS/Lerc#LercInfo#zMin. = mincxx . . $ std/min(2a338fce5842aa6a).(lercInfo.zMincxx . . $ LercNS/Lerc#LercInfo#zMin., hdInfo.zMincxx . . $ LercNS/Lerc2#HeaderInfo#zMin.);
162 lercInfo.zMaxcxx . . $ LercNS/Lerc#LercInfo#zMax. = maxcxx . . $ std/max(2a338fce5842aa6a).(lercInfo.zMaxcxx . . $ LercNS/Lerc#LercInfo#zMax., hdInfo.zMaxcxx . . $ LercNS/Lerc2#HeaderInfo#zMax.);
163 lercInfo.maxZErrorcxx . . $ LercNS/Lerc#LercInfo#maxZError. = maxcxx . . $ std/max(2a338fce5842aa6a).(lercInfo.maxZErrorcxx . . $ LercNS/Lerc#LercInfo#maxZError., hdInfo.maxZErrorcxx . . $ LercNS/Lerc2#HeaderInfo#maxZError.); // with the new bitplane compression, maxZError can vary between bands
164
165 if (pMins && pMaxs)
166 {
167 ErrCodecxx . . $ LercNS/ErrCode# errCode = GetRangescxx . . $ LercNS/Lerc#GetRanges(771fda4212732f67).(pLercBlob + lercInfo.blobSizecxx . . $ LercNS/Lerc#LercInfo#blobSize., numBytesBlob - lercInfo.blobSizecxx . . $ LercNS/Lerc#LercInfo#blobSize., lercInfo.nBandscxx . . $ LercNS/Lerc#LercInfo#nBands., hdInfo, pMins, pMaxs, nElem);
168 if (errCode != ErrCodecxx . . $ LercNS/ErrCode#::Okcxx . . $ LercNS/ErrCode#Ok.)
169 return errCode;
170 }
171
172 lercInfo.blobSizecxx . . $ LercNS/Lerc#LercInfo#blobSize. += hdInfo.blobSizecxx . . $ LercNS/Lerc2#HeaderInfo#blobSize.;
173 lercInfo.nBandscxx . . $ LercNS/Lerc#LercInfo#nBands.++;
174 }
175
176 lercInfo.nMaskscxx . . $ LercNS/Lerc#LercInfo#nMasks. = nMasks > 1 ? lercInfo.nBandscxx . . $ LercNS/Lerc#LercInfo#nBands. : nMasks;
177
178 if (lercInfo.nUsesNoDataValuecxx . . $ LercNS/Lerc#LercInfo#nUsesNoDataValue. > 0)
179 lercInfo.nUsesNoDataValuecxx . . $ LercNS/Lerc#LercInfo#nUsesNoDataValue. = lercInfo.nBandscxx . . $ LercNS/Lerc#LercInfo#nBands.; // if there is any noData used in any band, allow for different noData per band
180
181 return ErrCodecxx . . $ LercNS/ErrCode#::Okcxx . . $ LercNS/ErrCode#Ok.;
182 }
183
184
185 #ifdef HAVE_LERC1_DECODEcxx . . $ `src/LercLib/Defines.h:39:9`!
186 // only if not Lerc2, try legacy Lerc1
187 unsigned int numBytesHeaderBand0 = CntZImagecxx . . $ LercNS/CntZImage#::computeNumBytesNeededToReadHeadercxx . . $ LercNS/CntZImage#computeNumBytesNeededToReadHeader(9e1a662fef1ac081).(false);
188 unsigned int numBytesHeaderBand1 = CntZImagecxx . . $ LercNS/CntZImage#::computeNumBytesNeededToReadHeadercxx . . $ LercNS/CntZImage#computeNumBytesNeededToReadHeader(9e1a662fef1ac081).(true);
189 const Bytecxx . . $ LercNS/Byte#* pByte = pLercBlob;
190
191 lercInfo.zMincxx . . $ LercNS/Lerc#LercInfo#zMin. = FLT_MAXcxx . . $ `<external>/8223903785965736313/float.h:104:9`!;
192 lercInfo.zMaxcxx . . $ LercNS/Lerc#LercInfo#zMax. = -FLT_MAXcxx . . $ `<external>/8223903785965736313/float.h:104:9`!;
193
194 CntZImagecxx . . $ LercNS/CntZImage# cntZImgcxx . . $ LercNS/CntZImage#CntZImage(49f6e7a06ebc5aa8).;
195 if (numBytesHeaderBand0 <= numBytesBlob && cntZImg.readcxx . . $ LercNS/CntZImage#read(3fcf2816e18529d4).(&pByte, pLercBlob + numBytesBlob, 1e12, true)) // read just the header
196 {
197 size_t nBytesRead = pByte - pLercBlob;
198 size_t nBytesNeeded = 10 + 4 * sizeof(int) + 1 * sizeof(double);
199
200 if (nBytesRead < nBytesNeeded)
201 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
202
203 const Bytecxx . . $ LercNS/Byte#* ptr = pLercBlob;
204 ptr += 10 + 2 * sizeof(int);
205
206 int height(0), width(0);
207 memcpy(&height, ptr, sizeof(int)); ptr += sizeof(int);
208 memcpy(&width, ptr, sizeof(int)); ptr += sizeof(int);
209 double maxZErrorInFile(0);
210 memcpy(&maxZErrorInFile, ptr, sizeof(double));
211
212 if (height < 0 || width < 0 || height > 40000 || width > 40000) // guard against bogus numbers; size limitation for old Lerc1
213 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
214
215 if (sizeof(CntZcxx . . $ LercNS/CntZ#) * height * width > (size_t)INT_MAXcxx . . $ `<external>/2419331716647297546/limits.h:120:9`!)
216 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
217
218 lercInfo.nDepthcxx . . $ LercNS/Lerc#LercInfo#nDepth. = 1;
219 lercInfo.nColscxx . . $ LercNS/Lerc#LercInfo#nCols. = width;
220 lercInfo.nRowscxx . . $ LercNS/Lerc#LercInfo#nRows. = height;
221 lercInfo.dtcxx . . $ LercNS/Lerc#LercInfo#dt. = Lerccxx . . $ LercNS/Lerc#::DT_Floatcxx . . $ LercNS/Lerc#DT_Float.;
222 lercInfo.maxZErrorcxx . . $ LercNS/Lerc#LercInfo#maxZError. = maxZErrorInFile;
223
224 pByte = pLercBlob;
225 bool onlyZPart = false;
226
227 while (lercInfo.blobSizecxx . . $ LercNS/Lerc#LercInfo#blobSize. + numBytesHeaderBand1 < numBytesBlob) // means there could be another band
228 {
229 if (!cntZImg.readcxx . . $ LercNS/CntZImage#read(3fcf2816e18529d4).(&pByte, pLercBlob + numBytesBlob, 1e12, false, onlyZPart))
230 return (lercInfo.nBandscxx . . $ LercNS/Lerc#LercInfo#nBands. > 0) ? ErrCodecxx . . $ LercNS/ErrCode#::Okcxx . . $ LercNS/ErrCode#Ok. : ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.; // no other band, we are done
231
232 onlyZPart = true;
233 lercInfo.blobSizecxx . . $ LercNS/Lerc#LercInfo#blobSize. = (int)(pByte - pLercBlob);
234
235 // now that we have decoded it, we can go the extra mile and collect some extra info
236 int numValidPixels = 0;
237 float zMin = FLT_MAXcxx . . $ `<external>/8223903785965736313/float.h:104:9`!;
238 float zMax = -FLT_MAXcxx . . $ `<external>/8223903785965736313/float.h:104:9`!;
239
240 for (int i = 0; i < height; i++)
241 {
242 for (int j = 0; j < width; j++)
243 if (cntZImg(cxx . . $ LercNS/TImage#`operator()`(81532356f20a340f).i, j).cntcxx . . $ LercNS/CntZ#cnt. > 0)
244 {
245 numValidPixels++;
246 float z = cntZImg(cxx . . $ LercNS/TImage#`operator()`(81532356f20a340f).i, j).zcxx . . $ LercNS/CntZ#z.;
247 zMax = maxcxx . . $ std/max(2a338fce5842aa6a).(zMax, z);
248 zMin = mincxx . . $ std/min(2a338fce5842aa6a).(zMin, z);
249 }
250 }
251
252 lercInfo.numValidPixelcxx . . $ LercNS/Lerc#LercInfo#numValidPixel. = numValidPixels;
253 lercInfo.zMincxx . . $ LercNS/Lerc#LercInfo#zMin. = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).(lercInfo.zMincxx . . $ LercNS/Lerc#LercInfo#zMin., (double)zMin);
254 lercInfo.zMaxcxx . . $ LercNS/Lerc#LercInfo#zMax. = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).(lercInfo.zMaxcxx . . $ LercNS/Lerc#LercInfo#zMax., (double)zMax);
255 lercInfo.nMaskscxx . . $ LercNS/Lerc#LercInfo#nMasks. = numValidPixels < width * height ? 1 : 0;
256
257 if (pMins && pMaxs)
258 {
259 pMins[lercInfo.nBandscxx . . $ LercNS/Lerc#LercInfo#nBands.] = zMin;
260 pMaxs[lercInfo.nBandscxx . . $ LercNS/Lerc#LercInfo#nBands.] = zMax;
261 }
262
263 lercInfo.nBandscxx . . $ LercNS/Lerc#LercInfo#nBands.++;
264 }
265
266 return ErrCodecxx . . $ LercNS/ErrCode#::Okcxx . . $ LercNS/ErrCode#Ok.;
267 }
268 #endif
269
270 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
271 }
272
273 // -------------------------------------------------------------------------- ;
274
275 ErrCodecxx . . $ LercNS/ErrCode# Lerccxx . . $ LercNS/Lerc#::Decodecxx . . $ LercNS/Lerc#Decode(107064e3f405a899).(const Bytecxx . . $ LercNS/Byte#* pLercBlob, unsigned int numBytesBlob, int nMasks, Bytecxx . . $ LercNS/Byte#* pValidBytes,
276 int nDepth, int nCols, int nRows, int nBands, DataTypecxx . . $ LercNS/Lerc#DataType# dt, void* pData, unsigned char* pUsesNoData, double* noDataValues)
277 {
278 #define LERC_ARG_3cxx . . $ `src/LercLib/Lerc.cpp:278:9`! pLercBlob, numBytesBlob, nDepth, nCols, nRows, nBands, nMasks, pValidBytes, pUsesNoData, noDataValues
279
280 switch (dt)
281 {
282 case DT_Charcxx . . $ LercNS/Lerc#DT_Char.: return DecodeTemplcxx . . $ LercNS/Lerc#DecodeTempl(a7acf4c1e550641d).((signed char*)pData, LERC_ARG_3cxx . . $ `src/LercLib/Lerc.cpp:278:9`!);
283 case DT_Bytecxx . . $ LercNS/Lerc#DT_Byte.: return DecodeTemplcxx . . $ LercNS/Lerc#DecodeTempl(a7acf4c1e550641d).((Bytecxx . . $ LercNS/Byte#*)pData, LERC_ARG_3cxx . . $ `src/LercLib/Lerc.cpp:278:9`!);
284 case DT_Shortcxx . . $ LercNS/Lerc#DT_Short.: return DecodeTemplcxx . . $ LercNS/Lerc#DecodeTempl(a7acf4c1e550641d).((short*)pData, LERC_ARG_3cxx . . $ `src/LercLib/Lerc.cpp:278:9`!);
285 case DT_UShortcxx . . $ LercNS/Lerc#DT_UShort.: return DecodeTemplcxx . . $ LercNS/Lerc#DecodeTempl(a7acf4c1e550641d).((unsigned short*)pData, LERC_ARG_3cxx . . $ `src/LercLib/Lerc.cpp:278:9`!);
286 case DT_Intcxx . . $ LercNS/Lerc#DT_Int.: return DecodeTemplcxx . . $ LercNS/Lerc#DecodeTempl(a7acf4c1e550641d).((int*)pData, LERC_ARG_3cxx . . $ `src/LercLib/Lerc.cpp:278:9`!);
287 case DT_UIntcxx . . $ LercNS/Lerc#DT_UInt.: return DecodeTemplcxx . . $ LercNS/Lerc#DecodeTempl(a7acf4c1e550641d).((unsigned int*)pData, LERC_ARG_3cxx . . $ `src/LercLib/Lerc.cpp:278:9`!);
288 case DT_Floatcxx . . $ LercNS/Lerc#DT_Float.: return DecodeTemplcxx . . $ LercNS/Lerc#DecodeTempl(a7acf4c1e550641d).((float*)pData, LERC_ARG_3cxx . . $ `src/LercLib/Lerc.cpp:278:9`!);
289 case DT_Doublecxx . . $ LercNS/Lerc#DT_Double.: return DecodeTemplcxx . . $ LercNS/Lerc#DecodeTempl(a7acf4c1e550641d).((double*)pData, LERC_ARG_3cxx . . $ `src/LercLib/Lerc.cpp:278:9`!);
290
291 default:
292 return ErrCodecxx . . $ LercNS/ErrCode#::WrongParamcxx . . $ LercNS/ErrCode#WrongParam.;
293 }
294
295 #undef LERC_ARG_3cxx . . $ `src/LercLib/Lerc.cpp:278:9`!
296 }
297
298 // -------------------------------------------------------------------------- ;
299
300 ErrCodecxx . . $ LercNS/ErrCode# Lerccxx . . $ LercNS/Lerc#::ConvertToDoublecxx . . $ LercNS/Lerc#ConvertToDouble(82cb9f5f9c82c481).(const void* pDataIn, DataTypecxx . . $ LercNS/Lerc#DataType# dt, size_t nDataValues, double* pDataOut)
301 {
302 switch (dt)
303 {
304 case DT_Charcxx . . $ LercNS/Lerc#DT_Char.: return ConvertToDoubleTemplcxx . . $ LercNS/Lerc#ConvertToDoubleTempl(3116af53592bc7bf).((const signed char*)pDataIn, nDataValues, pDataOut);
305 case DT_Bytecxx . . $ LercNS/Lerc#DT_Byte.: return ConvertToDoubleTemplcxx . . $ LercNS/Lerc#ConvertToDoubleTempl(3116af53592bc7bf).((const Bytecxx . . $ LercNS/Byte#*)pDataIn, nDataValues, pDataOut);
306 case DT_Shortcxx . . $ LercNS/Lerc#DT_Short.: return ConvertToDoubleTemplcxx . . $ LercNS/Lerc#ConvertToDoubleTempl(3116af53592bc7bf).((const short*)pDataIn, nDataValues, pDataOut);
307 case DT_UShortcxx . . $ LercNS/Lerc#DT_UShort.: return ConvertToDoubleTemplcxx . . $ LercNS/Lerc#ConvertToDoubleTempl(3116af53592bc7bf).((const unsigned short*)pDataIn, nDataValues, pDataOut);
308 case DT_Intcxx . . $ LercNS/Lerc#DT_Int.: return ConvertToDoubleTemplcxx . . $ LercNS/Lerc#ConvertToDoubleTempl(3116af53592bc7bf).((const int*)pDataIn, nDataValues, pDataOut);
309 case DT_UIntcxx . . $ LercNS/Lerc#DT_UInt.: return ConvertToDoubleTemplcxx . . $ LercNS/Lerc#ConvertToDoubleTempl(3116af53592bc7bf).((const unsigned int*)pDataIn, nDataValues, pDataOut);
310 case DT_Floatcxx . . $ LercNS/Lerc#DT_Float.: return ConvertToDoubleTemplcxx . . $ LercNS/Lerc#ConvertToDoubleTempl(3116af53592bc7bf).((const float*)pDataIn, nDataValues, pDataOut);
311 //case DT_Double: no convert double to double
312
313 default:
314 return ErrCodecxx . . $ LercNS/ErrCode#::WrongParamcxx . . $ LercNS/ErrCode#WrongParam.;
315 }
316 }
317
318 // -------------------------------------------------------------------------- ;
319 // -------------------------------------------------------------------------- ;
320
321 template<class T>
322 ErrCodecxx . . $ LercNS/ErrCode# Lerccxx . . $ LercNS/Lerc#::ComputeCompressedSizeTemplcxx . . $ LercNS/Lerc#ComputeCompressedSizeTempl(40cc20dd7dade3bf).(const T* pData, int version, int nDepth, int nCols, int nRows,
323 int nBands, int nMasks, const Bytecxx . . $ LercNS/Byte#* pValidBytes, double maxZErr, unsigned int& numBytesNeeded,
324 const unsigned char* pUsesNoData, const double* noDataValues)
325 {
326 numBytesNeeded = 0;
327
328 if (!pData || nDepth <= 0 || nCols <= 0 || nRows <= 0 || nBands <= 0 || maxZErr < 0)
329 return ErrCodecxx . . $ LercNS/ErrCode#::WrongParamcxx . . $ LercNS/ErrCode#WrongParam.;
330
331 if (!(nMasks == 0 || nMasks == 1 || nMasks == nBands) || (nMasks > 0 && !pValidBytes))
332 return ErrCodecxx . . $ LercNS/ErrCode#::WrongParamcxx . . $ LercNS/ErrCode#WrongParam.;
333
334 if (!CheckDimensionscxx . . $ LercNS/Lerc#CheckDimensions(67519ccc50caacfb).(nDepth, nCols, nRows, sizeof(T)))
335 return ErrCodecxx . . $ LercNS/ErrCode#::DimensionsTooLargecxx . . $ LercNS/ErrCode#DimensionsTooLarge.;
336
337 unsigned int numBytesWritten = 0;
338
339 if (version >= 0 && version <= 5)
340 {
341 if (pUsesNoData)
342 for (int i = 0; i < nBands; i++)
343 if (pUsesNoData[i])
344 return ErrCodecxx . . $ LercNS/ErrCode#::WrongParamcxx . . $ LercNS/ErrCode#WrongParam.;
345
346 return EncodeInternal_v5cxx . . $ LercNS/Lerc#EncodeInternal_v5(f53d28dc65a36817).(pData, version, nDepth, nCols, nRows, nBands, nMasks, pValidBytes, maxZErr,
347 numBytesNeeded, nullptr, 0, numBytesWritten);
348 }
349 else
350 {
351 return EncodeInternalcxx . . $ LercNS/Lerc#EncodeInternal(810fae6e2d3e41ce).(pData, version, nDepth, nCols, nRows, nBands, nMasks, pValidBytes, maxZErr,
352 numBytesNeeded, nullptr, 0, numBytesWritten, pUsesNoData, noDataValues);
353 }
354 }
355
356 // -------------------------------------------------------------------------- ;
357
358 template<class T>
359 ErrCodecxx . . $ LercNS/ErrCode# Lerccxx . . $ LercNS/Lerc#::EncodeTemplcxx . . $ LercNS/Lerc#EncodeTempl(402103a7adc54e8c).(const T* pData, int version, int nDepth, int nCols, int nRows, int nBands,
360 int nMasks, const Bytecxx . . $ LercNS/Byte#* pValidBytes, double maxZErr, Bytecxx . . $ LercNS/Byte#* pBuffer, unsigned int numBytesBuffer,
361 unsigned int& numBytesWritten, const unsigned char* pUsesNoData, const double* noDataValues)
362 {
363 numBytesWritten = 0;
364
365 if (!pData || nDepth <= 0 || nCols <= 0 || nRows <= 0 || nBands <= 0 || maxZErr < 0 || !pBuffer || !numBytesBuffer)
366 return ErrCodecxx . . $ LercNS/ErrCode#::WrongParamcxx . . $ LercNS/ErrCode#WrongParam.;
367
368 if (!(nMasks == 0 || nMasks == 1 || nMasks == nBands) || (nMasks > 0 && !pValidBytes))
369 return ErrCodecxx . . $ LercNS/ErrCode#::WrongParamcxx . . $ LercNS/ErrCode#WrongParam.;
370
371 if (!CheckDimensionscxx . . $ LercNS/Lerc#CheckDimensions(67519ccc50caacfb).(nDepth, nCols, nRows, sizeof(T)))
372 return ErrCodecxx . . $ LercNS/ErrCode#::DimensionsTooLargecxx . . $ LercNS/ErrCode#DimensionsTooLarge.;
373
374 memset(pBuffer, 0, (size_t)numBytesBuffer);
375
376 unsigned int numBytesNeeded = 0;
377
378 if (version >= 0 && version <= 5)
379 {
380 if (pUsesNoData)
381 for (int i = 0; i < nBands; i++)
382 if (pUsesNoData[i])
383 return ErrCodecxx . . $ LercNS/ErrCode#::WrongParamcxx . . $ LercNS/ErrCode#WrongParam.;
384
385 return EncodeInternal_v5cxx . . $ LercNS/Lerc#EncodeInternal_v5(f53d28dc65a36817).(pData, version, nDepth, nCols, nRows, nBands, nMasks, pValidBytes, maxZErr,
386 numBytesNeeded, pBuffer, numBytesBuffer, numBytesWritten);
387 }
388 else
389 {
390 return EncodeInternalcxx . . $ LercNS/Lerc#EncodeInternal(810fae6e2d3e41ce).(pData, version, nDepth, nCols, nRows, nBands, nMasks, pValidBytes, maxZErr,
391 numBytesNeeded, pBuffer, numBytesBuffer, numBytesWritten, pUsesNoData, noDataValues);
392 }
393 }
394
395 // -------------------------------------------------------------------------- ;
396
397 template<class T>
398 ErrCodecxx . . $ LercNS/ErrCode# Lerccxx . . $ LercNS/Lerc#::DecodeTemplcxx . . $ LercNS/Lerc#DecodeTempl(a7acf4c1e550641d).(T* pData, const Bytecxx . . $ LercNS/Byte#* pLercBlob, unsigned int numBytesBlob,
399 int nDepth, int nCols, int nRows, int nBands, int nMasks, Bytecxx . . $ LercNS/Byte#* pValidBytes,
400 unsigned char* pUsesNoData, double* noDataValues)
401 {
402 if (!pData || nDepth <= 0 || nCols <= 0 || nRows <= 0 || nBands <= 0 || !pLercBlob || !numBytesBlob)
403 return ErrCodecxx . . $ LercNS/ErrCode#::WrongParamcxx . . $ LercNS/ErrCode#WrongParam.;
404
405 if (!(nMasks == 0 || nMasks == 1 || nMasks == nBands) || (nMasks > 0 && !pValidBytes))
406 return ErrCodecxx . . $ LercNS/ErrCode#::WrongParamcxx . . $ LercNS/ErrCode#WrongParam.;
407
408 if (!CheckDimensionscxx . . $ LercNS/Lerc#CheckDimensions(67519ccc50caacfb).(nDepth, nCols, nRows, sizeof(T)))
409 return ErrCodecxx . . $ LercNS/ErrCode#::DimensionsTooLargecxx . . $ LercNS/ErrCode#DimensionsTooLarge.;
410
411 const Bytecxx . . $ LercNS/Byte#* pByte = pLercBlob;
412 Lerc2cxx . . $ LercNS/Lerc2#::HeaderInfocxx . . $ LercNS/Lerc2#HeaderInfo# hdInfo;
413 bool bHasMask = false;
414
415 if (Lerc2cxx . . $ LercNS/Lerc2#::GetHeaderInfocxx . . $ LercNS/Lerc2#GetHeaderInfo(5bafa162245ba3da).(pByte, numBytesBlob, hdInfo, bHasMask) && hdInfo.versioncxx . . $ LercNS/Lerc2#HeaderInfo#version. >= 1) // if Lerc2
416 {
417 LercInfocxx . . $ LercNS/Lerc#LercInfo# lercInfo;
418 ErrCodecxx . . $ LercNS/ErrCode# errCode = GetLercInfocxx . . $ LercNS/Lerc#GetLercInfo(db35de8f40228c29).(pLercBlob, numBytesBlob, lercInfo); // fast for Lerc2, does most checks
419 if (errCode != ErrCodecxx . . $ LercNS/ErrCode#::Okcxx . . $ LercNS/ErrCode#Ok.)
420 return errCode;
421
422 // caller must provide enough space for the masks that are there (e.g., if they differ between bands)
423 if (nMasks < lercInfo.nMaskscxx . . $ LercNS/Lerc#LercInfo#nMasks.) // 0, 1, or nBands
424 return ErrCodecxx . . $ LercNS/ErrCode#::WrongParamcxx . . $ LercNS/ErrCode#WrongParam.;
425
426 // caller cannot ask for more bands than are there
427 if (nBands > lercInfo.nBandscxx . . $ LercNS/Lerc#LercInfo#nBands.)
428 return ErrCodecxx . . $ LercNS/ErrCode#::WrongParamcxx . . $ LercNS/ErrCode#WrongParam.;
429
430 // if Lerc blob has noData values not covered by the mask, caller must get it (make sure caller cannot miss it)
431 if (lercInfo.nUsesNoDataValuecxx . . $ LercNS/Lerc#LercInfo#nUsesNoDataValue. && nDepth > 1)
432 {
433 if (!pUsesNoData || !noDataValues)
434 return ErrCodecxx . . $ LercNS/ErrCode#::HasNoDatacxx . . $ LercNS/ErrCode#HasNoData.;
435
436 try
437 {
438 memset(pUsesNoData, 0, nBands);
439 memset(noDataValues, 0, nBands * sizeof(double));
440 }
441 catch (...)
442 {
443 return ErrCodecxx . . $ LercNS/ErrCode#::HasNoDatacxx . . $ LercNS/ErrCode#HasNoData.;
444 }
445 }
446
447 size_t nBytesRemaining = numBytesBlob;
448 Lerc2cxx . . $ LercNS/Lerc2# lerc2cxx . . $ LercNS/Lerc2#Lerc2(49f6e7a06ebc5aa8).;
449 BitMaskcxx . . $ LercNS/BitMask# bitMaskcxx . . $ LercNS/BitMask#BitMask(49f6e7a06ebc5aa8).;
450
451 for (int iBand = 0; iBand < nBands; iBand++)
452 {
453 if (((size_t)(pByte - pLercBlob) < numBytesBlob) && Lerc2cxx . . $ LercNS/Lerc2#::GetHeaderInfocxx . . $ LercNS/Lerc2#GetHeaderInfo(5bafa162245ba3da).(pByte, nBytesRemaining, hdInfo, bHasMask))
454 {
455 if (hdInfo.nDepthcxx . . $ LercNS/Lerc2#HeaderInfo#nDepth. != nDepth || hdInfo.nColscxx . . $ LercNS/Lerc2#HeaderInfo#nCols. != nCols || hdInfo.nRowscxx . . $ LercNS/Lerc2#HeaderInfo#nRows. != nRows || hdInfo.blobSizecxx . . $ LercNS/Lerc2#HeaderInfo#blobSize. < 0)
456 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
457
458 if ((pByte - pLercBlob) + (size_t)hdInfo.blobSizecxx . . $ LercNS/Lerc2#HeaderInfo#blobSize. > numBytesBlob) // corrupted blob
459 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
460
461 size_t nPix = (size_t)iBand * nRows * nCols;
462 T* arr = pData +cxx . . $ std/operator+(933aeee3a62e2e30). nPix * nDepth;
463
464 bool bGetMask = iBand < nMasks;
465
466 if (bGetMask && !bitMask.SetSizecxx . . $ LercNS/BitMask#SetSize(cf9ef193275fa6d8).(nCols, nRows))
467 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
468
469 if (!lerc2.Decodecxx . . $ LercNS/Lerc2#Decode(1e98a814b12037d0).(&pByte, nBytesRemaining, arr, bGetMask ? bitMask.Bitscxx . . $ LercNS/BitMask#Bits(37d8967d10cb1e7b).() : nullptr))
470 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
471
472 if (lercInfo.nUsesNoDataValuecxx . . $ LercNS/Lerc#LercInfo#nUsesNoDataValue. && nDepth > 1)
473 {
474 pUsesNoData[iBand] = hdInfo.bPassNoDataValuescxx . . $ LercNS/Lerc2#HeaderInfo#bPassNoDataValues. ? 1 : 0;
475 noDataValues[iBand] = hdInfo.noDataValOrigcxx . . $ LercNS/Lerc2#HeaderInfo#noDataValOrig.;
476
477 if (hdInfo.bPassNoDataValuescxx . . $ LercNS/Lerc2#HeaderInfo#bPassNoDataValues. && !RemapNoDatacxx . . $ LercNS/Lerc#RemapNoData(f51e42f49da7dd89).(arr, bitMask, hdInfo))
478 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
479 }
480
481 if (bGetMask && !Convertcxx . . $ LercNS/Lerc#Convert(54c99c5ab6149a98).(bitMask, pValidBytes + nPix))
482 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
483 }
484 } // iBand
485 } // Lerc2
486
487 else // might be old Lerc1
488 {
489 #ifdef HAVE_LERC1_DECODEcxx . . $ `src/LercLib/Defines.h:39:9`!
490 unsigned int numBytesHeaderBand0 = CntZImagecxx . . $ LercNS/CntZImage#::computeNumBytesNeededToReadHeadercxx . . $ LercNS/CntZImage#computeNumBytesNeededToReadHeader(9e1a662fef1ac081).(false);
491 unsigned int numBytesHeaderBand1 = CntZImagecxx . . $ LercNS/CntZImage#::computeNumBytesNeededToReadHeadercxx . . $ LercNS/CntZImage#computeNumBytesNeededToReadHeader(9e1a662fef1ac081).(true);
492 const Bytecxx . . $ LercNS/Byte#* pByte1 = pLercBlob;
493 CntZImagecxx . . $ LercNS/CntZImage# zImgcxx . . $ LercNS/CntZImage#CntZImage(49f6e7a06ebc5aa8).;
494
495 for (int iBand = 0; iBand < nBands; iBand++)
496 {
497 unsigned int numBytesHeader = iBand == 0 ? numBytesHeaderBand0 : numBytesHeaderBand1;
498 if ((size_t)(pByte1 - pLercBlob) + numBytesHeader > numBytesBlob) // corrupted blob or wrong nBands
499 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
500
501 bool onlyZPart = iBand > 0;
502 if (!zImg.readcxx . . $ LercNS/CntZImage#read(3fcf2816e18529d4).(&pByte1, pLercBlob + numBytesBlob, 1e12, false, onlyZPart))
503 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
504
505 if (zImg.getWidthcxx . . $ LercNS/Image#getWidth(455f465bc33b4cdf).() != nCols || zImg.getHeightcxx . . $ LercNS/Image#getHeight(455f465bc33b4cdf).() != nRows)
506 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
507
508 size_t nPix = (size_t)iBand * nRows * nCols;
509 T* arr = pData +cxx . . $ std/operator+(933aeee3a62e2e30). nPix;
510 Bytecxx . . $ LercNS/Byte#* pDst = iBand < nMasks ? pValidBytes + nPix : nullptr;
511
512 if (!Convertcxx . . $ LercNS/Lerc#Convert(d91d2f7413d17fb0).(zImg, arr, pDst, iBand == 0))
513 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
514 }
515 #else
516 return ErrCode::Failed;
517 #endif
518 }
519
520 return ErrCodecxx . . $ LercNS/ErrCode#::Okcxx . . $ LercNS/ErrCode#Ok.;
521 }
522
523 // -------------------------------------------------------------------------- ;
524 // -------------------------------------------------------------------------- ;
525
526 template<class T>
527 ErrCodecxx . . $ LercNS/ErrCode# Lerccxx . . $ LercNS/Lerc#::EncodeInternal_v5cxx . . $ LercNS/Lerc#EncodeInternal_v5(f53d28dc65a36817).(const T* pData, int version, int nDepth, int nCols, int nRows, int nBands,
528 int nMasks, const Bytecxx . . $ LercNS/Byte#* pValidBytes, double maxZErr, unsigned int& numBytesNeeded,
529 Bytecxx . . $ LercNS/Byte#* pBuffer, unsigned int numBytesBuffer, unsigned int& numBytesWritten)
530 {
531 numBytesNeeded = 0;
532 numBytesWritten = 0;
533
534 Lerc2cxx . . $ LercNS/Lerc2# lerc2cxx . . $ LercNS/Lerc2#Lerc2(49f6e7a06ebc5aa8).;
535 if (version >= 0 && !lerc2.SetEncoderToOldVersioncxx . . $ LercNS/Lerc2#SetEncoderToOldVersion(83c267b20bac841a).(version))
536 return ErrCodecxx . . $ LercNS/ErrCode#::WrongParamcxx . . $ LercNS/ErrCode#WrongParam.;
537
538 Bytecxx . . $ LercNS/Byte#* pDst = pBuffer;
539
540 const size_t nPix = (size_t)nCols * nRows;
541 const size_t nElem = nPix * nDepth;
542
543 const Bytecxx . . $ LercNS/Byte#* pPrevByteMask = nullptr;
544 vectorcxx . . $ std/vector#<T> dataBuffer;
545 vectorcxx . . $ std/vector#<Bytecxx . . $ LercNS/Byte#> maskBuffercxx . . $ std/vector#vector(49f6e7a06ebc5aa8)., prevMaskBuffercxx . . $ std/vector#vector(49f6e7a06ebc5aa8).;
546 BitMaskcxx . . $ LercNS/BitMask# bitMaskcxx . . $ LercNS/BitMask#BitMask(49f6e7a06ebc5aa8).;
547
548 // loop over the bands
549 for (int iBand = 0; iBand < nBands; iBand++)
550 {
551 bool bEncMsk = (iBand == 0);
552
553 // using the proper section of valid bytes, check this band for NaN
554 const T* arr = pData +cxx . . $ std/operator+(933aeee3a62e2e30). nElem * iBand;
555 const Bytecxx . . $ LercNS/Byte#* pByteMask = (nMasks > 0) ? (pValidBytes + ((nMasks > 1) ? nPix * iBand : 0)) : nullptr;
556
557 ErrCodecxx . . $ LercNS/ErrCode# errCode = CheckForNaNcxx . . $ LercNS/Lerc#CheckForNaN(db39ed3c12335d1).(arr, nDepth, nCols, nRows, pByteMask);
558 if (errCode != ErrCodecxx . . $ LercNS/ErrCode#::Okcxx . . $ LercNS/ErrCode#Ok. && errCode != ErrCodecxx . . $ LercNS/ErrCode#::NaNcxx . . $ LercNS/ErrCode#NaN.)
559 return errCode;
560
561 if (errCode == ErrCodecxx . . $ LercNS/ErrCode#::NaNcxx . . $ LercNS/ErrCode#NaN.) // found NaN values
562 {
563 if (!Resizecxx . . $ LercNS/Lerc#Resize(e6f179a37c0c9e9e).(dataBuffer, nElem) || !Resizecxx . . $ LercNS/Lerc#Resize(e6f179a37c0c9e9e).(maskBuffer, nPix))
564 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
565
566 memcpy(&dataBuffer[0], arr, nElem * sizeof(T));
567 pByteMask ? memcpy(&maskBuffer[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).0], pByteMask, nPix) : memset(&maskBuffer[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).0], 1, nPix);
568
569 if (!ReplaceNaNValuescxx . . $ LercNS/Lerc#ReplaceNaNValues(ad56b70f5ef9fcb2).(dataBuffer, maskBuffer, nDepth, nCols, nRows))
570 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
571
572 if (iBand > 0 && MasksDiffercxx . . $ LercNS/Lerc#MasksDiffer(176f309453ec74c9).(&maskBuffer[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).0], pPrevByteMask, nPix))
573 bEncMsk = true;
574
575 if (iBand < nBands - 1)
576 {
577 // keep current mask as new previous band mask
578 prevMaskBuffer =cxx . . $ std/vector#`operator=`(e6b68e61becad66f). maskBuffer;
579 pPrevByteMask = &prevMaskBuffer[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).0];
580 }
581
582 arr = &dataBuffer[0];
583 pByteMask = &maskBuffer[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).0];
584 }
585
586 else // no NaN in this band, the common case
587 {
588 if (iBand > 0 && MasksDiffercxx . . $ LercNS/Lerc#MasksDiffer(176f309453ec74c9).(pByteMask, pPrevByteMask, nPix))
589 bEncMsk = true;
590
591 pPrevByteMask = pByteMask;
592 }
593
594 if (bEncMsk)
595 {
596 if (pByteMask && !Convertcxx . . $ LercNS/Lerc#Convert(1633025d0e7a0295).(pByteMask, nCols, nRows, bitMask))
597 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
598
599 if (!lerc2.Setcxx . . $ LercNS/Lerc2#Set(2c716c8d5f4e4280).(nDepth, nCols, nRows, pByteMask ? bitMask.Bitscxx . . $ LercNS/BitMask#Bits(37d8967d10cb1e7b).() : nullptr))
600 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
601 }
602
603 unsigned int nBytes = lerc2.ComputeNumBytesNeededToWritecxx . . $ LercNS/Lerc2#ComputeNumBytesNeededToWrite(30bb643c04a8fd76).(arr, maxZErr, bEncMsk);
604 if (nBytes <= 0)
605 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
606
607 if ((size_t)numBytesNeeded + nBytes > (size_t)UINT_MAXcxx . . $ `<external>/2419331716647297546/limits.h:124:9`!) // keep total blob size (over all bands) <= 4 GB
608 return ErrCodecxx . . $ LercNS/ErrCode#::DimensionsTooLargecxx . . $ LercNS/ErrCode#DimensionsTooLarge.;
609
610 numBytesNeeded += nBytes;
611
612 if (pBuffer)
613 {
614 if ((size_t)(pDst - pBuffer) + nBytes > numBytesBuffer) // check we have enough space left
615 return ErrCodecxx . . $ LercNS/ErrCode#::BufferTooSmallcxx . . $ LercNS/ErrCode#BufferTooSmall.;
616
617 if (!lerc2.Encodecxx . . $ LercNS/Lerc2#Encode(9add4431f33ebb71).(arr, &pDst))
618 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
619 }
620 } // iBand
621
622 numBytesWritten = (unsigned int)(pDst - pBuffer);
623 return ErrCodecxx . . $ LercNS/ErrCode#::Okcxx . . $ LercNS/ErrCode#Ok.;
624 }
625
626 // -------------------------------------------------------------------------- ;
627
628 template<class T>
629 ErrCodecxx . . $ LercNS/ErrCode# Lerccxx . . $ LercNS/Lerc#::EncodeInternalcxx . . $ LercNS/Lerc#EncodeInternal(810fae6e2d3e41ce).(const T* pData, int version, int nDepth, int nCols, int nRows, int nBands,
630 int nMasks, const Bytecxx . . $ LercNS/Byte#* pValidBytes, double maxZErr, unsigned int& numBytesNeeded,
631 Bytecxx . . $ LercNS/Byte#* pBuffer, unsigned int numBytesBuffer, unsigned int& numBytesWritten,
632 const unsigned char* pUsesNoData, const double* noDataValues)
633 {
634 numBytesNeeded = 0;
635 numBytesWritten = 0;
636
637 if (version >= 0 && version <= 5)
638 return ErrCodecxx . . $ LercNS/ErrCode#::WrongParamcxx . . $ LercNS/ErrCode#WrongParam.;
639
640 Lerc2cxx . . $ LercNS/Lerc2# lerc2cxx . . $ LercNS/Lerc2#Lerc2(49f6e7a06ebc5aa8).;
641
642 #ifdef ENCODE_VERIFY
643 Lerc2 lerc2Verify;
644 #endif
645
646 if (version >= 0 && !lerc2.SetEncoderToOldVersioncxx . . $ LercNS/Lerc2#SetEncoderToOldVersion(83c267b20bac841a).(version))
647 return ErrCodecxx . . $ LercNS/ErrCode#::WrongParamcxx . . $ LercNS/ErrCode#WrongParam.;
648
649 if (pUsesNoData && !noDataValues)
650 for (int i = 0; i < nBands; i++)
651 if (pUsesNoData[i])
652 return ErrCodecxx . . $ LercNS/ErrCode#::WrongParamcxx . . $ LercNS/ErrCode#WrongParam.;
653
654 Bytecxx . . $ LercNS/Byte#* pDst = pBuffer;
655
656 const size_t nPix = (size_t)nCols * nRows;
657 const size_t nElem = nPix * nDepth;
658
659 const Bytecxx . . $ LercNS/Byte#* pPrevByteMask = nullptr;
660 vectorcxx . . $ std/vector#<T> dataBuffer;
661 vectorcxx . . $ std/vector#<Bytecxx . . $ LercNS/Byte#> maskBuffercxx . . $ std/vector#vector(49f6e7a06ebc5aa8)., prevMaskBuffercxx . . $ std/vector#vector(49f6e7a06ebc5aa8).;
662 BitMaskcxx . . $ LercNS/BitMask# bitMaskcxx . . $ LercNS/BitMask#BitMask(49f6e7a06ebc5aa8).;
663
664 // allocate buffer for 1 band
665 if (!Resizecxx . . $ LercNS/Lerc#Resize(e6f179a37c0c9e9e).(dataBuffer, nElem) || !Resizecxx . . $ LercNS/Lerc#Resize(e6f179a37c0c9e9e).(maskBuffer, nPix))
666 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
667
668 bool bIsFltOrDbl = (typeid(T) == typeid(float) || typeid(T) == typeid(double));
669 bool bAnyMaskModified = false;
670 ErrCodecxx . . $ LercNS/ErrCode# errCode = ErrCodecxx . . $ LercNS/ErrCode#::Okcxx . . $ LercNS/ErrCode#Ok.;
671
672 // loop over the bands
673 for (int iBand = 0; iBand < nBands; iBand++)
674 {
675 bool bEncMsk = (iBand == 0);
676
677 // get the data and mask for this band
678 const T* arrOrig = pData +cxx . . $ std/operator+(933aeee3a62e2e30). nElem * iBand;
679 const Bytecxx . . $ LercNS/Byte#* pByteMaskOrig = (nMasks > 0) ? (pValidBytes + ((nMasks > 1) ? nPix * iBand : 0)) : nullptr;
680
681 // copy to buffer so we can modify it
682 memcpy(&dataBuffer[0], arrOrig, nElem * sizeof(T));
683 pByteMaskOrig ? memcpy(&maskBuffer[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).0], pByteMaskOrig, nPix) : memset(&maskBuffer[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).0], 1, nPix);
684
685 double maxZErrL = maxZErr; // maxZErrL can get modified in the filter functions below
686
687 bool bPassNoDataValue = (pUsesNoData && (pUsesNoData[iBand] > 0));
688 const double noDataOrig = bPassNoDataValue ? noDataValues[iBand] : 0;
689
690 double noDataL = noDataOrig; // noDataL can get modified in the filter functions below
691
692 bool bIsFltDblAllInt = false; // are the flt numbers all integer really? Double can be used for int numbers beyond 32 bit range
693 bool bModifiedMask = false; // can turn true if NaN or noData found at valid pixels
694 bool bNeedNoData = false; // can only turn true for nDepth > 1, and mix of valid and invalid values at the same pixel (special case)
695 errCode = ErrCodecxx . . $ LercNS/ErrCode#::Okcxx . . $ LercNS/ErrCode#Ok.;
696
697 double minVal(+1), maxVal(-1);
698 lerc2.ClearMinMaxcxx . . $ LercNS/Lerc2#ClearMinMax(49f6e7a06ebc5aa8).();
699
700 if (bIsFltOrDbl) // if flt type, filter out NaN and / or noData values and update the mask if possible
701 {
702 errCode = FilterNoDataAndNaNcxx . . $ LercNS/Lerc#FilterNoDataAndNaN(52e685621fa17e88).(dataBuffer, maskBuffer, nDepth, nCols, nRows, maxZErrL, bPassNoDataValue, noDataL,
703 bModifiedMask, bNeedNoData, bIsFltDblAllInt, minVal, maxVal);
704 }
705 else if (bPassNoDataValue) // if int type (no NaN), and no noData value specified, nothing to do
706 {
707 errCode = FilterNoDatacxx . . $ LercNS/Lerc#FilterNoData(f77eaabeedbde36b).(dataBuffer, maskBuffer, nDepth, nCols, nRows, maxZErrL, bPassNoDataValue, noDataL,
708 bModifiedMask, bNeedNoData, minVal, maxVal);
709 }
710
711 if (errCode != ErrCodecxx . . $ LercNS/ErrCode#::Okcxx . . $ LercNS/ErrCode#Ok.)
712 return errCode;
713
714 if (bModifiedMask)
715 bAnyMaskModified = true;
716
717 bool bCompareMasks = (nMasks > 1) || bAnyMaskModified;
718
719 if (bCompareMasks && (iBand > 0) && MasksDiffercxx . . $ LercNS/Lerc#MasksDiffer(176f309453ec74c9).(&maskBuffer[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).0], pPrevByteMask, nPix))
720 bEncMsk = true;
721
722 if (nBands > 1 && iBand < nBands - 1)
723 {
724 // keep current mask as new previous band mask
725 prevMaskBuffer =cxx . . $ std/vector#`operator=`(e6b68e61becad66f). maskBuffer;
726 pPrevByteMask = &prevMaskBuffer[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).0];
727 }
728
729 const T* arrL = &dataBuffer[0];
730 const Bytecxx . . $ LercNS/Byte#* pByteMaskL = &maskBuffer[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).0];
731
732 if (bEncMsk)
733 {
734 bool bAllValid = !memchr(pByteMaskL, 0, nPix);
735
736 if (!bAllValid && !Convertcxx . . $ LercNS/Lerc#Convert(1633025d0e7a0295).(pByteMaskL, nCols, nRows, bitMask))
737 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
738
739 if (!lerc2.Setcxx . . $ LercNS/Lerc2#Set(2c716c8d5f4e4280).(nDepth, nCols, nRows, !bAllValid ? bitMask.Bitscxx . . $ LercNS/BitMask#Bits(37d8967d10cb1e7b).() : nullptr))
740 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
741 }
742
743 // set other flags
744 if ( !lerc2.SetNoDataValuescxx . . $ LercNS/Lerc2#SetNoDataValues(33f2d77f1c62171e).(bNeedNoData, noDataL, noDataOrig)
745 || !lerc2.SetNumBlobsMoreToComecxx . . $ LercNS/Lerc2#SetNumBlobsMoreToCome(83c267b20bac841a).(nBands - 1 - iBand)
746 || !lerc2.SetIsAllIntcxx . . $ LercNS/Lerc2#SetIsAllInt(efaadfb7dc14930a).(bIsFltDblAllInt))
747 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
748
749 if (nDepth == 1 && (maxVal >= minVal)
750 && !lerc2.SetMinMaxcxx . . $ LercNS/Lerc2#SetMinMax(9f72e67440d8b329).(nDepth, minVal, maxVal))
751 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
752
753 unsigned int nBytes = lerc2.ComputeNumBytesNeededToWritecxx . . $ LercNS/Lerc2#ComputeNumBytesNeededToWrite(30bb643c04a8fd76).(arrL, maxZErrL, bEncMsk);
754 if (nBytes <= 0)
755 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
756
757 if ((size_t)numBytesNeeded + nBytes > (size_t)UINT_MAXcxx . . $ `<external>/2419331716647297546/limits.h:124:9`!) // keep total blob size (over all bands) <= 4 GB
758 return ErrCodecxx . . $ LercNS/ErrCode#::DimensionsTooLargecxx . . $ LercNS/ErrCode#DimensionsTooLarge.;
759
760 numBytesNeeded += nBytes;
761
762 if (pBuffer)
763 {
764 if ((size_t)(pDst - pBuffer) + nBytes > numBytesBuffer) // check we have enough space left
765 return ErrCodecxx . . $ LercNS/ErrCode#::BufferTooSmallcxx . . $ LercNS/ErrCode#BufferTooSmall.;
766
767 #ifdef ENCODE_VERIFY
768 const Byte* pDst0 = pDst;
769 #endif
770
771 if (!lerc2.Encodecxx . . $ LercNS/Lerc2#Encode(9add4431f33ebb71).(arrL, &pDst))
772 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
773
774 #ifdef ENCODE_VERIFY
775 size_t blobSize = pDst - pDst0;
776
777 if (!DecodeAndCompareToInput(pDst0, blobSize, maxZErrL, lerc2Verify, arrL, pByteMaskL,
778 arrOrig, pByteMaskOrig, bPassNoDataValue, noDataOrig, bModifiedMask))
779 {
780 return ErrCode::Failed;
781 }
782 #endif
783
784 }
785 } // iBand
786
787 numBytesWritten = (unsigned int)(pDst - pBuffer);
788 return ErrCodecxx . . $ LercNS/ErrCode#::Okcxx . . $ LercNS/ErrCode#Ok.;
789 }
790
791 // -------------------------------------------------------------------------- ;
792
793 #ifdef HAVE_LERC1_DECODEcxx . . $ `src/LercLib/Defines.h:39:9`!
794 template<class T>
795 bool Lerccxx . . $ LercNS/Lerc#::Convertcxx . . $ LercNS/Lerc#Convert(d91d2f7413d17fb0).(const CntZImagecxx . . $ LercNS/CntZImage#& zImg, T* arr, Bytecxx . . $ LercNS/Byte#* pByteMask, bool bMustFillMask)
796 {
797 if (!arr || !zImg.getSizecxx . . $ LercNS/Image#getSize(455f465bc33b4cdf).())
798 return false;
799
800 const bool fltPnt = (typeid(*arr) == typeid(double)) || (typeid(*arr) == typeid(float));
801
802 int h = zImg.getHeightcxx . . $ LercNS/Image#getHeight(455f465bc33b4cdf).();
803 int w = zImg.getWidthcxx . . $ LercNS/Image#getWidth(455f465bc33b4cdf).();
804
805 const CntZcxx . . $ LercNS/CntZ#* srcPtr = zImg.getDatacxx . . $ LercNS/TImage#getData(1016ee5406d91ab9).();
806 T* dstPtr = arr;
807 int num = w * h;
808
809 if (pByteMask)
810 {
811 memset(pByteMask, 0, num);
812
813 for (int k = 0; k < num; k++)
814 {
815 if (srcPtr->cntcxx . . $ LercNS/CntZ#cnt. > 0)
816 {
817 *dstPtr = fltPnt ? (T)srcPtr->zcxx . . $ LercNS/CntZ#z. : (T)floor(srcPtr->zcxx . . $ LercNS/CntZ#z. + 0.5);
818 pByteMask[k] = 1;
819 }
820
821 srcPtr++;
822 dstPtr++;
823 }
824 }
825 else
826 {
827 for (int k = 0; k < num; k++)
828 {
829 if (srcPtr->cntcxx . . $ LercNS/CntZ#cnt. > 0)
830 {
831 *dstPtr = fltPnt ? (T)srcPtr->zcxx . . $ LercNS/CntZ#z. : (T)floor(srcPtr->zcxx . . $ LercNS/CntZ#z. + 0.5);
832 }
833 else if (bMustFillMask)
834 return false;
835
836 srcPtr++;
837 dstPtr++;
838 }
839 }
840
841 return true;
842 }
843 #endif
844
845 // -------------------------------------------------------------------------- ;
846
847 template<class T>
848 ErrCodecxx . . $ LercNS/ErrCode# Lerccxx . . $ LercNS/Lerc#::ConvertToDoubleTemplcxx . . $ LercNS/Lerc#ConvertToDoubleTempl(3116af53592bc7bf).(const T* pDataIn, size_t nDataValues, double* pDataOut)
849 {
850 if (!pDataIn || !nDataValues || !pDataOut)
851 return ErrCodecxx . . $ LercNS/ErrCode#::WrongParamcxx . . $ LercNS/ErrCode#WrongParam.;
852
853 for (size_t k = 0; k < nDataValues; k++)
854 pDataOut[k] = pDataIn[k];
855
856 return ErrCodecxx . . $ LercNS/ErrCode#::Okcxx . . $ LercNS/ErrCode#Ok.;
857 }
858
859 // -------------------------------------------------------------------------- ;
860
861 template<class T> ErrCodecxx . . $ LercNS/ErrCode# Lerccxx . . $ LercNS/Lerc#::CheckForNaNcxx . . $ LercNS/Lerc#CheckForNaN(db39ed3c12335d1).(const T* arr, int nDepth, int nCols, int nRows, const Bytecxx . . $ LercNS/Byte#* pByteMask)
862 {
863 if (!arr || nDepth <= 0 || nCols <= 0 || nRows <= 0)
864 return ErrCodecxx . . $ LercNS/ErrCode#::WrongParamcxx . . $ LercNS/ErrCode#WrongParam.;
865
866 if (typeid(T) != typeid(double) && typeid(T) != typeid(float))
867 return ErrCodecxx . . $ LercNS/ErrCode#::Okcxx . . $ LercNS/ErrCode#Ok.;
868
869 for (size_t k = 0, i = 0; i < (size_t)nRows; i++)
870 {
871 bool bFoundNaN = false;
872 const T* rowArr = &(arr[i * nCols * nDepth]);
873
874 if (!pByteMask) // all valid
875 {
876 size_t num = (size_t)nCols * nDepth;
877 for (size_t m = 0; m < num; m++)
878 if (stdcxx . . $ std/::isnan((double)rowArr[m]))
879 bFoundNaN = true;
880 }
881 else // not all valid
882 {
883 for (size_t n = 0, j = 0; j < (size_t)nCols; j++, k++, n += nDepth)
884 if (pByteMask[k])
885 {
886 for (int m = 0; m < nDepth; m++)
887 if (stdcxx . . $ std/::isnan((double)rowArr[n + m]))
888 bFoundNaN = true;
889 }
890 }
891
892 if (bFoundNaN)
893 return ErrCodecxx . . $ LercNS/ErrCode#::NaNcxx . . $ LercNS/ErrCode#NaN.;
894 }
895
896 return ErrCodecxx . . $ LercNS/ErrCode#::Okcxx . . $ LercNS/ErrCode#Ok.;
897 }
898
899 // -------------------------------------------------------------------------- ;
900
901 template<class T> bool Lerccxx . . $ LercNS/Lerc#::ReplaceNaNValuescxx . . $ LercNS/Lerc#ReplaceNaNValues(ad56b70f5ef9fcb2).(stdcxx . . $ std/::vectorcxx . . $ std/vector#<T>& dataBuffer, stdcxx . . $ std/::vectorcxx . . $ std/vector#<Bytecxx . . $ LercNS/Byte#>& maskBuffer, int nDepth, int nCols, int nRows)
902 {
903 if (nDepth <= 0 || nCols <= 0 || nRows <= 0 || dataBuffer.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() !=cxx . . $ std/`operator!=`(39b98f4fe21ae589). (size_t)nDepth * nCols * nRows || maskBuffer.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() != (size_t)nCols * nRows)
904 return false;
905
906 T noDataValue = 0;
907 {
908 #if defined _WIN32
909 #pragma warning(disable: 4756) // would trigger build warning in linux
910 #endif
911
912 noDataValue = (T)((typeid(T) == typeid(float)) ? -FLT_MAXcxx . . $ `<external>/8223903785965736313/float.h:104:9`! : -DBL_MAXcxx . . $ `<external>/8223903785965736313/float.h:105:9`!);
913 }
914
915 for (size_t k = 0, i = 0; i < (size_t)nRows; i++)
916 {
917 T* rowArr = &(dataBuffer[i * nCols * nDepth]);
918
919 for (size_t n = 0, j = 0; j < (size_t)nCols; j++, k++, n += nDepth)
920 {
921 if (maskBuffer[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).k])
922 {
923 int cntNaN = 0;
924
925 for (int m = 0; m < nDepth; m++)
926 if (stdcxx . . $ std/::isnan((double)rowArr[n + m]))
927 {
928 cntNaN++;
929 rowArr[n + m] = noDataValue;
930 }
931
932 if (cntNaN == nDepth)
933 maskBuffer[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).k] = 0;
934 }
935 }
936 }
937
938 return true;
939 }
940
941 // -------------------------------------------------------------------------- ;
942
943 template<class T> bool Lerccxx . . $ LercNS/Lerc#::Resizecxx . . $ LercNS/Lerc#Resize(e6f179a37c0c9e9e).(stdcxx . . $ std/::vectorcxx . . $ std/vector#<T>& buffer, size_t nElem)
944 {
945 try
946 {
947 buffer.resizecxx . . $ std/vector#resize(2c44cd906ae4e829).(nElem);
948 }
949 catch (...)
950 {
951 return false;
952 }
953
954 return (buffer.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() ==cxx . . $ std/`operator==`(39b98f4fe21ae589). nElem);
955 }
956
957 // -------------------------------------------------------------------------- ;
958
959 bool Lerccxx . . $ LercNS/Lerc#::Convertcxx . . $ LercNS/Lerc#Convert(1633025d0e7a0295).(const Bytecxx . . $ LercNS/Byte#* pByteMask, int nCols, int nRows, BitMaskcxx . . $ LercNS/BitMask#& bitMask)
960 {
961 if (!pByteMask || nCols <= 0 || nRows <= 0)
962 return false;
963
964 if (!bitMask.SetSizecxx . . $ LercNS/BitMask#SetSize(cf9ef193275fa6d8).(nCols, nRows))
965 return false;
966
967 bitMask.SetAllValidcxx . . $ LercNS/BitMask#SetAllValid(83f5f38750778ac8).();
968
969 for (int k = 0, i = 0; i < nRows; i++)
970 for (int j = 0; j < nCols; j++, k++)
971 if (!pByteMask[k])
972 bitMask.SetInvalidcxx . . $ LercNS/BitMask#SetInvalid(32107cb1e3ffb1ae).(k);
973
974 return true;
975 }
976
977 // -------------------------------------------------------------------------- ;
978
979 bool Lerccxx . . $ LercNS/Lerc#::Convertcxx . . $ LercNS/Lerc#Convert(54c99c5ab6149a98).(const BitMaskcxx . . $ LercNS/BitMask#& bitMask, Bytecxx . . $ LercNS/Byte#* pByteMask)
980 {
981 int nCols = bitMask.GetWidthcxx . . $ LercNS/BitMask#GetWidth(455f465bc33b4cdf).();
982 int nRows = bitMask.GetHeightcxx . . $ LercNS/BitMask#GetHeight(455f465bc33b4cdf).();
983
984 if (nCols <= 0 || nRows <= 0 || !pByteMask)
985 return false;
986
987 memset(pByteMask, 0, (size_t)nCols * nRows);
988
989 for (int k = 0, i = 0; i < nRows; i++)
990 for (int j = 0; j < nCols; j++, k++)
991 if (bitMask.IsValidcxx . . $ LercNS/BitMask#IsValid(7c969c143e367631).(k))
992 pByteMask[k] = 1;
993
994 return true;
995 }
996
997 // -------------------------------------------------------------------------- ;
998
999 bool Lerccxx . . $ LercNS/Lerc#::MasksDiffercxx . . $ LercNS/Lerc#MasksDiffer(176f309453ec74c9).(const Bytecxx . . $ LercNS/Byte#* p0, const Bytecxx . . $ LercNS/Byte#* p1, size_t n)
1000 {
1001 if (p0 == p1)
1002 return false;
1003
1004 if (!p0) // means all valid
1005 return memchr(p1, 0, n); // any invalid?
1006 else if (!p1)
1007 return memchr(p0, 0, n);
1008 else
1009 return memcmp(p0, p1, n);
1010 }
1011
1012 // -------------------------------------------------------------------------- ;
1013
1014 ErrCodecxx . . $ LercNS/ErrCode# Lerccxx . . $ LercNS/Lerc#::GetRangescxx . . $ LercNS/Lerc#GetRanges(771fda4212732f67).(const Bytecxx . . $ LercNS/Byte#* pLercBlob, unsigned int numBytesBlob, int iBand,
1015 const struct Lerc2cxx . . $ LercNS/Lerc2#::HeaderInfocxx . . $ LercNS/Lerc2#HeaderInfo#& lerc2Info, double* pMins, double* pMaxs, size_t nElem)
1016 {
1017 const int nDepth = lerc2Info.nDepthcxx . . $ LercNS/Lerc2#HeaderInfo#nDepth.;
1018
1019 if (nDepth <= 0 || iBand < 0 || !pMins || !pMaxs)
1020 return ErrCodecxx . . $ LercNS/ErrCode#::WrongParamcxx . . $ LercNS/ErrCode#WrongParam.;
1021
1022 if (nElem < ((size_t)iBand + 1) * (size_t)nDepth)
1023 return ErrCodecxx . . $ LercNS/ErrCode#::BufferTooSmallcxx . . $ LercNS/ErrCode#BufferTooSmall.;
1024
1025 if (nDepth == 1)
1026 {
1027 pMins[iBand] = lerc2Info.zMincxx . . $ LercNS/Lerc2#HeaderInfo#zMin.;
1028 pMaxs[iBand] = lerc2Info.zMaxcxx . . $ LercNS/Lerc2#HeaderInfo#zMax.;
1029 }
1030 else
1031 {
1032 if (lerc2Info.bPassNoDataValuescxx . . $ LercNS/Lerc2#HeaderInfo#bPassNoDataValues.) // for nDepth > 1, and mix of valid and invalid values at same pixel, better fail
1033 return ErrCodecxx . . $ LercNS/ErrCode#::HasNoDatacxx . . $ LercNS/ErrCode#HasNoData.; // than return min or max values that contain noData (to be fixed in next codec version)
1034
1035 // read header, mask, ranges, and copy them out
1036 Lerc2cxx . . $ LercNS/Lerc2# lerc2cxx . . $ LercNS/Lerc2#Lerc2(49f6e7a06ebc5aa8).;
1037 if (!lerc2.GetRangescxx . . $ LercNS/Lerc2#GetRanges(bfe330d8d1b8c5bf).(pLercBlob, numBytesBlob, &pMins[iBand * nDepth], &pMaxs[iBand * nDepth]))
1038 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
1039 }
1040
1041 return ErrCodecxx . . $ LercNS/ErrCode#::Okcxx . . $ LercNS/ErrCode#Ok.;
1042 }
1043
1044 // -------------------------------------------------------------------------- ;
1045
1046 template<class T>
1047 bool Lerccxx . . $ LercNS/Lerc#::RemapNoDatacxx . . $ LercNS/Lerc#RemapNoData(f51e42f49da7dd89).(T* data, const BitMaskcxx . . $ LercNS/BitMask#& bitMask, const struct Lerc2cxx . . $ LercNS/Lerc2#::HeaderInfocxx . . $ LercNS/Lerc2#HeaderInfo#& lerc2Info)
1048 {
1049 int nCols = lerc2Info.nColscxx . . $ LercNS/Lerc2#HeaderInfo#nCols.;
1050 int nRows = lerc2Info.nRowscxx . . $ LercNS/Lerc2#HeaderInfo#nRows.;
1051 int nDepth = lerc2Info.nDepthcxx . . $ LercNS/Lerc2#HeaderInfo#nDepth.;
1052
1053 if (!data || nCols <= 0 || nRows <= 0 || nDepth <= 0)
1054 return false;
1055
1056 const T noDataOld = (T)lerc2Info.noDataValcxx . . $ LercNS/Lerc2#HeaderInfo#noDataVal.;
1057 const T noDataNew = (T)lerc2Info.noDataValOrigcxx . . $ LercNS/Lerc2#HeaderInfo#noDataValOrig.;
1058
1059 if (noDataNew !=cxx . . $ std/`operator!=`(39b98f4fe21ae589). noDataOld)
1060 {
1061 bool bUseMask = (bitMask.GetWidthcxx . . $ LercNS/BitMask#GetWidth(455f465bc33b4cdf).() == nCols) && (bitMask.GetHeightcxx . . $ LercNS/BitMask#GetHeight(455f465bc33b4cdf).() == nRows);
1062
1063 for (long k = 0, i = 0; i < nRows; i++)
1064 {
1065 T* rowArr = &(data[i * nCols * nDepth]);
1066
1067 for (long n = 0, j = 0; j < nCols; j++, k++, n += nDepth)
1068 if (!bUseMask || bitMask.IsValidcxx . . $ LercNS/BitMask#IsValid(7c969c143e367631).(k))
1069 for (long m = 0; m < nDepth; m++)
1070 if (rowArr[n + m] ==cxx . . $ std/`operator==`(39b98f4fe21ae589). noDataOld)
1071 rowArr[n + m] = noDataNew;
1072 }
1073 }
1074
1075 return true;
1076 }
1077
1078 // -------------------------------------------------------------------------- ;
1079
1080 template<class T>
1081 bool Lerccxx . . $ LercNS/Lerc#::DecodeAndCompareToInputcxx . . $ LercNS/Lerc#DecodeAndCompareToInput(d81b550f31abee77).(const Bytecxx . . $ LercNS/Byte#* pLercBlob, size_t blobSize, double maxZErr, Lerc2cxx . . $ LercNS/Lerc2#& lerc2Verify,
1082 const T* pData, const Bytecxx . . $ LercNS/Byte#* pByteMask, const T* pDataOrig, const Bytecxx . . $ LercNS/Byte#* pByteMaskOrig,
1083 bool bInputHasNoData, double origNoDataA, bool bModifiedMask)
1084 {
1085 if (!pLercBlob || !pData || !pDataOrig)
1086 return false;
1087
1088 const Bytecxx . . $ LercNS/Byte#* bytePtr = pLercBlob;
1089 size_t nBytesRemaining = blobSize;
1090
1091 bool bHasMask(false);
1092 Lerc2cxx . . $ LercNS/Lerc2#::HeaderInfocxx . . $ LercNS/Lerc2#HeaderInfo# hd;
1093 if (!Lerc2cxx . . $ LercNS/Lerc2#::GetHeaderInfocxx . . $ LercNS/Lerc2#GetHeaderInfo(5bafa162245ba3da).(bytePtr, nBytesRemaining, hd, bHasMask))
1094 return false;
1095
1096 stdcxx . . $ std/::vectorcxx . . $ std/vector#<T> arrDec;
1097 try
1098 {
1099 arrDec.assigncxx . . $ std/vector#assign(3804226c8c0acbc3).(hd.nRowscxx . . $ LercNS/Lerc2#HeaderInfo#nRows. * hd.nColscxx . . $ LercNS/Lerc2#HeaderInfo#nCols. * hd.nDepthcxx . . $ LercNS/Lerc2#HeaderInfo#nDepth., 0);
1100 }
1101 catch (...)
1102 {
1103 return false;
1104 }
1105
1106 BitMaskcxx . . $ LercNS/BitMask# bitMaskDeccxx . . $ LercNS/BitMask#BitMask(49f6e7a06ebc5aa8).;
1107 if (!bitMaskDec.SetSizecxx . . $ LercNS/BitMask#SetSize(cf9ef193275fa6d8).(hd.nColscxx . . $ LercNS/Lerc2#HeaderInfo#nCols., hd.nRowscxx . . $ LercNS/Lerc2#HeaderInfo#nRows.))
1108 return false;
1109
1110 bitMaskDec.SetAllInvalidcxx . . $ LercNS/BitMask#SetAllInvalid(83f5f38750778ac8).();
1111
1112 if (!lerc2Verify.Decodecxx . . $ LercNS/Lerc2#Decode(1e98a814b12037d0).(&bytePtr, nBytesRemaining, &arrDec[0], bitMaskDec.Bitscxx . . $ LercNS/BitMask#Bits(37d8967d10cb1e7b).()))
1113 return false;
1114
1115 // compare decoded bit mask and data array against the input to lerc encode (as after that orig input had the noData value remapped, NaN removed, bit mask altered)
1116 {
1117 bool bHasMaskBug(false);
1118 double maxDelta = 0;
1119
1120 for (int k = 0, i = 0; i < hd.nRowscxx . . $ LercNS/Lerc2#HeaderInfo#nRows.; i++)
1121 for (int j = 0; j < hd.nColscxx . . $ LercNS/Lerc2#HeaderInfo#nCols.; j++, k++)
1122 if (bitMaskDec.IsValidcxx . . $ LercNS/BitMask#IsValid(7c969c143e367631).(k))
1123 {
1124 if (pByteMask && !pByteMask[k])
1125 bHasMaskBug = true;
1126
1127 for (int n = k * hd.nDepthcxx . . $ LercNS/Lerc2#HeaderInfo#nDepth., m = 0; m < hd.nDepthcxx . . $ LercNS/Lerc2#HeaderInfo#nDepth.; m++, n++)
1128 {
1129 double d = fabs((double)arrDec[n] - (double)pData[n]);
1130 if (d > maxDelta)
1131 maxDelta = d;
1132 }
1133 }
1134 else if (!pByteMask || pByteMask[k])
1135 bHasMaskBug = true;
1136
1137 if (bHasMaskBug || maxDelta > maxZErr * 1.1) // consider float rounding errors
1138 return false;
1139 }
1140
1141 if (!bInputHasNoData && !bModifiedMask) // any NaN in the input means either modify the mask or replace it by noData value
1142 return true;
1143
1144 const bool bIsFltOrDbl = (typeid(T) == typeid(double) || typeid(T) == typeid(float));
1145 const bool bHaveNoDataVal = (hd.versioncxx . . $ LercNS/Lerc2#HeaderInfo#version. >= 6 && hd.bPassNoDataValuescxx . . $ LercNS/Lerc2#HeaderInfo#bPassNoDataValues. && hd.nDepthcxx . . $ LercNS/Lerc2#HeaderInfo#nDepth. > 1);
1146
1147 if (bHaveNoDataVal && hd.noDataValOrigcxx . . $ LercNS/Lerc2#HeaderInfo#noDataValOrig. != origNoDataA)
1148 return false;
1149
1150 if (bHaveNoDataVal && hd.noDataValcxx . . $ LercNS/Lerc2#HeaderInfo#noDataVal. != hd.noDataValOrigcxx . . $ LercNS/Lerc2#HeaderInfo#noDataValOrig.)
1151 {
1152 // remap the noData value from internal to orig
1153 if (!RemapNoDatacxx . . $ LercNS/Lerc#RemapNoData(f51e42f49da7dd89).(&arrDec[0], bitMaskDec, hd))
1154 return false;
1155 }
1156
1157 // compare the final result to orig bit mask and data array as it was handed to the Compress() function
1158 {
1159 T noDataOrig = (T)origNoDataA;
1160 double maxDelta = 0;
1161 bool bHasBug(false);
1162
1163 for (int k = 0, i = 0; i < hd.nRowscxx . . $ LercNS/Lerc2#HeaderInfo#nRows.; i++)
1164 for (int j = 0; j < hd.nColscxx . . $ LercNS/Lerc2#HeaderInfo#nCols.; j++, k++)
1165 if (!pByteMaskOrig || pByteMaskOrig[k])
1166 {
1167 if (!bitMaskDec.IsValidcxx . . $ LercNS/BitMask#IsValid(7c969c143e367631).(k)) // then the orig data values must be noData or NaN
1168 {
1169 for (int n = k * hd.nDepthcxx . . $ LercNS/Lerc2#HeaderInfo#nDepth., m = 0; m < hd.nDepthcxx . . $ LercNS/Lerc2#HeaderInfo#nDepth.; m++, n++)
1170 {
1171 T zOrig = pDataOrig[n];
1172 bool bIsNoData = (bInputHasNoData && zOrig ==cxx . . $ std/`operator==`(39b98f4fe21ae589). noDataOrig) || (bIsFltOrDbl && stdcxx . . $ std/::isnan((double)zOrig));
1173
1174 if (!bIsNoData)
1175 bHasBug = true;
1176 }
1177 }
1178 else // valid pixel
1179 {
1180 for (int n = k * hd.nDepthcxx . . $ LercNS/Lerc2#HeaderInfo#nDepth., m = 0; m < hd.nDepthcxx . . $ LercNS/Lerc2#HeaderInfo#nDepth.; m++, n++)
1181 {
1182 T zOrig = pDataOrig[n];
1183 T z = arrDec[n];
1184
1185 if (z ==cxx . . $ std/`operator==`(39b98f4fe21ae589). zOrig) // valid value or noData
1186 continue;
1187
1188 if (bIsFltOrDbl && stdcxx . . $ std/::isnan((double)zOrig))
1189 zOrig = noDataOrig;
1190
1191 if (bInputHasNoData && (z ==cxx . . $ std/`operator==`(39b98f4fe21ae589). noDataOrig || zOrig ==cxx . . $ std/`operator==`(39b98f4fe21ae589). noDataOrig) && (z !=cxx . . $ std/`operator!=`(39b98f4fe21ae589). zOrig))
1192 bHasBug = true;
1193
1194 if (!bHaveNoDataVal || z !=cxx . . $ std/`operator!=`(39b98f4fe21ae589). noDataOrig) // is valid
1195 {
1196 double d = fabs((double)z - (double)zOrig);
1197 if (d > maxDelta)
1198 maxDelta = d;
1199 }
1200 }
1201 }
1202 }
1203 else if (bitMaskDec.IsValidcxx . . $ LercNS/BitMask#IsValid(7c969c143e367631).(k))
1204 bHasBug = true;
1205
1206 if (bHasBug || maxDelta > maxZErr * 1.1) // consider float rounding errors
1207 return false;
1208 }
1209
1210 return true;
1211 }
1212
1213 // -------------------------------------------------------------------------- ;
1214
1215 template<class T>
1216 bool Lerccxx . . $ LercNS/Lerc#::GetTypeRangecxx . . $ LercNS/Lerc#GetTypeRange(7b655ca3a0607607).(const T, stdcxx . . $ std/::paircxx . . $ std/pair#<double, double>& range)
1217 {
1218 range.firstcxx . . $ std/pair#first. = 0;
1219
1220 if (typeid(T) == typeid(Bytecxx . . $ LercNS/Byte#))
1221 range.secondcxx . . $ std/pair#second. = UCHAR_MAXcxx . . $ `<external>/2419331716647297546/limits.h:82:10`!;
1222 else if (typeid(T) == typeid(unsigned short))
1223 range.secondcxx . . $ std/pair#second. = USHRT_MAXcxx . . $ `<external>/2419331716647297546/limits.h:113:10`!;
1224 else if (typeid(T) == typeid(unsigned int) || typeid(T) == typeid(unsigned long))
1225 range.secondcxx . . $ std/pair#second. = UINT_MAXcxx . . $ `<external>/2419331716647297546/limits.h:124:9`!;
1226
1227 else if (typeid(T) == typeid(signed char))
1228 range =cxx . . $ std/pair#`operator=`(225bd4d59bf12f87). stdcxx . . $ std/pair#pair(14d64bafa7379ebc).::paircxx . . $ std/pair#<double, double>(CHAR_MINcxx . . $ `<external>/2419331716647297546/limits.h:97:10`!, CHAR_MAXcxx . . $ `<external>/2419331716647297546/limits.h:99:10`!);
1229 else if (typeid(T) == typeid(short))
1230 range =cxx . . $ std/pair#`operator=`(225bd4d59bf12f87). stdcxx . . $ std/pair#pair(14d64bafa7379ebc).::paircxx . . $ std/pair#<double, double>(SHRT_MINcxx . . $ `<external>/2419331716647297546/limits.h:104:9`!, SHRT_MAXcxx . . $ `<external>/2419331716647297546/limits.h:106:9`!);
1231 else if (typeid(T) == typeid(int) || typeid(T) == typeid(long))
1232 range =cxx . . $ std/pair#`operator=`(225bd4d59bf12f87). stdcxx . . $ std/pair#pair(14d64bafa7379ebc).::paircxx . . $ std/pair#<double, double>(INT_MINcxx . . $ `<external>/2419331716647297546/limits.h:118:9`!, INT_MAXcxx . . $ `<external>/2419331716647297546/limits.h:120:9`!);
1233 else
1234 return false;
1235
1236 return true;
1237 }
1238
1239 // -------------------------------------------------------------------------- ;
1240
1241 template<class T>
1242 ErrCodecxx . . $ LercNS/ErrCode# Lerccxx . . $ LercNS/Lerc#::FilterNoDatacxx . . $ LercNS/Lerc#FilterNoData(f77eaabeedbde36b).(stdcxx . . $ std/::vectorcxx . . $ std/vector#<T>& dataBuffer, stdcxx . . $ std/::vectorcxx . . $ std/vector#<Bytecxx . . $ LercNS/Byte#>& maskBuffer, int nDepth, int nCols, int nRows,
1243 double& maxZError, bool bPassNoDataValue, double& noDataValue, bool& bModifiedMask, bool& bNeedNoData,
1244 double& minValA, double& maxValA)
1245 {
1246 if (nDepth <= 0 || nCols <= 0 || nRows <= 0 || maxZError < 0)
1247 return ErrCodecxx . . $ LercNS/ErrCode#::WrongParamcxx . . $ LercNS/ErrCode#WrongParam.;
1248
1249 if ((dataBuffer.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() !=cxx . . $ std/`operator!=`(39b98f4fe21ae589). (size_t)nDepth * nCols * nRows) || (maskBuffer.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() != (size_t)nCols * nRows))
1250 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
1251
1252 bModifiedMask = false;
1253 bNeedNoData = false;
1254
1255 if (!bPassNoDataValue) // nothing to do
1256 return ErrCodecxx . . $ LercNS/ErrCode#::Okcxx . . $ LercNS/ErrCode#Ok.;
1257
1258 stdcxx . . $ std/::paircxx . . $ std/pair#<double, double> typeRangecxx . . $ std/pair#pair(49f6e7a06ebc5aa8).;
1259 if (!GetTypeRangecxx . . $ LercNS/Lerc#GetTypeRange(7b655ca3a0607607).(dataBuffer[0], typeRange))
1260 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
1261
1262 if (noDataValue < typeRange.firstcxx . . $ std/pair#first. || noDataValue > typeRange.secondcxx . . $ std/pair#second.)
1263 return ErrCodecxx . . $ LercNS/ErrCode#::WrongParamcxx . . $ LercNS/ErrCode#WrongParam.;
1264
1265 T origNoData = (T)noDataValue;
1266
1267 double minVal = DBL_MAXcxx . . $ `<external>/8223903785965736313/float.h:105:9`!;
1268 double maxVal = -DBL_MAXcxx . . $ `<external>/8223903785965736313/float.h:105:9`!;
1269
1270 // check for noData in valid pixels
1271 for (int k = 0, i = 0; i < nRows; i++)
1272 {
1273 T* rowArr = &(dataBuffer[(size_t)i * nCols * nDepth]);
1274
1275 for (int n = 0, j = 0; j < nCols; j++, k++, n += nDepth)
1276 if (maskBuffer[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).k])
1277 {
1278 int cntInvalid = 0;
1279
1280 for (int m = 0; m < nDepth; m++)
1281 {
1282 T z = rowArr[n + m];
1283
1284 if (z ==cxx . . $ std/`operator==`(39b98f4fe21ae589). origNoData)
1285 cntInvalid++;
1286 else
1287 {
1288 if (z <cxx . . $ std/`operator<`(39b98f4fe21ae589). minVal)
1289 minVal = z;
1290 if (z >cxx . . $ std/`operator>`(39b98f4fe21ae589). maxVal)
1291 maxVal = z;
1292 }
1293 }
1294
1295 if (cntInvalid == nDepth)
1296 {
1297 maskBuffer[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).k] = 0;
1298 bModifiedMask = true;
1299 }
1300 else if (cntInvalid > 0) // found mix of valid and invalid values at the same pixel
1301 bNeedNoData = true;
1302 }
1303 }
1304
1305 double maxZErrL = (stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).)(0.5, floor(maxZError)); // same mapping for int types as in Lerc2.cpp
1306 double dist = floor(maxZErrL);
1307
1308 if (minVal == DBL_MAXcxx . . $ `<external>/8223903785965736313/float.h:105:9`! && maxVal == -DBL_MAXcxx . . $ `<external>/8223903785965736313/float.h:105:9`!) // if the tile has no valid data
1309 {
1310 minValA = maxValA = 0;
1311 maxZError = 0.5;
1312 return ErrCodecxx . . $ LercNS/ErrCode#::Okcxx . . $ LercNS/ErrCode#Ok.;
1313 }
1314
1315 minValA = minVal;
1316 maxValA = maxVal;
1317
1318 // check the orig noData value is far enough away from the valid range
1319 if ((origNoData >=cxx . . $ std/`operator>=`(39b98f4fe21ae589). minVal - dist) && (origNoData <=cxx . . $ std/`operator<=`(39b98f4fe21ae589). maxVal + dist))
1320 {
1321 maxZError = 0.5; // fall back to int lossless
1322 return ErrCodecxx . . $ LercNS/ErrCode#::Okcxx . . $ LercNS/ErrCode#Ok.;
1323 }
1324
1325 if (bNeedNoData)
1326 {
1327 double minDist = floor(maxZErrL) + 1;
1328 double remapVal = minVal - minDist;
1329 T newNoData = origNoData;
1330
1331 if (remapVal >= typeRange.firstcxx . . $ std/pair#first.)
1332 {
1333 newNoData = (T)remapVal;
1334 }
1335 else
1336 {
1337 maxZErrL = 0.5; // repeat with int lossless
1338 remapVal = minVal - 1;
1339
1340 if (remapVal >= typeRange.firstcxx . . $ std/pair#first.)
1341 {
1342 newNoData = (T)remapVal;
1343 }
1344 else
1345 {
1346 remapVal = maxVal + 1; // try to map closer to valid range from top
1347
1348 if ((remapVal <= typeRange.secondcxx . . $ std/pair#second.) && (remapVal <cxx . . $ std/`operator<`(39b98f4fe21ae589). origNoData))
1349 newNoData = (T)remapVal;
1350 }
1351 }
1352
1353 if (newNoData !=cxx . . $ std/`operator!=`(39b98f4fe21ae589). origNoData)
1354 {
1355 for (int k = 0, i = 0; i < nRows; i++)
1356 {
1357 T* rowArr = &(dataBuffer[(size_t)i * nCols * nDepth]);
1358
1359 for (int n = 0, j = 0; j < nCols; j++, k++, n += nDepth)
1360 if (maskBuffer[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).k])
1361 for (int m = 0; m < nDepth; m++)
1362 if (rowArr[n + m] ==cxx . . $ std/`operator==`(39b98f4fe21ae589). origNoData)
1363 rowArr[n + m] = newNoData;
1364 }
1365
1366 noDataValue = newNoData;
1367 }
1368 }
1369
1370 if (maxZError != maxZErrL)
1371 maxZError = maxZErrL;
1372
1373 return ErrCodecxx . . $ LercNS/ErrCode#::Okcxx . . $ LercNS/ErrCode#Ok.;
1374 }
1375
1376 // -------------------------------------------------------------------------- ;
1377
1378 template<class T>
1379 ErrCodecxx . . $ LercNS/ErrCode# Lerccxx . . $ LercNS/Lerc#::FilterNoDataAndNaNcxx . . $ LercNS/Lerc#FilterNoDataAndNaN(52e685621fa17e88).(stdcxx . . $ std/::vectorcxx . . $ std/vector#<T>& dataBuffer, stdcxx . . $ std/::vectorcxx . . $ std/vector#<Bytecxx . . $ LercNS/Byte#>& maskBuffer, int nDepth, int nCols, int nRows,
1380 double& maxZError, bool bPassNoDataValue, double& noDataValue, bool& bModifiedMask, bool& bNeedNoData, bool& bIsFltDblAllInt,
1381 double& minValA, double& maxValA)
1382 {
1383 if (nDepth <= 0 || nCols <= 0 || nRows <= 0 || maxZError < 0)
1384 return ErrCodecxx . . $ LercNS/ErrCode#::WrongParamcxx . . $ LercNS/ErrCode#WrongParam.;
1385
1386 if ((dataBuffer.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() !=cxx . . $ std/`operator!=`(39b98f4fe21ae589). (size_t)nDepth * nCols * nRows) || (maskBuffer.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() != (size_t)nCols * nRows))
1387 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
1388
1389 if (typeid(T) != typeid(double) && typeid(T) != typeid(float)) // only for float or double
1390 return ErrCodecxx . . $ LercNS/ErrCode#::Failedcxx . . $ LercNS/ErrCode#Failed.;
1391
1392 bModifiedMask = false;
1393 bNeedNoData = false;
1394 bIsFltDblAllInt = false;
1395
1396 bool bHasNoDataValuesLeft = false;
1397 bool bIsFloat4 = (typeid(T) == typeid(float));
1398 bool bAllInt = true;
1399 bool bHasNaN = false;
1400
1401 T origNoData(0);
1402
1403 if (bPassNoDataValue)
1404 {
1405 if (bIsFloat4 && (noDataValue < -FLT_MAXcxx . . $ `<external>/8223903785965736313/float.h:104:9`! || noDataValue > FLT_MAXcxx . . $ `<external>/8223903785965736313/float.h:104:9`!))
1406 return ErrCodecxx . . $ LercNS/ErrCode#::WrongParamcxx . . $ LercNS/ErrCode#WrongParam.;
1407
1408 origNoData = (T)noDataValue;
1409 }
1410 else
1411 origNoData = (T)(bIsFloat4 ? -FLT_MAXcxx . . $ `<external>/8223903785965736313/float.h:104:9`! : -DBL_MAXcxx . . $ `<external>/8223903785965736313/float.h:105:9`!);
1412
1413 // only treat int values as int's if within this range, incl orig noData
1414 const double lowIntLimit = (double)(bIsFloat4 ? -((long)1 << 23) : -((int64_tcxx . . $ int64_t#)1 << 53));
1415 const double highIntLimit = (double)(bIsFloat4 ? ((long)1 << 23) : ((int64_tcxx . . $ int64_t#)1 << 53));
1416
1417 double minVal = DBL_MAXcxx . . $ `<external>/8223903785965736313/float.h:105:9`!;
1418 double maxVal = -DBL_MAXcxx . . $ `<external>/8223903785965736313/float.h:105:9`!;
1419
1420 // check for NaN or noData in valid pixels
1421 for (int k = 0, i = 0; i < nRows; i++)
1422 {
1423 T* rowArr = &(dataBuffer[(size_t)i * nCols * nDepth]);
1424
1425 for (int n = 0, j = 0; j < nCols; j++, k++, n += nDepth)
1426 if (maskBuffer[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).k])
1427 {
1428 int cntInvalidValues = 0;
1429
1430 for (int m = 0; m < nDepth; m++)
1431 {
1432 T& zVal = rowArr[n + m];
1433
1434 if (stdcxx . . $ std/::isnan((double)zVal))
1435 {
1436 bHasNaN = true;
1437 cntInvalidValues++;
1438
1439 if (bPassNoDataValue && nDepth > 1)
1440 zVal = origNoData; // replace NaN
1441 else if (nDepth == 1)
1442 zVal = 0;
1443 }
1444 else if (bPassNoDataValue && zVal ==cxx . . $ std/`operator==`(39b98f4fe21ae589). origNoData)
1445 {
1446 cntInvalidValues++;
1447 }
1448 else
1449 {
1450 if (zVal <cxx . . $ std/`operator<`(39b98f4fe21ae589). minVal)
1451 minVal = zVal;
1452 if (zVal >cxx . . $ std/`operator>`(39b98f4fe21ae589). maxVal)
1453 maxVal = zVal;
1454
1455 if (bAllInt && !IsIntcxx . . $ LercNS/Lerc#IsInt(ee44d93984fb03e9).(zVal))
1456 bAllInt = false;
1457 }
1458 }
1459
1460 if (cntInvalidValues == nDepth)
1461 {
1462 maskBuffer[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).k] = 0;
1463 bModifiedMask = true;
1464 }
1465 else if (cntInvalidValues > 0) // found mix of valid and invalid values at the same pixel
1466 bHasNoDataValuesLeft = true;
1467 }
1468 }
1469
1470 if (minVal == DBL_MAXcxx . . $ `<external>/8223903785965736313/float.h:105:9`! && maxVal == -DBL_MAXcxx . . $ `<external>/8223903785965736313/float.h:105:9`!) // if the tile has no valid data
1471 {
1472 minValA = maxValA = 0;
1473 maxZError = 0;
1474 return ErrCodecxx . . $ LercNS/ErrCode#::Okcxx . . $ LercNS/ErrCode#Ok.;
1475 }
1476
1477 minValA = minVal;
1478 maxValA = maxVal;
1479 bNeedNoData = bHasNoDataValuesLeft;
1480
1481 if (bHasNaN && nDepth > 1 && bHasNoDataValuesLeft && !bPassNoDataValue)
1482 {
1483 return ErrCodecxx . . $ LercNS/ErrCode#::NaNcxx . . $ LercNS/ErrCode#NaN.; // Lerc cannot handle this case, cannot pick a noData value on the tile level
1484 }
1485
1486 // now NaN's are gone, either moved to the mask or replaced by noData value
1487
1488 double maxZErrL = maxZError;
1489
1490 if (bAllInt)
1491 {
1492 bAllInt &= (minVal >= lowIntLimit) && (minVal <= highIntLimit)
1493 && (maxVal >= lowIntLimit) && (maxVal <= highIntLimit);
1494
1495 if (bHasNoDataValuesLeft)
1496 bAllInt &= IsIntcxx . . $ LercNS/Lerc#IsInt(ee44d93984fb03e9).(origNoData) && (origNoData >=cxx . . $ std/`operator>=`(39b98f4fe21ae589). lowIntLimit) && (origNoData <=cxx . . $ std/`operator<=`(39b98f4fe21ae589). highIntLimit);
1497
1498 if (bAllInt)
1499 maxZErrL = (stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).)(0.5, floor(maxZError)); // same mapping for int types as in Lerc2.cpp
1500 }
1501
1502 bIsFltDblAllInt = bAllInt;
1503
1504 if (maxZErrL == 0) // if flt lossless, we are done
1505 return ErrCodecxx . . $ LercNS/ErrCode#::Okcxx . . $ LercNS/ErrCode#Ok.;
1506
1507 if (bPassNoDataValue)
1508 {
1509 // check the orig noData value is far enough away from the valid range
1510 double dist = bAllInt ? floor(maxZErrL) : 2 * maxZErrL;
1511
1512 if ((origNoData >=cxx . . $ std/`operator>=`(39b98f4fe21ae589). minVal - dist) && (origNoData <=cxx . . $ std/`operator<=`(39b98f4fe21ae589). maxVal + dist))
1513 {
1514 maxZError = bAllInt ? 0.5 : 0; // fall back to lossless
1515 return ErrCodecxx . . $ LercNS/ErrCode#::Okcxx . . $ LercNS/ErrCode#Ok.;
1516 }
1517 }
1518
1519 if (bHasNoDataValuesLeft) // try to remap noData to new noData just below the global minimum
1520 {
1521 T remapVal = origNoData;
1522 bool bRemapNoData = FindNewNoDataBelowValidMincxx . . $ LercNS/Lerc#FindNewNoDataBelowValidMin(98c0c5cb235ea887).(minVal, maxZErrL, bAllInt, lowIntLimit, remapVal);
1523
1524 if (bRemapNoData)
1525 {
1526 if (remapVal !=cxx . . $ std/`operator!=`(39b98f4fe21ae589). origNoData)
1527 {
1528 for (int k = 0, i = 0; i < nRows; i++)
1529 {
1530 T* rowArr = &(dataBuffer[(size_t)i * nCols * nDepth]);
1531
1532 for (int n = 0, j = 0; j < nCols; j++, k++, n += nDepth)
1533 if (maskBuffer[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).k])
1534 for (int m = 0; m < nDepth; m++)
1535 if (rowArr[n + m] ==cxx . . $ std/`operator==`(39b98f4fe21ae589). origNoData)
1536 rowArr[n + m] = remapVal;
1537 }
1538
1539 noDataValue = remapVal;
1540 }
1541 }
1542 else if ((double)origNoData >= minVal) // sufficient distance of origNoData to valid range has been checked above
1543 {
1544 maxZErrL = bAllInt ? 0.5 : 0; // need lossless if noData cannot be mapped below valid range
1545 }
1546 }
1547
1548 if (maxZError != maxZErrL)
1549 maxZError = maxZErrL;
1550
1551 return ErrCodecxx . . $ LercNS/ErrCode#::Okcxx . . $ LercNS/ErrCode#Ok.;
1552 }
1553
1554 // -------------------------------------------------------------------------- ;
1555
1556 template<class T>
1557 bool Lerccxx . . $ LercNS/Lerc#::FindNewNoDataBelowValidMincxx . . $ LercNS/Lerc#FindNewNoDataBelowValidMin(98c0c5cb235ea887).(double minVal, double maxZErr, bool bAllInt, double lowIntLimit, T& newNoDataVal)
1558 {
1559 if (bAllInt)
1560 {
1561 stdcxx . . $ std/::vectorcxx . . $ std/vector#<T> noDataCandVec;
1562
1563 { // collect dist candidates
1564 stdcxx . . $ std/::vectorcxx . . $ std/vector#<double> distCandVec = {cxx . . $ std/vector#vector(9619226211e5a241). 4 * maxZErr, 1, 10, 100, 1000, 10000 }; // only int candidates
1565
1566 for (double dist : distCandVec)
1567 noDataCandVec.push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).((T)(minVal - dist));
1568
1569 double candForLargeMinVal = (minVal > 0 ? floor(minVal / 2) : minVal * 2); // also int
1570 noDataCandVec.push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).((T)candForLargeMinVal);
1571 }
1572
1573 // sort them in descending order
1574 stdcxx . . $ std/::sortcxx . . $ std/sort(249673c20e2b8d5).(noDataCandVec.begincxx . . $ std/vector#begin(55a6de06420d4903).(), noDataCandVec.endcxx . . $ std/vector#end(55a6de06420d4903).(), stdcxx . . $ std/::greatercxx . . $ std/greater#<double>());
1575
1576 // take the first one that satisfies the condition
1577 for (T noDataVal : noDataCandVec)
1578 {
1579 if ((noDataVal >cxx . . $ std/`operator>`(39b98f4fe21ae589). (T)lowIntLimit) && (noDataVal <cxx . . $ std/`operator<`(39b98f4fe21ae589). (T)(minVal - 2 * maxZErr)) && IsIntcxx . . $ LercNS/Lerc#IsInt(ee44d93984fb03e9).(noDataVal))
1580 {
1581 newNoDataVal = noDataVal;
1582 return true;
1583 }
1584 }
1585 }
1586 else
1587 {
1588 stdcxx . . $ std/::vectorcxx . . $ std/vector#<T> noDataCandVec;
1589
1590 { // dist candidates
1591 stdcxx . . $ std/::vectorcxx . . $ std/vector#<double> distCandVec = {cxx . . $ std/vector#vector(9619226211e5a241). 4 * maxZErr, 0.0001, 0.001, 0.01, 0.1, 1, 10, 100, 1000, 10000 };
1592
1593 for (double dist : distCandVec)
1594 noDataCandVec.push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).((T)(minVal - dist));
1595
1596 double candForLargeMinVal = (minVal > 0 ? minVal / 2 : minVal * 2);
1597 noDataCandVec.push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).((T)candForLargeMinVal);
1598 }
1599
1600 // sort them in descending order
1601 stdcxx . . $ std/::sortcxx . . $ std/sort(249673c20e2b8d5).(noDataCandVec.begincxx . . $ std/vector#begin(55a6de06420d4903).(), noDataCandVec.endcxx . . $ std/vector#end(55a6de06420d4903).(), stdcxx . . $ std/::greatercxx . . $ std/greater#<double>());
1602
1603 bool bIsFloat4 = (typeid(T) == typeid(float));
1604 T lowestVal = (T)(bIsFloat4 ? -FLT_MAXcxx . . $ `<external>/8223903785965736313/float.h:104:9`! : -DBL_MAXcxx . . $ `<external>/8223903785965736313/float.h:105:9`!);
1605
1606 // take the first one that satisfies the condition
1607 for (T noDataVal : noDataCandVec)
1608 {
1609 if ((noDataVal >cxx . . $ std/`operator>`(39b98f4fe21ae589). lowestVal) && (noDataVal <cxx . . $ std/`operator<`(39b98f4fe21ae589). (T)(minVal - 2 * maxZErr)))
1610 {
1611 newNoDataVal = noDataVal;
1612 return true;
1613 }
1614 }
1615 }
1616
1617 return false;
1618 }
1619
1620 // -------------------------------------------------------------------------- ;
1621
1622 bool Lerccxx . . $ LercNS/Lerc#::CheckDimensionscxx . . $ LercNS/Lerc#CheckDimensions(67519ccc50caacfb).(int nDepth, int nCols, int nRows, size_t sizeOfDataElement)
1623 {
1624 // here we guard against too large input dimensions that might cause a possible int32 overflow later;
1625 // we limit the size of input data per band to 2 GB, so any product [height * width * depth * sizeof(data element)] <= INT_MAX;
1626 // (see the corresponding size and dimension checks in Lerc2::ReadHeader(...));
1627
1628 if (nDepth <= 0 || nCols <= 0 || nRows <= 0)
1629 return false;
1630
1631 const uint64_tcxx . . $ uint64_t# numPixel = (uint64_tcxx . . $ uint64_t#)nRows * nCols;
1632 const uint64_tcxx . . $ uint64_t# maxint32 = (uint64_tcxx . . $ uint64_t#)INT_MAXcxx . . $ `<external>/2419331716647297546/limits.h:120:9`!;
1633 const uint64_tcxx . . $ uint64_t# nbpp = sizeOfDataElement;
1634
1635 if (numPixel > maxint32 || nbpp > maxint32 || nbpp * nDepth > maxint32 || nbpp * nDepth * numPixel > maxint32)
1636 return false;
1637
1638 return true;
1639 }
1640
1641 // -------------------------------------------------------------------------- ;