Skip to content

Commit 8bd273f

Browse files
Merge pull request #2514 from SixLabors/js/smaller-aot
Expose non-nullable configuration to remove AOT limiting null check
2 parents d378306 + 8908dba commit 8bd273f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+141
-191
lines changed

src/ImageSharp/Advanced/AdvancedImageExtensions.cs

+4-36
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,25 @@ public static IImageEncoder DetectEncoder(this Image source, string filePath)
2727
Guard.NotNull(filePath, nameof(filePath));
2828

2929
string ext = Path.GetExtension(filePath);
30-
if (!source.GetConfiguration().ImageFormatsManager.TryFindFormatByFileExtension(ext, out IImageFormat? format))
30+
if (!source.Configuration.ImageFormatsManager.TryFindFormatByFileExtension(ext, out IImageFormat? format))
3131
{
3232
StringBuilder sb = new();
3333
sb = sb.AppendLine(CultureInfo.InvariantCulture, $"No encoder was found for extension '{ext}'. Registered encoders include:");
34-
foreach (IImageFormat fmt in source.GetConfiguration().ImageFormats)
34+
foreach (IImageFormat fmt in source.Configuration.ImageFormats)
3535
{
3636
sb = sb.AppendFormat(CultureInfo.InvariantCulture, " - {0} : {1}{2}", fmt.Name, string.Join(", ", fmt.FileExtensions), Environment.NewLine);
3737
}
3838

3939
throw new UnknownImageFormatException(sb.ToString());
4040
}
4141

42-
IImageEncoder? encoder = source.GetConfiguration().ImageFormatsManager.GetEncoder(format);
42+
IImageEncoder? encoder = source.Configuration.ImageFormatsManager.GetEncoder(format);
4343

4444
if (encoder is null)
4545
{
4646
StringBuilder sb = new();
4747
sb = sb.AppendLine(CultureInfo.InvariantCulture, $"No encoder was found for extension '{ext}' using image format '{format.Name}'. Registered encoders include:");
48-
foreach (KeyValuePair<IImageFormat, IImageEncoder> enc in source.GetConfiguration().ImageFormatsManager.ImageEncoders)
48+
foreach (KeyValuePair<IImageFormat, IImageEncoder> enc in source.Configuration.ImageFormatsManager.ImageEncoders)
4949
{
5050
sb = sb.AppendFormat(CultureInfo.InvariantCulture, " - {0} : {1}{2}", enc.Key, enc.Value.GetType().Name, Environment.NewLine);
5151
}
@@ -76,30 +76,6 @@ public static void AcceptVisitor(this Image source, IImageVisitor visitor)
7676
public static Task AcceptVisitorAsync(this Image source, IImageVisitorAsync visitor, CancellationToken cancellationToken = default)
7777
=> source.AcceptAsync(visitor, cancellationToken);
7878

79-
/// <summary>
80-
/// Gets the configuration for the image.
81-
/// </summary>
82-
/// <param name="source">The source image.</param>
83-
/// <returns>Returns the configuration.</returns>
84-
public static Configuration GetConfiguration(this Image source)
85-
=> GetConfiguration((IConfigurationProvider)source);
86-
87-
/// <summary>
88-
/// Gets the configuration for the image frame.
89-
/// </summary>
90-
/// <param name="source">The source image.</param>
91-
/// <returns>Returns the configuration.</returns>
92-
public static Configuration GetConfiguration(this ImageFrame source)
93-
=> GetConfiguration((IConfigurationProvider)source);
94-
95-
/// <summary>
96-
/// Gets the configuration.
97-
/// </summary>
98-
/// <param name="source">The source image</param>
99-
/// <returns>Returns the bounds of the image</returns>
100-
private static Configuration GetConfiguration(IConfigurationProvider source)
101-
=> source?.Configuration ?? Configuration.Default;
102-
10379
/// <summary>
10480
/// Gets the representation of the pixels as a <see cref="IMemoryGroup{T}"/> containing the backing pixel data of the image
10581
/// stored in row major order, as a list of contiguous <see cref="Memory{T}"/> blocks in the source image's pixel format.
@@ -167,12 +143,4 @@ public static Memory<TPixel> DangerousGetPixelRowMemory<TPixel>(this Image<TPixe
167143

168144
return source.Frames.RootFrame.PixelBuffer.GetSafeRowMemory(rowIndex);
169145
}
170-
171-
/// <summary>
172-
/// Gets the <see cref="MemoryAllocator"/> assigned to 'source'.
173-
/// </summary>
174-
/// <param name="source">The source image.</param>
175-
/// <returns>Returns the configuration.</returns>
176-
internal static MemoryAllocator GetMemoryAllocator(this IConfigurationProvider source)
177-
=> GetConfiguration(source).MemoryAllocator;
178146
}

