Skip to content

Commit 5ffdc47

Browse files
Merge pull request #1146 from SixLabors/js/identify-overloads
Add overloads. Fix #1144
2 parents 3042f83 + 0873fe5 commit 5ffdc47

File tree

5 files changed

+295
-107
lines changed

5 files changed

+295
-107
lines changed

src/ImageSharp/Image.FromBytes.cs

Lines changed: 83 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,55 @@ public static IImageFormat DetectFormat(byte[] data)
2626
/// <summary>
2727
/// By reading the header on the provided byte array this calculates the images format.
2828
/// </summary>
29-
/// <param name="config">The configuration.</param>
29+
/// <param name="configuration">The configuration.</param>
3030
/// <param name="data">The byte array containing encoded image data to read the header from.</param>
3131
/// <returns>The mime type or null if none found.</returns>
32-
public static IImageFormat DetectFormat(Configuration config, byte[] data)
32+
public static IImageFormat DetectFormat(Configuration configuration, byte[] data)
3333
{
34-
using (var stream = new MemoryStream(data))
34+
Guard.NotNull(configuration, nameof(configuration));
35+
using (var stream = new MemoryStream(data, 0, data.Length, false, true))
36+
{
37+
return DetectFormat(configuration, stream);
38+
}
39+
}
40+
41+
/// <summary>
42+
/// Reads the raw image information from the specified stream without fully decoding it.
43+
/// </summary>
44+
/// <param name="data">The byte array containing encoded image data to read the header from.</param>
45+
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
46+
/// <returns>
47+
/// The <see cref="IImageInfo"/> or null if suitable info detector not found.
48+
/// </returns>
49+
public static IImageInfo Identify(byte[] data) => Identify(data, out IImageFormat _);
50+
51+
/// <summary>
52+
/// Reads the raw image information from the specified stream without fully decoding it.
53+
/// </summary>
54+
/// <param name="data">The byte array containing encoded image data to read the header from.</param>
55+
/// <param name="format">The format type of the decoded image.</param>
56+
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
57+
/// <returns>
58+
/// The <see cref="IImageInfo"/> or null if suitable info detector not found.
59+
/// </returns>
60+
public static IImageInfo Identify(byte[] data, out IImageFormat format) => Identify(Configuration.Default, data, out format);
61+
62+
/// <summary>
63+
/// Reads the raw image information from the specified stream without fully decoding it.
64+
/// </summary>
65+
/// <param name="configuration">The configuration.</param>
66+
/// <param name="data">The byte array containing encoded image data to read the header from.</param>
67+
/// <param name="format">The format type of the decoded image.</param>
68+
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
69+
/// <returns>
70+
/// The <see cref="IImageInfo"/> or null if suitable info detector is not found.
71+
/// </returns>
72+
public static IImageInfo Identify(Configuration configuration, byte[] data, out IImageFormat format)
73+
{
74+
Guard.NotNull(configuration, nameof(configuration));
75+
using (var stream = new MemoryStream(data, 0, data.Length, false, true))
3576
{
36-
return DetectFormat(config, stream);
77+
return Identify(configuration, stream, out format);
3778
}
3879
}
3980

@@ -68,33 +109,33 @@ public static Image<TPixel> Load<TPixel>(byte[] data, out IImageFormat format)
68109
/// <summary>
69110
/// Load a new instance of <see cref="Image{TPixel}"/> from the given encoded byte array.
70111
/// </summary>
71-
/// <param name="config">The configuration options.</param>
112+
/// <param name="configuration">The configuration options.</param>
72113
/// <param name="data">The byte array containing encoded image data.</param>
73114
/// <typeparam name="TPixel">The pixel format.</typeparam>
74115
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>
75-
public static Image<TPixel> Load<TPixel>(Configuration config, byte[] data)
116+
public static Image<TPixel> Load<TPixel>(Configuration configuration, byte[] data)
76117
where TPixel : unmanaged, IPixel<TPixel>
77118
{
78119
using (var stream = new MemoryStream(data, 0, data.Length, false, true))
79120
{
80-
return Load<TPixel>(config, stream);
121+
return Load<TPixel>(configuration, stream);
81122
}
82123
}
83124

