diff --git a/src/ImageSharp/Formats/Webp/Lossy/Vp8BandProbas.cs b/src/ImageSharp/Formats/Webp/Lossy/Vp8BandProbas.cs index 90506efb8..86304d4bd 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8BandProbas.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8BandProbas.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. +using System.Text.Json.Serialization; + namespace SixLabors.ImageSharp.Formats.Webp.Lossy; /// @@ -20,6 +22,13 @@ 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 2c8d723d7..8268e1c02 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8CostArray.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8CostArray.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. +using System.Text.Json.Serialization; + namespace SixLabors.ImageSharp.Formats.Webp.Lossy; internal class Vp8CostArray @@ -10,5 +12,12 @@ 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 eee22159e..2441436fb 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8Costs.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8Costs.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. +using System.Text.Json.Serialization; + namespace SixLabors.ImageSharp.Formats.Webp.Lossy; internal class Vp8Costs @@ -17,6 +19,13 @@ 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 337527542..a19f9742f 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8ProbaArray.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8ProbaArray.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. +using System.Text.Json.Serialization; + namespace SixLabors.ImageSharp.Formats.Webp.Lossy; /// @@ -13,6 +15,13 @@ 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 68bf09f94..6e0937d86 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8Residual.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8Residual.cs @@ -7,6 +7,7 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Intrinsics; using System.Runtime.Intrinsics.X86; +using System.Text.Json.Serialization; namespace SixLabors.ImageSharp.Formats.Webp.Lossy; @@ -15,6 +16,22 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy; /// 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 dda921a7c..b6c05f2d4 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8Stats.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8Stats.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. +using System.Text.Json.Serialization; + namespace SixLabors.ImageSharp.Formats.Webp.Lossy; internal class Vp8Stats @@ -17,5 +19,12 @@ 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 2fbba6996..864248dd8 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8StatsArray.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8StatsArray.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. +using System.Text.Json.Serialization; + namespace SixLabors.ImageSharp.Formats.Webp.Lossy; internal class Vp8StatsArray @@ -10,5 +12,12 @@ 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/Serialization/Vp8CostArrayJsonConverter.cs b/tests/ImageSharp.Tests/Formats/WebP/Serialization/Vp8CostArrayJsonConverter.cs deleted file mode 100644 index b3cfd0340..000000000 --- a/tests/ImageSharp.Tests/Formats/WebP/Serialization/Vp8CostArrayJsonConverter.cs +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright (c) Six Labors. -// Licensed under the Six Labors Split License. - -using System.Text.Json; -using System.Text.Json.Serialization; -using SixLabors.ImageSharp.Formats.Webp.Lossy; - -namespace SixLabors.ImageSharp.Tests.Formats.WebP.Serialization; - -internal class Vp8CostArrayJsonConverter : JsonConverter -{ - public override Vp8CostArray Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => throw new NotImplementedException(); - - public override void Write(Utf8JsonWriter writer, Vp8CostArray value, JsonSerializerOptions options) => throw new NotImplementedException(); -} diff --git a/tests/ImageSharp.Tests/Formats/WebP/Vp8ResidualTests.cs b/tests/ImageSharp.Tests/Formats/WebP/Vp8ResidualTests.cs index 457f3576f..20a161371 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/Vp8ResidualTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/Vp8ResidualTests.cs @@ -2,10 +2,8 @@ // Licensed under the Six Labors Split License. using System.Runtime.Intrinsics.X86; -using System.Text.Json; using SixLabors.ImageSharp.Formats.Webp; using SixLabors.ImageSharp.Formats.Webp.Lossy; -using SixLabors.ImageSharp.Tests.Formats.WebP.Serialization; using SixLabors.ImageSharp.Tests.TestUtilities; using JsonSerializer = System.Text.Json.JsonSerializer; @@ -14,29 +12,6 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp; [Trait("Format", "Webp")] public class Vp8ResidualTests { - [Fact] - public void Vp8CostArray_Serialization_Works() - { - // arrange - Vp8CostArray expected = new(); - for (ushort i = 0; i < expected.Costs.Length; i++) - { - expected.Costs[i] = i; - } - - JsonSerializerOptions options = new() - { - Converters = { new Vp8CostArrayJsonConverter() } - }; - - // act - string jsonString = JsonSerializer.Serialize(expected); - Vp8CostArray actual = JsonSerializer.Deserialize(jsonString, options); - - // assert - Assert.Equal(expected.Costs, actual.Costs); - } - [Fact] public void Vp8Residual_Serialization_Works() {