|
| 1 | +// Copyright (c) Six Labors and contributors. |
| 2 | +// Licensed under the Apache License, Version 2.0. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Runtime.CompilerServices; |
| 6 | +using System.Runtime.InteropServices; |
| 7 | + |
| 8 | +using SixLabors.ImageSharp.Memory; |
| 9 | +using SixLabors.ImageSharp.PixelFormats; |
| 10 | +using SixLabors.ImageSharp.Primitives; |
| 11 | + |
| 12 | +namespace SixLabors.ImageSharp |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Extension methods for <see cref="Buffer2D{T}"/>. |
| 16 | + /// TODO: One day rewrite all this to use SIMD intrinsics. There's a lot of scope for improvement. |
| 17 | + /// </summary> |
| 18 | + internal static class Buffer2DUtils |
| 19 | + { |
| 20 | + /// <summary> |
| 21 | + /// Computes the sum of vectors in <paramref name="targetRow"/> weighted by the kernel weight values. |
| 22 | + /// </summary> |
| 23 | + /// <typeparam name="TPixel">The pixel format.</typeparam> |
| 24 | + /// <param name="kernel">The 1D convolution kernel.</param> |
| 25 | + /// <param name="sourcePixels">The source frame.</param> |
| 26 | + /// <param name="targetRow">The target row.</param> |
| 27 | + /// <param name="row">The current row.</param> |
| 28 | + /// <param name="column">The current column.</param> |
| 29 | + /// <param name="minRow">The minimum working area row.</param> |
| 30 | + /// <param name="maxRow">The maximum working area row.</param> |
| 31 | + /// <param name="minColumn">The minimum working area column.</param> |
| 32 | + /// <param name="maxColumn">The maximum working area column.</param> |
| 33 | + public static void Convolve4<TPixel>( |
| 34 | + Span<Complex64> kernel, |
| 35 | + Buffer2D<TPixel> sourcePixels, |
| 36 | + Span<ComplexVector4> targetRow, |
| 37 | + int row, |
| 38 | + int column, |
| 39 | + int minRow, |
| 40 | + int maxRow, |
| 41 | + int minColumn, |
| 42 | + int maxColumn) |
| 43 | + where TPixel : struct, IPixel<TPixel> |
| 44 | + { |
| 45 | + ComplexVector4 vector = default; |
| 46 | + int kernelLength = kernel.Length; |
| 47 | + int radiusY = kernelLength >> 1; |
| 48 | + int sourceOffsetColumnBase = column + minColumn; |
| 49 | + ref Complex64 baseRef = ref MemoryMarshal.GetReference(kernel); |
| 50 | + |
| 51 | + for (int i = 0; i < kernelLength; i++) |
| 52 | + { |
| 53 | + int offsetY = (row + i - radiusY).Clamp(minRow, maxRow); |
| 54 | + int offsetX = sourceOffsetColumnBase.Clamp(minColumn, maxColumn); |
| 55 | + Span<TPixel> sourceRowSpan = sourcePixels.GetRowSpan(offsetY); |
| 56 | + var currentColor = sourceRowSpan[offsetX].ToVector4(); |
| 57 | + |
| 58 | + vector.Sum(Unsafe.Add(ref baseRef, i) * currentColor); |
| 59 | + } |
| 60 | + |
| 61 | + targetRow[column] = vector; |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Computes the sum of vectors in <paramref name="targetRow"/> weighted by the kernel weight values. |
| 66 | + /// </summary> |
| 67 | + /// <param name="kernel">The 1D convolution kernel.</param> |
| 68 | + /// <param name="sourceValues">The source frame.</param> |
| 69 | + /// <param name="targetRow">The target row.</param> |
| 70 | + /// <param name="row">The current row.</param> |
| 71 | + /// <param name="column">The current column.</param> |
| 72 | + /// <param name="minRow">The minimum working area row.</param> |
| 73 | + /// <param name="maxRow">The maximum working area row.</param> |
| 74 | + /// <param name="minColumn">The minimum working area column.</param> |
| 75 | + /// <param name="maxColumn">The maximum working area column.</param> |
| 76 | + public static void Convolve4( |
| 77 | + Span<Complex64> kernel, |
| 78 | + Buffer2D<ComplexVector4> sourceValues, |
| 79 | + Span<ComplexVector4> targetRow, |
| 80 | + int row, |
| 81 | + int column, |
| 82 | + int minRow, |
| 83 | + int maxRow, |
| 84 | + int minColumn, |
| 85 | + int maxColumn) |
| 86 | + { |
| 87 | + ComplexVector4 vector = default; |
| 88 | + int kernelLength = kernel.Length; |
| 89 | + int radiusX = kernelLength >> 1; |
| 90 | + int sourceOffsetColumnBase = column + minColumn; |
| 91 | + |
| 92 | + int offsetY = row.Clamp(minRow, maxRow); |
| 93 | + ref ComplexVector4 sourceRef = ref MemoryMarshal.GetReference(sourceValues.GetRowSpan(offsetY)); |
| 94 | + ref Complex64 baseRef = ref MemoryMarshal.GetReference(kernel); |
| 95 | + |
| 96 | + for (int x = 0; x < kernelLength; x++) |
| 97 | + { |
| 98 | + int offsetX = (sourceOffsetColumnBase + x - radiusX).Clamp(minColumn, maxColumn); |
| 99 | + vector.Sum(Unsafe.Add(ref baseRef, x) * Unsafe.Add(ref sourceRef, offsetX)); |
| 100 | + } |
| 101 | + |
| 102 | + targetRow[column] = vector; |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments