ソースを参照

Clean up Image::SavePNG() to use just Image::Save(). Fix jo & stbi_image_write to use wchar paths on Windows.

Lasse Öörni 9 年 前
コミット
d09f67f249

+ 16 - 0
Source/ThirdParty/JO/jo_jpeg.cpp

@@ -19,6 +19,8 @@
  * 	
  * */
 
+// Modified by Lasse Oorni for Urho3D
+
 #ifndef JO_JPEG_HEADER_FILE_ONLY
 
 #if defined(_MSC_VER) && _MSC_VER >= 0x1400
@@ -29,6 +31,11 @@
 #include <stdlib.h>
 #include <math.h>
 
+// Urho3D: for MultiByteToWideChar
+#ifdef _WIN32
+#include <windows.h>
+#endif
+
 static const unsigned char s_jo_ZigZag[] = { 0,1,5,6,14,15,27,28,2,4,7,13,16,26,29,42,3,8,12,17,25,30,41,43,9,11,18,24,31,40,44,53,10,19,23,32,39,45,52,54,20,22,33,38,46,51,55,60,21,34,37,47,50,56,59,61,35,36,48,49,57,58,62,63 };
 
 static void jo_writeBits(FILE *fp, int &bitBuf, int &bitCnt, const unsigned short *bs) {
@@ -231,7 +238,16 @@ bool jo_write_jpg(const char *filename, const void *data, int width, int height,
 		return false;
 	}
 
+	// Urho3D: proper UTF8 handling for Windows
+#ifndef _WIN32
 	FILE *fp = fopen(filename, "wb");
+#else
+	int wcharsize = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);
+	wchar_t* wstr = new wchar_t[wcharsize];
+	MultiByteToWideChar(CP_UTF8, 0, filename, -1, wstr, wcharsize);
+	FILE *fp = _wfopen(wstr, L"wb");
+	delete[] wstr;
+#endif
 	if(!fp) {
 		return false;
 	}

+ 8 - 0
Source/ThirdParty/STB/stb_image_write.h

@@ -111,6 +111,8 @@ publish, and distribute this file as you see fit.
 
 */
 
+// Modified by Lasse Oorni for Urho3D
+
 #ifndef INCLUDE_STB_IMAGE_WRITE_H
 #define INCLUDE_STB_IMAGE_WRITE_H
 
@@ -218,7 +220,13 @@ static void stbi__stdio_write(void *context, void *data, int size)
 
 static int stbi__start_write_file(stbi__write_context *s, const char *filename)
 {
+   // Urho3D: proper UTF8 handling for Windows, requires Urho3D WString class
+#ifndef _WIN32
    FILE *f = fopen(filename, "wb");
+#else
+    Urho3D::WString wstr(filename);
+    FILE *f = _wfopen(wstr.CString(), L"wb");
+#endif
    stbi__start_write_callbacks(s, stbi__stdio_write, (void *) f);
    return f != NULL;
 }

+ 3 - 33
Source/Urho3D/Resource/Image.cpp

@@ -1159,39 +1159,9 @@ bool Image::SavePNG(const String& fileName) const
 {
     URHO3D_PROFILE(SaveImagePNG);
 
-    FileSystem* fileSystem = GetSubsystem<FileSystem>();
-    if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
-    {
-        URHO3D_LOGERROR("Access denied to " + fileName);
-        return false;
-    }
-
-    if (IsCompressed())
-    {
-        URHO3D_LOGERROR("Can not save compressed image to PNG");
-        return false;
-    }
-
-    if (data_)
-    {
-        int len;
-        unsigned char* png =  stbi_write_png_to_mem(data_.Get(), 0, width_, height_, components_, &len);
-
-        if (png)
-        {
-            bool success = false;
-            File outFile(context_, fileName, FILE_WRITE);
-            if (outFile.IsOpen())
-                success = outFile.Write(png, len) == len;
-            STBIW_FREE(png);
-            return success;
-        }
-        else
-        {
-            URHO3D_LOGERROR("No data produced for image save to PNG");
-            return false;
-        }
-    }
+    File outFile(context_, fileName, FILE_WRITE);
+    if (outFile.IsOpen())
+        return Image::Save(outFile); // Save uses PNG format
     else
         return false;
 }