11// Copyright (c) Six Labors.
22// Licensed under the Six Labors Split License.
33
4- using System . Runtime . Serialization . Formatters . Binary ;
4+ using System . Text ;
55using SixLabors . ImageSharp . Formats . Webp ;
66using SixLabors . ImageSharp . Formats . Webp . Lossy ;
77using SixLabors . ImageSharp . Tests . TestUtilities ;
@@ -11,6 +11,117 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp;
1111[ Trait ( "Format" , "Webp" ) ]
1212public class Vp8ResidualTests
1313{
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+
14125 [ Fact ]
15126 public void Vp8Residual_Serialization_Works ( )
16127 {
@@ -27,12 +138,10 @@ public void Vp8Residual_Serialization_Works()
27138 }
28139
29140 // 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 ) ;
36145
37146 // assert
38147 Assert . Equal ( expected . CoeffType , actual . CoeffType ) ;
@@ -82,11 +191,7 @@ public void GetResidualCost_Works()
82191 // arrange
83192 int ctx0 = 0 ;
84193 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" ) ) ;
90195
91196 // act
92197 int actual = residual . GetResidualCost ( ctx0 ) ;
0 commit comments