Skip to content

Commit

Permalink
Using Binary serialization for Vp8Residual
Browse files Browse the repository at this point in the history
  • Loading branch information
brianpopow committed Apr 14, 2023
1 parent 25e6f6e commit 39f8c77
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 85 deletions.
8 changes: 1 addition & 7 deletions src/ImageSharp/Formats/Webp/Lossy/Vp8BandProbas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy;
/// <summary>
/// All the probabilities associated to one band.
/// </summary>
[Serializable]
internal class Vp8BandProbas
{
/// <summary>
Expand All @@ -22,13 +23,6 @@ public Vp8BandProbas()
}
}

/// <summary>
/// Initializes a new instance of the <see cref="Vp8BandProbas"/> class.
/// Only used for unit tests.
/// </summary>
[JsonConstructor]
public Vp8BandProbas(Vp8ProbaArray[] probabilities) => this.Probabilities = probabilities;

/// <summary>
/// Gets the Probabilities.
/// </summary>
Expand Down
10 changes: 1 addition & 9 deletions src/ImageSharp/Formats/Webp/Lossy/Vp8CostArray.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
// 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
{
/// <summary>
/// Initializes a new instance of the <see cref="Vp8CostArray"/> class.
/// </summary>
public Vp8CostArray() => this.Costs = new ushort[67 + 1];

/// <summary>
/// Initializes a new instance of the <see cref="Vp8CostArray"/> class.
/// Only used for unit tests.
/// </summary>
[JsonConstructor]
public Vp8CostArray(ushort[] costs) => this.Costs = costs;

public ushort[] Costs { get; }
}
10 changes: 1 addition & 9 deletions src/ImageSharp/Formats/Webp/Lossy/Vp8Costs.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
Expand All @@ -19,13 +18,6 @@ public Vp8Costs()
}
}

/// <summary>
/// Initializes a new instance of the <see cref="Vp8Costs"/> class.
/// Only used for unit tests.
/// </summary>
[JsonConstructor]
public Vp8Costs(Vp8CostArray[] costs) => this.Costs = costs;

/// <summary>
/// Gets the Costs.
/// </summary>
Expand Down
10 changes: 1 addition & 9 deletions src/ImageSharp/Formats/Webp/Lossy/Vp8ProbaArray.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

using System.Text.Json.Serialization;

namespace SixLabors.ImageSharp.Formats.Webp.Lossy;

/// <summary>
/// Probabilities associated to one of the contexts.
/// </summary>
[Serializable]
internal class Vp8ProbaArray
{
/// <summary>
/// Initializes a new instance of the <see cref="Vp8ProbaArray"/> class.
/// </summary>
public Vp8ProbaArray() => this.Probabilities = new byte[WebpConstants.NumProbas];

/// <summary>
/// Initializes a new instance of the <see cref="Vp8ProbaArray"/> class.
/// Only used for unit tests.
/// </summary>
[JsonConstructor]
public Vp8ProbaArray(byte[] probabilities) => this.Probabilities = probabilities;

/// <summary>
/// Gets the probabilities.
/// </summary>
Expand Down
13 changes: 1 addition & 12 deletions src/ImageSharp/Formats/Webp/Lossy/Vp8Residual.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,13 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy;
/// <summary>
/// On-the-fly info about the current set of residuals.
/// </summary>
[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; }
Expand Down
10 changes: 1 addition & 9 deletions src/ImageSharp/Formats/Webp/Lossy/Vp8Stats.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
Expand All @@ -19,12 +18,5 @@ public Vp8Stats()
}
}

/// <summary>
/// Initializes a new instance of the <see cref="Vp8Stats"/> class.
/// Only used for unit tests.
/// </summary>
[JsonConstructor]
public Vp8Stats(Vp8StatsArray[] stats) => this.Stats = stats;

public Vp8StatsArray[] Stats { get; }
}
10 changes: 1 addition & 9 deletions src/ImageSharp/Formats/Webp/Lossy/Vp8StatsArray.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
// 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
{
/// <summary>
/// Initializes a new instance of the <see cref="Vp8StatsArray"/> class.
/// </summary>
public Vp8StatsArray() => this.Stats = new uint[WebpConstants.NumProbas];

/// <summary>
/// Initializes a new instance of the <see cref="Vp8StatsArray"/> class.
/// Only used for unit tests.
/// </summary>
[JsonConstructor]
public Vp8StatsArray(uint[] stats) => this.Stats = stats;

public uint[] Stats { get; }
}
32 changes: 12 additions & 20 deletions tests/ImageSharp.Tests/Formats/WebP/Vp8ResidualTests.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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();
Expand All @@ -34,8 +27,12 @@ public void Vp8Residual_Serialization_Works()
}

// act
string jsonString = JsonSerializer.Serialize(expected);
Vp8Residual actual = JsonSerializer.Deserialize<Vp8Residual>(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);
Expand Down Expand Up @@ -82,17 +79,14 @@ public void Vp8Residual_Serialization_Works()
[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<Vp8Residual>(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);
Expand All @@ -101,8 +95,6 @@ public void GetResidualCost_Works()
Assert.Equal(expected, actual);
}



private static void CreateRandomProbas(Vp8EncProba probas, Random rand)
{
for (int t = 0; t < WebpConstants.NumTypes; ++t)
Expand Down
2 changes: 1 addition & 1 deletion tests/ImageSharp.Tests/ImageSharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<None Update="TestFonts\SixLaborsSampleAB.woff">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestDataWebp\Vp8Residual.json">
<None Update="TestDataWebp\Vp8Residual.bin">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="xunit.runner.json">
Expand Down
Binary file not shown.

0 comments on commit 39f8c77

Please sign in to comment.