Skip to content

Commit 39f8c77

Browse files
committed
Using Binary serialization for Vp8Residual
1 parent 25e6f6e commit 39f8c77

File tree

10 files changed

+20
-85
lines changed

10 files changed

+20
-85
lines changed

src/ImageSharp/Formats/Webp/Lossy/Vp8BandProbas.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy;
88
/// <summary>
99
/// All the probabilities associated to one band.
1010
/// </summary>
11+
[Serializable]
1112
internal class Vp8BandProbas
1213
{
1314
/// <summary>
@@ -22,13 +23,6 @@ public Vp8BandProbas()
2223
}
2324
}
2425

25-
/// <summary>
26-
/// Initializes a new instance of the <see cref="Vp8BandProbas"/> class.
27-
/// Only used for unit tests.
28-
/// </summary>
29-
[JsonConstructor]
30-
public Vp8BandProbas(Vp8ProbaArray[] probabilities) => this.Probabilities = probabilities;
31-
3226
/// <summary>
3327
/// Gets the Probabilities.
3428
/// </summary>
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4-
using System.Text.Json.Serialization;
5-
64
namespace SixLabors.ImageSharp.Formats.Webp.Lossy;
75

6+
[Serializable]
87
internal class Vp8CostArray
98
{
109
/// <summary>
1110
/// Initializes a new instance of the <see cref="Vp8CostArray"/> class.
1211
/// </summary>
1312
public Vp8CostArray() => this.Costs = new ushort[67 + 1];
1413

15-
/// <summary>
16-
/// Initializes a new instance of the <see cref="Vp8CostArray"/> class.
17-
/// Only used for unit tests.
18-
/// </summary>
19-
[JsonConstructor]
20-
public Vp8CostArray(ushort[] costs) => this.Costs = costs;
21-
2214
public ushort[] Costs { get; }
2315
}

src/ImageSharp/Formats/Webp/Lossy/Vp8Costs.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4-
using System.Text.Json.Serialization;
5-
64
namespace SixLabors.ImageSharp.Formats.Webp.Lossy;
75

6+
[Serializable]
87
internal class Vp8Costs
98
{
109
/// <summary>
@@ -19,13 +18,6 @@ public Vp8Costs()
1918
}
2019
}
2120

22-
/// <summary>
23-
/// Initializes a new instance of the <see cref="Vp8Costs"/> class.
24-
/// Only used for unit tests.
25-
/// </summary>
26-
[JsonConstructor]
27-
public Vp8Costs(Vp8CostArray[] costs) => this.Costs = costs;
28-
2921
/// <summary>
3022
/// Gets the Costs.
3123
/// </summary>

src/ImageSharp/Formats/Webp/Lossy/Vp8ProbaArray.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,19 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4-
using System.Text.Json.Serialization;
5-
64
namespace SixLabors.ImageSharp.Formats.Webp.Lossy;
75

86
/// <summary>
97
/// Probabilities associated to one of the contexts.
108
/// </summary>
9+
[Serializable]
1110
internal class Vp8ProbaArray
1211
{
1312
/// <summary>
1413
/// Initializes a new instance of the <see cref="Vp8ProbaArray"/> class.
1514
/// </summary>
1615
public Vp8ProbaArray() => this.Probabilities = new byte[WebpConstants.NumProbas];
1716

18-
/// <summary>
19-
/// Initializes a new instance of the <see cref="Vp8ProbaArray"/> class.
20-
/// Only used for unit tests.
21-
/// </summary>
22-
[JsonConstructor]
23-
public Vp8ProbaArray(byte[] probabilities) => this.Probabilities = probabilities;
24-
2517
/// <summary>
2618
/// Gets the probabilities.
2719
/// </summary>

src/ImageSharp/Formats/Webp/Lossy/Vp8Residual.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,13 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy;
1414
/// <summary>
1515
/// On-the-fly info about the current set of residuals.
1616
/// </summary>
17+
[Serializable]
1718
internal class Vp8Residual
1819
{
1920
public Vp8Residual()
2021
{
2122
}
2223

23-
[JsonConstructor]
24-
public Vp8Residual(int first, int last, int coeffType, short[] coeffs, Vp8BandProbas[] prob, Vp8Stats[] stats, Vp8Costs[] costs)
25-
{
26-
this.First = first;
27-
this.Last = last;
28-
this.CoeffType = coeffType;
29-
this.Coeffs = coeffs;
30-
this.Prob = prob;
31-
this.Stats = stats;
32-
this.Costs = costs;
33-
}
34-
3524
public int First { get; set; }
3625

3726
public int Last { get; set; }
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4-
using System.Text.Json.Serialization;
5-
64
namespace SixLabors.ImageSharp.Formats.Webp.Lossy;
75

6+
[Serializable]
87
internal class Vp8Stats
98
{
109
/// <summary>
@@ -19,12 +18,5 @@ public Vp8Stats()
1918
}
2019
}
2120

22-
/// <summary>
23-
/// Initializes a new instance of the <see cref="Vp8Stats"/> class.
24-
/// Only used for unit tests.
25-
/// </summary>
26-
[JsonConstructor]
27-
public Vp8Stats(Vp8StatsArray[] stats) => this.Stats = stats;
28-
2921
public Vp8StatsArray[] Stats { get; }
3022
}
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4-
using System.Text.Json.Serialization;
5-
64
namespace SixLabors.ImageSharp.Formats.Webp.Lossy;
75

