From 39f8c776debeabba977055c2ac1007c437eeba01 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Fri, 14 Apr 2023 19:50:09 +0200 Subject: [PATCH] Using Binary serialization for Vp8Residual --- .../Formats/Webp/Lossy/Vp8BandProbas.cs | 8 +---- .../Formats/Webp/Lossy/Vp8CostArray.cs | 10 +----- src/ImageSharp/Formats/Webp/Lossy/Vp8Costs.cs | 10 +----- .../Formats/Webp/Lossy/Vp8ProbaArray.cs | 10 +----- .../Formats/Webp/Lossy/Vp8Residual.cs | 13 +------ src/ImageSharp/Formats/Webp/Lossy/Vp8Stats.cs | 10 +----- .../Formats/Webp/Lossy/Vp8StatsArray.cs | 10 +----- .../Formats/WebP/Vp8ResidualTests.cs | 32 +++++++----------- .../ImageSharp.Tests/ImageSharp.Tests.csproj | 2 +- .../TestDataWebp/Vp8Residual.bin | Bin 0 -> 15043 bytes 10 files changed, 20 insertions(+), 85 deletions(-) create mode 100644 tests/ImageSharp.Tests/TestDataWebp/Vp8Residual.bin diff --git a/src/ImageSharp/Formats/Webp/Lossy/Vp8BandProbas.cs b/src/ImageSharp/Formats/Webp/Lossy/Vp8BandProbas.cs index 86304d4bd..5db842ba3 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8BandProbas.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8BandProbas.cs @@ -8,6 +8,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy; /// /// All the probabilities associated to one band. /// +[Serializable] internal class Vp8BandProbas { /// @@ -22,13 +23,6 @@ internal class Vp8BandProbas } } - /// - /// Initializes a new instance of the class. - /// Only used for unit tests. - /// - [JsonConstructor] - public Vp8BandProbas(Vp8ProbaArray[] probabilities) => this.Probabilities = probabilities; - /// /// Gets the Probabilities. /// diff --git a/src/ImageSharp/Formats/Webp/Lossy/Vp8CostArray.cs b/src/ImageSharp/Formats/Webp/Lossy/Vp8CostArray.cs index 8268e1c02..ba1b49881 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8CostArray.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8CostArray.cs @@ -1,10 +1,9 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -using System.Text.Json.Serialization; - namespace SixLabors.ImageSharp.Formats.Webp.Lossy; +[Serializable] internal class Vp8CostArray { /// @@ -12,12 +11,5 @@ internal class Vp8CostArray /// public Vp8CostArray() => this.Costs = new ushort[67 + 1]; - /// - /// Initializes a new instance of the class. - /// Only used for unit tests. - /// - [JsonConstructor] - public Vp8CostArray(ushort[] costs) => this.Costs = costs; - public ushort[] Costs { get; } } diff --git a/src/ImageSharp/Formats/Webp/Lossy/Vp8Costs.cs b/src/ImageSharp/Formats/Webp/Lossy/Vp8Costs.cs index 2441436fb..676874820 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8Costs.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8Costs.cs @@ -1,10 +1,9 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -using System.Text.Json.Serialization; - namespace SixLabors.ImageSharp.Formats.Webp.Lossy; +[Serializable] internal class Vp8Costs { /// @@ -19,13 +18,6 @@ internal class Vp8Costs } } - /// - /// Initializes a new instance of the class. - /// Only used for unit tests. - /// - [JsonConstructor] - public Vp8Costs(Vp8CostArray[] costs) => this.Costs = costs; - /// /// Gets the Costs. /// diff --git a/src/ImageSharp/Formats/Webp/Lossy/Vp8ProbaArray.cs b/src/ImageSharp/Formats/Webp/Lossy/Vp8ProbaArray.cs index a19f9742f..ddc1c9d61 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8ProbaArray.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8ProbaArray.cs @@ -1,13 +1,12 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -using System.Text.Json.Serialization; - namespace SixLabors.ImageSharp.Formats.Webp.Lossy; /// /// Probabilities associated to one of the contexts. /// +[Serializable] internal class Vp8ProbaArray { /// @@ -15,13 +14,6 @@ internal class Vp8ProbaArray /// public Vp8ProbaArray() => this.Probabilities = new byte[WebpConstants.NumProbas]; - /// - /// Initializes a new instance of the class. - /// Only used for unit tests. - /// - [JsonConstructor] - public Vp8ProbaArray(byte[] probabilities) => this.Probabilities = probabilities; - /// /// Gets the probabilities. /// diff --git a/src/ImageSharp/Formats/Webp/Lossy/Vp8Residual.cs b/src/ImageSharp/Formats/Webp/Lossy/Vp8Residual.cs index 6e0937d86..58fba10a7 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8Residual.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8Residual.cs @@ -14,24 +14,13 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy; /// /// On-the-fly info about the current set of residuals. /// +[Serializable] internal class Vp8Residual { public Vp8Residual() { } - [JsonConstructor] - public Vp8Residual(int first, int last, int coeffType, short[] coeffs, Vp8BandProbas[] prob, Vp8Stats[] stats, Vp8Costs[] costs) - { - this.First = first; - this.Last = last; - this.CoeffType = coeffType; - this.Coeffs = coeffs; - this.Prob = prob; - this.Stats = stats; - this.Costs = costs; - } - public int First { get; set; } public int Last { get; set; } diff --git a/src/ImageSharp/Formats/Webp/Lossy/Vp8Stats.cs b/src/ImageSharp/Formats/Webp/Lossy/Vp8Stats.cs index b6c05f2d4..23d63f322 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8Stats.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8Stats.cs @@ -1,10 +1,9 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -using System.Text.Json.Serialization; - namespace SixLabors.ImageSharp.Formats.Webp.Lossy; +[Serializable] internal class Vp8Stats { /// @@ -19,12 +18,5 @@ internal class Vp8Stats } } - /// - /// Initializes a new instance of the class. - /// Only used for unit tests. - /// - [JsonConstructor] - public Vp8Stats(Vp8StatsArray[] stats) => this.Stats = stats; - public Vp8StatsArray[] Stats { get; } } diff --git a/src/ImageSharp/Formats/Webp/Lossy/Vp8StatsArray.cs b/src/ImageSharp/Formats/Webp/Lossy/Vp8StatsArray.cs index 864248dd8..2ab0702eb 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8StatsArray.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8StatsArray.cs @@ -1,10 +1,9 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -using System.Text.Json.Serialization; - namespace SixLabors.ImageSharp.Formats.Webp.Lossy; +[Serializable] internal class Vp8StatsArray { /// @@ -12,12 +11,5 @@ internal class Vp8StatsArray /// public Vp8StatsArray() => this.Stats = new uint[WebpConstants.NumProbas]; - /// - /// Initializes a new instance of the class. - /// Only used for unit tests. - /// - [JsonConstructor] - public Vp8StatsArray(uint[] stats) => this.Stats = stats; - public uint[] Stats { get; } } diff --git a/tests/ImageSharp.Tests/Formats/WebP/Vp8ResidualTests.cs b/tests/ImageSharp.Tests/Formats/WebP/Vp8ResidualTests.cs index 20a161371..2371a179e 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/Vp8ResidualTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/Vp8ResidualTests.cs @@ -1,11 +1,10 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -using System.Runtime.Intrinsics.X86; +using System.Runtime.Serialization.Formatters.Binary; using SixLabors.ImageSharp.Formats.Webp; using SixLabors.ImageSharp.Formats.Webp.Lossy; using SixLabors.ImageSharp.Tests.TestUtilities; -using JsonSerializer = System.Text.Json.JsonSerializer; namespace SixLabors.ImageSharp.Tests.Formats.Webp; @@ -15,12 +14,6 @@ public class Vp8ResidualTests [Fact] public void Vp8Residual_Serialization_Works() { - if (!Sse2.IsSupported) - { - // JsonSerializer without SSE2 does not seem to work, skip test then. - return; - } - // arrange Vp8Residual expected = new(); Vp8EncProba encProb = new(); @@ -34,8 +27,12 @@ public class Vp8ResidualTests } // act - string jsonString = JsonSerializer.Serialize(expected); - Vp8Residual actual = JsonSerializer.Deserialize(jsonString); + BinaryFormatter formatter = new(); + using MemoryStream ms = new(); + formatter.Serialize(ms, expected); + ms.Position = 0; + object obj = formatter.Deserialize(ms); + Vp8Residual actual = (Vp8Residual)obj; // assert Assert.Equal(expected.CoeffType, actual.CoeffType); @@ -82,17 +79,14 @@ public class Vp8ResidualTests [Fact] public void GetResidualCost_Works() { - if (!Sse2.IsSupported) - { - // JsonSerializer without SSE2 does not seem to work, skip test then. - return; - } - // arrange int ctx0 = 0; int expected = 20911; - string jsonString = File.ReadAllText(Path.Combine("TestDataWebp", "Vp8Residual.json")); - Vp8Residual residual = JsonSerializer.Deserialize(jsonString); + byte[] data = File.ReadAllBytes(Path.Combine("TestDataWebp", "Vp8Residual.bin")); + BinaryFormatter formatter = new(); + using Stream stream = new MemoryStream(data); + object obj = formatter.Deserialize(stream); + Vp8Residual residual = (Vp8Residual)obj; // act int actual = residual.GetResidualCost(ctx0); @@ -101,8 +95,6 @@ public class Vp8ResidualTests Assert.Equal(expected, actual); } - - private static void CreateRandomProbas(Vp8EncProba probas, Random rand) { for (int t = 0; t < WebpConstants.NumTypes; ++t) diff --git a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj index 270a02c0c..d17cffc4f 100644 --- a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj +++ b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj @@ -56,7 +56,7 @@ PreserveNewest - + PreserveNewest diff --git a/tests/ImageSharp.Tests/TestDataWebp/Vp8Residual.bin b/tests/ImageSharp.Tests/TestDataWebp/Vp8Residual.bin new file mode 100644 index 0000000000000000000000000000000000000000..d96a3c093a7e4e97708b276341d17cbad0047afc GIT binary patch literal 15043 zcmd6t3wRXO6~}irySbS>cJeUTiYQgAeb5jd3bcVLSXvQnDhO?@#SJVNi3!c4B_aqa zFTocIB0j*kf^S7pqauyst3^u{TT4Zup(3J)5|qb!e`mPOegVHa`R3bi_I&wge`n{O zdw0&gGiPQtWQ9VZq<-3- zl$Mm1^eidu-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