mirror of
https://git.mirrors.martin98.com/https://github.com/syoyo/tinygltf.git
synced 2025-07-06 04:45:12 +08:00
Merge pull request #396 from pknowlesnv/image_write_failure
propagate image writing failures
This commit is contained in:
commit
03bbe0921c
@ -593,13 +593,38 @@ TEST_CASE("serialize-image-callback", "[issue-394]") {
|
|||||||
|
|
||||||
tinygltf::TinyGLTF ctx;
|
tinygltf::TinyGLTF ctx;
|
||||||
ctx.SetImageWriter(writer, (void *)0xba5e1e55);
|
ctx.SetImageWriter(writer, (void *)0xba5e1e55);
|
||||||
ctx.WriteGltfSceneToStream(const_cast<const tinygltf::Model *>(&m), os, false,
|
bool result = ctx.WriteGltfSceneToStream(const_cast<const tinygltf::Model *>(&m), os, false,
|
||||||
false);
|
false);
|
||||||
|
|
||||||
// use nlohmann json
|
// use nlohmann json
|
||||||
nlohmann::json j = nlohmann::json::parse(os.str());
|
nlohmann::json j = nlohmann::json::parse(os.str());
|
||||||
|
|
||||||
|
REQUIRE(true == result);
|
||||||
REQUIRE(1 == j["images"].size());
|
REQUIRE(1 == j["images"].size());
|
||||||
REQUIRE(j["images"][0].is_object());
|
REQUIRE(j["images"][0].is_object());
|
||||||
REQUIRE(j["images"][0]["uri"].get<std::string>() == "bar");
|
REQUIRE(j["images"][0]["uri"].get<std::string>() == "bar");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("serialize-image-failure", "[issue-394]") {
|
||||||
|
tinygltf::Model m;
|
||||||
|
tinygltf::Image i;
|
||||||
|
// Set some data so the ImageWriter callback will be called
|
||||||
|
i.image = {255, 255, 255, 255};
|
||||||
|
m.images.push_back(i);
|
||||||
|
|
||||||
|
std::stringstream os;
|
||||||
|
|
||||||
|
auto writer = [](const std::string *basepath, const std::string *filename,
|
||||||
|
const tinygltf::Image *image, bool embedImages,
|
||||||
|
std::string *out_uri, void *user_pointer) -> bool {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
tinygltf::TinyGLTF ctx;
|
||||||
|
ctx.SetImageWriter(writer, (void *)0xba5e1e55);
|
||||||
|
bool result = ctx.WriteGltfSceneToStream(const_cast<const tinygltf::Model *>(&m), os, false,
|
||||||
|
false);
|
||||||
|
|
||||||
|
REQUIRE(false == result);
|
||||||
|
REQUIRE(os.str().size() == 0);
|
||||||
|
}
|
||||||
|
27
tiny_gltf.h
27
tiny_gltf.h
@ -2834,7 +2834,7 @@ static std::string MimeToExt(const std::string &mimeType) {
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
static void UpdateImageObject(const Image &image, std::string &baseDir,
|
static bool UpdateImageObject(const Image &image, std::string &baseDir,
|
||||||
int index, bool embedImages,
|
int index, bool embedImages,
|
||||||
WriteImageDataFunction *WriteImageData,
|
WriteImageDataFunction *WriteImageData,
|
||||||
std::string *out_uri, void *user_data) {
|
std::string *out_uri, void *user_data) {
|
||||||
@ -2857,17 +2857,24 @@ static void UpdateImageObject(const Image &image, std::string &baseDir,
|
|||||||
filename = std::to_string(index) + "." + ext;
|
filename = std::to_string(index) + "." + ext;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If callback is set, modify image data object
|
// If callback is set and image data exists, modify image data object. If
|
||||||
|
// image data does not exist, this is not considered a failure and the
|
||||||
|
// original uri should be maintained.
|
||||||
bool imageWritten = false;
|
bool imageWritten = false;
|
||||||
if (*WriteImageData != nullptr && !filename.empty()) {
|
if (*WriteImageData != nullptr && !filename.empty() && !image.image.empty()) {
|
||||||
imageWritten = (*WriteImageData)(&baseDir, &filename, &image, embedImages,
|
imageWritten = (*WriteImageData)(&baseDir, &filename, &image, embedImages,
|
||||||
out_uri, user_data);
|
out_uri, user_data);
|
||||||
|
if (!imageWritten) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use the original uri if the image was not written.
|
// Use the original uri if the image was not written.
|
||||||
if (!imageWritten) {
|
if (!imageWritten) {
|
||||||
*out_uri = image.uri;
|
*out_uri = image.uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IsDataURI(const std::string &in) {
|
bool IsDataURI(const std::string &in) {
|
||||||
@ -7016,7 +7023,7 @@ static void SerializeGltfBufferView(const BufferView &bufferView, json &o) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void SerializeGltfImage(const Image &image, const std::string uri,
|
static void SerializeGltfImage(const Image &image, const std::string &uri,
|
||||||
json &o) {
|
json &o) {
|
||||||
// From 2.7.0, we look for `uri` parameter, not `Image.uri`
|
// From 2.7.0, we look for `uri` parameter, not `Image.uri`
|
||||||
// if uri is empty, the mimeType and bufferview should be set
|
// if uri is empty, the mimeType and bufferview should be set
|
||||||
@ -7817,9 +7824,11 @@ bool TinyGLTF::WriteGltfSceneToStream(const Model *model, std::ostream &stream,
|
|||||||
// enabled, since we won't write separate images when writing to a stream
|
// enabled, since we won't write separate images when writing to a stream
|
||||||
// we
|
// we
|
||||||
std::string uri;
|
std::string uri;
|
||||||
UpdateImageObject(model->images[i], dummystring, int(i), true,
|
if (!UpdateImageObject(model->images[i], dummystring, int(i), true,
|
||||||
&this->WriteImageData, &uri,
|
&this->WriteImageData, &uri,
|
||||||
this->write_image_user_data_);
|
this->write_image_user_data_)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
SerializeGltfImage(model->images[i], uri, image);
|
SerializeGltfImage(model->images[i], uri, image);
|
||||||
JsonPushBack(images, std::move(image));
|
JsonPushBack(images, std::move(image));
|
||||||
}
|
}
|
||||||
@ -7908,9 +7917,11 @@ bool TinyGLTF::WriteGltfSceneToFile(const Model *model,
|
|||||||
json image;
|
json image;
|
||||||
|
|
||||||
std::string uri;
|
std::string uri;
|
||||||
UpdateImageObject(model->images[i], baseDir, int(i), embedImages,
|
if (!UpdateImageObject(model->images[i], baseDir, int(i), embedImages,
|
||||||
&this->WriteImageData, &uri,
|
&this->WriteImageData, &uri,
|
||||||
this->write_image_user_data_);
|
this->write_image_user_data_)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
SerializeGltfImage(model->images[i], uri, image);
|
SerializeGltfImage(model->images[i], uri, image);
|
||||||
JsonPushBack(images, std::move(image));
|
JsonPushBack(images, std::move(image));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user