./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
75 / 75
100.0%
Branches:
46 / 47
97.9%
Functions:
8 / 8
100.0%

src/raster/geotiff/tiff_format.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "raster/geotiff/tiff_format.h"
4
5 namespace terraingen::detail {
6
7 50 void ThrowFileError(const std::filesystem::path& path, const std::string& reason) {
8
3/3
✓ Branch 2 taken 50 times.
✓ Branch 5 taken 50 times.
✓ Branch 8 taken 50 times.
150 throw GeoTiffError(path.string() + ": " + reason);
9 }
10
11 23519 std::size_t FieldSize(std::uint16_t type) {
12
5/5
✓ Branch 0 taken 14289 times.
✓ Branch 1 taken 6638 times.
✓ Branch 2 taken 2041 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 550 times.
23519 switch (type) {
13 case kByte:
14 case kAscii:
15 case kSByte:
16 case kUndefined:
17 return 1;
18 14289 case kShort:
19 14289 case kSShort:
20 14289 return 2;
21 6638 case kLong:
22 6638 case kSLong:
23 6638 case kFloat:
24 6638 return 4;
25 2041 case kRational:
26 2041 case kSRational:
27 2041 case kDouble:
28 2041 case kLong8:
29 2041 case kSLong8:
30 2041 case kIfd8:
31 2041 return 8;
32 1 default:
33 1 return 0;
34 }
35 }
36
37 817 std::optional<TiffCompression> CompressionFromCode(std::uint64_t code) {
38
5/5
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 650 times.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 60 times.
✓ Branch 4 taken 8 times.
817 switch (code) {
39 84 case kCompressionNone:
40 84 return TiffCompression::None;
41 650 case kCompressionLzw:
42 650 return TiffCompression::Lzw;
43 15 case kCompressionPackBits:
44 15 return TiffCompression::PackBits;
45 60 case kCompressionDeflate:
46 60 case kCompressionAdobeDeflate:
47 60 return TiffCompression::Deflate;
48 8 default:
49 8 return std::nullopt;
50 }
51 }
52
53 121 std::uint16_t TiffCompressionCode(TiffCompression compression) {
54
4/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 108 times.
121 switch (compression) {
55 case TiffCompression::Lzw:
56 return kCompressionLzw;
57 1 case TiffCompression::PackBits:
58 1 return kCompressionPackBits;
59 8 case TiffCompression::Deflate:
60 8 return kCompressionDeflate;
61 4 default:
62 4 return kCompressionNone;
63 }
64 }
65
66 24 GeoTransform ShiftHalfPixel(const GeoTransform& transform, double sign) {
67 24 GeoTransform shifted = transform;
68 24 shifted.originX += sign * 0.5 * (transform.pixelWidth + transform.rowRotation);
69 24 shifted.originY += sign * 0.5 * (transform.colRotation + transform.pixelHeight);
70 24 return shifted;
71 }
72
73 117 ModelTags EncodeTransform(const GeoTransform& transform, bool pixelIsPoint) {
74
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 113 times.
117 const GeoTransform stored = pixelIsPoint ? ShiftHalfPixel(transform, 1.0) : transform;
75
2/2
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 6 times.
117 ModelTags tags;
76
2/2
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 6 times.
117 if (!stored.IsRotated()) {
77
1/1
✓ Branch 1 taken 111 times.
111 tags.pixelScale = {stored.pixelWidth, -stored.pixelHeight, 0.0};
78
1/1
✓ Branch 1 taken 111 times.
111 tags.tiepoint = {0.0, 0.0, 0.0, stored.originX, stored.originY, 0.0};
79 } else {
80 6 tags.transformation = {stored.pixelWidth,
81 stored.rowRotation,
82 0.0,
83 stored.originX,
84 stored.colRotation,
85 stored.pixelHeight,
86 0.0,
87 stored.originY,
88 0.0,
89 0.0,
90 0.0,
91 0.0,
92 0.0,
93 0.0,
94 0.0,
95
1/1
✓ Branch 1 taken 6 times.
6 1.0};
96 }
97 117 return tags;
98 }
99
100 namespace {
101
102 /// The transform the model tags store as GeoTIFF places it; nothing when they store none.
103 807 std::optional<GeoTransform> StoredTransform(const ModelTags& tags) {
104
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 783 times.
807 if (!tags.transformation.empty()) {
105 24 const std::vector<double>& m = tags.transformation;
106
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 23 times.
24 if (m.size() < 16) {
107
1/1
✓ Branch 2 taken 1 time.
1 throw GeoTiffError("ModelTransformation needs 16 values");
108 }
109 23 return GeoTransform::FromGdal({m[3], m[0], m[1], m[7], m[4], m[5]});
110 }
111
4/4
✓ Branch 0 taken 768 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 767 times.
783 if (tags.pixelScale.empty() || tags.tiepoint.empty()) {
112 16 return std::nullopt;
113 }
114 767 const std::vector<double>& s = tags.pixelScale;
115 767 const std::vector<double>& t = tags.tiepoint;
116
3/4
✓ Branch 0 taken 766 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 766 times.
✗ Branch 3 not taken.
767 if (s.size() < 2 || t.size() < 6) {
117
1/1
✓ Branch 2 taken 1 time.
1 throw GeoTiffError("ModelPixelScale needs 2 and ModelTiepoint 6 values");
118 }
119
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 765 times.
766 if (t.size() > 6) {
120
1/1
✓ Branch 2 taken 1 time.
1 throw GeoTiffError("multiple tiepoints (ground control points) are not supported");
121 }
122 765 return GeoTransform::FromGdal({t[3] - t[0] * s[0], s[0], 0.0, t[4] + t[1] * s[1], 0.0, -s[1]});
123 }
124
125 } // namespace
126
127 807 std::optional<GeoTransform> DecodeTransform(const ModelTags& tags, bool pixelIsPoint) {
128 807 const std::optional<GeoTransform> stored = StoredTransform(tags);
129
4/4
✓ Branch 0 taken 788 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 770 times.
804 if (stored && pixelIsPoint) {
130 18 return ShiftHalfPixel(*stored, -1.0);
131 }
132 786 return stored;
133 }
134
135 } // namespace terraingen::detail
136