Thanks.
I was trying to avoid any pre-conversion (making the conversion at execution in the RAM) but surprisingly I didn't find any RGBA8888 to PNG conversion lightweight library (ImageMagick does that but is over-complicated).
But whatever, making preconverted copies is ok.
edit:
Finally, with Magick++:
Code:
#include <stdint.h>
#include <fstream>
#include <iostream>
#include <Magick++.h>
#include "squish.h"
bool BLP2_Convert(const std::string& _blpfile, const std::string& _extension) {
struct {
char BLP2[4];
uint32_t version;
uint8_t compression;
uint8_t alpha_depth;
uint8_t alpha_encoding;
uint8_t mipmaps;
uint32_t width;
uint32_t height;
uint32_t mipmaps_offsets[16];
uint32_t mipmaps_sizes[16];
} blpheader;
std::ifstream ifs(_blpfile.c_str(), std::ifstream::in|std::ifstream::binary);
//Get header informations
ifs.read((char*)&blpheader, 0x94);
if (*(uint32_t*)&blpheader.BLP2[0] != 844123202) {
std::cout << "Not BLP2\n";
return false;
}
//Copy first mipmap
squish::u8* blpimage = new squish::u8[blpheader.mipmaps_sizes[0]];
ifs.seekg(blpheader.mipmaps_offsets[0], std::ios::beg);
ifs.read((char*)blpimage, blpheader.mipmaps_sizes[0]);
ifs.close();
//Decompress it
squish::u8* uncompressed;
if (blpheader.compression == 2) {
int dxtver;
switch (blpheader.alpha_encoding) {
case 0: dxtver = squish::kDxt1; break;
case 1: dxtver = squish::kDxt3; break;
case 7: dxtver = squish::kDxt5; break;
default: std::cout << "DXT version not recognized.\n"; return false; break;
}
uncompressed = new squish::u8[blpheader.width * blpheader.height * 4];
squish::DecompressImage(uncompressed, blpheader.width, blpheader.height, blpimage, dxtver);
} else
uncompressed = blpimage;
//Convert and save it
Magick::Blob blob(uncompressed, blpheader.width * blpheader.height * 4);
Magick::Image converted;
converted.size("256x256");
converted.magick("RGBA");
converted.depth(8);
converted.read(blob);
converted.write(_blpfile.substr(0, _blpfile.find_last_of('.')+1)+_extension);
delete[] blpimage;
if (blpheader.compression == 2)
delete[] uncompressed;
}
int main() {
BLP2_Convert("AzjolNerub3_2.blp", "jpg");
BLP2_Convert("AzjolNerub3_2.blp", "png");
return 0;
}