niacdoial / blemd

BMD importer for blender (BDL format also partially supported)
GNU General Public License v3.0
32 stars 13 forks source link

bmdview TGA export fails if texture contains more than one mipmap #11

Closed julmb closed 5 years ago

julmb commented 6 years ago

The file bmdview.cpp contains the following piece of code for saving I8 format textures as TGA:

//flip vertical
std::vector<u8> data(h.width*h.height);
data = img.imageData;
//std::copy(img.imageData.begin(), img.imageData.begin() + data.size(),
//  data.begin());
flipVertical(data, h.width, h.height, 1);
file.write((char*)&data[0], data.size());

The object that is constructed in the first line is completely overwritten by the copy in the second line. If there is more than one mipmap in the texture, the data object then contains too much data and the flipVertical function fails.

The commented-out lines would do the right thing by only copying the first mipmap rather than the whole img.imageData vector. Alternatively, the following code would also do the trick:

//flip vertical
std::vector<u8> data(img.imageData.begin(), img.imageData.begin() + h.width * h.height);
flipVertical(data, h.width, h.height, 1);
file.write((char*)&data[0], data.size());

I'm not sure if the largest mipmap is always the first one, but at least for my test cases, it worked. If that is not the case in general, one would have to do something smarter here.

Sorry for not just providing a patch, I'm currently on Windows and things are painful.

niacdoial commented 6 years ago

This patch was put into the latest commit. Thanks!