M3DWrapper.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2020, assimp team
  5. Copyright (c) 2019 bzt
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the
  9. following conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ----------------------------------------------------------------------
  33. */
  34. #if !(ASSIMP_BUILD_NO_EXPORT || ASSIMP_BUILD_NO_M3D_EXPORTER) || !ASSIMP_BUILD_NO_M3D_IMPORTER
  35. #include "M3DWrapper.h"
  36. #include <assimp/DefaultIOSystem.h>
  37. #include <assimp/IOStreamBuffer.h>
  38. #include <assimp/ai_assert.h>
  39. #ifdef ASSIMP_USE_M3D_READFILECB
  40. #if (__cplusplus >= 201103L) || !defined(_MSC_VER) || (_MSC_VER >= 1900) // C++11 and MSVC 2015 onwards
  41. #define threadlocal thread_local
  42. #else
  43. #if defined(_MSC_VER) && (_MSC_VER >= 1800) // there's an alternative for MSVC 2013
  44. #define threadlocal __declspec(thread)
  45. #else
  46. #define threadlocal
  47. #endif
  48. #endif
  49. extern "C" {
  50. // workaround: the M3D SDK expects a C callback, but we want to use Assimp::IOSystem to implement that
  51. threadlocal void *m3dimporter_pIOHandler;
  52. unsigned char *m3dimporter_readfile(char *fn, unsigned int *size) {
  53. ai_assert(nullptr != fn);
  54. ai_assert(nullptr != size);
  55. std::string file(fn);
  56. std::unique_ptr<Assimp::IOStream> pStream(
  57. (reinterpret_cast<Assimp::IOSystem *>(m3dimporter_pIOHandler))->Open(file, "rb"));
  58. size_t fileSize = 0;
  59. unsigned char *data = nullptr;
  60. // sometimes pStream is nullptr in a single-threaded scenario too for some reason
  61. // (should be an empty object returning nothing I guess)
  62. if (pStream) {
  63. fileSize = pStream->FileSize();
  64. // should be allocated with malloc(), because the library will call free() to deallocate
  65. data = (unsigned char *)malloc(fileSize);
  66. if (!data || !pStream.get() || !fileSize || fileSize != pStream->Read(data, 1, fileSize)) {
  67. pStream.reset();
  68. *size = 0;
  69. // don't throw a deadly exception, it's not fatal if we can't read an external asset
  70. return nullptr;
  71. }
  72. pStream.reset();
  73. }
  74. *size = (int)fileSize;
  75. return data;
  76. }
  77. }
  78. #endif
  79. namespace Assimp {
  80. M3DWrapper::M3DWrapper() {
  81. // use malloc() here because m3d_free() will call free()
  82. m3d_ = (m3d_t *)calloc(1, sizeof(m3d_t));
  83. }
  84. M3DWrapper::M3DWrapper(IOSystem *pIOHandler, const std::vector<unsigned char> &buffer) {
  85. if (nullptr == pIOHandler) {
  86. ai_assert(nullptr != pIOHandler);
  87. }
  88. #ifdef ASSIMP_USE_M3D_READFILECB
  89. // pass this IOHandler to the C callback in a thread-local pointer
  90. m3dimporter_pIOHandler = pIOHandler;
  91. m3d_ = m3d_load(const_cast<unsigned char *>(buffer.data()), m3dimporter_readfile, free, nullptr);
  92. // Clear the C callback
  93. m3dimporter_pIOHandler = nullptr;
  94. #else
  95. m3d_ = m3d_load(const_cast<unsigned char *>(buffer.data()), nullptr, nullptr, nullptr);
  96. #endif
  97. }
  98. M3DWrapper::~M3DWrapper() {
  99. reset();
  100. }
  101. void M3DWrapper::reset() {
  102. ClearSave();
  103. if (m3d_) {
  104. m3d_free(m3d_);
  105. }
  106. m3d_ = nullptr;
  107. }
  108. unsigned char *M3DWrapper::Save(int quality, int flags, unsigned int &size) {
  109. #if (!(ASSIMP_BUILD_NO_EXPORT || ASSIMP_BUILD_NO_M3D_EXPORTER))
  110. ClearSave();
  111. saved_output_ = m3d_save(m3d_, quality, flags, &size);
  112. return saved_output_;
  113. #else
  114. return nullptr;
  115. #endif
  116. }
  117. void M3DWrapper::ClearSave() {
  118. if (saved_output_)
  119. M3D_FREE(saved_output_);
  120. saved_output_ = nullptr;
  121. }
  122. } // namespace Assimp
  123. #endif