Re added unicode filename support for stb_image and stb_image_write with whitespace issues fixed.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by JR · committed by JR · parent e6afb9c · Browse files · 2 files·+60 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+60 added−0 removed
- other:+60 added−0 removed
Provenance
- Commit
d0ae424- Tree
7355c41- Author
JR <jrsmith17@gmail.com>- Committer
JR <jrsmith17@gmail.com>- Parent
e6afb9c
2 files changed+60 added−0 removed
modified:stb_image_write.h+32 added
277 unmodified lines278 static int stbi__start_write_file(stbi__write_context *s, const char *filename)279 {280 FILE *f;281 #ifdef STBI_MSC_SECURE_CRT282+#ifdef UNICODE283+ int filenameLength = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);284+ wchar_t* wFilename = (wchar_t*)STBIW_MALLOC(filenameLength * sizeof(wchar_t));285+ MultiByteToWideChar(CP_UTF8, 0, filename, -1, wFilename, filenameLength);286+ 287+ if (0 != _wfopen_s(&f, wFilename, L"wb"))288+ f = NULL;289+#else290 if (fopen_s(&f, filename, "wb"))291 f = NULL;292+#endif293 #else294 f = fopen(filename, "wb");295 #endif296 stbi__start_write_callbacks(s, stbi__stdio_write, (void *) f);823 unmodified lines1120 int len;1121 unsigned char *png = stbi_write_png_to_mem((unsigned char *) data, stride_bytes, x, y, comp, &len);1122 if (png == NULL) return 0;1123 #ifdef STBI_MSC_SECURE_CRT1124+#ifdef UNICODE1125+ int filenameLength = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);1126+ wchar_t* wFilename = (wchar_t*)STBIW_MALLOC(filenameLength * sizeof(wchar_t));1127+ MultiByteToWideChar(CP_UTF8, 0, filename, -1, wFilename, filenameLength);1128+ 1129+ if (0 != _wfopen_s(&f, wFilename, L"wb"))1130+ f = NULL;1131+ 1132+ STBIW_FREE(wFilename);1133+#else1134 if (fopen_s(&f, filename, "wb"))1135 f = NULL;1136+#endif1137 #else1138 f = fopen(filename, "wb");1139 #endif1140 if (!f) { STBIW_FREE(png); return 0; }3 unmodified lines1144 return 1;1145 }1146 #endif1147 1148+char* stbiw_convert_wchar_to_utf8(wchar_t* input) {1149+#ifdef _WINDOWS_1150+ int outputSizeNeeded = WideCharToMultiByte(CP_UTF8, 0, &input[0], wcslen(input), NULL, 0, NULL, NULL);1151+ char* temp = (char*)STBIW_MALLOC(outputSizeNeeded);1152+ int error = WideCharToMultiByte(65001, 0, input, -1, temp, outputSizeNeeded, NULL, NULL);1153+ temp[outputSizeNeeded] = '\0';1154+ return temp;1155+#else1156+ return NULL;1157+#endif1158+}1159+1160 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)1161 {1162 int len;1163 unsigned char *png = stbi_write_png_to_mem((unsigned char *) data, stride_bytes, x, y, comp, &len);437 unmodified linesmodified:stb_image.h+28 added
1141 unmodified lines1142 #endif1143 1144 #ifndef STBI_NO_STDIO1145 1146+char* stbi_convert_wchar_to_utf8(wchar_t* input) {1147+#ifdef _WINDOWS_1148+ int outputSizeNeeded = WideCharToMultiByte(CP_UTF8, 0, &input[0], wcslen(input), NULL, 0, NULL, NULL);1149+ char* temp = (char*)STBI_MALLOC(outputSizeNeeded);1150+ int error = WideCharToMultiByte(65001, 0, input, -1, temp, outputSizeNeeded, NULL, NULL);1151+ temp[outputSizeNeeded] = '\0';1152+ return temp;1153+#else1154+ return NULL;1155+ #endif1156+}1157+1158 static FILE *stbi__fopen(char const *filename, char const *mode)1159 {1160 FILE *f;1161 #if defined(_MSC_VER) && _MSC_VER >= 14001162+#ifdef UNICODE1163+ int filenameLength = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);1164+ wchar_t* wFilename = (wchar_t*)stbi__malloc(filenameLength * sizeof(wchar_t));1165+ MultiByteToWideChar(CP_UTF8, 0, filename, -1, wFilename, filenameLength);1166+ 1167+ int modeLength = MultiByteToWideChar(CP_UTF8, 0, mode, -1, NULL, 0);1168+ wchar_t* wMode = (wchar_t*)stbi__malloc(modeLength * sizeof(wchar_t));1169+ MultiByteToWideChar(CP_UTF8, 0, mode, -1, wMode, modeLength);1170+1171+ if (0 != _wfopen_s(&f, wFilename, wMode))1172+ f = 0;1173+ 1174+ STBI_FREE(wFilename);1175+ STBI_FREE(wMode);1176+#else1177 if (0 != fopen_s(&f, filename, mode))1178 f=0;1179+#endif1180 #else1181 f = fopen(filename, mode);1182 #endif1183 return f;6307 unmodified lines