2018-01-08 08:26:26 +00:00
|
|
|
/*
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2022-01-10 20:13:43 +00:00
|
|
|
Copyright (c) 2006-2022, assimp team
|
2018-01-28 18:42:05 +00:00
|
|
|
|
2018-01-08 08:26:26 +00:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the
|
|
|
|
following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the assimp team, nor the names of its
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
|
|
|
written permission of the assimp team.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "EmbedTexturesProcess.h"
|
2021-06-15 20:20:12 +00:00
|
|
|
#include <assimp/IOStream.hpp>
|
|
|
|
#include <assimp/IOSystem.hpp>
|
2018-01-11 21:21:40 +00:00
|
|
|
#include <assimp/ParsingUtils.h>
|
2018-01-08 08:26:26 +00:00
|
|
|
#include "ProcessHelper.h"
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
|
|
|
|
bool EmbedTexturesProcess::IsActive(unsigned int pFlags) const {
|
|
|
|
return (pFlags & aiProcess_EmbedTextures) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmbedTexturesProcess::SetupProperties(const Importer* pImp) {
|
|
|
|
mRootPath = pImp->GetPropertyString("sourceFilePath");
|
|
|
|
mRootPath = mRootPath.substr(0, mRootPath.find_last_of("\\/") + 1u);
|
2021-06-15 20:20:12 +00:00
|
|
|
mIOHandler = pImp->GetIOHandler();
|
2018-01-08 08:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EmbedTexturesProcess::Execute(aiScene* pScene) {
|
2021-06-16 09:21:31 +00:00
|
|
|
if (pScene == nullptr || pScene->mRootNode == nullptr || mIOHandler == nullptr){
|
|
|
|
return;
|
|
|
|
}
|
2018-01-08 08:26:26 +00:00
|
|
|
|
|
|
|
aiString path;
|
|
|
|
uint32_t embeddedTexturesCount = 0u;
|
|
|
|
for (auto matId = 0u; matId < pScene->mNumMaterials; ++matId) {
|
|
|
|
auto material = pScene->mMaterials[matId];
|
|
|
|
|
|
|
|
for (auto ttId = 1u; ttId < AI_TEXTURE_TYPE_MAX; ++ttId) {
|
|
|
|
auto tt = static_cast<aiTextureType>(ttId);
|
|
|
|
auto texturesCount = material->GetTextureCount(tt);
|
|
|
|
|
|
|
|
for (auto texId = 0u; texId < texturesCount; ++texId) {
|
|
|
|
material->GetTexture(tt, texId, &path);
|
|
|
|
if (path.data[0] == '*') continue; // Already embedded
|
|
|
|
|
|
|
|
// Indeed embed
|
|
|
|
if (addTexture(pScene, path.data)) {
|
|
|
|
auto embeddedTextureId = pScene->mNumTextures - 1u;
|
2021-10-02 22:36:40 +00:00
|
|
|
path.length = ::ai_snprintf(path.data, 1024, "*%u", embeddedTextureId);
|
2018-01-08 08:26:26 +00:00
|
|
|
material->AddProperty(&path, AI_MATKEY_TEXTURE(tt, texId));
|
|
|
|
embeddedTexturesCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_INFO("EmbedTexturesProcess finished. Embedded ", embeddedTexturesCount, " textures." );
|
2018-01-08 08:26:26 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 16:27:15 +00:00
|
|
|
bool EmbedTexturesProcess::addTexture(aiScene *pScene, const std::string &path) const {
|
2018-03-19 17:22:27 +00:00
|
|
|
std::streampos imageSize = 0;
|
|
|
|
std::string imagePath = path;
|
2018-01-08 08:26:26 +00:00
|
|
|
|
|
|
|
// Test path directly
|
2021-06-15 20:20:12 +00:00
|
|
|
if (!mIOHandler->Exists(imagePath)) {
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_WARN("EmbedTexturesProcess: Cannot find image: ", imagePath, ". Will try to find it in root folder.");
|
2018-01-08 08:26:26 +00:00
|
|
|
|
|
|
|
// Test path in root path
|
|
|
|
imagePath = mRootPath + path;
|
2021-06-15 20:20:12 +00:00
|
|
|
if (!mIOHandler->Exists(imagePath)) {
|
2018-01-08 08:26:26 +00:00
|
|
|
// Test path basename in root path
|
|
|
|
imagePath = mRootPath + path.substr(path.find_last_of("\\/") + 1u);
|
2021-06-15 20:20:12 +00:00
|
|
|
if (!mIOHandler->Exists(imagePath)) {
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_ERROR("EmbedTexturesProcess: Unable to embed texture: ", path, ".");
|
2018-01-08 08:26:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-15 20:20:12 +00:00
|
|
|
IOStream* pFile = mIOHandler->Open(imagePath);
|
|
|
|
if (pFile == nullptr) {
|
|
|
|
ASSIMP_LOG_ERROR("EmbedTexturesProcess: Unable to embed texture: ", path, ".");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
imageSize = pFile->FileSize();
|
2018-01-08 08:26:26 +00:00
|
|
|
|
2018-09-21 14:07:09 +00:00
|
|
|
aiTexel* imageContent = new aiTexel[ 1ul + static_cast<unsigned long>( imageSize ) / sizeof(aiTexel)];
|
2021-06-15 20:20:12 +00:00
|
|
|
pFile->Seek(0, aiOrigin_SET);
|
2022-06-18 21:11:24 +00:00
|
|
|
pFile->Read(reinterpret_cast<char*>(imageContent), static_cast<size_t>(imageSize), 1);
|
2021-06-15 20:20:12 +00:00
|
|
|
mIOHandler->Close(pFile);
|
2018-01-08 08:26:26 +00:00
|
|
|
|
|
|
|
// Enlarging the textures table
|
2018-12-31 13:47:54 +00:00
|
|
|
unsigned int textureId = pScene->mNumTextures++;
|
2018-01-08 08:26:26 +00:00
|
|
|
auto oldTextures = pScene->mTextures;
|
|
|
|
pScene->mTextures = new aiTexture*[pScene->mNumTextures];
|
2018-12-31 13:47:54 +00:00
|
|
|
::memmove(pScene->mTextures, oldTextures, sizeof(aiTexture*) * (pScene->mNumTextures - 1u));
|
2020-01-09 21:04:46 +00:00
|
|
|
delete [] oldTextures;
|
2021-07-29 11:28:51 +00:00
|
|
|
|
2018-01-08 08:26:26 +00:00
|
|
|
// Add the new texture
|
2018-12-31 13:47:54 +00:00
|
|
|
auto pTexture = new aiTexture;
|
2018-01-08 08:26:26 +00:00
|
|
|
pTexture->mHeight = 0; // Means that this is still compressed
|
2018-03-20 20:56:26 +00:00
|
|
|
pTexture->mWidth = static_cast<uint32_t>(imageSize);
|
2018-01-08 08:26:26 +00:00
|
|
|
pTexture->pcData = imageContent;
|
|
|
|
|
|
|
|
auto extension = path.substr(path.find_last_of('.') + 1u);
|
2021-04-03 08:25:03 +00:00
|
|
|
extension = ai_tolower(extension);
|
2018-12-31 13:47:54 +00:00
|
|
|
if (extension == "jpeg") {
|
|
|
|
extension = "jpg";
|
|
|
|
}
|
|
|
|
|
2018-12-31 11:16:20 +00:00
|
|
|
size_t len = extension.size();
|
2018-12-31 13:47:54 +00:00
|
|
|
if (len > HINTMAXTEXTURELEN -1 ) {
|
2018-12-31 11:16:20 +00:00
|
|
|
len = HINTMAXTEXTURELEN - 1;
|
|
|
|
}
|
2018-12-31 13:47:54 +00:00
|
|
|
::strncpy(pTexture->achFormatHint, extension.c_str(), len);
|
2018-01-08 08:26:26 +00:00
|
|
|
pScene->mTextures[textureId] = pTexture;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|