static string ExifValueToString(IExifValue v) { if (v.Tag == ExifTag.UserComment || v.Tag == ExifTag.ExifVersion) { var data = v.GetValue() as byte[]; string s = Encoding.UTF8.GetString(data); return(s); } if (v.IsArray) { if (v.DataType == ExifDataType.Undefined || v.DataType == ExifDataType.Byte) { var data = v.GetValue() as byte[]; int max = Math.Min(data.Length, MaxBytesToShow); string hasMore = data.Length > MaxBytesToShow ? " ..." : ""; string hex = BitConverter.ToString(data, 0, max).Replace("-", " "); string s = $"Bytes [{hex}{hasMore}]"; return(s); } return($"[not implemented. type:{v.DataType}]"); } else { return(v.GetValue().ToString()); } }
/// <summary> /// Gets a formatted string for a given Exif value /// </summary> /// <param name="exifValue">The source Exif value</param> /// <returns>The formatted string</returns> public string GetFormattedString(IExifValue exifValue) { var values = exifValue.Values.Cast<ushort>(); string formattedString = String.Empty; switch (values.FirstOrDefault()) { case 0: formattedString = "Auto exposure"; break; case 1: formattedString = "Manual exposure"; break; case 2: formattedString = "Auto bracket"; break; default: formattedString = "Reserved"; break; } return formattedString; }
private static void TestProfile(ExifProfile profile) { Assert.NotNull(profile); // todo: duplicate tags Assert.Equal(2, profile.Values.Count(v => (ushort)v.Tag == 59932)); Assert.Equal(16, profile.Values.Count); foreach (IExifValue value in profile.Values) { Assert.NotNull(value.GetValue()); } IExifValue <string> software = profile.GetValue(ExifTag.Software); Assert.Equal("Windows Photo Editor 10.0.10011.16384", software.Value); IExifValue <Rational> xResolution = profile.GetValue(ExifTag.XResolution); Assert.Equal(new Rational(300.0), xResolution.Value); IExifValue <Number> xDimension = profile.GetValue(ExifTag.PixelXDimension); Assert.Equal(2338U, xDimension.Value); }
public void ReadWriteInfinity(TestImageWriteFormat imageFormat) { Image <Rgba32> image = TestFile.Create(TestImages.Jpeg.Baseline.Floorplan).CreateRgba32Image(); image.Metadata.ExifProfile.SetValue(ExifTag.ExposureBiasValue, new SignedRational(double.PositiveInfinity)); image = WriteAndReadJpeg(image); IExifValue <SignedRational> value = image.Metadata.ExifProfile.GetValue(ExifTag.ExposureBiasValue); Assert.NotNull(value); Assert.Equal(new SignedRational(double.PositiveInfinity), value.Value); image.Metadata.ExifProfile.SetValue(ExifTag.ExposureBiasValue, new SignedRational(double.NegativeInfinity)); image = WriteAndRead(image, imageFormat); value = image.Metadata.ExifProfile.GetValue(ExifTag.ExposureBiasValue); Assert.NotNull(value); Assert.Equal(new SignedRational(double.NegativeInfinity), value.Value); image.Metadata.ExifProfile.SetValue(ExifTag.FlashEnergy, new Rational(double.NegativeInfinity)); image = WriteAndRead(image, imageFormat); IExifValue <Rational> value2 = image.Metadata.ExifProfile.GetValue(ExifTag.FlashEnergy); Assert.NotNull(value2); Assert.Equal(new Rational(double.PositiveInfinity), value2.Value); }
public void TestExifTag() { var exifProfile = new ExifProfile(); exifProfile.SetValue(ExifTag.ResolutionUnit, (ushort)1); IExifValue value = exifProfile.GetValue(ExifTag.ResolutionUnit); Assert.Equal("None", value.ToString()); exifProfile.SetValue(ExifTag.ResolutionUnit, (ushort)2); value = exifProfile.GetValue(ExifTag.ResolutionUnit); Assert.Equal("Inches", value.ToString()); exifProfile.SetValue(ExifTag.ResolutionUnit, (ushort)3); value = exifProfile.GetValue(ExifTag.ResolutionUnit); Assert.Equal("Centimeter", value.ToString()); exifProfile.SetValue(ExifTag.ResolutionUnit, (ushort)4); value = exifProfile.GetValue(ExifTag.ResolutionUnit); Assert.Equal("4", value.ToString()); exifProfile.SetValue(ExifTag.ImageWidth, 123U); value = exifProfile.GetValue(ExifTag.ImageWidth); Assert.Equal("123", value.ToString()); }
/// <summary> /// Returns the current EXIF orientation /// </summary> /// <param name="source">The image to auto rotate.</param> /// <returns>The <see cref="OrientationMode"/></returns> private static OrientationMode GetExifOrientation(Image <TPixel> source) { if (source.Metadata.ExifProfile is null) { return(OrientationMode.Unknown); } IExifValue <ushort> value = source.Metadata.ExifProfile.GetValue(ExifTag.Orientation); if (value is null) { return(OrientationMode.Unknown); } OrientationMode orientation; if (value.DataType == ExifDataType.Short) { orientation = (OrientationMode)value.Value; } else { orientation = (OrientationMode)Convert.ToUInt16(value.Value); source.Metadata.ExifProfile.RemoveValue(ExifTag.Orientation); } source.Metadata.ExifProfile.SetValue(ExifTag.Orientation, (ushort)OrientationMode.TopLeft); return(orientation); }
internal static uint GetNumberOfComponents(IExifValue exifValue) { object value = exifValue.GetValue(); if (ExifUcs2StringHelpers.IsUcs2Tag((ExifTagValue)(ushort)exifValue.Tag)) { return((uint)ExifUcs2StringHelpers.Ucs2Encoding.GetByteCount((string)value)); } if (value is EncodedString encodedString) { return(ExifEncodedStringHelpers.GetDataLength(encodedString)); } if (exifValue.DataType == ExifDataType.Ascii) { return((uint)ExifConstants.DefaultEncoding.GetByteCount((string)value) + 1); } if (value is Array arrayValue) { return((uint)arrayValue.Length); } return(1); }
public static PixelResolutionUnit ExifProfileToResolutionUnit(ExifProfile profile) { IExifValue <ushort> resolution = profile.GetValue(ExifTag.ResolutionUnit); // EXIF is 1, 2, 3 so we minus "1" off the result. return(resolution is null ? DefaultResolutionUnit : (PixelResolutionUnit)(byte)(resolution.Value - 1)); }
private static void VerifyExifDataIsPresent(ExifProfile exif) { Assert.Equal(1, exif.Values.Count); IExifValue <string> software = exif.GetValue(ExifTag.Software); Assert.NotNull(software); Assert.Equal("ImageSharp", software.Value); }
protected override JProperty Transform(IExifValue value) { Byte[] bytes = (Byte[])value.GetValue(); string[] strings = bytes.Select(b => b.ToString()).ToArray(); string version = string.Join('.', strings); return(new JProperty(Name, version)); }
public void IEquatable() { IExifValue <string> first = this.GetExifValue(); IExifValue <string> second = this.GetExifValue(); Assert.True(first == second); Assert.True(first.Equals(second)); }
/// <summary> /// Gets a formatted string for a given Exif value /// </summary> /// <param name="exifValue">The source Exif value</param> /// <returns>The formatted string</returns> public string GetFormattedString(IExifValue exifValue) { var values = exifValue.Values.Cast<Rational32>(); if (values.Count() != 3) { return String.Empty; } return String.Format("{0}:{1}:{2}", (int)(double)values.ElementAt(0), (int)(double)values.ElementAt(1), (int)(double)values.ElementAt(2)); }
public void ShouldReturnFalseWhenValueIsInvalidDataType1() { var profile = new ExifProfile(); profile.SetValue(ExifTag.Software, "Magick.NET"); IExifValue value = profile.GetValue(ExifTag.Software); Assert.False(value.SetValue(10.5)); }
public void ShouldReturnFalseWhenValueIsInvalidDataType2() { var profile = new ExifProfile(); profile.SetValue(ExifTag.ShutterSpeedValue, new SignedRational(75.55)); IExifValue value = profile.GetValue(ExifTag.ShutterSpeedValue); Assert.False(value.SetValue(75)); }
/// <summary> /// Returns the EXIF data. /// </summary> /// <returns> /// The <see cref="T:byte[]"/>. /// </returns> public byte[] GetData() { const uint startIndex = 0; IExifValue exifOffset = GetOffsetValue(this.ifdValues, this.exifValues, ExifTag.SubIFDOffset); IExifValue gpsOffset = GetOffsetValue(this.ifdValues, this.gpsValues, ExifTag.GPSIFDOffset); uint ifdLength = this.GetLength(this.ifdValues); uint exifLength = this.GetLength(this.exifValues); uint gpsLength = this.GetLength(this.gpsValues); uint length = ifdLength + exifLength + gpsLength; if (length == 0) { return(Array.Empty <byte>()); } // 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; // first IFD offset length += 4; byte[] result = new byte[length]; int i = 0; // The byte order marker for little-endian, followed by the number 42 and a 0 ExifConstants.LittleEndianByteOrderMarker.CopyTo(result.AsSpan(start: i)); i += ExifConstants.LittleEndianByteOrderMarker.Length; uint ifdOffset = (uint)i - startIndex + 4U; exifOffset?.TrySetValue(ifdOffset + ifdLength); gpsOffset?.TrySetValue(ifdOffset + ifdLength + exifLength); i = WriteUInt32(ifdOffset, result, i); i = this.WriteHeaders(this.ifdValues, result, i); i = this.WriteData(startIndex, this.ifdValues, result, i); if (exifLength > 0) { i = this.WriteHeaders(this.exifValues, result, i); i = this.WriteData(startIndex, this.exifValues, result, i); } if (gpsLength > 0) { i = this.WriteHeaders(this.gpsValues, result, i); i = this.WriteData(startIndex, this.gpsValues, result, i); } return(result); }
/// <summary> /// Gets the height of the image frame. /// </summary> /// <param name="exifProfile">The image frame exif profile.</param> /// <returns>The image height.</returns> private static int GetImageHeight(ExifProfile exifProfile) { IExifValue <Number> height = exifProfile.GetValue(ExifTag.ImageLength); if (height == null) { TiffThrowHelper.ThrowImageFormatException("The TIFF image frame is missing the ImageLength"); } return((int)height.Value); }
private static int WriteValue(IExifValue value, byte[] destination, int offset) { if (value.IsArray && value.DataType != ExifDataType.String) { return(WriteArray(value, destination, offset)); } else { return(WriteValue(value.DataType, value.Value, destination, offset)); } }
private static int WriteArray(IExifValue value, Span <byte> destination, int offset) { int newOffset = offset; foreach (object obj in (Array)value.GetValue()) { newOffset = WriteValue(value.DataType, obj, destination, newOffset); } return(newOffset); }
/// <summary> /// Gets the width of the image frame. /// </summary> /// <param name="exifProfile">The image frame exif profile.</param> /// <returns>The image width.</returns> private static int GetImageWidth(ExifProfile exifProfile) { IExifValue <Number> width = exifProfile.GetValue(ExifTag.ImageWidth); if (width == null) { TiffThrowHelper.ThrowImageFormatException("The TIFF image frame is missing the ImageWidth"); } return((int)width.Value); }
/// <summary> /// Gets a formatted string for a given Exif value /// </summary> /// <param name="exifValue">The source Exif value</param> /// <returns>The formatted string</returns> public virtual string GetFormattedString(IExifValue exifValue) { string firstValue = String.Empty; foreach (var item in exifValue.Values) { firstValue = item.ToString(); break; } return firstValue; }
private IExifValue CreateValue() { if (!_reader.CanRead(12)) { return(null); } var tag = (ExifTag)ReadShort(); var dataType = EnumHelper.Parse(ReadShort(), ExifDataType.Unknown); IExifValue value = null; if (dataType == ExifDataType.Unknown) { return(null); } var numberOfComponents = ReadLong(); if (dataType == ExifDataType.Undefined && numberOfComponents == 0) { numberOfComponents = 4; } var oldIndex = _reader.Index; var length = numberOfComponents * ExifDataTypes.GetSize(dataType); if (length <= 4) { value = CreateValue(tag, dataType, numberOfComponents); } else { var newIndex = _startIndex + ReadLong(); if (_reader.Seek(newIndex)) { if (_reader.CanRead(length)) { value = CreateValue(tag, dataType, numberOfComponents); } } if (value == null) { _invalidTags.Add(tag); } } _reader.Seek(oldIndex + 4); return(value); }
public void ShouldReturnFalseWhenValueIsInvalidDataType3() { var profile = new ExifProfile(); profile.SetValue(ExifTag.XResolution, new Rational(150.0)); IExifValue value = profile.GetValue(ExifTag.XResolution); Assert.NotNull(value); Assert.Equal("150", value.ToString()); Assert.False(value.SetValue("Magick.NET")); }
public void Properties() { IExifValue <string> value = this.GetExifValue(); Assert.Equal(ExifDataType.Ascii, value.DataType); Assert.Equal(ExifTag.Software, value.Tag); Assert.False(value.IsArray); const string expected = "Windows Photo Editor 10.0.10011.16384"; Assert.Equal(expected, value.ToString()); Assert.Equal(expected, value.Value); }
/// <summary> /// Gets the width of the image frame. /// </summary> /// <param name="exifProfile">The image frame exif profile.</param> /// <returns>The image width.</returns> private static int GetImageWidth(ExifProfile exifProfile) { IExifValue <Number> width = exifProfile.GetValue(ExifTag.ImageWidth); if (width == null) { TiffThrowHelper.ThrowImageFormatException("The TIFF image frame is missing the ImageWidth"); } DebugGuard.MustBeLessThanOrEqualTo((ulong)width.Value, (ulong)int.MaxValue, nameof(ExifTag.ImageWidth)); return((int)width.Value); }
public void AddOrReplace(IExifValue entry) { int index = this.Entries.FindIndex(t => t.Tag == entry.Tag); if (index >= 0) { this.Entries[index] = entry; } else { this.Entries.Add(entry); } }
private async Task <GeoLocation?> GetGeoLocationDataAsync( ExifProfile exifProfile, CancellationToken cancellationToken) { IExifValue <Rational[]> lat = exifProfile.GetValue(ExifTag.GPSLatitude); IExifValue <Rational[]> lon = exifProfile.GetValue(ExifTag.GPSLongitude); IExifValue <string> latRef = exifProfile.GetValue(ExifTag.GPSLatitudeRef); IExifValue <string> lonRef = exifProfile.GetValue(ExifTag.GPSLongitudeRef); IExifValue <Rational> alt = exifProfile.GetValue(ExifTag.GPSAltitude); if (lat != null) { var gps = new GeoLocation() { Type = "gps", Point = new GeoPoint() }; var latValue = ConvertToLocation(lat.Value); var lonValue = ConvertToLocation(lon.Value); if (latRef.Value.ToString() == "S") { latValue = latValue * -1; } if (lonRef.Value.ToString() == "W") { lonValue = lonValue * -1; } gps.Point = GeoPoint.Create(latValue, lonValue); if (alt != null) { var dominator = (int)alt.Value.Denominator; if (dominator == 0) { dominator = 1; } gps.Altitude = (int)alt.Value.Numerator / dominator; } gps.GeoHash = GeoHash.Encode(gps.Point.Coordinates[1], gps.Point.Coordinates[0]); gps.Address = await _geoDecoderService.DecodeAsync( gps.Point.Coordinates[1], gps.Point.Coordinates[0], cancellationToken); return(gps); } return(null); }
private static uint GetNumberOfComponents(IExifValue value) { if (value.DataType == ExifDataType.String) { return((uint)Encoding.UTF8.GetBytes((string)value.Value).Length + 1); } if (value.Value is Array arrayValue) { return((uint)arrayValue.Length); } return(1); }
public static JProperty Transform(TransformType type, IExifValue value) { try { Transformer service = TransformProvider.GetService(type); service.Name = value.GetName(); return(service.Transform(value)); } catch (Exception ex) { string name = value.GetName(); return(new JProperty(name, ex.Message)); } }
private static bool HasValue(IExifValue value) { if (value.DataType == ExifDataType.String) { var stringValue = (string)value.Value; return(stringValue != null && stringValue.Length > 0); } if (value.Value is Array arrayValue) { return(arrayValue != null && arrayValue.Length > 0); } return(true); }
/// <summary> /// Gets a formatted string for a given Exif value /// </summary> /// <param name="exifValue">The source Exif value</param> /// <returns>The formatted string</returns> public string GetFormattedString(IExifValue exifValue) { var values = exifValue.Values.Cast<Rational32>(); if (values.Count() == 0) { return String.Empty; } Rational32 exposure = values.First(); uint numerator = (uint)exposure.Numerator; uint denominator = (uint)exposure.Denominator; return denominator == 1 ? String.Format("{0} sec.", numerator) : String.Format("{0}/{1} sec.", numerator, denominator); }
private static int WriteArray(IExifValue value, byte[] destination, int offset) { if (value.DataType == ExifDataType.String) { return(WriteValue(ExifDataType.String, value.Value, destination, offset)); } int newOffset = offset; foreach (object obj in (Array)value.Value) { newOffset = WriteValue(value.DataType, obj, destination, newOffset); } return(newOffset); }
/// <summary> /// Gets a formatted string for a given Exif value /// </summary> /// <param name="exifValue">The source Exif value</param> /// <returns>The formatted string</returns> public string GetFormattedString(IExifValue exifValue) { var values = exifValue.Values.Cast<Rational32>(); if (values.Count() == 0) { return String.Empty; } double apexValue = (double)values.First(); double shutterSpeed = 1 / Math.Pow(2, apexValue); return shutterSpeed > 1 ? String.Format("{0} sec.", (int)Math.Round(shutterSpeed)) : String.Format("{0}/{1} sec.", 1, (int)Math.Round(1 / shutterSpeed)); }
private int WriteArray(IExifValue value, Span <byte> destination, int offset) { if (value.DataType == ExifDataType.Ascii) { return(this.WriteValue(ExifDataType.Ascii, value.GetValue(), destination, offset)); } int newOffset = offset; foreach (object obj in (Array)value.GetValue()) { newOffset = this.WriteValue(value.DataType, obj, destination, newOffset); } return(newOffset); }
private static uint GetNumberOfComponents(IExifValue exifValue) { object value = exifValue.GetValue(); if (exifValue.DataType == ExifDataType.Ascii) { return((uint)Encoding.UTF8.GetBytes((string)value).Length + 1); } if (value is Array arrayValue) { return((uint)arrayValue.Length); } return(1); }
private CameraData?GetCameraData(ExifProfile exifProfile) { IExifValue <string> model = exifProfile.GetValue(ExifTag.Model); IExifValue <string> make = exifProfile.GetValue(ExifTag.Make); if (model?.GetValue() != null) { return(new CameraData { Model = model.ToString(), Make = make.ToString() }); } return(null); }
/// <summary> /// Gets a formatted string for a given Exif value /// </summary> /// <param name="exifValue">The source Exif value</param> /// <returns>The formatted string</returns> public virtual string GetFormattedString(IExifValue exifValue) { var values = exifValue.Values.Cast<ushort>(); string formattedString = String.Empty; switch (values.FirstOrDefault()) { case 2: formattedString = "Inches"; break; case 3: formattedString = "Centimeters"; break; default: formattedString = "Reserved"; break; } return formattedString; }
/// <summary> /// Gets a formatted string for a given Exif value /// </summary> /// <param name="exifValue">The source Exif value</param> /// <returns>The formatted string</returns> public string GetFormattedString(IExifValue exifValue) { var values = exifValue.Values.Cast<Rational32>(); return String.Format("{0:g2} step", (double)values.FirstOrDefault()); }
/// <summary> /// Initializes the exifValue field. /// </summary> /// <returns>The initialized exifValue</returns> private IExifValue InitializeExifValue() { try { var customExtractor = this.parentReader.QueryForCustomUndefinedExtractor(this.RawExifTagId); if (customExtractor != null) { return this.exifValue = customExtractor.GetExifValue(this.propertyItem.Value, this.propertyItem.Len); } return this.exifValue = this.ExifDatatype == PropertyTagType.Undefined ? ExifValueCreator.CreateUndefined(this.ExifTag, this.propertyItem.Value, this.propertyItem.Len) : ExifValueCreator.Create(this.ExifDatatype, this.propertyItem.Value, this.propertyItem.Len); } catch (ExifReaderException ex) { throw new InvalidOperationException( "An ExifReaderException was caught. See InnerException for more details", ex); } catch (Exception ex) { throw new InvalidOperationException( "An ExifReaderException was caught. See InnerException for more details", new ExifReaderException(ex, this.propertyFormatter, null)); } }
/// <summary> /// Gets a formatted string for a given Exif value /// </summary> /// <param name="exifValue">The source Exif value</param> /// <returns>The formatted string</returns> public string GetFormattedString(IExifValue exifValue) { var values = exifValue.Values.Cast<ushort>(); return values.Count() == 0 ? String.Empty : String.Format("ISO-{0}", values.First()); }
/// <summary> /// Gets a formatted string for a given Exif value /// </summary> /// <param name="exifValue">The source Exif value</param> /// <returns>The formatted string</returns> public override string GetFormattedString(IExifValue exifValue) { var values = exifValue.Values.Cast<Rational32>(); return values.Count() == 0 ? String.Empty : String.Concat(((double)values.First()).ToString("0.00"), " m"); }
/// <summary> /// Gets a formatted string for a given Exif value /// </summary> /// <param name="exifValue">The source Exif value</param> /// <returns>The formatted string</returns> public override string GetFormattedString(IExifValue exifValue) { var values = exifValue.Values.Cast<Rational32>(); return values.Count() == 0 ? String.Empty : String.Format("f/{0:g3}", (double)values.First()); }