|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Six Labors Split License. |
| 3 | + |
| 4 | +using System.Numerics; |
| 5 | +using System.Runtime.CompilerServices; |
| 6 | + |
| 7 | +// ReSharper disable UseObjectOrCollectionInitializer |
| 8 | +// ReSharper disable InconsistentNaming |
| 9 | +namespace SixLabors.ImageSharp.Formats.Jpeg.Components |
| 10 | +{ |
| 11 | + internal partial struct Block8x8F |
| 12 | + { |
| 13 | + [MethodImpl(InliningOptions.ShortMethod)] |
| 14 | + public void ScaledCopyFrom(ref float areaOrigin, int areaStride) => |
| 15 | + CopyFrom1x1Scale(ref Unsafe.As<float, byte>(ref areaOrigin), ref Unsafe.As<Block8x8F, byte>(ref this), areaStride); |
| 16 | + |
| 17 | + [MethodImpl(InliningOptions.ShortMethod)] |
| 18 | + public void ScaledCopyTo(ref float areaOrigin, int areaStride, int horizontalScale, int verticalScale) |
| 19 | + { |
| 20 | + if (horizontalScale == 1 && verticalScale == 1) |
| 21 | + { |
| 22 | + CopyTo1x1Scale(ref Unsafe.As<Block8x8F, byte>(ref this), ref Unsafe.As<float, byte>(ref areaOrigin), areaStride); |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + if (horizontalScale == 2 && verticalScale == 2) |
| 27 | + { |
| 28 | + this.CopyTo2x2Scale(ref areaOrigin, areaStride); |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + // TODO: Optimize: implement all cases with scale-specific, loopless code! |
| 33 | + this.CopyArbitraryScale(ref areaOrigin, areaStride, horizontalScale, verticalScale); |
| 34 | + } |
| 35 | + |
| 36 | + private void CopyTo2x2Scale(ref float areaOrigin, int areaStride) |
| 37 | + { |
| 38 | + ref Vector2 destBase = ref Unsafe.As<float, Vector2>(ref areaOrigin); |
| 39 | + int destStride = (int)((uint)areaStride / 2); |
| 40 | + |
| 41 | + WidenCopyRowImpl2x2(ref this.V0L, ref destBase, 0, destStride); |
| 42 | + WidenCopyRowImpl2x2(ref this.V0L, ref destBase, 1, destStride); |
| 43 | + WidenCopyRowImpl2x2(ref this.V0L, ref destBase, 2, destStride); |
| 44 | + WidenCopyRowImpl2x2(ref this.V0L, ref destBase, 3, destStride); |
| 45 | + WidenCopyRowImpl2x2(ref this.V0L, ref destBase, 4, destStride); |
| 46 | + WidenCopyRowImpl2x2(ref this.V0L, ref destBase, 5, destStride); |
| 47 | + WidenCopyRowImpl2x2(ref this.V0L, ref destBase, 6, destStride); |
| 48 | + WidenCopyRowImpl2x2(ref this.V0L, ref destBase, 7, destStride); |
| 49 | + |
| 50 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 51 | + static void WidenCopyRowImpl2x2(ref Vector4 selfBase, ref Vector2 destBase, nint row, nint destStride) |
| 52 | + { |
| 53 | + ref Vector4 sLeft = ref Unsafe.Add(ref selfBase, 2 * row); |
| 54 | + ref Vector4 sRight = ref Unsafe.Add(ref sLeft, 1); |
| 55 | + |
| 56 | + nint offset = 2 * row * destStride; |
| 57 | + ref Vector4 dTopLeft = ref Unsafe.As<Vector2, Vector4>(ref Unsafe.Add(ref destBase, offset)); |
| 58 | + ref Vector4 dBottomLeft = ref Unsafe.As<Vector2, Vector4>(ref Unsafe.Add(ref destBase, offset + destStride)); |
| 59 | + |
| 60 | + var xyLeft = new Vector4(sLeft.X); |
| 61 | + xyLeft.Z = sLeft.Y; |
| 62 | + xyLeft.W = sLeft.Y; |
| 63 | + |
| 64 | + var zwLeft = new Vector4(sLeft.Z); |
| 65 | + zwLeft.Z = sLeft.W; |
| 66 | + zwLeft.W = sLeft.W; |
| 67 | + |
| 68 | + var xyRight = new Vector4(sRight.X); |
| 69 | + xyRight.Z = sRight.Y; |
| 70 | + xyRight.W = sRight.Y; |
| 71 | + |
| 72 | + var zwRight = new Vector4(sRight.Z); |
| 73 | + zwRight.Z = sRight.W; |
| 74 | + zwRight.W = sRight.W; |
| 75 | + |
| 76 | + dTopLeft = xyLeft; |
| 77 | + Unsafe.Add(ref dTopLeft, 1) = zwLeft; |
| 78 | + Unsafe.Add(ref dTopLeft, 2) = xyRight; |
| 79 | + Unsafe.Add(ref dTopLeft, 3) = zwRight; |
| 80 | + |
| 81 | + dBottomLeft = xyLeft; |
| 82 | + Unsafe.Add(ref dBottomLeft, 1) = zwLeft; |
| 83 | + Unsafe.Add(ref dBottomLeft, 2) = xyRight; |
| 84 | + Unsafe.Add(ref dBottomLeft, 3) = zwRight; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + [MethodImpl(InliningOptions.ColdPath)] |
| 89 | + private void CopyArbitraryScale(ref float areaOrigin, int areaStride, int horizontalScale, int verticalScale) |
| 90 | + { |
| 91 | + for (int y = 0; y < 8; y++) |
| 92 | + { |
| 93 | + int yy = y * verticalScale; |
| 94 | + int y8 = y * 8; |
| 95 | + |
| 96 | + for (int x = 0; x < 8; x++) |
| 97 | + { |
| 98 | + int xx = x * horizontalScale; |
| 99 | + |
| 100 | + float value = this[y8 + x]; |
| 101 | + nint baseIdx = (yy * areaStride) + xx; |
| 102 | + |
| 103 | + for (nint i = 0; i < verticalScale; i++, baseIdx += areaStride) |
| 104 | + { |
| 105 | + for (nint j = 0; j < horizontalScale; j++) |
| 106 | + { |
| 107 | + // area[xx + j, yy + i] = value; |
| 108 | + Unsafe.Add(ref areaOrigin, baseIdx + j) = value; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private static void CopyTo1x1Scale(ref byte origin, ref byte dest, int areaStride) |
| 116 | + { |
| 117 | + int destStride = areaStride * sizeof(float); |
| 118 | + |
| 119 | + CopyRowImpl(ref origin, ref dest, destStride, 0); |
| 120 | + CopyRowImpl(ref origin, ref dest, destStride, 1); |
| 121 | + CopyRowImpl(ref origin, ref dest, destStride, 2); |
| 122 | + CopyRowImpl(ref origin, ref dest, destStride, 3); |
| 123 | + CopyRowImpl(ref origin, ref dest, destStride, 4); |
| 124 | + CopyRowImpl(ref origin, ref dest, destStride, 5); |
| 125 | + CopyRowImpl(ref origin, ref dest, destStride, 6); |
| 126 | + CopyRowImpl(ref origin, ref dest, destStride, 7); |
| 127 | + |
| 128 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 129 | + static void CopyRowImpl(ref byte origin, ref byte dest, int destStride, int row) |
| 130 | + { |
| 131 | + origin = ref Unsafe.Add(ref origin, row * 8 * sizeof(float)); |
| 132 | + dest = ref Unsafe.Add(ref dest, row * destStride); |
| 133 | + Unsafe.CopyBlock(ref dest, ref origin, 8 * sizeof(float)); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private static void CopyFrom1x1Scale(ref byte origin, ref byte dest, int areaStride) |
| 138 | + { |
| 139 | + int destStride = areaStride * sizeof(float); |
| 140 | + |
| 141 | + CopyRowImpl(ref origin, ref dest, destStride, 0); |
| 142 | + CopyRowImpl(ref origin, ref dest, destStride, 1); |
| 143 | + CopyRowImpl(ref origin, ref dest, destStride, 2); |
| 144 | + CopyRowImpl(ref origin, ref dest, destStride, 3); |
| 145 | + CopyRowImpl(ref origin, ref dest, destStride, 4); |
| 146 | + CopyRowImpl(ref origin, ref dest, destStride, 5); |
| 147 | + CopyRowImpl(ref origin, ref dest, destStride, 6); |
| 148 | + CopyRowImpl(ref origin, ref dest, destStride, 7); |
| 149 | + |
| 150 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 151 | + static void CopyRowImpl(ref byte origin, ref byte dest, int sourceStride, int row) |
| 152 | + { |
| 153 | + origin = ref Unsafe.Add(ref origin, row * sourceStride); |
| 154 | + dest = ref Unsafe.Add(ref dest, row * 8 * sizeof(float)); |
| 155 | + Unsafe.CopyBlock(ref dest, ref origin, 8 * sizeof(float)); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments