Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow WriteImageDataFunction() callback to be called with empty images #496

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion tests/tester.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1199,4 +1199,51 @@ TEST_CASE("inverse-bind-matrices-optional", "[issue-492]") {

REQUIRE(true == ret);
REQUIRE(err.empty());
}
}

bool LoadImageData(tinygltf::Image * /* image */, const int /* image_idx */, std::string * /* err */,
std::string * /* warn */, int /* req_width */, int /* req_height */,
const unsigned char * /* bytes */, int /* size */, void * /*user_data */) {
return true;
}

bool WriteImageData(const std::string * /* basepath */, const std::string * /* filename */,
const tinygltf::Image *image, bool /* embedImages */,
const tinygltf::FsCallbacks * /* fs_cb */, const tinygltf::URICallbacks * /* uri_cb */,
std::string * /* out_uri */, void * user_pointer) {
REQUIRE(user_pointer != nullptr);
auto counter = static_cast<int*>(user_pointer);
*counter = *counter + 1;
return true;
}

TEST_CASE("empty-images-not-written", "[issue-495]") {
std::string err;
std::string warn;
tinygltf::Model model;
tinygltf::TinyGLTF ctx;

ctx.SetImageLoader(LoadImageData, nullptr);
bool ok = ctx.LoadASCIIFromFile(&model, &err, &warn, "../models/Cube/Cube.gltf");
REQUIRE(ok);
REQUIRE(err.empty());
REQUIRE(warn.empty());

CHECK(model.images.size() == 2);
for (const auto& image : model.images) {
// No data loaded or decoded
CHECK(image.image.empty());
// The URI is kept
CHECK_FALSE(image.uri.empty());
// The URI should not be a data URI
CHECK(image.uri.find("data:") != 0);
}

// Now write the loaded model
int counter = 0;
ctx.SetImageWriter(WriteImageData, &counter);
ok = ctx.WriteGltfSceneToFile(&model, "issue-495-external.gltf");
CHECK(ok);
// WriteImageData should be invoked for both images
CHECK(counter == 2);
}
18 changes: 13 additions & 5 deletions tiny_gltf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this we keep the previous behaviour, although the callback is now invoked for empty images.

*out_uri = *filename;
return true;
}

const std::string ext = GetFilePathExtension(*filename);

// Write image to temporary buffer
Expand Down Expand Up @@ -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()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Invoke the callback for empty images

imageWritten = WriteImageData(&baseDir, &filename, &image, embedImages,
fs_cb, uri_cb, out_uri, user_data);
if (!imageWritten) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Loading