@@ -24,6 +24,10 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
2424 [ Trait ( "Format" , "Jpg" ) ]
2525 public class JpegEncoderTests
2626 {
27+ private static JpegEncoder JpegEncoder => new JpegEncoder ( ) ;
28+
29+ private static JpegDecoder JpegDecoder => new JpegDecoder ( ) ;
30+
2731 public static readonly TheoryData < string , int > QualityFiles =
2832 new TheoryData < string , int >
2933 {
@@ -62,17 +66,37 @@ public class JpegEncoderTests
6266 } ;
6367
6468 [ Theory ]
65- [ MemberData ( nameof ( QualityFiles ) ) ]
66- public void Encode_PreserveQuality ( string imagePath , int quality )
69+ [ WithFile ( TestImages . Jpeg . Baseline . Floorplan , PixelTypes . Rgba32 , JpegColorType . Luminance ) ]
70+ [ WithFile ( TestImages . Jpeg . Baseline . Jpeg444 , PixelTypes . Rgba32 , JpegColorType . YCbCrRatio444 ) ]
71+ [ WithFile ( TestImages . Jpeg . Baseline . Jpeg420Small , PixelTypes . Rgba32 , JpegColorType . YCbCrRatio420 ) ]
72+ [ WithFile ( TestImages . Jpeg . Baseline . JpegRgb , PixelTypes . Rgba32 , JpegColorType . Rgb ) ]
73+ public void Encode_PreservesColorType < TPixel > ( TestImageProvider < TPixel > provider , JpegColorType expectedColorType )
74+ where TPixel : unmanaged, IPixel < TPixel >
6775 {
68- var options = new JpegEncoder ( ) ;
76+ // arrange
77+ using Image < TPixel > input = provider . GetImage ( JpegDecoder ) ;
78+ using var memoryStream = new MemoryStream ( ) ;
79+
80+ // act
81+ input . Save ( memoryStream , JpegEncoder ) ;
6982
83+ // assert
84+ memoryStream . Position = 0 ;
85+ using var output = Image . Load < Rgba32 > ( memoryStream ) ;
86+ JpegMetadata meta = output . Metadata . GetJpegMetadata ( ) ;
87+ Assert . Equal ( expectedColorType , meta . ColorType ) ;
88+ }
89+
90+ [ Theory ]
91+ [ MemberData ( nameof ( QualityFiles ) ) ]
92+ public void Encode_PreservesQuality ( string imagePath , int quality )
93+ {
7094 var testFile = TestFile . Create ( imagePath ) ;
7195 using ( Image < Rgba32 > input = testFile . CreateRgba32Image ( ) )
7296 {
7397 using ( var memStream = new MemoryStream ( ) )
7498 {
75- input . Save ( memStream , options ) ;
99+ input . Save ( memStream , JpegEncoder ) ;
76100
77101 memStream . Position = 0 ;
78102 using ( var output = Image . Load < Rgba32 > ( memStream ) )
@@ -226,14 +250,12 @@ public void Quality_0_And_100_Are_Not_Identical()
226250 [ MemberData ( nameof ( RatioFiles ) ) ]
227251 public void Encode_PreserveRatio ( string imagePath , int xResolution , int yResolution , PixelResolutionUnit resolutionUnit )
228252 {
229- var options = new JpegEncoder ( ) ;
230-
231253 var testFile = TestFile . Create ( imagePath ) ;
232254 using ( Image < Rgba32 > input = testFile . CreateRgba32Image ( ) )
233255 {
234256 using ( var memStream = new MemoryStream ( ) )
235257 {
236- input . Save ( memStream , options ) ;
258+ input . Save ( memStream , JpegEncoder ) ;
237259
238260 memStream . Position = 0 ;
239261 using ( var output = Image . Load < Rgba32 > ( memStream ) )
@@ -254,11 +276,10 @@ public void Encode_PreservesIptcProfile()
254276 using var input = new Image < Rgba32 > ( 1 , 1 ) ;
255277 input . Metadata . IptcProfile = new IptcProfile ( ) ;
256278 input . Metadata . IptcProfile . SetValue ( IptcTag . Byline , "unit_test" ) ;
257- var encoder = new JpegEncoder ( ) ;
258279
259280 // act
260281 using var memStream = new MemoryStream ( ) ;
261- input . Save ( memStream , encoder ) ;
282+ input . Save ( memStream , JpegEncoder ) ;
262283
263284 // assert
264285 memStream . Position = 0 ;
@@ -276,11 +297,10 @@ public void Encode_PreservesExifProfile()
276297 using var input = new Image < Rgba32 > ( 1 , 1 ) ;
277298 input . Metadata . ExifProfile = new ExifProfile ( ) ;
278299 input . Metadata . ExifProfile . SetValue ( ExifTag . Software , "unit_test" ) ;
279- var encoder = new JpegEncoder ( ) ;
280300
281301 // act
282302 using var memStream = new MemoryStream ( ) ;
283- input . Save ( memStream , encoder ) ;
303+ input . Save ( memStream , JpegEncoder ) ;
284304
285305 // assert
286306 memStream . Position = 0 ;
@@ -297,11 +317,10 @@ public void Encode_PreservesIccProfile()
297317 // arrange
298318 using var input = new Image < Rgba32 > ( 1 , 1 ) ;
299319 input . Metadata . IccProfile = new IccProfile ( IccTestDataProfiles . Profile_Random_Array ) ;
300- var encoder = new JpegEncoder ( ) ;
301320
302321 // act
303322 using var memStream = new MemoryStream ( ) ;
304- input . Save ( memStream , encoder ) ;
323+ input . Save ( memStream , JpegEncoder ) ;
305324
306325 // assert
307326 memStream . Position = 0 ;
0 commit comments