From 7d36e7f4d73d186ee13105ae7506dfb5a1d8d3d2 Mon Sep 17 00:00:00 2001 From: Par Winzell Date: Mon, 27 Aug 2018 15:48:33 -0700 Subject: [PATCH] Rearrange, extract, clean up. Hopefully without unintentional changes to functionality. This renames header files to .hpp, imposes a gltf/raw/fbx directory structure, extracts standalone chunks of Fbx2Raw into distinct files, and undoes some particularly egregious mistakes from when I knew even less C++ than I do now. This is in anticipation of implementing 3ds Max's "Physical Material". --- src/utils/File_Utils.cpp | 4 +- src/utils/Image_Utils.cpp | 77 ++++++++++++++++++++++++-------------- src/utils/String_Utils.cpp | 46 ++++++++++++++++++++++- 3 files changed, 97 insertions(+), 30 deletions(-) diff --git a/src/utils/File_Utils.cpp b/src/utils/File_Utils.cpp index 30ad6bf..9de2c5c 100644 --- a/src/utils/File_Utils.cpp +++ b/src/utils/File_Utils.cpp @@ -7,6 +7,8 @@ * of patent rights can be found in the PATENTS file in the same directory. */ +#include "File_Utils.hpp" + #include #include #include @@ -35,7 +37,7 @@ #include #include "FBX2glTF.h" -#include "String_Utils.h" +#include "String_Utils.hpp" namespace FileUtils { diff --git a/src/utils/Image_Utils.cpp b/src/utils/Image_Utils.cpp index 9faf411..cac8e6f 100644 --- a/src/utils/Image_Utils.cpp +++ b/src/utils/Image_Utils.cpp @@ -7,50 +7,71 @@ * of patent rights can be found in the PATENTS file in the same directory. */ +#include "Image_Utils.hpp" + #include +#include #define STB_IMAGE_IMPLEMENTATION + #include + #define STB_IMAGE_WRITE_IMPLEMENTATION + #include -#include "Image_Utils.h" +namespace ImageUtils { -static bool imageHasTransparentPixels(FILE *f) { - int width, height, channels; - // RGBA: we have to load the pixels to figure out if the image is fully opaque - uint8_t *pixels = stbi_load_from_file(f, &width, &height, &channels, 0); - if (pixels != nullptr) { - int pixelCount = width * height; - for (int ix = 0; ix < pixelCount; ix ++) { - // test fourth byte (alpha); 255 is 1.0 - if (pixels[4*ix + 3] != 255) { - return true; + static bool imageHasTransparentPixels(FILE *f) + { + int width, height, channels; + // RGBA: we have to load the pixels to figure out if the image is fully opaque + uint8_t *pixels = stbi_load_from_file(f, &width, &height, &channels, 0); + if (pixels != nullptr) { + int pixelCount = width * height; + for (int ix = 0; ix < pixelCount; ix++) { + // test fourth byte (alpha); 255 is 1.0 + if (pixels[4 * ix + 3] != 255) { + return true; + } } } + return false; } - return false; -} -ImageProperties GetImageProperties(char const *filePath) -{ - ImageProperties result = { - 1, - 1, - IMAGE_OPAQUE, - }; + ImageProperties GetImageProperties(char const *filePath) + { + ImageProperties result = { + 1, + 1, + IMAGE_OPAQUE, + }; - FILE *f = fopen(filePath, "rb"); - if (f == nullptr) { + FILE *f = fopen(filePath, "rb"); + if (f == nullptr) { + return result; + } + + int channels; + int success = stbi_info_from_file(f, &result.width, &result.height, &channels); + + if (success && channels == 4 && imageHasTransparentPixels(f)) { + result.occlusion = IMAGE_TRANSPARENT; + } return result; } - int channels; - int success = stbi_info_from_file(f, &result.width, &result.height, &channels); + std::string suffixToMimeType(std::string suffix) + { + std::transform(suffix.begin(), suffix.end(), suffix.begin(), ::tolower); - if (success && channels == 4 && imageHasTransparentPixels(f)) { - result.occlusion = IMAGE_TRANSPARENT; + if (suffix == "jpg" || suffix == "jpeg") { + return "image/jpeg"; + } + if (suffix == "png") { + return "image/png"; + } + return "image/unknown"; } - return result; -} +} diff --git a/src/utils/String_Utils.cpp b/src/utils/String_Utils.cpp index 4fb0712..4746402 100644 --- a/src/utils/String_Utils.cpp +++ b/src/utils/String_Utils.cpp @@ -7,7 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. */ -#include "String_Utils.h" +#include "String_Utils.hpp" namespace StringUtils { @@ -16,4 +16,48 @@ namespace StringUtils { return (s == PATH_WIN) ? PATH_UNIX : PATH_WIN; } + const std::string GetFolderString(const std::string &path) + { + size_t s = path.rfind(PATH_WIN); + s = (s != std::string::npos) ? s : path.rfind(PATH_UNIX); + return path.substr(0, s + 1); + } + + const std::string GetCleanPathString(const std::string &path, const PathSeparator separator) + { + std::string cleanPath = path; + for (size_t s = cleanPath.find(!separator, 0); s != std::string::npos; s = cleanPath.find(!separator, s)) { + cleanPath[s] = separator; + } + return cleanPath; + } + + const std::string GetFileNameString(const std::string &path) + { + size_t s = path.rfind(PATH_WIN); + s = (s != std::string::npos) ? s : path.rfind(PATH_UNIX); + return path.substr(s + 1, std::string::npos); + } + + const std::string GetFileBaseString(const std::string &path) + { + const std::string fileName = GetFileNameString(path); + return fileName.substr(0, fileName.rfind('.')).c_str(); + } + + const std::string GetFileSuffixString(const std::string &path) + { + const std::string fileName = GetFileNameString(path); + size_t pos = fileName.rfind('.'); + if (pos == std::string::npos) { + return ""; + } + return fileName.substr(++pos); + } + + int CompareNoCase(const std::string &s1, const std::string &s2) + { + return strncasecmp(s1.c_str(), s2.c_str(), MAX_PATH_LENGTH); + } + }