Table of Contents

Class StbImage

Namespace
ReFuel.Stb
Assembly
ReFuel.StbImage.dll

A class that encompasses all features of stb_image.h in a safe way.

public class StbImage : IDisposable
Inheritance
StbImage
Implements
Inherited Members

Properties

FlipVerticallyOnLoad

Set to flip the y-axis of loaded images on load.

public static bool FlipVerticallyOnLoad { set; }

Property Value

bool

FlipVerticallyOnSave

Set to flip the y-axis of saved images.

public static bool FlipVerticallyOnSave { set; }

Property Value

bool

Format

Internal image format.

public StbiImageFormat Format { get; }

Property Value

StbiImageFormat

Height

Height of the image.

public int Height { get; }

Property Value

int

ImagePointer

Pointer to the image.

public IntPtr ImagePointer { get; }

Property Value

IntPtr

IsFloat

True if the image is a floating point image.

public bool IsFloat { get; }

Property Value

bool

UnpremultiplyOnLoad

Set to unpremultiply images on load.

public static bool UnpremultiplyOnLoad { set; }

Property Value

bool

Remarks

According to the stb_image documentation, only iPhone PNG images can come with premultiplied alpha.

Width

Width of the image.

public int Width { get; }

Property Value

int

WriteForcePngFilter

Force a filter on PNG filter when saving.

public static int WriteForcePngFilter { get; set; }

Property Value

int

Remarks

-1 for auto, 0 through 5 to pick a filter. Higher is more. Not thread safe.

WritePngCompressionLevel

Change the PNG compression level on save.

public static int WritePngCompressionLevel { get; set; }

Property Value

int

Remarks

Higher is more. Defaults to 8. Not thread safe.

WriteTgaEnableRLE

Enable run length encoding on TGA images on save.

public static bool WriteTgaEnableRLE { get; set; }

Property Value

bool

Remarks

Not thread safe.

Methods

AsSpan<T>()

Get a safe span to the image pointer.

public ReadOnlySpan<T> AsSpan<T>() where T : unmanaged

Returns

ReadOnlySpan<T>

A span to the image data.

Type Parameters

T

The pixel type.

Exceptions

Exception

The image uses an unexpected image format.

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

~StbImage()

protected ~StbImage()

Load(Stream, StbiImageFormat, bool)

Load an image.

public static StbImage Load(Stream stream, StbiImageFormat format = StbiImageFormat.Default, bool asFloat = false)

Parameters

stream Stream

The stream to load from.

format StbiImageFormat

The desired image format.

asFloat bool

Returns

StbImage

The image object.

Load(ReadOnlySpan<byte>, StbiImageFormat, bool)

Load an image.

public static StbImage Load(ReadOnlySpan<byte> span, StbiImageFormat format = StbiImageFormat.Default, bool asFloat = false)

Parameters

span ReadOnlySpan<byte>

The span of memory to load from.

format StbiImageFormat

The desired image format.

asFloat bool

Returns

StbImage

The image object.

PeekInfo(Stream, out int, out int, out StbiImageFormat)

Peek image info from a stream.

public static bool PeekInfo(Stream stream, out int width, out int height, out StbiImageFormat format)

Parameters

stream Stream

The stream to peek into.

width int

Width of the image.

height int

Height of the image.

format StbiImageFormat

The image format.

Returns

bool

True if the stream contained an image.

PeekInfo<T>(ReadOnlySpan<T>, out int, out int, out StbiImageFormat)

Peek image info from a span.

public static bool PeekInfo<T>(ReadOnlySpan<T> span, out int width, out int height, out StbiImageFormat format) where T : unmanaged

Parameters

span ReadOnlySpan<T>

The span to peek into.

width int

Width of the image.

height int

Height of the image.

format StbiImageFormat

The image format.

Returns

bool

True if the stream contained an image.

Type Parameters

T

TryLoad(out StbImage?, Stream, StbiImageFormat, bool)

Try loading an image, without raising exceptions.

public static bool TryLoad(out StbImage? image, Stream stream, StbiImageFormat format = StbiImageFormat.Default, bool asFloat = false)

Parameters

image StbImage

The resulting image.

stream Stream

Source stream.

format StbiImageFormat

The desired image format.

asFloat bool

Returns

bool

True on success.

TryLoad(out StbImage?, ReadOnlySpan<byte>, StbiImageFormat, bool)

Try loading an image, without raising exceptions.

public static bool TryLoad(out StbImage? image, ReadOnlySpan<byte> span, StbiImageFormat format = StbiImageFormat.Default, bool asFloat = false)

Parameters

image StbImage

The resulting image.

span ReadOnlySpan<byte>

Source memory span.

format StbiImageFormat

The desired image format.

asFloat bool

Returns

bool

True on success.

WriteBmp(Stream)

Write image to a BMP file.

public void WriteBmp(Stream dest)

Parameters

dest Stream

The destination stream.

Remarks

Incurs a conversion cost if the image format is not a byte format. Affected by non-thread safe global options.

WriteBmp<T>(ReadOnlySpan<T>, int, int, StbiImageFormat, Stream, bool)

