@@ -46,6 +46,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46
46
#include " ProcessHelper.h"
47
47
48
48
#include < fstream>
49
+ #include < algorithm>
49
50
50
51
using namespace Assimp ;
51
52
@@ -91,25 +92,47 @@ void EmbedTexturesProcess::Execute(aiScene* pScene) {
91
92
ASSIMP_LOG_INFO (" EmbedTexturesProcess finished. Embedded " , embeddedTexturesCount, " textures." );
92
93
}
93
94
95
+ std::string EmbedTexturesProcess::tryToFindValidPath (const std::string &imagePath) const
96
+ {
97
+ // Test path directly
98
+ if (mIOHandler ->Exists (imagePath)) {
99
+ return imagePath;
100
+ }
101
+ ASSIMP_LOG_WARN (" EmbedTexturesProcess: Cannot find image: " , imagePath, " . Will try to find it in root folder." );
102
+
103
+ // Test path in root path
104
+ std::string testPath = mRootPath + imagePath;
105
+ if (mIOHandler ->Exists (testPath)) {
106
+ return testPath;
107
+ }
108
+
109
+ // Test path basename in root path
110
+ testPath = mRootPath + imagePath.substr (imagePath.find_last_of (" \\ /" ) + 1u );
111
+ if (mIOHandler ->Exists (testPath)) {
112
+ return testPath;
113
+ }
114
+
115
+ // In unix systems, '\' is a valid file name character, but some files may use \ as a directory separator.
116
+ // Try replacing '\' by '/'.
117
+ if (mIOHandler ->getOsSeparator () != ' \\ ' && imagePath.find (' \\ ' ) != std::string::npos) {
118
+ ASSIMP_LOG_WARN (" EmbedTexturesProcess: Cannot find image '" , imagePath, " ' in root folder. Will try replacing directory separators." );
119
+ testPath = imagePath;
120
+ std::replace (testPath.begin (), testPath.end (), ' \\ ' , mIOHandler ->getOsSeparator ());
121
+ return tryToFindValidPath (testPath);
122
+ }
123
+
124
+ ASSIMP_LOG_ERROR (" EmbedTexturesProcess: Unable to embed texture: " , imagePath, " ." );
125
+ return {};
126
+ }
127
+
94
128
bool EmbedTexturesProcess::addTexture (aiScene *pScene, const std::string &path) const {
95
129
std::streampos imageSize = 0 ;
96
- std::string imagePath = path;
130
+ std::string imagePath = tryToFindValidPath ( path) ;
97
131
98
- // Test path directly
99
- if (!mIOHandler ->Exists (imagePath)) {
100
- ASSIMP_LOG_WARN (" EmbedTexturesProcess: Cannot find image: " , imagePath, " . Will try to find it in root folder." );
101
-
102
- // Test path in root path
103
- imagePath = mRootPath + path;
104
- if (!mIOHandler ->Exists (imagePath)) {
105
- // Test path basename in root path
106
- imagePath = mRootPath + path.substr (path.find_last_of (" \\ /" ) + 1u );
107
- if (!mIOHandler ->Exists (imagePath)) {
108
- ASSIMP_LOG_ERROR (" EmbedTexturesProcess: Unable to embed texture: " , path, " ." );
109
- return false ;
110
- }
111
- }
132
+ if (imagePath.empty ()) {
133
+ return false ;
112
134
}
135
+
113
136
IOStream* pFile = mIOHandler ->Open (imagePath);
114
137
if (pFile == nullptr ) {
115
138
ASSIMP_LOG_ERROR (" EmbedTexturesProcess: Unable to embed texture: " , path, " ." );
0 commit comments