Coverage Report

Created: 2026-05-23 06:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/assimp/code/PostProcessing/EmbedTexturesProcess.cpp
Line
Count
Source
1
/*
2
Open Asset Import Library (assimp)
3
----------------------------------------------------------------------
4
5
Copyright (c) 2006-2026, 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
#include <algorithm>
50
51
using namespace Assimp;
52
53
3.36k
bool EmbedTexturesProcess::IsActive(unsigned int pFlags) const {
54
3.36k
    return (pFlags & aiProcess_EmbedTextures) != 0;
55
3.36k
}
56
57
0
void EmbedTexturesProcess::SetupProperties(const Importer* pImp) {
58
0
    mRootPath = pImp->GetPropertyString("sourceFilePath");
59
0
    mRootPath = mRootPath.substr(0, mRootPath.find_last_of("\\/") + 1u);
60
0
    mIOHandler = pImp->GetIOHandler();
61
0
}
62
63
0
void EmbedTexturesProcess::Execute(aiScene* pScene) {
64
0
    if (pScene == nullptr || pScene->mRootNode == nullptr || mIOHandler == nullptr){
65
0
        return;
66
0
    }
67
68
0
    aiString path;
69
0
    uint32_t embeddedTexturesCount = 0u;
70
0
    for (auto matId = 0u; matId < pScene->mNumMaterials; ++matId) {
71
0
        auto material = pScene->mMaterials[matId];
72
73
0
        for (auto ttId = 1u; ttId < AI_TEXTURE_TYPE_MAX; ++ttId) {
74
0
            auto tt = static_cast<aiTextureType>(ttId);
75
0
            auto texturesCount = material->GetTextureCount(tt);
76
77
0
            for (auto texId = 0u; texId < texturesCount; ++texId) {
78
0
                material->GetTexture(tt, texId, &path);
79
0
                if (path.data[0] == '*') continue; // Already embedded
80
81
                // Indeed embed
82
0
                if (addTexture(pScene, path.data)) {
83
0
                    auto embeddedTextureId = pScene->mNumTextures - 1u;
84
0
                    path.length = ::ai_snprintf(path.data, 1024, "*%u", embeddedTextureId);
85
0
                    material->AddProperty(&path, AI_MATKEY_TEXTURE(tt, texId));
86
0
                    embeddedTexturesCount++;
87
0
                }
88
0
            }
89
0
        }
90
0
    }
91
92
0
    ASSIMP_LOG_INFO("EmbedTexturesProcess finished. Embedded ", embeddedTexturesCount, " textures." );
93
0
}
94
95
std::string EmbedTexturesProcess::tryToFindValidPath(const std::string &imagePath) const
96
0
{
97
    // Test path directly
98
0
    if (mIOHandler->Exists(imagePath)) {
99
0
        return imagePath;
100
0
    }
101
0
    ASSIMP_LOG_WARN("EmbedTexturesProcess: Cannot find image: ", imagePath, ". Will try to find it in root folder.");
102
103
    // Test path in root path
104
0
    std::string testPath = mRootPath + imagePath;
105
0
    if (mIOHandler->Exists(testPath)) {
106
0
        return testPath;
107
0
    }
108
109
    // Test path basename in root path
110
0
    testPath = mRootPath + imagePath.substr(imagePath.find_last_of("\\/") + 1u);
111
0
    if (mIOHandler->Exists(testPath)) {
112
0
        return testPath;
113
0
    }
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
0
    if (mIOHandler->getOsSeparator() != '\\' && imagePath.find('\\') != std::string::npos) {
118
0
        ASSIMP_LOG_WARN("EmbedTexturesProcess: Cannot find image '", imagePath, "' in root folder. Will try replacing directory separators.");
119
0
        testPath = imagePath;
120
0
        std::replace(testPath.begin(), testPath.end(), '\\', mIOHandler->getOsSeparator());
121
0
        return tryToFindValidPath(testPath);
122
0
    }
123
124
0
    ASSIMP_LOG_ERROR("EmbedTexturesProcess: Unable to embed texture: ", imagePath, ".");
125
0
    return {};
126
0
}
127
128
0
bool EmbedTexturesProcess::addTexture(aiScene *pScene, const std::string &path) const {
129
0
    std::streampos imageSize = 0;
130
0
    std::string    imagePath = tryToFindValidPath(path);
131
132
0
    if (imagePath.empty()) {
133
0
        return false;
134
0
    }
135
136
0
    IOStream* pFile = mIOHandler->Open(imagePath);
137
0
    if (pFile == nullptr) {
138
0
        ASSIMP_LOG_ERROR("EmbedTexturesProcess: Unable to embed texture: ", path, ".");
139
0
        return false;
140
0
    }
141
0
    imageSize = pFile->FileSize();
142
143
0
    aiTexel* imageContent = new aiTexel[ 1ul + static_cast<unsigned long>( imageSize ) / sizeof(aiTexel)];
144
0
    pFile->Seek(0, aiOrigin_SET);
145
0
    pFile->Read(reinterpret_cast<char*>(imageContent), static_cast<size_t>(imageSize), 1);
146
0
    mIOHandler->Close(pFile);
147
148
    // Enlarging the textures table
149
0
    unsigned int textureId = pScene->mNumTextures++;
150
0
    auto oldTextures = pScene->mTextures;
151
0
    pScene->mTextures = new aiTexture*[pScene->mNumTextures];
152
0
    ::memmove(pScene->mTextures, oldTextures, sizeof(aiTexture*) * (pScene->mNumTextures - 1u));
153
0
    delete [] oldTextures;
154
155
    // Add the new texture
156
0
    auto pTexture = new aiTexture;
157
0
    pTexture->mHeight = 0; // Means that this is still compressed
158
0
    pTexture->mWidth = static_cast<uint32_t>(imageSize);
159
0
    pTexture->pcData = imageContent;
160
161
0
    auto extension = path.substr(path.find_last_of('.') + 1u);
162
0
    extension = ai_tolower(extension);
163
0
    if (extension == "jpeg") {
164
0
        extension = "jpg";
165
0
    }
166
167
0
    size_t len = extension.size();
168
0
    if (len > HINTMAXTEXTURELEN -1 ) {
169
0
        len = HINTMAXTEXTURELEN - 1;
170
0
    }
171
0
    ::strncpy(pTexture->achFormatHint, extension.c_str(), len);
172
0
    pScene->mTextures[textureId] = pTexture;
173
174
0
    return true;
175
0
}