1
1
// Copyright (c) Six Labors.
2
2
// Licensed under the Six Labors Split License.
3
3
4
- using System . Runtime . Serialization . Formatters . Binary ;
4
+ using System . Text ;
5
5
using SixLabors . ImageSharp . Formats . Webp ;
6
6
using SixLabors . ImageSharp . Formats . Webp . Lossy ;
7
7
using SixLabors . ImageSharp . Tests . TestUtilities ;
@@ -11,6 +11,117 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp;
11
11
[ Trait ( "Format" , "Webp" ) ]
12
12
public class Vp8ResidualTests
13
13
{
14
+ private static void WriteVp8Residual ( string filename , Vp8Residual residual )
15
+ {
16
+ using FileStream stream = File . Open ( filename , FileMode . Create ) ;
17
+ using BinaryWriter writer = new ( stream , Encoding . UTF8 , false ) ;
18
+
19
+ writer . Write ( residual . First ) ;
20
+ writer . Write ( residual . Last ) ;
21
+ writer . Write ( residual . CoeffType ) ;
22
+
23
+ for ( int i = 0 ; i < residual . Coeffs . Length ; i ++ )
24
+ {
25
+ writer . Write ( residual . Coeffs [ i ] ) ;
26
+ }
27
+
28
+ for ( int i = 0 ; i < residual . Prob . Length ; i ++ )
29
+ {
30
+ for ( int j = 0 ; j < residual . Prob [ i ] . Probabilities . Length ; j ++ )
31
+ {
32
+ writer . Write ( residual . Prob [ i ] . Probabilities [ j ] . Probabilities ) ;
33
+ }
34
+ }
35
+
36
+ for ( int i = 0 ; i < residual . Costs . Length ; i ++ )
37
+ {
38
+ Vp8Costs costs = residual . Costs [ i ] ;
39
+ Vp8CostArray [ ] costsArray = costs . Costs ;
40
+ for ( int j = 0 ; j < costsArray . Length ; j ++ )
41
+ {
42
+ for ( int k = 0 ; k < costsArray [ j ] . Costs . Length ; k ++ )
43
+ {
44
+ writer . Write ( costsArray [ j ] . Costs [ k ] ) ;
45
+ }
46
+ }
47
+ }
48
+
49
+ for ( int i = 0 ; i < residual . Stats . Length ; i ++ )
50
+ {
51
+ for ( int j = 0 ; j < residual . Stats [ i ] . Stats . Length ; j ++ )
52
+ {
53
+ for ( int k = 0 ; k < residual . Stats [ i ] . Stats [ j ] . Stats . Length ; k ++ )
54
+ {
55
+ writer . Write ( residual . Stats [ i ] . Stats [ j ] . Stats [ k ] ) ;
56
+ }
57
+ }
58
+ }
59
+
60
+ writer . Flush ( ) ;
61
+ }
62
+
63
+ private static Vp8Residual ReadVp8Residual ( string fileName )
64
+ {
65
+ using FileStream stream = File . Open ( fileName , FileMode . Open ) ;
66
+ using BinaryReader reader = new ( stream , Encoding . UTF8 , false ) ;
67
+
68
+ Vp8Residual residual = new ( )
69
+ {
70
+ First = reader . ReadInt32 ( ) ,
71
+ Last = reader . ReadInt32 ( ) ,
72
+ CoeffType = reader . ReadInt32 ( )
73
+ } ;
74
+
75
+ for ( int i = 0 ; i < residual . Coeffs . Length ; i ++ )
76
+ {
77
+ residual . Coeffs [ i ] = reader . ReadInt16 ( ) ;
78
+ }
79
+
80
+ Vp8BandProbas [ ] bandProbas = new Vp8BandProbas [ 8 ] ;
81
+ for ( int i = 0 ; i < bandProbas . Length ; i ++ )
82
+ {
83
+ bandProbas [ i ] = new Vp8BandProbas ( ) ;
84
+ for ( int j = 0 ; j < bandProbas [ i ] . Probabilities . Length ; j ++ )
85
+ {
86
+ for ( int k = 0 ; k < 11 ; k ++ )
87
+ {
88
+ bandProbas [ i ] . Probabilities [ j ] . Probabilities [ k ] = reader . ReadByte ( ) ;
89
+ }
90
+ }
91
+ }
92
+
93
+ residual . Prob = bandProbas ;
94
+
95
+ residual . Costs = new Vp8Costs [ 16 ] ;
96
+ for ( int i = 0 ; i < residual . Costs . Length ; i ++ )
97
+ {
98
+ residual . Costs [ i ] = new Vp8Costs ( ) ;
99
+ Vp8CostArray [ ] costsArray = residual . Costs [ i ] . Costs ;
100
+ for ( int j = 0 ; j < costsArray . Length ; j ++ )
101
+ {
102
+ for ( int k = 0 ; k < costsArray [ j ] . Costs . Length ; k ++ )
103
+ {
104
+ costsArray [ j ] . Costs [ k ] = reader . ReadUInt16 ( ) ;
105
+ }
106
+ }
107
+ }
108
+
109
+ residual . Stats = new Vp8Stats [ 8 ] ;
110
+ for ( int i = 0 ; i < residual . Stats . Length ; i ++ )
111
+ {
112
+ residual . Stats [ i ] = new Vp8Stats ( ) ;
113
+ for ( int j = 0 ; j < residual . Stats [ i ] . Stats . Length ; j ++ )
114
+ {
115
+ for ( int k = 0 ; k < residual . Stats [ i ] . Stats [ j ] . Stats . Length ; k ++ )
116
+ {
117
+ residual . Stats [ i ] . Stats [ j ] . Stats [ k ] = reader . ReadUInt32 ( ) ;
118
+ }
119
+ }
120
+ }
121
+
122
+ return residual ;
123
+ }
124
+
14
125
[ Fact ]
15
126
public void Vp8Residual_Serialization_Works ( )
16
127
{
@@ -27,12 +138,10 @@ public void Vp8Residual_Serialization_Works()
27
138
}
28
139
29
140
// act
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 ;
141
+ string fileName = "Vp8SerializationTest.bin" ;
142
+ WriteVp8Residual ( fileName , expected ) ;
143
+ Vp8Residual actual = ReadVp8Residual ( fileName ) ;
144
+ File . Delete ( fileName ) ;
36
145
37
146
// assert
38
147
Assert . Equal ( expected . CoeffType , actual . CoeffType ) ;
@@ -82,11 +191,7 @@ public void GetResidualCost_Works()
82
191
// arrange
83
192
int ctx0 = 0 ;
84
193
int expected = 20911 ;
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 ;
194
+ Vp8Residual residual = ReadVp8Residual ( Path . Combine ( "TestDataWebp" , "Vp8Residual.bin" ) ) ;
90
195
91
196
// act
92
197
int actual = residual . GetResidualCost ( ctx0 ) ;
0 commit comments