src/raster/geotiff/tiff_format.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief TIFF and GeoTIFF numbers shared by the GeoTIFF reader and writer. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/raster/geo_transform.h" | ||
| 8 | #include "terraingen/raster/geotiff/geotiff.h" | ||
| 9 | #include "terraingen/util/bits.h" | ||
| 10 | |||
| 11 | #include <cstddef> | ||
| 12 | #include <cstdint> | ||
| 13 | #include <filesystem> | ||
| 14 | #include <optional> | ||
| 15 | #include <string> | ||
| 16 | #include <vector> | ||
| 17 | |||
| 18 | namespace terraingen::detail { | ||
| 19 | |||
| 20 | static_assert(kLittleEndian, "The GeoTIFF reader and writer assume a little-endian host"); | ||
| 21 | |||
| 22 | /// TIFF tags read or written. | ||
| 23 | enum Tag : std::uint16_t { | ||
| 24 | kImageWidth = 256, | ||
| 25 | kImageLength = 257, | ||
| 26 | kBitsPerSample = 258, | ||
| 27 | kCompression = 259, | ||
| 28 | kPhotometric = 262, | ||
| 29 | kStripOffsets = 273, | ||
| 30 | kSamplesPerPixel = 277, | ||
| 31 | kRowsPerStrip = 278, | ||
| 32 | kStripByteCounts = 279, | ||
| 33 | kPlanarConfiguration = 284, | ||
| 34 | kPredictor = 317, | ||
| 35 | kTileWidth = 322, | ||
| 36 | kTileLength = 323, | ||
| 37 | kTileOffsets = 324, | ||
| 38 | kTileByteCounts = 325, | ||
| 39 | kExtraSamples = 338, | ||
| 40 | kSampleFormat = 339, | ||
| 41 | kModelPixelScale = 33550, | ||
| 42 | kModelTiepoint = 33922, | ||
| 43 | kModelTransformation = 34264, | ||
| 44 | kGeoKeyDirectory = 34735, | ||
| 45 | kGeoDoubleParams = 34736, | ||
| 46 | kGeoAsciiParams = 34737, | ||
| 47 | kGdalMetadata = 42112, | ||
| 48 | kGdalNoData = 42113, | ||
| 49 | }; | ||
| 50 | |||
| 51 | /// TIFF field types. | ||
| 52 | enum FieldType : std::uint16_t { | ||
| 53 | kByte = 1, | ||
| 54 | kAscii = 2, | ||
| 55 | kShort = 3, | ||
| 56 | kLong = 4, | ||
| 57 | kRational = 5, | ||
| 58 | kSByte = 6, | ||
| 59 | kUndefined = 7, | ||
| 60 | kSShort = 8, | ||
| 61 | kSLong = 9, | ||
| 62 | kSRational = 10, | ||
| 63 | kFloat = 11, | ||
| 64 | kDouble = 12, | ||
| 65 | kLong8 = 16, | ||
| 66 | kSLong8 = 17, | ||
| 67 | kIfd8 = 18, | ||
| 68 | }; | ||
| 69 | |||
| 70 | /// Values of the Compression tag. | ||
| 71 | enum CompressionCode : std::uint16_t { | ||
| 72 | kCompressionNone = 1, | ||
| 73 | kCompressionLzw = 5, | ||
| 74 | kCompressionDeflate = 8, | ||
| 75 | kCompressionAdobeDeflate = 32946, | ||
| 76 | kCompressionPackBits = 32773, | ||
| 77 | }; | ||
| 78 | |||
| 79 | /// GeoKey numbers from OGC 19-008r4. | ||
| 80 | enum GeoKey : std::uint16_t { | ||
| 81 | kGtModelTypeGeoKey = 1024, | ||
| 82 | kGtRasterTypeGeoKey = 1025, | ||
| 83 | kGeographicTypeGeoKey = 2048, | ||
| 84 | kGeogGeodeticDatumGeoKey = 2050, | ||
| 85 | kGeogAngularUnitsGeoKey = 2054, | ||
| 86 | kGeogEllipsoidGeoKey = 2056, | ||
| 87 | kGeogSemiMajorAxisGeoKey = 2057, | ||
| 88 | kGeogSemiMinorAxisGeoKey = 2058, | ||
| 89 | }; | ||
| 90 | |||
| 91 | constexpr std::uint16_t kModelTypeGeographic = 2; ///< GTModelTypeGeoKey value. | ||
| 92 | constexpr std::uint16_t kRasterPixelIsArea = 1; ///< GTRasterTypeGeoKey value. | ||
| 93 | constexpr std::uint16_t kRasterPixelIsPoint = 2; ///< GTRasterTypeGeoKey value. | ||
| 94 | constexpr std::uint16_t kUserDefined = 32767; ///< A user-defined CRS, datum or ellipsoid. | ||
| 95 | constexpr std::uint16_t kAngularDegree = 9102; ///< GeogAngularUnitsGeoKey value. | ||
| 96 | |||
| 97 | /// The size in bytes of one value of a TIFF field type code, or 0 for an unknown type. | ||
| 98 | std::size_t FieldSize(std::uint16_t type); | ||
| 99 | |||
| 100 | /// The compression a Compression tag value names, or nothing for a scheme this library does not | ||
| 101 | /// decode. | ||
| 102 | std::optional<TiffCompression> CompressionFromCode(std::uint64_t code); | ||
| 103 | |||
| 104 | /// The Compression tag value written for `compression`; Deflate is written as code 8. | ||
| 105 | std::uint16_t TiffCompressionCode(TiffCompression compression); | ||
| 106 | |||
| 107 | /// The sizes of IFD fields in classic TIFF or BigTIFF. | ||
| 108 | struct IfdFormat { | ||
| 109 | std::size_t countSize; ///< Size of the entry count before the entries. | ||
| 110 | std::size_t wordSize; ///< Size of a value count, an inline value and an offset. | ||
| 111 | |||
| 112 | /// The size in bytes of one entry: tag, type, count and value or offset. | ||
| 113 | 2784 | constexpr std::size_t EntrySize() const { | |
| 114 | 2784 | return 4 + 2 * wordSize; | |
| 115 | } | ||
| 116 | |||
| 117 | /// The byte offset within an entry where its value or offset starts. | ||
| 118 | 15343 | constexpr std::size_t ValueAt() const { | |
| 119 | 15343 | return 4 + wordSize; | |
| 120 | } | ||
| 121 | }; | ||
| 122 | |||
| 123 | /// The IFD format of classic TIFF (`bigTiff` false) or BigTIFF. | ||
| 124 | 932 | constexpr IfdFormat IfdLayout(bool bigTiff) { | |
| 125 | 932 | return bigTiff ? IfdFormat{8, 8} : IfdFormat{2, 4}; | |
| 126 | } | ||
| 127 | |||
| 128 | /// One IFD entry with its value bytes, already read from wherever they are stored. | ||
| 129 | 44177 | struct IfdEntry { | |
| 130 | std::uint16_t type = 0; ///< Field type (`FieldType`). | ||
| 131 | std::uint64_t count = 0; ///< Number of values. | ||
| 132 | std::vector<std::uint8_t> bytes; ///< The values, in the file's byte order. | ||
| 133 | }; | ||
| 134 | |||
| 135 | /// `transform` shifted by half a pixel along both pixel axes. | ||
| 136 | /// @param sign +1 to shift forward, -1 back. | ||
| 137 | GeoTransform ShiftHalfPixel(const GeoTransform& transform, double sign); | ||
| 138 | |||
| 139 | /// The GeoTIFF model tags that store a pixel-to-world transform: ModelPixelScale and | ||
| 140 | /// ModelTiepoint for one without rotation, otherwise ModelTransformation. Tags not stored are | ||
| 141 | /// empty. | ||
| 142 | 915 | struct ModelTags { | |
| 143 | std::vector<double> pixelScale; ///< ModelPixelScale: x and y pixel size, and z. | ||
| 144 | std::vector<double> tiepoint; ///< ModelTiepoint: one pixel (i, j, k) and world point. | ||
| 145 | std::vector<double> transformation; ///< ModelTransformation: a 4x4 matrix, row by row. | ||
| 146 | }; | ||
| 147 | |||
| 148 | /// The model tags storing `transform`. GeoTIFF places PixelIsPoint rasters at their first | ||
| 149 | /// pixel's center, so `transform` is shifted half a pixel for them. | ||
| 150 | ModelTags EncodeTransform(const GeoTransform& transform, bool pixelIsPoint); | ||
| 151 | |||
| 152 | /// The transform `tags` store, undoing the PixelIsPoint shift; nothing when they store none | ||
| 153 | /// (neither ModelTransformation nor both ModelPixelScale and ModelTiepoint). | ||
| 154 | /// @throws GeoTiffError for tags with too few values or several tiepoints. | ||
| 155 | std::optional<GeoTransform> DecodeTransform(const ModelTags& tags, bool pixelIsPoint); | ||
| 156 | |||
| 157 | /// Throws a `GeoTiffError` "<path>: <reason>" for a file read or written. | ||
| 158 | [[noreturn]] void ThrowFileError(const std::filesystem::path& path, const std::string& reason); | ||
| 159 | |||
| 160 | /// How many strips or tiles the GeoTIFF reader has decoded on any thread, for tests of windowed | ||
| 161 | /// reads. | ||
| 162 | std::size_t DecodedChunkCount(); | ||
| 163 | |||
| 164 | /// The value of the SHORT GeoKey `key` stored inline in the directory of `keys`, if present. | ||
| 165 | std::optional<std::uint16_t> ShortGeoKey(const GeoKeys& keys, std::uint16_t key); | ||
| 166 | |||
| 167 | /// The band scaling a GDAL_METADATA tag (42112) gives, one per band; scale 1 and offset 0 for | ||
| 168 | /// bands not mentioned, nor for items without a band, beyond `bands` or not a number. | ||
| 169 | std::vector<BandScaling> ParseGdalBandScaling(const std::string& xml, std::size_t bands); | ||
| 170 | |||
| 171 | } // namespace terraingen::detail | ||
| 172 |