LogicAndTrick / sledge-formats

C# parsers and formats for Half-Life 1 and related engines.
MIT License
71 stars 10 forks source link

Issues reading data from GetBgra32Data #29

Closed cabeca1143 closed 3 months ago

cabeca1143 commented 6 months ago

I've been trying convert TF2 sprays into bmp files, but the images end up all warped and I'm unable to pinpoint the issue.

This is the original image: image

And this is what I end up with: Output

Here's a snipet of my code: image

Perhaps I misinterpreted how the pixels in the byte array are organized? I couldn't find much information about it. The spray is in the Dxt1 format.

LogicAndTrick commented 6 months ago

I can see an issue in your code - the data array that you get from GetBgra32Data is row-first, but your loops are column-first. Try switching the loops in your code:

    for (int y = 0; y < img.Height; y++)
    {
        for (int x = 0; x < img.Width; x++)
        {

Also, ImageSharp has a built-in method to do this for you, try this code instead:

using var fileStream = File.OpenRead(InputPath);
var testFile = new VtfFile(fileStream);
var image = testFile.Images.First();

var pixelData = image.GetBgra32Data();
var outputImg = Image.LoadPixelData<Bgra32>(pixelData, image.Width, image.Height);

outputImg.SaveAsBmp(OutputPath);
cabeca1143 commented 5 months ago

Aw shoot, can't believe I overlooked that 🤡 Thanks for the tip!

cabeca1143 commented 5 months ago

As I started dumping sprays, I noticed quite a lot of them end up as single pixels. From my testing, 191 out of the 377 sprays end up as single pixels. I'll attach a few of them.

Sprays.zip

LogicAndTrick commented 5 months ago

That's most likely because you are selecting the first image inside the VTF. For files with mipmaps, the first image is often the very smallest mipmap. Try selecting the largest image rather than the first one. (e.g. testFile.Images.MaxBy(x => x.Width)).

LogicAndTrick commented 3 months ago

With version 1.1.0 and the new Sledge.Formats.Texture.ImageSharp extension package, I've added some extension methods to make this easier. Example code looks like:

using var file = File.OpenRead(@"path/to/some.vtf");
var vtf = new VtfFile(file);
using var image = vtf.ToImage<Rgba32>();
image.Save(@"path/to/some.png");