Gmic-Sharp - A .NET wrapper for libgmic

I noticed that there did not appear to be any existing .NET Platform bindings for libgmic, so I decided to write one.
I also had a request on the Paint.NET forum from a G’MIC filter developer for a way to call the G’MIC API from a Paint.NET plugin.

Please note that this is currently a beta version.
It currently only targets Windows and the .NET Framework, but it should be possible for it to work with .NET Core on Linux / macOS.
This would require changing the gmic-sharp code to use the correct native library name for each platform.

The wrapper consists of a 32-bit and 64-bit C++ libraries that wrap libgmic, and a managed library written in C# that calls into the appropriate C++ library for the current platform.
There is a Windows-based sample application that demonstrates the use of the library.

gmic-sharp-native provides the native interface between gmic-sharp and libgmic.

gmic-sharp is the library that .NET application use to call G’MIC.

gmic-sharp-example is the Windows-based sample application that demonstrates the use of gmic-sharp.

gmic-sharp-native uses the same dual-license as libgmic (CeCILL v2.1 or CeCILL-C v1), this makes it simpler to statically-link the Windows DLLs to G’MIC.
Both gmic-sharp and gmic-sharp-example use the MIT/X11 license, same as .NET Core.

3 Likes

That is really interesting!
I’d like to share these links from the G’MIC webpage.
We also have a Python binding almost ready for the libgmic. So, it seems a perfect timing to make both appear on the webpage, and communicate about alternative bindings.
What do you think ?

I don’t have a Windows machine right here to test unfortunately. Would it be possible to have a screenshot of the example application running, that I could put on the webpage ?

That would be fine with me.

I added a note to my first post that the library is a Beta version.

I have been investigating Linux and macOS support for the gmic-sharp library, the main issue I see is that .NET Core does not currently have a built-in API for processing images that is can be used on all of its supported operating systems.

Here you go: gmic-sharp-example/images/GmicSharpExample.png at master · 0xC0000054/gmic-sharp-example · GitHub

1 Like

Excellent news as I can convert even more filters in theory. I noticed you haven’t crossposted to pdn forum.

This library is still a beta version, I want to get the final API figured out before anything takes a dependency on it.

Released beta version 0.6.0.

  • Added a GdiPlusGmicBitmap(Image) constructor overload.
  • Improved type-safety for the Gmic class, it now uses a generic parameter to specify the GmicBitmap class that is in use.
  • The IGmicOutputImageFactory now uses a generic parameter to specify the GmicBitmap class that is in use.

See the Changelog for more information on the other changes.
Also updated the Readme to list the library features.

Released beta version 0.7.0.

  • Added support for changing the host application name seen by G’MIC scripts
  • Added support for running on Linux and macOS when using .NET Standard 2.1
  • The CustomResourcePath property now supports paths with non-ASCII characters

See the Changelog for more information on the other changes.

Added a cross-platform example application, gmic-sharp-cli-example.
It is a .NET Core 3.1 Console application that mimics gmic-cli.
The current release includes native binaries for the following operating systems:

Ubuntu 20.4 64-bit (x64)
Microsoft Windows 64-bit (x64)
Microsoft Windows 32-bit (x86)

@PDN_GMIC I don’t know where to report this via github since there’s gmic-sharp-native,gmic-sharp,gmic-sharp pdn. So… When I tried doing this.

                string command = string.Format(CultureInfo.InvariantCulture,
                                               "m \"gmic_pdn_shape_circle: shape_circle $\"\"1\"" +
                                               "gmic_pdn_shape_circle 100 f.. i0#-1*255 k[0]");

Just a black image. Not even a circle there.

What it the equivalent G’MIC CLI command?
My guess is that it may be a formatting error.

rep_test_pdn:
m "gmic_shape_circle: shape_circle $""1"
gmic_shape_circle 100 f.. i0#-1*255 k[0]
$ sp , rep_test_pdn

outputs a circle of 100 px in the corner.

I am getting a format exception whenever I run that command.

 *** Error in ./ *** Command 'f': Invalid selection [-2] (contains index '-2', not in range -1...0)

I also had to use a StringBuilder to get the first line properly formatted.

Did you add a image before running rep_test_pdn command? Should work.

EDIT: Also, the first line of rep_test_pdn is properly formatting in g’mic-cli. On context of gmic-c#, what’s the line looks like after stringbuilder?

image

m "gmic_pdn_shape_circle: shape_circle $""1"gmic_shape_circle 100 f.. i0#-1*255 k[0]

Try this:

m "gmic_pdn_shape_circle: shape_circle $""1" gmic_pdn_shape_circle 100 f.. i0#-1*255 k[0]

This actually worked on PDN. I’m not sure why the others don’t work.

That works.
Here is the StringBuilder code for reference.

StringBuilder sb = new StringBuilder("m ");
sb.Append('"').Append("gmic_pdn_shape_circle: shape_circle$").Append('"').Append('"').Append('1').Append('"');
sb.Append(" gmic_pdn_shape_circle 100 f.. i0#-1*255 k[0]");

Now I see why the others don’t work. They need a space on the beginning of new line for it to work. That is not convenient, but I can work with it though.

\n will also work.


@PDN_GMIC Just one more thing. What I supposed to do when I have to use {} characters within the g’mic parser? I know that {} are reserved for c# to.string, so, I’m wondering if there is a workaround.

EDIT: Using double brackets is the escape. Had to search for a solution for a while.