/// <summary> /// Parse IFD section of Jpeg's header. /// </summary> /// <param name="App1Data">Raw data of App1 section</param> /// <param name="IfdOffset">Offset to the target IFD section from start of App1 data.</param> /// <param name="IfdSectionEndian">Alignment of all sections of this IFD data. This value is contained in TIFF header.</param> /// <returns>All entries in given IFD section.</returns> public static IfdData ParseIfd(byte[] App1Data, UInt32 IfdOffset, Definitions.Endian IfdSectionEndian) { var ifd = new IfdData(); ifd.Offset = IfdOffset; var entries = new Dictionary<UInt32, Entry>(); var EntryNum = Util.GetUIntValue(App1Data, (int)IfdOffset, 2, IfdSectionEndian); // Debug.WriteLine("Entry num: " + EntryNum); ifd.NextIfdPointer = Util.GetUIntValue(App1Data, (int)IfdOffset + 2 + (int)EntryNum * ENTRY_SIZE, 4, IfdSectionEndian); // if there's no extra data area, (if all data is 4 bytes or less), this is length of this IFD section. ifd.Length = 2 + EntryNum * ENTRY_SIZE + 4; // entry num (2 bytes), each entries (12 bytes each), Next IFD pointer (4 byte) for (int i = 0; i < EntryNum; i++) { // Debug.WriteLine("--- Entry[" + i + "] ---"); var EntryOrigin = (int)IfdOffset + 2 + i * ENTRY_SIZE; var entry = new Entry(); // tag entry.Tag = Util.GetUIntValue(App1Data, EntryOrigin, 2, IfdSectionEndian); var tagTypeName = "Unknown"; if (Util.TagNames.ContainsKey(entry.Tag)) { tagTypeName = Util.TagNames[entry.Tag]; } // Debug.WriteLine("Tag: " + entry.Tag.ToString("X") + " " + tagTypeName); // type var typeValue = Util.GetUIntValue(App1Data, EntryOrigin + 2, 2, IfdSectionEndian); entry.Type = Util.ToEntryType(typeValue); // Debug.WriteLine("Type: " + entry.Type.ToString()); // count entry.Count = Util.GetUIntValue(App1Data, EntryOrigin + 4, 4, IfdSectionEndian); // Debug.WriteLine("Count: " + entry.Count); var valueSize = 0; valueSize = Util.FindDataSize(entry.Type); var TotalValueSize = valueSize * (int)entry.Count; // Debug.WriteLine("Total value size: " + TotalValueSize); var valueBuff = new byte[TotalValueSize]; if (TotalValueSize <= 4) { // in this case, the value is stored directly here. Array.Copy(App1Data, EntryOrigin + 8, valueBuff, 0, TotalValueSize); } else { // other cases, actual value is stored in separated area var EntryValuePointer = (int)Util.GetUIntValue(App1Data, EntryOrigin + 8, 4, IfdSectionEndian); // Debug.WriteLine("Entry pointer: " + EntryValuePointer.ToString("X")); Array.Copy(App1Data, EntryValuePointer, valueBuff, 0, TotalValueSize); // If there's extra data, its length should be added to total length. ifd.Length += (UInt32)TotalValueSize; } if (IfdSectionEndian != Entry.InternalEndian) { // to change endian, each sections should be reversed. var ReversedValue = new byte[valueBuff.Length]; var valueLength = Util.FindDataSize(entry.Type); if (valueLength == 8) { // for fraction value, each value should be reversed individually valueLength = 4; } var valueNum = valueBuff.Length / valueLength; for (int j = 0; j < valueNum; j++) { var tempValue = new byte[valueLength]; Array.Copy(valueBuff, j * valueLength, tempValue, 0, valueLength); Array.Reverse(tempValue); Array.Copy(tempValue, 0, ReversedValue, j * valueLength, valueLength); } entry.value = ReversedValue; } else { // if internal endian and target metadata's one is same, no need to reverse. entry.value = valueBuff; } switch (entry.Type) { case Entry.EntryType.Ascii: // Debug.WriteLine("value: " + entry.StringValue + Environment.NewLine + Environment.NewLine); // Debug.WriteLine(" "); break; case Entry.EntryType.Byte: case Entry.EntryType.Undefined: if (entry.Tag == 0x927C) { // Debug.WriteLine("Maker note is too long to print."); } else { foreach (int val in entry.UIntValues) { // Debug.WriteLine("value: " + val.ToString("X")); } } break; case Entry.EntryType.Short: case Entry.EntryType.SShort: case Entry.EntryType.Long: case Entry.EntryType.SLong: // Util.DumpByteArrayAll(entry.value); foreach (int val in entry.UIntValues) { // Debug.WriteLine("value: " + val); } break; case Entry.EntryType.Rational: case Entry.EntryType.SRational: // Util.DumpByteArrayAll(entry.value); foreach (double val in entry.DoubleValues) { // Debug.WriteLine("value: " + val); } break; default: break; } entries[entry.Tag] = entry; } ifd.Entries = entries; return ifd; }
public static IfdData CreateGpsIfdData(Geoposition position) { var gpsIfdData = new IfdData(); gpsIfdData.Entries = new Dictionary<UInt32, Entry>(); var GPSVersionEntry = new Entry() { Tag = 0x0, Type = Entry.EntryType.Byte, Count = 4, }; GPSVersionEntry.UIntValues = new UInt32[] { 2, 2, 0, 0 }; gpsIfdData.Entries.Add(GPSVersionEntry.Tag, GPSVersionEntry); var LatitudeRefEntry = new Entry() { Tag = 0x1, Type = Entry.EntryType.Ascii, Count = 2, }; char latRef; double latitude = 0; #if (WINDOWS_APP || WINDOWS_UWP) latitude = position.Coordinate.Point.Position.Latitude; #elif WINDOWS_PHONE latitude = position.Coordinate.Latitude; #endif if (latitude > 0) { latRef = 'N'; } else { latRef = 'S'; latitude *= -1; } LatitudeRefEntry.UIntValues = new UInt32[] { (byte)latRef, 0 }; gpsIfdData.Entries.Add(LatitudeRefEntry.Tag, LatitudeRefEntry); var LatitudeEntry = new Entry() { Tag = 0x2, Type = Entry.EntryType.Rational, Count = 3, }; var LatDeg = Math.Floor(latitude); var LatMin = Math.Floor((latitude - LatDeg) * 60); var LatSec = Util.ToRoundUp(((latitude - LatDeg) * 60 - LatMin) * 60, 2); Debug.WriteLine("Latitude: " + LatDeg + " " + LatMin + " " + LatSec); try { LatitudeEntry.DoubleValues = new double[] { LatDeg, LatMin, LatSec }; } catch (OverflowException) { var sec = Util.ToRoundUp(((latitude - LatDeg) * 60 - LatMin) * 60, 0); Debug.WriteLine("Latitude: " + LatDeg + " " + LatMin + " " + sec); LatitudeEntry.DoubleValues = new double[] { LatDeg, LatMin, sec }; } gpsIfdData.Entries.Add(LatitudeEntry.Tag, LatitudeEntry); var LongitudeRef = new Entry() { Tag = 0x3, Type = Entry.EntryType.Ascii, Count = 3, }; char lonRef; double longitude = 0; #if (WINDOWS_APP || WINDOWS_UWP) longitude = position.Coordinate.Point.Position.Longitude; #elif WINDOWS_PHONE longitude = position.Coordinate.Longitude; #endif if (longitude > 0) { lonRef = 'E'; } else { lonRef = 'W'; longitude *= -1; } LongitudeRef.UIntValues = new UInt32[] { (byte)lonRef, 0 }; gpsIfdData.Entries.Add(LongitudeRef.Tag, LongitudeRef); var Longitude = new Entry() { Tag = 0x4, Type = Entry.EntryType.Rational, Count = 3, }; var LonDeg = Math.Floor(longitude); var LonMin = Math.Floor((longitude - LonDeg) * 60); var LonSec = Util.ToRoundUp(((longitude - LonDeg) * 60 - LonMin) * 60, 2); Debug.WriteLine("Longitude: " + LonDeg + " " + LonMin + " " + LonSec); try { Longitude.DoubleValues = new double[] { LonDeg, LonMin, LonSec }; } catch (OverflowException) { var sec = Util.ToRoundUp(((longitude - LonDeg) * 60 - LonMin) * 60, 0); Debug.WriteLine("Longitude: " + LonDeg + " " + LonMin + " " + sec); Longitude.DoubleValues = new double[] { LonDeg, LonMin, sec }; } gpsIfdData.Entries.Add(Longitude.Tag, Longitude); var TimeStampEntry = new Entry() { Tag = 0x7, Type = Entry.EntryType.Rational, Count = 3, }; TimeStampEntry.DoubleValues = new double[] { position.Coordinate.Timestamp.Hour, position.Coordinate.Timestamp.Minute, position.Coordinate.Timestamp.Second }; gpsIfdData.Entries.Add(TimeStampEntry.Tag, TimeStampEntry); var GpsMapDetum = new Entry() { Tag = 0x12, Type = Entry.EntryType.Ascii, Count = 7, }; GpsMapDetum.value = Util.ToByte("WGS-84"); gpsIfdData.Entries.Add(GpsMapDetum.Tag, GpsMapDetum); var GpsDateStamp = new Entry() { Tag = 0x1D, Type = Entry.EntryType.Ascii, Count = 11, }; String str = position.Coordinate.Timestamp.Year.ToString("D4") + ":" + position.Coordinate.Timestamp.Month.ToString("D2") + ":" + position.Coordinate.Timestamp.Day.ToString("D2"); GpsDateStamp.value = Util.ToByte(str); gpsIfdData.Entries.Add(GpsDateStamp.Tag, GpsDateStamp); return gpsIfdData; }
/// <summary> /// Create app1 data section in byte array. /// Needs many informations.... /// </summary> /// <param name="OriginalMetaData">metadata from original image.</param> /// <param name="NewMetaData">metadata which will be recorded in app1 data section</param> /// <param name="OriginalApp1Data">Original app1 data. Other than Exif meta data will be copied to new one</param> /// <param name="OutputImageMetadataEndian">Endian which will be used in Exif meta data. for WP OS, Big endian is recommended.</param> /// <returns></returns> private static byte[] CreateApp1Data(JpegMetaData OriginalMetaData, JpegMetaData NewMetaData, byte[] OriginalApp1Data, Definitions.Endian OutputImageMetadataEndian) { if (NewMetaData.ExifIfd != null && !NewMetaData.PrimaryIfd.Entries.ContainsKey(Definitions.EXIF_IFD_POINTER_TAG)) { // Add Exif IFD section's pointer entry as dummy. NewMetaData.PrimaryIfd.Entries.Add( Definitions.EXIF_IFD_POINTER_TAG, new Entry() { Tag = Definitions.EXIF_IFD_POINTER_TAG, Type = Entry.EntryType.Long, Count = 1, value = new byte[] { 0, 0, 0, 0 } }); } if (NewMetaData.GpsIfd != null && !NewMetaData.PrimaryIfd.Entries.ContainsKey(Definitions.GPS_IFD_POINTER_TAG)) { // Add GPS IFD section's pointer entry as dummy NewMetaData.PrimaryIfd.Entries.Add( Definitions.GPS_IFD_POINTER_TAG, new Entry() { Tag = Definitions.GPS_IFD_POINTER_TAG, Type = Entry.EntryType.Long, Count = 1, value = new byte[] { 0, 0, 0, 0 } }); } byte[] primaryIfd = IfdComposer.ComposeIfdsection(NewMetaData.PrimaryIfd, OutputImageMetadataEndian); byte[] exifIfd = new byte[] { }; byte[] gpsIfd = new byte[] { }; if (NewMetaData.ExifIfd != null) { exifIfd = IfdComposer.ComposeIfdsection(NewMetaData.ExifIfd, OutputImageMetadataEndian); } if (NewMetaData.GpsIfd != null) { gpsIfd = IfdComposer.ComposeIfdsection(NewMetaData.GpsIfd, OutputImageMetadataEndian); } // Debug.WriteLine("Size fixed. Primary: " + primaryIfd.Length.ToString("X") + " exif: " + exifIfd.Length.ToString("X") + " gps: " + gpsIfd.Length.ToString("X")); // after size fixed, set each IFD sections' offset. NewMetaData.PrimaryIfd.Offset = 8; // fixed value // now it's possible to calcurate pointer to Exif/GPS IFD var exifIfdPointer = 8 + primaryIfd.Length; var gpsIfdPointer = 8 + primaryIfd.Length + exifIfd.Length; if (NewMetaData.PrimaryIfd.Entries.ContainsKey(Definitions.EXIF_IFD_POINTER_TAG)) { NewMetaData.PrimaryIfd.Entries.Remove(Definitions.EXIF_IFD_POINTER_TAG); } var exifIfdPointerEntry = new Entry() { Tag = Definitions.EXIF_IFD_POINTER_TAG, Type = Entry.EntryType.Long, Count = 1, }; exifIfdPointerEntry.UIntValues = new UInt32[] { (UInt32)exifIfdPointer }; NewMetaData.PrimaryIfd.Entries.Add(Definitions.EXIF_IFD_POINTER_TAG, exifIfdPointerEntry); if (NewMetaData.PrimaryIfd.Entries.ContainsKey(Definitions.GPS_IFD_POINTER_TAG)) { NewMetaData.PrimaryIfd.Entries.Remove(Definitions.GPS_IFD_POINTER_TAG); } var gpsIfdPointerEntry = new Entry() { Tag = Definitions.GPS_IFD_POINTER_TAG, Type = Entry.EntryType.Long, Count = 1, }; gpsIfdPointerEntry.UIntValues = new UInt32[] { (UInt32)gpsIfdPointer }; NewMetaData.PrimaryIfd.Entries.Add(Definitions.GPS_IFD_POINTER_TAG, gpsIfdPointerEntry); var nextIfdPointer = 8 + primaryIfd.Length + exifIfd.Length + gpsIfd.Length; NewMetaData.PrimaryIfd.NextIfdPointer = (UInt32)nextIfdPointer; // finally, create byte array again primaryIfd = IfdComposer.ComposeIfdsection(NewMetaData.PrimaryIfd, OutputImageMetadataEndian); if (NewMetaData.ExifIfd != null) { NewMetaData.ExifIfd.Offset = 8 + (UInt32)primaryIfd.Length; exifIfd = IfdComposer.ComposeIfdsection(NewMetaData.ExifIfd, OutputImageMetadataEndian); } if (NewMetaData.GpsIfd != null) { NewMetaData.GpsIfd.Offset = 8 + (UInt32)primaryIfd.Length + (UInt32)exifIfd.Length; gpsIfd = IfdComposer.ComposeIfdsection(NewMetaData.GpsIfd, OutputImageMetadataEndian); } // 1st IFD data (after primary IFD data) should be kept var Original1stIfdData = new byte[OriginalApp1Data.Length - OriginalMetaData.PrimaryIfd.NextIfdPointer]; Array.Copy(OriginalApp1Data, (int)OriginalMetaData.PrimaryIfd.NextIfdPointer, Original1stIfdData, 0, Original1stIfdData.Length); // Build App1 section. From "Exif\0\0" to end of thumbnail data (1st IFD) // Exif00 + TIFF header + 3 IFD sections (Primary, Exif, GPS) + 1st IFD data from original data // new app1 size may overfrow from 0xFFFF bytes. in this case, it should be limited up to 0xFFFF byte. var NewApp1DataSize = Math.Min(0xFFFF, 6 + 8 + primaryIfd.Length + exifIfd.Length + gpsIfd.Length + Original1stIfdData.Length); var NewApp1DataOverflowSize = 0; if (NewApp1DataSize == 0xFFFF) { NewApp1DataOverflowSize = (6 + 8 + primaryIfd.Length + exifIfd.Length + gpsIfd.Length + Original1stIfdData.Length) - 0xFFFF; Debug.WriteLine("App1 overflow size: " + NewApp1DataOverflowSize); } var NewApp1Data = new byte[NewApp1DataSize]; // var NewApp1Data = new byte[6 + 8 + primaryIfd.Length + exifIfd.Length + gpsIfd.Length]; // Debug.WriteLine("New App1 size: " + NewApp1Data.Length.ToString("X")); // Array.Copy(OriginalApp1Data, 0, NewApp1Data, 0, 6 + 8); Array.Copy(OriginalApp1Data, 0, NewApp1Data, 0, 6); // only EXIF00 should be copied. var endianSection = Util.ToByte(0x4d4d, 2, OutputImageMetadataEndian); Array.Copy(endianSection, 0, NewApp1Data, 6, 2); var magicNumber = Util.ToByte(0x002A, 2, OutputImageMetadataEndian); Array.Copy(magicNumber, 0, NewApp1Data, 8, 2); var primaryIfdOffset = Util.ToByte(8, 4, OutputImageMetadataEndian); Array.Copy(primaryIfdOffset, 0, NewApp1Data, 10, 4); Array.Copy(primaryIfd, 0, NewApp1Data, 6 + 8, primaryIfd.Length); Array.Copy(exifIfd, 0, NewApp1Data, 6 + 8 + primaryIfd.Length, exifIfd.Length); Array.Copy(gpsIfd, 0, NewApp1Data, 6 + 8 + primaryIfd.Length + exifIfd.Length, gpsIfd.Length); Array.Copy(Original1stIfdData, 0, NewApp1Data, 6 + 8 + primaryIfd.Length + exifIfd.Length + gpsIfd.Length, Original1stIfdData.Length - NewApp1DataOverflowSize); return NewApp1Data; }