2
0

EmbedTexturesProcess.cpp 5.6 KB

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