SOURCE DUMP v1

This commit is contained in:
Rian Drake 2026-10-15 14:09:11 +13:00
commit fb20299e9d
6 changed files with 768360 additions and 0 deletions

View file

@ -0,0 +1,14 @@
namespace Brutal.StbApi;
public static partial class Stb
{
/// <summary>
/// Describes the types of pixel channel data supported by Stb
/// </summary>
public enum DataType
{
_8Bit,
_16Bit,
Float,
}
}

View file

@ -0,0 +1,65 @@
Brutal.Stb redistributes the third-party components listed below. Each is
reproduced with the license that governs it.
============================================================
stb -- MIT OR Unlicense -- https://github.com/nothings/stb
Compiled: stb_image v2.30, stb_image_write v1.16, at commit f58f558
Shipped as: stb.dll / libstb.so / libstb.dylib
============================================================
Upstream offers both licenses and both are reproduced below.
Only the headers named above are compiled in. The rest of upstream's collection,
stb_truetype among them, does not reach the libraries shipped here.
Two parts of those headers carry upstream attributions to other authors, and both
are present in the binaries. Upstream states them as follows.
The GIF loader in stb_image is described upstream as "public domain by
Jean-Marc Lienher -- simplified/shrunk by stb".
The JPEG writer in stb_image_write is described upstream as based on
jo_jpeg.cpp by Jon Olick, "public domain Simple, Minimalistic JPEG writer".
Upstream's contributor list credits Jon Olick with the original jo_jpeg.cpp
code.
Both declarations are reported as upstream states them. The license below is the
one upstream applies to the headers in which that code sits.
This software is available under 2 licenses -- choose whichever you prefer.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2017 Sean Barrett
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View file

