diff --git a/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs b/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs index cd95eba560..c28e490cbe 100644 --- a/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs @@ -26,22 +26,20 @@ namespace ImageSharp.Formats public static byte[] Decode(byte[] scanline, byte[] previousScanline, int bytesPerPixel) { // Average(x) + floor((Raw(x-bpp)+Prior(x))/2) - byte[] result = new byte[scanline.Length]; fixed (byte* scan = scanline) fixed (byte* prev = previousScanline) - fixed (byte* res = result) { for (int x = 1; x < scanline.Length; x++) { - byte left = (x - bytesPerPixel < 1) ? (byte)0 : res[x - bytesPerPixel]; + byte left = (x - bytesPerPixel < 1) ? (byte)0 : scan[x - bytesPerPixel]; byte above = prev[x]; - res[x] = (byte)((scan[x] + Average(left, above)) % 256); + scan[x] = (byte)((scan[x] + Average(left, above)) % 256); } } - return result; + return scanline; } /// diff --git a/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs b/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs index 16c0378e96..e0f455e002 100644 --- a/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs @@ -25,23 +25,21 @@ namespace ImageSharp.Formats public static byte[] Decode(byte[] scanline, byte[] previousScanline, int bytesPerPixel) { // Paeth(x) + PaethPredictor(Raw(x-bpp), Prior(x), Prior(x-bpp)) - byte[] result = new byte[scanline.Length]; fixed (byte* scan = scanline) fixed (byte* prev = previousScanline) - fixed (byte* res = result) { for (int x = 1; x < scanline.Length; x++) { - byte left = (x - bytesPerPixel < 1) ? (byte)0 : res[x - bytesPerPixel]; + byte left = (x - bytesPerPixel < 1) ? (byte)0 : scan[x - bytesPerPixel]; byte above = prev[x]; byte upperLeft = (x - bytesPerPixel < 1) ? (byte)0 : prev[x - bytesPerPixel]; - res[x] = (byte)((scan[x] + PaethPredicator(left, above, upperLeft)) % 256); + scan[x] = (byte)((scan[x] + PaethPredicator(left, above, upperLeft)) % 256); } } - return result; + return scanline; } /// diff --git a/src/ImageSharp/Formats/Png/Filters/SubFilter.cs b/src/ImageSharp/Formats/Png/Filters/SubFilter.cs index 63e41a1d55..904dc191d0 100644 --- a/src/ImageSharp/Formats/Png/Filters/SubFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/SubFilter.cs @@ -21,20 +21,17 @@ namespace ImageSharp.Formats public static byte[] Decode(byte[] scanline, int bytesPerPixel) { // Sub(x) + Raw(x-bpp) - byte[] result = new byte[scanline.Length]; fixed (byte* scan = scanline) - fixed (byte* res = result) { for (int x = 1; x < scanline.Length; x++) { - byte priorRawByte = (x - bytesPerPixel < 1) ? (byte)0 : res[x - bytesPerPixel]; - - res[x] = (byte)((scan[x] + priorRawByte) % 256); + byte priorRawByte = (x - bytesPerPixel < 1) ? (byte)0 : scan[x - bytesPerPixel]; + scan[x] = (byte)((scan[x] + priorRawByte) % 256); } } - return result; + return scanline; } /// diff --git a/src/ImageSharp/Formats/Png/Filters/UpFilter.cs b/src/ImageSharp/Formats/Png/Filters/UpFilter.cs index b40aa944b5..c4572f7a5a 100644 --- a/src/ImageSharp/Formats/Png/Filters/UpFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/UpFilter.cs @@ -21,21 +21,19 @@ namespace ImageSharp.Formats public static byte[] Decode(byte[] scanline, byte[] previousScanline) { // Up(x) + Prior(x) - byte[] result = new byte[scanline.Length]; fixed (byte* scan = scanline) fixed (byte* prev = previousScanline) - fixed (byte* res = result) { for (int x = 1; x < scanline.Length; x++) { byte above = prev[x]; - res[x] = (byte)((scan[x] + above) % 256); + scan[x] = (byte)((scan[x] + above) % 256); } } - return result; + return scanline; } /// diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index 186e994372..22062c964c 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -10,7 +10,7 @@ namespace ImageSharp.Formats using System.IO; using System.Linq; using System.Text; - + /// /// Performs the png decoding operation. /// @@ -276,45 +276,42 @@ namespace ImageSharp.Formats // TODO: ArrayPool.Shared.Rent(this.bytesPerScanline) byte[] previousScanline = new byte[this.bytesPerScanline]; byte[] scanline = new byte[this.bytesPerScanline]; + for (int y = 0; y < this.header.Height; y++) { compressedStream.Read(scanline, 0, this.bytesPerScanline); FilterType filterType = (FilterType)scanline[0]; - byte[] defilteredScanline; - // TODO: It would be good if we can reduce the memory usage here - Each filter is creating a new row. - // Every time I try to use the same approach as I have in the encoder though I keep messing up. - // Fingers crossed someone with a big brain and a kind heart will come along and finish optimizing this for me. switch (filterType) { case FilterType.None: - defilteredScanline = NoneFilter.Decode(scanline); + NoneFilter.Decode(scanline); break; case FilterType.Sub: - defilteredScanline = SubFilter.Decode(scanline, this.bytesPerPixel); + SubFilter.Decode(scanline, this.bytesPerPixel); break; case FilterType.Up: - defilteredScanline = UpFilter.Decode(scanline, previousScanline); + UpFilter.Decode(scanline, previousScanline); break; case FilterType.Average: - defilteredScanline = AverageFilter.Decode(scanline, previousScanline, this.bytesPerPixel); + AverageFilter.Decode(scanline, previousScanline, this.bytesPerPixel); break; case FilterType.Paeth: - defilteredScanline = PaethFilter.Decode(scanline, previousScanline, this.bytesPerPixel); + PaethFilter.Decode(scanline, previousScanline, this.bytesPerPixel); break; @@ -322,8 +319,11 @@ namespace ImageSharp.Formats throw new ImageFormatException("Unknown filter type."); } - previousScanline = defilteredScanline; - this.ProcessDefilteredScanline(defilteredScanline, y, pixels); + this.ProcessDefilteredScanline(scanline, y, pixels); + + byte[] temp = previousScanline; + previousScanline = scanline; + scanline = temp; } } diff --git a/tests/ImageSharp.Tests/FileTestBase.cs b/tests/ImageSharp.Tests/FileTestBase.cs index b9a226fc8c..6ef94fb6f9 100644 --- a/tests/ImageSharp.Tests/FileTestBase.cs +++ b/tests/ImageSharp.Tests/FileTestBase.cs @@ -32,6 +32,12 @@ namespace ImageSharp.Tests // new TestFile(TestImages.Png.Blur), // Perf: Enable for local testing only // new TestFile(TestImages.Png.Indexed), // Perf: Enable for local testing only new TestFile(TestImages.Png.Splash), + // new TestFile(TestImages.Png.Filter0), // Perf: Enable for local testing only + // new TestFile(TestImages.Png.Filter1), // Perf: Enable for local testing only + // new TestFile(TestImages.Png.Filter2), // Perf: Enable for local testing only + // new TestFile(TestImages.Png.Filter3), // Perf: Enable for local testing only + // new TestFile(TestImages.Png.Filter4), // Perf: Enable for local testing only + // new TestFile(TestImages.Png.FilterVar), // Perf: Enable for local testing only new TestFile(TestImages.Gif.Rings), // new TestFile(TestImages.Gif.Giphy) // Perf: Enable for local testing only }; diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index e6d3cd7570..a5eebeebe9 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -19,6 +19,15 @@ namespace ImageSharp.Tests public static string Blur => folder + "blur.png"; public static string Indexed => folder + "indexed.png"; public static string Splash => folder + "splash.png"; + + // filtered test images from http://www.schaik.com/pngsuite/pngsuite_fil_png.html + public static string Filter0 => folder + "filter0.png"; + public static string Filter1 => folder + "filter1.png"; + public static string Filter2 => folder + "filter2.png"; + public static string Filter3 => folder + "filter3.png"; + public static string Filter4 => folder + "filter4.png"; + // filter changing per scanline + public static string FilterVar => folder + "filterVar.png"; } public static class Jpeg diff --git a/tests/ImageSharp.Tests/TestImages/Formats/Png/filter0.png b/tests/ImageSharp.Tests/TestImages/Formats/Png/filter0.png new file mode 100644 index 0000000000..d6a1ffff62 Binary files /dev/null and b/tests/ImageSharp.Tests/TestImages/Formats/Png/filter0.png differ diff --git a/tests/ImageSharp.Tests/TestImages/Formats/Png/filter1.png b/tests/ImageSharp.Tests/TestImages/Formats/Png/filter1.png new file mode 100644 index 0000000000..26fee958ce Binary files /dev/null and b/tests/ImageSharp.Tests/TestImages/Formats/Png/filter1.png differ diff --git a/tests/ImageSharp.Tests/TestImages/Formats/Png/filter2.png b/tests/ImageSharp.Tests/TestImages/Formats/Png/filter2.png new file mode 100644 index 0000000000..e590f12348 Binary files /dev/null and b/tests/ImageSharp.Tests/TestImages/Formats/Png/filter2.png differ diff --git a/tests/ImageSharp.Tests/TestImages/Formats/Png/filter3.png b/tests/ImageSharp.Tests/TestImages/Formats/Png/filter3.png new file mode 100644 index 0000000000..758115059d Binary files /dev/null and b/tests/ImageSharp.Tests/TestImages/Formats/Png/filter3.png differ diff --git a/tests/ImageSharp.Tests/TestImages/Formats/Png/filter4.png b/tests/ImageSharp.Tests/TestImages/Formats/Png/filter4.png new file mode 100644 index 0000000000..3c8b5116e7 Binary files /dev/null and b/tests/ImageSharp.Tests/TestImages/Formats/Png/filter4.png differ diff --git a/tests/ImageSharp.Tests/TestImages/Formats/Png/filterVar.png b/tests/ImageSharp.Tests/TestImages/Formats/Png/filterVar.png new file mode 100644 index 0000000000..0b521c1d56 Binary files /dev/null and b/tests/ImageSharp.Tests/TestImages/Formats/Png/filterVar.png differ