forked from libertiff/libertiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibertiff.hpp
1346 lines (1209 loc) · 40.8 KB
/
libertiff.hpp
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: MIT
// Copyright 2024, Even Rouault <even.rouault at spatialys.com>
// Canonical URL: https://github.com/libertiff/libertiff/blob/master/libertiff.hpp
#ifndef LIBERTIFF_HPP_INCLUDED
#define LIBERTIFF_HPP_INCLUDED
//////////////////////////////////////////////////////////////
// libertiff = libre TIFF or LIB E(ven) R(ouault) TIFF... ? //
//////////////////////////////////////////////////////////////
#if __cplusplus >= 202002L
#include <bit> // std::endian
#endif
#include <array>
#include <cassert>
#include <cstring>
#include <limits>
#include <memory>
#include <set>
#include <string>
#include <type_traits>
#include <vector>
#ifndef LIBERTIFF_NS
#define LIBERTIFF_NS libertiff
#endif
/** Libertiff is a C++11 simple, header-only, TIFF reader. It is MIT licensed.
*
* Handles both ClassicTIFF and BigTIFF, little-endian or big-endian ordered.
*
* The library does not (yet?) offer codec facilities. It is mostly aimed at
* browsing through the linked chain of Image File Descriptors and their tags.
*
* The library is thread-safe (that is the instances that it returns can
* be used from multiple threads), if passed FileReader instances are themselves
* thread-safe.
*
* The library does not throw exceptions (but underlying std library might
* throw exceptions in case of out-of-memory when allocating the tag array)
*
* Optional features:
* - define LIBERTIFF_C_FILE_READER before including that file, so that
* the libertiff::CFileReader class is available
*/
namespace LIBERTIFF_NS
{
#if __cplusplus >= 201703L
#define LIBERTIFF_STATIC_ASSERT(x) static_assert(x)
#define LIBERTIFF_CONSTEXPR constexpr
#else
#define LIBERTIFF_STATIC_ASSERT(x) static_assert((x), #x)
#define LIBERTIFF_CONSTEXPR
#endif
template <typename T, typename... Args>
std::unique_ptr<T> make_unique(Args &&...args)
{
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
/** Returns whether the host is little-endian ordered */
inline bool isHostLittleEndian()
{
#if __cplusplus >= 202002L
return std::endian::native == std::endian::little;
#elif (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)) || \
defined(_MSC_VER)
return true;
#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
return false;
#else
uint32_t one = 1;
char one_as_char_array[sizeof(uint32_t)];
std::memcpy(one_as_char_array, &one, sizeof(uint32_t));
return one_as_char_array[0] == 1;
#endif
}
/** Byte-swap */
template <class T> inline T byteSwap(T v);
/** Byte-swap a uint8_t */
template <> uint8_t byteSwap(uint8_t v)
{
return v;
}
/** Byte-swap a int8_t */
template <> int8_t byteSwap(int8_t v)
{
return v;
}
/** Byte-swap a uint16_t */
template <> uint16_t byteSwap(uint16_t v)
{
return uint16_t((v >> 8) | ((v & 0xff) << 8));
}
/** Byte-swap a int16_t */
template <> int16_t byteSwap(int16_t v)
{
uint16_t u;
LIBERTIFF_STATIC_ASSERT(sizeof(v) == sizeof(u));
std::memcpy(&u, &v, sizeof(u));
u = byteSwap(u);
std::memcpy(&v, &u, sizeof(u));
return v;
}
/** Byte-swap a uint32_t */
template <> uint32_t byteSwap(uint32_t v)
{
return (v >> 24) | (((v >> 16) & 0xff) << 8) | (((v >> 8) & 0xff) << 16) |
((v & 0xff) << 24);
}
/** Byte-swap a int32_t */
template <> int32_t byteSwap(int32_t v)
{
uint32_t u;
LIBERTIFF_STATIC_ASSERT(sizeof(v) == sizeof(u));
std::memcpy(&u, &v, sizeof(u));
u = byteSwap(u);
std::memcpy(&v, &u, sizeof(u));
return v;
}
/** Byte-swap a uint64_t */
template <> uint64_t byteSwap(uint64_t v)
{
return (uint64_t(byteSwap(uint32_t(v & ~(0U)))) << 32) |
byteSwap(uint32_t(v >> 32));
}
/** Byte-swap a int64_t */
template <> int64_t byteSwap(int64_t v)
{
uint64_t u;
std::memcpy(&u, &v, sizeof(u));
u = byteSwap(u);
std::memcpy(&v, &u, sizeof(u));
return v;
}
/** Byte-swap a float */
template <> float byteSwap(float v)
{
uint32_t u;
LIBERTIFF_STATIC_ASSERT(sizeof(v) == sizeof(u));
std::memcpy(&u, &v, sizeof(u));
u = byteSwap(u);
std::memcpy(&v, &u, sizeof(u));
return v;
}
/** Byte-swap a double */
template <> double byteSwap(double v)
{
uint64_t u;
LIBERTIFF_STATIC_ASSERT(sizeof(v) == sizeof(u));
std::memcpy(&u, &v, sizeof(u));
u = byteSwap(u);
std::memcpy(&v, &u, sizeof(u));
return v;
}
} // namespace LIBERTIFF_NS
namespace LIBERTIFF_NS
{
/** Interface to read from a file. */
class FileReader
{
public:
virtual ~FileReader() = default;
/** Return file size in bytes */
virtual uint64_t size() const = 0;
/** Read 'count' bytes from offset 'offset' into 'buffer' and
* return the number of bytes actually read.
*/
virtual size_t read(uint64_t offset, size_t count, void *buffer) const = 0;
};
} // namespace LIBERTIFF_NS
namespace LIBERTIFF_NS
{
/** Read context: associates a file, and the byte ordering of the TIFF file */
class ReadContext
{
public:
/** Constructor */
ReadContext(const std::shared_ptr<const FileReader> &file,
bool mustByteSwap)
: m_file(file), m_mustByteSwap(mustByteSwap)
{
}
/** Return if values of more than 1-byte must be byte swapped.
* To be only taken into account when reading pixels. Tag values are
* automatically byte-swapped */
inline bool mustByteSwap() const
{
return m_mustByteSwap;
}
/** Return file size */
inline uint64_t size() const
{
return m_file->size();
}
/** Read count raw bytes at offset into buffer */
void read(uint64_t offset, size_t count, void *buffer, bool &ok) const
{
if (m_file->read(offset, count, buffer) != count)
ok = false;
}
/** Read single value at offset */
template <class T> T read(uint64_t offset, bool &ok) const
{
#if __cplusplus >= 201703L
static_assert(
std::is_same_v<T, uint8_t> || std::is_same_v<T, int8_t> ||
std::is_same_v<T, uint16_t> || std::is_same_v<T, int16_t> ||
std::is_same_v<T, uint32_t> || std::is_same_v<T, int32_t> ||
std::is_same_v<T, uint64_t> || std::is_same_v<T, int64_t> ||
std::is_same_v<T, float> || std::is_same_v<T, double>);
#endif
T res = 0;
if (m_file->read(offset, sizeof(res), &res) != sizeof(res))
{
ok = false;
return 0;
}
if LIBERTIFF_CONSTEXPR (sizeof(T) > 1)
{
if (m_mustByteSwap)
res = byteSwap(res);
}
return res;
}
/** Read a unsigned rational (type == Type::Rational) */
template <class T = uint32_t>
double readRational(uint64_t offset, bool &ok) const
{
const auto numerator = read<T>(offset, ok);
const auto denominator = read<T>(offset + sizeof(T), ok);
if (denominator == 0)
{
ok = false;
return std::numeric_limits<double>::quiet_NaN();
}
return double(numerator) / denominator;
}
/** Read a signed rational (type == Type::SRational) */
double readSignedRational(uint64_t offset, bool &ok) const
{
return readRational<int32_t>(offset, ok);
}
/** Read length bytes at offset (typically for ASCII tag) as a string */
std::string readString(std::string &res, uint64_t offset, size_t length,
bool &ok) const
{
res.resize(length);
if (length > 0 && m_file->read(offset, length, &res[0]) != length)
{
ok = false;
res.clear();
return res;
}
// Strip trailing nul byte if found
if (length > 0 && res.back() == 0)
res.pop_back();
return res;
}
/** Read length bytes at offset (typically for ASCII tag) as a string */
std::string readString(uint64_t offset, size_t length, bool &ok) const
{
std::string res;
readString(res, offset, length, ok);
return res;
}
/** Read an array of count values starting at offset */
template <class T>
void readArray(std::vector<T> &array, uint64_t offset, size_t count,
bool &ok) const
{
#if __cplusplus >= 201703L
static_assert(
std::is_same_v<T, uint8_t> || std::is_same_v<T, int8_t> ||
std::is_same_v<T, uint16_t> || std::is_same_v<T, int16_t> ||
std::is_same_v<T, uint32_t> || std::is_same_v<T, int32_t> ||
std::is_same_v<T, uint64_t> || std::is_same_v<T, int64_t> ||
std::is_same_v<T, float> || std::is_same_v<T, double>);
#endif
array.resize(count);
const size_t countBytes = count * sizeof(T);
if (count > 0 &&
m_file->read(offset, countBytes, &array[0]) != countBytes)
{
ok = false;
array.clear();
}
if LIBERTIFF_CONSTEXPR (sizeof(T) > 1)
{
if (m_mustByteSwap)
{
if LIBERTIFF_CONSTEXPR (std::is_same<T, float>::value)
{
uint32_t *uint32Array =
reinterpret_cast<uint32_t *>(array.data());
for (size_t i = 0; i < count; ++i)
{
uint32Array[i] = byteSwap(uint32Array[i]);
}
}
else if LIBERTIFF_CONSTEXPR (std::is_same<T, double>::value)
{
uint64_t *uint64Array =
reinterpret_cast<uint64_t *>(array.data());
for (size_t i = 0; i < count; ++i)
{
uint64Array[i] = byteSwap(uint64Array[i]);
}
}
else
{
for (size_t i = 0; i < count; ++i)
{
array[i] = byteSwap(array[i]);
}
}
}
}
}
/** Read an array of count values starting at offset */
template <class T>
std::vector<T> readArray(uint64_t offset, size_t count, bool &ok) const
{
std::vector<T> array;
readArray(array, offset, count, ok);
return array;
}
private:
const std::shared_ptr<const FileReader> m_file;
const bool m_mustByteSwap;
};
} // namespace LIBERTIFF_NS
namespace LIBERTIFF_NS
{
/** TIFF tag codes */
enum class Tag
{
NewSubfileType = 254,
SubfileType = 255,
// Base line and extended TIFF tags
ImageWidth = 256,
ImageLength = 257,
BitsPerSample = 258,
Compression = 259,
PhotometricInterpretation = 262,
ImageDescription = 270,
StripOffsets = 273,
SamplesPerPixel = 277,
RowsPerStrip = 278,
StripByteCounts = 279,
PlanarConfiguration = 284,
Predictor = 317,
TileWidth = 322,
TileLength = 323,
TileOffsets = 324,
TileByteCounts = 325,
SampleFormat = 339,
// GeoTIFF tags
GeoTIFFPixelScale = 33550,
GeoTIFFTiePoints = 33922,
GeoTIFFGeoKeyDirectory = 34735,
};
/** TIFF data types */
enum class Type
{
Byte = 1, /*! Unsigned 8-bit integer */
ASCII = 2, /*! Character */
Short = 3, /*! Unsigned 16-bit integer */
Long = 4, /*! Unsigned 32-bit integer */
Rational =
5, /*! Positive number as a ratio of two unsigned 32-bit integers */
SByte = 6, /*! Signed 8-bit integer */
Undefined = 7, /*! Untyped 8-bit data */
SShort = 8, /*! Signed 16-bit integer */
SLong = 9, /*! Signed 32-bit integer */
SRational =
10, /*! Signed number as a ratio of two signed 32-bit integers */
Float = 11, /*! 32-bit IEEE-754 floating point number */
Double = 12, /*! 64-bit IEEE-754 floating point number */
// BigTIFF additions
Long8 = 16, /*! Unsigned 64-bit integer */
SLong8 = 17, /*! Signed 64-bit integer */
IFD8 = 18, /*! Unsigned 64-bit IFD offset */
};
/** Values of the PlanarConfiguration tag */
enum class PlanarConfiguration
{
Contiguous = 1, /*! Single image plane */
Separate = 2, /*! Separate planes per sample */
};
/** Values of the PhotometricInterpretation tag */
enum class PhotometricInterpretation
{
MinIsWhite = 0,
MinIsBlack = 1,
RGB = 2,
Palette = 3,
Mask = 4,
Separated = 5,
YCbCr = 6,
Reserved_7 = 7,
CIELab = 8,
ICCLab = 9,
ITULab = 10,
// NOTE: if adding new values, edit processTag()
// CFA = 32803,
// Log2L = 32844,
// Log2LUV = 32845,
};
/** Compression methods */
enum class Compression
{
Unknown = 0, //! libertiff special marker
None = 1,
CCITT_RLE = 2,
CCITT_FAX3 = 3,
CCITT_FAX4 = 4,
LZW = 5,
OldJPEG = 6,
JPEG = 7,
Deflate = 8,
// NOTE: if adding new values, edit processTag()
};
/** Sample format */
enum class SampleFormat
{
UnsignedInt = 1,
SignedInt = 2,
IEEEFP = 3,
Void = 4,
ComplexInt = 5,
ComplexIEEEFP = 6
};
/** Content of a tag entry in a Image File Directory (IFD) */
struct TagEntry
{
uint16_t tag = 0; // of type Tag when recognized
uint16_t type = 0; // of type Type when recognized
uint64_t count = 0; // number of values in the tag
// Inline values. Only valid if value_offset == 0.
// The actual number in the arrays is count
union
{
std::array<char, 8> charValues;
std::array<uint8_t, 8> uint8Values;
std::array<int8_t, 8> int8Values;
std::array<uint16_t, 4> uint16Values;
std::array<int16_t, 4> int16Values;
std::array<uint32_t, 2> uint32Values;
std::array<int32_t, 2> int32Values;
std::array<float, 2> float32Values;
std::array<double, 1>
float64Values; // Valid for Double, Rational, SRational
std::array<uint64_t, 1> uint64Values = {0};
std::array<int64_t, 1> int64Values;
};
uint64_t value_offset = 0; // 0 for inline values
bool invalid_value_offset = true; // whether value_offset is invalid
};
// clang-format off
/** Return the size in bytes of a data type */
static uint32_t getSize(Type type)
{
switch (type)
{
case Type::Byte: return 1;
case Type::ASCII: return 1;
case Type::Short: return 2;
case Type::Long: return 4;
case Type::Rational: return 8; // 2 Long
case Type::SByte: return 1;
case Type::Undefined: break;
case Type::SShort: return 2;
case Type::SLong: return 4;
case Type::SRational: return 8; // 2 SLong
case Type::Float: return 4;
case Type::Double: return 8;
case Type::Long8: return 8;
case Type::SLong8: return 8;
case Type::IFD8: return 8;
}
return 1;
}
// clang-format on
/** Return the size in bytes of a data type, or 0 if unknown */
static uint32_t getSize(uint16_t type)
{
if (type >= static_cast<uint16_t>(Type::Byte) &&
type <= static_cast<uint16_t>(Type::Double))
{
return getSize(static_cast<Type>(type));
}
if (type >= static_cast<uint16_t>(Type::Long8) &&
type <= static_cast<uint16_t>(Type::IFD8))
{
return 8;
}
return 0;
}
/** Represents a TIFF Image File Directory (IFD). */
class Image
{
public:
/** Constructor. Should not be called directly. Use the open() method */
Image(const std::shared_ptr<const ReadContext> &rc, bool isBigTIFF)
: m_rc(rc), m_isBigTIFF(isBigTIFF)
{
}
/** Return read context */
const std::shared_ptr<const ReadContext> &readContext() const
{
return m_rc;
}
/** Return whether the file is BigTIFF (if false, classic TIFF) */
inline bool isBigTIFF() const
{
return m_isBigTIFF;
}
/** Return if values of more than 1-byte must be byte swapped.
* To be only taken into account when reading pixels. Tag values are
* automatically byte-swapped */
inline bool mustByteSwap() const
{
return m_rc->mustByteSwap();
}
/** Return the offset of the next IFD (to pass to Image::open()),
* or 0 if there is no more */
inline uint64_t nextImageOffset() const
{
return m_nextImageOffset;
}
/** Return width of the image in pixels */
inline uint32_t width() const
{
return m_width;
}
/** Return height of the image in pixels */
inline uint32_t height() const
{
return m_height;
}
/** Return number of bits per sample */
inline uint32_t bitsPerSample() const
{
return m_bitsPerSample;
}
/** Return number of samples (a.k.a. channels, bands) per pixel */
inline uint32_t samplesPerPixel() const
{
return m_samplesPerPixel;
}
/** Return planar configuration */
inline PlanarConfiguration planarConfiguration() const
{
return m_planarConfiguration;
}
/** Return planar configuration */
inline PhotometricInterpretation photometricInterpretation() const
{
return m_photometricInterpretation;
}
/** Return compression method used */
inline Compression compression() const
{
return m_compression;
}
/** Return predictor value (used for Deflate, LZW, ZStd, etc. compression) */
inline uint32_t predictor() const
{
return m_predictor;
}
/** Return sample format */
inline SampleFormat sampleFormat() const
{
return m_sampleFormat;
}
/** Return the number of rows per strip */
inline uint32_t rowsPerStrip() const
{
return m_rowsPerStrip;
}
/** Return the number of strips/tiles.
* Return 0 if inconsistent values between ByteCounts and Offsets arrays. */
inline uint64_t strileCount() const
{
return m_strileCount;
}
/** Return whether image is tiled */
inline bool isTiled() const
{
return m_isTiled;
}
/** Return tile width */
inline uint32_t tileWidth() const
{
return m_tileWidth;
}
/** Return tile width */
inline uint32_t tileHeight() const
{
return m_tileHeight;
}
/** Return number of tiles per row */
uint32_t tilesPerRow() const
{
if (m_tileWidth > 0)
{
return uint32_t((uint64_t(m_width) + m_tileWidth - 1) /
m_tileWidth);
}
return 0;
}
/** Return number of tiles per column */
uint32_t tilesPerCol() const
{
if (m_tileHeight > 0)
{
return uint32_t((uint64_t(m_height) + m_tileHeight - 1) /
m_tileHeight);
}
return 0;
}
/** Convert a tile coordinate (xtile, ytile, bandIdx) to a flat index */
uint64_t tileCoordinateToIdx(uint32_t xtile, uint32_t ytile,
uint32_t bandIdx, bool &ok) const
{
if (m_isTiled && m_tileWidth > 0 && m_tileHeight > 0)
{
const auto lTilesPerRow = tilesPerRow();
const auto lTilesPerCol = tilesPerCol();
if (xtile >= lTilesPerRow || ytile >= lTilesPerCol)
{
ok = false;
return 0;
}
auto idx = uint64_t(ytile) * lTilesPerRow + xtile;
if (bandIdx &&
m_planarConfiguration == PlanarConfiguration::Separate)
{
idx += uint64_t(bandIdx) * lTilesPerCol * lTilesPerRow;
}
return idx;
}
ok = false;
return 0;
}
/** Return the offset of strip/tile of index idx */
uint64_t strileOffset(uint64_t idx, bool &ok) const
{
return readUIntTag(m_strileOffsetsTag, idx, ok);
}
/** Return the offset of a tile from its coordinates */
uint64_t tileOffset(uint32_t xtile, uint32_t ytile, uint32_t bandIdx,
bool &ok) const
{
const auto idx = tileCoordinateToIdx(xtile, ytile, bandIdx, ok);
return ok ? strileOffset(idx, ok) : 0;
}
/** Return the byte count of strip/tile of index idx */
uint64_t strileByteCount(uint64_t idx, bool &ok) const
{
return readUIntTag(m_strileByteCountsTag, idx, ok);
}
/** Return the offset of a tile from its coordinates */
uint64_t tileByteCount(uint32_t xtile, uint32_t ytile, uint32_t bandIdx,
bool &ok) const
{
const auto idx = tileCoordinateToIdx(xtile, ytile, bandIdx, ok);
return ok ? strileByteCount(idx, ok) : 0;
}
/** Return the list of tags */
inline const std::vector<TagEntry> &tags() const
{
return m_tags;
}
/** Return the (first) tag corresponding to a code, or nullptr if not found */
const TagEntry *tag(uint16_t tagCode) const
{
for (const auto &tag : m_tags)
{
if (tag.tag == tagCode)
return &tag;
}
return nullptr;
}
/** Return the (first) tag corresponding to a code, or nullptr if not found */
const TagEntry *tag(Tag tagCode) const
{
return tag(static_cast<uint16_t>(tagCode));
}
/** Read an ASCII tag as a string */
std::string readTagAsString(const TagEntry &tag, bool &ok) const
{
if (tag.type == static_cast<uint16_t>(Type::ASCII))
{
if (tag.value_offset)
{
return readContext()->readString(tag.value_offset, tag.count,
ok);
}
if (tag.count)
{
std::string res;
res.resize(static_cast<size_t>(tag.count));
std::memcpy(&res[0], tag.charValues.data(), res.size());
if (res.back() == 0)
res.pop_back();
return res;
}
}
ok = false;
return std::string();
}
/** Read a numeric tag as a vector */
template <class T>
std::vector<T> readTagAsVector(const TagEntry &tag, bool &ok) const
{
if (tag.value_offset)
{
return readContext()->readArray<T>(tag.value_offset, tag.count, ok);
}
if LIBERTIFF_CONSTEXPR (std::is_same<T, uint16_t>::value)
{
if (tag.type == static_cast<uint16_t>(Type::Short))
{
std::vector<T> v;
v.insert(v.end(), tag.uint16Values.data(),
tag.uint16Values.data() +
static_cast<size_t>(tag.count));
return v;
}
}
else if LIBERTIFF_CONSTEXPR (std::is_same<T, uint32_t>::value)
{
if (tag.type == static_cast<uint16_t>(Type::Long))
{
std::vector<T> v;
v.insert(v.end(), tag.uint32Values.data(),
tag.uint32Values.data() +
static_cast<size_t>(tag.count));
return v;
}
}
ok = false;
return {};
}
/** Returns a new Image instance for the IFD starting at offset imageOffset */
template <bool isBigTIFF>
static std::unique_ptr<const Image>
open(const std::shared_ptr<const ReadContext> &rc,
const uint64_t imageOffset,
const std::set<uint64_t> &alreadyVisitedImageOffsets =
std::set<uint64_t>())
{
// To prevent infinite looping on corrupted files
if (imageOffset == 0 || alreadyVisitedImageOffsets.find(imageOffset) !=
alreadyVisitedImageOffsets.end())
{
return nullptr;
}
auto image = LIBERTIFF_NS::make_unique<Image>(rc, isBigTIFF);
image->m_alreadyVisitedImageOffsets = alreadyVisitedImageOffsets;
image->m_alreadyVisitedImageOffsets.insert(imageOffset);
bool ok = true;
int tagCount = 0;
uint64_t offset = imageOffset;
if LIBERTIFF_CONSTEXPR (isBigTIFF)
{
const auto tagCount64Bit = rc->read<uint64_t>(offset, ok);
// Artificially limit to the same number of entries as ClassicTIFF
if (tagCount64Bit > std::numeric_limits<uint16_t>::max())
return nullptr;
tagCount = static_cast<int>(tagCount64Bit);
offset += sizeof(uint64_t);
}
else
{
tagCount = rc->read<uint16_t>(offset, ok);
offset += sizeof(uint16_t);
}
if (!ok)
return nullptr;
image->m_tags.reserve(tagCount);
for (int i = 0; i < tagCount; ++i)
{
TagEntry entry;
// Read tag code
entry.tag = rc->read<uint16_t>(offset, ok);
offset += sizeof(uint16_t);
// Read tag data type
entry.type = rc->read<uint16_t>(offset, ok);
offset += sizeof(uint16_t);
// Read number of values
if LIBERTIFF_CONSTEXPR (isBigTIFF)
{
auto count = rc->read<uint64_t>(offset, ok);
entry.count = count;
offset += sizeof(count);
}
else
{
auto count = rc->read<uint32_t>(offset, ok);
entry.count = count;
offset += sizeof(count);
}
uint32_t singleValue = 0;
bool singleValueFitsInUInt32 = false;
if (entry.count)
{
if LIBERTIFF_CONSTEXPR (isBigTIFF)
{
image->ParseTagEntryDataOrOffset<uint64_t>(
entry, offset, singleValueFitsInUInt32, singleValue,
ok);
}
else
{
image->ParseTagEntryDataOrOffset<uint32_t>(
entry, offset, singleValueFitsInUInt32, singleValue,
ok);
}
}
if (!ok)
return nullptr;
image->processTag(entry, singleValueFitsInUInt32, singleValue);
image->m_tags.push_back(entry);
}
image->finalTagProcessing();
if LIBERTIFF_CONSTEXPR (isBigTIFF)
image->m_nextImageOffset = rc->read<uint64_t>(offset, ok);
else
image->m_nextImageOffset = rc->read<uint32_t>(offset, ok);
image->m_openFunc = open<isBigTIFF>;
return std::unique_ptr<const Image>(image.release());
}
/** Returns a new Image instance at the next IFD, or nullptr if there is none */
std::unique_ptr<const Image> next() const
{
return m_openFunc(m_rc, m_nextImageOffset,
m_alreadyVisitedImageOffsets);
}
private:
const std::shared_ptr<const ReadContext> m_rc;
std::unique_ptr<const Image> (*m_openFunc)(
const std::shared_ptr<const ReadContext> &, const uint64_t,
const std::set<uint64_t> &) = nullptr;
std::set<uint64_t> m_alreadyVisitedImageOffsets{};
uint64_t m_nextImageOffset = 0;
uint32_t m_width = 0;
uint32_t m_height = 0;
uint32_t m_bitsPerSample = 0;
uint32_t m_samplesPerPixel = 0;
uint32_t m_rowsPerStrip = 0;
Compression m_compression = Compression::None;
SampleFormat m_sampleFormat = SampleFormat::UnsignedInt;
PlanarConfiguration m_planarConfiguration = PlanarConfiguration::Contiguous;
PhotometricInterpretation m_photometricInterpretation =
PhotometricInterpretation::MinIsBlack;
uint32_t m_predictor = 0;
const bool m_isBigTIFF;
bool m_isTiled = false;
uint32_t m_tileWidth = 0;
uint32_t m_tileHeight = 0;
uint64_t m_strileCount = 0;
std::vector<TagEntry> m_tags{};
const TagEntry *m_strileOffsetsTag = nullptr;
const TagEntry *m_strileByteCountsTag = nullptr;
Image(const Image &) = delete;
Image &operator=(const Image &) = delete;
/** Process tag */
void processTag(const TagEntry &entry, bool singleValueFitsInUInt32,
uint32_t singleValue)
{
if (singleValueFitsInUInt32)
{
switch (entry.tag)
{
case static_cast<uint16_t>(Tag::ImageWidth):
m_width = singleValue;
break;
case static_cast<uint16_t>(Tag::ImageLength):
m_height = singleValue;
break;
case static_cast<uint16_t>(Tag::Compression):
{
if (singleValue >=
static_cast<uint32_t>(Compression::None) &&
singleValue <=
static_cast<uint32_t>(Compression::Deflate))
{
m_compression = static_cast<Compression>(singleValue);
}
else
m_compression = Compression::Unknown;
break;