84125
/// <summary>
85126
/// Load a new instance of <see cref="Image{TPixel}"/> from the given encoded byte array.
86127
/// </summary>
87-
/// <param name="config">The configuration options.</param>
128+
/// <param name="configuration">The configuration options.</param>
88129
/// <param name="data">The byte array containing encoded image data.</param>
89130
/// <param name="format">The <see cref="IImageFormat"/> of the decoded image.</param>
90131
/// <typeparam name="TPixel">The pixel format.</typeparam>
91132
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>
92-
public static Image<TPixel> Load<TPixel>(Configuration config, byte[] data, out IImageFormat format)
133+
public static Image<TPixel> Load<TPixel>(Configuration configuration, byte[] data, out IImageFormat format)
93134
where TPixel : unmanaged, IPixel<TPixel>
94135
{
95136
using (var stream = new MemoryStream(data, 0, data.Length, false, true))
96137
{
97-
return Load<TPixel>(config, stream, out format);
138+
return Load<TPixel>(configuration, stream, out format);
98139
}
99140
}
100141

@@ -117,17 +158,17 @@ public static Image<TPixel> Load<TPixel>(byte[] data, IImageDecoder decoder)
117158
/// <summary>
118159
/// Load a new instance of <see cref="Image{TPixel}"/> from the given encoded byte array.
119160
/// </summary>
120-
/// <param name="config">The Configuration.</param>
161+
/// <param name="configuration">The Configuration.</param>
121162
/// <param name="data">The byte array containing encoded image data.</param>
122163
/// <param name="decoder">The decoder.</param>
123164
/// <typeparam name="TPixel">The pixel format.</typeparam>
124165
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>
125-
public static Image<TPixel> Load<TPixel>(Configuration config, byte[] data, IImageDecoder decoder)
166+
public static Image<TPixel> Load<TPixel>(Configuration configuration, byte[] data, IImageDecoder decoder)
126167
where TPixel : unmanaged, IPixel<TPixel>
127168
{
128169
using (var stream = new MemoryStream(data, 0, data.Length, false, true))
129170
{
130-
return Load<TPixel>(config, stream, decoder);
171+
return Load<TPixel>(configuration, stream, decoder);
131172
}
132173
}
133174

