refuel.mixedup.dev/docs/ReFuel.StbImage.md

49 lines
1.9 KiB
Markdown

# ReFuel.StbImage
StbImage is a very common library that has been developed mainly by Sean Barett.
It is one of the easiest to use image loading libraries on the planet
especially for C and C++ users. It is relatively simple to load up and use in
C#, however the C# build system makes it a bit difficult to manage native
dependencies. Therefore this package makes it easy for you and everyone else
who needs a cross platform way to load images.
Intended OS targets are:
* Windows (x86 and x64)
* Linux (arm32, arm64, x86 and x64)
* MacOS (arm64, x64)
## Usage
```cs
using ReFuel.Stb;
// Simply load an image from a Stream or Span<T> (T: unmanaged) source.
using StbImage image = StbImage.Load(source);
Console.WriteLine("{0}x{1}", image.Width, image.Height);
// StbImage.ImagePointer is a pointer to the image data.
// Pass the image to an unsafe library as is (e.g. OpenGL), or cast it to a span. Currently there is no "safe" way to do this.
// OpenGL example
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, image.Width, image.Height, 0, StbiFormatToGLFormat(image.Format, image.IsFloat ? PixelFormat.Float : PixelFormat.UnsignedByte), image.ImagePointer);
// C# example
struct Pixel
{
int R, G, B, A;
}
Span<Pixel> pixels;
unsafe
{
pixels = new Span<Pixel>(image.ImagePointer, image.Width * image.Height);
}
```
### Global Options
| Option | Description |
|--------|-------------|
| `bool StbImage.FlipVerticallyOnLoad` | Flips the image vertically after loading. Practical for uses where a right handed coordinate system is used (e.g. OpenGL) |
| `bool UnpremultiplyOnLoad` | Applies to Apple iPhone PNG images, where the image is sometimes encoded with premultiplied alpha. Reverses this premultiplication. |
## Calling Stbi directly.
If you wish to call the native Stbi library, call functions in the static class `Stbi`. See `stb_image.h` documentation [online](https://github.com/nothings/stb/blob/master/stb_image.h).