@ -0,0 +1,501 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using Brutal.Strings;
// ReSharper disable MemberCanBePrivate.Global
namespace Brutal.StbApi;
public static partial class Stb
{
/// <summary>
/// Load image from file using 8-bits-per-channel interface.
/// </summary>
/// <param name="fileName">Name of the file to load</param>
/// <param name="desiredChannels">If non-zero, the number of image components requested in the result</param>
/// <returns>Byte* which points to the pixel data, or nullptr on an allocation failure or if the image is corrupt or invalid.</returns>
/// <remarks>
/// The pixel data consists of *y scanlines of *x pixels,
/// with each pixel consisting of N interleaved 8-bit components; the first
/// pixel pointed to is top-left-most in the image. There is no padding between
/// image scanlines or between pixels, regardless of format. The number of
/// components N is 'desired_channels' if desired_channels is non-zero, or
/// *channels_in_file otherwise. If desired_channels is non-zero,
/// *channels_in_file has the number of components that _would_ have been
/// output otherwise. E.g. if you set desired_channels to 4, you will always
/// get RGBA output, but you can check *channels_in_file to see if it's trivially
/// opaque because e.g. there were only 3 channels in the source image.
/// </remarks>
public static unsafe StbImage8 Load(string fileName, int desiredChannels = 0)
{
ValidateDesiredChannels(desiredChannels);
var ptr = PInvoke.stbi_load(fileName, out var width, out var height, out var channelsInFile, desiredChannels);
return new StbImage8(ptr, width, height, desiredChannels != 0 ? desiredChannels : channelsInFile);
}
/// <summary>
/// Load image from memory using 8-bits-per-channel interface.
/// </summary>
/// <seealso cref="Load">Load</seealso>
public static unsafe StbImage8 LoadFromMemory(ReadOnlySpan<byte> buffer, int desiredChannels = 0)
{
ValidateDesiredChannels(desiredChannels);
var ptr = PInvoke.stbi_load_from_memory(
buffer,
buffer.Length,
out var width,
out var height,
out var channelsInFile,
desiredChannels
);
return new StbImage8(ptr, width, height, desiredChannels != 0 ? desiredChannels : channelsInFile);
}
/// <summary>
/// Load image from file using 16-bits-per-channel interface.
/// </summary>
/// <seealso cref="Load">Load</seealso>
public static unsafe StbImage16 Load16(string fileName, int desiredChannels = 0)
{
ValidateDesiredChannels(desiredChannels);
var ptr = PInvoke.stbi_load_16(
fileName,
out var width,
out var height,
out var channelsInFile,
desiredChannels
);
return new StbImage16(ptr, width, height, desiredChannels != 0 ? desiredChannels : channelsInFile);
}
/// <summary>
/// Load image from memory using 16-bits-per-channel interface.
/// </summary>
/// <seealso cref="Load">Load</seealso>
public static unsafe StbImage16 Load16FromMemory(ReadOnlySpan<byte> buffer, int desiredChannels = 0)
{
ValidateDesiredChannels(desiredChannels);
var ptr = PInvoke.stbi_load_16_from_memory(
buffer,
buffer.Length,
out var width,
out var height,
out var channelsInFile,
desiredChannels
);
return new StbImage16(ptr, width, height, desiredChannels != 0 ? desiredChannels : channelsInFile);
}
/// <summary>
/// Load image from file using float-per-channel interface.
/// </summary>
/// <seealso cref="Load">Load</seealso>
public static unsafe StbImageF LoadF(string fileName, int desiredChannels = 0)
{
ValidateDesiredChannels(desiredChannels);
var ptr = PInvoke.stbi_loadf(fileName, out var width, out var height, out var channelsInFile, desiredChannels);
return new StbImageF(ptr, width, height, desiredChannels != 0 ? desiredChannels : channelsInFile);
}
/// <summary>
/// Load image from memory using float-per-channel interface.
/// </summary>
/// <seealso cref="Load">Load</seealso>
public static unsafe StbImageF LoadFFromMemory(ReadOnlySpan<byte> buffer, int desiredChannels = 0)
{
ValidateDesiredChannels(desiredChannels);
var ptr = PInvoke.stbi_loadf_from_memory(
buffer,
buffer.Length,
out var width,
out var height,
out var channelsInFile,
desiredChannels
);
return new StbImageF(ptr, width, height, desiredChannels != 0 ? desiredChannels : channelsInFile);
}
/// <summary>
/// Creates a png image and writes to file
/// </summary>
/// <param name="fileName">The name of the file to create</param>
/// <param name="width">The image width</param>
/// <param name="height">The image height</param>
/// <param name="channels">The number of channels per pixel</param>
/// <param name="data">The image data, a pointer to the first byte of the top-left-most pixel</param>
/// <param name="rowStrideInBytes">The distance in bytes from the first byte of a row of pixels to the first byte of the next row of pixels</param>
/// <returns>True on success</returns>
/// <remarks>
/// The image is a rectangle of pixels stored from left-to-right, top-to-bottom.
/// Each pixel contains 'comp' channels of data stored interleaved with 8-bits
/// per channel, in the following order: 1=Y, 2=YA, 3=RGB, 4=RGBA. (Y is
/// monochrome color.) The rectangle is 'w' pixels wide and 'h' pixels tall.
/// <para />
/// PNG creates output files with the same number of components as the input.
/// <para />
/// PNG supports writing rectangles of data even when the bytes storing rows of
/// data are not consecutive in memory (e.g. sub-rectangles of a larger image),
/// by supplying the stride between the beginning of adjacent rows. The other
/// formats do not. (Thus you cannot write a native-format BMP through the BMP
/// writer, both because it is in BGR order and because it may have padding
/// at the end of the line.)
/// <para />
/// PNG allows you to set the deflate compression level by setting the global
/// variable 'stbi_write_png_compression_level' (it defaults to 8).
/// </remarks>
public static bool WritePng(
string fileName,
int width,
int height,
int channels,
ReadOnlySpan<byte> data,
int rowStrideInBytes = 0
)
{
return PInvoke.stbi_write_png(
fileName,
width,
height,
channels,
data,
rowStrideInBytes != 0 ? rowStrideInBytes : width * channels
);
}
public static bool WritePng(string fileName, StbImage8 image)
{
return WritePng(
fileName,
image.Width,
image.Height,
image.Channels,
image.AsSpan(),
image.Width * image.Channels
);
}
/// <summary>
/// Creates a bmp image and writes to file
/// </summary>
/// <param name="fileName">The name of the file to create</param>
/// <param name="width">The image width</param>
/// <param name="height">The image height</param>
/// <param name="channels">The number of channels per pixel</param>
/// <param name="data">The image data, a pointer to the first byte of the top-left-most pixel</param>
/// <returns>True on success</returns>
/// <remarks>
/// The image is a rectangle of pixels stored from left-to-right, top-to-bottom.
/// Each pixel contains 'comp' channels of data stored interleaved with 8-bits
/// per channel, in the following order: 1=Y, 2=YA, 3=RGB, 4=RGBA. (Y is
/// monochrome color.) The rectangle is 'w' pixels wide and 'h' pixels tall.
/// <para />
/// The BMP format expands Y to RGB in the file format and does not output alpha.
/// </remarks>
public static bool WriteBmp(string fileName, int width, int height, int channels, ReadOnlySpan<byte> data)
{
return PInvoke.stbi_write_bmp(fileName, width, height, channels, data);
}
public static bool WriteBmp(string fileName, StbImage8 image)
{
return WriteBmp(fileName, image.Width, image.Height, image.Channels, image.AsSpan());
}
/// <summary>
/// Creates a tga image and writes to file
/// </summary>
/// <param name="fileName">The name of the file to create</param>
/// <param name="width">The image width</param>
/// <param name="height">The image height</param>
/// <param name="channels">The number of channels per pixel</param>
/// <param name="data">The image data, a pointer to the first byte of the top-left-most pixel</param>
/// <returns>True on success</returns>
/// <remarks>
/// The image is a rectangle of pixels stored from left-to-right, top-to-bottom.
/// Each pixel contains 'comp' channels of data stored interleaved with 8-bits
/// per channel, in the following order: 1=Y, 2=YA, 3=RGB, 4=RGBA. (Y is
/// monochrome color.) The rectangle is 'w' pixels wide and 'h' pixels tall.
/// <para />
/// TGA supports RLE or non-RLE compressed data. To use non-RLE-compressed
/// data, set the global variable 'stbi_write_tga_with_rle' to 0.
/// </remarks>
public static bool WriteTga(string fileName, int width, int height, int channels, ReadOnlySpan<byte> data)
{
return PInvoke.stbi_write_tga(fileName, width, height, channels, data);
}
public static bool WriteTga(string fileName, StbImage8 image)
{
return WriteTga(fileName, image.Width, image.Height, image.Channels, image.AsSpan());
}
/// <summary>
/// Creates a hdr image and writes to file
/// </summary>
/// <param name="fileName">The name of the file to create</param>
/// <param name="width">The image width</param>
/// <param name="height">The image height</param>
/// <param name="channels">The number of channels per pixel</param>
/// <param name="data">The image data, a pointer to the first byte of the top-left-most pixel</param>
/// <returns>True on success</returns>
/// <remarks>
/// The image is a rectangle of pixels stored from left-to-right, top-to-bottom.
/// Each pixel contains 'comp' channels of data stored interleaved with one float
/// per channel, in the following order: 1=Y, 2=YA, 3=RGB, 4=RGBA. (Y is
/// monochrome color.) The rectangle is 'w' pixels wide and 'h' pixels tall.
/// <para />
/// HDR expects linear float data. Since the format is always 32-bit rgb(e)
/// data, alpha (if provided) is discarded, and for monochrome data it is
/// replicated across all three channels.
/// </remarks>
public static bool WriteHdr(string fileName, int width, int height, int channels, ReadOnlySpan<float> data)
{
return PInvoke.stbi_write_hdr(fileName, width, height, channels, data);
}
public static bool WriteHdr(string fileName, StbImageF image)
{
return WriteHdr(fileName, image.Width, image.Height, image.Channels, image.AsSpan());
}
/// <summary>
/// Creates a jpg image and writes to file
/// </summary>
/// <param name="fileName">The name of the file to create</param>
/// <param name="width">The image width</param>
/// <param name="height">The image height</param>
/// <param name="channels">The number of channels per pixel</param>
/// <param name="data">The image data, a pointer to the first byte of the top-left-most pixel</param>
/// <param name="quality">The compression quality to use, 1 lowest to 100 highest.</param>
/// <returns>True on success</returns>
/// <remarks>
/// The image is a rectangle of pixels stored from left-to-right, top-to-bottom.
/// Each pixel contains 'comp' channels of data stored interleaved with one float
/// per channel, in the following order: 1=Y, 2=YA, 3=RGB, 4=RGBA. (Y is
/// monochrome color.) The rectangle is 'w' pixels wide and 'h' pixels tall.
/// <para />
/// JPEG does ignore alpha channels in input data; quality is between 1 and 100.
/// Higher quality looks better but results in a bigger image.
/// JPEG baseline (no JPEG progressive).
/// </remarks>
public static bool WriteJpg(
string fileName,
int width,
int height,
int channels,
ReadOnlySpan<byte> data,
int quality
)
{
return PInvoke.stbi_write_jpg(fileName, width, height, channels, data, quality);
}
public static bool WriteJpg(string fileName, StbImage8 image, int quality)
{
return WriteJpg(fileName, image.Width, image.Height, image.Channels, image.AsSpan(), quality);
}
/// <summary>
/// Get a brief reason for the most recent failure
/// </summary>
/// <returns>
/// A string describing the failure reason, or an empty string when no image operation has failed
/// </returns>
public static unsafe bool TryGetFailureReason([NotNullWhen(true)] out string? failureReason)
{
var reason = PInvoke.stbi_failure_reason();
failureReason = reason == null ? null : Utf8.GetString(reason);
return !string.IsNullOrEmpty(failureReason);
}
/// <summary>
/// Checks whether the file at this path contains HDR data
/// </summary>
/// <remarks>
/// Specifically checks whether this file has a Radiance RGBE header (which would typically be a .hdr file)
/// </remarks>
/// <param name="fileName">The path of the file to check</param>
/// <returns>True if the file is HDR</returns>
public static bool IsHdr(string fileName)
{
return PInvoke.stbi_is_hdr(fileName);
}
/// <summary>
/// Checks whether the data in this buffer contains HDR image data
/// </summary>
/// <remarks>
/// Specifically checks whether this data has a Radiance RGBE header (which would typically be a .hdr file)
/// </remarks>
/// <param name="buffer">The data to check</param>
/// <returns>True if the data is HDR</returns>
public static bool IsHdrFromMemory(ReadOnlySpan<byte> buffer)
{
return PInvoke.stbi_is_hdr_from_memory(buffer, buffer.Length);
}
/// <summary>
/// Checks whether the file at this path contains 16-bit-per-channel data
/// </summary>
/// <param name="fileName">The path of the file to check</param>
/// <returns>True if the file is 16 bit</returns>
public static bool Is16Bit(string fileName)
{
return PInvoke.stbi_is_16_bit(fileName);
}
/// <summary>
/// Checks whether the data in this buffer contains 16-bit-per-channel image data
/// </summary>
/// <param name="buffer">The data to check</param>
/// <returns>True if the data is 16 bit</returns>
public static bool Is16BitFromMemory(ReadOnlySpan<byte> buffer)
{
return PInvoke.stbi_is_16_bit_from_memory(buffer, buffer.Length);
}
private static void ValidateDesiredChannels(int desiredChannels)
{
// stb interprets 0 as "keep the source channel count" and 1-4 as grey, grey+alpha, RGB, RGBA.
ArgumentOutOfRangeException.ThrowIfNegative(desiredChannels);
ArgumentOutOfRangeException.ThrowIfGreaterThan(desiredChannels, 4);
}
internal static partial class PInvoke
{
[LibraryImport("stb", StringMarshalling = StringMarshalling.Utf8)]
public static unsafe partial byte* stbi_load(
string fileName,
out int width,
out int height,
out int channelsInFile,
int desiredChannels
);
[LibraryImport("stb")]
public static unsafe partial byte* stbi_load_from_memory(
ReadOnlySpan<byte> buffer,
int len,
out int width,
out int height,
out int channelsInFile,
int desiredChannels
);
[LibraryImport("stb", StringMarshalling = StringMarshalling.Utf8)]
public static unsafe partial ushort* stbi_load_16(
string fileName,
out int width,
out int height,
out int channelsInFile,
int desiredChannels
);
[LibraryImport("stb")]
public static unsafe partial ushort* stbi_load_16_from_memory(
ReadOnlySpan<byte> buffer,
int len,
out int width,
out int height,
out int channelsInFile,
int desiredChannels
);
[LibraryImport("stb", StringMarshalling = StringMarshalling.Utf8)]
public static unsafe partial float* stbi_loadf(
string fileName,
out int width,
out int height,
out int channelsInFile,
int desiredChannels
);
[LibraryImport("stb")]
public static unsafe partial float* stbi_loadf_from_memory(
ReadOnlySpan<byte> buffer,
int len,
out int width,
out int height,
out int channelsInFile,
int desiredChannels
);
[LibraryImport("stb")]
public static unsafe partial void stbi_image_free(void* data);
[LibraryImport("stb", StringMarshalling = StringMarshalling.Utf8)]
public static unsafe partial Bool32 stbi_write_png(
string fileName,
int width,
int height,
int comp,
ReadOnlySpan<byte> data,
int strideInBytes
);
[LibraryImport("stb", StringMarshalling = StringMarshalling.Utf8)]
public static unsafe partial Bool32 stbi_write_bmp(
string fileName,
int width,
int height,
int comp,
ReadOnlySpan<byte> data
);
[LibraryImport("stb", StringMarshalling = StringMarshalling.Utf8)]
public static unsafe partial Bool32 stbi_write_tga(
string fileName,
int width,
int height,
int comp,
ReadOnlySpan<byte> data
);
[LibraryImport("stb", StringMarshalling = StringMarshalling.Utf8)]
public static unsafe partial Bool32 stbi_write_hdr(
string fileName,
int width,
int height,
int comp,
ReadOnlySpan<float> data
);
[LibraryImport("stb", StringMarshalling = StringMarshalling.Utf8)]
public static unsafe partial Bool32 stbi_write_jpg(
string fileName,
int width,
int height,
int comp,
ReadOnlySpan<byte> data,
int quality
);
[LibraryImport("stb")]
public static unsafe partial byte* stbi_failure_reason();
[LibraryImport("stb", StringMarshalling = StringMarshalling.Utf8)]
public static unsafe partial Bool32 stbi_is_16_bit(string fileName);
[LibraryImport("stb")]
public static unsafe partial Bool32 stbi_is_16_bit_from_memory(ReadOnlySpan<byte> buffer, int len);
[LibraryImport("stb", StringMarshalling = StringMarshalling.Utf8)]
public static unsafe partial Bool32 stbi_is_hdr(string fileName);
[LibraryImport("stb")]
public static unsafe partial Bool32 stbi_is_hdr_from_memory(ReadOnlySpan<byte> buffer, int len);
}
}

