Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added methods to expose the unmanaged pixel array. #138

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions src/Graphics/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,44 @@ public class Image : ObjectBase
throw new LoadingFailedException("image");
}

////////////////////////////////////////////////////////////
/// <summary>
/// Construct the image from a file in memory
/// </summary>
/// <param name="bytes">Byte array containing the file contents</param>
/// <param name="length">The length of the byte array</param>
/// <exception cref="ArgumentException">The passed in pointer is zero or the length is less or equal to zero.</exception>
/// <exception cref="LoadingFailedException" />
////////////////////////////////////////////////////////////
public Image(IntPtr bytes, ulong length) :
base(IntPtr.Zero)
{
if (bytes == IntPtr.Zero)
throw new ArgumentException("The pointer to byte array must not be null.", nameof(bytes));

if (length <= 0u)
throw new ArgumentException("The length of the byte array must be greater than zero.", nameof(length));

CPointer = sfImage_createFromMemory(bytes, length);

if (CPointer == IntPtr.Zero)
throw new LoadingFailedException("image");
}

////////////////////////////////////////////////////////////
/// <summary>
/// Construct the image from a file in memory
/// </summary>
/// <param name="bytes">Byte array containing the file contents</param>
/// <param name="length">The length of the byte array</param>
/// <exception cref="ArgumentException">The passed in pointer is zero or the length is less or equal to zero.</exception>
/// <exception cref="LoadingFailedException" />
////////////////////////////////////////////////////////////
public Image(IntPtr bytes, int length) :
this(bytes, (ulong) length)
{
}

////////////////////////////////////////////////////////////
/// <summary>
/// Construct the image directly from an array of pixels
Expand Down Expand Up @@ -293,6 +331,57 @@ public byte[] Pixels
}
}

////////////////////////////////////////////////////////////
/// <summary>
/// Returns the pointer to the pixel data in unmanaged memory.
/// </summary>
/// <remarks>
/// No memory is copied.
/// </remarks>
/// <param name="length">Length of the array in unmanaged memory.</param>
/// <returns>Pointer to the pixel data</returns>
////////////////////////////////////////////////////////////
public IntPtr GetNativeData(out int length)
{
ulong l;
IntPtr data = GetNativeData(out l);
length = (int) l;
return data;
}

////////////////////////////////////////////////////////////
/// <summary>
/// Returns the pointer to the pixel data in unmanaged memory.
/// </summary>
/// <remarks>
/// No memory is copied.
/// </remarks>
/// <param name="length">Length of the array in unmanaged memory.</param>
/// <returns>Pointer to the pixel data</returns>
////////////////////////////////////////////////////////////
public IntPtr GetNativeData(out ulong length)
{
Vector2u size = Size;
length = size.X * size.Y * 4;
return sfImage_getPixelsPtr(CPointer);
}

////////////////////////////////////////////////////////////
/// <summary>
/// Returns the pointer to the pixel data in unmanaged memory.
/// </summary>
/// <remarks>
/// No memory is copied.
/// The length of the array in unmanaged memory is "width * height * 4".
/// </remarks>
/// <returns>Pointer to the pixel data</returns>
////////////////////////////////////////////////////////////
public IntPtr GetNativeData()
{
ulong l;
return GetNativeData(out l);
}

////////////////////////////////////////////////////////////
/// <summary>
/// Size of the image, in pixels
Expand Down