Fix issues that block custom image loaders and writers to deal with empty images
This commit is contained in:
Thomas Gamper
2024-07-09 10:30:12 +02:00
parent fea6786129
commit fe3cfbe996
2 changed files with 61 additions and 6 deletions

View File

@@ -2767,6 +2767,12 @@ bool WriteImageData(const std::string *basepath, const std::string *filename,
const Image *image, bool embedImages,
const FsCallbacks* fs_cb, const URICallbacks *uri_cb,
std::string *out_uri, void *) {
// Early out on empty images, report the original uri if the image was not written.
if (image->image.empty()) {
*out_uri = *filename;
return true;
}
const std::string ext = GetFilePathExtension(*filename);
// Write image to temporary buffer
@@ -3309,11 +3315,12 @@ static bool UpdateImageObject(const Image &image, std::string &baseDir,
filename = std::to_string(index) + "." + ext;
}
// 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.
// If callback is set, modify image data object.
// Note that the callback is also invoked for images without data.
// The default callback implementation simply returns true for
// empty images and sets the out URI to filename.
bool imageWritten = false;
if (WriteImageData != nullptr && !filename.empty() && !image.image.empty()) {
if (WriteImageData != nullptr && !filename.empty()) {
imageWritten = WriteImageData(&baseDir, &filename, &image, embedImages,
fs_cb, uri_cb, out_uri, user_data);
if (!imageWritten) {
@@ -4387,7 +4394,7 @@ static bool ParseImage(Image *image, const int image_idx, std::string *err,
}
} else {
// Assume external file
// Keep texture path (for textures that cannot be decoded)
// Unconditionally keep the external URI of the image
image->uri = uri;
#ifdef TINYGLTF_NO_EXTERNAL_IMAGE
return true;
@@ -4434,6 +4441,7 @@ static bool ParseImage(Image *image, const int image_idx, std::string *err,
}
return false;
}
return LoadImageData(image, image_idx, err, warn, 0, 0, &img.at(0),
static_cast<int>(img.size()), load_image_user_data);
}