/src/assimp/code/PostProcessing/EmbedTexturesProcess.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | Open Asset Import Library (assimp) |
3 | | ---------------------------------------------------------------------- |
4 | | |
5 | | Copyright (c) 2006-2024, assimp team |
6 | | |
7 | | All rights reserved. |
8 | | |
9 | | Redistribution and use of this software in source and binary forms, |
10 | | with or without modification, are permitted provided that the |
11 | | following conditions are met: |
12 | | |
13 | | * Redistributions of source code must retain the above |
14 | | copyright notice, this list of conditions and the |
15 | | following disclaimer. |
16 | | |
17 | | * Redistributions in binary form must reproduce the above |
18 | | copyright notice, this list of conditions and the |
19 | | following disclaimer in the documentation and/or other |
20 | | materials provided with the distribution. |
21 | | |
22 | | * Neither the name of the assimp team, nor the names of its |
23 | | contributors may be used to endorse or promote products |
24 | | derived from this software without specific prior |
25 | | written permission of the assimp team. |
26 | | |
27 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
28 | | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
29 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
30 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
31 | | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
32 | | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
33 | | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
34 | | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
35 | | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
36 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
37 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
38 | | |
39 | | ---------------------------------------------------------------------- |
40 | | */ |
41 | | |
42 | | #include "EmbedTexturesProcess.h" |
43 | | #include <assimp/IOStream.hpp> |
44 | | #include <assimp/IOSystem.hpp> |
45 | | #include <assimp/ParsingUtils.h> |
46 | | #include "ProcessHelper.h" |
47 | | |
48 | | #include <fstream> |
49 | | |
50 | | using namespace Assimp; |
51 | | |
52 | 2 | bool EmbedTexturesProcess::IsActive(unsigned int pFlags) const { |
53 | 2 | return (pFlags & aiProcess_EmbedTextures) != 0; |
54 | 2 | } |
55 | | |
56 | 0 | void EmbedTexturesProcess::SetupProperties(const Importer* pImp) { |
57 | 0 | mRootPath = pImp->GetPropertyString("sourceFilePath"); |
58 | 0 | mRootPath = mRootPath.substr(0, mRootPath.find_last_of("\\/") + 1u); |
59 | 0 | mIOHandler = pImp->GetIOHandler(); |
60 | 0 | } |
61 | | |
62 | 0 | void EmbedTexturesProcess::Execute(aiScene* pScene) { |
63 | 0 | if (pScene == nullptr || pScene->mRootNode == nullptr || mIOHandler == nullptr){ |
64 | 0 | return; |
65 | 0 | } |
66 | | |
67 | 0 | aiString path; |
68 | 0 | uint32_t embeddedTexturesCount = 0u; |
69 | 0 | for (auto matId = 0u; matId < pScene->mNumMaterials; ++matId) { |
70 | 0 | auto material = pScene->mMaterials[matId]; |
71 | |
|
72 | 0 | for (auto ttId = 1u; ttId < AI_TEXTURE_TYPE_MAX; ++ttId) { |
73 | 0 | auto tt = static_cast<aiTextureType>(ttId); |
74 | 0 | auto texturesCount = material->GetTextureCount(tt); |
75 | |
|
76 | 0 | for (auto texId = 0u; texId < texturesCount; ++texId) { |
77 | 0 | material->GetTexture(tt, texId, &path); |
78 | 0 | if (path.data[0] == '*') continue; // Already embedded |
79 | | |
80 | | // Indeed embed |
81 | 0 | if (addTexture(pScene, path.data)) { |
82 | 0 | auto embeddedTextureId = pScene->mNumTextures - 1u; |
83 | 0 | path.length = ::ai_snprintf(path.data, 1024, "*%u", embeddedTextureId); |
84 | 0 | material->AddProperty(&path, AI_MATKEY_TEXTURE(tt, texId)); |
85 | 0 | embeddedTexturesCount++; |
86 | 0 | } |
87 | 0 | } |
88 | 0 | } |
89 | 0 | } |
90 | |
|
91 | 0 | ASSIMP_LOG_INFO("EmbedTexturesProcess finished. Embedded ", embeddedTexturesCount, " textures." ); |
92 | 0 | } |
93 | | |
94 | 0 | bool EmbedTexturesProcess::addTexture(aiScene *pScene, const std::string &path) const { |
95 | 0 | std::streampos imageSize = 0; |
96 | 0 | std::string imagePath = path; |
97 | | |
98 | | // Test path directly |
99 | 0 | if (!mIOHandler->Exists(imagePath)) { |
100 | 0 | ASSIMP_LOG_WARN("EmbedTexturesProcess: Cannot find image: ", imagePath, ". Will try to find it in root folder."); |
101 | | |
102 | | // Test path in root path |
103 | 0 | imagePath = mRootPath + path; |
104 | 0 | if (!mIOHandler->Exists(imagePath)) { |
105 | | // Test path basename in root path |
106 | 0 | imagePath = mRootPath + path.substr(path.find_last_of("\\/") + 1u); |
107 | 0 | if (!mIOHandler->Exists(imagePath)) { |
108 | 0 | ASSIMP_LOG_ERROR("EmbedTexturesProcess: Unable to embed texture: ", path, "."); |
109 | 0 | return false; |
110 | 0 | } |
111 | 0 | } |
112 | 0 | } |
113 | 0 | IOStream* pFile = mIOHandler->Open(imagePath); |
114 | 0 | if (pFile == nullptr) { |
115 | 0 | ASSIMP_LOG_ERROR("EmbedTexturesProcess: Unable to embed texture: ", path, "."); |
116 | 0 | return false; |
117 | 0 | } |
118 | 0 | imageSize = pFile->FileSize(); |
119 | |
|
120 | 0 | aiTexel* imageContent = new aiTexel[ 1ul + static_cast<unsigned long>( imageSize ) / sizeof(aiTexel)]; |
121 | 0 | pFile->Seek(0, aiOrigin_SET); |
122 | 0 | pFile->Read(reinterpret_cast<char*>(imageContent), static_cast<size_t>(imageSize), 1); |
123 | 0 | mIOHandler->Close(pFile); |
124 | | |
125 | | // Enlarging the textures table |
126 | 0 | unsigned int textureId = pScene->mNumTextures++; |
127 | 0 | auto oldTextures = pScene->mTextures; |
128 | 0 | pScene->mTextures = new aiTexture*[pScene->mNumTextures]; |
129 | 0 | ::memmove(pScene->mTextures, oldTextures, sizeof(aiTexture*) * (pScene->mNumTextures - 1u)); |
130 | 0 | delete [] oldTextures; |
131 | | |
132 | | // Add the new texture |
133 | 0 | auto pTexture = new aiTexture; |
134 | 0 | pTexture->mHeight = 0; // Means that this is still compressed |
135 | 0 | pTexture->mWidth = static_cast<uint32_t>(imageSize); |
136 | 0 | pTexture->pcData = imageContent; |
137 | |
|
138 | 0 | auto extension = path.substr(path.find_last_of('.') + 1u); |
139 | 0 | extension = ai_tolower(extension); |
140 | 0 | if (extension == "jpeg") { |
141 | 0 | extension = "jpg"; |
142 | 0 | } |
143 | |
|
144 | 0 | size_t len = extension.size(); |
145 | 0 | if (len > HINTMAXTEXTURELEN -1 ) { |
146 | 0 | len = HINTMAXTEXTURELEN - 1; |
147 | 0 | } |
148 | 0 | ::strncpy(pTexture->achFormatHint, extension.c_str(), len); |
149 | 0 | pScene->mTextures[textureId] = pTexture; |
150 | |
|
151 | 0 | return true; |
152 | 0 | } |