D3MFOpcPackage.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, 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. #ifndef ASSIMP_BUILD_NO_3MF_IMPORTER
  34. #include "D3MFOpcPackage.h"
  35. #include <assimp/Exceptional.h>
  36. #include <assimp/XmlParser.h>
  37. #include <assimp/ZipArchiveIOSystem.h>
  38. #include <assimp/ai_assert.h>
  39. #include <assimp/DefaultLogger.hpp>
  40. #include <assimp/IOStream.hpp>
  41. #include <assimp/IOSystem.hpp>
  42. #include <assimp/texture.h>
  43. #include "3MFXmlTags.h"
  44. #include <algorithm>
  45. #include <cassert>
  46. #include <cstdlib>
  47. #include <map>
  48. #include <vector>
  49. namespace Assimp {
  50. namespace D3MF {
  51. // ------------------------------------------------------------------------------------------------
  52. using OpcPackageRelationshipPtr = std::shared_ptr<OpcPackageRelationship>;
  53. class OpcPackageRelationshipReader {
  54. public:
  55. OpcPackageRelationshipReader(XmlParser &parser) :
  56. mRelations() {
  57. XmlNode root = parser.getRootNode();
  58. ParseRootNode(root);
  59. }
  60. void ParseRootNode(XmlNode &node) {
  61. ParseAttributes(node);
  62. for (XmlNode currentNode = node.first_child(); currentNode; currentNode = currentNode.next_sibling()) {
  63. std::string name = currentNode.name();
  64. if (name == "Relationships") {
  65. ParseRelationsNode(currentNode);
  66. }
  67. }
  68. }
  69. void ParseAttributes(XmlNode & /*node*/) {
  70. // empty
  71. }
  72. bool validateRels(OpcPackageRelationshipPtr &relPtr) {
  73. if (relPtr->id.empty() || relPtr->type.empty() || relPtr->target.empty()) {
  74. return false;
  75. }
  76. return true;
  77. }
  78. void ParseRelationsNode(XmlNode &node) {
  79. if (node.empty()) {
  80. return;
  81. }
  82. for (XmlNode currentNode = node.first_child(); currentNode; currentNode = currentNode.next_sibling()) {
  83. const std::string name = currentNode.name();
  84. if (name == "Relationship") {
  85. OpcPackageRelationshipPtr relPtr(new OpcPackageRelationship());
  86. relPtr->id = currentNode.attribute(XmlTag::RELS_ATTRIB_ID).as_string();
  87. relPtr->type = currentNode.attribute(XmlTag::RELS_ATTRIB_TYPE).as_string();
  88. relPtr->target = currentNode.attribute(XmlTag::RELS_ATTRIB_TARGET).as_string();
  89. if (validateRels(relPtr)) {
  90. mRelations.push_back(relPtr);
  91. }
  92. }
  93. }
  94. }
  95. std::vector<OpcPackageRelationshipPtr> mRelations;
  96. };
  97. static bool IsEmbeddedTexture( const std::string &filename ) {
  98. const std::string extension = BaseImporter::GetExtension(filename);
  99. if (extension == "jpg" || extension == "png" || extension == "jpeg") {
  100. std::string::size_type pos = filename.find("thumbnail");
  101. if (pos != std::string::npos) {
  102. return false;
  103. }
  104. return true;
  105. }
  106. return false;
  107. }
  108. // ------------------------------------------------------------------------------------------------
  109. D3MFOpcPackage::D3MFOpcPackage(IOSystem *pIOHandler, const std::string &rFile) :
  110. mRootStream(nullptr),
  111. mZipArchive() {
  112. mZipArchive = new ZipArchiveIOSystem(pIOHandler, rFile);
  113. if (!mZipArchive->isOpen()) {
  114. throw DeadlyImportError("Failed to open file ", rFile, ".");
  115. }
  116. std::vector<std::string> fileList;
  117. mZipArchive->getFileList(fileList);
  118. for (auto &file : fileList) {
  119. if (file == D3MF::XmlTag::ROOT_RELATIONSHIPS_ARCHIVE) {
  120. if (!mZipArchive->Exists(file.c_str())) {
  121. continue;
  122. }
  123. IOStream *fileStream = mZipArchive->Open(file.c_str());
  124. if (nullptr == fileStream) {
  125. ASSIMP_LOG_ERROR("Filestream is nullptr.");
  126. continue;
  127. }
  128. std::string rootFile = ReadPackageRootRelationship(fileStream);
  129. if (!rootFile.empty() && rootFile[0] == '/') {
  130. rootFile = rootFile.substr(1);
  131. if (rootFile[0] == '/') {
  132. // deal with zip-bug
  133. rootFile = rootFile.substr(1);
  134. }
  135. }
  136. ASSIMP_LOG_VERBOSE_DEBUG(rootFile);
  137. mZipArchive->Close(fileStream);
  138. mRootStream = mZipArchive->Open(rootFile.c_str());
  139. ai_assert(mRootStream != nullptr);
  140. if (nullptr == mRootStream) {
  141. throw DeadlyImportError("Cannot open root-file in archive : " + rootFile);
  142. }
  143. } else if (file == D3MF::XmlTag::CONTENT_TYPES_ARCHIVE) {
  144. ASSIMP_LOG_WARN("Ignored file of unsupported type CONTENT_TYPES_ARCHIVES", file);
  145. } else if (IsEmbeddedTexture(file)) {
  146. IOStream *fileStream = mZipArchive->Open(file.c_str());
  147. LoadEmbeddedTextures(fileStream, file);
  148. mZipArchive->Close(fileStream);
  149. } else {
  150. ASSIMP_LOG_WARN("Ignored file of unknown type: ", file);
  151. }
  152. }
  153. }
  154. D3MFOpcPackage::~D3MFOpcPackage() {
  155. mZipArchive->Close(mRootStream);
  156. delete mZipArchive;
  157. }
  158. IOStream *D3MFOpcPackage::RootStream() const {
  159. return mRootStream;
  160. }
  161. const std::vector<aiTexture *> &D3MFOpcPackage::GetEmbeddedTextures() const {
  162. return mEmbeddedTextures;
  163. }
  164. static const char *const ModelRef = "3D/3dmodel.model";
  165. bool D3MFOpcPackage::validate() {
  166. if (nullptr == mRootStream || nullptr == mZipArchive) {
  167. return false;
  168. }
  169. return mZipArchive->Exists(ModelRef);
  170. }
  171. std::string D3MFOpcPackage::ReadPackageRootRelationship(IOStream *stream) {
  172. XmlParser xmlParser;
  173. if (!xmlParser.parse(stream)) {
  174. return std::string();
  175. }
  176. OpcPackageRelationshipReader reader(xmlParser);
  177. auto itr = std::find_if(reader.mRelations.begin(), reader.mRelations.end(), [](const OpcPackageRelationshipPtr &rel) {
  178. return rel->type == XmlTag::PACKAGE_START_PART_RELATIONSHIP_TYPE;
  179. });
  180. if (itr == reader.mRelations.end()) {
  181. throw DeadlyImportError("Cannot find ", XmlTag::PACKAGE_START_PART_RELATIONSHIP_TYPE);
  182. }
  183. return (*itr)->target;
  184. }
  185. void D3MFOpcPackage::LoadEmbeddedTextures(IOStream *fileStream, const std::string &filename) {
  186. if (nullptr == fileStream) {
  187. return;
  188. }
  189. const size_t size = fileStream->FileSize();
  190. if (0 == size) {
  191. return;
  192. }
  193. unsigned char *data = new unsigned char[size];
  194. fileStream->Read(data, 1, size);
  195. aiTexture *texture = new aiTexture;
  196. std::string embName = "*" + filename;
  197. texture->mFilename.Set(embName.c_str());
  198. texture->mWidth = static_cast<unsigned int>(size);
  199. texture->mHeight = 0;
  200. texture->achFormatHint[0] = 'p';
  201. texture->achFormatHint[1] = 'n';
  202. texture->achFormatHint[2] = 'g';
  203. texture->achFormatHint[3] = '\0';
  204. texture->pcData = (aiTexel*) data;
  205. mEmbeddedTextures.emplace_back(texture);
  206. }
  207. } // Namespace D3MF
  208. } // Namespace Assimp
  209. #endif //ASSIMP_BUILD_NO_3MF_IMPORTER