3
3
4
4
using System ;
5
5
using System . IO ;
6
+ using System . Runtime . CompilerServices ;
7
+ using SixLabors . ImageSharp . Advanced ;
6
8
using SixLabors . ImageSharp . Formats ;
7
9
using SixLabors . ImageSharp . PixelFormats ;
8
10
@@ -11,10 +13,12 @@ namespace SixLabors.ImageSharp.Web
11
13
/// <summary>
12
14
/// A class encapsulating an image with a particular file encoding.
13
15
/// </summary>
14
- /// <seealso cref="IDisposable" />
16
+ /// <seealso cref="IDisposable"/>
15
17
public sealed class FormattedImage : IDisposable
16
18
{
19
+ private readonly ImageFormatManager imageFormatsManager ;
17
20
private IImageFormat format ;
21
+ private IImageEncoder encoder ;
18
22
19
23
/// <summary>
20
24
/// Initializes a new instance of the <see cref="FormattedImage"/> class.
@@ -23,8 +27,9 @@ public sealed class FormattedImage : IDisposable
23
27
/// <param name="format">The format.</param>
24
28
internal FormattedImage ( Image < Rgba32 > image , IImageFormat format )
25
29
{
26
- this . format = format ;
27
30
this . Image = image ;
31
+ this . imageFormatsManager = image . GetConfiguration ( ) . ImageFormatsManager ;
32
+ this . Format = format ;
28
33
}
29
34
30
35
/// <summary>
@@ -38,7 +43,40 @@ internal FormattedImage(Image<Rgba32> image, IImageFormat format)
38
43
public IImageFormat Format
39
44
{
40
45
get => this . format ;
41
- set => this . format = value ?? throw new ArgumentNullException ( nameof ( value ) ) ;
46
+ set
47
+ {
48
+ if ( value is null )
49
+ {
50
+ ThrowNull ( nameof ( value ) ) ;
51
+ }
52
+
53
+ this . format = value ;
54
+ this . encoder = this . imageFormatsManager . FindEncoder ( value ) ;
55
+ }
56
+ }
57
+
58
+ /// <summary>
59
+ /// Gets or sets the encoder.
60
+ /// </summary>
61
+ public IImageEncoder Encoder
62
+ {
63
+ get => this . encoder ;
64
+ set
65
+ {
66
+ if ( value is null )
67
+ {
68
+ ThrowNull ( nameof ( value ) ) ;
69
+ }
70
+
71
+ // The given type should match the format encoder.
72
+ IImageEncoder reference = this . imageFormatsManager . FindEncoder ( this . Format ) ;
73
+ if ( reference . GetType ( ) != value . GetType ( ) )
74
+ {
75
+ ThrowInvalid ( nameof ( value ) ) ;
76
+ }
77
+
78
+ this . encoder = value ;
79
+ }
42
80
}
43
81
44
82
/// <summary>
@@ -54,18 +92,25 @@ public static FormattedImage Load(Configuration configuration, Stream source)
54
92
}
55
93
56
94
/// <summary>
57
- /// Saves the specified destination.
95
+ /// Saves image to the specified destination stream .
58
96
/// </summary>
59
- /// <param name="destination">The destination.</param>
60
- public void Save ( Stream destination ) => this . Image . Save ( destination , this . format ) ;
97
+ /// <param name="destination">The destination stream .</param>
98
+ public void Save ( Stream destination ) => this . Image . Save ( destination , this . encoder ) ;
61
99
62
100
/// <summary>
63
- /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
101
+ /// Performs application-defined tasks associated with freeing, releasing, or resetting
102
+ /// unmanaged resources.
64
103
/// </summary>
65
104
public void Dispose ( )
66
105
{
67
106
this . Image ? . Dispose ( ) ;
68
107
this . Image = null ;
69
108
}
109
+
110
+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
111
+ private static void ThrowNull ( string name ) => throw new ArgumentNullException ( name ) ;
112
+
113
+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
114
+ private static void ThrowInvalid ( string name ) => throw new ArgumentException ( name ) ;
70
115
}
71
116
}
0 commit comments