From 47be2ba79a35ac217f2934559fe8e09ebaf4ae21 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 26 Aug 2021 21:38:46 +1000 Subject: [PATCH 1/2] Revert "Update JpegEncoderCore.cs" This reverts commit f25ce22ed85b4c7a7d481af5d92e6101632eac5d. --- .../Formats/Jpeg/JpegEncoderCore.cs | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs index 14e7cc677..3fd1a3c48 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs @@ -167,10 +167,14 @@ namespace SixLabors.ImageSharp.Formats.Jpeg /// The color type. /// true, if color type is supported. private static bool IsSupportedColorType(JpegColorType? colorType) - => colorType == JpegColorType.YCbCrRatio444 - || colorType == JpegColorType.YCbCrRatio420 - || colorType == JpegColorType.Luminance - || colorType == JpegColorType.Rgb; + { + if (colorType == JpegColorType.YCbCrRatio444 || colorType == JpegColorType.YCbCrRatio420 || colorType == JpegColorType.Luminance || colorType == JpegColorType.Rgb) + { + return true; + } + + return false; + } /// /// Gets the component ids. @@ -567,14 +571,14 @@ namespace SixLabors.ImageSharp.Formats.Jpeg // This uses a C#'s compiler optimization that refers to the static data segment of the assembly, // and doesn't incur any allocation at all. // "default" to 4:2:0 - ReadOnlySpan subsamples = stackalloc byte[] + ReadOnlySpan subsamples = new byte[] { 0x22, 0x11, 0x11 }; - ReadOnlySpan chroma = stackalloc byte[] + ReadOnlySpan chroma = new byte[] { 0x00, 0x01, @@ -583,7 +587,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg if (this.colorType == JpegColorType.Luminance) { - subsamples = stackalloc byte[] + subsamples = new byte[] { 0x11, 0x00, @@ -596,7 +600,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg { case JpegColorType.YCbCrRatio444: case JpegColorType.Rgb: - subsamples = stackalloc byte[] + subsamples = new byte[] { 0x11, 0x11, @@ -605,7 +609,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg if (this.colorType == JpegColorType.Rgb) { - chroma = stackalloc byte[] + chroma = new byte[] { 0x00, 0x00, @@ -615,7 +619,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg break; case JpegColorType.YCbCrRatio420: - subsamples = stackalloc byte[] + subsamples = new byte[] { 0x22, 0x11, @@ -658,7 +662,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg { // This uses a C#'s compiler optimization that refers to the static data segment of the assembly, // and doesn't incur any allocation at all. - ReadOnlySpan huffmanId = stackalloc byte[] + ReadOnlySpan huffmanId = new byte[] { 0x00, 0x11, @@ -668,7 +672,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg // Use the same DC/AC tables for all channels for RGB. if (this.colorType == JpegColorType.Rgb) { - huffmanId = stackalloc byte[] + huffmanId = new byte[] { 0x00, 0x00, From b4a5335a388fc26a0e9cc61f0fa2ddb097dfe787 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 26 Aug 2021 21:42:46 +1000 Subject: [PATCH 2/2] Add nint optimization --- src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs | 4 ++-- .../Encoder/RgbForwardConverter{TPixel}.cs | 2 +- src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs | 12 ++++-------- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs b/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs index d55dfced7..f669a7ad9 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs @@ -104,7 +104,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components { GuardBlockIndex(idx); ref float selfRef = ref Unsafe.As(ref this); - return Unsafe.Add(ref selfRef, idx); + return Unsafe.Add(ref selfRef, (nint)(uint)idx); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -112,7 +112,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components { GuardBlockIndex(idx); ref float selfRef = ref Unsafe.As(ref this); - Unsafe.Add(ref selfRef, idx) = value; + Unsafe.Add(ref selfRef, (nint)(uint)idx) = value; } } diff --git a/src/ImageSharp/Formats/Jpeg/Components/Encoder/RgbForwardConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Encoder/RgbForwardConverter{TPixel}.cs index 7fad63e11..0be1076e2 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Encoder/RgbForwardConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Encoder/RgbForwardConverter{TPixel}.cs @@ -103,7 +103,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder for (int i = 0; i < Block8x8F.Size; i++) { - Rgb24 c = Unsafe.Add(ref rgbStart, i); + Rgb24 c = Unsafe.Add(ref rgbStart, (nint)(uint)i); redBlock[i] = c.R; greenBlock[i] = c.G; diff --git a/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs index 3fd1a3c48..270a11ed6 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs @@ -167,14 +167,10 @@ namespace SixLabors.ImageSharp.Formats.Jpeg /// The color type. /// true, if color type is supported. private static bool IsSupportedColorType(JpegColorType? colorType) - { - if (colorType == JpegColorType.YCbCrRatio444 || colorType == JpegColorType.YCbCrRatio420 || colorType == JpegColorType.Luminance || colorType == JpegColorType.Rgb) - { - return true; - } - - return false; - } + => colorType == JpegColorType.YCbCrRatio444 + || colorType == JpegColorType.YCbCrRatio420 + || colorType == JpegColorType.Luminance + || colorType == JpegColorType.Rgb; /// /// Gets the component ids.