[C# Library] GDImageLibrary

Foreword

I was looking for a way to display textures from Grim Dawn properly for a long time now and had a really hard time getting on with it which is why I started this project.

The idea is to provide an easy-to-use library that allows displaying the .tex and .dds files from Grim Dawn as I want to prevent you guys from getting frustrated the same way I did.

The library is written in C#, using Visual Studio Express 2013 for desktop.

Usage (Visual Studio)

Adding a reference to your project

  1. Right click “references” in your project explorer
  2. Choose “Add reference”


(German system, sorry)

  1. Choose “browse” on the left of the window that should now appear.
  2. Choose “browse” on the lower right of the window

  1. Navigate to “GDImageLibrary.dll” and double click it.
  2. If you did everything right, it should look like this:

  1. Now click “ok”. You now should be able to use the GDImageLibrary.
  2. To add it as namespace, add it to your code like this:

Code Summary

GDImage

ImageBuild Method

The ImageBuild Method builds an object of the type “Bitmap” from a byte array that holds the pixels of an image. It also requires the width and height of the image.


Bitmap bm = GDImage.ImageBuild(byte[] PixelArray, int ImageWidth, int ImageHeight);

ConvertIntArrayToByteArray Method

The ConvertIntArrayToByteArray method converts, as the name says, an integer array to a byte array.



byte[] ByteArray = GDImage.ConvertIntArrayToByteArray(int[] IntArray);

GDImageLibrary._Tex

ConvertTexToDDs Method

The ConvertTexToDDs method converts a .tex file to a .dds file and returns an array of bytes containing the whole image file.


byte[] DDsFile = GDImageLibrary._TEX.ConvertTexToDDS(string Textfile);

Overload:


byte[] DDsFile = GDImageLibrary._TEX.ConvertTexToDDS(byte[] TexFileContent);

LoadTexImage Method

The LoadTexImage method loads a .tex file and returns a bitmap for easy displaying of .tex files.


Bitmap bm = GDImageLibrary._TEX.LoadTexImage(string Texfile);

Overload:


Bitmap bm = GDImageLibrary._TEX.LoadTexImage(byte[] TexFileContent);

GetTexFileWidth Method

The GetTexFileWidth method returns an integer that declares the width of a .tex file.


int Width = GDImageLibrary._TEX.GetTexFileWidth(string Texfile);

Overload:


int Width = GDImageLibrary._TEX.GetTexFileWidth(byte[] TexFileContent);

GetTexFileHeight Method

The GetTexFileHeight Method returns an integer that declares the height of a .tex file.


int Height = GDImageLibrary._TEX.GetTexFileHeight(string Texfile);

Overload:


int Height = GDImageLibrary._TEX.GetTexFileHeight(byte[] TexFileContent);

SaveAsDDS Method

The SaveAsDDS method saves a .tex file as a .dds file.


GDImageLibrary._TEX.SaveAsDDS(string texfile, bool overwrite);

Overload:


GDImageLibrary._TEX.SaveAsDDS(string texfile, string DDsFileName, bool overwrite);

GDImageLibrary._DDS

LoadImage Method

The LoadImage method loads a .dds file and returns an object of the type “bitmap” for easy displaying of .dds files.


Bitmap bm = GDImageLibrary._DDS.LoadImage(string DDsFile);

Overload:


Bitmap bm = GDImageLibrary._DDS.LoadImage(byte[] DDsFileContent);

GetFileWidth Method

The GetFileWidth method returns an integer that declares the width of a .dds file.


int Width = GDImageLibrary._DDS.GetFileWidth(string DDsfile);

Overload:


int Width = GDImageLibrary._DDS.GetFileWidth(byte[] DDsFileContent);

GetFileHeight Method

The GetFileHeight method returns an integer that declares the height of a .dds file.


int Width = GDImageLibrary._DDS.GetFileHeight(string DDsfile);

Overload:


int Width = GDImageLibrary._DDS.GetFileHeight(byte[] DDsFileContent);

Special Thanks

  • Special thanks to Tamschi for providing his TQ.Texture library under the GPL and not getting tired of answering my questions
  • Special thanks to Mamba for pointing me torwards the DDSReader Class (see below) and offering some sample code
  • Special thanks to Kenji Sasaki for providing his DDSReader class under the MIT license.

Please let me know if there are any problems / wishes for future updates.

Since this is the first ever library that I release it might be that there are a few things I didn’t think about and therefore didn’t discover which could result in not intended problems.

Attachment: GDImageLibrary.zip
Attachment: GDImageLibrary Source.zip
Attachment: c010_axe.png

Thanks Elfe, very useful!

Have you found a good way to make the backgrounds transparent? Like a way to use/set the alpha channel on the dds files.
Simply setting MakeTransparent() on a Bitmap isn’t perfect (see the attached example), but the ingame bitmaps are.

I haven’t been looking for something like that since the initial reason (a project of mine) that I made this for uses only textures that have no alphas or at least don’t require using them atm.

Maybe I’ll look into that in the future but I can’t give promises as I’m pretty much new to this field of programming.

As I wrote it took me long to get this working and the reason I release this is simply that I want to prevent others from going through the same frustration as I had the feeling that my head explodes of ideas for tools but this was always the point that made me stop trying to make ideas work since I couldn’t display the images.

Don’t worry about it, we’ll see what the modding tools add first. This is already a great addition to go straight from tex to image.

I suggest to rename it into something like GrimImageLibrary, because GDI part is very confusing.

I’ve found out that nconvert from XnView works: . It’s a CLI tool though, not a library.

I’ve also tried ImageMagick, but I’m not sure if it supports this kind of DDS transparency.

I’m already using convert from ImageMagick, which gives similar results as the MakeTransparent from bitmap.

That convert from xnview looks very promising, I’ll look into it!

Edit XnConvert does exactly what I needed, it’s great :slight_smile: Graceful Dusk looks really good now!

Hi @Shalie, @Abradoks,

Apologies for necro’ing an older thread but could you post the command line arguments you used for nconvert? I’ve just tried converting a .TEX to .PNG using it now and get the below error;

my_error_exit...<Not a JPEG file: starts with 0x54 0x45>
  Error: Don't know how to read this picture (d02_medal_badge_of_mastery.tex)

Thanks

I don’t use nconvert from the command line, I use the program XnConvert. It’s very user-friendly, just give a folder with images as input, select your output type and click on a Convert button.

I used TQ Texture Tools to convert tex to DDS first. Then called nconvert like this:

nconvert -out png -o d02_medal_badge_of_mastery.png d02_medal_badge_of_mastery.tex.dds

That error looks like the program you are using either does not support dds or you need to specify the in type of the picture you’re trying to convert.

Right, forgot about the tex part. I use TQ Texture Tools too, a bit buggy, but it works.

He probably ported the code the wiki and GD stashes uses/used.

There’s a bug in readA8R8G8B8 where it reads the alpha channel then just discards it.