forked from jcelaya/hdrmerge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTiffDirectory.hpp
136 lines (125 loc) · 3.94 KB
/
TiffDirectory.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
/*
* HDRMerge - HDR exposure merging software.
* Copyright 2012 Javier Celaya
*
* This file is part of HDRMerge.
*
* HDRMerge is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HDRMerge is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HDRMerge. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <vector>
#include <string>
#ifndef _TIFFDIRECTORY_HPP_
#define _TIFFDIRECTORY_HPP_
namespace hdrmerge {
struct TiffHeader {
union {
uint32_t endian;
struct {
uint16_t endian;
uint16_t magic;
} sep;
};
uint32_t offset;
// It sets the first two bytes to their correct value, given the architecture
TiffHeader() : endian(0x4D4D4949), offset(8) { sep.magic = 42; }
void write(uint8_t * buffer, size_t & pos);
};
class IFD {
public:
enum {
BYTE = 1,
ASCII,
SHORT,
LONG,
RATIONAL,
SBYTE,
UNDEFINED,
SSHORT,
SLONG,
SRATIONAL,
FLOAT,
DOUBLE
} Type;
template <typename T> void addEntry(uint16_t tag, uint16_t type, const T & value) {
DirEntry * entry = &(*entries.insert(entries.end(), DirEntry({tag, type, 1, 0})));
setValue(entry, value);
}
template <typename T> void setValue(uint16_t tag, const T * value) {
DirEntry * entry = getEntry(tag);
if (entry) {
setValue(entry, (const void *)value);
}
}
template <typename T> void setValue(uint16_t tag, const T & value) {
DirEntry * entry = getEntry(tag);
if (entry) {
setValue(entry, value);
}
}
void addEntry(uint16_t tag, uint16_t type, uint32_t count, const void * data);
void addEntry(uint16_t tag, const std::string &str) {
addEntry(tag, ASCII, str.length() + 1, str.c_str());
}
void write(uint8_t * buffer, size_t & pos, bool hasNext);
size_t length() const;
private:
struct DirEntry {
uint16_t tag;
uint16_t type;
uint32_t count;
uint32_t offset;
int bytesPerValue() const {
static int bytesPerValue[12] =
{ 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8 };
return bytesPerValue[type - 1];
}
uint32_t dataSize() const {
return count * bytesPerValue();
}
bool operator<(const DirEntry & r) const {
return tag < r.tag;
}
};
std::vector<DirEntry> entries;
std::vector<uint8_t> entryData;
DirEntry * getEntry(uint16_t tag);
void setValue(DirEntry * entry, const void * data);
template <typename T> void setValue(DirEntry * entry, const T & value) {
if (entry->dataSize() > 4) {
setValue(entry, (const void *)&value);
} else {
union {
float floatValue;
uint32_t longValue;
uint16_t shortValue;
uint8_t byteValue;
} u;
switch (entry->type) {
case BYTE: case ASCII: case SBYTE: case UNDEFINED:
u.byteValue = (uint8_t) value; break;
case SHORT: case SSHORT:
u.shortValue = (uint16_t) value; break;
case LONG: case SLONG:
u.longValue = (uint32_t)value; break;
case FLOAT:
u.floatValue = (float)value; break;
}
entry->offset = u.longValue;
}
}
};
} // namespace hdrmerge
#endif // _TIFFDIRECTORY_HPP_