Skip to content

Commit dc40ea0

Browse files
Backport #3105 to v3 branch
1 parent 634f554 commit dc40ea0

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

src/ImageSharp/Common/Helpers/SimdUtils.HwIntrinsics.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,8 +1077,10 @@ internal static void UnpackToRgbPlanesAvx2Reduce(
10771077
Vector256<byte> rgb, rg, bx;
10781078
Vector256<float> r, g, b;
10791079

1080+
// Each iteration consumes 8 Rgb24 pixels (24 bytes) but starts with a 32-byte load,
1081+
// so we need 3 extra pixels of addressable slack beyond the vectorized chunk.
10801082
const int bytesPerRgbStride = 24;
1081-
nuint count = (uint)source.Length / 8;
1083+
nuint count = source.Length > 3 ? (uint)(source.Length - 3) / 8 : 0;
10821084
for (nuint i = 0; i < count; i++)
10831085
{
10841086
rgb = Avx2.PermuteVar8x32(Unsafe.AddByteOffset(ref rgbByteSpan, (uint)(bytesPerRgbStride * i)).AsUInt32(), extractToLanesMask).AsByte();
@@ -1098,10 +1100,10 @@ internal static void UnpackToRgbPlanesAvx2Reduce(
10981100
}
10991101

11001102
int sliceCount = (int)(count * 8);
1101-
redChannel = redChannel.Slice(sliceCount);
1102-
greenChannel = greenChannel.Slice(sliceCount);
1103-
blueChannel = blueChannel.Slice(sliceCount);
1104-
source = source.Slice(sliceCount);
1103+
redChannel = redChannel[sliceCount..];
1104+
greenChannel = greenChannel[sliceCount..];
1105+
blueChannel = blueChannel[sliceCount..];
1106+
source = source[sliceCount..];
11051107
}
11061108
}
11071109
}

src/ImageSharp/Formats/Jpeg/Components/Encoder/SpectralConverter{TPixel}.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ private void ConvertStride(int spectralStep)
113113
Span<TPixel> sourceRow = this.pixelBuffer.DangerousGetRowSpan(srcIndex);
114114
PixelOperations<TPixel>.Instance.UnpackIntoRgbPlanes(rLane, gLane, bLane, sourceRow);
115115

116-
rLane.Slice(paddingStartIndex).Fill(rLane[paddingStartIndex - 1]);
117-
gLane.Slice(paddingStartIndex).Fill(gLane[paddingStartIndex - 1]);
118-
bLane.Slice(paddingStartIndex).Fill(bLane[paddingStartIndex - 1]);
116+
rLane.Slice(paddingStartIndex, paddedPixelsCount).Fill(rLane[paddingStartIndex - 1]);
117+
gLane.Slice(paddingStartIndex, paddedPixelsCount).Fill(gLane[paddingStartIndex - 1]);
118+
bLane.Slice(paddingStartIndex, paddedPixelsCount).Fill(bLane[paddingStartIndex - 1]);
119119

120120
// Convert from rgb24 to target pixel type
121121
var values = new JpegColorConverterBase.ComponentValues(this.componentProcessors, y);

0 commit comments

Comments
 (0)