./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
216 / 219
98.6%
Branches:
123 / 131
93.9%
Functions:
22 / 22, 1 excl
100.0%

src/raster/geotiff/tiff_codecs.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "raster/geotiff/tiff_codecs.h"
4
5 #include "terraingen/raster/geotiff/geotiff.h"
6
7 #include <miniz.h>
8
9 #include <algorithm>
10 #include <array>
11 #include <cstring>
12
13 namespace terraingen::detail {
14
15 namespace {
16
17 constexpr int kClear = 256;
18 constexpr int kEndOfInfo = 257;
19 constexpr int kFirstCode = 258;
20 constexpr int kMaxCode = 4096;
21
22 /// Reads MSB-first codes of varying width.
23 class BitReader {
24 public:
25 634 explicit BitReader(Span<const std::uint8_t> data) : m_data(data) {}
26
27 /// The next `width`-bit code, or EndOfInfo when the data runs out.
28 840148 int Next(int width) {
29
2/2
✓ Branch 0 taken 1103223 times.
✓ Branch 1 taken 840158 times.
1943381 while (m_count < width) {
30
1/2
✓ Branch 0 taken 1103223 times.
✗ Branch 1 not taken.
1103223 if (m_position >= m_data.size()) {
31 return kEndOfInfo;
32 }
33 1103223 m_buffer = (m_buffer << 8) | m_data[m_position++];
34 1103233 m_count += 8;
35 }
36 840158 m_count -= width;
37 840158 return static_cast<int>((m_buffer >> m_count) & ((1u << width) - 1u));
38 }
39
40 private:
41 Span<const std::uint8_t> m_data;
42 std::size_t m_position = 0;
43 std::uint32_t m_buffer = 0;
44 int m_count = 0;
45 };
46
47 /// Writes MSB-first codes of varying width.
48 516 class BitWriter {
49 public:
50 1150197 void Put(int code, int width) {
51 1150197 m_buffer = (m_buffer << width) | static_cast<std::uint32_t>(code);
52 1150197 m_count += width;
53
2/2
✓ Branch 0 taken 1590558 times.
✓ Branch 1 taken 1150197 times.
2740755 while (m_count >= 8) {
54 1590558 m_count -= 8;
55 1590558 m_bytes.push_back(static_cast<std::uint8_t>(m_buffer >> m_count));
56 }
57 1150197 }
58
59 258 std::vector<std::uint8_t> Finish() {
60
2/2
✓ Branch 0 taken 247 times.
✓ Branch 1 taken 11 times.
258 if (m_count > 0) {
61 247 m_bytes.push_back(static_cast<std::uint8_t>(m_buffer << (8 - m_count)));
62 247 m_count = 0;
63 }
64 258 return std::move(m_bytes);
65 }
66
67 private:
68 std::vector<std::uint8_t> m_bytes;
69 std::uint32_t m_buffer = 0;
70 int m_count = 0;
71 };
72
73 3 [[noreturn]] void Corrupt(const char* what) {
74
2/2
✓ Branch 2 taken 3 times.
✓ Branch 5 taken 3 times.
6 throw GeoTiffError(std::string("Corrupt LZW data: ") + what);
75 }
76
77 /// Throws for `codec` data that decoded to `decoded` of the `size` bytes expected.
78 4 [[noreturn]] void ShortOutput(const char* codec, std::size_t decoded, std::size_t size) {
79
3/3
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 4 times.
16 throw GeoTiffError(std::string("Corrupt ") + codec + " data: decoded " +
80
4/4
✓ Branch 2 taken 4 times.
✓ Branch 5 taken 4 times.
✓ Branch 8 taken 4 times.
✓ Branch 11 taken 4 times.
16 std::to_string(decoded) + " of " + std::to_string(size) + " bytes");
81 }
82
83 /// The LZW string table for decoding: each code's string as its prefix code and last byte.
84 class LzwDecodeTable {
85 public:
86 634 LzwDecodeTable() {
87
2/2
✓ Branch 0 taken 162304 times.
✓ Branch 1 taken 634 times.
162938 for (int code = 0; code < 256; ++code) {
88 162304 m_suffix[static_cast<std::size_t>(code)] = static_cast<std::uint8_t>(code);
89 162304 m_length[static_cast<std::size_t>(code)] = 1;
90 }
91 634 }
92
93 /// The code the next added string gets.
94 1677567 int Next() const {
95 1677567 return m_next;
96 }
97
98 /// Forgets every added string, as after Clear.
99 703 void Reset() {
100 703 m_next = kFirstCode;
101 }
102
103 /// Adds the string of `previous` followed by `last`.
104 838742 void Add(int previous, std::uint8_t last) {
105 838742 const auto entry = static_cast<std::size_t>(m_next++);
106 838742 m_prefix[entry] = static_cast<std::uint16_t>(previous);
107 838742 m_suffix[entry] = last;
108 838742 m_length[entry] =
109 838742 static_cast<std::uint16_t>(m_length[static_cast<std::size_t>(previous)] + 1);
110 }
111
112 /// Appends the string of `code` to `out` and returns its first byte.
113 839408 std::uint8_t Emit(int code, std::vector<std::uint8_t>& out) const {
114 839408 const std::size_t start = out.size();
115 839408 out.resize(start + m_length[static_cast<std::size_t>(code)]);
116
2/2
✓ Branch 0 taken 21621277 times.
✓ Branch 1 taken 839334 times.
22460611 for (std::size_t i = out.size(); i > start; --i) {
117 21621277 out[i - 1] = m_suffix[static_cast<std::size_t>(code)];
118 21621277 code = m_prefix[static_cast<std::size_t>(code)];
119 }
120 839334 return out[start];
121 }
122
123 private:
124 std::array<std::uint16_t, kMaxCode> m_prefix{};
125 std::array<std::uint8_t, kMaxCode> m_suffix{};
126 std::array<std::uint16_t, kMaxCode> m_length{};
127 int m_next = kFirstCode;
128 };
129
130 /// The LZW string table for encoding: (prefix code, next byte) -> code, open-addressed with a
131 /// generation stamp so that clearing it is constant time.
132 class LzwTable {
133 public:
134
2/2
✓ Branch 2 taken 258 times.
✓ Branch 5 taken 258 times.
258 LzwTable() : m_keys(kSlots), m_codes(kSlots), m_stamps(kSlots, 0) {}
135
136 219 void Clear() {
137 219 ++m_generation;
138 219 }
139
140 /// The code for `key`, or -1.
141 30033938 int Find(std::uint32_t key) const {
142 2679145 for (std::size_t slot = Hash(key);; slot = (slot + 1) & (kSlots - 1)) {
143
2/2
✓ Branch 0 taken 31563878 times.
✓ Branch 1 taken 1149205 times.
32713083 if (m_stamps[slot] != m_generation) {
144 return -1;
145 }
146
2/2
✓ Branch 0 taken 28884733 times.
✓ Branch 1 taken 2679145 times.
31563878 if (m_keys[slot] == key) {
147 28884733 return m_codes[slot];
148 }
149 }
150 }
151
152 1149205 void Insert(std::uint32_t key, int code) {
153 1149205 std::size_t slot = Hash(key);
154
2/2
✓ Branch 0 taken 131258 times.
✓ Branch 1 taken 1149205 times.
1280463 while (m_stamps[slot] == m_generation) {
155 131258 slot = (slot + 1) & (kSlots - 1);
156 }
157 1149205 m_keys[slot] = key;
158 1149205 m_codes[slot] = static_cast<std::uint16_t>(code);
159 1149205 m_stamps[slot] = m_generation;
160 1149205 }
161
162 private:
163 static constexpr std::size_t kSlots = 16384;
164
165 31183143 static std::size_t Hash(std::uint32_t key) {
166 31183143 return static_cast<std::size_t>((key * 2654435761u) >> 18) & (kSlots - 1);
167 }
168
169 std::vector<std::uint32_t> m_keys;
170 std::vector<std::uint16_t> m_codes;
171 std::vector<std::uint32_t> m_stamps;
172 std::uint32_t m_generation = 1;
173 };
174
175 /// Writes LZW codes, widening them one code early as the table grows.
176 ✗ class LzwCodeWriter {
177 public:
178
1/1
✓ Branch 1 taken 258 times.
258 LzwCodeWriter() {
179
1/1
✓ Branch 1 taken 258 times.
258 m_writer.Put(kClear, m_width);
180 258 }
181
182 /// The code the next string gets.
183 2298410 int Next() const {
184 2298410 return m_next;
185 }
186
187 /// Writes `code`; the encoder adds a string for every code it writes.
188 1149462 void PutCode(int code) {
189 1149462 m_writer.Put(code, m_width);
190 1149462 ++m_next;
191 1149462 m_width = LzwNextWidth(m_next, m_width);
192 1149462 }
193
194 /// Writes Clear and starts again from 9-bit codes.
195 219 void Clear() {
196 219 m_writer.Put(kClear, m_width);
197 219 m_width = 9;
198 219 m_next = kFirstCode;
199 219 }
200
201 258 std::vector<std::uint8_t> Finish() {
202 258 m_writer.Put(kEndOfInfo, m_width);
203 258 return m_writer.Finish();
204 }
205
206 private:
207 BitWriter m_writer;
208 int m_width = 9;
209 int m_next = kFirstCode;
210 };
211
212 } // namespace
213
214 1988206 int LzwNextWidth(int codes, int width) {
215
4/4
✓ Branch 0 taken 2273 times.
✓ Branch 1 taken 1985933 times.
✓ Branch 2 taken 2272 times.
✓ Branch 3 taken 1 time.
1988206 return codes >= (1 << width) && width < 12 ? width + 1 : width;
216 }
217
218 635 std::vector<std::uint8_t> DecodeLzw(Span<const std::uint8_t> data, std::size_t size) {
219
4/6
✓ Branch 0 taken 635 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 634 times.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
635 if (data.size() >= 2 && data[0] == 0 && (data[1] & 1u) != 0) {
220
1/1
✓ Branch 2 taken 1 time.
1 throw GeoTiffError("Old-style LZW compression is not supported");
221 }
222 634 LzwDecodeTable table;
223
1/1
✓ Branch 1 taken 634 times.
634 std::vector<std::uint8_t> out;
224
1/1
✓ Branch 1 taken 634 times.
634 out.reserve(size);
225 634 BitReader reader(data);
226 634 int width = 9;
227 634 int previous = -1;
228
2/2
✓ Branch 0 taken 840151 times.
✓ Branch 1 taken 630 times.
840781 while (out.size() < size) {
229 840151 const int code = reader.Next(width);
230
2/2
✓ Branch 0 taken 840233 times.
✓ Branch 1 taken 1 time.
840234 if (code == kEndOfInfo) {
231 break;
232 }
233
2/2
✓ Branch 0 taken 703 times.
✓ Branch 1 taken 839530 times.
840233 if (code == kClear) {
234 703 width = 9;
235 703 table.Reset();
236 703 previous = -1;
237 1405 continue;
238 }
239
2/2
✓ Branch 0 taken 705 times.
✓ Branch 1 taken 838825 times.
839530 if (previous < 0) {
240
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 702 times.
705 if (code > 255) {
241 3 Corrupt("first code after Clear is not a byte");
242 }
243
1/1
✓ Branch 1 taken 702 times.
702 table.Emit(code, out);
244 702 previous = code;
245 702 continue;
246 }
247
2/4
✓ Branch 0 taken 838825 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 838825 times.
✗ Branch 3 not taken.
838825 if (code > table.Next() || table.Next() >= kMaxCode) {
248 ✗ Corrupt("code out of range");
249 }
250 838825 std::uint8_t first = 0;
251
2/2
✓ Branch 0 taken 784290 times.
✓ Branch 1 taken 54535 times.
838825 if (code < table.Next()) {
252
1/1
✓ Branch 1 taken 784257 times.
784290 first = table.Emit(code, out);
253 } else {
254
1/1
✓ Branch 1 taken 54540 times.
54535 first = table.Emit(previous, out);
255
1/1
✓ Branch 1 taken 54485 times.
54540 out.push_back(first);
256 }
257 838742 table.Add(previous, first);
258 838742 width = LzwNextWidth(table.Next() + 1, width);
259 838742 previous = code;
260 }
261
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 630 times.
631 if (out.size() < size) {
262 1 ShortOutput("LZW", out.size(), size);
263 }
264
1/1
✓ Branch 1 taken 630 times.
630 out.resize(size);
265 630 return out;
266 4 }
267
268 258 std::vector<std::uint8_t> EncodeLzw(Span<const std::uint8_t> data) {
269 258 LzwCodeWriter writer;
270
1/1
✓ Branch 1 taken 258 times.
258 LzwTable table;
271 258 int current = -1;
272
2/2
✓ Branch 0 taken 30034195 times.
✓ Branch 1 taken 258 times.
30034453 for (const std::uint8_t byte : data) {
273
2/2
✓ Branch 0 taken 257 times.
✓ Branch 1 taken 30033938 times.
30034195 if (current < 0) {
274 257 current = byte;
275 257 continue;
276 }
277 30033938 const std::uint32_t key = (static_cast<std::uint32_t>(current) << 8) | byte;
278
2/2
✓ Branch 1 taken 28884733 times.
✓ Branch 2 taken 1149205 times.
30033938 if (const int found = table.Find(key); found >= 0) {
279 28884733 current = found;
280 28884733 continue;
281 }
282 1149205 table.Insert(key, writer.Next());
283
1/1
✓ Branch 1 taken 1149205 times.
1149205 writer.PutCode(current);
284
2/2
✓ Branch 0 taken 219 times.
✓ Branch 1 taken 1148986 times.
1149205 if (writer.Next() >= kMaxCode - 2) {
285
1/1
✓ Branch 1 taken 219 times.
219 writer.Clear();
286 219 table.Clear();
287 }
288 1149205 current = byte;
289 }
290
2/2
✓ Branch 0 taken 257 times.
✓ Branch 1 taken 1 time.
258 if (current >= 0) {
291
1/1
✓ Branch 1 taken 257 times.
257 writer.PutCode(current);
292 }
293
1/1
✓ Branch 1 taken 258 times.
258 return writer.Finish();
294 258 }
295
296 430 std::vector<std::uint8_t> DecodeDeflate(Span<const std::uint8_t> data, std::size_t size) {
297 430 std::vector<std::uint8_t> out(size);
298
1/1
✓ Branch 1 taken 429 times.
430 const std::size_t written = tinfl_decompress_mem_to_mem(
299
1/1
✓ Branch 1 taken 429 times.
430 out.data(), out.size(), data.data(), data.size(), TINFL_FLAG_PARSE_ZLIB_HEADER);
300
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 427 times.
429 if (written == TINFL_DECOMPRESS_MEM_TO_MEM_FAILED) {
301
1/1
✓ Branch 2 taken 2 times.
2 throw GeoTiffError("Corrupt Deflate data");
302 }
303
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 426 times.
427 if (written != size) {
304 1 ShortOutput("Deflate", written, size);
305 }
306 426 return out;
307 3 }
308
309 61 std::vector<std::uint8_t> EncodeDeflate(Span<const std::uint8_t> data) {
310 61 constexpr int kLevel = 6;
311 61 constexpr int kWindowBits = 15;
312 61 const mz_uint flags =
313 61 tdefl_create_comp_flags_from_zip_params(kLevel, kWindowBits, MZ_DEFAULT_STRATEGY);
314 61 std::size_t length = 0;
315 61 void* compressed =
316 61 tdefl_compress_mem_to_heap(data.data(), data.size(), &length, static_cast<int>(flags));
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 if (compressed == nullptr) {
318
0/1
✗ Branch 2 not taken.
✗ throw GeoTiffError("Deflate compression failed");
319 }
320 61 const auto* bytes = static_cast<const std::uint8_t*>(compressed);
321 61 std::vector<std::uint8_t> out(bytes, bytes + length);
322
1/1
✓ Branch 1 taken 61 times.
61 mz_free(compressed);
323 61 return out;
324 }
325
326 17 std::vector<std::uint8_t> DecodePackBits(Span<const std::uint8_t> data, std::size_t size) {
327
1/1
✓ Branch 1 taken 17 times.
17 std::vector<std::uint8_t> out;
328
1/1
✓ Branch 1 taken 17 times.
17 out.reserve(size);
329 std::size_t i = 0;
330
4/4
✓ Branch 0 taken 236 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 234 times.
✓ Branch 3 taken 2 times.
249 while (out.size() < size && i < data.size()) {
331 234 const auto header = static_cast<std::int8_t>(data[i++]);
332
2/2
✓ Branch 0 taken 232 times.
✓ Branch 1 taken 2 times.
234 if (header >= 0) {
333 232 const std::size_t count = static_cast<std::size_t>(header) + 1;
334
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 231 times.
232 if (i + count > data.size()) {
335
1/1
✓ Branch 2 taken 1 time.
1 throw GeoTiffError("Corrupt PackBits data: literal run past the end");
336 }
337
1/1
✓ Branch 1 taken 231 times.
231 out.insert(out.end(), data.begin() + static_cast<std::ptrdiff_t>(i),
338
1/1
✓ Branch 1 taken 231 times.
231 data.begin() + static_cast<std::ptrdiff_t>(i + count));
339 231 i += count;
340
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 } else if (header != -128) {
341
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 if (i >= data.size()) {
342
1/1
✓ Branch 2 taken 1 time.
1 throw GeoTiffError("Corrupt PackBits data: repeat run past the end");
343 }
344
1/1
✓ Branch 2 taken 1 time.
1 out.insert(out.end(), static_cast<std::size_t>(1 - header), data[i++]);
345 }
346 }
347
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 13 times.
15 if (out.size() < size) {
348 2 ShortOutput("PackBits", out.size(), size);
349 }
350
1/1
✓ Branch 1 taken 13 times.
13 out.resize(size);
351 13 return out;
352 4 }
353
354 314 void EncodeFloatPredictor(std::uint8_t* data, std::size_t rows, std::size_t sampleBytes,
355 std::size_t rowSamples, std::size_t stride) {
356 314 const std::size_t rowBytes = rowSamples * sampleBytes;
357 314 std::vector<std::uint8_t> native(rowBytes);
358
2/2
✓ Branch 0 taken 32086 times.
✓ Branch 1 taken 314 times.
32400 for (std::size_t r = 0; r < rows; ++r) {
359 32086 std::uint8_t* row = data + r * rowBytes;
360 32086 std::copy_n(row, rowBytes, native.begin());
361
2/2
✓ Branch 1 taken 7456036 times.
✓ Branch 2 taken 32086 times.
7488122 for (std::size_t sample = 0; sample < rowSamples; ++sample) {
362
2/2
✓ Branch 0 taken 29824144 times.
✓ Branch 1 taken 7456036 times.
37280180 for (std::size_t byte = 0; byte < sampleBytes; ++byte) {
363 29824144 row[(sampleBytes - byte - 1) * rowSamples + sample] =
364 29824144 native[sample * sampleBytes + byte];
365 }
366 }
367
2/2
✓ Branch 0 taken 29792055 times.
✓ Branch 1 taken 32086 times.
29824141 for (std::size_t i = rowBytes - 1; i >= stride; --i) {
368 29792055 row[i] = static_cast<std::uint8_t>(row[i] - row[i - stride]);
369 }
370 }
371 314 }
372
373 889 void DecodeFloatPredictor(std::uint8_t* data, std::size_t rows, std::size_t sampleBytes,
374 std::size_t rowSamples, std::size_t stride) {
375 889 const std::size_t rowBytes = rowSamples * sampleBytes;
376 889 std::vector<std::uint8_t> planes(rowBytes);
377
2/2
✓ Branch 0 taken 33879 times.
✓ Branch 1 taken 889 times.
34768 for (std::size_t r = 0; r < rows; ++r) {
378 33879 std::uint8_t* row = data + r * rowBytes;
379
2/2
✓ Branch 0 taken 21929169 times.
✓ Branch 1 taken 33879 times.
21963048 for (std::size_t i = stride; i < rowBytes; ++i) {
380 21929169 row[i] = static_cast<std::uint8_t>(row[i] + row[i - stride]);
381 }
382 33879 std::copy_n(row, rowBytes, planes.begin());
383
2/2
✓ Branch 1 taken 5514428 times.
✓ Branch 2 taken 33881 times.
5548307 for (std::size_t sample = 0; sample < rowSamples; ++sample) {
384
2/2
✓ Branch 0 taken 22097584 times.
✓ Branch 1 taken 5514428 times.
27612012 for (std::size_t byte = 0; byte < sampleBytes; ++byte) {
385 22097584 row[sample * sampleBytes + byte] =
386 22097584 planes[(sampleBytes - byte - 1) * rowSamples + sample];
387 }
388 }
389 }
390 889 }
391
392 } // namespace terraingen::detail
393