# Generate TIFF file from scratch (in current byte order) # Inputs: 0) hash of IFD entries (TagID => Value; multiple values space-delimited) # 1) raw image data reference # Returns: TIFF image data, or undef on error sub GenerateTIFF($$) { my ($entries, $dataPt) = @_; my ($rtnVal, $tag, $offsetPos); my $num = scalar keys %$entries; my $ifhBuff = GetByteOrder() . Set16u(42) . Set32u(length($dataPt)+8); my $ifdBuff = Set16u($num); my $valBuff = ''; my $tagTablePtr = GetTagTable('Image::ExifTool::Exif::Main'); foreach $tag (sort { $a <=> $b } keys %$entries) { my $tagInfo = $$tagTablePtr{$tag}; my $fmt = ref $tagInfo eq 'HASH' ? $$tagInfo{Writable} : 'int32u'; return undef unless defined $fmt; my $val = Image::ExifTool::WriteValue($$entries{$tag}, $fmt, -1); return undef unless defined $val; my $format = $formatNumber{$fmt}; $ifdBuff .= Set16u($tag) . Set16u($format) . Set32u(length($val)/$formatSize[$format]); $offsetPos = length($ifdBuff) if $tag == 0x111; # (remember StripOffsets position) if (length $val > 4) { $ifdBuff .= Set32u(10 + 12 * $num + 4 + length($valBuff)); $valBuff .= $val; } else { $val .= "\0" x (4 - length($val)) if length $val < 4; $ifdBuff .= $val; } } $ifdBuff .= "\0\0\0\0"; # (no IFD1) return undef unless $offsetPos; Set32u(Set32u(length($dataPt)+42716+8), \$ifdBuff, $offsetPos); return $ifhBuff . $$dataPT . $ifdBuff . $valBuff; }