Debian Codegraph @

src/LercLib/include/Lerc_c_api.h

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 #ifndef LERC_API_INCLUDE_GUARD
25 #define LERC_API_INCLUDE_GUARD
26
27 //#define USE_EMSCRIPTEN // to build a wasm Lerc decoder, install emscripten first
28
29 #ifdef USE_EMSCRIPTEN
30 #include <emscripten/emscripten.h>
31 #endif
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 /* LERC version numbers and related macros added in 3.0.0 */
38
39 #define LERC_VERSION_MAJOR 4
40 #define LERC_VERSION_MINOR 2
41 #define LERC_VERSION_PATCH 0
42
43 /* Macro to compute a LERC version number from its components */
44 #define LERC_COMPUTE_VERSION(maj,min,patch) ((maj)*10000+(min)*100+(patch))
45
46 /* Current LERC version from the above version numbers */
47 #define LERC_VERSION_NUMBER \
48 LERC_COMPUTE_VERSION(LERC_VERSION_MAJOR, LERC_VERSION_MINOR, LERC_VERSION_PATCH)
49
50 /* Macro that returns true if the current LERC version is at least the version specified by (maj,min,patch) */
51 #define LERC_AT_LEAST_VERSION(maj,min,patch) \
52 (LERC_VERSION_NUMBER >= LERC_COMPUTE_VERSION(maj,min,patch))
53
54 #if (defined _WIN32 || defined __CYGWIN__)
55 # if defined(LERC_STATIC)
56 # define LERCDLL_API
57 # elif defined(LERC_EXPORTS)
58 # define LERCDLL_API __declspec(dllexport)
59 # else
60 # define LERCDLL_API __declspec(dllimport)
61 # endif
62 #elif (defined(LERC_EXPORTS) && __GNUC__ >= 4)
63 #define LERCDLL_API __attribute__((visibility("default")))
64 #else
65 #define LERCDLL_API
66 #endif
67
68 //! C-API for LERC library
69
70
71 //! Added in version 4.2:
72 //!
73 //! Lerc is mainly used as an image tile compression format, usually not to compress an entire large
74 //! image into one single compressed binary file. While this is possible, it would not be optimal in
75 //! both compression and read / write access.
76 //!
77 //! Now we put some well defined size limitations in place. We don't expect this to have any
78 //! implications for typical usage scenarios when encoding image tiles. The main goal is improved
79 //! security and protection, both against accidental integer overflows thanks to wrong or too large
80 //! dimensions as well as bogus dimensions or parameters planted by an attacker into the binary Lerc
81 //! header of the compressed Lerc blob.
82 //!
83 //! - 1) The size of input data (to be encoded) is limited to 2 GB per band.
84 //! - 2) The size of the compressed binary Lerc blob is also limited to 2 GB per band.
85 //! - 3) The size of the entire compressed binary Lerc blob (over all bands) is limited to 4 GB.
86 //! - 4) The size of the entire input data (to be encoded, over all bands) is NOT limited, as long
87 //! as the above conditions are met.
88
89
90 //! Added in version 4.0:
91 //!
92 //! - 1) better support 3D and 4D data, allow for lossy encoding even if a noData value is used
93 //! - 2) better lossless compression for float and double (pass maxZError = 0)
94 //! - 3) allow to pass integer > 32 bit as double (Lerc detects it is all integer and uses that)
95 //! - 4) renamed nDim to nDepth (without changing the function signatures)
96 //!
97 //! More on 1). In version 3.0, for 2D images, the 2D valid / invalid byte masks represent invalid pixels.
98 //! For more than 1 band, different masks per band can be used. No change to that.
99 //! For nDepth > 1, or an array of values per pixel, there is the special case of a mix of valid and invalid values
100 //! at the same pixel. The 2D mask cannot cover this case.
101 //! We have added 4 new functions to version 4.0 to cover this case, see below. If you don't encounter this
102 //! "mixed case", you can continue using the same API functions as in version 3.0.
103 //! If you should encounter a Lerc blob that has this mix, both the regular lerc_decode() and
104 //! lerc_getDataRanges() functions will fail with "ErrCode::HasNoData".
105 //! In that case, you need to call the new lerc_decode_4D() function.
106 //!
107 //! More on 2). Better lossless compression for float and double is enabled for all API functions.
108 //! For 1) and 3) you have to call the new "..._4D()" functions, see further below.
109
110
111 typedef unsigned int lerc_status;
112
113 //! All output buffers must have been allocated by the caller.
114
115 //! Compute the buffer size in bytes required to hold the compressed input tile. Optional.
116 //! You can call lerc_encode(...) directly as long as the output buffer is big enough.
117
118 //! Order of raw input data is top left corner to lower right corner, row by row. This for each band.
119 //! Data type is { char = 0, uchar = 1, short = 2, ushort = 3, int = 4, uint = 5, float = 6, double = 7 }, see Lerc_types.h .
120 //! maxZErr is the max compression error per pixel allowed.
121
122 //! The image or mask of valid pixels is optional. Nullptr means all pixels are valid.
123 //! If not all pixels are valid, set invalid pixel bytes to 0, valid pixel bytes to 1.
124 //! Size of the valid / invalid pixel image is (nCols * nRows * nMasks).
125
126 LERCDLL_API
127 lerc_status lerc_computeCompressedSize(
128 const void* pData, // raw image data, row by row, band by band
129 unsigned int dataType, // char = 0, uchar = 1, short = 2, ushort = 3, int = 4, uint = 5, float = 6, double = 7
130 int nDepth, // number of values per pixel (e.g., 3 for RGB, data is stored as [RGB, RGB, ...])
131 int nCols, // number of columns
132 int nRows, // number of rows
133 int nBands, // number of bands (e.g., 3 for [RRRR ..., GGGG ..., BBBB ...])
134 int nMasks, // 0 - all valid, 1 - same mask for all bands, nBands - masks can differ between bands
135 const unsigned char* pValidBytes, // nullptr if all pixels are valid; otherwise 1 byte per pixel (1 = valid, 0 = invalid)
136 double maxZErr, // max coding error per pixel, defines the precision
137 unsigned int* numBytes); // size of outgoing Lerc blob
138
139 //! Encode the input data into a compressed Lerc blob.
140
141 LERCDLL_API
142 lerc_status lerc_encode(
143 const void* pData, // raw image data, row by row, band by band
144 unsigned int dataType, // char = 0, uchar = 1, short = 2, ushort = 3, int = 4, uint = 5, float = 6, double = 7
145 int nDepth, // number of values per pixel (e.g., 3 for RGB, data is stored as [RGB, RGB, ...])
146 int nCols, // number of columns
147 int nRows, // number of rows
148 int nBands, // number of bands (e.g., 3 for [RRRR ..., GGGG ..., BBBB ...])
149 int nMasks, // 0 - all valid, 1 - same mask for all bands, nBands - masks can differ between bands
150 const unsigned char* pValidBytes, // nullptr if all pixels are valid; otherwise 1 byte per pixel (1 = valid, 0 = invalid)
151 double maxZErr, // max coding error per pixel, defines the precision
152 unsigned char* pOutBuffer, // buffer to write to, function fails if buffer too small
153 unsigned int outBufferSize, // size of output buffer
154 unsigned int* nBytesWritten); // number of bytes written to output buffer
155
156
157 //! Use the 2 functions below to encode to an older codec version
158
159 LERCDLL_API
160 lerc_status lerc_computeCompressedSizeForVersion(
161 const void* pData, // raw image data, row by row, band by band
162 int codecVersion, // [2 .. 6] for [v2.2 .. v2.6], or -1 for latest codec v2.6
163 unsigned int dataType, // char = 0, uchar = 1, short = 2, ushort = 3, int = 4, uint = 5, float = 6, double = 7
164 int nDepth, // number of values per pixel (e.g., 3 for RGB, data is stored as [RGB, RGB, ...])
165 int nCols, // number of columns
166 int nRows, // number of rows
167 int nBands, // number of bands (e.g., 3 for [RRRR ..., GGGG ..., BBBB ...])
168 int nMasks, // 0 - all valid, 1 - same mask for all bands, nBands - masks can differ between bands
169 const unsigned char* pValidBytes, // nullptr if all pixels are valid; otherwise 1 byte per pixel (1 = valid, 0 = invalid)
170 double maxZErr, // max coding error per pixel, defines the precision
171 unsigned int* numBytes); // size of outgoing Lerc blob
172
173 LERCDLL_API
174 lerc_status lerc_encodeForVersion(
175 const void* pData, // raw image data, row by row, band by band
176 int codecVersion, // [2 .. 6] for [v2.2 .. v2.6], or -1 for latest codec v2.6
177 unsigned int dataType, // char = 0, uchar = 1, short = 2, ushort = 3, int = 4, uint = 5, float = 6, double = 7
178 int nDepth, // number of values per pixel (e.g., 3 for RGB, data is stored as [RGB, RGB, ...])
179 int nCols, // number of columns
180 int nRows, // number of rows
181 int nBands, // number of bands (e.g., 3 for [RRRR ..., GGGG ..., BBBB ...])
182 int nMasks, // 0 - all valid, 1 - same mask for all bands, nBands - masks can differ between bands
183 const unsigned char* pValidBytes, // nullptr if all pixels are valid; otherwise 1 byte per pixel (1 = valid, 0 = invalid)
184 double maxZErr, // max coding error per pixel, defines the precision
185 unsigned char* pOutBuffer, // buffer to write to, function fails if buffer too small
186 unsigned int outBufferSize, // size of output buffer
187 unsigned int* nBytesWritten); // number of bytes written to output buffer
188
189
190 //! Call this to get info about the compressed Lerc blob. Optional.
191 //! Info returned in infoArray is
192 //! { version, dataType, nDepth, nCols, nRows, nBands, nValidPixels, blobSize, nMasks, nDepth, nUsesNoDataValue }, see Lerc_types.h .
193 //! Info returned in dataRangeArray is { zMin, zMax, maxZErrorUsed }, see Lerc_types.h .
194 //! If nDepth > 1 or nBands > 1 the data range [zMin, zMax] is over all values.
195
196 // Remark on function signature. The arrays to be filled may grow in future versions. In order not to break
197 // existing code, the function fills these arrays only up to their allocated size.
198
199 // Remark on param blobSize. Usually it is known, either the file size of the blob written to disk,
200 // or the size of the blob transmitted. It should be passed accurately for 2 reasons:
201 // _ function finds out how many single band Lerc blobs are concatenated, if any
202 // _ function checks for truncated file or blob
203 // It is OK to pass blobSize too large as long as there is no other (valid) Lerc blob following next.
204 // If in doubt, check the code in Lerc::GetLercInfo(...) for the exact logic.
205
206 LERCDLL_API
207 #ifdef USE_EMSCRIPTEN
208 EMSCRIPTEN_KEEPALIVE
209 #endif
210 lerc_status lerc_getBlobInfo(
211 const unsigned char* pLercBlob, // Lerc blob to decode
212 unsigned int blobSize, // blob size in bytes
213 unsigned int* infoArray, // info array with all info needed to allocate the outgoing arrays for calling decode
214 double* dataRangeArray, // quick access to overall data range [zMin, zMax] without having to decode the data
215 int infoArraySize, // number of elements of infoArray
216 int dataRangeArraySize); // number of elements of dataRangeArray
217
218 //! Call this to quickly get the data ranges [min, max] per dimension and band without having to decode the pixels. Optional.
219 //! The 2 output data arrays must have been allocated to the same size (nDepth * nBands).
220 //! The output data array's layout is an image with nDepth columns and nBands rows.
221
222 LERCDLL_API
223 #ifdef USE_EMSCRIPTEN
224 EMSCRIPTEN_KEEPALIVE
225 #endif
226 lerc_status lerc_getDataRanges(
227 const unsigned char* pLercBlob, // Lerc blob to decode
228 unsigned int blobSize, // blob size in bytes
229 int nDepth, // number of values per pixel (e.g., 3 for RGB, data is stored as [RGB, RGB, ...])
230 int nBands, // number of bands (e.g., 3 for [RRRR ..., GGGG ..., BBBB ...])
231 double* pMins, // outgoing minima per dimension and band
232 double* pMaxs); // outgoing maxima per dimension and band
233
234 //! Decode the compressed Lerc blob into a raw data array.
235 //! The data array must have been allocated to size (nDepth * nCols * nRows * nBands * sizeof(dataType)).
236 //! The valid pixels array, if not all pixels valid, must have been allocated to size (nCols * nRows * nMasks).
237
238 LERCDLL_API
239 #ifdef USE_EMSCRIPTEN
240 EMSCRIPTEN_KEEPALIVE
241 #endif
242 lerc_status lerc_decode(
243 const unsigned char* pLercBlob, // Lerc blob to decode
244 unsigned int blobSize, // blob size in bytes
245 int nMasks, // 0, 1, or nBands; return as many masks in the next array
246 unsigned char* pValidBytes, // gets filled if not nullptr, even if all valid
247 int nDepth, // number of values per pixel (e.g., 3 for RGB, data is stored as [RGB, RGB, ...])
248 int nCols, // number of columns
249 int nRows, // number of rows
250 int nBands, // number of bands (e.g., 3 for [RRRR ..., GGGG ..., BBBB ...])
251 unsigned int dataType, // char = 0, uchar = 1, short = 2, ushort = 3, int = 4, uint = 5, float = 6, double = 7
252 void* pData); // outgoing data array
253
254 //! Same as above, but decode into double array independent of compressed data type.
255 //! Wasteful in memory, but convenient if a caller from Python or C# does not want to deal with
256 //! data type conversion, templating, or casting.
257 //! Should this api be extended to new data types that don't fit into a double such as int64,
258 //! then this function will fail for such compressed data types.
259
260 LERCDLL_API
261 lerc_status lerc_decodeToDouble(
262 const unsigned char* pLercBlob, // Lerc blob to decode
263 unsigned int blobSize, // blob size in bytes
264 int nMasks, // 0, 1, or nBands; return as many masks in the next array
265 unsigned char* pValidBytes, // gets filled if not nullptr, even if all valid
266 int nDepth, // number of values per pixel (e.g., 3 for RGB, data is stored as [RGB, RGB, ...])
267 int nCols, // number of columns
268 int nRows, // number of rows
269 int nBands, // number of bands (e.g., 3 for [RRRR ..., GGGG ..., BBBB ...])
270 double* pData); // outgoing data array
271
272
273 //! Added in version 4.0:
274 //!
275 //! The 4 functions below are new. The main purpose (and difference to the functions above) is to support, for 3D and 4D data,
276 //! the special case of a mix of valid and invalid values at the same pixel.
277 //!
278 //! Main idea: Lerc has the property that for each 8x8 pixel block the minimum value is always encoded lossless in the block header.
279 //! To enable lossy encoding in the presence of noData values, the original noData value is mapped below the range of the valid values,
280 //! if possible. If not possible, it switches to lossless. On decode, that temporary noData value gets mapped back to the original
281 //! noData value.
282 //!
283 //! To minimize the occurrence of noData values (and for better compression), Lerc tries to move noData values to the byte mask
284 //! wherever possible (e.g., all values at some pixel are invalid). So for a given band the noData values may disappear and get
285 //! all moved to the byte mask. Decode only returns a noData value if it is really used. In that case the caller needs to filter
286 //! the decoded arrays using both the byte mask returned and the noData value returned.
287 //!
288 //! In addition to the noData support, the new functions can also take integer values > 32 bit (but < 53 bit) as a double array,
289 //! and if all integer, use that for compression.
290 //!
291 //! If floating point data contains NaN, Lerc tries to move it to the byte mask or replace it by a passed noData value.
292 //! Note, if not all NaN values can be moved to the mask (mixed case), and no noData value was passed, Lerc will fail.
293 //! It would be wrong to invent a noData value on the tile level.
294
295
296 //! Encode functions:
297 //!
298 //! If you don't use a noData value, are fine with the byte masks, just pass nullptr for the last 2 arguments.
299 //!
300 //! If you do have noData values at pixels that are marked as valid pixels by the byte mask,
301 //! pass 2 arrays of size nBands each, one value per band.
302 //! In pUsesNoData array, for each band, pass 1 for noData value is used, 0 if not.
303 //! In noDataValues array, for each band, pass the noData value if there is one.
304
305 LERCDLL_API
306 lerc_status lerc_computeCompressedSize_4D(
307 const void* pData, // raw image data, row by row, band by band
308 unsigned int dataType, // char = 0, uchar = 1, short = 2, ushort = 3, int = 4, uint = 5, float = 6, double = 7
309 int nDepth, // number of values per pixel (e.g., 3 for RGB, data is stored as [RGB, RGB, ...])
310 int nCols, // number of columns
311 int nRows, // number of rows
312 int nBands, // number of bands (e.g., 3 for [RRRR ..., GGGG ..., BBBB ...])
313 int nMasks, // 0 - all valid, 1 - same mask for all bands, nBands - masks can differ between bands
314 const unsigned char* pValidBytes, // nullptr if all pixels are valid; otherwise 1 byte per pixel (1 = valid, 0 = invalid)
315 double maxZErr, // max coding error per pixel, defines the precision
316 unsigned int* numBytes, // size of outgoing Lerc blob
317 const unsigned char* pUsesNoData, // if there are invalid values not marked by the mask, pass an array of size nBands, 1 - uses noData, 0 - not
318 const double* noDataValues); // same, pass an array of size nBands with noData value per band, or pass nullptr
319
320 LERCDLL_API
321 lerc_status lerc_encode_4D(
322 const void* pData, // raw image data, row by row, band by band
323 unsigned int dataType, // char = 0, uchar = 1, short = 2, ushort = 3, int = 4, uint = 5, float = 6, double = 7
324 int nDepth, // number of values per pixel (e.g., 3 for RGB, data is stored as [RGB, RGB, ...])
325 int nCols, // number of columns
326 int nRows, // number of rows
327 int nBands, // number of bands (e.g., 3 for [RRRR ..., GGGG ..., BBBB ...])
328 int nMasks, // 0 - all valid, 1 - same mask for all bands, nBands - masks can differ between bands
329 const unsigned char* pValidBytes, // nullptr if all pixels are valid; otherwise 1 byte per pixel (1 = valid, 0 = invalid)
330 double maxZErr, // max coding error per pixel, defines the precision
331 unsigned char* pOutBuffer, // buffer to write to, function fails if buffer too small
332 unsigned int outBufferSize, // size of output buffer
333 unsigned int* nBytesWritten, // number of bytes written to output buffer
334 const unsigned char* pUsesNoData, // if there are invalid values not marked by the mask, pass an array of size nBands, 1 - uses noData, 0 - not
335 const double* noDataValues); // same, pass an array of size nBands with noData value per band, or pass nullptr
336
337
338 //! Decode functions:
339 //!
340 //! Same as for regular decode, first call lerc_getBlobInfo() to get all info needed from the blob header.
341 //! Check the property (InfoArray::nUsesNoDataValue) to check if there is any noData value used.
342 //!
343 //! If not, just pass nullptr for the last 2 arguments.
344 //!
345 //! If yes, pass 2 arrays of size nBands each, one value per band.
346 //! In pUsesNoData array, for each band, 1 means a noData value is used, 0 means not.
347 //! In noDataValues array, for each band, it has the noData value if there is one.
348 //! This is the same noData value as passed for encode.
349
350 LERCDLL_API
351 #ifdef USE_EMSCRIPTEN
352 EMSCRIPTEN_KEEPALIVE
353 #endif
354 lerc_status lerc_decode_4D(
355 const unsigned char* pLercBlob, // Lerc blob to decode
356 unsigned int blobSize, // blob size in bytes
357 int nMasks, // 0, 1, or nBands; return as many masks in the next array
358 unsigned char* pValidBytes, // gets filled if not nullptr, even if all valid
359 int nDepth, // number of values per pixel (e.g., 3 for RGB, data is stored as [RGB, RGB, ...])
360 int nCols, // number of columns
361 int nRows, // number of rows
362 int nBands, // number of bands (e.g., 3 for [RRRR ..., GGGG ..., BBBB ...])
363 unsigned int dataType, // char = 0, uchar = 1, short = 2, ushort = 3, int = 4, uint = 5, float = 6, double = 7
364 void* pData, // outgoing data array
365 unsigned char* pUsesNoData, // pass an array of size nBands, 1 - band uses noData, 0 - not
366 double* noDataValues); // same, pass an array of size nBands to get the noData value per band, if any
367
368 LERCDLL_API
369 lerc_status lerc_decodeToDouble_4D(
370 const unsigned char* pLercBlob, // Lerc blob to decode
371 unsigned int blobSize, // blob size in bytes
372 int nMasks, // 0, 1, or nBands; return as many masks in the next array
373 unsigned char* pValidBytes, // gets filled if not nullptr, even if all valid
374 int nDepth, // number of values per pixel (e.g., 3 for RGB, data is stored as [RGB, RGB, ...])
375 int nCols, // number of columns
376 int nRows, // number of rows
377 int nBands, // number of bands (e.g., 3 for [RRRR ..., GGGG ..., BBBB ...])
378 double* pData, // outgoing data array
379 unsigned char* pUsesNoData, // pass an array of size nBands, 1 - band uses noData, 0 - not
380 double* noDataValues); // same, pass an array of size nBands to get the noData value per band, if any
381
382
383 #ifdef __cplusplus
384 }
385 #endif
386
387 #endif // LERC_API_INCLUDE_GUARD