67 lines
1.8 KiB
C++
67 lines
1.8 KiB
C++
// Copyright (c) 2023 Dominic Masters
|
|
//
|
|
// This software is released under the MIT License.
|
|
// https://opensource.org/licenses/MIT
|
|
|
|
#include "TextureGen.hpp"
|
|
|
|
using namespace Dawn;
|
|
|
|
std::vector<std::string> TextureGen::getRequiredFlags() {
|
|
return std::vector<std::string>{ "input", "output" };
|
|
}
|
|
|
|
int32_t TextureGen::start() {
|
|
// Finished with XML data, now we can write data out.
|
|
File fileOut(flags["output"] + ".texture");
|
|
if(fileOut.exists()) return 0;
|
|
|
|
// Load input file
|
|
File in(flags["input"]);
|
|
if(!in.open(FILE_MODE_READ)) return 1;
|
|
|
|
int w, h, channels;
|
|
auto imageRaw = stbi_load_from_file(in.file, &w, &h, &channels, STBI_rgb_alpha);
|
|
if(imageRaw == NULL) {
|
|
std::cout << "Failed to load input texture!" << std::endl;
|
|
return 1;
|
|
}
|
|
in.close();
|
|
|
|
// Convert to floating points
|
|
size_t len = STBI_rgb_alpha * w * h;
|
|
float_t *dataImage = (float_t*)malloc(sizeof(float) * len);
|
|
for(size_t i = 0; i < len; i++) {
|
|
auto dataIn = imageRaw[i];
|
|
dataImage[i] = ((float_t)dataIn) / 255.0f;
|
|
}
|
|
stbi_image_free(imageRaw);
|
|
|
|
// Open and create output
|
|
File out(flags["output"] + ".texture");
|
|
if(!out.mkdirp()) {
|
|
std::cout << "Failed to make output dir " << out.filename << std::endl;
|
|
return 1;
|
|
}
|
|
if(!out.open(FILE_MODE_WRITE)) {
|
|
std::cout << "Failed to open texture file for writing " << out.filename << std::endl;
|
|
return 1;
|
|
}
|
|
|
|
// Write info
|
|
char headerBuffer[64];
|
|
size_t headerBufferLength = sprintf((char *)headerBuffer, "%i|%i|", w, h);
|
|
if(!out.writeRaw(headerBuffer, headerBufferLength)) {
|
|
std::cout << "Failed to write texture header for " << out.filename << std::endl;
|
|
return 1;
|
|
}
|
|
|
|
// Write texture
|
|
if(!out.writeRaw((char *)dataImage, sizeof(float_t) * len)) {
|
|
std::cout << "Failed to write texture data for " << out.filename << std::endl;
|
|
return 1;
|
|
}
|
|
free(dataImage);
|
|
|
|
return 0;
|
|
} |