Skip to content

Commit

Permalink
Merge pull request #4 from jchand99/verticallyflip
Browse files Browse the repository at this point in the history
Add SetFlipVerticallyOnLoad method
  • Loading branch information
Tom94 authored Nov 6, 2020
2 parents 721b6dd + 2def2cb commit bd889ef
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion dependencies/stb
4 changes: 4 additions & 0 deletions libstbi/src/stbi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ extern "C" {
return stbi_load_from_memory(data, (int)len, w, h, nChannels, nDesiredChannels);
}

EXPORT void SetFlipVerticallyOnLoad(int shouldFlip) {
stbi_set_flip_vertically_on_load(shouldFlip);
}

EXPORT void Free(unsigned char* pixels) {
stbi_image_free(pixels);
}
Expand Down
23 changes: 16 additions & 7 deletions stbi-sharp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ public class Stbi
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()}");
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>
Expand Down Expand Up @@ -204,6 +204,13 @@ public static void InfoFromMemory(MemoryStream data, out int width, out int heig
[DllImport("stbi")]
unsafe public static extern byte* LoadFromMemory(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"/>. Only
/// has to be called when the byte-pointer overload of <see cref="LoadFromMemory"/> was used.
Expand All @@ -213,9 +220,9 @@ public static void InfoFromMemory(MemoryStream data, out int width, out int heig
unsafe public static extern void Free(byte* data);

/// <summary>
/// After failure to load an image, returns a string describing the reason for the failure.
/// After failure to load an image, returns a pointer to a string describing the reason for the failure.
/// </summary>
[DllImport("stbi", EntryPoint="FailureReason")]
[DllImport("stbi", EntryPoint = "FailureReason")]
unsafe public static extern IntPtr FailureReasonIntPtr();

/// <summary>
Expand All @@ -240,9 +247,11 @@ public static void InfoFromMemory(MemoryStream data, out int width, out int heig
/// been allocated to store the image data.</returns>
unsafe public static StbiImage LoadFromMemory(ReadOnlySpan<byte> data, int desiredNumChannels)
{
fixed (byte* address = data) {
fixed (byte* address = data)
{
byte* pixels = LoadFromMemory(address, data.Length, out int width, out int height, out int numChannels, desiredNumChannels);
if (pixels == null) {
if (pixels == null)
{
throw new ArgumentException($"STBI could not load an image from the provided {nameof(data)}: {FailureReason()}");
}

Expand Down

0 comments on commit bd889ef

Please sign in to comment.