From e2cf90e683fe211cd1e4e3a49cadf270dd10762e Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Thu, 22 Feb 2018 20:22:53 +0100 Subject: [PATCH] YCbCrForwardConverter --- .../Components/Encoder/RgbToYCbCrTables.cs | 24 +++++++++++++++++++ .../Jpeg/GolangPort/JpegEncoderCore.cs | 23 ++++++++---------- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Encoder/RgbToYCbCrTables.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Encoder/RgbToYCbCrTables.cs index 01fe51c8d..86b3fc903 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Encoder/RgbToYCbCrTables.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Encoder/RgbToYCbCrTables.cs @@ -9,6 +9,8 @@ using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Encoder { + using System; + /// /// Provides 8-bit lookup tables for converting from Rgb to YCbCr colorspace. /// Methods to build the tables are based on libjpeg implementation. @@ -179,7 +181,29 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Encoder public void Convert(IPixelSource pixels, int x, int y) { + this.pixelBlock.LoadAndStretchEdges(pixels, x, y); + + Span rgbSpan = this.rgbBlock.AsSpanUnsafe(); + PixelOperations.Instance.ToRgb24(this.pixelBlock.AsSpanUnsafe(), rgbSpan, 64); + + ref float yBlockStart = ref Unsafe.As(ref this.Y); + ref float cbBlockStart = ref Unsafe.As(ref this.Cb); + ref float crBlockStart = ref Unsafe.As(ref this.Cr); + ref Rgb24 rgbStart = ref rgbSpan[0]; + for (int i = 0; i < 64; i++) + { + ref Rgb24 c = ref Unsafe.Add(ref rgbStart, i); + + this.colorTables.ConvertPixelInto( + c.R, + c.G, + c.B, + ref Unsafe.Add(ref yBlockStart, i), + ref Unsafe.Add(ref cbBlockStart, i), + ref Unsafe.Add(ref crBlockStart, i) + ); + } } } } \ No newline at end of file diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/JpegEncoderCore.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/JpegEncoderCore.cs index fcac30a2c..e67a3f020 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/JpegEncoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/JpegEncoderCore.cs @@ -313,11 +313,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort ref float cbBlockStart = ref Unsafe.As(ref cbBlock); ref float crBlockStart = ref Unsafe.As(ref crBlock); - float* yBlockRaw = (float*) Unsafe.AsPointer(ref yBlock); - float* cbBlockRaw = (float*)Unsafe.AsPointer(ref cbBlock); - float* crBlockRaw = (float*)Unsafe.AsPointer(ref crBlock); - - rgbBytes.Reset(); pixels.CopyRGBBytesStretchedTo(rgbBytes, y, x); ref byte data0 = ref rgbBytes.Bytes[0]; @@ -335,14 +330,14 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort int index = j8 + i; - RgbToYCbCrTables.Rgb2YCbCr(tables, yBlockRaw, cbBlockRaw, crBlockRaw, index, r, g, b); - //tables->ConvertPixelInto( - // r, - // g, - // b, - // ref Unsafe.Add(ref yBlockRaw, index), - // ref Unsafe.Add(ref cbBlockRaw, index), - // ref Unsafe.Add(ref crBlockRaw, index)); + // RgbToYCbCrTables.Rgb2YCbCr(tables, yBlockRaw, cbBlockRaw, crBlockRaw, index, r, g, b); + tables->ConvertPixelInto( + r, + g, + b, + ref Unsafe.Add(ref yBlockStart, index), + ref Unsafe.Add(ref cbBlockStart, index), + ref Unsafe.Add(ref crBlockStart, index)); dataIdx += 3; } @@ -463,6 +458,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort // ReSharper disable once InconsistentNaming int prevDCY = 0, prevDCCb = 0, prevDCCr = 0; + YCbCrForwardConverter pixelConverter = YCbCrForwardConverter.Create(); + fixed (RgbToYCbCrTables* tables = &rgbToYCbCrTables) { using (PixelArea rgbBytes = new PixelArea(8, 8, ComponentOrder.Xyz))