-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLerc_c_api_impl.cpp
More file actions
242 lines (191 loc) · 8.39 KB
/
Lerc_c_api_impl.cpp
File metadata and controls
242 lines (191 loc) · 8.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
Copyright 2016 Esri
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
A local copy of the license and additional notices are located with the
source distribution at:
http://github.com/Esri/lerc/
Contributors: Thomas Maurer
*/
#include "Defines.h"
#include "Lerc_c_api.h"
#include "Lerc_types.h"
#include "Lerc.h"
USING_NAMESPACE_LERC
// -------------------------------------------------------------------------- ;
lerc_status lerc_computeCompressedSize(const void* pData, unsigned int dataType, int nDim, int nCols, int nRows, int nBands,
const unsigned char* pValidBytes, double maxZErr, unsigned int* numBytes)
{
return lerc_computeCompressedSizeForVersion(pData, -1, dataType, nDim, nCols, nRows, nBands, pValidBytes, maxZErr, numBytes);
}
// -------------------------------------------------------------------------- ;
lerc_status lerc_computeCompressedSizeForVersion(const void* pData, int version, unsigned int dataType, int nDim, int nCols, int nRows, int nBands,
const unsigned char* pValidBytes, double maxZErr, unsigned int* numBytes)
{
if (!pData || dataType >= Lerc::DT_Undefined || nDim <= 0 || nCols <= 0 || nRows <= 0 || nBands <= 0 || maxZErr < 0 || !numBytes)
return (lerc_status)ErrCode::WrongParam;
BitMask bitMask;
if (pValidBytes)
{
bitMask.SetSize(nCols, nRows);
bitMask.SetAllValid();
for (int k = 0, i = 0; i < nRows; i++)
for (int j = 0; j < nCols; j++, k++)
if (!pValidBytes[k])
bitMask.SetInvalid(k);
}
const BitMask* pBitMask = pValidBytes ? &bitMask : nullptr;
Lerc::DataType dt = (Lerc::DataType)dataType;
return (lerc_status)Lerc::ComputeCompressedSize(pData, version, dt, nDim, nCols, nRows, nBands, pBitMask, maxZErr, *numBytes);
}
// -------------------------------------------------------------------------- ;
lerc_status lerc_encode(const void* pData, unsigned int dataType, int nDim, int nCols, int nRows, int nBands,
const unsigned char* pValidBytes, double maxZErr, unsigned char* pOutBuffer, unsigned int outBufferSize,
unsigned int* nBytesWritten)
{
return lerc_encodeForVersion(pData, -1, dataType, nDim, nCols, nRows, nBands, pValidBytes, maxZErr, pOutBuffer, outBufferSize, nBytesWritten);
}
// -------------------------------------------------------------------------- ;
lerc_status lerc_encodeForVersion(const void* pData, int version, unsigned int dataType, int nDim, int nCols, int nRows, int nBands,
const unsigned char* pValidBytes, double maxZErr, unsigned char* pOutBuffer, unsigned int outBufferSize,
unsigned int* nBytesWritten)
{
if (!pData || dataType >= Lerc::DT_Undefined || nDim <= 0 || nCols <= 0 || nRows <= 0 || nBands <= 0 || maxZErr < 0 || !pOutBuffer || !outBufferSize || !nBytesWritten)
return (lerc_status)ErrCode::WrongParam;
BitMask bitMask;
if (pValidBytes)
{
bitMask.SetSize(nCols, nRows);
bitMask.SetAllValid();
for (int k = 0, i = 0; i < nRows; i++)
for (int j = 0; j < nCols; j++, k++)
if (!pValidBytes[k])
bitMask.SetInvalid(k);
}
const BitMask* pBitMask = pValidBytes ? &bitMask : nullptr;
Lerc::DataType dt = (Lerc::DataType)dataType;
return (lerc_status)Lerc::Encode(pData, version, dt, nDim, nCols, nRows, nBands, pBitMask, maxZErr, pOutBuffer, outBufferSize, *nBytesWritten);
}
// -------------------------------------------------------------------------- ;
lerc_status lerc_getBlobInfo(const unsigned char* pLercBlob, unsigned int blobSize,
unsigned int* infoArray, double* dataRangeArray, int infoArraySize, int dataRangeArraySize)
{
if (!pLercBlob || !blobSize || (!infoArray && !dataRangeArray) || ((infoArraySize <= 0) && (dataRangeArraySize <= 0)))
return (lerc_status)ErrCode::WrongParam;
Lerc::LercInfo lercInfo;
ErrCode errCode = Lerc::GetLercInfo(pLercBlob, blobSize, lercInfo);
if (errCode != ErrCode::Ok)
return (lerc_status)errCode;
if (infoArray)
{
int i = 0, ias = infoArraySize;
if (ias > 0)
memset(infoArray, 0, ias * sizeof(infoArray[0]));
if (i < ias)
infoArray[i++] = (unsigned int)lercInfo.version;
if (i < ias)
infoArray[i++] = (unsigned int)lercInfo.dt;
if (i < ias)
infoArray[i++] = (unsigned int)lercInfo.nDim;
if (i < ias)
infoArray[i++] = (unsigned int)lercInfo.nCols;
if (i < ias)
infoArray[i++] = (unsigned int)lercInfo.nRows;
if (i < ias)
infoArray[i++] = (unsigned int)lercInfo.nBands;
if (i < ias)
infoArray[i++] = (unsigned int)lercInfo.numValidPixel;
if (i < ias)
infoArray[i++] = (unsigned int)lercInfo.blobSize;
}
if (dataRangeArray)
{
int i = 0, dras = dataRangeArraySize;
if (dras > 0)
memset(dataRangeArray, 0, dras * sizeof(dataRangeArray[0]));
if (i < dras)
dataRangeArray[i++] = lercInfo.zMin;
if (i < dras)
dataRangeArray[i++] = lercInfo.zMax;
if (i < dras)
dataRangeArray[i++] = lercInfo.maxZError;
}
return (lerc_status)ErrCode::Ok;
}
// -------------------------------------------------------------------------- ;
lerc_status lerc_decode(const unsigned char* pLercBlob, unsigned int blobSize,
unsigned char* pValidBytes, int nDim, int nCols, int nRows, int nBands, unsigned int dataType, void* pData)
{
if (!pLercBlob || !blobSize || !pData || dataType >= Lerc::DT_Undefined || nDim <= 0 || nCols <= 0 || nRows <= 0 || nBands <= 0)
return (lerc_status)ErrCode::WrongParam;
BitMask bitMask;
if (pValidBytes)
{
bitMask.SetSize(nCols, nRows);
bitMask.SetAllInvalid();
}
BitMask* pBitMask = pValidBytes ? &bitMask : nullptr;
Lerc::DataType dt = (Lerc::DataType)dataType;
ErrCode errCode = Lerc::Decode(pLercBlob, blobSize, pBitMask, nDim, nCols, nRows, nBands, dt, pData);
if (errCode != ErrCode::Ok)
return (lerc_status)errCode;
if (pValidBytes)
{
for (int k = 0, i = 0; i < nRows; i++)
for (int j = 0; j < nCols; j++, k++)
pValidBytes[k] = bitMask.IsValid(k);
}
return (lerc_status)ErrCode::Ok;
}
// -------------------------------------------------------------------------- ;
lerc_status lerc_decodeToDouble(const unsigned char* pLercBlob, unsigned int blobSize,
unsigned char* pValidBytes, int nDim, int nCols, int nRows, int nBands, double* pData)
{
if (!pLercBlob || !blobSize || !pData || nDim <= 0 || nCols <= 0 || nRows <= 0 || nBands <= 0)
return (lerc_status)ErrCode::WrongParam;
Lerc::LercInfo lercInfo;
ErrCode errCode;
if ((errCode = Lerc::GetLercInfo(pLercBlob, blobSize, lercInfo)) != ErrCode::Ok)
return (lerc_status)errCode;
Lerc::DataType dt = lercInfo.dt;
if (dt > Lerc::DT_Double)
return (lerc_status)ErrCode::Failed;
BitMask bitMask;
if (pValidBytes)
{
bitMask.SetSize(nCols, nRows);
bitMask.SetAllInvalid();
}
BitMask* pBitMask = pValidBytes ? &bitMask : nullptr;
if (dt == Lerc::DT_Double)
{
if ((errCode = Lerc::Decode(pLercBlob, blobSize, pBitMask, nDim, nCols, nRows, nBands, dt, pData)) != ErrCode::Ok)
return (lerc_status)errCode;
}
else
{
// use the buffer passed for in place decode and convert
int sizeofDt[] = { 1, 1, 2, 2, 4, 4, 4, 8 };
size_t nDataValues = nDim * nCols * nRows * nBands;
void* ptrDec = (Byte*)pData + nDataValues * (sizeof(double) - sizeofDt[dt]);
if ((errCode = Lerc::Decode(pLercBlob, blobSize, pBitMask, nDim, nCols, nRows, nBands, dt, ptrDec)) != ErrCode::Ok)
return (lerc_status)errCode;
if ((errCode = Lerc::ConvertToDouble(ptrDec, dt, nDataValues, pData)) != ErrCode::Ok)
return (lerc_status)errCode;
}
if (pValidBytes)
{
for (int k = 0, i = 0; i < nRows; i++)
for (int j = 0; j < nCols; j++, k++)
pValidBytes[k] = bitMask.IsValid(k);
}
return (lerc_status)ErrCode::Ok;
}
// -------------------------------------------------------------------------- ;