src/ImageSharp/Advanced/IConfigurationProvider.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace SixLabors.ImageSharp.Advanced;
66
/// <summary>
77
/// Defines the contract for objects that can provide access to configuration.
88
/// </summary>
9-
internal interface IConfigurationProvider
9+
public interface IConfigurationProvider
1010
{
1111
/// <summary>
1212
/// Gets the configuration which allows altering default behaviour or extending the library.

src/ImageSharp/Formats/Bmp/BmpEncoder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public sealed class BmpEncoder : QuantizingImageEncoder
3232
/// <inheritdoc/>
3333
protected override void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken cancellationToken)
3434
{
35-
BmpEncoderCore encoder = new(this, image.GetMemoryAllocator());
35+
BmpEncoderCore encoder = new(this, image.Configuration.MemoryAllocator);
3636
encoder.Encode(image, stream, cancellationToken);
3737
}
3838
}

src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
119119
Guard.NotNull(image, nameof(image));
120120
Guard.NotNull(stream, nameof(stream));
121121

122-
Configuration configuration = image.GetConfiguration();
122+
Configuration configuration = image.Configuration;
123123
ImageMetadata metadata = image.Metadata;
124124
BmpMetadata bmpMetadata = metadata.GetBmpMetadata();
125125
this.bitsPerPixel ??= bmpMetadata.BitsPerPixel;

src/ImageSharp/Formats/Gif/GifEncoder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public sealed class GifEncoder : QuantizingImageEncoder
1818
/// <inheritdoc/>
1919
protected override void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken cancellationToken)
2020
{
21-
GifEncoderCore encoder = new(image.GetConfiguration(), this);
21+
GifEncoderCore encoder = new(image.Configuration, this);
2222
encoder.Encode(image, stream, cancellationToken);
2323
}
2424
}

src/ImageSharp/Formats/Gif/GifEncoderCore.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ private void EncodeAdditionalFrames<TPixel>(
189189
// This frame is reused to store de-duplicated pixel buffers.
190190
// This is more expensive memory-wise than de-duplicating indexed buffer but allows us to deduplicate
191191
// frames using both local and global palettes.
192-
using ImageFrame<TPixel> encodingFrame = new(previousFrame.GetConfiguration(), previousFrame.Size());
192+
using ImageFrame<TPixel> encodingFrame = new(previousFrame.Configuration, previousFrame.Size());
193193

194194
for (int i = 1; i < image.Frames.Count; i++)
195195
{

src/ImageSharp/Formats/ImageEncoder.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ protected abstract void Encode<TPixel>(Image<TPixel> image, Stream stream, Cance
4242
private void EncodeWithSeekableStream<TPixel>(Image<TPixel> image, Stream stream, CancellationToken cancellationToken)
4343
where TPixel : unmanaged, IPixel<TPixel>
4444
{
45-
Configuration configuration = image.GetConfiguration();
45+
Configuration configuration = image.Configuration;
4646
if (stream.CanSeek)
4747
{
4848
this.Encode(image, stream, cancellationToken);
@@ -59,7 +59,7 @@ private void EncodeWithSeekableStream<TPixel>(Image<TPixel> image, Stream stream
5959
private async Task EncodeWithSeekableStreamAsync<TPixel>(Image<TPixel> image, Stream stream, CancellationToken cancellationToken)
6060
where TPixel : unmanaged, IPixel<TPixel>
6161
{
62-
Configuration configuration = image.GetConfiguration();
62+
Configuration configuration = image.Configuration;
6363
if (stream.CanSeek)
6464
{
6565
await DoEncodeAsync(stream).ConfigureAwait(false);

0 commit comments

Comments
 (0)