Sign in to follow this  

SharpDX loading TGA textures

Recommended Posts

Hello, I am trying to load TGA textures in sharpdx, but it seems it is not supported.

 

Is there any simple method or do I have to write my own TGA format parser, parse the file into pixel array and then use those pixels?

 

I was trying to use 

var bitmapDecoder = new SharpDX.WIC.BitmapDecoder(
                factory,
                filename,
                SharpDX.WIC.DecodeOptions.CacheOnDemand
                );

But I get WINCODEC_ERR_COMPONENTNOTFOUND/Componentnotfound on this code, so I presume TGA is not supported.

Share this post


Link to post
Share on other sites

Before rolling your own parser, there are some C# texture IO libraries out there that support loading TGA files.

 

Shameless plug, because I maintain one - TeximpNet. It wraps two native libraries (FreeImage and Nvidia Texture Tools) in one convenient package for loading and processing textures. FreeImage also has its own C# bindings, but I don't know how well they are maintained.

Share this post


Link to post
Share on other sites

I'm using this code to load TGA-images: https://www.codeproject.com/Articles/31702/NET-Targa-Image-Reader

It's released under CPOL-license, so should be comparable to MIT-license and won't bring problems, when using it

using Paloma;

...
                var image  = new TargaImage(filename);
                var format = Format.Unknown;
                var imageData = image.Image;
                if(image.Header.PixelDepth==24) {
                    format = Format.B8G8R8A8_UNorm;
                    imageData = image.Image.Clone(new System.Drawing.Rectangle(0, 0, image.Image.Width, image.Image.Height), System.Drawing.Imaging.PixelFormat.Format32bppRgb);
                }
                var bitmapData = imageData.LockBits(new System.Drawing.Rectangle(0, 0, image.Image.Width, image.Image.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, imageData.PixelFormat);
                var bufferSize = bitmapData.Height * bitmapData.Stride;

                // copy our buffer to the texture
                int stride = image.Image.Width * 4;
                var tex = new Texture2D(device, new Texture2DDescription() {
                    Width = image.Header.Width,
                    Height = image.Header.Height,
                    Format = format,
                    ArraySize = 1,
                    BindFlags = BindFlags.ShaderResource | BindFlags.RenderTarget,
                    Usage = ResourceUsage.Default,
                    CpuAccessFlags = CpuAccessFlags.None,
                    MipLevels = GetNumMipLevels(image.Header.Width,image.Header.Height),
                    OptionFlags = ResourceOptionFlags.GenerateMipMaps,
                    SampleDescription = new SampleDescription(1, 0),
                });
                device.ImmediateContext.UpdateSubresource(tex,0,null,bitmapData.Scan0,stride,0);

                // unlock the bitmap data
                imageData.UnlockBits(bitmapData);

Share this post


Link to post
Share on other sites

Thank you both, will test it once I get over another problem I found.

 

EDIT: I am using the teximpnet lib and works fine for me.

Edited by satik

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this