Browse Source

exif ifd writing fixes

pull/1923/head
Ildar Khayrutdinov 5 years ago
parent
commit
2216980c2f
  1. 26
      src/ImageSharp/Metadata/Profiles/Exif/ExifWriter.cs

26
src/ImageSharp/Metadata/Profiles/Exif/ExifWriter.cs

@ -46,7 +46,6 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif
public byte[] GetData() public byte[] GetData()
{ {
const uint startIndex = 0; const uint startIndex = 0;
uint length;
IExifValue exifOffset = GetOffsetValue(this.ifdValues, this.exifValues, ExifTag.SubIFDOffset); IExifValue exifOffset = GetOffsetValue(this.ifdValues, this.exifValues, ExifTag.SubIFDOffset);
IExifValue gpsOffset = GetOffsetValue(this.ifdValues, this.gpsValues, ExifTag.GPSIFDOffset); IExifValue gpsOffset = GetOffsetValue(this.ifdValues, this.gpsValues, ExifTag.GPSIFDOffset);
@ -56,13 +55,13 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif
return Array.Empty<byte>(); return Array.Empty<byte>();
} }
uint ifdLength = this.GetLength(this.ifdValues) + 4U; uint ifdLength = this.GetLength(this.ifdValues);
uint exifLength = this.GetLength(this.exifValues); uint exifLength = this.GetLength(this.exifValues);
uint gpsLength = this.GetLength(this.gpsValues); uint gpsLength = this.GetLength(this.gpsValues);
length = ifdLength + exifLength + gpsLength; uint length = ifdLength + exifLength + gpsLength;
if (length == 4U) if (length == 0)
{ {
return Array.Empty<byte>(); return Array.Empty<byte>();
} }
@ -70,9 +69,10 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif
// two bytes for the byte Order marker 'II' or 'MM', followed by the number 42 (0x2A) and a 0, making 4 bytes total // two bytes for the byte Order marker 'II' or 'MM', followed by the number 42 (0x2A) and a 0, making 4 bytes total
length += (uint)ExifConstants.LittleEndianByteOrderMarker.Length; length += (uint)ExifConstants.LittleEndianByteOrderMarker.Length;
length += 4 + 4; // first IFD offset
length += 4;
var result = new byte[length]; byte[] result = new byte[length];
int i = 0; int i = 0;
@ -80,15 +80,13 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif
ExifConstants.LittleEndianByteOrderMarker.CopyTo(result.AsSpan(start: i)); ExifConstants.LittleEndianByteOrderMarker.CopyTo(result.AsSpan(start: i));
i += ExifConstants.LittleEndianByteOrderMarker.Length; i += ExifConstants.LittleEndianByteOrderMarker.Length;
uint ifdOffset = ((uint)i - startIndex) + 4U; uint ifdOffset = (uint)i - startIndex + 4U;
uint thumbnailOffset = ifdOffset + ifdLength + exifLength + gpsLength;
exifOffset?.TrySetValue(ifdOffset + ifdLength); exifOffset?.TrySetValue(ifdOffset + ifdLength);
gpsOffset?.TrySetValue(ifdOffset + ifdLength + exifLength); gpsOffset?.TrySetValue(ifdOffset + ifdLength + exifLength);
i = WriteUInt32(ifdOffset, result, i); i = WriteUInt32(ifdOffset, result, i);
i = this.WriteHeaders(this.ifdValues, result, i); i = this.WriteHeaders(this.ifdValues, result, i);
i = WriteUInt32(thumbnailOffset, result, i);
i = this.WriteData(startIndex, this.ifdValues, result, i); i = this.WriteData(startIndex, this.ifdValues, result, i);
if (exifLength > 0) if (exifLength > 0)
@ -103,8 +101,6 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif
i = this.WriteData(startIndex, this.gpsValues, result, i); i = this.WriteData(startIndex, this.gpsValues, result, i);
} }
WriteUInt32(0, result, i);
return result; return result;
} }
@ -263,7 +259,7 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif
{ {
uint valueLength = GetLength(value); uint valueLength = GetLength(value);
length += 2 + 2 + 4 + 4; length += 12;
if (valueLength > 4) if (valueLength > 4)
{ {
@ -271,6 +267,9 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif
} }
} }
// next IFD offset
length += 4;
return length; return length;
} }
@ -361,6 +360,9 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif
newOffset += 4; newOffset += 4;
} }
// next IFD offset
newOffset = WriteUInt32(0, destination, newOffset);
return newOffset; return newOffset;
} }

Loading…
Cancel
Save