|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Six Labors Split License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Runtime.CompilerServices; |
| 6 | +using SixLabors.ImageSharp.Memory; |
| 7 | + |
| 8 | +namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Processes component spectral data and converts it to color data in 2-to-1 scale. |
| 12 | + /// </summary> |
| 13 | + internal sealed class DownScalingComponentProcessor2 : ComponentProcessor |
| 14 | + { |
| 15 | + private Block8x8F dequantizationTable; |
| 16 | + |
| 17 | + public DownScalingComponentProcessor2(MemoryAllocator memoryAllocator, JpegFrame frame, IRawJpegData rawJpeg, Size postProcessorBufferSize, IJpegComponent component) |
| 18 | + : base(memoryAllocator, frame, postProcessorBufferSize, component, 4) |
| 19 | + { |
| 20 | + this.dequantizationTable = rawJpeg.QuantizationTables[component.QuantizationTableIndex]; |
| 21 | + ScaledFloatingPointDCT.AdjustToIDCT(ref this.dequantizationTable); |
| 22 | + } |
| 23 | + |
| 24 | + public override void CopyBlocksToColorBuffer(int spectralStep) |
| 25 | + { |
| 26 | + Buffer2D<Block8x8> spectralBuffer = this.Component.SpectralBlocks; |
| 27 | + |
| 28 | + float maximumValue = this.Frame.MaxColorChannelValue; |
| 29 | + float normalizationValue = MathF.Ceiling(maximumValue / 2); |
| 30 | + |
| 31 | + int destAreaStride = this.ColorBuffer.Width; |
| 32 | + |
| 33 | + int blocksRowsPerStep = this.Component.SamplingFactors.Height; |
| 34 | + Size subSamplingDivisors = this.Component.SubSamplingDivisors; |
| 35 | + |
| 36 | + Block8x8F workspaceBlock = default; |
| 37 | + |
| 38 | + int yBlockStart = spectralStep * blocksRowsPerStep; |
| 39 | + |
| 40 | + for (int y = 0; y < blocksRowsPerStep; y++) |
| 41 | + { |
| 42 | + int yBuffer = y * this.BlockAreaSize.Height; |
| 43 | + |
| 44 | + Span<float> colorBufferRow = this.ColorBuffer.DangerousGetRowSpan(yBuffer); |
| 45 | + Span<Block8x8> blockRow = spectralBuffer.DangerousGetRowSpan(yBlockStart + y); |
| 46 | + |
| 47 | + for (int xBlock = 0; xBlock < spectralBuffer.Width; xBlock++) |
| 48 | + { |
| 49 | + // Integer to float |
| 50 | + workspaceBlock.LoadFrom(ref blockRow[xBlock]); |
| 51 | + |
| 52 | + // IDCT/Normalization/Range |
| 53 | + ScaledFloatingPointDCT.TransformIDCT_4x4(ref workspaceBlock, ref this.dequantizationTable, normalizationValue, maximumValue); |
| 54 | + |
| 55 | + // Save to the intermediate buffer |
| 56 | + int xColorBufferStart = xBlock * this.BlockAreaSize.Width; |
| 57 | + ScaledCopyTo( |
| 58 | + ref workspaceBlock, |
| 59 | + ref colorBufferRow[xColorBufferStart], |
| 60 | + destAreaStride, |
| 61 | + subSamplingDivisors.Width, |
| 62 | + subSamplingDivisors.Height); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + [MethodImpl(InliningOptions.ShortMethod)] |
| 68 | + public static void ScaledCopyTo(ref Block8x8F block, ref float destRef, int destStrideWidth, int horizontalScale, int verticalScale) |
| 69 | + { |
| 70 | + // TODO: Optimize: implement all cases with scale-specific, loopless code! |
| 71 | + CopyArbitraryScale(ref block, ref destRef, destStrideWidth, horizontalScale, verticalScale); |
| 72 | + |
| 73 | + [MethodImpl(InliningOptions.ColdPath)] |
| 74 | + static void CopyArbitraryScale(ref Block8x8F block, ref float areaOrigin, int areaStride, int horizontalScale, int verticalScale) |
| 75 | + { |
| 76 | + for (int y = 0; y < 4; y++) |
| 77 | + { |
| 78 | + int yy = y * verticalScale; |
| 79 | + int y8 = y * 8; |
| 80 | + |
| 81 | + for (int x = 0; x < 4; x++) |
| 82 | + { |
| 83 | + int xx = x * horizontalScale; |
| 84 | + |
| 85 | + float value = block[y8 + x]; |
| 86 | + |
| 87 | + for (int i = 0; i < verticalScale; i++) |
| 88 | + { |
| 89 | + int baseIdx = ((yy + i) * areaStride) + xx; |
| 90 | + |
| 91 | + for (int j = 0; j < horizontalScale; j++) |
| 92 | + { |
| 93 | + // area[xx + j, yy + i] = value; |
| 94 | + Unsafe.Add(ref areaOrigin, (nint)(uint)(baseIdx + j)) = value; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments