// Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. using System; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder { /// /// On-stack worker struct to efficiently encapsulate the TPixel -> L8 -> Y conversion chain of 8x8 pixel blocks. /// /// The pixel type to work on internal ref struct LuminanceForwardConverter where TPixel : unmanaged, IPixel { /// /// The Y component /// public Block8x8F Y; /// /// The converter /// private L8ToYConverter converter; /// /// Temporal 8x8 block to hold TPixel data /// private GenericBlock8x8 pixelBlock; /// /// Temporal RGB block /// private GenericBlock8x8 l8Block; public static LuminanceForwardConverter Create() { var result = default(LuminanceForwardConverter); result.converter = L8ToYConverter.Create(); return result; } /// /// Converts a 8x8 image area inside 'pixels' at position (x,y) placing the result members of the structure () /// public void Convert(ImageFrame frame, int x, int y, ref RowOctet currentRows) { this.pixelBlock.LoadAndStretchEdges(frame.PixelBuffer, x, y, ref currentRows); Span l8Span = this.l8Block.AsSpanUnsafe(); PixelOperations.Instance.ToL8(frame.GetConfiguration(), this.pixelBlock.AsSpanUnsafe(), l8Span); ref Block8x8F yBlock = ref this.Y; this.converter.Convert(l8Span, ref yBlock); } } }