Debian Codegraph @

src/LercLib/Lerc_c_api_impl.cpp

raw symbols SCIP
1 /*
2 Copyright 2016 - 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"
25 #include "include/Lerc_c_api.h"
26 #include "include/Lerc_types.h"
27 #include "Lerc.h"
28
29 USING_NAMESPACE_LERC
30
31 // -------------------------------------------------------------------------- ;
32
33 lerc_status lerc_computeCompressedSize(const void* pData, unsigned int dataType, int nDepth, int nCols, int nRows, int nBands,
34 int nMasks, const unsigned char* pValidBytes, double maxZErr, unsigned int* numBytes)
35 {
36 return lerc_computeCompressedSizeForVersion(pData, -1, dataType, nDepth, nCols, nRows, nBands, nMasks,
37 pValidBytes, maxZErr, numBytes);
38 }
39
40 // -------------------------------------------------------------------------- ;
41
42 lerc_status lerc_computeCompressedSizeForVersion(const void* pData, int version, unsigned int dataType, int nDepth, int nCols, int nRows, int nBands,
43 int nMasks, const unsigned char* pValidBytes, double maxZErr, unsigned int* numBytes)
44 {
45 if (!numBytes)
46 return (lerc_status)ErrCode::WrongParam;
47
48 *numBytes = 0;
49
50 if (!pData || dataType >= Lerc::DT_Undefined || nDepth <= 0 || nCols <= 0 || nRows <= 0 || nBands <= 0 || maxZErr < 0)
51 return (lerc_status)ErrCode::WrongParam;
52
53 if (!(nMasks == 0 || nMasks == 1 || nMasks == nBands) || (nMasks > 0 && !pValidBytes))
54 return (lerc_status)ErrCode::WrongParam;
55
56 Lerc::DataType dt = (Lerc::DataType)dataType;
57 return (lerc_status)Lerc::ComputeCompressedSize(pData, version, dt, nDepth, nCols, nRows, nBands, nMasks,
58 pValidBytes, maxZErr, *numBytes, nullptr, nullptr);
59 }
60
61 // -------------------------------------------------------------------------- ;
62
63 lerc_status lerc_encode(const void* pData, unsigned int dataType, int nDepth, int nCols, int nRows, int nBands,
64 int nMasks, const unsigned char* pValidBytes, double maxZErr, unsigned char* pOutBuffer, unsigned int outBufferSize,
65 unsigned int* nBytesWritten)
66 {
67 return lerc_encodeForVersion(pData, -1, dataType, nDepth, nCols, nRows, nBands, nMasks, pValidBytes,
68 maxZErr, pOutBuffer, outBufferSize, nBytesWritten);
69 }
70
71 // -------------------------------------------------------------------------- ;
72
73 lerc_status lerc_encodeForVersion(const void* pData, int version, unsigned int dataType, int nDepth, int nCols, int nRows, int nBands,
74 int nMasks, const unsigned char* pValidBytes, double maxZErr, unsigned char* pOutBuffer, unsigned int outBufferSize,
75 unsigned int* nBytesWritten)
76 {
77 if (!nBytesWritten)
78 return (lerc_status)ErrCode::WrongParam;
79
80 *nBytesWritten = 0;
81
82 if (!pData || dataType >= Lerc::DT_Undefined || nDepth <= 0 || nCols <= 0 || nRows <= 0 || nBands <= 0 || maxZErr < 0 || !pOutBuffer || !outBufferSize)
83 return (lerc_status)ErrCode::WrongParam;
84
85 if (!(nMasks == 0 || nMasks == 1 || nMasks == nBands) || (nMasks > 0 && !pValidBytes))
86 return (lerc_status)ErrCode::WrongParam;
87
88 Lerc::DataType dt = (Lerc::DataType)dataType;
89 return (lerc_status)Lerc::Encode(pData, version, dt, nDepth, nCols, nRows, nBands, nMasks, pValidBytes,
90 maxZErr, pOutBuffer, outBufferSize, *nBytesWritten, nullptr, nullptr);
91 }
92
93 // -------------------------------------------------------------------------- ;
94
95 lerc_status lerc_getBlobInfo(const unsigned char* pLercBlob, unsigned int blobSize,
96 unsigned int* infoArray, double* dataRangeArray, int infoArraySize, int dataRangeArraySize)
97 {
98 if (!pLercBlob || !blobSize || (!infoArray && !dataRangeArray) || ((infoArraySize <= 0) && (dataRangeArraySize <= 0)))
99 return (lerc_status)ErrCode::WrongParam;
100
101 Lerc::LercInfo lercInfo;
102 ErrCode errCode = Lerc::GetLercInfo(pLercBlob, blobSize, lercInfo);
103 if (errCode != ErrCode::Ok)
104 return (lerc_status)errCode;
105
106 if (infoArray)
107 {
108 int i = 0, ias = infoArraySize;
109
110 if (ias > 0)
111 memset(infoArray, 0, ias * sizeof(infoArray[0]));
112
113 if (i < ias)
114 infoArray[i++] = (unsigned int)lercInfo.version;
115 if (i < ias)
116 infoArray[i++] = (unsigned int)lercInfo.dt;
117 if (i < ias)
118 infoArray[i++] = (unsigned int)lercInfo.nDepth;
119 if (i < ias)
120 infoArray[i++] = (unsigned int)lercInfo.nCols;
121 if (i < ias)
122 infoArray[i++] = (unsigned int)lercInfo.nRows;
123 if (i < ias)
124 infoArray[i++] = (unsigned int)lercInfo.nBands;
125 if (i < ias)
126 infoArray[i++] = (unsigned int)lercInfo.numValidPixel;
127 if (i < ias)
128 infoArray[i++] = (unsigned int)lercInfo.blobSize;
129 if (i < ias)
130 infoArray[i++] = (unsigned int)lercInfo.nMasks;
131 if (i < ias)
132 infoArray[i++] = (unsigned int)lercInfo.nDepth;
133 if (i < ias)
134 infoArray[i++] = (unsigned int)lercInfo.nUsesNoDataValue;
135 }
136
137 if (dataRangeArray)
138 {
139 int i = 0, dras = dataRangeArraySize;
140
141 if (dras > 0)
142 memset(dataRangeArray, 0, dras * sizeof(dataRangeArray[0]));
143
144 // for nDepth > 1, and mix of valid and invalid values at same pixel, better reset min / max to -1
145 // than return min or max values that contain noData (to be fixed in next codec version)
146
147 bool bUsesNoData = (lercInfo.nDepth > 1) && (lercInfo.nUsesNoDataValue > 0);
148
149 if (i < dras)
150 dataRangeArray[i++] = !bUsesNoData ? lercInfo.zMin : -1;
151 if (i < dras)
152 dataRangeArray[i++] = !bUsesNoData ? lercInfo.zMax : -1;
153 if (i < dras)
154 dataRangeArray[i++] = lercInfo.maxZError;
155 }
156
157 return (lerc_status)ErrCode::Ok;
158 }
159
160 // -------------------------------------------------------------------------- ;
161
162 lerc_status lerc_getDataRanges(const unsigned char* pLercBlob, unsigned int blobSize,
163 int nDepth, int nBands, double* pMins, double* pMaxs)
164 {
165 if (!pLercBlob || !blobSize || !pMins || !pMaxs || nDepth <= 0 || nBands <= 0)
166 return (lerc_status)ErrCode::WrongParam;
167
168 Lerc::LercInfo lercInfo;
169 ErrCode errCode = Lerc::GetLercInfo(pLercBlob, blobSize, lercInfo, pMins, pMaxs, (size_t)nDepth * (size_t)nBands);
170 if (errCode != ErrCode::Ok)
171 return (lerc_status)errCode;
172
173 return (lerc_status)ErrCode::Ok;
174 }
175
176 // -------------------------------------------------------------------------- ;
177
178 lerc_status lerc_decode(const unsigned char* pLercBlob, unsigned int blobSize, int nMasks,
179 unsigned char* pValidBytes, int nDepth, int nCols, int nRows, int nBands, unsigned int dataType, void* pData)
180 {
181 return lerc_decode_4D(pLercBlob, blobSize, nMasks, pValidBytes,
182 nDepth, nCols, nRows, nBands, dataType, pData, nullptr, nullptr);
183 }
184 // -------------------------------------------------------------------------- ;
185
186 lerc_status lerc_decodeToDouble(const unsigned char* pLercBlob, unsigned int blobSize, int nMasks,
187 unsigned char* pValidBytes, int nDepth, int nCols, int nRows, int nBands, double* pData)
188 {
189 return lerc_decodeToDouble_4D(pLercBlob, blobSize, nMasks, pValidBytes,
190 nDepth, nCols, nRows, nBands, pData, nullptr, nullptr);
191 }
192
193 // -------------------------------------------------------------------------- ;
194 // -------------------------------------------------------------------------- ;
195
196 lerc_status lerc_computeCompressedSize_4D(const void* pData, unsigned int dataType, int nDepth, int nCols, int nRows, int nBands,
197 int nMasks, const unsigned char* pValidBytes, double maxZErr, unsigned int* numBytes, const unsigned char* pUsesNoData, const double* noDataValues)
198 {
199 if (!numBytes)
200 return (lerc_status)ErrCode::WrongParam;
201
202 *numBytes = 0;
203
204 if (!pData || dataType >= Lerc::DT_Undefined || nDepth <= 0 || nCols <= 0 || nRows <= 0 || nBands <= 0 || maxZErr < 0)
205 return (lerc_status)ErrCode::WrongParam;
206
207 if (!(nMasks == 0 || nMasks == 1 || nMasks == nBands) || (nMasks > 0 && !pValidBytes))
208 return (lerc_status)ErrCode::WrongParam;
209
210 Lerc::DataType dt = (Lerc::DataType)dataType;
211 return (lerc_status)Lerc::ComputeCompressedSize(pData, -1, dt, nDepth, nCols, nRows, nBands, nMasks,
212 pValidBytes, maxZErr, *numBytes, pUsesNoData, noDataValues);
213 }
214
215 // -------------------------------------------------------------------------- ;
216
217 lerc_status lerc_encode_4D(const void* pData, unsigned int dataType, int nDepth, int nCols, int nRows, int nBands,
218 int nMasks, const unsigned char* pValidBytes, double maxZErr, unsigned char* pOutBuffer, unsigned int outBufferSize,
219 unsigned int* nBytesWritten, const unsigned char* pUsesNoData, const double* noDataValues)
220 {
221 if (!nBytesWritten)
222 return (lerc_status)ErrCode::WrongParam;
223
224 *nBytesWritten = 0;
225
226 if (!pData || dataType >= Lerc::DT_Undefined || nDepth <= 0 || nCols <= 0 || nRows <= 0 || nBands <= 0 || maxZErr < 0 || !pOutBuffer || !outBufferSize)
227 return (lerc_status)ErrCode::WrongParam;
228
229 if (!(nMasks == 0 || nMasks == 1 || nMasks == nBands) || (nMasks > 0 && !pValidBytes))
230 return (lerc_status)ErrCode::WrongParam;
231
232 Lerc::DataType dt = (Lerc::DataType)dataType;
233 return (lerc_status)Lerc::Encode(pData, -1, dt, nDepth, nCols, nRows, nBands, nMasks, pValidBytes,
234 maxZErr, pOutBuffer, outBufferSize, *nBytesWritten, pUsesNoData, noDataValues);
235 }
236
237 // -------------------------------------------------------------------------- ;
238
239 lerc_status lerc_decode_4D(const unsigned char* pLercBlob, unsigned int blobSize, int nMasks,
240 unsigned char* pValidBytes, int nDepth, int nCols, int nRows, int nBands, unsigned int dataType, void* pData,
241 unsigned char* pUsesNoData, double* noDataValues)
242 {
243 if (!pLercBlob || !blobSize || !pData || dataType >= Lerc::DT_Undefined || nDepth <= 0 || nCols <= 0 || nRows <= 0 || nBands <= 0)
244 return (lerc_status)ErrCode::WrongParam;
245
246 if (!(nMasks == 0 || nMasks == 1 || nMasks == nBands) || (nMasks > 0 && !pValidBytes))
247 return (lerc_status)ErrCode::WrongParam;
248
249 Lerc::DataType dt = (Lerc::DataType)dataType;
250
251 return (lerc_status)Lerc::Decode(pLercBlob, blobSize, nMasks, pValidBytes, nDepth, nCols, nRows, nBands, dt, pData, pUsesNoData, noDataValues);
252 }
253
254 // -------------------------------------------------------------------------- ;
255
256 lerc_status lerc_decodeToDouble_4D(const unsigned char* pLercBlob, unsigned int blobSize, int nMasks,
257 unsigned char* pValidBytes, int nDepth, int nCols, int nRows, int nBands, double* pData,
258 unsigned char* pUsesNoData, double* noDataValues)
259 {
260 if (!pLercBlob || !blobSize || !pData || nDepth <= 0 || nCols <= 0 || nRows <= 0 || nBands <= 0)
261 return (lerc_status)ErrCode::WrongParam;
262
263 if (!(nMasks == 0 || nMasks == 1 || nMasks == nBands) || (nMasks > 0 && !pValidBytes))
264 return (lerc_status)ErrCode::WrongParam;
265
266 Lerc::LercInfo lercInfo;
267 ErrCode errCode;
268 if ((errCode = Lerc::GetLercInfo(pLercBlob, blobSize, lercInfo)) != ErrCode::Ok)
269 return (lerc_status)errCode;
270
271 Lerc::DataType dt = lercInfo.dt;
272 if (dt < 0 || dt > Lerc::DT_Double)
273 return (lerc_status)ErrCode::Failed;
274
275 if (lercInfo.nDepth != nDepth || lercInfo.nCols != nCols || lercInfo.nRows != nRows || lercInfo.nBands != nBands)
276 return (lerc_status)ErrCode::Failed;
277
278 if (dt == Lerc::DT_Double)
279 {
280 if ((errCode = Lerc::Decode(pLercBlob, blobSize, nMasks, pValidBytes,
281 nDepth, nCols, nRows, nBands, dt, pData, pUsesNoData, noDataValues)) != ErrCode::Ok)
282 {
283 return (lerc_status)errCode;
284 }
285 }
286 else
287 {
288 // use the buffer passed for in place decode and convert
289 int sizeofDt[] = { 1, 1, 2, 2, 4, 4, 4, 8 };
290 size_t nDataValues = (size_t)nDepth * nCols * nRows * nBands; // can be > 4 GB
291 void* ptrDec = (Byte*)pData + nDataValues * (sizeof(double) - sizeofDt[dt]);
292
293 if ((errCode = Lerc::Decode(pLercBlob, blobSize, nMasks, pValidBytes,
294 nDepth, nCols, nRows, nBands, dt, ptrDec, pUsesNoData, noDataValues)) != ErrCode::Ok)
295 {
296 return (lerc_status)errCode;
297 }
298
299 if ((errCode = Lerc::ConvertToDouble(ptrDec, dt, nDataValues, pData)) != ErrCode::Ok)
300 return (lerc_status)errCode;
301 }
302
303 return (lerc_status)ErrCode::Ok;
304 }
305
306 // -------------------------------------------------------------------------- ;
307