Skip to content

Commit

Permalink
Merge pull request #1257 from pekspro/LoadExtensions
Browse files Browse the repository at this point in the history
LoadAsync methods accepting string path.
  • Loading branch information
antonfirsov authored Jul 6, 2020
2 parents f80a960 + f0d9784 commit 4a48355
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
54 changes: 54 additions & 0 deletions src/ImageSharp/Image.FromFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.IO;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.PixelFormats;

Expand Down Expand Up @@ -89,6 +90,17 @@ public static IImageInfo Identify(Configuration configuration, string filePath,
public static Image Load(string path)
=> Load(Configuration.Default, path);

/// <summary>
/// Create a new instance of the <see cref="Image"/> class from the given file.
/// </summary>
/// <param name="path">The file path to the image.</param>
/// <exception cref="NotSupportedException">
/// Thrown if the stream is not readable nor seekable.
/// </exception>
/// <returns>A <see cref="Task{Image}"/> representing the asynchronous operation.</returns>
public static Task<Image> LoadAsync(string path)
=> LoadAsync(Configuration.Default, path);

/// <summary>
/// Create a new instance of the <see cref="Image"/> class from the given file.
/// </summary>
Expand All @@ -114,6 +126,25 @@ public static Image Load(string path, out IImageFormat format)
public static Image Load(Configuration configuration, string path)
=> Load(configuration, path, out _);

/// <summary>
/// Create a new instance of the <see cref="Image"/> class from the given file.
/// </summary>
/// <param name="configuration">The configuration for the decoder.</param>
/// <param name="path">The file path to the image.</param>
/// <exception cref="ArgumentNullException">The configuration is null.</exception>
/// <exception cref="ArgumentNullException">The path is null.</exception>
/// <exception cref="UnknownImageFormatException">Image format not recognised.</exception>
/// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
/// <returns>A <see cref="Task{Image}"/> representing the asynchronous operation.</returns>
public static async Task<Image> LoadAsync(Configuration configuration, string path)
{
using (Stream stream = configuration.FileSystem.OpenRead(path))
{
(Image img, _) = await LoadWithFormatAsync(configuration, stream).ConfigureAwait(false);
return img;
}
}

/// <summary>
/// Create a new instance of the <see cref="Image"/> class from the given file.
/// </summary>
Expand All @@ -137,6 +168,29 @@ public static Image Load(Configuration configuration, string path, IImageDecoder
}
}

/// <summary>
/// Create a new instance of the <see cref="Image"/> class from the given file.
/// </summary>
/// <param name="configuration">The Configuration.</param>
/// <param name="path">The file path to the image.</param>
/// <param name="decoder">The decoder.</param>
/// <exception cref="ArgumentNullException">The configuration is null.</exception>
/// <exception cref="ArgumentNullException">The path is null.</exception>
/// <exception cref="ArgumentNullException">The decoder is null.</exception>
/// <exception cref="UnknownImageFormatException">Image format not recognised.</exception>
/// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
/// <returns>A <see cref="Task{Image}"/> representing the asynchronous operation.</returns>
public static Task<Image> LoadAsync(Configuration configuration, string path, IImageDecoder decoder)
{
Guard.NotNull(configuration, nameof(configuration));
Guard.NotNull(path, nameof(path));

using (Stream stream = configuration.FileSystem.OpenRead(path))
{
return LoadAsync(configuration, stream, decoder);
}
}

/// <summary>
/// Create a new instance of the <see cref="Image"/> class from the given file.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the Apache License, Version 2.0.

using System;

using System.Threading.Tasks;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Bmp;
using SixLabors.ImageSharp.PixelFormats;
Expand Down Expand Up @@ -40,6 +40,24 @@ public void Path_Agnostic()
}
}

[Fact]
public async Task Path_Agnostic_Async()
{
using (var img = await Image.LoadAsync(this.Path))
{
VerifyDecodedImage(img);
}
}

[Fact]
public async Task Path_Agnostic_Configuration_Async()
{
using (var img = await Image.LoadAsync(Configuration.Default, this.Path))
{
VerifyDecodedImage(img);
}
}

[Fact]
public void Path_Decoder_Specific()
{
Expand All @@ -58,6 +76,15 @@ public void Path_Decoder_Agnostic()
}
}

[Fact]
public async Task Path_Decoder_Agnostic_Async()
{
using (var img = await Image.LoadAsync(Configuration.Default, this.Path, new BmpDecoder()))
{
VerifyDecodedImage(img);
}
}

[Fact]
public void Path_OutFormat_Specific()
{
Expand Down

0 comments on commit 4a48355

Please sign in to comment.