102 lines
2.2 KiB
C
102 lines
2.2 KiB
C
/**
|
|
* Copyright (c) 2021 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#pragma once
|
|
#include "../utils/file.h"
|
|
#ifndef STB_IMAGE_IMPLEMENTATION
|
|
#define STB_IMAGE_IMPLEMENTATION
|
|
#include <stb_image.h>
|
|
#endif
|
|
#ifndef STB_IMAGE_RESIZE_IMPLEMENTATION
|
|
#define STB_IMAGE_RESIZE_IMPLEMENTATION
|
|
#include <stb_image_resize.h>
|
|
#endif
|
|
|
|
#define RESIZE_VARIANT_COUNT 4
|
|
int RESIZE_SCALES[RESIZE_VARIANT_COUNT] = { 1, 2, 3, 4 };
|
|
|
|
int main(int argc, char *argv[]) {
|
|
FILE *file;
|
|
char path[FILENAME_MAX + 1];
|
|
char *in;
|
|
char *out;
|
|
char pathSep;
|
|
int w, h, channels, rw, rh, i, scale;
|
|
stbi_uc *dataOriginal;
|
|
stbi_uc *dataResized;
|
|
|
|
if(argc != 3) {
|
|
printf("Invalid number of arguments\n");
|
|
return 1;
|
|
}
|
|
|
|
// Set up strings
|
|
pathSep = FILE_PATH_SEP;
|
|
in = argv[1];
|
|
out = argv[2];
|
|
|
|
// Normalize slashes
|
|
fileNormalizeSlashes(in);
|
|
fileNormalizeSlashes(out);
|
|
|
|
// Read in original texture
|
|
file = fopen(in, "rb");
|
|
if(file == NULL) {
|
|
printf("Failed to open file!\n");
|
|
return 1;
|
|
}
|
|
|
|
dataOriginal = stbi_load_from_file(file, &w, &h, &channels, STBI_rgb_alpha);
|
|
if(dataOriginal == NULL) {
|
|
printf("Failed to load input texture!\n");
|
|
return 1;
|
|
}
|
|
fclose(file);
|
|
|
|
// For each scale.
|
|
for(i = 0; i < RESIZE_VARIANT_COUNT; i++) {
|
|
// Resize image
|
|
scale = RESIZE_SCALES[i];
|
|
rw = w / scale;
|
|
rh = h / scale;
|
|
dataResized = malloc(sizeof(stbi_uc) * rw * rh * channels);
|
|
stbir_resize_uint8(dataOriginal, w, h, 0, dataResized, rw, rh, 0, channels);
|
|
|
|
// Determine output path
|
|
path[0] = '\0';
|
|
if(getcwd(path,sizeof(path)) == NULL) {
|
|
printf("Failed to get current dir!\n");
|
|
stbi_image_free(dataOriginal);
|
|
return 1;
|
|
}
|
|
|
|
|
|
// Determine Output path
|
|
sprintf(path, "%s%c%s_%i.texture", path, FILE_PATH_SEP, out, scale);
|
|
printf("Writing %s\n", path);
|
|
|
|
// Open output file
|
|
fileMkdirp(path);
|
|
file = fopen(path, "wb");
|
|
if(file == NULL) {
|
|
printf("Invalid file out!\n");
|
|
return 1;
|
|
}
|
|
|
|
// Write texture
|
|
fwrite(dataResized, sizeof(unsigned char), rw * rh * channels, file);
|
|
|
|
// Cleanup
|
|
fclose(file);
|
|
free(dataResized);
|
|
}
|
|
|
|
// Cleanup
|
|
stbi_image_free(dataOriginal);
|
|
|
|
return 0;
|
|
} |