浏览代码

Re added unicode filename support for stb_image and stb_image_write with whitespace issues fixed.

JR 7 年之前
父节点
当前提交
d0ae424061
共有 2 个文件被更改,包括 60 次插入0 次删除
  1. 28 0
      stb_image.h
  2. 32 0
      stb_image_write.h

+ 28 - 0
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

+ 32 - 0
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;