Skip to content

Commit cbfff38

Browse files
Merge pull request #1856 from justinbhopper/master
Added missing CancellationToken parameters to Image
2 parents 013c29e + c0ee67b commit cbfff38

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

src/ImageSharp/Image.FromFile.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,21 +255,23 @@ public static Task<Image> LoadAsync(string path, CancellationToken cancellationT
255255
/// </summary>
256256
/// <param name="path">The file path to the image.</param>
257257
/// <param name="decoder">The decoder.</param>
258+
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
258259
/// <exception cref="ArgumentNullException">The configuration is null.</exception>
259260
/// <exception cref="ArgumentNullException">The path is null.</exception>
260261
/// <exception cref="ArgumentNullException">The decoder is null.</exception>
261262
/// <exception cref="UnknownImageFormatException">Image format not recognised.</exception>
262263
/// <exception cref="NotSupportedException">Image format is not supported.</exception>
263264
/// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
264265
/// <returns>A <see cref="Task{Image}"/> representing the asynchronous operation.</returns>
265-
public static Task<Image> LoadAsync(string path, IImageDecoder decoder)
266-
=> LoadAsync(Configuration.Default, path, decoder, default);
266+
public static Task<Image> LoadAsync(string path, IImageDecoder decoder, CancellationToken cancellationToken = default)
267+
=> LoadAsync(Configuration.Default, path, decoder, cancellationToken);
267268

268269
/// <summary>
269270
/// Create a new instance of the <see cref="Image"/> class from the given file.
270271
/// </summary>
271272
/// <param name="path">The file path to the image.</param>
272273
/// <param name="decoder">The decoder.</param>
274+
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
273275
/// <exception cref="ArgumentNullException">The configuration is null.</exception>
274276
/// <exception cref="ArgumentNullException">The path is null.</exception>
275277
/// <exception cref="ArgumentNullException">The decoder is null.</exception>
@@ -278,9 +280,9 @@ public static Task<Image> LoadAsync(string path, IImageDecoder decoder)
278280
/// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
279281
/// <typeparam name="TPixel">The pixel format.</typeparam>
280282
/// <returns>A <see cref="Task{Image}"/> representing the asynchronous operation.</returns>
281-
public static Task<Image<TPixel>> LoadAsync<TPixel>(string path, IImageDecoder decoder)
283+
public static Task<Image<TPixel>> LoadAsync<TPixel>(string path, IImageDecoder decoder, CancellationToken cancellationToken = default)
282284
where TPixel : unmanaged, IPixel<TPixel>
283-
=> LoadAsync<TPixel>(Configuration.Default, path, decoder, default);
285+
=> LoadAsync<TPixel>(Configuration.Default, path, decoder, cancellationToken);
284286

285287
/// <summary>
286288
/// Create a new instance of the <see cref="Image"/> class from the given file.
@@ -342,16 +344,17 @@ public static Task<Image<TPixel>> LoadAsync<TPixel>(
342344
/// Create a new instance of the <see cref="Image"/> class from the given file.
343345
/// </summary>
344346
/// <param name="path">The file path to the image.</param>
347+
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
345348
/// <exception cref="ArgumentNullException">The configuration is null.</exception>
346349
/// <exception cref="ArgumentNullException">The path is null.</exception>
347350
/// <exception cref="UnknownImageFormatException">Image format not recognised.</exception>
348351
/// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
349352
/// <exception cref="NotSupportedException">Image format is not supported.</exception>
350353
/// <typeparam name="TPixel">The pixel format.</typeparam>
351354
/// <returns>A <see cref="Task{Image}"/> representing the asynchronous operation.</returns>
352-
public static Task<Image<TPixel>> LoadAsync<TPixel>(string path)
355+
public static Task<Image<TPixel>> LoadAsync<TPixel>(string path, CancellationToken cancellationToken = default)
353356
where TPixel : unmanaged, IPixel<TPixel>
354-
=> LoadAsync<TPixel>(Configuration.Default, path, default(CancellationToken));
357+
=> LoadAsync<TPixel>(Configuration.Default, path, cancellationToken);
355358

356359
/// <summary>
357360
/// Create a new instance of the <see cref="Image"/> class from the given file.

src/ImageSharp/Image.FromStream.cs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,29 @@ public static IImageFormat DetectFormat(Configuration configuration, Stream stre
4444
/// By reading the header on the provided stream this calculates the images format type.
4545
/// </summary>
4646
/// <param name="stream">The image stream to read the header from.</param>
47+
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
4748
/// <exception cref="ArgumentNullException">The stream is null.</exception>
4849
/// <exception cref="NotSupportedException">The stream is not readable.</exception>
4950
/// <returns>A <see cref="Task{IImageFormat}"/> representing the asynchronous operation or null if none is found.</returns>
50-
public static Task<IImageFormat> DetectFormatAsync(Stream stream)
51-
=> DetectFormatAsync(Configuration.Default, stream);
51+
public static Task<IImageFormat> DetectFormatAsync(Stream stream, CancellationToken cancellationToken = default)
52+
=> DetectFormatAsync(Configuration.Default, stream, cancellationToken);
5253

5354
/// <summary>
5455
/// By reading the header on the provided stream this calculates the images format type.
5556
/// </summary>
5657
/// <param name="configuration">The configuration.</param>
5758
/// <param name="stream">The image stream to read the header from.</param>
59+
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
5860
/// <exception cref="ArgumentNullException">The configuration is null.</exception>
5961
/// <exception cref="ArgumentNullException">The stream is null.</exception>
6062
/// <exception cref="NotSupportedException">The stream is not readable.</exception>
6163
/// <returns>A <see cref="Task{IImageFormat}"/> representing the asynchronous operation.</returns>
62-
public static Task<IImageFormat> DetectFormatAsync(Configuration configuration, Stream stream)
64+
public static Task<IImageFormat> DetectFormatAsync(Configuration configuration, Stream stream, CancellationToken cancellationToken = default)
6365
=> WithSeekableStreamAsync(
6466
configuration,
6567
stream,
6668
(s, _) => InternalDetectFormatAsync(s, configuration),
67-
default);
69+
cancellationToken);
6870

6971
/// <summary>
7072
/// Reads the raw image information from the specified stream without fully decoding it.
@@ -83,15 +85,16 @@ public static IImageInfo Identify(Stream stream)
8385
/// Reads the raw image information from the specified stream without fully decoding it.
8486
/// </summary>
8587
/// <param name="stream">The image stream to read the header from.</param>
88+
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
8689
/// <exception cref="ArgumentNullException">The stream is null.</exception>
8790
/// <exception cref="NotSupportedException">The stream is not readable.</exception>
8891
/// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
8992
/// <returns>
9093
/// A <see cref="Task{IImageInfo}"/> representing the asynchronous operation or null if
9194
/// a suitable detector is not found.
9295
/// </returns>
93-
public static Task<IImageInfo> IdentifyAsync(Stream stream)
94-
=> IdentifyAsync(Configuration.Default, stream);
96+
public static Task<IImageInfo> IdentifyAsync(Stream stream, CancellationToken cancellationToken = default)
97+
=> IdentifyAsync(Configuration.Default, stream, cancellationToken);
9598

9699
/// <summary>
97100
/// Reads the raw image information from the specified stream without fully decoding it.
@@ -227,13 +230,14 @@ public static Image Load(Stream stream, out IImageFormat format)
227230
/// The pixel format is selected by the decoder.
228231
/// </summary>
229232
/// <param name="stream">The stream containing image information.</param>
233+
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
230234
/// <exception cref="ArgumentNullException">The stream is null.</exception>
231235
/// <exception cref="NotSupportedException">The stream is not readable or the image format is not supported.</exception>
232236
/// <exception cref="UnknownImageFormatException">Image format not recognised.</exception>
233237
/// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
234238
/// <returns>A <see cref="Task{ValueTuple}"/> representing the asynchronous operation.</returns>
235-
public static Task<(Image Image, IImageFormat Format)> LoadWithFormatAsync(Stream stream)
236-
=> LoadWithFormatAsync(Configuration.Default, stream);
239+
public static Task<(Image Image, IImageFormat Format)> LoadWithFormatAsync(Stream stream, CancellationToken cancellationToken = default)
240+
=> LoadWithFormatAsync(Configuration.Default, stream, cancellationToken);
237241

238242
/// <summary>
239243
/// Decode a new instance of the <see cref="Image"/> class from the given stream.
@@ -252,12 +256,14 @@ public static Image Load(Stream stream, out IImageFormat format)
252256
/// The pixel format is selected by the decoder.
253257
/// </summary>
254258
/// <param name="stream">The stream containing image information.</param>
259+
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
255260
/// <exception cref="ArgumentNullException">The stream is null.</exception>
256261
/// <exception cref="NotSupportedException">The stream is not readable or the image format is not supported.</exception>
257262
/// <exception cref="UnknownImageFormatException">Image format not recognised.</exception>
258263
/// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
259264
/// <returns>A <see cref="Task{Image}"/> representing the asynchronous operation.</returns>
260-
public static Task<Image> LoadAsync(Stream stream) => LoadAsync(Configuration.Default, stream);
265+
public static Task<Image> LoadAsync(Stream stream, CancellationToken cancellationToken = default)
266+
=> LoadAsync(Configuration.Default, stream, cancellationToken);
261267

262268
/// <summary>
263269
/// Decode a new instance of the <see cref="Image"/> class from the given stream.
@@ -280,14 +286,15 @@ public static Image Load(Stream stream, IImageDecoder decoder)
280286
/// </summary>
281287
/// <param name="stream">The stream containing image information.</param>
282288
/// <param name="decoder">The decoder.</param>
289+
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
283290
/// <exception cref="ArgumentNullException">The stream is null.</exception>
284291
/// <exception cref="ArgumentNullException">The decoder is null.</exception>
285292
/// <exception cref="NotSupportedException">The stream is not readable or the image format is not supported.</exception>
286293
/// <exception cref="UnknownImageFormatException">Image format not recognised.</exception>
287294
/// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
288295
/// <returns>A <see cref="Task{Image}"/> representing the asynchronous operation.</returns>
289-
public static Task<Image> LoadAsync(Stream stream, IImageDecoder decoder)
290-
=> LoadAsync(Configuration.Default, stream, decoder);
296+
public static Task<Image> LoadAsync(Stream stream, IImageDecoder decoder, CancellationToken cancellationToken = default)
297+
=> LoadAsync(Configuration.Default, stream, decoder, cancellationToken);
291298

292299
/// <summary>
293300
/// Decode a new instance of the <see cref="Image"/> class from the given stream.
@@ -388,15 +395,16 @@ public static Image<TPixel> Load<TPixel>(Stream stream)
388395
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given stream.
389396
/// </summary>
390397
/// <param name="stream">The stream containing image information.</param>
398+
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
391399
/// <exception cref="ArgumentNullException">The stream is null.</exception>
392400
/// <exception cref="NotSupportedException">The stream is not readable or the image format is not supported.</exception>
393401
/// <exception cref="UnknownImageFormatException">Image format not recognised.</exception>
394402
/// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
395403
/// <typeparam name="TPixel">The pixel format.</typeparam>
396404
/// <returns>A <see cref="Task{Image}"/> representing the asynchronous operation.</returns>
397-
public static Task<Image<TPixel>> LoadAsync<TPixel>(Stream stream)
405+
public static Task<Image<TPixel>> LoadAsync<TPixel>(Stream stream, CancellationToken cancellationToken = default)
398406
where TPixel : unmanaged, IPixel<TPixel>
399-
=> LoadAsync<TPixel>(Configuration.Default, stream);
407+
=> LoadAsync<TPixel>(Configuration.Default, stream, cancellationToken);
400408

401409
/// <summary>
402410
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given stream.
@@ -417,15 +425,16 @@ public static Image<TPixel> Load<TPixel>(Stream stream, out IImageFormat format)
417425
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given stream.
418426
/// </summary>
419427
/// <param name="stream">The stream containing image information.</param>
428+
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
420429
/// <exception cref="ArgumentNullException">The stream is null.</exception>
421430
/// <exception cref="NotSupportedException">The stream is not readable or the image format is not supported.</exception>
422431
/// <exception cref="UnknownImageFormatException">Image format not recognised.</exception>
423432
/// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
424433
/// <typeparam name="TPixel">The pixel format.</typeparam>
425434
/// <returns>A <see cref="Task{ValueTuple}"/> representing the asynchronous operation.</returns>
426-
public static async Task<(Image<TPixel> Image, IImageFormat Format)> LoadWithFormatAsync<TPixel>(Stream stream)
435+
public static async Task<(Image<TPixel> Image, IImageFormat Format)> LoadWithFormatAsync<TPixel>(Stream stream, CancellationToken cancellationToken = default)
427436
where TPixel : unmanaged, IPixel<TPixel>
428-
=> await LoadWithFormatAsync<TPixel>(Configuration.Default, stream).ConfigureAwait(false);
437+
=> await LoadWithFormatAsync<TPixel>(Configuration.Default, stream, cancellationToken).ConfigureAwait(false);
429438

430439
/// <summary>
431440
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given stream.

0 commit comments

Comments
 (0)