/** * Copyright (c) 2021 Dominic Masters * * This software is released under the MIT License. * https://opensource.org/licenses/MIT */ #pragma once #include #include #include #include #if defined(_MSC_VER) #include #define getcwd _getcwd #define FILE_PATH_SEP '\\' #elif defined(__GNUC__) #include #define FILE_PATH_SEP '/' #endif #ifndef STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION #include #endif // void mkdirp(const char *dir) { // char tmp[FILENAME_MAX + 1]; // char *p = NULL; // size_t len; // // Copy the input to the output // snprintf(tmp, sizeof(tmp), "%s", dir); // len = strlen(tmp);// Determine string length // // Does the string end with a directory terminator? // if(tmp[len - 1] == '/' || tmp[len - 1] == '\\') { // tmp[len - 1] = 0; // } // // Foreach character, take the value from TMP as I go. // for(p = tmp + 1; *p; p++) { // if(*p == '/' || *p == '\\') { // *p = 0; // _mkdir(tmp); // *p = '/'; // } // } // _mkdir(tmp); // } int main(int argc, char *argv[]) { FILE *file; char path[FILENAME_MAX + 1]; char *in; char *out; char pathSep; int x, y, channels; stbi_uc *data; if(argc != 3) { printf("Invalid number of arguments\n"); return 1; } pathSep = FILE_PATH_SEP; in = argv[1]; out = argv[2]; // Normalize slashes fileNormalizeSlashes(in); fileNormalizeSlashes(out); // Read in original texture printf("Opening %s\n", in); file = fopen(in, "rb"); if(file == NULL) { printf("Failed to open file!\n"); return 1; } data = stbi_load_from_file(file, &x, &y, &channels, STBI_rgb_alpha); if(data == NULL) { printf("Failed to load input texture!\n"); return 1; } // Write out texture path[0] = '\0'; if(getcwd(path,sizeof(path)) == NULL) { printf("Failed to get current dir!\n"); stbi_image_free(data); return 1; } // Determine Output path strncat(path, &pathSep, 1); strcat(path, out); 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 printf("Size %i x %i @ %i\n", x, y, channels); fwrite(data, sizeof(unsigned char), x*y*channels, file); // Cleanup fclose(file); stbi_image_free(data); return 0; }