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

Add ability to disable image loading #494

Closed

Conversation

ptc-tgamper
Copy link
Contributor

@ptc-tgamper ptc-tgamper commented Jul 5, 2024

Hi. This is a PR which allows to completely disable image loading. This is important if one has glTF models with gigabytes of external images, or a huge GLB where most of the data is images.

Image saving is still correctly handled, by looking for the source model's image files, and copying them over to the directory where the target glTF is written.

@@ -1213,6 +1213,9 @@ class Model {

bool operator==(const Model &) const;

// The base directory of the glTF model file
std::string baseDir;
Copy link
Contributor Author

@ptc-tgamper ptc-tgamper Jul 5, 2024

Choose a reason for hiding this comment

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

This is needed to find the model's image files

Question is if operator== for Model should honor this or not? Compare only the contents of the model, or also where it was loaded from?

tiny_gltf.h Show resolved Hide resolved
std::string decoded_uri;
if (!image.uri.empty()) {
// If image has uri, use it as a filename if it is not a data uri
if (!IsDataURI(image.uri)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need to handle data URI here, as we leave images with data URIs as is, instead of loading the data and clearing their URI.

bool sourceIsTarget = false;
if (!decoded_uri.empty() && imageToWrite.image.empty()) {
std::string sourceFilePath;
if (LoadExternalFile(&imageToWrite.image, &sourceFilePath, nullptr, nullptr, decoded_uri, sourceBaseDir,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Load the source image, and if able to do so write it to target directory.

@@ -2522,6 +2586,10 @@ static bool LoadExternalFile(std::vector<unsigned char> *out, std::string *err,
return false;
}

if (foundFile) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Return the filepath that was actually found

@syoyo
Copy link
Owner

syoyo commented Jul 5, 2024

I recommend to use your own image load/save handler to control image loading for a while.

@ptc-tgamper
Copy link
Contributor Author

ptc-tgamper commented Jul 5, 2024

I tried that, but having my own image loader and writer is not sufficient to solve the problem. Loading works with my own image loader, but writing doesn't. Images that do not hold any data are not passed to the image writer routine. Moreover, images that use data URIs are also not possible to handle, because their URIs are wiped outside of the image loader, so one loses their data. Only images that have their data in buffers are handled correctly.

I can try to come up with a minimal changeset to fix those deficiencies, so that I can skip image loading, if I use custom image loaders and writers.

@syoyo
Copy link
Owner

syoyo commented Jul 6, 2024

You are better first file a github issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants