From 36787b23568dbc458323d9ece4e7a3bd8dc8f6a1 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 11 May 2018 15:04:09 +1000 Subject: [PATCH] Merge constants and rename decoder core --- .../JpegConstants.cs} | 69 ++++--- .../GolangPort/Components/Decoder/Bytes.cs | 10 +- .../Components/Decoder/InputProcessor.cs | 8 +- .../Components/Decoder/OrigComponent.cs | 10 +- .../OrigJpegScanDecoder.ComputationData.cs | 4 +- .../Components/Decoder/OrigJpegScanDecoder.cs | 36 ++-- ...ecoderCore.cs => GolangJpegDecoderCore.cs} | 48 ++--- .../Jpeg/GolangPort/JpegEncoderCore.cs | 61 +++--- .../Jpeg/GolangPort/OrigJpegConstants.cs | 189 ------------------ .../Jpeg/GolangPort/OrigJpegDecoder.cs | 4 +- src/ImageSharp/Formats/Jpeg/JpegDecoder.cs | 4 +- src/ImageSharp/Formats/Jpeg/JpegFormat.cs | 7 +- .../PdfJsPort/Components/PdfJsScanDecoder.cs | 4 +- .../Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs | 70 +++---- .../Formats/Jpg/AdobeMarkerTests.cs | 26 +-- .../Jpg/JpegImagePostProcessorTests.cs | 4 +- .../Formats/Jpg/ParseStreamTests.cs | 8 +- .../Formats/Jpg/SpectralJpegTests.cs | 4 +- .../Formats/Jpg/Utils/JpegFixture.cs | 4 +- .../Jpg/Utils/LibJpegTools.SpectralData.cs | 2 +- 20 files changed, 203 insertions(+), 369 deletions(-) rename src/ImageSharp/Formats/Jpeg/{PdfJsPort/PdfJsJpegConstants.cs => Common/JpegConstants.cs} (78%) rename src/ImageSharp/Formats/Jpeg/GolangPort/{OrigJpegDecoderCore.cs => GolangJpegDecoderCore.cs} (94%) delete mode 100644 src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegConstants.cs diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegConstants.cs b/src/ImageSharp/Formats/Jpeg/Common/JpegConstants.cs similarity index 78% rename from src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegConstants.cs rename to src/ImageSharp/Formats/Jpeg/Common/JpegConstants.cs index 437f77286..e0f4e0731 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegConstants.cs +++ b/src/ImageSharp/Formats/Jpeg/Common/JpegConstants.cs @@ -1,23 +1,45 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -// ReSharper disable InconsistentNaming -namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort +using System.Collections.Generic; + +namespace SixLabors.ImageSharp.Formats.Jpeg.Common { /// - /// Contains jpeg constant values + /// Contains jpeg constant values defined in the specification. /// - internal static class PdfJsJpegConstants + internal static class JpegConstants { + /// + /// The maximum allowable length in each dimension of a jpeg image. + /// + public const ushort MaxLength = 65535; + + /// + /// The list of mimetypes that equate to a jpeg. + /// + public static readonly IEnumerable MimeTypes = new[] { "image/jpeg", "image/pjpeg" }; + + /// + /// The list of file extensions that equate to a jpeg. + /// + public static readonly IEnumerable FileExtensions = new[] { "jpg", "jpeg", "jfif" }; + /// /// Contains marker specific constants /// - public static class Markers + // ReSharper disable InconsistentNaming + internal static class Markers { /// /// The prefix used for all markers. /// - public const byte Prefix = 0xFF; + public const byte XFF = 0xFF; + + /// + /// Same as but of type + /// + public const int XFFInt = XFF; /// /// The Start of Image marker @@ -161,7 +183,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort /// /// Define Restart Interval /// - /// Specifies the interval between RSTn markers, in macroblocks.This marker is followed by two bytes indicating the fixed size so it can be treated like any other variable size segment. + /// Specifies the interval between RSTn markers, in macroblocks.This marker is followed by two bytes indicating the fixed size so + /// it can be treated like any other variable size segment. /// /// public const byte DRI = 0xDD; @@ -193,27 +216,27 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort /// /// public const byte RST7 = 0xD7; + } + /// + /// Contains Adobe specific constants + /// + internal static class Adobe + { /// - /// Contains Adobe specific markers + /// The color transform is unknown.(RGB or CMYK) /// - public static class Adobe - { - /// - /// The color transform is unknown.(RGB or CMYK) - /// - public const byte ColorTransformUnknown = 0; + public const byte ColorTransformUnknown = 0; - /// - /// The color transform is YCbCr (luminance, red chroma, blue chroma) - /// - public const byte ColorTransformYCbCr = 1; + /// + /// The color transform is YCbCr (luminance, red chroma, blue chroma) + /// + public const byte ColorTransformYCbCr = 1; - /// - /// The color transform is YCCK (luminance, red chroma, blue chroma, keyline) - /// - public const byte ColorTransformYcck = 2; - } + /// + /// The color transform is YCCK (luminance, red chroma, blue chroma, keyline) + /// + public const byte ColorTransformYcck = 2; } } } \ No newline at end of file diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/Bytes.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/Bytes.cs index 2a3817400..467b9b46a 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/Bytes.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/Bytes.cs @@ -5,6 +5,8 @@ using System; using System.IO; using System.Runtime.CompilerServices; +using SixLabors.ImageSharp.Formats.Jpeg.Common; + namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder { /// @@ -86,7 +88,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder x = this.BufferAsInt[this.I]; this.I++; this.UnreadableBytes = 1; - if (x != OrigJpegConstants.Markers.XFFInt) + if (x != JpegConstants.Markers.XFFInt) { return OrigDecoderErrorCode.NoError; } @@ -98,7 +100,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder this.I++; this.UnreadableBytes = 2; - x = OrigJpegConstants.Markers.XFF; + x = JpegConstants.Markers.XFF; return OrigDecoderErrorCode.NoError; } @@ -111,7 +113,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder return errorCode; } - if (x != OrigJpegConstants.Markers.XFF) + if (x != JpegConstants.Markers.XFF) { return OrigDecoderErrorCode.NoError; } @@ -128,7 +130,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder return OrigDecoderErrorCode.MissingFF00; } - x = OrigJpegConstants.Markers.XFF; + x = JpegConstants.Markers.XFF; return OrigDecoderErrorCode.NoError; } diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/InputProcessor.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/InputProcessor.cs index cb4b63cff..8932248e4 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/InputProcessor.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/InputProcessor.cs @@ -8,7 +8,7 @@ using System.Runtime.CompilerServices; namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder { /// - /// Encapsulates stream reading and processing data and operations for . + /// Encapsulates stream reading and processing data and operations for . /// It's a value type for imporved data locality, and reduced number of CALLVIRT-s /// internal struct InputProcessor : IDisposable @@ -27,7 +27,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder /// Initializes a new instance of the struct. /// /// The input - /// Temporal buffer, same as + /// Temporal buffer, same as public InputProcessor(Stream inputStream, byte[] temp) { this.Bits = default(Bits); @@ -43,7 +43,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder public Stream InputStream { get; } /// - /// Gets the temporary buffer, same instance as + /// Gets the temporary buffer, same instance as /// public byte[] Temp { get; } @@ -138,7 +138,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder /// /// Reads exactly length bytes into data. It does not care about byte stuffing. - /// Does not throw on errors, returns instead! + /// Does not throw on errors, returns instead! /// /// The data to write to. /// The offset in the source buffer diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/OrigComponent.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/OrigComponent.cs index e2b72db05..1317af394 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/OrigComponent.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/OrigComponent.cs @@ -56,8 +56,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder /// Initializes /// /// The to use for buffer allocations. - /// The instance - public void InitializeDerivedData(MemoryManager memoryManager, OrigJpegDecoderCore decoder) + /// The instance + public void InitializeDerivedData(MemoryManager memoryManager, GolangJpegDecoderCore decoder) { // For 4-component images (either CMYK or YCbCrK), we only support two // hv vectors: [0x11 0x11 0x11 0x11] and [0x22 0x11 0x11 0x22]. @@ -86,8 +86,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder /// /// Initializes all component data except . /// - /// The instance - public void InitializeCoreData(OrigJpegDecoderCore decoder) + /// The instance + public void InitializeCoreData(GolangJpegDecoderCore decoder) { // Section B.2.2 states that "the value of C_i shall be different from // the values of C_1 through C_(i-1)". @@ -102,7 +102,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder } this.QuantizationTableIndex = decoder.Temp[8 + (3 * i)]; - if (this.QuantizationTableIndex > OrigJpegDecoderCore.MaxTq) + if (this.QuantizationTableIndex > GolangJpegDecoderCore.MaxTq) { throw new ImageFormatException("Bad Tq value"); } diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/OrigJpegScanDecoder.ComputationData.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/OrigJpegScanDecoder.ComputationData.cs index c9bb898aa..41845ff72 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/OrigJpegScanDecoder.ComputationData.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/OrigJpegScanDecoder.ComputationData.cs @@ -30,12 +30,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder /// /// The buffer storing the -s for each component /// - public fixed byte ScanData[3 * OrigJpegDecoderCore.MaxComponents]; + public fixed byte ScanData[3 * GolangJpegDecoderCore.MaxComponents]; /// /// The DC values for each component /// - public fixed int Dc[OrigJpegDecoderCore.MaxComponents]; + public fixed int Dc[GolangJpegDecoderCore.MaxComponents]; /// /// Creates and initializes a new instance diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/OrigJpegScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/OrigJpegScanDecoder.cs index d10def3ce..052635a31 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/OrigJpegScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/OrigJpegScanDecoder.cs @@ -110,12 +110,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder private byte expectedRst; /// - /// Initializes a default-constructed instance for reading data from -s stream. + /// Initializes a default-constructed instance for reading data from -s stream. /// /// Pointer to on the stack - /// The instance + /// The instance /// The remaining bytes in the segment block. - public static void InitStreamReading(OrigJpegScanDecoder* p, OrigJpegDecoderCore decoder, int remaining) + public static void InitStreamReading(OrigJpegScanDecoder* p, GolangJpegDecoderCore decoder, int remaining) { p->data = ComputationData.Create(); p->pointers = new DataPointers(&p->data); @@ -123,7 +123,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder } /// - /// Read Huffman data from Jpeg scans in , + /// Read Huffman data from Jpeg scans in , /// and decode it as into . /// /// The blocks are traversed one MCU at a time. For 4:2:0 chroma @@ -149,14 +149,14 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder /// 0 1 2 /// 3 4 5 /// - /// The instance - public void DecodeBlocks(OrigJpegDecoderCore decoder) + /// The instance + public void DecodeBlocks(GolangJpegDecoderCore decoder) { decoder.InputProcessor.ResetErrorState(); this.blockCounter = 0; this.mcuCounter = 0; - this.expectedRst = OrigJpegConstants.Markers.RST0; + this.expectedRst = JpegConstants.Markers.RST0; for (int my = 0; my < decoder.MCUCountY; my++) { @@ -177,7 +177,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder } } - private void DecodeBlocksAtMcuIndex(OrigJpegDecoderCore decoder, int mx, int my) + private void DecodeBlocksAtMcuIndex(GolangJpegDecoderCore decoder, int mx, int my) { for (int scanIndex = 0; scanIndex < this.componentScanCount; scanIndex++) { @@ -223,7 +223,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder } } - private void ProcessRSTMarker(OrigJpegDecoderCore decoder) + private void ProcessRSTMarker(GolangJpegDecoderCore decoder) { // Attempt to look for RST[0-7] markers to resynchronize from corrupt input. if (!decoder.InputProcessor.ReachedEOF) @@ -262,15 +262,15 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder } this.expectedRst++; - if (this.expectedRst == OrigJpegConstants.Markers.RST7 + 1) + if (this.expectedRst == JpegConstants.Markers.RST7 + 1) { - this.expectedRst = OrigJpegConstants.Markers.RST0; + this.expectedRst = JpegConstants.Markers.RST0; } } } } - private void Reset(OrigJpegDecoderCore decoder) + private void Reset(GolangJpegDecoderCore decoder) { decoder.InputProcessor.ResetHuffmanDecoder(); @@ -285,15 +285,15 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder /// private void ResetDcValues() { - Unsafe.InitBlock(this.pointers.Dc, default(byte), sizeof(int) * OrigJpegDecoderCore.MaxComponents); + Unsafe.InitBlock(this.pointers.Dc, default(byte), sizeof(int) * GolangJpegDecoderCore.MaxComponents); } /// /// The implementation part of as an instance method. /// - /// The + /// The /// The remaining bytes - private void InitStreamReadingImpl(OrigJpegDecoderCore decoder, int remaining) + private void InitStreamReadingImpl(GolangJpegDecoderCore decoder, int remaining) { if (decoder.ComponentCount == 0) { @@ -360,7 +360,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder /// /// The decoder /// The index of the scan - private void DecodeBlock(OrigJpegDecoderCore decoder, int scanIndex) + private void DecodeBlock(GolangJpegDecoderCore decoder, int scanIndex) { Block8x8* b = this.pointers.Block; int huffmannIdx = (OrigHuffmanTree.AcTableIndex * OrigHuffmanTree.ThRowSize) + this.pointers.ComponentScan[scanIndex].AcTableSelector; @@ -475,7 +475,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder this.eobRun |= bitsResult; } - private void InitComponentScan(OrigJpegDecoderCore decoder, int i, ref OrigComponentScan currentComponentScan, ref int totalHv) + private void InitComponentScan(GolangJpegDecoderCore decoder, int i, ref OrigComponentScan currentComponentScan, ref int totalHv) { // Component selector. int cs = decoder.Temp[1 + (2 * i)]; @@ -500,7 +500,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder } private void ProcessComponentImpl( - OrigJpegDecoderCore decoder, + GolangJpegDecoderCore decoder, int i, ref OrigComponentScan currentComponentScan, ref int totalHv, diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/GolangJpegDecoderCore.cs similarity index 94% rename from src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs rename to src/ImageSharp/Formats/Jpeg/GolangPort/GolangJpegDecoderCore.cs index 875f16ec2..688e0afbf 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/GolangJpegDecoderCore.cs @@ -19,7 +19,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort /// /// Performs the jpeg decoding operation. /// - internal sealed unsafe class OrigJpegDecoderCore : IRawJpegData + internal sealed unsafe class GolangJpegDecoderCore : IRawJpegData { /// /// The maximum number of color components @@ -40,7 +40,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort #pragma warning disable SA1401 // FieldsMustBePrivate /// - /// Encapsulates stream reading and processing data and operations for . + /// Encapsulates stream reading and processing data and operations for . /// It's a value type for improved data locality, and reduced number of CALLVIRT-s /// public InputProcessor InputProcessor; @@ -79,11 +79,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort private AdobeMarker adobe; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The configuration. /// The options. - public OrigJpegDecoderCore(Configuration configuration, IJpegDecoderOptions options) + public GolangJpegDecoderCore(Configuration configuration, IJpegDecoderOptions options) { this.IgnoreMetadata = options.IgnoreMetadata; this.configuration = configuration ?? Configuration.Default; @@ -238,7 +238,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort // Check for the Start Of Image marker. this.InputProcessor.ReadFull(this.Temp, 0, 2); - if (this.Temp[0] != OrigJpegConstants.Markers.XFF || this.Temp[1] != OrigJpegConstants.Markers.SOI) + if (this.Temp[0] != JpegConstants.Markers.XFF || this.Temp[1] != JpegConstants.Markers.SOI) { throw new ImageFormatException("Missing SOI marker."); } @@ -302,12 +302,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort } // End Of Image. - if (marker == OrigJpegConstants.Markers.EOI) + if (marker == JpegConstants.Markers.EOI) { break; } - if (marker >= OrigJpegConstants.Markers.RST0 && marker <= OrigJpegConstants.Markers.RST7) + if (marker >= JpegConstants.Markers.RST0 && marker <= JpegConstants.Markers.RST7) { // Figures B.2 and B.16 of the specification suggest that restart markers should // only occur between Entropy Coded Segments and not after the final ECS. @@ -329,14 +329,14 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort switch (marker) { - case OrigJpegConstants.Markers.SOF0: - case OrigJpegConstants.Markers.SOF1: - case OrigJpegConstants.Markers.SOF2: - this.IsProgressive = marker == OrigJpegConstants.Markers.SOF2; + case JpegConstants.Markers.SOF0: + case JpegConstants.Markers.SOF1: + case JpegConstants.Markers.SOF2: + this.IsProgressive = marker == JpegConstants.Markers.SOF2; this.ProcessStartOfFrameMarker(remaining, metadataOnly); break; - case OrigJpegConstants.Markers.DHT: + case JpegConstants.Markers.DHT: if (metadataOnly) { this.InputProcessor.Skip(remaining); @@ -347,7 +347,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort } break; - case OrigJpegConstants.Markers.DQT: + case JpegConstants.Markers.DQT: if (metadataOnly) { this.InputProcessor.Skip(remaining); @@ -358,7 +358,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort } break; - case OrigJpegConstants.Markers.SOS: + case JpegConstants.Markers.SOS: if (!metadataOnly) { this.ProcessStartOfScanMarker(remaining); @@ -377,7 +377,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort break; - case OrigJpegConstants.Markers.DRI: + case JpegConstants.Markers.DRI: if (metadataOnly) { this.InputProcessor.Skip(remaining); @@ -388,21 +388,21 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort } break; - case OrigJpegConstants.Markers.APP0: + case JpegConstants.Markers.APP0: this.ProcessApplicationHeaderMarker(remaining); break; - case OrigJpegConstants.Markers.APP1: + case JpegConstants.Markers.APP1: this.ProcessApp1Marker(remaining); break; - case OrigJpegConstants.Markers.APP2: + case JpegConstants.Markers.APP2: this.ProcessApp2Marker(remaining); break; - case OrigJpegConstants.Markers.APP14: + case JpegConstants.Markers.APP14: this.ProcessApp14Marker(remaining); break; default: - if ((marker >= OrigJpegConstants.Markers.APP0 && marker <= OrigJpegConstants.Markers.APP15) - || marker == OrigJpegConstants.Markers.COM) + if ((marker >= JpegConstants.Markers.APP0 && marker <= JpegConstants.Markers.APP15) + || marker == JpegConstants.Markers.COM) { this.InputProcessor.Skip(remaining); } @@ -779,19 +779,19 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort case 1: return JpegColorSpace.Grayscale; case 3: - if (!this.isAdobe || this.adobe.ColorTransform == OrigJpegConstants.Adobe.ColorTransformYCbCr) + if (!this.isAdobe || this.adobe.ColorTransform == JpegConstants.Adobe.ColorTransformYCbCr) { return JpegColorSpace.YCbCr; } - if (this.adobe.ColorTransform == OrigJpegConstants.Adobe.ColorTransformUnknown) + if (this.adobe.ColorTransform == JpegConstants.Adobe.ColorTransformUnknown) { return JpegColorSpace.RGB; } break; case 4: - if (this.adobe.ColorTransform == OrigJpegConstants.Adobe.ColorTransformYcck) + if (this.adobe.ColorTransform == JpegConstants.Adobe.ColorTransformYcck) { return JpegColorSpace.Ycck; } diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/JpegEncoderCore.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/JpegEncoderCore.cs index 4fbb20ee8..7d8a4bb5c 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/JpegEncoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/JpegEncoderCore.cs @@ -1,17 +1,14 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System.Buffers; using System.IO; using System.Runtime.CompilerServices; using SixLabors.ImageSharp.Formats.Jpeg.Common; using SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components; using SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Encoder; -using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.MetaData.Profiles.Exif; using SixLabors.ImageSharp.MetaData.Profiles.Icc; using SixLabors.ImageSharp.PixelFormats; -using Block8x8F = SixLabors.ImageSharp.Formats.Jpeg.Common.Block8x8F; namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort { @@ -58,7 +55,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort /// private static readonly byte[] SosHeaderYCbCr = { - OrigJpegConstants.Markers.XFF, OrigJpegConstants.Markers.SOS, + JpegConstants.Markers.XFF, JpegConstants.Markers.SOS, // Marker 0x00, 0x0c, @@ -190,7 +187,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort Guard.NotNull(image, nameof(image)); Guard.NotNull(stream, nameof(stream)); - ushort max = OrigJpegConstants.MaxLength; + ushort max = JpegConstants.MaxLength; if (image.Width >= max || image.Height >= max) { throw new ImageFormatException($"Image is too large to encode at {image.Width}x{image.Height}."); @@ -234,8 +231,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort this.WriteStartOfScan(image); // Write the End Of Image marker. - this.buffer[0] = OrigJpegConstants.Markers.XFF; - this.buffer[1] = OrigJpegConstants.Markers.EOI; + this.buffer[0] = JpegConstants.Markers.XFF; + this.buffer[1] = JpegConstants.Markers.EOI; stream.Write(this.buffer, 0, 2); stream.Flush(); } @@ -382,18 +379,18 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort { // TODO: Need a JpegScanEncoder class or struct that encapsulates the scan-encoding implementation. (Similar to JpegScanDecoder.) // (Partially done with YCbCrForwardConverter) - Block8x8F temp1 = default(Block8x8F); - Block8x8F temp2 = default(Block8x8F); + Block8x8F temp1 = default; + Block8x8F temp2 = default; Block8x8F onStackLuminanceQuantTable = this.luminanceQuantTable; Block8x8F onStackChrominanceQuantTable = this.chrominanceQuantTable; - ZigZag unzig = ZigZag.CreateUnzigTable(); + var unzig = ZigZag.CreateUnzigTable(); // ReSharper disable once InconsistentNaming int prevDCY = 0, prevDCCb = 0, prevDCCr = 0; - YCbCrForwardConverter pixelConverter = YCbCrForwardConverter.Create(); + var pixelConverter = YCbCrForwardConverter.Create(); for (int y = 0; y < pixels.Height; y += 8) { @@ -437,12 +434,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort private void WriteApplicationHeader(short horizontalResolution, short verticalResolution) { // Write the start of image marker. Markers are always prefixed with with 0xff. - this.buffer[0] = OrigJpegConstants.Markers.XFF; - this.buffer[1] = OrigJpegConstants.Markers.SOI; + this.buffer[0] = JpegConstants.Markers.XFF; + this.buffer[1] = JpegConstants.Markers.SOI; // Write the JFIF headers - this.buffer[2] = OrigJpegConstants.Markers.XFF; - this.buffer[3] = OrigJpegConstants.Markers.APP0; // Application Marker + this.buffer[2] = JpegConstants.Markers.XFF; + this.buffer[3] = JpegConstants.Markers.APP0; // Application Marker this.buffer[4] = 0x00; this.buffer[5] = 0x10; this.buffer[6] = 0x4a; // J @@ -502,7 +499,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort this.EmitHuffRLE((HuffIndex)((2 * (int)index) + 0), 0, dc - prevDC); // Emit the AC components. - HuffIndex h = (HuffIndex)((2 * (int)index) + 1); + var h = (HuffIndex)((2 * (int)index) + 1); int runLength = 0; for (int zig = 1; zig < Block8x8F.Size; zig++) @@ -556,7 +553,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort markerlen += 1 + 16 + s.Values.Length; } - this.WriteMarkerHeader(OrigJpegConstants.Markers.DHT, markerlen); + this.WriteMarkerHeader(JpegConstants.Markers.DHT, markerlen); for (int i = 0; i < specs.Length; i++) { HuffmanSpec spec = specs[i]; @@ -590,7 +587,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort { // Marker + quantization table lengths int markerlen = 2 + (QuantizationTableCount * (1 + Block8x8F.Size)); - this.WriteMarkerHeader(OrigJpegConstants.Markers.DQT, markerlen); + this.WriteMarkerHeader(JpegConstants.Markers.DQT, markerlen); // Loop through and collect the tables as one array. // This allows us to reduce the number of writes to the stream. @@ -627,8 +624,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort int length = data.Length + 2; - this.buffer[0] = OrigJpegConstants.Markers.XFF; - this.buffer[1] = OrigJpegConstants.Markers.APP1; // Application Marker + this.buffer[0] = JpegConstants.Markers.XFF; + this.buffer[1] = JpegConstants.Markers.APP1; // Application Marker this.buffer[2] = (byte)((length >> 8) & 0xFF); this.buffer[3] = (byte)(length & 0xFF); @@ -686,8 +683,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort dataLength -= length; - this.buffer[0] = OrigJpegConstants.Markers.XFF; - this.buffer[1] = OrigJpegConstants.Markers.APP2; // Application Marker + this.buffer[0] = JpegConstants.Markers.XFF; + this.buffer[1] = JpegConstants.Markers.APP2; // Application Marker int markerLength = length + 16; this.buffer[2] = (byte)((markerLength >> 8) & 0xFF); this.buffer[3] = (byte)(markerLength & 0xFF); @@ -759,7 +756,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort // Length (high byte, low byte), 8 + components * 3. int markerlen = 8 + (3 * componentCount); - this.WriteMarkerHeader(OrigJpegConstants.Markers.SOF0, markerlen); + this.WriteMarkerHeader(JpegConstants.Markers.SOF0, markerlen); this.buffer[0] = 8; // Data Precision. 8 for now, 12 and 16 bit jpegs not supported this.buffer[1] = (byte)(height >> 8); this.buffer[2] = (byte)(height & 0xff); // (2 bytes, Hi-Lo), must be > 0 if DNL not supported @@ -827,20 +824,20 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort where TPixel : struct, IPixel { // TODO: Need a JpegScanEncoder class or struct that encapsulates the scan-encoding implementation. (Similar to JpegScanDecoder.) - Block8x8F b = default(Block8x8F); + Block8x8F b = default; - BlockQuad cb = default(BlockQuad); - BlockQuad cr = default(BlockQuad); - Block8x8F* cbPtr = (Block8x8F*)cb.Data; - Block8x8F* crPtr = (Block8x8F*)cr.Data; + BlockQuad cb = default; + BlockQuad cr = default; + var cbPtr = (Block8x8F*)cb.Data; + var crPtr = (Block8x8F*)cr.Data; - Block8x8F temp1 = default(Block8x8F); - Block8x8F temp2 = default(Block8x8F); + Block8x8F temp1 = default; + Block8x8F temp2 = default; Block8x8F onStackLuminanceQuantTable = this.luminanceQuantTable; Block8x8F onStackChrominanceQuantTable = this.chrominanceQuantTable; - ZigZag unzig = ZigZag.CreateUnzigTable(); + var unzig = ZigZag.CreateUnzigTable(); var pixelConverter = YCbCrForwardConverter.Create(); @@ -902,7 +899,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort private void WriteMarkerHeader(byte marker, int length) { // Markers are always prefixed with with 0xff. - this.buffer[0] = OrigJpegConstants.Markers.XFF; + this.buffer[0] = JpegConstants.Markers.XFF; this.buffer[1] = marker; this.buffer[2] = (byte)(length >> 8); this.buffer[3] = (byte)(length & 0xff); diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegConstants.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegConstants.cs deleted file mode 100644 index be383d212..000000000 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegConstants.cs +++ /dev/null @@ -1,189 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using System.Collections.Generic; - -namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort -{ - /// - /// Defines jpeg constants defined in the specification. - /// - internal static class OrigJpegConstants - { - /// - /// The maximum allowable length in each dimension of a jpeg image. - /// - public const ushort MaxLength = 65535; - - /// - /// The list of mimetypes that equate to a jpeg. - /// - public static readonly IEnumerable MimeTypes = new[] { "image/jpeg", "image/pjpeg" }; - - /// - /// The list of file extensions that equate to a jpeg. - /// - public static readonly IEnumerable FileExtensions = new[] { "jpg", "jpeg", "jfif" }; - - /// - /// Describes common Jpeg markers - /// - internal static class Markers - { - /// - /// Marker prefix. Next byte is a marker. - /// - public const byte XFF = 0xff; - - /// - /// Same as but of type - /// - public const int XFFInt = XFF; - - /// - /// Start of Image - /// - public const byte SOI = 0xd8; - - /// - /// Start of Frame (baseline DCT) - /// - /// Indicates that this is a baseline DCT-based JPEG, and specifies the width, height, number of components, - /// and component subsampling (e.g., 4:2:0). - /// - /// - public const byte SOF0 = 0xc0; - - /// - /// Start Of Frame (Extended Sequential DCT) - /// - /// Indicates that this is a progressive DCT-based JPEG, and specifies the width, height, number of components, - /// and component subsampling (e.g., 4:2:0). - /// - /// - public const byte SOF1 = 0xc1; - - /// - /// Start Of Frame (progressive DCT) - /// - /// Indicates that this is a progressive DCT-based JPEG, and specifies the width, height, number of components, - /// and component subsampling (e.g., 4:2:0). - /// - /// - public const byte SOF2 = 0xc2; - - /// - /// Define Huffman Table(s) - /// - /// Specifies one or more Huffman tables. - /// - /// - public const byte DHT = 0xc4; - - /// - /// Define Quantization Table(s) - /// - /// Specifies one or more quantization tables. - /// - /// - public const byte DQT = 0xdb; - - /// - /// Define Restart Interval - /// - /// Specifies the interval between RSTn markers, in macroblocks. This marker is followed by two bytes - /// indicating the fixed size so it can be treated like any other variable size segment. - /// - /// - public const byte DRI = 0xdd; - - /// - /// Define First Restart - /// - /// Inserted every r macroblocks, where r is the restart interval set by a DRI marker. - /// Not used if there was no DRI marker. The low three bits of the marker code cycle in value from 0 to 7. - /// - /// - public const byte RST0 = 0xd0; - - /// - /// Define Eigth Restart - /// - /// Inserted every r macroblocks, where r is the restart interval set by a DRI marker. - /// Not used if there was no DRI marker. The low three bits of the marker code cycle in value from 0 to 7. - /// - /// - public const byte RST7 = 0xd7; - - /// - /// Start of Scan - /// - /// Begins a top-to-bottom scan of the image. In baseline DCT JPEG images, there is generally a single scan. - /// Progressive DCT JPEG images usually contain multiple scans. This marker specifies which slice of data it - /// will contain, and is immediately followed by entropy-coded data. - /// - /// - public const byte SOS = 0xda; - - /// - /// Comment - /// - /// Contains a text comment. - /// - /// - public const byte COM = 0xfe; - - /// - /// End of Image - /// - public const byte EOI = 0xd9; - - /// - /// Application specific marker for marking the jpeg format. - /// - /// - public const byte APP0 = 0xe0; - - /// - /// Application specific marker for marking where to store metadata. - /// - public const byte APP1 = 0xe1; - - /// - /// Application specific marker for marking where to store ICC profile information. - /// - public const byte APP2 = 0xe2; - - /// - /// Application specific marker used by Adobe for storing encoding information for DCT filters. - /// - public const byte APP14 = 0xee; - - /// - /// Application specific marker used by GraphicConverter to store JPEG quality. - /// - public const byte APP15 = 0xef; - } - - /// - /// Describes Adobe specific markers - /// - internal static class Adobe - { - /// - /// The color transform is unknown.(RGB or CMYK) - /// - public const int ColorTransformUnknown = 0; - - /// - /// The color transform is YCbCr (luminance, red chroma, blue chroma) - /// - public const int ColorTransformYCbCr = 1; - - /// - /// The color transform is YCCK (luminance, red chroma, blue chroma, keyline) - /// - public const int ColorTransformYcck = 2; - } - } -} \ No newline at end of file diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoder.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoder.cs index bf2f64b34..03afa770f 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoder.cs @@ -22,7 +22,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort { Guard.NotNull(stream, nameof(stream)); - using (var decoder = new OrigJpegDecoderCore(configuration, this)) + using (var decoder = new GolangJpegDecoderCore(configuration, this)) { return decoder.Decode(stream); } @@ -33,7 +33,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort { Guard.NotNull(stream, nameof(stream)); - using (var decoder = new OrigJpegDecoderCore(configuration, this)) + using (var decoder = new GolangJpegDecoderCore(configuration, this)) { return decoder.Identify(stream); } diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs index 91835b5d7..788e97e14 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs @@ -24,7 +24,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg { Guard.NotNull(stream, nameof(stream)); - using (var decoder = new OrigJpegDecoderCore(configuration, this)) + using (var decoder = new GolangJpegDecoderCore(configuration, this)) { return decoder.Decode(stream); } @@ -35,7 +35,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg { Guard.NotNull(stream, "stream"); - using (var decoder = new OrigJpegDecoderCore(configuration, this)) + using (var decoder = new GolangJpegDecoderCore(configuration, this)) { return decoder.Identify(stream); } diff --git a/src/ImageSharp/Formats/Jpeg/JpegFormat.cs b/src/ImageSharp/Formats/Jpeg/JpegFormat.cs index 4f368dcde..51d582499 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegFormat.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegFormat.cs @@ -2,7 +2,8 @@ // Licensed under the Apache License, Version 2.0. using System.Collections.Generic; -using SixLabors.ImageSharp.Formats.Jpeg.GolangPort; + +using SixLabors.ImageSharp.Formats.Jpeg.Common; namespace SixLabors.ImageSharp.Formats.Jpeg { @@ -18,9 +19,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg public string DefaultMimeType => "image/jpeg"; /// - public IEnumerable MimeTypes => OrigJpegConstants.MimeTypes; + public IEnumerable MimeTypes => JpegConstants.MimeTypes; /// - public IEnumerable FileExtensions => OrigJpegConstants.FileExtensions; + public IEnumerable FileExtensions => JpegConstants.FileExtensions; } } \ No newline at end of file diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs index c6b14d6fb..8b6282e4e 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs @@ -136,7 +136,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components byte marker = fileMarker.Marker; // RSTn - We've already read the bytes and altered the position so no need to skip - if (marker >= PdfJsJpegConstants.Markers.RST0 && marker <= PdfJsJpegConstants.Markers.RST7) + if (marker >= JpegConstants.Markers.RST0 && marker <= JpegConstants.Markers.RST7) { continue; } @@ -452,7 +452,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components this.endOfStreamReached = true; return false; - case PdfJsJpegConstants.Markers.Prefix: + case JpegConstants.Markers.XFF: int nextByte = stream.ReadByte(); if (nextByte == -0x1) diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs index df803a920..67d921ef1 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs @@ -150,20 +150,20 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort if (value == 0) { - return new PdfJsFileMarker(PdfJsJpegConstants.Markers.EOI, stream.Length - 2); + return new PdfJsFileMarker(JpegConstants.Markers.EOI, stream.Length - 2); } - if (marker[0] == PdfJsJpegConstants.Markers.Prefix) + if (marker[0] == JpegConstants.Markers.XFF) { // According to Section B.1.1.2: // "Any marker may optionally be preceded by any number of fill bytes, which are bytes assigned code 0xFF." int m = marker[1]; - while (m == PdfJsJpegConstants.Markers.Prefix) + while (m == JpegConstants.Markers.XFF) { int suffix = stream.ReadByte(); if (suffix == -1) { - return new PdfJsFileMarker(PdfJsJpegConstants.Markers.EOI, stream.Length - 2); + return new PdfJsFileMarker(JpegConstants.Markers.EOI, stream.Length - 2); } m = suffix; @@ -213,7 +213,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort // Check for the Start Of Image marker. this.InputStream.Read(this.markerBuffer, 0, 2); var fileMarker = new PdfJsFileMarker(this.markerBuffer[1], 0); - if (fileMarker.Marker != PdfJsJpegConstants.Markers.SOI) + if (fileMarker.Marker != JpegConstants.Markers.SOI) { throw new ImageFormatException("Missing SOI marker."); } @@ -230,7 +230,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort this.acHuffmanTables = new PdfJsHuffmanTables(); } - while (fileMarker.Marker != PdfJsJpegConstants.Markers.EOI) + while (fileMarker.Marker != JpegConstants.Markers.EOI) { if (!fileMarker.Invalid) { @@ -239,13 +239,13 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort switch (fileMarker.Marker) { - case PdfJsJpegConstants.Markers.SOF0: - case PdfJsJpegConstants.Markers.SOF1: - case PdfJsJpegConstants.Markers.SOF2: + case JpegConstants.Markers.SOF0: + case JpegConstants.Markers.SOF1: + case JpegConstants.Markers.SOF2: this.ProcessStartOfFrameMarker(remaining, fileMarker, metadataOnly); break; - case PdfJsJpegConstants.Markers.SOS: + case JpegConstants.Markers.SOS: if (!metadataOnly) { this.ProcessStartOfScanMarker(); @@ -258,7 +258,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort return; } - case PdfJsJpegConstants.Markers.DHT: + case JpegConstants.Markers.DHT: if (metadataOnly) { this.InputStream.Skip(remaining); @@ -270,7 +270,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort break; - case PdfJsJpegConstants.Markers.DQT: + case JpegConstants.Markers.DQT: if (metadataOnly) { this.InputStream.Skip(remaining); @@ -282,7 +282,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort break; - case PdfJsJpegConstants.Markers.DRI: + case JpegConstants.Markers.DRI: if (metadataOnly) { this.InputStream.Skip(remaining); @@ -294,38 +294,38 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort break; - case PdfJsJpegConstants.Markers.APP0: + case JpegConstants.Markers.APP0: this.ProcessApplicationHeaderMarker(remaining); break; - case PdfJsJpegConstants.Markers.APP1: + case JpegConstants.Markers.APP1: this.ProcessApp1Marker(remaining); break; - case PdfJsJpegConstants.Markers.APP2: + case JpegConstants.Markers.APP2: this.ProcessApp2Marker(remaining); break; - case PdfJsJpegConstants.Markers.APP3: - case PdfJsJpegConstants.Markers.APP4: - case PdfJsJpegConstants.Markers.APP5: - case PdfJsJpegConstants.Markers.APP6: - case PdfJsJpegConstants.Markers.APP7: - case PdfJsJpegConstants.Markers.APP8: - case PdfJsJpegConstants.Markers.APP9: - case PdfJsJpegConstants.Markers.APP10: - case PdfJsJpegConstants.Markers.APP11: - case PdfJsJpegConstants.Markers.APP12: - case PdfJsJpegConstants.Markers.APP13: + case JpegConstants.Markers.APP3: + case JpegConstants.Markers.APP4: + case JpegConstants.Markers.APP5: + case JpegConstants.Markers.APP6: + case JpegConstants.Markers.APP7: + case JpegConstants.Markers.APP8: + case JpegConstants.Markers.APP9: + case JpegConstants.Markers.APP10: + case JpegConstants.Markers.APP11: + case JpegConstants.Markers.APP12: + case JpegConstants.Markers.APP13: this.InputStream.Skip(remaining); break; - case PdfJsJpegConstants.Markers.APP14: + case JpegConstants.Markers.APP14: this.ProcessApp14Marker(remaining); break; - case PdfJsJpegConstants.Markers.APP15: - case PdfJsJpegConstants.Markers.COM: + case JpegConstants.Markers.APP15: + case JpegConstants.Markers.COM: this.InputStream.Skip(remaining); break; } @@ -362,11 +362,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort if (this.ComponentCount == 3) { - if (this.adobe.Equals(default) || this.adobe.ColorTransform == PdfJsJpegConstants.Markers.Adobe.ColorTransformYCbCr) + if (this.adobe.Equals(default) || this.adobe.ColorTransform == JpegConstants.Adobe.ColorTransformYCbCr) { return JpegColorSpace.YCbCr; } - else if (this.adobe.ColorTransform == PdfJsJpegConstants.Markers.Adobe.ColorTransformUnknown) + else if (this.adobe.ColorTransform == JpegConstants.Adobe.ColorTransformUnknown) { return JpegColorSpace.RGB; } @@ -374,7 +374,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort if (this.ComponentCount == 4) { - return this.adobe.ColorTransform == PdfJsJpegConstants.Markers.Adobe.ColorTransformYcck + return this.adobe.ColorTransform == JpegConstants.Adobe.ColorTransformYcck ? JpegColorSpace.Ycck : JpegColorSpace.Cmyk; } @@ -622,8 +622,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort this.Frame = new PdfJsFrame { - Extended = frameMarker.Marker == PdfJsJpegConstants.Markers.SOF1, - Progressive = frameMarker.Marker == PdfJsJpegConstants.Markers.SOF2, + Extended = frameMarker.Marker == JpegConstants.Markers.SOF1, + Progressive = frameMarker.Marker == JpegConstants.Markers.SOF2, Precision = this.temp[0], Scanlines = (short)((this.temp[1] << 8) | this.temp[2]), SamplesPerLine = (short)((this.temp[3] << 8) | this.temp[4]), diff --git a/tests/ImageSharp.Tests/Formats/Jpg/AdobeMarkerTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/AdobeMarkerTests.cs index 2ee9498e0..7b0a0a7b1 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/AdobeMarkerTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/AdobeMarkerTests.cs @@ -1,8 +1,8 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using SixLabors.ImageSharp.Formats.Jpeg.Common; using SixLabors.ImageSharp.Formats.Jpeg.Common.Decoder; -using SixLabors.ImageSharp.Formats.Jpeg.GolangPort; using Xunit; @@ -25,29 +25,29 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg [Fact] public void MarkerReturnsCorrectParsedValue() { - bool isAdobe = AdobeMarker.TryParse(this.bytes, out var marker); + bool isAdobe = AdobeMarker.TryParse(this.bytes, out AdobeMarker marker); Assert.True(isAdobe); Assert.Equal(100, marker.DCTEncodeVersion); Assert.Equal(0, marker.APP14Flags0); Assert.Equal(0, marker.APP14Flags1); - Assert.Equal(OrigJpegConstants.Adobe.ColorTransformYcck, marker.ColorTransform); + Assert.Equal(JpegConstants.Adobe.ColorTransformYcck, marker.ColorTransform); } [Fact] public void MarkerIgnoresIncorrectValue() { - bool isAdobe = AdobeMarker.TryParse(new byte[] { 0, 0, 0, 0 }, out var marker); + bool isAdobe = AdobeMarker.TryParse(new byte[] { 0, 0, 0, 0 }, out AdobeMarker marker); Assert.False(isAdobe); - Assert.Equal(default(AdobeMarker), marker); + Assert.Equal(default, marker); } [Fact] public void MarkerEqualityIsCorrect() { - AdobeMarker.TryParse(this.bytes, out var marker); - AdobeMarker.TryParse(this.bytes, out var marker2); + AdobeMarker.TryParse(this.bytes, out AdobeMarker marker); + AdobeMarker.TryParse(this.bytes, out AdobeMarker marker2); Assert.True(marker.Equals(marker2)); } @@ -55,8 +55,8 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg [Fact] public void MarkerInEqualityIsCorrect() { - AdobeMarker.TryParse(this.bytes, out var marker); - AdobeMarker.TryParse(this.bytes2, out var marker2); + AdobeMarker.TryParse(this.bytes, out AdobeMarker marker); + AdobeMarker.TryParse(this.bytes2, out AdobeMarker marker2); Assert.False(marker.Equals(marker2)); } @@ -64,8 +64,8 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg [Fact] public void MarkerHashCodeIsReplicable() { - AdobeMarker.TryParse(this.bytes, out var marker); - AdobeMarker.TryParse(this.bytes, out var marker2); + AdobeMarker.TryParse(this.bytes, out AdobeMarker marker); + AdobeMarker.TryParse(this.bytes, out AdobeMarker marker2); Assert.True(marker.GetHashCode().Equals(marker2.GetHashCode())); } @@ -73,8 +73,8 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg [Fact] public void MarkerHashCodeIsUnique() { - AdobeMarker.TryParse(this.bytes, out var marker); - AdobeMarker.TryParse(this.bytes2, out var marker2); + AdobeMarker.TryParse(this.bytes, out AdobeMarker marker); + AdobeMarker.TryParse(this.bytes2, out AdobeMarker marker2); Assert.False(marker.GetHashCode().Equals(marker2.GetHashCode())); } diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegImagePostProcessorTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegImagePostProcessorTests.cs index ffaccb3f7..4eea6a74d 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegImagePostProcessorTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegImagePostProcessorTests.cs @@ -57,7 +57,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg where TPixel : struct, IPixel { string imageFile = provider.SourceFileOrDescription; - using (OrigJpegDecoderCore decoder = JpegFixture.ParseStream(imageFile)) + using (GolangJpegDecoderCore decoder = JpegFixture.ParseStream(imageFile)) using (var pp = new JpegImagePostProcessor(Configuration.Default.MemoryManager, decoder)) using (var imageFrame = new ImageFrame(Configuration.Default.MemoryManager, decoder.ImageWidth, decoder.ImageHeight)) { @@ -79,7 +79,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg where TPixel : struct, IPixel { string imageFile = provider.SourceFileOrDescription; - using (OrigJpegDecoderCore decoder = JpegFixture.ParseStream(imageFile)) + using (GolangJpegDecoderCore decoder = JpegFixture.ParseStream(imageFile)) using (var pp = new JpegImagePostProcessor(Configuration.Default.MemoryManager, decoder)) using (var image = new Image(decoder.ImageWidth, decoder.ImageHeight)) { diff --git a/tests/ImageSharp.Tests/Formats/Jpg/ParseStreamTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/ParseStreamTests.cs index b665d69e8..f5ca5076a 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/ParseStreamTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/ParseStreamTests.cs @@ -33,7 +33,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg { var expecteColorSpace = (JpegColorSpace)expectedColorSpaceValue; - using (OrigJpegDecoderCore decoder = JpegFixture.ParseStream(imageFile, false)) + using (GolangJpegDecoderCore decoder = JpegFixture.ParseStream(imageFile, false)) { Assert.Equal(expecteColorSpace, decoder.ColorSpace); } @@ -42,7 +42,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg [Fact] public void ComponentScalingIsCorrect_1ChannelJpeg() { - using (OrigJpegDecoderCore decoder = JpegFixture.ParseStream(TestImages.Jpeg.Baseline.Jpeg400, false)) + using (GolangJpegDecoderCore decoder = JpegFixture.ParseStream(TestImages.Jpeg.Baseline.Jpeg400, false)) { Assert.Equal(1, decoder.ComponentCount); Assert.Equal(1, decoder.Components.Length); @@ -68,7 +68,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg { var sb = new StringBuilder(); - using (OrigJpegDecoderCore decoder = JpegFixture.ParseStream(imageFile, false)) + using (GolangJpegDecoderCore decoder = JpegFixture.ParseStream(imageFile, false)) { sb.AppendLine(imageFile); sb.AppendLine($"Size:{decoder.ImageSizeInPixels} MCU:{decoder.ImageSizeInMCU}"); @@ -103,7 +103,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg Size fLuma = (Size)expectedLumaFactors; Size fChroma = (Size)expectedChromaFactors; - using (OrigJpegDecoderCore decoder = JpegFixture.ParseStream(imageFile, false)) + using (GolangJpegDecoderCore decoder = JpegFixture.ParseStream(imageFile, false)) { Assert.Equal(componentCount, decoder.ComponentCount); Assert.Equal(componentCount, decoder.Components.Length); diff --git a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs index 9c134ada9..26b9a06cb 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs @@ -63,7 +63,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg public void OriginalDecoder_ParseStream_SaveSpectralResult(TestImageProvider provider) where TPixel : struct, IPixel { - var decoder = new OrigJpegDecoderCore(Configuration.Default, new JpegDecoder()); + var decoder = new GolangJpegDecoderCore(Configuration.Default, new JpegDecoder()); byte[] sourceBytes = TestFile.Create(provider.SourceFileOrDescription).Bytes; @@ -153,7 +153,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg return; } - var decoder = new OrigJpegDecoderCore(Configuration.Default, new JpegDecoder()); + var decoder = new GolangJpegDecoderCore(Configuration.Default, new JpegDecoder()); byte[] sourceBytes = TestFile.Create(provider.SourceFileOrDescription).Bytes; diff --git a/tests/ImageSharp.Tests/Formats/Jpg/Utils/JpegFixture.cs b/tests/ImageSharp.Tests/Formats/Jpg/Utils/JpegFixture.cs index 3e66af50a..ea11d395d 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/Utils/JpegFixture.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/Utils/JpegFixture.cs @@ -174,12 +174,12 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg.Utils Assert.False(failed); } - internal static OrigJpegDecoderCore ParseStream(string testFileName, bool metaDataOnly = false) + internal static GolangJpegDecoderCore ParseStream(string testFileName, bool metaDataOnly = false) { byte[] bytes = TestFile.Create(testFileName).Bytes; using (var ms = new MemoryStream(bytes)) { - var decoder = new OrigJpegDecoderCore(Configuration.Default, new JpegDecoder()); + var decoder = new GolangJpegDecoderCore(Configuration.Default, new JpegDecoder()); decoder.ParseStream(ms, metaDataOnly); return decoder; } diff --git a/tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.SpectralData.cs b/tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.SpectralData.cs index d11b3d79b..8aa10a96c 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.SpectralData.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.SpectralData.cs @@ -37,7 +37,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg.Utils return new SpectralData(destComponents); } - public static SpectralData LoadFromImageSharpDecoder(OrigJpegDecoderCore decoder) + public static SpectralData LoadFromImageSharpDecoder(GolangJpegDecoderCore decoder) { OrigComponent[] srcComponents = decoder.Components; LibJpegTools.ComponentData[] destComponents = srcComponents.Select(LibJpegTools.ComponentData.Load).ToArray();