@@ -144,18 +185,18 @@ public static IImageFormat DetectFormat(ReadOnlySpan<byte> data)
144185
/// <summary>
145186
/// By reading the header on the provided byte array this calculates the images format.
146187
/// </summary>
147-
/// <param name="config">The configuration.</param>
188+
/// <param name="configuration">The configuration.</param>
148189
/// <param name="data">The byte array containing encoded image data to read the header from.</param>
149190
/// <returns>The mime type or null if none found.</returns>
150-
public static IImageFormat DetectFormat(Configuration config, ReadOnlySpan<byte> data)
191+
public static IImageFormat DetectFormat(Configuration configuration, ReadOnlySpan<byte> data)
151192
{
152-
int maxHeaderSize = config.MaxHeaderSize;
193+
int maxHeaderSize = configuration.MaxHeaderSize;
153194
if (maxHeaderSize <= 0)
154195
{
155196
return null;
156197
}
157198

158-
foreach (IImageFormatDetector detector in config.ImageFormatsManager.FormatDetectors)
199+
foreach (IImageFormatDetector detector in configuration.ImageFormatsManager.FormatDetectors)
159200
{
160201
IImageFormat f = detector.DetectFormat(data);
161202

@@ -203,32 +244,32 @@ public static Image<TPixel> Load<TPixel>(ReadOnlySpan<byte> data, IImageDecoder
203244
/// <summary>
204245
/// Load a new instance of <see cref="Image{TPixel}"/> from the given encoded byte span.
205246
/// </summary>
206-
/// <param name="config">The configuration options.</param>
247+
/// <param name="configuration">The configuration options.</param>
207248
/// <param name="data">The byte span containing encoded image data.</param>
208249
/// <typeparam name="TPixel">The pixel format.</typeparam>
209250
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>
210-
public static unsafe Image<TPixel> Load<TPixel>(Configuration config, ReadOnlySpan<byte> data)
251+
public static unsafe Image<TPixel> Load<TPixel>(Configuration configuration, ReadOnlySpan<byte> data)
211252
where TPixel : unmanaged, IPixel<TPixel>
212253
{
213254
fixed (byte* ptr = &data.GetPinnableReference())
214255
{
215256
using (var stream = new UnmanagedMemoryStream(ptr, data.Length))
216257
{
217-
return Load<TPixel>(config, stream);
258+
return Load<TPixel>(configuration, stream);
218259
}
219260
}
220261
}
221262

222263
/// <summary>
223264
/// Load a new instance of <see cref="Image{TPixel}"/> from the given encoded byte span.
224265
/// </summary>
225-
/// <param name="config">The Configuration.</param>
266+
/// <param name="configuration">The Configuration.</param>
226267
/// <param name="data">The byte span containing image data.</param>
227268
/// <param name="decoder">The decoder.</param>
228269
/// <typeparam name="TPixel">The pixel format.</typeparam>
229270
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>
230271
public static unsafe Image<TPixel> Load<TPixel>(
231-
Configuration config,
272+
Configuration configuration,
232273
ReadOnlySpan<byte> data,
233274
IImageDecoder decoder)
234275
where TPixel : unmanaged, IPixel<TPixel>
@@ -237,21 +278,21 @@ public static unsafe Image<TPixel> Load<TPixel>(
237278
{
238279
using (var stream = new UnmanagedMemoryStream(ptr, data.Length))
239280
{
240-
return Load<TPixel>(config, stream, decoder);
281+
return Load<TPixel>(configuration, stream, decoder);
241282
}
242283
}
243284
}
244285

245286
/// <summary>
246287
/// Load a new instance of <see cref="Image{TPixel}"/> from the given encoded byte span.
247288
/// </summary>
248-
/// <param name="config">The configuration options.</param>
289+
/// <param name="configuration">The configuration options.</param>
249290
/// <param name="data">The byte span containing image data.</param>
250291
/// <param name="format">The <see cref="IImageFormat"/> of the decoded image.</param>
251292
/// <typeparam name="TPixel">The pixel format.</typeparam>
252293
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>
253294
public static unsafe Image<TPixel> Load<TPixel>(
254-
Configuration config,
295+
Configuration configuration,
255296
ReadOnlySpan<byte> data,
256297
out IImageFormat format)
257298
where TPixel : unmanaged, IPixel<TPixel>
@@ -260,7 +301,7 @@ public static unsafe Image<TPixel> Load<TPixel>(
260301
{
261302
using (var stream = new UnmanagedMemoryStream(ptr, data.Length))
262303
{
263-
return Load<TPixel>(config, stream, out format);
304+
return Load<TPixel>(configuration, stream, out format);
264305
}
265306
}
266307
}
@@ -285,38 +326,38 @@ public static Image Load(byte[] data, out IImageFormat format) =>
285326
/// <summary>
286327
/// Load a new instance of <see cref="Image"/> from the given encoded byte array.
287328
/// </summary>
288-
/// <param name="config">The config for the decoder.</param>
329+
/// <param name="configuration">The configuration for the decoder.</param>
289330
/// <param name="data">The byte array containing encoded image data.</param>
290331
/// <returns>The <see cref="Image"/>.</returns>
291-
public static Image Load(Configuration config, byte[] data) => Load(config, data, out _);
332+
public static Image Load(Configuration configuration, byte[] data) => Load(configuration, data, out _);
292333

293334
/// <summary>
294335
/// Load a new instance of <see cref="Image"/> from the given encoded byte array.
295336
/// </summary>
296-
/// <param name="config">The config for the decoder.</param>
337+
/// <param name="configuration">The configuration for the decoder.</param>
297338
/// <param name="data">The byte array containing image data.</param>
298339
/// <param name="decoder">The decoder.</param>
299340
/// <returns>The <see cref="Image"/>.</returns>
300-
public static Image Load(Configuration config, byte[] data, IImageDecoder decoder)
341+
public static Image Load(Configuration configuration, byte[] data, IImageDecoder decoder)
301342
{
302343
using (var stream = new MemoryStream(data, 0, data.Length, false, true))
303344
{
304-
return Load(config, stream, decoder);
345+
return Load(configuration, stream, decoder);
305346
}
306347
}
307348

308349
/// <summary>
309350
/// Load a new instance of <see cref="Image"/> from the given encoded byte array.
310351
/// </summary>
311-
/// <param name="config">The config for the decoder.</param>
352+
/// <param name="configuration">The configuration for the decoder.</param>
312353
/// <param name="data">The byte array containing image data.</param>
313354
/// <param name="format">The mime type of the decoded image.</param>
314355
/// <returns>The <see cref="Image"/>.</returns>
315-
public static Image Load(Configuration config, byte[] data, out IImageFormat format)
356+
public static Image Load(Configuration configuration, byte[] data, out IImageFormat format)
316357
{
317358
using (var stream = new MemoryStream(data, 0, data.Length, false, true))
318359
{
319-
return Load(config, stream, out format);
360+
return Load(configuration, stream, out format);
320361
}
321362
}
322363

@@ -348,49 +389,49 @@ public static Image Load(ReadOnlySpan<byte> data, out IImageFormat format) =>
348389
/// <summary>
349390
/// Decodes a new instance of <see cref="Image"/> from the given encoded byte span.
350391
/// </summary>
351-
/// <param name="config">The configuration options.</param>
392+
/// <param name="configuration">The configuration options.</param>
352393
/// <param name="data">The byte span containing image data.</param>
353394
/// <returns>The <see cref="Image"/>.</returns>
354-
public static Image Load(Configuration config, ReadOnlySpan<byte> data) => Load(config, data, out _);
395+
public static Image Load(Configuration configuration, ReadOnlySpan<byte> data) => Load(configuration, data, out _);
355396

356397
/// <summary>
357398
/// Load a new instance of <see cref="Image"/> from the given encoded byte span.
358399
/// </summary>
359-
/// <param name="config">The Configuration.</param>
400+
/// <param name="configuration">The Configuration.</param>
360401
/// <param name="data">The byte span containing image data.</param>
361402
/// <param name="decoder">The decoder.</param>
362403
/// <returns>The <see cref="Image"/>.</returns>
363404
public static unsafe Image Load(
364-
Configuration config,
405+
Configuration configuration,
365406
ReadOnlySpan<byte> data,
366407
IImageDecoder decoder)
367408
{
368409
fixed (byte* ptr = &data.GetPinnableReference())
369410
{
370411
using (var stream = new UnmanagedMemoryStream(ptr, data.Length))
371412
{
372-
return Load(config, stream, decoder);
413+
return Load(configuration, stream, decoder);
373414
}
374415
}
375416
}
376417

377418
/// <summary>
378419
/// Load a new instance of <see cref="Image"/> from the given encoded byte span.
379420
/// </summary>
380-
/// <param name="config">The configuration options.</param>
421+
/// <param name="configuration">The configuration options.</param>
381422
/// <param name="data">The byte span containing image data.</param>
382423
/// <param name="format">The <see cref="IImageFormat"/> of the decoded image.</param>>
383424
/// <returns>The <see cref="Image"/>.</returns>
384425
public static unsafe Image Load(
385-
Configuration config,
426+
Configuration configuration,
386427
ReadOnlySpan<byte> data,
387428
out IImageFormat format)
388429
{
389430
fixed (byte* ptr = &data.GetPinnableReference())
390431
{
391432
using (var stream = new UnmanagedMemoryStream(ptr, data.Length))
392433
{
393-
return Load(config, stream, out format);
434+
return Load(configuration, stream, out format);
394435
}
395436
}
396437
}

0 commit comments

Comments
 (0)