diff --git a/src/DlibDotNet.Native/dlib/image_transforms/image_gradients.cpp b/src/DlibDotNet.Native/dlib/image_transforms/image_gradients.cpp new file mode 100644 index 00000000..dcd4079b --- /dev/null +++ b/src/DlibDotNet.Native/dlib/image_transforms/image_gradients.cpp @@ -0,0 +1 @@ +#include "image_gradients.h" \ No newline at end of file diff --git a/src/DlibDotNet.Native/dlib/image_transforms/image_gradients.h b/src/DlibDotNet.Native/dlib/image_transforms/image_gradients.h new file mode 100644 index 00000000..d5058cba --- /dev/null +++ b/src/DlibDotNet.Native/dlib/image_transforms/image_gradients.h @@ -0,0 +1,133 @@ +#ifndef _CPP_IMAGE_GRADIENTS_H_ +#define _CPP_IMAGE_GRADIENTS_H_ + +#include "../export.h" +#include +#include +#include "../template.h" +#include "../shared.h" + +using namespace dlib; +using namespace std; + +#pragma region template + +#define image_gradients_gradient_x_template(__TYPE__, error, type, ...) \ +*((dlib::rectangle*)valid_area) = gradients->gradient_x(*((array2d<__TYPE__>*)in_image), *((array2d*)out_image)); + +#define image_gradients_gradient_y_template(__TYPE__, error, type, ...) \ +*((dlib::rectangle*)valid_area) = gradients->gradient_y(*((array2d<__TYPE__>*)in_image), *((array2d*)out_image)); + +#define image_gradients_gradient_xx_template(__TYPE__, error, type, ...) \ +*((dlib::rectangle*)valid_area) = gradients->gradient_xx(*((array2d<__TYPE__>*)in_image), *((array2d*)out_image)); + +#define image_gradients_gradient_xy_template(__TYPE__, error, type, ...) \ +*((dlib::rectangle*)valid_area) = gradients->gradient_xy(*((array2d<__TYPE__>*)in_image), *((array2d*)out_image)); + +#define image_gradients_gradient_yy_template(__TYPE__, error, type, ...) \ +*((dlib::rectangle*)valid_area) = gradients->gradient_yy(*((array2d<__TYPE__>*)in_image), *((array2d*)out_image)); + +#pragma endregion template + +DLLEXPORT dlib::image_gradients* get_image_gradients(long scale) +{ + return new dlib::image_gradients(scale); +} + +DLLEXPORT int image_gradients_gradient_x(dlib::image_gradients* gradients, + array2d_type type, + void* in_image, + void* out_image, + void* valid_area) +{ + int error = ERR_OK; + + array2d_nonalpha_template(type, + error, + image_gradients_gradient_x_template, + in_image, + out_image, + valid_area); + + return error; +} + +DLLEXPORT int image_gradients_gradient_y(dlib::image_gradients* gradients, + array2d_type type, + void* in_image, + void* out_image, + void* valid_area) +{ + int error = ERR_OK; + + array2d_nonalpha_template(type, + error, + image_gradients_gradient_y_template, + in_image, + out_image, + valid_area); + + return error; +} + +DLLEXPORT int image_gradients_gradient_xx(dlib::image_gradients* gradients, + array2d_type type, + void* in_image, + void* out_image, + void* valid_area) +{ + int error = ERR_OK; + + array2d_nonalpha_template(type, + error, + image_gradients_gradient_xx_template, + in_image, + out_image, + valid_area); + + return error; +} + +DLLEXPORT int image_gradients_gradient_xy(dlib::image_gradients* gradients, + array2d_type type, + void* in_image, + void* out_image, + void* valid_area) +{ + int error = ERR_OK; + + array2d_nonalpha_template(type, + error, + image_gradients_gradient_xy_template, + in_image, + out_image, + valid_area); + + return error; +} + + +DLLEXPORT int image_gradients_gradient_yy(dlib::image_gradients* gradients, + array2d_type type, + void* in_image, + void* out_image, + void* valid_area) +{ + int error = ERR_OK; + + array2d_nonalpha_template(type, + error, + image_gradients_gradient_yy_template, + in_image, + out_image, + valid_area); + + return error; +} + +DLLEXPORT void image_gradients_delete(dlib::image_gradients* obj) +{ + delete obj; +} + +#endif \ No newline at end of file diff --git a/src/DlibDotNet/ImageTransforms/ImageGradients.cs b/src/DlibDotNet/ImageTransforms/ImageGradients.cs new file mode 100644 index 00000000..113e71f6 --- /dev/null +++ b/src/DlibDotNet/ImageTransforms/ImageGradients.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections.Generic; +using DlibDotNet.Extensions; + +// ReSharper disable once CheckNamespace +namespace DlibDotNet +{ + + public sealed class ImageGradients : DlibObject + { + + #region Constructors + + public ImageGradients(long scale = 1) + { + this.NativePtr = NativeMethods.get_image_gradients(scale); + } + + #endregion + + #region Methods + + public Rectangle GetGradientX(Array2DBase image, Array2D gradient) + { + return InvokeGetGradient(image, gradient, NativeMethods.image_gradients_gradient_x); + } + public Rectangle GetGradientY(Array2DBase image, Array2D gradient) + { + return InvokeGetGradient(image, gradient, NativeMethods.image_gradients_gradient_y); + } + + public Rectangle GetGradientXY(Array2DBase image, Array2D gradient) + { + return InvokeGetGradient(image, gradient, NativeMethods.image_gradients_gradient_xy); + } + + public Rectangle GetGradientXX(Array2DBase image, Array2D gradient) + { + return InvokeGetGradient(image, gradient, NativeMethods.image_gradients_gradient_xx); + } + + public Rectangle GetGradientYY(Array2DBase image, Array2D gradient) + { + return InvokeGetGradient(image, gradient, NativeMethods.image_gradients_gradient_yy); + } + + + private Rectangle InvokeGetGradient(Array2DBase image, Array2D gradient, Func gradientMethod) + { + this.ThrowIfDisposed(); + + if (image == null) + throw new ArgumentNullException(nameof(image)); + + if (gradient == null) + throw new ArgumentNullException(nameof(gradient)); + + image.ThrowIfDisposed(); + gradient.ThrowIfDisposed(); + + using(var rect = new Rectangle.NativeRectangle()) + { + var inType = image.ImageType.ToNativeArray2DType(); + + var ret = gradientMethod(this.NativePtr, inType, image.NativePtr, gradient.NativePtr, rect.NativePtr); + + switch (ret) + { + case NativeMethods.ErrorType.Array2DTypeTypeNotSupport: + throw new ArgumentException($"Input {inType} is not supported."); + } + + return rect.ToManaged(); + } + } + + #region Overrides + + /// + /// Releases all unmanaged resources. + /// + protected override void DisposeUnmanaged() + { + base.DisposeUnmanaged(); + + if (this.NativePtr == IntPtr.Zero) + return; + + NativeMethods.image_gradients_delete(this.NativePtr); + } + + #endregion + + #endregion + + } + +} \ No newline at end of file diff --git a/src/DlibDotNet/PInvoke/ImageTransforms/ImageGradients.cs b/src/DlibDotNet/PInvoke/ImageTransforms/ImageGradients.cs new file mode 100644 index 00000000..876f2568 --- /dev/null +++ b/src/DlibDotNet/PInvoke/ImageTransforms/ImageGradients.cs @@ -0,0 +1,55 @@ +using System; +using System.Runtime.InteropServices; + +// ReSharper disable once CheckNamespace +namespace DlibDotNet +{ + + internal sealed partial class NativeMethods + { + + [DllImport(NativeLibrary, CallingConvention = CallingConvention)] + public static extern IntPtr get_image_gradients(long scale); + + [DllImport(NativeLibrary, CallingConvention = CallingConvention)] + public static extern ErrorType image_gradients_gradient_x(IntPtr gradients, + Array2DType imgType, + IntPtr in_img, + IntPtr out_img, + IntPtr valid_rectangle); + + [DllImport(NativeLibrary, CallingConvention = CallingConvention)] + public static extern ErrorType image_gradients_gradient_y(IntPtr gradients, + Array2DType imgType, + IntPtr in_img, + IntPtr out_img, + IntPtr valid_rectangle); + + [DllImport(NativeLibrary, CallingConvention = CallingConvention)] + public static extern ErrorType image_gradients_gradient_xx(IntPtr gradients, + Array2DType imgType, + IntPtr in_img, + IntPtr out_img, + IntPtr valid_rectangle); + + + [DllImport(NativeLibrary, CallingConvention = CallingConvention)] + public static extern ErrorType image_gradients_gradient_xy(IntPtr gradients, + Array2DType imgType, + IntPtr in_img, + IntPtr out_img, + IntPtr valid_rectangle); + + [DllImport(NativeLibrary, CallingConvention = CallingConvention)] + public static extern ErrorType image_gradients_gradient_yy(IntPtr gradients, + Array2DType imgType, + IntPtr in_img, + IntPtr out_img, + IntPtr valid_rectangle); + + [DllImport(NativeLibrary, CallingConvention = CallingConvention)] + public static extern void image_gradients_delete(IntPtr gradients); + + } + +} \ No newline at end of file