-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlerc1Image.h
252 lines (205 loc) · 6.11 KB
/
lerc1Image.h
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
243
244
245
246
247
248
249
250
251
252
/*
Copyright 2015 - 2024 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
Lucian Plesea
*/
#ifndef LERC1IMAGE_H
#define LERC1IMAGE_H
#include <cstring>
#include <vector>
#ifndef NAMESPACE_LERC1_START
#define NAMESPACE_LERC1_START \
namespace Lerc1NS \
{
#define NAMESPACE_LERC1_END }
#define USING_NAMESPACE_LERC1 using namespace Lerc1NS;
#endif
NAMESPACE_LERC1_START
static_assert(sizeof(float) == 4, "lerc requires float to be exactly 4 bytes");
typedef unsigned char Byte;
/** BitMaskV1 - Convenient and fast access to binary mask bits
* includes RLE compression and decompression
*
*/
class BitMaskV1
{
public:
BitMaskV1() : m_nRows(0), m_nCols(0)
{
}
int size() const
{
return 1 + (m_nCols * m_nRows - 1) / 8;
}
void Set(int k, bool v)
{
if (v)
SetValid(k);
else
SetInvalid(k);
}
Byte IsValid(int k) const
{
return (bits[k >> 3] & Bit(k)) != 0;
}
void resize(int nCols, int nRows)
{
m_nRows = nRows;
m_nCols = nCols;
bits.resize(size());
}
// max RLE compressed size is n + 4 + 2 * (n - 1) / 32767
// Returns encoded size
int RLEcompress(Byte *aRLE) const;
// Encoded size, without doing the work
int RLEsize() const;
// Decompress a RLE bitmask, bitmask size should be already set
// Returns false if input seems wrong
bool RLEdecompress(const Byte *src, size_t sz);
private:
int m_nRows, m_nCols;
std::vector<Byte> bits;
static Byte Bit(int k)
{
return static_cast<Byte>(0x80 >> (k & 7));
}
void SetValid(int k)
{
bits[k >> 3] |= Bit(k);
}
void SetInvalid(int k)
{
bits[k >> 3] &= ~Bit(k);
}
};
template <typename T> class TImage
{
public:
TImage() : width_(0), height_(0)
{
}
~TImage()
{
}
bool setsize(int width, int height)
{
width_ = width;
height_ = height;
values.resize(getSize());
return true;
}
int getWidth() const
{
return width_;
}
int getHeight() const
{
return height_;
}
int getSize() const
{
return width_ * height_;
}
const T &operator()(int row, int col) const
{
return values[row * width_ + col];
}
T &operator()(int row, int col)
{
return values[row * width_ + col];
}
const T *data() const
{
return values.data();
}
private:
int width_, height_;
std::vector<T> values;
};
class Lerc1Image : public TImage<float>
{
protected:
struct InfoFromComputeNumBytes
{
double maxZError;
int numTilesVertCnt;
int numTilesHoriCnt;
int numBytesCnt;
float maxCntInImg;
int numTilesVertZ;
int numTilesHoriZ;
int numBytesZ;
float maxZInImg;
InfoFromComputeNumBytes()
{
std::memset(this, 0, sizeof(*this));
}
};
bool findTiling(double maxZError, int &numTilesVert, int &numTilesHori,
int &numBytesOpt, float &maxValInImg) const;
bool writeTiles(double maxZError, int numTilesVert, int numTilesHori,
Byte *bArr, int &numBytes, float &maxValInImg) const;
bool readTiles(double maxZErrorInFile, int numTilesVert, int numTilesHori,
float maxValInImg, Byte *bArr, size_t nRemainingBytes);
bool computeZStats(int r0, int r1, int c0, int c1, float &zMin, float &zMax,
int &numValidPixel, int &numFinite) const;
// returns true if all floating point values in the region have the same
// binary representation
bool isallsameval(int r0, int r1, int c0, int c1) const;
bool writeZTile(Byte **ppByte, int &numBytes, int r0, int r1, int c0,
int c1, int numValidPixel, float zMin, float zMax,
double maxZError) const;
bool readZTile(Byte **ppByte, size_t &nRemainingBytes, int r0, int r1,
int c0, int c1, double maxZErrorInFile, float maxZInImg);
unsigned int
computeNumBytesNeededToWrite(double maxZError, bool onlyZPart,
InfoFromComputeNumBytes *info) const;
std::vector<unsigned int> idataVec; // temporary buffer
BitMaskV1 mask;
public:
/// binary file IO with optional compression
/// (maxZError = 0 means no lossy compression for Z; the mask part is
/// compressed lossless or not at all) read succeeds only if maxZError on
/// file <= maxZError requested (!)
Lerc1Image()
{
}
~Lerc1Image()
{
}
static unsigned int computeNumBytesNeededToWriteVoidImage();
// Only initialize the size from the header, if LERC1
static bool getwh(const Byte *ppByte, size_t nBytes, int &w, int &h);
void resize(int width, int height)
{
setsize(width, height);
mask.resize(getWidth(), getHeight());
}
bool IsValid(int row, int col) const
{
return mask.IsValid(row * getWidth() + col) != 0;
}
void SetMask(int row, int col, bool v)
{
mask.Set(row * getWidth() + col, v);
}
// Read and write into a memory buffer
bool write(Byte **ppByte, double maxZError = 0,
bool onlyZPart = false) const;
bool read(Byte **ppByte, size_t &nRemainingBytes, double maxZError,
bool onlyZPart = false);
};
NAMESPACE_LERC1_END
#endif