View file

@ -0,0 +1,39 @@
using Brutal.Numerics;
namespace Brutal.StbApi;
public readonly unsafe struct StbImage16(ushort* inData, int inWidth, int inHeight, int inChannels)
: INullable,
IDisposable
{
public readonly ushort* Data = inData;
public readonly int Width = inWidth;
public readonly int Height = inHeight;
public readonly int Channels = inChannels;
public Span<ushort> AsSpan()
{
return new Span<ushort>(Data, Width * Height * Channels);
}
public Span<T> AsSpan<T>()
where T : IVector<T, ushort>
{
if (T.ComponentCount != Channels)
throw new InvalidOperationException(
$"Cannot convert to Span<{typeof(T).Name}> because it has {T.ComponentCount} components, but the image has {Channels} channels."
);
return new Span<T>(Data, Width * Height);
}
public void Dispose()
{
Stb.PInvoke.stbi_image_free(Data);
}
public bool IsNull()
{
return Data == null;
}
}

View file

@ -0,0 +1,39 @@
using Brutal.Numerics;
namespace Brutal.StbApi;
public readonly unsafe struct StbImage8(byte* inData, int inWidth, int inHeight, int inChannels)
: INullable,
IDisposable
{
public readonly byte* Data = inData;
public readonly int Width = inWidth;
public readonly int Height = inHeight;
public readonly int Channels = inChannels;
public Span<byte> AsSpan()
{
return new Span<byte>(Data, Width * Height * Channels);
}
public Span<T> AsSpan<T>()
where T : IVector<T, byte>
{
if (T.ComponentCount != Channels)
throw new InvalidOperationException(
$"Cannot convert to Span<{typeof(T).Name}> because it has {T.ComponentCount} components, but the image has {Channels} channels."
);
return new Span<T>(Data, Width * Height);
}
public void Dispose()
{
Stb.PInvoke.stbi_image_free(Data);
}
public bool IsNull()
{
return Data == null;
}
}

View file

@ -0,0 +1,39 @@
using Brutal.Numerics;
namespace Brutal.StbApi;
public readonly unsafe struct StbImageF(float* inData, int inWidth, int inHeight, int inChannels)
: INullable,
IDisposable
{
public readonly float* Data = inData;
public readonly int Width = inWidth;
public readonly int Height = inHeight;
public readonly int Channels = inChannels;
public Span<float> AsSpan()
{
return new Span<float>(Data, Width * Height * Channels);
}
public Span<T> AsSpan<T>()
where T : IVector<T, float>
{
if (T.ComponentCount != Channels)
throw new InvalidOperationException(
$"Cannot convert to Span<{typeof(T).Name}> because it has {T.ComponentCount} components, but the image has {Channels} channels."
);
return new Span<T>(Data, Width * Height);
}
public void Dispose()
{
Stb.PInvoke.stbi_image_free(Data);
}
public bool IsNull()
{
return Data == null;
}
}