Browse Source

Better profile identifers

pull/348/head
JimBobSquarePants 9 years ago
parent
commit
748a2c8673
  1. 29
      src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegConstants.cs
  2. 49
      src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs

29
src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegConstants.cs

@ -25,6 +25,35 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort
/// </summary>
public static readonly IEnumerable<string> FileExtensions = new[] { "jpg", "jpeg", "jfif" };
/// <summary>
/// Descibes the various header identifers for metadata profiles
/// </summary>
/// <remarks>
/// Encoding ASCII isn't available for NetStandard 1.1
/// </remarks>
internal static class ProfileIdentifiers
{
/// <summary>
/// Describes the EXIF specific markers
/// </summary>
public static readonly byte[] JFifMarker = { JFif.J, JFif.F, JFif.I, JFif.F, JFif.Null };
/// <summary>
/// Describes the EXIF specific markers
/// </summary>
public static readonly byte[] IccMarker = { ICC.I, ICC.C, ICC.C, ICC.UnderScore, ICC.P, ICC.R, ICC.O, ICC.F, ICC.I, ICC.L, ICC.E, ICC.Null };
/// <summary>
/// Describes the ICC specific markers
/// </summary>
public static readonly byte[] ExifMarker = { Exif.E, Exif.X, Exif.I, Exif.F, Exif.Null, Exif.Null };
/// <summary>
/// Describes Adobe specific markers <see href="http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/JPEG.html#Adobe"/>
/// </summary>
public static readonly byte[] AdobeMarker = { Adobe.A, Adobe.D, Adobe.O, Adobe.B, Adobe.E };
}
/// <summary>
/// Describes common Jpeg markers
/// </summary>

49
src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs

@ -395,6 +395,17 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort
this.InitDerivedMetaDataProperties();
}
/// <summary>
/// Returns a value indicating whether the passed bytes are a match to the profile identifer
/// </summary>
/// <param name="bytesToCheck">The bytes to check</param>
/// <param name="profileIdentifier">The profile identifier</param>
/// <returns>The <see cref="bool"/></returns>
private static bool IsProfile(Span<byte> bytesToCheck, Span<byte> profileIdentifier)
{
return bytesToCheck.Slice(0, profileIdentifier.Length).SequenceEqual(profileIdentifier);
}
/// <summary>
/// Assigns derived metadata properties to <see cref="MetaData"/>, eg. horizontal and vertical resolution if it has a JFIF header.
/// </summary>
@ -435,14 +446,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort
this.InputProcessor.ReadFull(this.Temp, 0, 13);
remaining -= 13;
this.isJFif = this.Temp[0] == OrigJpegConstants.JFif.J &&
this.Temp[1] == OrigJpegConstants.JFif.F &&
this.Temp[2] == OrigJpegConstants.JFif.I &&
this.Temp[3] == OrigJpegConstants.JFif.F &&
this.Temp[4] == OrigJpegConstants.JFif.Null;
if (this.isJFif)
if (IsProfile(this.Temp, OrigJpegConstants.ProfileIdentifiers.JFifMarker))
{
this.isJFif = true;
this.jFifHorizontalResolution = (short)((this.Temp[8] << 8) | this.Temp[9]);
this.jFifVerticalResolution = (short)((this.Temp[10] << 8) | this.Temp[11]);
}
@ -468,12 +474,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort
byte[] profile = new byte[remaining];
this.InputProcessor.ReadFull(profile, 0, remaining);
if (profile[0] == OrigJpegConstants.Exif.E &&
profile[1] == OrigJpegConstants.Exif.X &&
profile[2] == OrigJpegConstants.Exif.I &&
profile[3] == OrigJpegConstants.Exif.F &&
profile[4] == OrigJpegConstants.Exif.Null &&
profile[5] == OrigJpegConstants.Exif.Null)
if (IsProfile(profile, OrigJpegConstants.ProfileIdentifiers.ExifMarker))
{
this.isExif = true;
this.MetaData.ExifProfile = new ExifProfile(profile);
@ -498,18 +499,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort
this.InputProcessor.ReadFull(identifier, 0, Icclength);
remaining -= Icclength; // We have read it by this point
if (identifier[0] == OrigJpegConstants.ICC.I &&
identifier[1] == OrigJpegConstants.ICC.C &&
identifier[2] == OrigJpegConstants.ICC.C &&
identifier[3] == OrigJpegConstants.ICC.UnderScore &&
identifier[4] == OrigJpegConstants.ICC.P &&
identifier[5] == OrigJpegConstants.ICC.R &&
identifier[6] == OrigJpegConstants.ICC.O &&
identifier[7] == OrigJpegConstants.ICC.F &&
identifier[8] == OrigJpegConstants.ICC.I &&
identifier[9] == OrigJpegConstants.ICC.L &&
identifier[10] == OrigJpegConstants.ICC.E &&
identifier[11] == OrigJpegConstants.ICC.Null)
if (IsProfile(identifier, OrigJpegConstants.ProfileIdentifiers.IccMarker))
{
byte[] profile = new byte[remaining];
this.InputProcessor.ReadFull(profile, 0, remaining);
@ -547,14 +537,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort
this.InputProcessor.ReadFull(this.Temp, 0, 12);
remaining -= 12;
this.isAdobe = this.Temp[0] == OrigJpegConstants.Adobe.A &&
this.Temp[1] == OrigJpegConstants.Adobe.D &&
this.Temp[2] == OrigJpegConstants.Adobe.O &&
this.Temp[3] == OrigJpegConstants.Adobe.B &&
this.Temp[4] == OrigJpegConstants.Adobe.E;
if (this.isAdobe)
if (IsProfile(this.Temp, OrigJpegConstants.ProfileIdentifiers.AdobeMarker))
{
this.isAdobe = true;
this.adobeColorTransform = this.Temp[11];
}

Loading…
Cancel
Save