From d0ae4240610a63bf8367a19b59142ea6e28ab7d3 Mon Sep 17 00:00:00 2001 From: JR Date: Tue, 20 Feb 2018 21:38:00 -0500 Subject: [PATCH] Re added unicode filename support for stb_image and stb_image_write with whitespace issues fixed. --- stb_image.h | 28 ++++++++++++++++++++++++++++ stb_image_write.h | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/stb_image.h b/stb_image.h index d9c21bc..67681a7 100644 --- a/stb_image.h +++ b/stb_image.h @@ -1143,12 +1143,40 @@ static void stbi__float_postprocess(float *result, int *x, int *y, int *comp, in #ifndef STBI_NO_STDIO +char* stbi_convert_wchar_to_utf8(wchar_t* input) { +#ifdef _WINDOWS_ + int outputSizeNeeded = WideCharToMultiByte(CP_UTF8, 0, &input[0], wcslen(input), NULL, 0, NULL, NULL); + char* temp = (char*)STBI_MALLOC(outputSizeNeeded); + int error = WideCharToMultiByte(65001, 0, input, -1, temp, outputSizeNeeded, NULL, NULL); + temp[outputSizeNeeded] = '\0'; + return temp; +#else + return NULL; + #endif +} + static FILE *stbi__fopen(char const *filename, char const *mode) { FILE *f; #if defined(_MSC_VER) && _MSC_VER >= 1400 +#ifdef UNICODE + int filenameLength = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0); + wchar_t* wFilename = (wchar_t*)stbi__malloc(filenameLength * sizeof(wchar_t)); + MultiByteToWideChar(CP_UTF8, 0, filename, -1, wFilename, filenameLength); + + int modeLength = MultiByteToWideChar(CP_UTF8, 0, mode, -1, NULL, 0); + wchar_t* wMode = (wchar_t*)stbi__malloc(modeLength * sizeof(wchar_t)); + MultiByteToWideChar(CP_UTF8, 0, mode, -1, wMode, modeLength); + + if (0 != _wfopen_s(&f, wFilename, wMode)) + f = 0; + + STBI_FREE(wFilename); + STBI_FREE(wMode); +#else if (0 != fopen_s(&f, filename, mode)) f=0; +#endif #else f = fopen(filename, mode); #endif diff --git a/stb_image_write.h b/stb_image_write.h index c05e958..4c41b1f 100644 --- a/stb_image_write.h +++ b/stb_image_write.h @@ -279,8 +279,17 @@ static int stbi__start_write_file(stbi__write_context *s, const char *filename) { FILE *f; #ifdef STBI_MSC_SECURE_CRT +#ifdef UNICODE + int filenameLength = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0); + wchar_t* wFilename = (wchar_t*)STBIW_MALLOC(filenameLength * sizeof(wchar_t)); + MultiByteToWideChar(CP_UTF8, 0, filename, -1, wFilename, filenameLength); + + if (0 != _wfopen_s(&f, wFilename, L"wb")) + f = NULL; +#else if (fopen_s(&f, filename, "wb")) f = NULL; +#endif #else f = fopen(filename, "wb"); #endif @@ -1112,8 +1121,19 @@ STBIWDEF int stbi_write_png(char const *filename, int x, int y, int comp, const unsigned char *png = stbi_write_png_to_mem((unsigned char *) data, stride_bytes, x, y, comp, &len); if (png == NULL) return 0; #ifdef STBI_MSC_SECURE_CRT +#ifdef UNICODE + int filenameLength = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0); + wchar_t* wFilename = (wchar_t*)STBIW_MALLOC(filenameLength * sizeof(wchar_t)); + MultiByteToWideChar(CP_UTF8, 0, filename, -1, wFilename, filenameLength); + + if (0 != _wfopen_s(&f, wFilename, L"wb")) + f = NULL; + + STBIW_FREE(wFilename); +#else if (fopen_s(&f, filename, "wb")) f = NULL; +#endif #else f = fopen(filename, "wb"); #endif @@ -1125,6 +1145,18 @@ STBIWDEF int stbi_write_png(char const *filename, int x, int y, int comp, const } #endif +char* stbiw_convert_wchar_to_utf8(wchar_t* input) { +#ifdef _WINDOWS_ + int outputSizeNeeded = WideCharToMultiByte(CP_UTF8, 0, &input[0], wcslen(input), NULL, 0, NULL, NULL); + char* temp = (char*)STBIW_MALLOC(outputSizeNeeded); + int error = WideCharToMultiByte(65001, 0, input, -1, temp, outputSizeNeeded, NULL, NULL); + temp[outputSizeNeeded] = '\0'; + return temp; +#else + return NULL; +#endif +} + STBIWDEF int stbi_write_png_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int stride_bytes) { int len;