Skip to content

Commit 29e775c

Browse files
committed
little endian support
1 parent 4c8205b commit 29e775c

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

ExifBulider/ExifBuilder.h

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,35 +68,36 @@ class ExifBuilder {
6868

6969
std::vector<uint8_t> buildExifBlob() {
7070
std::vector<uint8_t> exifBlob;
71+
72+
bool bigendian = true;
7173

7274
// Placeholder for APP1 header (to be filled in later)
7375
exifBlob.insert(exifBlob.end(), { 0xFF, 0xE1, 0x00, 0x00 }); // APP1 marker, length placeholder
7476
exifBlob.insert(exifBlob.end(), { 'E', 'x', 'i', 'f', 0x00, 0x00 }); // "Exif" identifier and padding
7577

7678
// Write TIFF Header
77-
bool bigendian = true;
7879
appendUInt16(exifBlob, bigendian ? 0x4D4D : 0x4949); // Big-endian indicator
79-
appendUInt16(exifBlob, bigendian ? 0x002A : 0x2000); // TIFF version
80-
appendUInt32(exifBlob, bigendian ? 0x00000008 : 0x08000000); // Offset to the first IFD
80+
appendUInt16(exifBlob, 0x002A, bigendian); // TIFF version
81+
appendUInt32(exifBlob, 0x00000008, bigendian); // Offset to the first IFD
8182

8283
// Number of directory entries
83-
appendUInt16(exifBlob, static_cast<uint16_t>(tags.size()));
84+
appendUInt16(exifBlob, static_cast<uint16_t>(tags.size()), bigendian);
8485

8586
// Calculate data offset (just after IFD entries and next IFD offset)
8687
size_t dataOffset = 8 + 2 + (tags.size() * 12) + 4;
8788

8889
// Process each tag
8990
for (auto& tag : tags) {
90-
appendUInt16(exifBlob, tag.tag);
91-
appendUInt16(exifBlob, tag.type);
92-
appendUInt32(exifBlob, tag.count);
91+
appendUInt16(exifBlob, tag.tag, bigendian);
92+
appendUInt16(exifBlob, tag.type, bigendian);
93+
appendUInt32(exifBlob, tag.count, bigendian);
9394

9495
if (tagFitsInField(tag)) {
95-
writeTagValue(exifBlob, tag); // Write values directly as is
96+
writeTagValue(exifBlob, tag, bigendian); // Write values directly as is
9697
}
9798
else {
98-
appendUInt32(exifBlob, static_cast<uint32_t>(dataOffset));
99-
appendExtraData(tag, dataOffset);
99+
appendUInt32(exifBlob, static_cast<uint32_t>(dataOffset), bigendian);
100+
appendExtraData(tag, dataOffset, bigendian);
100101
}
101102
}
102103

@@ -184,10 +185,18 @@ class ExifBuilder {
184185
return false;
185186
}
186187

187-
void appendExtraData(const ExifTag& tag, size_t& dataOffset) {
188-
const auto& data = tag.value;
189-
extraData.insert(extraData.end(), data.begin(), data.end());
190-
dataOffset += data.size();
188+
void appendExtraData(const ExifTag& tag, size_t& dataOffset, bool bigendian) {
189+
const auto& data = tag.value;
190+
if (bigendian || tag.type == 0x0002) {
191+
extraData.insert(extraData.end(), data.begin(), data.end());
192+
dataOffset += data.size();
193+
}
194+
else {
195+
for (size_t i = data.size(); i > 0; --i) {
196+
extraData.push_back(data[i - 1]);
197+
}
198+
dataOffset += data.size();
199+
}
191200
// add a padding 0 byte.
192201
if (data.size() % 2 != 0) {
193202
extraData.push_back(0);

0 commit comments

Comments
 (0)