diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegConstants.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegConstants.cs index 8098cc339..57bc1118c 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegConstants.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegConstants.cs @@ -25,6 +25,35 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort /// public static readonly IEnumerable FileExtensions = new[] { "jpg", "jpeg", "jfif" }; + /// + /// Descibes the various header identifers for metadata profiles + /// + /// + /// Encoding ASCII isn't available for NetStandard 1.1 + /// + internal static class ProfileIdentifiers + { + /// + /// Describes the EXIF specific markers + /// + public static readonly byte[] JFifMarker = { JFif.J, JFif.F, JFif.I, JFif.F, JFif.Null }; + + /// + /// Describes the EXIF specific markers + /// + 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 }; + + /// + /// Describes the ICC specific markers + /// + public static readonly byte[] ExifMarker = { Exif.E, Exif.X, Exif.I, Exif.F, Exif.Null, Exif.Null }; + + /// + /// Describes Adobe specific markers + /// + public static readonly byte[] AdobeMarker = { Adobe.A, Adobe.D, Adobe.O, Adobe.B, Adobe.E }; + } + /// /// Describes common Jpeg markers /// diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs index d7224f3cf..96ade60a0 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs @@ -395,6 +395,17 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort this.InitDerivedMetaDataProperties(); } + /// + /// Returns a value indicating whether the passed bytes are a match to the profile identifer + /// + /// The bytes to check + /// The profile identifier + /// The + private static bool IsProfile(Span bytesToCheck, Span profileIdentifier) + { + return bytesToCheck.Slice(0, profileIdentifier.Length).SequenceEqual(profileIdentifier); + } + /// /// Assigns derived metadata properties to , eg. horizontal and vertical resolution if it has a JFIF header. /// @@ -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]; }