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

Save images to PNG files #323

Open
Alexeyt89 opened this issue Apr 30, 2021 · 2 comments
Open

Save images to PNG files #323

Alexeyt89 opened this issue Apr 30, 2021 · 2 comments

Comments

@Alexeyt89
Copy link

Alexeyt89 commented Apr 30, 2021

Issue description
I'm implementing glTF exporter with using tinygltf. I need to make tinygltf write each image to separate png files.
On exporting models png files are not created.

To Reproduce
I include tinygltf this way:

#define TINYGLTF_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "tiny_gltf.h"

Then write all needed data to tinygltf::Image:

mimeType = "image/png"
name = "test"
uri = "test.png"
width = 512
height = 512
write the image data to image.image
bits = 8
component = 4
pixel_type = TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT

To save models I call WriteGhtfSceneToFile with these parameters:

embedeImages = false;
embedeBuffers = false;
prettyPrint = true;
writeBinary = false;

As a result I see 2 files (png files are not created):

test.gltf
test.bin

On exporting I have this call stack:

  1. tinygltf::TinyGLTF::WriteGhtfSceneToFile
  2. tinygltf::UpdateImageObject
  3. tinygltyf::WriteImageData <- here it fails to write png files because fsPtr is nullptr

fsPtr is nullptr because this->write_image_user_data_ is nullptr.
Is there a description or an example how png files can be created?

@syoyo
Copy link
Owner

syoyo commented May 1, 2021

Writing images to a file(i.e. do not embed images) is not well documented and supported.
Currently you'll need to set the pointer to FsCallback as a user_data through SetImageWriter()

There is a room for improving implementation(Use FsCallback from SetFsCallbacks) and documentation.
Contribution is welcome!

@Alexeyt89
Copy link
Author

Alexeyt89 commented May 4, 2021

If somebody needs it, I set it this way:

tinygltf::TinyGLTF gltf_ctx;
tinygltf::WriteImageDataFunction WriteImageData = &tinygltf::WriteImageData;
tinygltf::FsCallbacks fs = {
&tinygltf::FileExists, &tinygltf::ExpandFilePath,
&tinygltf::ReadWholeFile, &tinygltf::WriteWholeFile,
nullptr
};
gltf_ctx.SetImageWriter(WriteImageData, &fs);

I tested it with PNG, JPG and BMP. The only visible problem is in bmp semi-transparent textures are purple.

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

No branches or pull requests

2 participants