| 126 | } |
| 127 | |
| 128 | bool EmbedTexturesProcess::addTexture(aiScene *pScene, const std::string &path) const { |
| 129 | std::streampos imageSize = 0; |
| 130 | std::string imagePath = tryToFindValidPath(path); |
| 131 | |
| 132 | if (imagePath.empty()) { |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | IOStream* pFile = mIOHandler->Open(imagePath); |
| 137 | if (pFile == nullptr) { |
| 138 | ASSIMP_LOG_ERROR("EmbedTexturesProcess: Unable to embed texture: ", path, "."); |
| 139 | return false; |
| 140 | } |
| 141 | imageSize = pFile->FileSize(); |
| 142 | |
| 143 | aiTexel* imageContent = new aiTexel[ 1ul + static_cast<unsigned long>( imageSize ) / sizeof(aiTexel)]; |
| 144 | pFile->Seek(0, aiOrigin_SET); |
| 145 | pFile->Read(reinterpret_cast<char*>(imageContent), static_cast<size_t>(imageSize), 1); |
| 146 | mIOHandler->Close(pFile); |
| 147 | |
| 148 | // Enlarging the textures table |
| 149 | unsigned int textureId = pScene->mNumTextures++; |
| 150 | auto oldTextures = pScene->mTextures; |
| 151 | pScene->mTextures = new aiTexture*[pScene->mNumTextures]; |
| 152 | ::memmove(pScene->mTextures, oldTextures, sizeof(aiTexture*) * (pScene->mNumTextures - 1u)); |
| 153 | delete [] oldTextures; |
| 154 | |
| 155 | // Add the new texture |
| 156 | auto pTexture = new aiTexture; |
| 157 | pTexture->mHeight = 0; // Means that this is still compressed |
| 158 | pTexture->mWidth = static_cast<uint32_t>(imageSize); |
| 159 | pTexture->pcData = imageContent; |
| 160 | |
| 161 | auto extension = path.substr(path.find_last_of('.') + 1u); |
| 162 | extension = ai_tolower(extension); |
| 163 | if (extension == "jpeg") { |
| 164 | extension = "jpg"; |
| 165 | } |
| 166 | |
| 167 | size_t len = extension.size(); |
| 168 | if (len > HINTMAXTEXTURELEN -1 ) { |
| 169 | len = HINTMAXTEXTURELEN - 1; |
| 170 | } |
| 171 | ::strncpy(pTexture->achFormatHint, extension.c_str(), len); |
| 172 | pScene->mTextures[textureId] = pTexture; |
| 173 | |
| 174 | return true; |
| 175 | } |