My friend gave me the code that I modified to work in the console, but ran into a problem when I needed to add a new flag. It was needed to save the original Mip-Maps. I mean, I have a dds texture with modified Mip-Maps and I need to save it with a script saving the original Mip-Maps, and not generate new ones. By creating different Mip-Maps, you can achieve a special visual effect. I did not see in the documentation that there was such a possibility at all, can you help in solving such a situation, if necessary, I can send all the files. The code is attached below.
#include "NVTT/nvtt.h"
#include <stdio.h>
#include <iostream>
#include <filesystem>
#include <cstring>
#include <regex>
using namespace std;
using namespace nvtt;
namespace fs = filesystem;
#pragma comment(lib,"NVTT/nvtt")
bool peresvet = false;
//bool saveMipmaps = false;
void nvttBC3(const char* input, const char* output, bool srgb = false)
{
Surface image;
OutputOptions outputOptions;
Context context;
CompressionOptions compressionOptions;
bool s = true;
image.load(input, &s);
compressionOptions.setFormat(Format_BC3);
outputOptions.setContainer(Container_DDS10);
outputOptions.setFileName(output);
const int numMipmaps = image.countMipmaps() - 1;
if (srgb) {
if (peresvet)
image.toSrgb();
outputOptions.setSrgbFlag(true);
}
if (!context.outputHeader(image, numMipmaps, compressionOptions, outputOptions)) {
cerr << "[error] Writing the DDS header failed!\n";
return;
}
for (int mip = 0; mip <= numMipmaps; mip++) {
if (!context.compress(image, 0, mip, compressionOptions, outputOptions)) {
cerr << "[error] Compressing and writing the DDS file failed!\n";
return;
}
if (mip == numMipmaps) break;
//if (mip == numMipmaps || saveMipmaps) break;
image.buildNextMipmap(MipmapFilter_Box);
}
}
int main(int argc, char** argv)
{
vector<string> args(argv, argv + argc);
string inputFilePath;
string outputFilePath;
string action;
for (size_t i = 1; i < args.size(); ++i) {
if (args[i] == "-i" && i + 1 < args.size()) {
inputFilePath = args[i + 1];
i++;
}
else if (args[i] == "-a" && i + 1 < args.size()) {
action = args[i + 1];
i++;
}
else if (args[i] == "-o" && i + 1 < args.size()) {
outputFilePath = args[i + 1];
i++;
}
//else if (args[i] == "-sm" && i + 1 < args.size()) {
// saveMipmaps = (args[i + 1] == "true");
// i++;
//}
}
if (inputFilePath.empty() || action.empty() || outputFilePath.empty()) {
cerr << "Usage: NVTTConverter.exe -i <input_file> -a <action> -o <output_file>" << endl;
cerr << " -i <input_file> - Source file in TGA format." << endl;
cerr << " -a <action> - The action to use. (\"DT - Default texture\" or \"BC - Base Color\")" << endl;
cerr << " -o <output_file> - Where to save a file in DDS format." << endl;
//cerr << " -sm <save_mipmaps> - Whether to save the original mipmaps (\"true\" or \"false\")." << endl;
return 1;
}
if (!fs::exists(inputFilePath)) {
cerr << "[error] Input file does not exist: " << inputFilePath << endl;
return 1;
}
fs::path outputPath(outputFilePath);
fs::path outputDir = outputPath.parent_path();
if (!fs::exists(outputDir)) {
cerr << "[error] Output directory does not exist: " << outputDir.string() << endl;
return 1;
}
bool isBC = false;
bool needSwap = false;
if (action == "DT") {
needSwap = true;
}
else if (action == "BC") {
isBC = true;
}
else {
cerr << "[error] Unknown action: " << action << endl;
return 1;
}
nvttBC3(inputFilePath.c_str(), outputFilePath.c_str(), isBC);
cout << "[nvtt] Conversion complete!" << endl;
return 0;
}