Merge pull request #45 from Squareys/custom-image-loaders

Allow custom image loaders
This commit is contained in:
Syoyo Fujita 2018-03-14 10:07:49 +09:00 committed by GitHub
commit 4f0b893d73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 84 additions and 37 deletions

View File

@ -101,6 +101,7 @@ if (!ret) {
## Compile options ## Compile options
* `TINYGLTF_NOEXCEPTION` : Disable C++ exception in JSON parsing. You can use `-fno-exceptions` or by defining the symbol `JSON_NOEXCEPTION` and `TINYGLTF_NOEXCEPTION` to fully remove C++ exception codes when compiling TinyGLTF. * `TINYGLTF_NOEXCEPTION` : Disable C++ exception in JSON parsing. You can use `-fno-exceptions` or by defining the symbol `JSON_NOEXCEPTION` and `TINYGLTF_NOEXCEPTION` to fully remove C++ exception codes when compiling TinyGLTF.
* `TINYGLTF_NO_STB_IMAGE` : Do not load images with stb_image. Instead use `TinyGLTF::SetImageLoader(LoadimageDataFunction LoadImageData, void *user_data)` to set a callback for loading images.
### Saving gltTF 2.0 model ### Saving gltTF 2.0 model

View File

@ -450,7 +450,7 @@ struct Image {
std::vector<unsigned char> image; std::vector<unsigned char> image;
int bufferView; // (required if no uri) int bufferView; // (required if no uri)
std::string mimeType; // (required if no uri) ["image/jpeg", "image/png", "image/bmp", "image/gif"] std::string mimeType; // (required if no uri) ["image/jpeg", "image/png", "image/bmp", "image/gif"]
std::string uri; // (reqiored if no mimeType) std::string uri; // (required if no mimeType)
Value extras; Value extras;
Image() { bufferView = -1; } Image() { bufferView = -1; }
@ -725,6 +725,17 @@ enum SectionCheck {
REQUIRE_ALL = 0x3f REQUIRE_ALL = 0x3f
}; };
///
/// LoadImageDataFunction type. Signature for custom image loading callbacks.
///
typedef bool (*LoadImageDataFunction)(Image *, std::string *, int, int, const unsigned char *, int, void *);
#ifndef TINYGLTF_NO_STB_IMAGE
// Declaration of default image loader callback
static bool LoadImageData(Image *image, std::string *err, int req_width,
int req_height, const unsigned char *bytes,
int size, void*);
#endif
class TinyGLTF { class TinyGLTF {
public: public:
@ -788,6 +799,11 @@ class TinyGLTF {
const std::string & const std::string &
filename /*, bool embedImages, bool embedBuffers, bool writeBinary*/); filename /*, bool embedImages, bool embedBuffers, bool writeBinary*/);
///
/// Set callback to use for loading image data
///
void SetImageLoader(LoadImageDataFunction LoadImageData, void *user_data);
private: private:
/// ///
/// Loads glTF asset from string(memory). /// Loads glTF asset from string(memory).
@ -801,6 +817,14 @@ class TinyGLTF {
const unsigned char *bin_data_; const unsigned char *bin_data_;
size_t bin_size_; size_t bin_size_;
bool is_binary_; bool is_binary_;
LoadImageDataFunction LoadImageData =
#ifndef TINYGLTF_NO_STB_IMAGE
&tinygltf::LoadImageData;
#else
nullptr;
#endif
void *load_image_user_data_ = nullptr;
}; };
#ifdef __clang__ #ifdef __clang__
@ -847,7 +871,11 @@ class TinyGLTF {
#endif #endif
#include "./json.hpp" #include "./json.hpp"
#ifndef TINYGLTF_NO_STB_IMAGE
#include "./stb_image.h" #include "./stb_image.h"
#endif
#ifdef __clang__ #ifdef __clang__
#pragma clang diagnostic pop #pragma clang diagnostic pop
#endif #endif
@ -1159,11 +1187,15 @@ static bool LoadExternalFile(std::vector<unsigned char> *out, std::string *err,
return true; return true;
} }
void TinyGLTF::SetImageLoader(LoadImageDataFunction func, void *user_data) {
LoadImageData = func;
load_image_user_data_ = user_data;
}
#ifndef TINYGLTF_NO_STB_IMAGE
static bool LoadImageData(Image *image, std::string *err, int req_width, static bool LoadImageData(Image *image, std::string *err, int req_width,
int req_height, const unsigned char *bytes, int req_height, const unsigned char *bytes,
int size) { int size, void*) {
//std::cout << "size " << size << std::endl;
int w, h, comp; int w, h, comp;
// if image cannot be decoded, ignore parsing and keep it by its path // if image cannot be decoded, ignore parsing and keep it by its path
// don't break in this case // don't break in this case
@ -1217,6 +1249,7 @@ static bool LoadImageData(Image *image, std::string *err, int req_width,
return true; return true;
} }
#endif
static bool IsDataURI(const std::string &in) { static bool IsDataURI(const std::string &in) {
std::string header = "data:application/octet-stream;base64,"; std::string header = "data:application/octet-stream;base64,";
@ -1611,7 +1644,8 @@ static bool ParseAsset(Asset *asset, std::string *err,
static bool ParseImage(Image *image, std::string *err, static bool ParseImage(Image *image, std::string *err,
const json &o, const std::string &basedir, const json &o, const std::string &basedir,
bool is_binary, const unsigned char *bin_data, bool is_binary, const unsigned char *bin_data,
size_t bin_size) { size_t bin_size, LoadImageDataFunction* LoadImageData = nullptr,
void *user_data = nullptr) {
// A glTF image must either reference a bufferView or an image uri // A glTF image must either reference a bufferView or an image uri
double bufferView = -1; double bufferView = -1;
bool isEmbedded = bool isEmbedded =
@ -1703,8 +1737,14 @@ static bool ParseImage(Image *image, std::string *err,
} }
} }
return LoadImageData(image, err, 0, 0, &img.at(0), if (*LoadImageData == nullptr) {
static_cast<int>(img.size())); if (err) {
(*err) += "No LoadImageData callback specified.\n";
}
return false;
}
return (*LoadImageData)(image, err, 0, 0, &img.at(0),
static_cast<int>(img.size()), user_data);
} }
static bool ParseTexture(Texture *texture, std::string *err, static bool ParseTexture(Texture *texture, std::string *err,
@ -2877,7 +2917,7 @@ bool TinyGLTF::LoadFromString(Model *model, std::string *err, const char *str,
} }
Image image; Image image;
if (!ParseImage(&image, err, it.value(), base_dir, if (!ParseImage(&image, err, it.value(), base_dir,
is_binary_, bin_data_, bin_size_)) { is_binary_, bin_data_, bin_size_, &this->LoadImageData, load_image_user_data_)) {
return false; return false;
} }
@ -2897,9 +2937,15 @@ bool TinyGLTF::LoadFromString(Model *model, std::string *err, const char *str,
model->bufferViews[size_t(image.bufferView)]; model->bufferViews[size_t(image.bufferView)];
const Buffer &buffer = model->buffers[size_t(bufferView.buffer)]; const Buffer &buffer = model->buffers[size_t(bufferView.buffer)];
if (*LoadImageData == nullptr) {
if (err) {
(*err) += "No LoadImageData callback specified.\n";
}
return false;
}
bool ret = LoadImageData(&image, err, image.width, image.height, bool ret = LoadImageData(&image, err, image.width, image.height,
&buffer.data[bufferView.byteOffset], &buffer.data[bufferView.byteOffset],
static_cast<int>(bufferView.byteLength)); static_cast<int>(bufferView.byteLength), load_image_user_data_);
if (!ret) { if (!ret) {
return false; return false;
} }