Merge branch 'master' into jere9309/read_collada_materials

pull/4339/head
Jeremy Sigrist 2022-01-18 09:55:33 -08:00 committed by GitHub
commit 59f28e168e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 942 additions and 125 deletions

View File

@ -331,7 +331,16 @@ void ColladaParser::ReadAssetInfo(XmlNode &node) {
const std::string &currentName = currentNode.name();
if (currentName == "unit") {
mUnitSize = 1.f;
XmlParser::getRealAttribute(currentNode, "meter", mUnitSize);
std::string tUnitSizeString;
if (XmlParser::getStdStrAttribute(currentNode, "meter", tUnitSizeString)) {
try {
fast_atoreal_move<ai_real>(tUnitSizeString.data(), mUnitSize);
} catch (const DeadlyImportError& die) {
std::string warning("Collada: Failed to parse meter parameter to real number. Exception:\n");
warning.append(die.what());
ASSIMP_LOG_WARN(warning.data());
}
}
} else if (currentName == "up_axis") {
std::string v;
if (!XmlParser::getValueAsString(currentNode, v)) {

View File

@ -0,0 +1,322 @@
/*
Open Asset Import Library (assimp)
----------------------------------------------------------------------
Copyright (c) 2006-2021, assimp team
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.
----------------------------------------------------------------------
*/
#ifndef ASSIMP_BUILD_NO_IQM_IMPORTER
#include <assimp/DefaultIOSystem.h>
#include <assimp/IOStreamBuffer.h>
#include <assimp/ai_assert.h>
#include <assimp/importerdesc.h>
#include <assimp/scene.h>
#include <assimp/DefaultLogger.hpp>
#include <assimp/Importer.hpp>
#include <assimp/ByteSwapper.h>
#include <memory>
#include <numeric>
#include "IQMImporter.h"
#include "iqm.h"
// RESOURCES:
// http://sauerbraten.org/iqm/
// https://github.com/lsalzman/iqm
inline void swap_block( uint32_t *block, size_t size ){
(void)block; // suppress 'unreferenced formal parameter' MSVC warning
size >>= 2;
for ( size_t i = 0; i < size; ++i )
AI_SWAP4( block[ i ] );
}
static const aiImporterDesc desc = {
"Inter-Quake Model Importer",
"",
"",
"",
aiImporterFlags_SupportBinaryFlavour,
0,
0,
0,
0,
"iqm"
};
namespace Assimp {
// ------------------------------------------------------------------------------------------------
// Default constructor
IQMImporter::IQMImporter() :
mScene(nullptr) {
// empty
}
// ------------------------------------------------------------------------------------------------
// Returns true, if file is a binary Inter-Quake Model file.
bool IQMImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const {
const std::string extension = GetExtension(pFile);
if (extension == "iqm")
return true;
else if (!extension.length() || checkSig) {
if (!pIOHandler) {
return true;
}
/*
* don't use CheckMagicToken because that checks with swapped bytes too, leading to false
* positives. This magic is not uint32_t, but char[4], so memcmp is the best way
const char* tokens[] = {"3DMO", "3dmo"};
return CheckMagicToken(pIOHandler,pFile,tokens,2,0,4);
*/
std::unique_ptr<IOStream> pStream(pIOHandler->Open(pFile, "rb"));
unsigned char data[15];
if (!pStream || 15 != pStream->Read(data, 1, 15)) {
return false;
}
return !memcmp(data, "INTERQUAKEMODEL", 15);
}
return false;
}
// ------------------------------------------------------------------------------------------------
const aiImporterDesc *IQMImporter::GetInfo() const {
return &desc;
}
// ------------------------------------------------------------------------------------------------
// Model 3D import implementation
void IQMImporter::InternReadFile(const std::string &file, aiScene *pScene, IOSystem *pIOHandler) {
// Read file into memory
std::unique_ptr<IOStream> pStream(pIOHandler->Open(file, "rb"));
if (!pStream.get()) {
throw DeadlyImportError("Failed to open file ", file, ".");
}
// Get the file-size and validate it, throwing an exception when fails
const size_t fileSize = pStream->FileSize();
if (fileSize < sizeof( iqmheader )) {
throw DeadlyImportError("IQM-file ", file, " is too small.");
}
std::vector<unsigned char> buffer(fileSize);
unsigned char *data = buffer.data();
if (fileSize != pStream->Read(data, 1, fileSize)) {
throw DeadlyImportError("Failed to read the file ", file, ".");
}
// get header
iqmheader &hdr = reinterpret_cast<iqmheader&>( *data );
swap_block( &hdr.version, sizeof( iqmheader ) - sizeof( iqmheader::magic ) );
// extra check for header
if (memcmp(data, IQM_MAGIC, sizeof( IQM_MAGIC ) )
|| hdr.version != IQM_VERSION
|| hdr.filesize != fileSize) {
throw DeadlyImportError("Bad binary header in file ", file, ".");
}
ASSIMP_LOG_DEBUG("IQM: loading ", file);
// create the root node
pScene->mRootNode = new aiNode( "<IQMRoot>" );
// Now rotate the whole scene 90 degrees around the x axis to convert to internal coordinate system
pScene->mRootNode->mTransformation = aiMatrix4x4(
1.f, 0.f, 0.f, 0.f,
0.f, 0.f, 1.f, 0.f,
0.f, -1.f, 0.f, 0.f,
0.f, 0.f, 0.f, 1.f);
pScene->mRootNode->mNumMeshes = hdr.num_meshes;
pScene->mRootNode->mMeshes = new unsigned int[hdr.num_meshes];
std::iota( pScene->mRootNode->mMeshes, pScene->mRootNode->mMeshes + pScene->mRootNode->mNumMeshes, 0 );
mScene = pScene;
// Allocate output storage
pScene->mNumMeshes = 0;
pScene->mMeshes = new aiMesh *[hdr.num_meshes](); // Set arrays to zero to ensue proper destruction if an exception is raised
pScene->mNumMaterials = 0;
pScene->mMaterials = new aiMaterial *[hdr.num_meshes]();
// swap vertex arrays beforehand...
for( auto array = reinterpret_cast<iqmvertexarray*>( data + hdr.ofs_vertexarrays ), end = array + hdr.num_vertexarrays; array != end; ++array )
{
swap_block( &array->type, sizeof( iqmvertexarray ) );
}
// Read all surfaces from the file
for( auto imesh = reinterpret_cast<iqmmesh*>( data + hdr.ofs_meshes ), end_ = imesh + hdr.num_meshes; imesh != end_; ++imesh )
{
swap_block( &imesh->name, sizeof( iqmmesh ) );
// Allocate output mesh & material
auto mesh = pScene->mMeshes[pScene->mNumMeshes++] = new aiMesh();
mesh->mMaterialIndex = pScene->mNumMaterials;
auto mat = pScene->mMaterials[pScene->mNumMaterials++] = new aiMaterial();
{
auto text = reinterpret_cast<char*>( data + hdr.ofs_text );
aiString name( text + imesh->material );
mat->AddProperty( &name, AI_MATKEY_NAME );
mat->AddProperty( &name, AI_MATKEY_TEXTURE_DIFFUSE(0) );
}
// Fill mesh information
mesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
mesh->mNumFaces = 0;
mesh->mFaces = new aiFace[imesh->num_triangles];
// Fill in all triangles
for( auto tri = reinterpret_cast<iqmtriangle*>( data + hdr.ofs_triangles ) + imesh->first_triangle, end = tri + imesh->num_triangles; tri != end; ++tri )
{
swap_block( tri->vertex, sizeof( tri->vertex ) );
auto& face = mesh->mFaces[mesh->mNumFaces++];
face.mNumIndices = 3;
face.mIndices = new unsigned int[3]{ tri->vertex[0] - imesh->first_vertex,
tri->vertex[2] - imesh->first_vertex,
tri->vertex[1] - imesh->first_vertex };
}
// Fill in all vertices
for( auto array = reinterpret_cast<const iqmvertexarray*>( data + hdr.ofs_vertexarrays ), end__ = array + hdr.num_vertexarrays; array != end__; ++array )
{
const unsigned int nVerts = imesh->num_vertexes;
const unsigned int step = array->size;
switch ( array->type )
{
case IQM_POSITION:
if( array->format == IQM_FLOAT && step >= 3 ){
mesh->mNumVertices = nVerts;
auto v = mesh->mVertices = new aiVector3D[nVerts];
for( auto f = reinterpret_cast<const float*>( data + array->offset ) + imesh->first_vertex * step,
end = f + nVerts * step; f != end; f += step, ++v )
{
*v = { AI_BE( f[0] ),
AI_BE( f[1] ),
AI_BE( f[2] ) };
}
}
break;
case IQM_TEXCOORD:
if( array->format == IQM_FLOAT && step >= 2)
{
auto v = mesh->mTextureCoords[0] = new aiVector3D[nVerts];
mesh->mNumUVComponents[0] = 2;
for( auto f = reinterpret_cast<const float*>( data + array->offset ) + imesh->first_vertex * step,
end = f + nVerts * step; f != end; f += step, ++v )
{
*v = { AI_BE( f[0] ),
1 - AI_BE( f[1] ), 0 };
}
}
break;
case IQM_NORMAL:
if (array->format == IQM_FLOAT && step >= 3)
{
auto v = mesh->mNormals = new aiVector3D[nVerts];
for( auto f = reinterpret_cast<const float*>( data + array->offset ) + imesh->first_vertex * step,
end = f + nVerts * step; f != end; f += step, ++v )
{
*v = { AI_BE( f[0] ),
AI_BE( f[1] ),
AI_BE( f[2] ) };
}
}
break;
case IQM_COLOR:
if (array->format == IQM_UBYTE && step >= 3)
{
auto v = mesh->mColors[0] = new aiColor4D[nVerts];
for( auto f = ( data + array->offset ) + imesh->first_vertex * step,
end = f + nVerts * step; f != end; f += step, ++v )
{
*v = { ( f[0] ) / 255.f,
( f[1] ) / 255.f,
( f[2] ) / 255.f,
step == 3? 1 : ( f[3] ) / 255.f };
}
}
else if (array->format == IQM_FLOAT && step >= 3)
{
auto v = mesh->mColors[0] = new aiColor4D[nVerts];
for( auto f = reinterpret_cast<const float*>( data + array->offset ) + imesh->first_vertex * step,
end = f + nVerts * step; f != end; f += step, ++v )
{
*v = { AI_BE( f[0] ),
AI_BE( f[1] ),
AI_BE( f[2] ),
step == 3? 1 : AI_BE( f[3] ) };
}
}
break;
case IQM_TANGENT:
#if 0
if (array->format == IQM_FLOAT && step >= 3)
{
auto v = mesh->mTangents = new aiVector3D[nVerts];
for( auto f = reinterpret_cast<const float*>( data + array->offset ) + imesh->first_vertex * step,
end = f + nVerts * step; f != end; f += step, ++v )
{
*v = { AI_BE( f[0] ),
AI_BE( f[1] ),
AI_BE( f[2] ) };
}
}
#endif
break;
case IQM_BLENDINDEXES:
case IQM_BLENDWEIGHTS:
case IQM_CUSTOM:
break; // these attributes are not relevant.
default:
break;
}
}
}
}
// ------------------------------------------------------------------------------------------------
} // Namespace Assimp
#endif // !! ASSIMP_BUILD_NO_IQM_IMPORTER

View File

@ -0,0 +1,78 @@
/*
Open Asset Import Library (assimp)
----------------------------------------------------------------------
Copyright (c) 2006-2021, assimp team
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.
----------------------------------------------------------------------
*/
/** @file IQMImporter.h
* @brief Declares the importer class to read a scene from an Inter-Quake Model file
*/
#pragma once
#ifndef ASSIMP_BUILD_NO_IQM_IMPORTER
#include <assimp/BaseImporter.h>
#include <assimp/material.h>
namespace Assimp {
class IQMImporter : public BaseImporter {
public:
/// \brief Default constructor
IQMImporter();
~IQMImporter() override {}
/// \brief Returns whether the class can handle the format of the given file.
/// \remark See BaseImporter::CanRead() for details.
bool CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const override;
protected:
//! \brief Appends the supported extension.
const aiImporterDesc *GetInfo() const override;
//! \brief File import implementation.
void InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) override;
private:
aiScene *mScene = nullptr; // the scene to import to
};
} // Namespace Assimp
#endif // ASSIMP_BUILD_NO_IQM_IMPORTER

View File

@ -0,0 +1,134 @@
#ifndef __IQM_H__
#define __IQM_H__
#define IQM_MAGIC "INTERQUAKEMODEL"
#define IQM_VERSION 2
struct iqmheader
{
char magic[16];
unsigned int version;
unsigned int filesize;
unsigned int flags;
unsigned int num_text, ofs_text;
unsigned int num_meshes, ofs_meshes;
unsigned int num_vertexarrays, num_vertexes, ofs_vertexarrays;
unsigned int num_triangles, ofs_triangles, ofs_adjacency;
unsigned int num_joints, ofs_joints;
unsigned int num_poses, ofs_poses;
unsigned int num_anims, ofs_anims;
unsigned int num_frames, num_framechannels, ofs_frames, ofs_bounds;
unsigned int num_comment, ofs_comment;
unsigned int num_extensions, ofs_extensions;
};
struct iqmmesh
{
unsigned int name;
unsigned int material;
unsigned int first_vertex, num_vertexes;
unsigned int first_triangle, num_triangles;
};
enum
{
IQM_POSITION = 0,
IQM_TEXCOORD = 1,
IQM_NORMAL = 2,
IQM_TANGENT = 3,
IQM_BLENDINDEXES = 4,
IQM_BLENDWEIGHTS = 5,
IQM_COLOR = 6,
IQM_CUSTOM = 0x10
};
enum
{
IQM_BYTE = 0,
IQM_UBYTE = 1,
IQM_SHORT = 2,
IQM_USHORT = 3,
IQM_INT = 4,
IQM_UINT = 5,
IQM_HALF = 6,
IQM_FLOAT = 7,
IQM_DOUBLE = 8
};
struct iqmtriangle
{
unsigned int vertex[3];
};
struct iqmadjacency
{
unsigned int triangle[3];
};
struct iqmjointv1
{
unsigned int name;
int parent;
float translate[3], rotate[3], scale[3];
};
struct iqmjoint
{
unsigned int name;
int parent;
float translate[3], rotate[4], scale[3];
};
struct iqmposev1
{
int parent;
unsigned int mask;
float channeloffset[9];
float channelscale[9];
};
struct iqmpose
{
int parent;
unsigned int mask;
float channeloffset[10];
float channelscale[10];
};
struct iqmanim
{
unsigned int name;
unsigned int first_frame, num_frames;
float framerate;
unsigned int flags;
};
enum
{
IQM_LOOP = 1<<0
};
struct iqmvertexarray
{
unsigned int type;
unsigned int flags;
unsigned int format;
unsigned int size;
unsigned int offset;
};
struct iqmbounds
{
float bbmin[3], bbmax[3];
float xyradius, radius;
};
struct iqmextension
{
unsigned int name;
unsigned int num_data, ofs_data;
unsigned int ofs_extensions; // pointer to next extension
};
#endif

View File

@ -45,6 +45,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Header files, Assimp
#include <assimp/DefaultLogger.hpp>
#include <assimp/Base64.hpp>
#ifdef ASSIMP_IMPORTER_GLTF_USE_OPEN3DGC
// Header files, Open3DGC.
@ -194,7 +195,7 @@ inline void Buffer::Read(Value &obj, Asset &r) {
if (ParseDataURI(uri, it->GetStringLength(), dataURI)) {
if (dataURI.base64) {
uint8_t *data = 0;
this->byteLength = Util::DecodeBase64(dataURI.data, dataURI.dataLength, data);
this->byteLength = Base64::Decode(dataURI.data, dataURI.dataLength, data);
this->mData.reset(data, std::default_delete<uint8_t[]>());
if (statedLength > 0 && this->byteLength != statedLength) {
@ -513,7 +514,7 @@ inline void Image::Read(Value &obj, Asset &r) {
mimeType = dataURI.mediaType;
if (dataURI.base64) {
uint8_t *ptr = nullptr;
mDataLength = glTFCommon::Util::DecodeBase64(dataURI.data, dataURI.dataLength, ptr);
mDataLength = Base64::Decode(dataURI.data, dataURI.dataLength, ptr);
mData.reset(ptr);
}
} else {

View File

@ -39,6 +39,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------
*/
#include <assimp/Base64.hpp>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>
#include <rapidjson/prettywriter.h>
@ -237,7 +239,7 @@ namespace glTF {
else if (img.HasData()) {
uri = "data:" + (img.mimeType.empty() ? "application/octet-stream" : img.mimeType);
uri += ";base64,";
glTFCommon::Util::EncodeBase64(img.GetData(), img.GetDataLength(), uri);
Base64::Encode(img.GetData(), img.GetDataLength(), uri);
}
else {
uri = img.uri;

View File

@ -48,84 +48,6 @@ using namespace glTFCommon::Util;
namespace Util {
size_t DecodeBase64(const char *in, size_t inLength, uint8_t *&out) {
if (inLength % 4 != 0) {
throw DeadlyImportError("Invalid base64 encoded data: \"", std::string(in, std::min(size_t(32), inLength)), "\", length:", inLength);
}
if (inLength < 4) {
out = nullptr;
return 0;
}
int nEquals = int(in[inLength - 1] == '=') +
int(in[inLength - 2] == '=');
size_t outLength = (inLength * 3) / 4 - nEquals;
out = new uint8_t[outLength];
memset(out, 0, outLength);
size_t i, j = 0;
for (i = 0; i + 4 < inLength; i += 4) {
uint8_t b0 = DecodeCharBase64(in[i]);
uint8_t b1 = DecodeCharBase64(in[i + 1]);
uint8_t b2 = DecodeCharBase64(in[i + 2]);
uint8_t b3 = DecodeCharBase64(in[i + 3]);
out[j++] = (uint8_t)((b0 << 2) | (b1 >> 4));
out[j++] = (uint8_t)((b1 << 4) | (b2 >> 2));
out[j++] = (uint8_t)((b2 << 6) | b3);
}
{
uint8_t b0 = DecodeCharBase64(in[i]);
uint8_t b1 = DecodeCharBase64(in[i + 1]);
uint8_t b2 = DecodeCharBase64(in[i + 2]);
uint8_t b3 = DecodeCharBase64(in[i + 3]);
out[j++] = (uint8_t)((b0 << 2) | (b1 >> 4));
if (b2 < 64) out[j++] = (uint8_t)((b1 << 4) | (b2 >> 2));
if (b3 < 64) out[j++] = (uint8_t)((b2 << 6) | b3);
}
return outLength;
}
void EncodeBase64(const uint8_t *in, size_t inLength, std::string &out) {
size_t outLength = ((inLength + 2) / 3) * 4;
size_t j = out.size();
out.resize(j + outLength);
for (size_t i = 0; i < inLength; i += 3) {
uint8_t b = (in[i] & 0xFC) >> 2;
out[j++] = EncodeCharBase64(b);
b = (in[i] & 0x03) << 4;
if (i + 1 < inLength) {
b |= (in[i + 1] & 0xF0) >> 4;
out[j++] = EncodeCharBase64(b);
b = (in[i + 1] & 0x0F) << 2;
if (i + 2 < inLength) {
b |= (in[i + 2] & 0xC0) >> 6;
out[j++] = EncodeCharBase64(b);
b = in[i + 2] & 0x3F;
out[j++] = EncodeCharBase64(b);
} else {
out[j++] = EncodeCharBase64(b);
out[j++] = '=';
}
} else {
out[j++] = EncodeCharBase64(b);
out[j++] = '=';
out[j++] = '=';
}
}
}
bool ParseDataURI(const char *const_uri, size_t uriLen, DataURI &out) {
if (nullptr == const_uri) {
return false;

View File

@ -232,37 +232,6 @@ struct DataURI {
//! Check if a uri is a data URI
bool ParseDataURI(const char *const_uri, size_t uriLen, DataURI &out);
template <bool B>
struct DATA {
static const uint8_t tableDecodeBase64[128];
};
template <bool B>
const uint8_t DATA<B>::tableDecodeBase64[128] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 64, 0, 0,
0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0,
0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0
};
inline char EncodeCharBase64(uint8_t b) {
return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="[size_t(b)];
}
inline uint8_t DecodeCharBase64(char c) {
if (c & 0x80) {
throw DeadlyImportError("Invalid base64 char value: ", size_t(c));
}
return DATA<true>::tableDecodeBase64[size_t(c & 0x7F)]; // TODO faster with lookup table or ifs?
}
size_t DecodeBase64(const char *in, size_t inLength, uint8_t *&out);
void EncodeBase64(const uint8_t *in, size_t inLength, std::string &out);
} // namespace Util
#define CHECK_EXT(EXT) \

View File

@ -44,6 +44,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <assimp/MemoryIOWrapper.h>
#include <assimp/StringUtils.h>
#include <assimp/DefaultLogger.hpp>
#include <assimp/Base64.hpp>
// clang-format off
#ifdef ASSIMP_ENABLE_DRACO
@ -563,7 +564,7 @@ inline void Buffer::Read(Value &obj, Asset &r) {
if (ParseDataURI(uri, it->GetStringLength(), dataURI)) {
if (dataURI.base64) {
uint8_t *data = nullptr;
this->byteLength = glTFCommon::Util::DecodeBase64(dataURI.data, dataURI.dataLength, data);
this->byteLength = Base64::Decode(dataURI.data, dataURI.dataLength, data);
this->mData.reset(data, std::default_delete<uint8_t[]>());
if (statedLength > 0 && this->byteLength != statedLength) {
@ -1062,7 +1063,7 @@ inline void Image::Read(Value &obj, Asset &r) {
mimeType = dataURI.mediaType;
if (dataURI.base64) {
uint8_t *ptr = nullptr;
mDataLength = glTFCommon::Util::DecodeBase64(dataURI.data, dataURI.dataLength, ptr);
mDataLength = Base64::Decode(dataURI.data, dataURI.dataLength, ptr);
mData.reset(ptr);
}
} else {

View File

@ -39,6 +39,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------
*/
#include <assimp/Base64.hpp>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>
#include <rapidjson/prettywriter.h>
@ -260,7 +261,7 @@ namespace glTF2 {
if (img.HasData()) {
uri = "data:" + (img.mimeType.empty() ? "application/octet-stream" : img.mimeType);
uri += ";base64,";
glTFCommon::Util::EncodeBase64(img.GetData(), img.GetDataLength(), uri);
Base64::Encode(img.GetData(), img.GetDataLength(), uri);
}
else {
uri = img.uri;

View File

@ -142,6 +142,7 @@ SET( PUBLIC_HEADERS
${HEADER_PATH}/MathFunctions.h
${HEADER_PATH}/Exceptional.h
${HEADER_PATH}/ByteSwapper.h
${HEADER_PATH}/Base64.hpp
)
SET( Core_SRCS
@ -174,6 +175,7 @@ SET( Common_SRCS
Common/ImporterRegistry.cpp
Common/DefaultProgressHandler.h
Common/DefaultIOStream.cpp
Common/IOSystem.cpp
Common/DefaultIOSystem.cpp
Common/ZipArchiveIOSystem.cpp
Common/PolyTools.h
@ -201,6 +203,7 @@ SET( Common_SRCS
Common/material.cpp
Common/AssertHandler.cpp
Common/Exceptional.cpp
Common/Base64.cpp
)
SOURCE_GROUP(Common FILES ${Common_SRCS})
@ -373,6 +376,12 @@ ADD_ASSIMP_IMPORTER( IRRMESH
AssetLib/Irr/IRRShared.h
)
ADD_ASSIMP_IMPORTER( IQM
AssetLib/IQM/IQMImporter.cpp
AssetLib/IQM/iqm.h
AssetLib/IQM/IQMImporter.h
)
ADD_ASSIMP_IMPORTER( IRR
AssetLib/Irr/IRRLoader.cpp
AssetLib/Irr/IRRLoader.h

View File

@ -0,0 +1,181 @@
/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------
Copyright (c) 2006-2021, assimp team
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 <assimp/Base64.hpp>
#include <assimp/Exceptional.h>
namespace Assimp {
namespace Base64 {
static const uint8_t tableDecodeBase64[128] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 64, 0, 0,
0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0,
0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0
};
static const char* tableEncodeBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
static inline char EncodeChar(uint8_t b) {
return tableEncodeBase64[size_t(b)];
}
inline uint8_t DecodeChar(char c) {
if (c & 0x80) {
throw DeadlyImportError("Invalid base64 char value: ", size_t(c));
}
return tableDecodeBase64[size_t(c & 0x7F)]; // TODO faster with lookup table or ifs?
}
void Encode(const uint8_t *in, size_t inLength, std::string &out) {
size_t outLength = ((inLength + 2) / 3) * 4;
size_t j = out.size();
out.resize(j + outLength);
for (size_t i = 0; i < inLength; i += 3) {
uint8_t b = (in[i] & 0xFC) >> 2;
out[j++] = EncodeChar(b);
b = (in[i] & 0x03) << 4;
if (i + 1 < inLength) {
b |= (in[i + 1] & 0xF0) >> 4;
out[j++] = EncodeChar(b);
b = (in[i + 1] & 0x0F) << 2;
if (i + 2 < inLength) {
b |= (in[i + 2] & 0xC0) >> 6;
out[j++] = EncodeChar(b);
b = in[i + 2] & 0x3F;
out[j++] = EncodeChar(b);
} else {
out[j++] = EncodeChar(b);
out[j++] = '=';
}
} else {
out[j++] = EncodeChar(b);
out[j++] = '=';
out[j++] = '=';
}
}
}
void Encode(const std::vector<uint8_t>& in, std::string &out) {
Encode (in.data (), in.size (), out);
}
std::string Encode (const std::vector<uint8_t>& in) {
std::string encoded;
Encode (in, encoded);
return encoded;
}
size_t Decode(const char *in, size_t inLength, uint8_t *&out) {
if (inLength % 4 != 0) {
throw DeadlyImportError("Invalid base64 encoded data: \"", std::string(in, std::min(size_t(32), inLength)), "\", length:", inLength);
}
if (inLength < 4) {
out = nullptr;
return 0;
}
int nEquals = int(in[inLength - 1] == '=') +
int(in[inLength - 2] == '=');
size_t outLength = (inLength * 3) / 4 - nEquals;
out = new uint8_t[outLength];
memset(out, 0, outLength);
size_t i, j = 0;
for (i = 0; i + 4 < inLength; i += 4) {
uint8_t b0 = DecodeChar(in[i]);
uint8_t b1 = DecodeChar(in[i + 1]);
uint8_t b2 = DecodeChar(in[i + 2]);
uint8_t b3 = DecodeChar(in[i + 3]);
out[j++] = (uint8_t)((b0 << 2) | (b1 >> 4));
out[j++] = (uint8_t)((b1 << 4) | (b2 >> 2));
out[j++] = (uint8_t)((b2 << 6) | b3);
}
{
uint8_t b0 = DecodeChar(in[i]);
uint8_t b1 = DecodeChar(in[i + 1]);
uint8_t b2 = DecodeChar(in[i + 2]);
uint8_t b3 = DecodeChar(in[i + 3]);
out[j++] = (uint8_t)((b0 << 2) | (b1 >> 4));
if (b2 < 64) out[j++] = (uint8_t)((b1 << 4) | (b2 >> 2));
if (b3 < 64) out[j++] = (uint8_t)((b2 << 6) | b3);
}
return outLength;
}
size_t Decode(const std::string& in, std::vector<uint8_t>& out) {
uint8_t* outPtr = nullptr;
size_t decodedSize = Decode (in.data (), in.size (), outPtr);
if (outPtr == nullptr) {
return 0;
}
out.assign (outPtr, outPtr + decodedSize);
delete[] outPtr;
return decodedSize;
}
std::vector<uint8_t> Decode (const std::string& in) {
std::vector<uint8_t> result;
Decode (in, result);
return result;
}
}
}

View File

@ -0,0 +1,53 @@
/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------
Copyright (c) 2006-2019, assimp team
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.
---------------------------------------------------------------------------
*/
/** @file Default implementation of IOSystem using the standard C file functions */
#include <assimp/IOSystem.hpp>
using namespace Assimp;
const std::string &IOSystem::CurrentDirectory() const {
if ( m_pathStack.empty() ) {
static const std::string Dummy = std::string();
return Dummy;
}
return m_pathStack[ m_pathStack.size()-1 ];
}

View File

@ -202,6 +202,9 @@ corresponding preprocessor flag to selectively disable formats.
#ifndef ASSIMP_BUILD_NO_M3D_IMPORTER
#include "AssetLib/M3D/M3DImporter.h"
#endif
#ifndef ASSIMP_BUILD_NO_IQM_IMPORTER
#include "AssetLib/IQM/IQMImporter.h"
#endif
namespace Assimp {
@ -370,6 +373,9 @@ void GetImporterInstanceList(std::vector<BaseImporter *> &out) {
#endif
#ifndef ASSIMP_BUILD_NO_MMD_IMPORTER
out.push_back(new MMDImporter());
#endif
#ifndef ASSIMP_BUILD_NO_IQM_IMPORTER
out.push_back(new IQMImporter());
#endif
//#ifndef ASSIMP_BUILD_NO_STEP_IMPORTER
// out.push_back(new StepFile::StepFileImporter());

View File

@ -0,0 +1,64 @@
/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------
Copyright (c) 2006-2022, assimp team
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.
---------------------------------------------------------------------------
*/
#pragma once
#ifndef AI_BASE64_HPP_INC
#define AI_BASE64_HPP_INC
#include <stdint.h>
#include <vector>
#include <string>
namespace Assimp {
namespace Base64 {
void Encode(const uint8_t *in, size_t inLength, std::string &out);
void Encode(const std::vector<uint8_t>& in, std::string &out);
std::string Encode(const std::vector<uint8_t>& in);
size_t Decode(const char *in, size_t inLength, uint8_t *&out);
size_t Decode(const std::string& in, std::vector<uint8_t>& out);
std::vector<uint8_t> Decode(const std::string& in);
} // namespace Base64
} // namespace Assimp
#endif // AI_BASE64_HPP_INC

View File

@ -288,15 +288,6 @@ AI_FORCE_INLINE bool IOSystem::PushDirectory( const std::string &path ) {
return true;
}
// ----------------------------------------------------------------------------
AI_FORCE_INLINE const std::string &IOSystem::CurrentDirectory() const {
if ( m_pathStack.empty() ) {
static const std::string Dummy;
return Dummy;
}
return m_pathStack[ m_pathStack.size()-1 ];
}
// ----------------------------------------------------------------------------
AI_FORCE_INLINE size_t IOSystem::StackSize() const {
return m_pathStack.size();

View File

@ -97,6 +97,7 @@ SET( COMMON
unit/Common/utSpatialSort.cpp
unit/Common/utAssertHandler.cpp
unit/Common/utXmlParser.cpp
unit/Common/utBase64.cpp
)
SET( IMPORTERS
@ -208,6 +209,7 @@ add_executable( unit
unit/CCompilerTest.c
unit/Main.cpp
../code/Common/Version.cpp
../code/Common/Base64.cpp
${COMMON}
${IMPORTERS}
${MATERIAL}

Binary file not shown.

After

Width:  |  Height:  |  Size: 361 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 116 KiB

Binary file not shown.

View File

@ -0,0 +1,72 @@
/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------
Copyright (c) 2006-2022, assimp team
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 "UnitTestPCH.h"
#include "TestIOSystem.h"
#include <assimp/Base64.hpp>
using namespace std;
using namespace Assimp;
class Base64Test : public ::testing::Test {
public:
virtual void SetUp() {
}
virtual void TearDown() {
}
};
static const std::vector<uint8_t> assimpStringBinary = { 97, 115, 115, 105, 109, 112 };
static const std::string assimpStringEncoded = "YXNzaW1w";
TEST_F( Base64Test, encodeTest ) {
EXPECT_EQ( "", Base64::Encode (std::vector<uint8_t>{}) );
EXPECT_EQ( "Vg==", Base64::Encode (std::vector<uint8_t>{ 86 }) );
EXPECT_EQ( assimpStringEncoded, Base64::Encode (assimpStringBinary) );
}
TEST_F( Base64Test, decodeTest ) {
EXPECT_EQ( std::vector<uint8_t> {}, Base64::Decode ("") );
EXPECT_EQ( std::vector<uint8_t> { 86 }, Base64::Decode ("Vg==") );
EXPECT_EQ( assimpStringBinary, Base64::Decode (assimpStringEncoded) );
}