Skip to content

Commit 65bd9b5

Browse files
Merge pull request #2775 from SixLabors/js-fix-2771
Fix filtering on PNG encode.
2 parents d7ef0e2 + 94f4083 commit 65bd9b5

File tree

4 files changed

+13
-8
lines changed

4 files changed

+13
-8
lines changed

src/ImageSharp/Formats/Png/Filters/AverageFilter.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public static void Encode(ReadOnlySpan<byte> scanline, ReadOnlySpan<byte> previo
169169
Vector256<int> sumAccumulator = Vector256<int>.Zero;
170170
Vector256<byte> allBitsSet = Avx2.CompareEqual(sumAccumulator, sumAccumulator).AsByte();
171171

172-
for (nuint xLeft = x - bytesPerPixel; x <= (uint)(scanline.Length - Vector256<byte>.Count); xLeft += (uint)Vector256<byte>.Count)
172+
for (nuint xLeft = x - bytesPerPixel; (int)x <= scanline.Length - Vector256<byte>.Count; xLeft += (uint)Vector256<byte>.Count)
173173
{
174174
Vector256<byte> scan = Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref scanBaseRef, x));
175175
Vector256<byte> left = Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref scanBaseRef, xLeft));
@@ -192,7 +192,7 @@ public static void Encode(ReadOnlySpan<byte> scanline, ReadOnlySpan<byte> previo
192192
Vector128<int> sumAccumulator = Vector128<int>.Zero;
193193
Vector128<byte> allBitsSet = Sse2.CompareEqual(sumAccumulator, sumAccumulator).AsByte();
194194

195-
for (nuint xLeft = x - bytesPerPixel; x <= (uint)(scanline.Length - Vector128<byte>.Count); xLeft += (uint)Vector128<byte>.Count)
195+
for (nuint xLeft = x - bytesPerPixel; (int)x <= scanline.Length - Vector128<byte>.Count; xLeft += (uint)Vector128<byte>.Count)
196196
{
197197
Vector128<byte> scan = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref scanBaseRef, x));
198198
Vector128<byte> left = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref scanBaseRef, xLeft));

src/ImageSharp/Formats/Png/Filters/SubFilter.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public static void Encode(ReadOnlySpan<byte> scanline, ReadOnlySpan<byte> result
136136
Vector256<byte> zero = Vector256<byte>.Zero;
137137
Vector256<int> sumAccumulator = Vector256<int>.Zero;
138138

139-
for (nuint xLeft = x - (uint)bytesPerPixel; x <= (uint)(scanline.Length - Vector256<byte>.Count); xLeft += (uint)Vector256<byte>.Count)
139+
for (nuint xLeft = x - (uint)bytesPerPixel; (int)x <= (scanline.Length - Vector256<byte>.Count); xLeft += (uint)Vector256<byte>.Count)
140140
{
141141
Vector256<byte> scan = Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref scanBaseRef, x));
142142
Vector256<byte> prev = Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref scanBaseRef, xLeft));
@@ -150,11 +150,12 @@ public static void Encode(ReadOnlySpan<byte> scanline, ReadOnlySpan<byte> result
150150

151151
sum += Numerics.EvenReduceSum(sumAccumulator);
152152
}
153-
else if (Vector.IsHardwareAccelerated)
153+
else
154+
if (Vector.IsHardwareAccelerated)
154155
{
155156
Vector<uint> sumAccumulator = Vector<uint>.Zero;
156157

157-
for (nuint xLeft = x - (uint)bytesPerPixel; x <= (uint)(scanline.Length - Vector<byte>.Count); xLeft += (uint)Vector<byte>.Count)
158+
for (nuint xLeft = x - (uint)bytesPerPixel; (int)x <= (scanline.Length - Vector<byte>.Count); xLeft += (uint)Vector<byte>.Count)
158159
{
159160
Vector<byte> scan = Unsafe.As<byte, Vector<byte>>(ref Unsafe.Add(ref scanBaseRef, x));
160161
Vector<byte> prev = Unsafe.As<byte, Vector<byte>>(ref Unsafe.Add(ref scanBaseRef, xLeft));

src/ImageSharp/Formats/Png/Filters/UpFilter.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public static void Encode(ReadOnlySpan<byte> scanline, ReadOnlySpan<byte> previo
179179
Vector256<byte> zero = Vector256<byte>.Zero;
180180
Vector256<int> sumAccumulator = Vector256<int>.Zero;
181181

182-
for (; x <= (uint)(scanline.Length - Vector256<byte>.Count);)
182+
for (; (int)x <= scanline.Length - Vector256<byte>.Count;)
183183
{
184184
Vector256<byte> scan = Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref scanBaseRef, x));
185185
Vector256<byte> above = Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref prevBaseRef, x));
@@ -197,7 +197,7 @@ public static void Encode(ReadOnlySpan<byte> scanline, ReadOnlySpan<byte> previo
197197
{
198198
Vector<uint> sumAccumulator = Vector<uint>.Zero;
199199

200-
for (; x <= (uint)(scanline.Length - Vector<byte>.Count);)
200+
for (; (int)x <= scanline.Length - Vector<byte>.Count;)
201201
{
202202
Vector<byte> scan = Unsafe.As<byte, Vector<byte>>(ref Unsafe.Add(ref scanBaseRef, x));
203203
Vector<byte> above = Unsafe.As<byte, Vector<byte>>(ref Unsafe.Add(ref prevBaseRef, x));

src/ImageSharp/Formats/Png/PngEncoderCore.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1477,7 +1477,11 @@ private void SanitizeAndSetEncoderOptions<TPixel>(
14771477
// Use options, then check metadata, if nothing set there then we suggest
14781478
// a sensible default based upon the pixel format.
14791479
this.colorType = encoder.ColorType ?? pngMetadata.ColorType ?? SuggestColorType<TPixel>();
1480-
if (!encoder.FilterMethod.HasValue)
1480+
if (encoder.FilterMethod.HasValue)
1481+
{
1482+
this.filterMethod = encoder.FilterMethod.Value;
1483+
}
1484+
else
14811485
{
14821486
// Specification recommends default filter method None for paletted images and Paeth for others.
14831487
this.filterMethod = this.colorType is PngColorType.Palette ? PngFilterMethod.None : PngFilterMethod.Paeth;

0 commit comments

Comments
 (0)