StbSharp: C# Port of Famous C Framework

Hello everyone,
I would like to introduce StbSharp: https://github.com/rds1983/StbSharp
Which is open-source(MIT licensed) C# port of famous framework Stb: https://github.com/nothings/stb
Right now, though, it is quite young. Only stb_image.h is ported and not completely; it can load only .png and .jpg files.

This sample demonstrates using StbSharp in the MonoGame: https://github.com/rds1983/StbSharp/releases/download/0.1/StbSharp.MonoGame.WindowsDX.Test.0.1.zip
It requires VS2013 & MonoGame 3.5 to be opened/compiled.
The sample loads the same .jpg image twice: first time through the Texture2D.FromStream and second - through StbSharp. Then it renders both images and reports loading time:

As you can see the StbSharp is somewhat slower. But on the other hand it gives consistent result of the run-time image loading through different platforms.

It is important to note, that StbSharp is not manual port. It has been ported using another open-source(MIT also) project called Sichem: https://github.com/rds1983/Sichem
Sichem is C to C# code converter. It uses libclang to build the syntax tree from C and generates the C# code from it. Right now, Sichem works only with stb_image.h. However if required it could be tuned to port other C projects.

Now, let’s return to StbSharp. My first priority is to add other images types supported by stb_image.h (gif, pic, psd, etc). Then I’ll consider taking another stb library (i.e. stb_truetype.h).

Thanks.

2 Likes

Interesting to see. Looking at the code, you may be able to get a small improvement at Image.cs(303) by using fixed on the data array as it will be doing bounds checking on every access. You may also get a benefit from Marshal.Copy from an IntPtr to managed array.

You may be interested to see there is already a port of stb_truetype in the form of TrueTypeSharp.

1 Like

Thanks for the advice. I’ve replaced that cycle with Marshal.Copy

Meanwhile, the latest StbSharp became PCL and theoretically should work on any C# supported platform. I’ve tested it on Android with the following sample (it requires VS 2013 & MG 3.5 & Xamarin in order to be run):
https://github.com/rds1983/StbSharp/releases/download/0.1.5/StbSharp.MonoGame.Android.Test.zip

On the following device: http://www.dns-shop.ru/product/485ccb8e38123330/8-planset-irbis-tz80-16-gb-3g-cernyj/characteristics
The above link is in Russian, however the device characteristics are understandable.

The testing gave following result:

So the loading time is 151 ms for Texture2D.FromStream and 227 for StbSharp. Overally StbSharp is about 1.6 times slower than Texture2D.FromStream. And there was same proportion on WindowsDX.

Great there is TrueTypeSharp already. Probably I’ll port something else then.

Hi everyone,
I would like to post update on this project. As there was significant progress during the last year.
Now following stb libraries had been ported: stb_image.h, stb_image_write.h, stb_image_resize.h, stb_dxt.h, stb_vorbis.c and stb_truetype.h
As there was some confusion, I would like to clarify, that StbSharp is port. It’s not wrapper. I took original C code and converted it to C#. So StbSharp doesnt require any native binaries.
I’ve made a small MG sample(requires MG to be installed). It demonstrates StbSharp in action: it draws image loaded with StbImage, plays music loaded with StbVorbis and draws text from font loaded with StbTrueType.
Link to the sample: https://github.com/rds1983/StbSharp/releases/download/0.6.5.31/MonoGameWithStb.zip

Do you thing that we could port IMGUI in full C# with Sichem ?

Unfortunately, no.
Sichem ports code from C to C#, while IMGUI is C++.
There’s another pure C GUI lib called nuklear, which I’ve ported already: https://github.com/rds1983/NuklearSharp

Meanwhile StbSharp 0.7.1 is out.
It has MonoGame sample that does following:

  1. Draws an image loaded with stb_image.
  2. Dynamically creates SpriteFont from two different ttfs using stb_truetype and draws text.
  3. Streams audio to DynamicSoundEffect from an ogg file using stb_vorbis.

Sample Source Code: https://github.com/rds1983/StbSharp/blob/master/Samples/StbSharp.MonoGame.Test/Game1.cs
StbSharp Project Site: https://github.com/rds1983/StbSharp

3 Likes

I would like to post update for the project.
Latest StbSharp is no longer monolithic, but had been split into separate projects: https://github.com/StbSharp
Each project is avaiable at the nuget with the version corresponding to the one it had been ported from(i.e. StbImageSharp version 2.22 means it was ported from stb_image.h’s version 2.22).

Also I’ve kept only projects that are at least somewhat useful to the developers. Which are StbImageSharp, StbImageWriteSharp, StbTrueTypeSharp and StbVorbisSharp. Other stb libs seems to have almost zero interest in them.
Though if anyone would like any other stb lib to be ported, then, please, let me know.

1 Like

Made safe version of StbImageSharp: https://github.com/StbSharp/StbImageLib

Made performance testing framework that runs over ~800 images and tries to load 10 times each with StbImageSharp, then with native stb_image.h called through C++/CLI(Stb.Native), then with SixLabors ImageSharp.
Here are results(total loading times):

9 – StbImageSharp - jpg: 9368 ms, tga: 1337 ms, bmp: 208 ms, psd: 0 ms, png: 51936 ms, Total: 62849 ms
9 – Stb.Native - jpg: 3175 ms, tga: 1315 ms, bmp: 112 ms, psd: 0 ms, png: 45751 ms, Total: 50353 ms
9 – ImageSharp - jpg: 75071 ms, bmp: 44 ms, png: 48527 ms, Total: 123642 ms
9 – Total files processed - jpg: 170, tga: 41, bmp: 7, psd: 1, png: 568, Total: 787

More details are available here: https://github.com/StbSharp/StbImageSharp#reliability--performance

Ported stb_dxt.h: https://github.com/StbSharp/StbDxtSharp