Write any image to a BMP file.

public static void WriteBmp<T>(ReadOnlySpan<T> data, int width, int height, StbiImageFormat format, Stream destination, bool isFloat = false) where T : unmanaged

Parameters

data ReadOnlySpan<T>

Span of pixel data.

width int

Width of the pixel data in pixels.

height int

Height of the pixel data in pixels.

format StbiImageFormat

Color format of the pixel data. Must not be StbiImageFormat().

destination Stream

The destination stream.

isFloat bool

True if the pixel format in data is a floating point format.

Type Parameters

T

Any packed byte or float array structure.

Remarks

This will incur a conversion cost if the pixel format is not a byte format. Ignores the alpha channel. Affected by global non-thread safe options.

WriteHdr(Stream)

Write image to a PNG file.

public void WriteHdr(Stream dest)

Parameters

dest Stream

The destination stream.

Remarks

Incurs a conversion cost if the image format is not a float format. Affected by non-thread safe global options.

WriteHdr<T>(ReadOnlySpan<T>, int, int, StbiImageFormat, Stream, bool)

Write any image to a PNG file.

public static void WriteHdr<T>(ReadOnlySpan<T> data, int width, int height, StbiImageFormat format, Stream destination, bool isFloat = false) where T : unmanaged

Parameters

data ReadOnlySpan<T>

Span of pixel data.

width int

Width of the pixel data in pixels.

height int

Height of the pixel data in pixels.

format StbiImageFormat

Color format of the pixel data. Must not be StbiImageFormat().

destination Stream

The destination stream.

isFloat bool

True if the pixel format in data is a floating point format.

Type Parameters

T

Any packed byte or float array structure.

Remarks

This will incur a conversion cost if the pixel format is not a float format. Affected by global non-thread safe options.

WriteJpg(Stream, int)

Write image to a PNG file.

public void WriteJpg(Stream dest, int quality = 90)

Parameters

dest Stream

The destination stream.

quality int

Remarks

Incurs a conversion cost if the image format is not a byte format. Ignores alpha channel. Affected by non-thread safe global options.

WriteJpg<T>(ReadOnlySpan<T>, int, int, StbiImageFormat, Stream, int, bool)

Write any image to a PNG file.

public static void WriteJpg<T>(ReadOnlySpan<T> data, int width, int height, StbiImageFormat format, Stream destination, int quality = 90, bool isFloat = false) where T : unmanaged

Parameters

data ReadOnlySpan<T>

Span of pixel data.

width int

Width of the pixel data in pixels.

height int

Height of the pixel data in pixels.

format StbiImageFormat

Color format of the pixel data. Must not be StbiImageFormat().

destination Stream

The destination stream.

quality int
isFloat bool

True if the pixel format in data is a floating point format.

Type Parameters

T

Any packed byte or float array structure.

Remarks

This will incur a conversion cost if the pixel format is not a byte format. Ignores the alpha channel. Affected by global non-thread safe options.

WritePng(Stream)

Write image to a PNG file.

public void WritePng(Stream dest)

Parameters

dest Stream

The destination stream.

Remarks

Incurs a conversion cost if the image format is not a byte format. Affected by non-thread safe global options.

WritePng<T>(ReadOnlySpan<T>, int, int, StbiImageFormat, Stream, bool)

Write any image to a PNG file.

public static void WritePng<T>(ReadOnlySpan<T> data, int width, int height, StbiImageFormat format, Stream destination, bool isFloat = false) where T : unmanaged

Parameters

data ReadOnlySpan<T>

Span of pixel data.

width int

Width of the pixel data in pixels.

height int

Height of the pixel data in pixels.

format StbiImageFormat

Color format of the pixel data. Must not be StbiImageFormat().

destination Stream

The destination stream.

isFloat bool

True if the pixel format in data is a floating point format.

Type Parameters

T

Any packed byte or float array structure.

Remarks

This will incur a conversion cost if the pixel format is not a byte format. Affected by global non-thread safe options.

WriteTga(Stream)

Write image to a TGA file.

public void WriteTga(Stream dest)

Parameters

dest Stream

The destination stream.

Remarks

Incurs a conversion cost if the image format is not a byte format. Ignores alpha channel. Affected by non-thread safe global options.

WriteTga<T>(ReadOnlySpan<T>, int, int, StbiImageFormat, Stream, bool)

Write any image to a TGA file.

public static void WriteTga<T>(ReadOnlySpan<T> data, int width, int height, StbiImageFormat format, Stream destination, bool isFloat = false) where T : unmanaged

Parameters

data ReadOnlySpan<T>

Span of pixel data.

width int

Width of the pixel data in pixels.

height int

Height of the pixel data in pixels.

format StbiImageFormat

Color format of the pixel data. Must not be StbiImageFormat().

destination Stream

The destination stream.

isFloat bool

True if the pixel format in data is a floating point format.

Type Parameters

T

Any packed byte or float array structure.

Remarks

This will incur a conversion cost if the pixel format is not a byte format. Affected by global non-thread safe options.