6+
[Serializable]
87
internal class Vp8StatsArray
98
{
109
/// <summary>
1110
/// Initializes a new instance of the <see cref="Vp8StatsArray"/> class.
1211
/// </summary>
1312
public Vp8StatsArray() => this.Stats = new uint[WebpConstants.NumProbas];
1413

15-
/// <summary>
16-
/// Initializes a new instance of the <see cref="Vp8StatsArray"/> class.
17-
/// Only used for unit tests.
18-
/// </summary>
19-
[JsonConstructor]
20-
public Vp8StatsArray(uint[] stats) => this.Stats = stats;
21-
2214
public uint[] Stats { get; }
2315
}

tests/ImageSharp.Tests/Formats/WebP/Vp8ResidualTests.cs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4-
using System.Runtime.Intrinsics.X86;
4+
using System.Runtime.Serialization.Formatters.Binary;
55
using SixLabors.ImageSharp.Formats.Webp;
66
using SixLabors.ImageSharp.Formats.Webp.Lossy;
77
using SixLabors.ImageSharp.Tests.TestUtilities;
8-
using JsonSerializer = System.Text.Json.JsonSerializer;
98

109
namespace SixLabors.ImageSharp.Tests.Formats.Webp;
1110

@@ -15,12 +14,6 @@ public class Vp8ResidualTests
1514
[Fact]
1615
public void Vp8Residual_Serialization_Works()
1716
{
18-
if (!Sse2.IsSupported)
19-
{
20-
// JsonSerializer without SSE2 does not seem to work, skip test then.
21-
return;
22-
}
23-
2417
// arrange
2518
Vp8Residual expected = new();
2619
Vp8EncProba encProb = new();
@@ -34,8 +27,12 @@ public void Vp8Residual_Serialization_Works()
3427
}
3528

3629
// act
37-
string jsonString = JsonSerializer.Serialize(expected);
38-
Vp8Residual actual = JsonSerializer.Deserialize<Vp8Residual>(jsonString);
30+
BinaryFormatter formatter = new();
31+
using MemoryStream ms = new();
32+
formatter.Serialize(ms, expected);
33+
ms.Position = 0;
34+
object obj = formatter.Deserialize(ms);
35+
Vp8Residual actual = (Vp8Residual)obj;
3936

4037
// assert
4138
Assert.Equal(expected.CoeffType, actual.CoeffType);
@@ -82,17 +79,14 @@ public void Vp8Residual_Serialization_Works()
8279
[Fact]
8380
public void GetResidualCost_Works()
8481
{
85-
if (!Sse2.IsSupported)
86-
{
87-
// JsonSerializer without SSE2 does not seem to work, skip test then.
88-
return;
89-
}
90-
9182
// arrange
9283
int ctx0 = 0;
9384
int expected = 20911;
94-
string jsonString = File.ReadAllText(Path.Combine("TestDataWebp", "Vp8Residual.json"));
95-
Vp8Residual residual = JsonSerializer.Deserialize<Vp8Residual>(jsonString);
85+
byte[] data = File.ReadAllBytes(Path.Combine("TestDataWebp", "Vp8Residual.bin"));
86+
BinaryFormatter formatter = new();
87+
using Stream stream = new MemoryStream(data);
88+
object obj = formatter.Deserialize(stream);
89+
Vp8Residual residual = (Vp8Residual)obj;
9690

9791
// act
9892
int actual = residual.GetResidualCost(ctx0);
@@ -101,8 +95,6 @@ public void GetResidualCost_Works()
10195
Assert.Equal(expected, actual);
10296
}
10397

104-
105-
10698
private static void CreateRandomProbas(Vp8EncProba probas, Random rand)
10799
{
108100
for (int t = 0; t < WebpConstants.NumTypes; ++t)

tests/ImageSharp.Tests/ImageSharp.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
<None Update="TestFonts\SixLaborsSampleAB.woff">
5757
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
5858
</None>
59-
<None Update="TestDataWebp\Vp8Residual.json">
59+
<None Update="TestDataWebp\Vp8Residual.bin">
6060
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
6161
</None>
6262
<None Update="xunit.runner.json">
Binary file not shown.

0 commit comments

Comments
 (0)