Skip to content

Commit

Permalink
Update TiffWriter to properly order the tiff tags in ascending order.
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Jackson <[email protected]>
  • Loading branch information
imikejackson committed Sep 25, 2024
1 parent 14066a2 commit 068b0c2
Showing 1 changed file with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,29 @@ std::pair<int32_t, std::string> TiffWriter::WriteImage(const std::string& filepa
// Generate the offset into the Image File Directory (ifd) which we are going to write first
constexpr uint32_t ifdOffset = 8;
outputFile.write(reinterpret_cast<const char*>(&ifdOffset), sizeof(ifdOffset));

const int k_NumTags = 12;
std::vector<TIFTAG> tags;
tags.push_back(TIFTAG{0x00FE, 0x0004, 1, 0x00000000}); // NewSubfileType
tags.push_back(TIFTAG{0x0100, 0x0004, 1, width}); // ImageWidth
tags.push_back(TIFTAG{0x0101, 0x0004, 1, height}); // ImageLength
tags.push_back(TIFTAG{0x0102, 0x0003, 1, 8 * sizeof(char)}); // BitsPerSample
tags.push_back(TIFTAG{0x0103, 0x0003, 1, 0x0001}); // Compression
tags.push_back(TIFTAG{0x0106, 0x0003, 1, photometricInterpretation}); // PhotometricInterpretation // For SamplesPerPixel = 3 or 4 (RGB or RGBA)
tags.push_back(TIFTAG{0x00FE, 0x0004, 1, 0x00000000}); // NewSubfileType
tags.push_back(TIFTAG{0x0100, 0x0004, 1, width}); // ImageWidth
tags.push_back(TIFTAG{0x0101, 0x0004, 1, height}); // ImageLength
tags.push_back(TIFTAG{0x0102, 0x0003, 1, 8 * sizeof(char)}); // BitsPerSample
tags.push_back(TIFTAG{0x0103, 0x0003, 1, 0x0001}); // Compression
tags.push_back(TIFTAG{0x0106, 0x0003, 1, photometricInterpretation}); // PhotometricInterpretation // For SamplesPerPixel = 3 or 4 (RGB or RGBA)
// Now compute the offset to the image data so that we can put that into the tag.
// The math on this ONLY Works if we have 11 total Tags.
// IF YOU ADD MORE TAGS, YOU NEED TO ADJUST THE NEXT LINE OF CODE
int32_t imageDataOffset = static_cast<int32_t>(8 + (k_NumTags * 12) + 6); // Header + tags + IDF Tag entry count and Next IFD Offset
tags.push_back(TIFTAG{0x0111, 0x0004, 1, imageDataOffset}); // StripOffsets

tags.push_back(TIFTAG{0x0112, 0x0003, 1, 1}); // Orientation
tags.push_back(TIFTAG{0x0115, 0x0003, 1, samplesPerPixel}); // SamplesPerPixel
tags.push_back(TIFTAG{0x0116, 0x0004, 1, height}); // RowsPerStrip
tags.push_back(TIFTAG{0x0117, 0x0004, 1, width * height * samplesPerPixel}); // StripByteCounts

// TIFTAG XResolution;
// TIFTAG YResolution;
// TIFTAG ResolutionUnit;
tags.push_back(TIFTAG{0x011c, 0x0003, 1, 0x0001}); // PlanarConfiguration

// Now compute the offset to the image data so that we can put that into the tag.
// THESE NEXT 2 LINES MUST BE THE LAST TAG TO BE PUSHED BACK INTO THE VECTOR OR THE MATH WILL BE WRONG
const int32_t imageDataOffset = static_cast<int32_t>(8 + ((tags.size() + 1) * 12) + 6); // Header + tags + IDF Tag entry count and Next IFD Offset
tags.push_back(TIFTAG{0x0111, 0x0004, 1, imageDataOffset}); // StripOffsets
tags.push_back(TIFTAG{0x011c, 0x0003, 1, 0x0001}); // PlanarConfiguration // 284

// Write the number of tags to the IFD section
uint16_t numEntries = static_cast<uint16_t>(tags.size());
Expand Down

0 comments on commit 068b0c2

Please sign in to comment.