-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstbi-sharp.cs
488 lines (448 loc) · 30.5 KB
/
stbi-sharp.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// This file was developed by Thomas Müller <thomas94@gmx.net>.
// It is published under the BSD 3-Clause License within the LICENSE file.
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace StbiSharp
{
unsafe public class StbiImageBase : IDisposable
{
protected void* data = null;
/// <summary>
/// The width of the image in number of pixels.
/// </summary>
public int Width { get; private set; }
/// <summary>
/// The height of the image in number of pixels.
/// </summary>
public int Height { get; private set; }
/// <summary>
/// The number of colour channels of the image.
/// </summary>
public int NumChannels { get; private set; }
internal StbiImageBase(void* data, int width, int height, int numChannels)
{
this.data = data;
Width = width;
Height = height;
NumChannels = numChannels;
}
#region IDisposable Support
protected virtual void Dispose(bool disposing)
{
if (data != null)
{
Stbi.Free(data);
data = null;
}
}
~StbiImageBase()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
/// <summary>
/// A disposable class that exposes image data and metadata for images loaded via STBI.
/// On disposal, frees any native memory that has been allocated to store the image data.
/// </summary>
unsafe public class StbiImage : StbiImageBase
{
/// <summary>
/// The raw image data. It is stored in in row-major order, pixel by pixel. Each pixel consists
/// of <see cref="NumChannels"/> bytes ordered RGBA.
/// </summary>
public ReadOnlySpan<byte> Data => new ReadOnlySpan<byte>(data, Width * Height * NumChannels);
internal StbiImage(byte* data, int width, int height, int numChannels)
: base(data, width, height, numChannels)
{
}
}
/// <summary>
/// A disposable class that exposes image data and metadata for images loaded via STBI.
/// On disposal, frees any native memory that has been allocated to store the image data.
/// </summary>
unsafe public class StbiImageF : StbiImageBase
{
/// <summary>
/// The raw image data. It is stored in in row-major order, pixel by pixel. Each pixel consists
/// of <see cref="NumChannels"/> floats ordered RGBA.
/// </summary>
public ReadOnlySpan<float> Data => new ReadOnlySpan<float>(data, Width * Height * NumChannels);
internal StbiImageF(float* data, int width, int height, int numChannels)
: base(data, width, height, numChannels)
{
}
}
public class Stbi
{
/// <summary>
/// Loads an encoded image (in PNG, JPG, or another supported format; see the README of
/// https://github.com/nothings/stb/blob/master/stb_image.h for a list of supported formats)
/// residing at <paramref name="data"/> into <paramref name="dst"/>. Requires an additional copy
/// to <paramref name="dst"/> and is thus slower than <see cref="LoadFromMemory"/>.
/// </summary>
/// <param name="data">Pointer to the beginning of the encoded image data.</param>
/// <param name="len">Number of bytes that the encoded image data is long.</param>
/// <param name="desiredNumChannels">The number of desired colour channels in the output.
/// When the encoded image has fewer channels than the desired number of channels,
/// then the desired number of channels will be produced automatically. For example,
/// when the encoded image is RGB, but 4 channels are requested, then a fully opaque
/// alpha channel will be generated. Supplying a value of 0 means that the native number
/// of channels of the encoded image is used.</param>
/// <param name="dst">Pointer to the beginning of the destination buffer into which the
/// image is loaded. The loaded image will be stored in this buffer in row-major format, pixel
/// by pixel. Each pixel consists of N bytes where N is the number of channels, ordered RGBA.</param>
/// <returns>True on success, false on failure.</returns>
[DllImport("stbi")]
unsafe public static extern bool LoadFromMemoryIntoBuffer(byte* data, long len, int desiredNumChannels, byte* dst);
/// <summary>
/// Loads an encoded image (in PNG, JPG, or another supported format; see the README of
/// https://github.com/nothings/stb/blob/master/stb_image.h for a list of supported formats)
/// residing at <paramref name="data"/> into <paramref name="dst"/>. Requires an additional copy
/// to <paramref name="dst"/> and is thus slower than <see cref="LoadFromMemory"/>.
/// </summary>
/// <param name="data">The encoded image data to be loaded.</param>
/// <param name="desiredNumChannels">The number of desired colour channels in the output.
/// When the encoded image has fewer channels than the desired number of channels,
/// then the desired number of channels will be produced automatically. For example,
/// when the encoded image is RGB, but 4 channels are requested, then a fully opaque
/// alpha channel will be generated. Supplying a value of 0 means that the native number
/// of channels of the encoded image is used.</param>
/// <param name="dst">The destination buffer into which the image is loaded. The loaded image
/// will be stored in this buffer in row-major format, pixel by pixel. Each pixel consists of
/// N bytes where N is the number of channels, ordered RGBA.</param>
/// <exception cref="ArgumentException">Thrown when image loading fails.</exception>
unsafe public static void LoadFromMemoryIntoBuffer(ReadOnlySpan<byte> data, int desiredNumChannels, Span<byte> dst)
{
fixed (byte* address = data)
fixed (byte* dstAddress = dst)
if (!LoadFromMemoryIntoBuffer(address, data.Length, desiredNumChannels, dstAddress))
throw new ArgumentException($"STBI could not load an image from the provided {nameof(data)}: {FailureReason()}");
}
/// <summary>
/// Loads an encoded image (in PNG, JPG, or another supported format; see the README of
/// https://github.com/nothings/stb/blob/master/stb_image.h for a list of supported formats)
/// residing at <paramref name="data"/> into <paramref name="dst"/>. Requires an additional copy
/// to <paramref name="dst"/> and is thus slower than <see cref="LoadFromMemory"/>.
/// </summary>
/// <param name="data">The encoded image data to be loaded.</param>
/// <param name="desiredNumChannels">The number of desired colour channels in the output.
/// When the encoded image has fewer channels than the desired number of channels,
/// then the desired number of channels will be produced automatically. For example,
/// when the encoded image is RGB, but 4 channels are requested, then a fully opaque
/// alpha channel will be generated. Supplying a value of 0 means that the native number
/// of channels of the encoded image is used.</param>
/// <param name="dst">The destination buffer into which the image is loaded. The loaded image
/// will be stored in this buffer in row-major format, pixel by pixel. Each pixel consists of
/// N bytes where N is the number of channels, ordered RGBA.</param>
/// <exception cref="ArgumentException">Thrown when image loading fails.</exception>
public static void LoadFromMemoryInfoBuffer(MemoryStream data, int desiredNumChannels, Span<byte> dst) =>
LoadFromMemoryIntoBuffer(data.GetBuffer(), desiredNumChannels, dst);
/// <summary>
/// Loads an encoded image (in HDR, PNG, JPG, or another supported format; see the README of
/// https://github.com/nothings/stb/blob/master/stb_image.h for a list of supported formats)
/// residing at <paramref name="data"/> into <paramref name="dst"/>. Requires an additional copy
/// to <paramref name="dst"/> and is thus slower than <see cref="LoadFFromMemory"/>.
/// The image is returned in floating point format, which preserves the full dynamic range of HDR files.
/// When loading non-HDR images, they will be converted to floating point.
/// sRGB images will be remapped to linear.
/// </summary>
/// <param name="data">Pointer to the beginning of the encoded image data.</param>
/// <param name="len">Number of bytes that the encoded image data is long.</param>
/// <param name="desiredNumChannels">The number of desired colour channels in the output.
/// When the encoded image has fewer channels than the desired number of channels,
/// then the desired number of channels will be produced automatically. For example,
/// when the encoded image is RGB, but 4 channels are requested, then a fully opaque
/// alpha channel will be generated. Supplying a value of 0 means that the native number
/// of channels of the encoded image is used.</param>
/// <param name="dst">Pointer to the beginning of the destination buffer into which the
/// image is loaded. The loaded image will be stored in this buffer in row-major format, pixel
/// by pixel. Each pixel consists of N floats where N is the number of channels, ordered RGBA.</param>
/// <returns>True on success, false on failure.</returns>
[DllImport("stbi")]
unsafe public static extern bool LoadFFromMemoryIntoBuffer(byte* data, long len, int desiredNumChannels, float* dst);
/// <summary>
/// Loads an encoded image (in HDR, PNG, JPG, or another supported format; see the README of
/// https://github.com/nothings/stb/blob/master/stb_image.h for a list of supported formats)
/// residing at <paramref name="data"/> into <paramref name="dst"/>. Requires an additional copy
/// to <paramref name="dst"/> and is thus slower than <see cref="LoadFFromMemory"/>.
/// The image is returned in floating point format, which preserves the full dynamic range of HDR files.
/// When loading non-HDR images, they will be converted to floating point.
/// sRGB images will be remapped to linear.
/// </summary>
/// <param name="data">The encoded image data to be loaded.</param>
/// <param name="desiredNumChannels">The number of desired colour channels in the output.
/// When the encoded image has fewer channels than the desired number of channels,
/// then the desired number of channels will be produced automatically. For example,
/// when the encoded image is RGB, but 4 channels are requested, then a fully opaque
/// alpha channel will be generated. Supplying a value of 0 means that the native number
/// of channels of the encoded image is used.</param>
/// <param name="dst">The destination buffer into which the image is loaded. The loaded image
/// will be stored in this buffer in row-major format, pixel by pixel. Each pixel consists of
/// N floats where N is the number of channels, ordered RGBA.</param>
/// <exception cref="ArgumentException">Thrown when image loading fails.</exception>
unsafe public static void LoadFFromMemoryIntoBuffer(ReadOnlySpan<byte> data, int desiredNumChannels, Span<float> dst)
{
fixed (byte* address = data)
fixed (float* dstAddress = dst)
if (!LoadFFromMemoryIntoBuffer(address, data.Length, desiredNumChannels, dstAddress))
throw new ArgumentException($"STBI could not load an image from the provided {nameof(data)}: {FailureReason()}");
}
/// <summary>
/// Loads an encoded image (in HDR, PNG, JPG, or another supported format; see the README of
/// https://github.com/nothings/stb/blob/master/stb_image.h for a list of supported formats)
/// residing at <paramref name="data"/> into <paramref name="dst"/>. Requires an additional copy
/// to <paramref name="dst"/> and is thus slower than <see cref="LoadFFromMemory"/>.
/// The image is returned in floating point format, which preserves the full dynamic range of HDR files.
/// When loading non-HDR images, they will be converted to floating point.
/// sRGB images will be remapped to linear.
/// </summary>
/// <param name="data">The encoded image data to be loaded.</param>
/// <param name="desiredNumChannels">The number of desired colour channels in the output.
/// When the encoded image has fewer channels than the desired number of channels,
/// then the desired number of channels will be produced automatically. For example,
/// when the encoded image is RGB, but 4 channels are requested, then a fully opaque
/// alpha channel will be generated. Supplying a value of 0 means that the native number
/// of channels of the encoded image is used.</param>
/// <param name="dst">The destination buffer into which the image is loaded. The loaded image
/// will be stored in this buffer in row-major format, pixel by pixel. Each pixel consists of
/// N floats where N is the number of channels, ordered RGBA.</param>
/// <exception cref="ArgumentException">Thrown when image loading fails.</exception>
public static void LoadFFromMemoryInfoBuffer(MemoryStream data, int desiredNumChannels, Span<float> dst) =>
LoadFFromMemoryIntoBuffer(data.GetBuffer(), desiredNumChannels, dst);
/// <summary>
/// Retrieves metadata from an encoded image (in PNG, JPG, or another supported format; see the README of
/// https://github.com/nothings/stb/blob/master/stb_image.h for a list of supported formats)
/// residing at <paramref name="data"/>.
/// </summary>
/// <param name="data">Pointer to the beginning of the encoded image data.</param>
/// <param name="len">Number of bytes that the encoded image data is long.</param>
/// <param name="width">The number of pixels the image is wide.</param>
/// <param name="height">The number of pixels the image is tall.</param>
/// <param name="numChannels">The number of colour channels of the image.</param>
/// <returns>True on success, false on failure.</returns>
[DllImport("stbi")]
unsafe public static extern bool InfoFromMemory(byte* data, long len, out int width, out int height, out int numChannels);
/// <summary>
/// Retrieves metadata from an encoded image (in PNG, JPG, or another supported format; see the README of
/// https://github.com/nothings/stb/blob/master/stb_image.h for a list of supported formats)
/// residing at <paramref name="data"/>.
/// </summary>
/// <param name="data">The encoded image data.</param>
/// <param name="width">The number of pixels the image is wide.</param>
/// <param name="height">The number of pixels the image is tall.</param>
/// <param name="numChannels">The number of colour channels of the image.</param>
/// <exception cref="ArgumentException">Thrown when image metadata loading fails.</exception>
unsafe public static void InfoFromMemory(ReadOnlySpan<byte> data, out int width, out int height, out int numChannels)
{
fixed (byte* address = data)
if (!InfoFromMemory(address, data.Length, out width, out height, out numChannels))
throw new ArgumentException($"STBI could not load image metadata from the provided {nameof(data)}: {FailureReason()}");
}
/// <summary>
/// Retrieves metadata from an encoded image (in PNG, JPG, or another supported format; see the README of
/// https://github.com/nothings/stb/blob/master/stb_image.h for a list of supported formats)
/// residing at <paramref name="data"/>.
/// </summary>
/// <param name="data">The encoded image data.</param>
/// <param name="width">The number of pixels the image is wide.</param>
/// <param name="height">The number of pixels the image is tall.</param>
/// <param name="numChannels">The number of colour channels of the image.</param>
/// <exception cref="ArgumentException">Thrown when image metadata loading fails.</exception>
public static void InfoFromMemory(MemoryStream data, out int width, out int height, out int numChannels)
=> InfoFromMemory(data.GetBuffer(), out width, out height, out numChannels);
/// <summary>
/// Returns whether or not an encoded image is an HDR image (e.g. Radiance .HDR image; see the README of
/// https://github.com/nothings/stb/blob/master/stb_image.h for a list of supported HDR formats)
/// </summary>
/// <param name="data">Pointer to the beginning of the encoded image data.</param>
/// <param name="len">Number of bytes that the encoded image data is long.</param>
[DllImport("stbi")]
unsafe public static extern bool IsHdrFromMemory(byte* data, long len);
/// <summary>
/// Returns whether or not an encoded image is an HDR image (e.g. Radiance .HDR image; see the README of
/// https://github.com/nothings/stb/blob/master/stb_image.h for a list of supported HDR formats)
/// </summary>
/// <param name="data">The encoded image data.</param>
unsafe public static bool IsHdrFromMemory(ReadOnlySpan<byte> data)
{
fixed (byte* address = data)
return IsHdrFromMemory(address, data.Length);
}
/// <summary>
/// Returns whether or not an encoded image is an HDR image (e.g. Radiance .HDR image; see the README of
/// https://github.com/nothings/stb/blob/master/stb_image.h for a list of supported HDR formats)
/// </summary>
/// <param name="data">The encoded image data.</param>
public static bool IsHdrFromMemory(MemoryStream data)
=> IsHdrFromMemory(data.GetBuffer());
/// <summary>
/// Loads an encoded image (in PNG, JPG, or another supported format; see the README of
/// https://github.com/nothings/stb/blob/master/stb_image.h for a list of supported formats)
/// residing at <paramref name="data"/>.
/// </summary>
/// <param name="data">Pointer to the beginning of the encoded image data.</param>
/// <param name="len">Number of bytes that the encoded image data is long.</param>
/// <param name="width">The number of pixels the image is wide.</param>
/// <param name="height">The number of pixels the image is tall.</param>
/// <param name="numChannels">The number of colour channels of the image.</param>
/// <param name="desiredNumChannels">The number of desired colour channels in the output.
/// When the encoded image has fewer channels than the desired number of channels,
/// then the desired number of channels will be produced automatically. For example,
/// when the encoded image is RGB, but 4 channels are requested, then a fully opaque
/// alpha channel will be generated. Supplying a value of 0 means that the native number
/// of channels of the encoded image is used.</param>
/// <returns>Null on failure. On success, returns a pointer to the beginning of the buffer into which the
/// image was loaded. The loaded image will be stored in this buffer in row-major format, pixel
/// by pixel. Each pixel consists of N bytes where N is the number of channels, ordered RGBA.</returns>
[DllImport("stbi")]
unsafe public static extern byte* LoadFromMemory(byte* data, long len, out int width, out int height, out int numChannels, int desiredNumChannels);
/// <summary>
/// Loads an encoded image (in HDR, PNG, JPG, or another supported format; see the README of
/// https://github.com/nothings/stb/blob/master/stb_image.h for a list of supported formats)
/// residing at <paramref name="data"/>.
/// The image is returned in floating point format, which preserves the full dynamic range of HDR files.
/// When loading non-HDR images, they will be converted to floating point.
/// sRGB images will be remapped to linear.
/// </summary>
/// <param name="data">Pointer to the beginning of the encoded image data.</param>
/// <param name="len">Number of bytes that the encoded image data is long.</param>
/// <param name="width">The number of pixels the image is wide.</param>
/// <param name="height">The number of pixels the image is tall.</param>
/// <param name="numChannels">The number of colour channels of the image.</param>
/// <param name="desiredNumChannels">The number of desired colour channels in the output.
/// When the encoded image has fewer channels than the desired number of channels,
/// then the desired number of channels will be produced automatically. For example,
/// when the encoded image is RGB, but 4 channels are requested, then a fully opaque
/// alpha channel will be generated. Supplying a value of 0 means that the native number
/// of channels of the encoded image is used.</param>
/// <returns>Null on failure. On success, returns a pointer to the beginning of the buffer into which the
/// image was loaded. The loaded image will be stored in this buffer in row-major format, pixel
/// by pixel. Each pixel consists of N floats where N is the number of channels, ordered RGBA.</returns>
[DllImport("stbi")]
unsafe public static extern float* LoadFFromMemory(byte* data, long len, out int width, out int height, out int numChannels, int desiredNumChannels);
/// <summary>
/// Flip the image vertically, so the first pixel in the output array is the bottom left.
/// </summary>
/// <param name="shouldFlip">True if should flip vertically on load.</param>
[DllImport("stbi")]
unsafe public static extern void SetFlipVerticallyOnLoad(bool shouldFlip);
/// <summary>
/// Frees memory of an image that has previously been loaded by <see cref="LoadFromMemory"/> or <see cref="LoadFFromMemory"/>.
/// Only has to be called when the byte-pointer overload of <see cref="LoadFromMemory"/>
/// or the float-pointer overload of <see cref="LoadFFromMemory"/> was used.
/// </summary>
/// <param name="data">Pointer to the beginning of the pixel data.</param>
[DllImport("stbi")]
unsafe public static extern void Free(void* data);
/// <summary>
/// After failure to load an image, returns a pointer to a string describing the reason for the failure.
/// </summary>
[DllImport("stbi", EntryPoint = "FailureReason")]
unsafe public static extern IntPtr FailureReasonIntPtr();
/// <summary>
/// After failure to load an image, returns a string describing the reason for the failure.
/// </summary>
public static string FailureReason() => Marshal.PtrToStringAuto(FailureReasonIntPtr());
/// <summary>
/// Loads an encoded image (in PNG, JPG, or another supported format; see the README of
/// https://github.com/nothings/stb/blob/master/stb_image.h for a list of supported formats)
/// residing at <paramref name="data"/>.
/// </summary>
/// <param name="data">The encoded image data to be loaded.</param>
/// <param name="desiredNumChannels">The number of desired colour channels in the output.
/// When the encoded image has fewer channels than the desired number of channels,
/// then the desired number of channels will be produced automatically. For example,
/// when the encoded image is RGB, but 4 channels are requested, then a fully opaque
/// alpha channel will be generated. Supplying a value of 0 means that the native number
/// of channels of the encoded image is used.</param>
/// <returns>Returns a disposable <see cref="StbiImage"/> object that exposes image data
/// and metadata. On disposal, <see cref="StbiImage"/> frees any native memory that has
/// been allocated to store the image data.</returns>
unsafe public static StbiImage LoadFromMemory(ReadOnlySpan<byte> data, int desiredNumChannels)
{
fixed (byte* address = data)
{
byte* pixels = LoadFromMemory(address, data.Length, out int width, out int height, out int numChannels, desiredNumChannels);
if (pixels == null)
{
throw new ArgumentException($"STBI could not load an image from the provided {nameof(data)}: {FailureReason()}");
}
return new StbiImage(pixels, width, height, desiredNumChannels == 0 ? numChannels : desiredNumChannels);
}
}
/// <summary>
/// Loads an encoded image (in PNG, JPG, or another supported format; see the README of
/// https://github.com/nothings/stb/blob/master/stb_image.h for a list of supported formats)
/// residing at <paramref name="data"/>.
/// </summary>
/// <param name="data">The encoded image data to be loaded.</param>
/// <param name="desiredNumChannels">The number of desired colour channels in the output.
/// When the encoded image has fewer channels than the desired number of channels,
/// then the desired number of channels will be produced automatically. For example,
/// when the encoded image is RGB, but 4 channels are requested, then a fully opaque
/// alpha channel will be generated. Supplying a value of 0 means that the native number
/// of channels of the encoded image is used.</param>
/// <returns>Returns a disposable <see cref="StbiImage"/> object that exposes image data
/// and metadata. On disposal, <see cref="StbiImage"/> frees any native memory that has
/// been allocated to store the image data.</returns>
public static StbiImage LoadFromMemory(MemoryStream data, int desiredNumChannels)
=> LoadFromMemory(data.GetBuffer(), desiredNumChannels);
/// <summary>
/// Loads an encoded image (in HDR, PNG, JPG, or another supported format; see the README of
/// https://github.com/nothings/stb/blob/master/stb_image.h for a list of supported formats)
/// residing at <paramref name="data"/>.
/// The image is returned in floating point format, which preserves the full dynamic range of HDR files.
/// When loading non-HDR images, they will be converted to floating point.
/// sRGB images will be remapped to linear.
/// </summary>
/// <param name="data">The encoded image data to be loaded.</param>
/// <param name="desiredNumChannels">The number of desired colour channels in the output.
/// When the encoded image has fewer channels than the desired number of channels,
/// then the desired number of channels will be produced automatically. For example,
/// when the encoded image is RGB, but 4 channels are requested, then a fully opaque
/// alpha channel will be generated. Supplying a value of 0 means that the native number
/// of channels of the encoded image is used.</param>
/// <returns>Returns a disposable <see cref="StbiImageF"/> object that exposes image data
/// and metadata. On disposal, <see cref="StbiImageF"/> frees any native memory that has
/// been allocated to store the image data.</returns>
unsafe public static StbiImageF LoadFFromMemory(ReadOnlySpan<byte> data, int desiredNumChannels)
{
fixed (byte* address = data)
{
float* pixels = LoadFFromMemory(address, data.Length, out int width, out int height, out int numChannels, desiredNumChannels);
if (pixels == null)
{
throw new ArgumentException($"STBI could not load an image from the provided {nameof(data)}: {FailureReason()}");
}
return new StbiImageF(pixels, width, height, desiredNumChannels == 0 ? numChannels : desiredNumChannels);
}
}
/// <summary>
/// Loads an encoded image (in HDR, PNG, JPG, or another supported format; see the README of
/// https://github.com/nothings/stb/blob/master/stb_image.h for a list of supported formats)
/// residing at <paramref name="data"/>.
/// The image is returned in floating point format, which preserves the full dynamic range of HDR files.
/// When loading non-HDR images, they will be converted to floating point.
/// sRGB images will be remapped to linear.
/// </summary>
/// <param name="data">The encoded image data to be loaded.</param>
/// <param name="desiredNumChannels">The number of desired colour channels in the output.
/// When the encoded image has fewer channels than the desired number of channels,
/// then the desired number of channels will be produced automatically. For example,
/// when the encoded image is RGB, but 4 channels are requested, then a fully opaque
/// alpha channel will be generated. Supplying a value of 0 means that the native number
/// of channels of the encoded image is used.</param>
/// <returns>Returns a disposable <see cref="StbiImageF"/> object that exposes image data
/// and metadata. On disposal, <see cref="StbiImageF"/> frees any native memory that has
/// been allocated to store the image data.</returns>
public static StbiImageF LoadFFromMemory(MemoryStream data, int desiredNumChannels)
=> LoadFFromMemory(data.GetBuffer(), desiredNumChannels);
}
}