87 lines
1.9 KiB
C++

/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "dawnsharedlibs.hpp"
#include "../../util/file.hpp"
#include "../../util/image.hpp"
int main(int argc, char *argv[]) {
FILE *file;
char path[FILENAME_MAX + 1];
uint8_t headerBuffer[32];
char *in;
char *out;
int w, h, channels, headerBufferLength;
stbi_uc *dataImageRaw, dataIn;
float *dataImage;
size_t i, len;
if(argc != 3) {
printf("Invalid number of arguments\n");
return 1;
}
// Set up strings
in = argv[1];
out = argv[2];
// Normalize slashes
fileNormalizeSlashes(in);
fileNormalizeSlashes(out);
// Check the output doesn't already exist
sprintf(path, "%s.texture", out);
file = fopen(path, "rb");
if(file != NULL) {
fclose(file);
return 0;
}
// Read in original texture
file = fopen(in, "rb");
if(file == NULL) {
printf("Failed to open file!\n");
return 1;
}
dataImageRaw = stbi_load_from_file(file, &w, &h, &channels, STBI_rgb_alpha);
if(dataImageRaw == NULL) {
printf("Failed to load input texture!\n");
return 1;
}
fclose(file);
// Convert to floating points
len = STBI_rgb_alpha * w * h;
dataImage = (float *)malloc(sizeof(float) * len);
for(i = 0; i < len; i++) {
dataIn = dataImageRaw[i];
dataImage[i] = ((float)dataIn) / 255.0f;
}
stbi_image_free(dataImageRaw);
// Open output file
fileMkdirp(path);
file = fopen(path, "wb");
if(file == NULL) {
printf("Invalid texture file out!\n");
return 1;
}
// Write info
headerBufferLength = sprintf((char *)headerBuffer, "%i|%i|", w, h);
fwrite(headerBuffer, sizeof(uint8_t), headerBufferLength, file);
// Write texture
fwrite(dataImage, sizeof(float), len, file);
// Cleanup
fclose(file);
free(dataImage);
return 0;
}