mirror of
https://git.mirrors.martin98.com/https://github.com/syoyo/tinygltf.git
synced 2025-07-07 12:31:50 +08:00
Merge pull request #45 from Squareys/custom-image-loaders
Allow custom image loaders
This commit is contained in:
commit
4f0b893d73
@ -101,6 +101,7 @@ if (!ret) {
|
||||
## 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_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
|
||||
|
||||
|
64
tiny_gltf.h
64
tiny_gltf.h
@ -450,7 +450,7 @@ struct Image {
|
||||
std::vector<unsigned char> image;
|
||||
int bufferView; // (required if no uri)
|
||||
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;
|
||||
|
||||
Image() { bufferView = -1; }
|
||||
@ -725,6 +725,17 @@ enum SectionCheck {
|
||||
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 {
|
||||
public:
|
||||
@ -788,6 +799,11 @@ class TinyGLTF {
|
||||
const std::string &
|
||||
filename /*, bool embedImages, bool embedBuffers, bool writeBinary*/);
|
||||
|
||||
///
|
||||
/// Set callback to use for loading image data
|
||||
///
|
||||
void SetImageLoader(LoadImageDataFunction LoadImageData, void *user_data);
|
||||
|
||||
private:
|
||||
///
|
||||
/// Loads glTF asset from string(memory).
|
||||
@ -801,6 +817,14 @@ class TinyGLTF {
|
||||
const unsigned char *bin_data_;
|
||||
size_t bin_size_;
|
||||
bool is_binary_;
|
||||
|
||||
LoadImageDataFunction LoadImageData =
|
||||
#ifndef TINYGLTF_NO_STB_IMAGE
|
||||
&tinygltf::LoadImageData;
|
||||
#else
|
||||
nullptr;
|
||||
#endif
|
||||
void *load_image_user_data_ = nullptr;
|
||||
};
|
||||
|
||||
#ifdef __clang__
|
||||
@ -847,7 +871,11 @@ class TinyGLTF {
|
||||
#endif
|
||||
|
||||
#include "./json.hpp"
|
||||
|
||||
#ifndef TINYGLTF_NO_STB_IMAGE
|
||||
#include "./stb_image.h"
|
||||
#endif
|
||||
|
||||
#ifdef __clang__
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
@ -1159,11 +1187,15 @@ static bool LoadExternalFile(std::vector<unsigned char> *out, std::string *err,
|
||||
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,
|
||||
int req_height, const unsigned char *bytes,
|
||||
int size) {
|
||||
//std::cout << "size " << size << std::endl;
|
||||
|
||||
int size, void*) {
|
||||
int w, h, comp;
|
||||
// if image cannot be decoded, ignore parsing and keep it by its path
|
||||
// don't break in this case
|
||||
@ -1217,6 +1249,7 @@ static bool LoadImageData(Image *image, std::string *err, int req_width,
|
||||
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
static bool IsDataURI(const std::string &in) {
|
||||
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,
|
||||
const json &o, const std::string &basedir,
|
||||
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
|
||||
double bufferView = -1;
|
||||
bool isEmbedded =
|
||||
@ -1703,8 +1737,14 @@ static bool ParseImage(Image *image, std::string *err,
|
||||
}
|
||||
}
|
||||
|
||||
return LoadImageData(image, err, 0, 0, &img.at(0),
|
||||
static_cast<int>(img.size()));
|
||||
if (*LoadImageData == nullptr) {
|
||||
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,
|
||||
@ -2877,7 +2917,7 @@ bool TinyGLTF::LoadFromString(Model *model, std::string *err, const char *str,
|
||||
}
|
||||
Image image;
|
||||
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;
|
||||
}
|
||||
|
||||
@ -2897,9 +2937,15 @@ bool TinyGLTF::LoadFromString(Model *model, std::string *err, const char *str,
|
||||
model->bufferViews[size_t(image.bufferView)];
|
||||
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,
|
||||
&buffer.data[bufferView.byteOffset],
|
||||
static_cast<int>(bufferView.byteLength));
|
||||
static_cast<int>(bufferView.byteLength), load_image_user_data_);
|
||||
if (!ret) {
|
||||
return false;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user