Skip to content

Commit

Permalink
fixes #473
Browse files Browse the repository at this point in the history
tiny_gltf.h - explicitly pass filesystem callbacks to image related functions
tester.cc - add respective test case, fix image uri test case
  • Loading branch information
ptc-tgamper committed Jun 25, 2024
1 parent 847df84 commit 3245906
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 47 deletions.
40 changes: 35 additions & 5 deletions tests/tester.cc
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ TEST_CASE("image-uri-spaces", "[issue-236]") {
}
REQUIRE(true == ret);
REQUIRE(err.empty());
REQUIRE(!warn.empty()); // relative image path won't exist in tests/
REQUIRE(warn.empty());
REQUIRE(saved.images.size() == model.images.size());

// The image uri in CubeImageUriMultipleSpaces.gltf is not encoded and
Expand Down Expand Up @@ -662,10 +662,11 @@ TEST_CASE("serialize-image-callback", "[issue-394]") {

auto writer = [](const std::string *basepath, const std::string *filename,
const tinygltf::Image *image, bool embedImages,
const tinygltf::URICallbacks *uri_cb, std::string *out_uri,
void *user_pointer) -> bool {
const tinygltf::FsCallbacks* fs, const tinygltf::URICallbacks *uri_cb,
std::string *out_uri, void *user_pointer) -> bool {
(void)basepath;
(void)image;
(void)fs;
(void)uri_cb;
REQUIRE(*filename == "foo");
REQUIRE(embedImages == true);
Expand Down Expand Up @@ -699,12 +700,13 @@ TEST_CASE("serialize-image-failure", "[issue-394]") {

auto writer = [](const std::string *basepath, const std::string *filename,
const tinygltf::Image *image, bool embedImages,
const tinygltf::URICallbacks *uri_cb, std::string *out_uri,
void *user_pointer) -> bool {
const tinygltf::FsCallbacks* fs, const tinygltf::URICallbacks *uri_cb,
std::string *out_uri, void *user_pointer) -> bool {
(void)basepath;
(void)filename;
(void)image;
(void)embedImages;
(void)fs;
(void)uri_cb;
(void)out_uri;
(void)user_pointer;
Expand Down Expand Up @@ -1056,3 +1058,31 @@ TEST_CASE("serialize-lods", "[lods]") {
CHECK(nodeWithoutLods.extensions.count("MSFT_lod") == 0);
}
}

TEST_CASE("write-image-issue", "[issue-473]") {

tinygltf::Model model;
tinygltf::TinyGLTF ctx;
std::string err;
std::string warn;
bool ok = ctx.LoadASCIIFromFile(&model, &err, &warn, "../models/Cube/Cube.gltf");
REQUIRE(ok);
REQUIRE(err.empty());
REQUIRE(warn.empty());

REQUIRE(model.images.size() == 2);
REQUIRE(model.images[0].uri == "Cube_BaseColor.png");
REQUIRE(model.images[1].uri == "Cube_MetallicRoughness.png");

REQUIRE_FALSE(model.images[0].image.empty());
REQUIRE_FALSE(model.images[1].image.empty());

ok = ctx.WriteGltfSceneToFile(&model, "Cube.gltf", false, true);
REQUIRE(ok);

for (const auto& image : model.images)
{
std::fstream file(image.uri);
CHECK(file.good());
}
}
85 changes: 43 additions & 42 deletions tiny_gltf.h
Original file line number Diff line number Diff line change
Expand Up @@ -1292,39 +1292,6 @@ struct URICallbacks {
void *user_data; // An argument that is passed to all uri callbacks
};

///
/// LoadImageDataFunction type. Signature for custom image loading callbacks.
///
using LoadImageDataFunction = std::function<bool(
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 */)>;

///
/// WriteImageDataFunction type. Signature for custom image writing callbacks.
/// The out_uri parameter becomes the URI written to the gltf and may reference
/// a file or contain a data URI.
///
using WriteImageDataFunction = std::function<bool(
const std::string * /* basepath */, const std::string * /* filename */,
const Image *image, bool /* embedImages */,
const URICallbacks * /* uri_cb */, std::string * /* out_uri */,
void * /* user_pointer */)>;

#ifndef TINYGLTF_NO_STB_IMAGE
// Declaration of default image loader callback
bool LoadImageData(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 *);
#endif

#ifndef TINYGLTF_NO_STB_IMAGE_WRITE
// Declaration of default image writer callback
bool WriteImageData(const std::string *basepath, const std::string *filename,
const Image *image, bool embedImages,
const URICallbacks *uri_cb, std::string *out_uri, void *);
#endif

///
/// FileExistsFunction type. Signature for custom filesystem callbacks.
///
Expand Down Expand Up @@ -1396,6 +1363,40 @@ bool GetFileSizeInBytes(size_t *filesize_out, std::string *err,
const std::string &filepath, void *);
#endif

///
/// LoadImageDataFunction type. Signature for custom image loading callbacks.
///
using LoadImageDataFunction = std::function<bool(
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 */)>;

///
/// WriteImageDataFunction type. Signature for custom image writing callbacks.
/// The out_uri parameter becomes the URI written to the gltf and may reference
/// a file or contain a data URI.
///
using WriteImageDataFunction = std::function<bool(
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 * /* user_pointer */)>;

#ifndef TINYGLTF_NO_STB_IMAGE
// Declaration of default image loader callback
bool LoadImageData(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 *);
#endif

#ifndef TINYGLTF_NO_STB_IMAGE_WRITE
// Declaration of default image writer callback
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 *);
#endif

///
/// glTF Parser/Serializer context.
///
Expand Down Expand Up @@ -2725,8 +2726,8 @@ static void WriteToMemory_stbi(void *context, void *data, int size) {

bool WriteImageData(const std::string *basepath, const std::string *filename,
const Image *image, bool embedImages,
const URICallbacks *uri_cb, std::string *out_uri,
void *fsPtr) {
const FsCallbacks* fs_cb, const URICallbacks *uri_cb,
std::string *out_uri, void *) {
const std::string ext = GetFilePathExtension(*filename);

// Write image to temporary buffer
Expand Down Expand Up @@ -2775,12 +2776,11 @@ bool WriteImageData(const std::string *basepath, const std::string *filename,
}
} else {
// Write image to disc
FsCallbacks *fs = reinterpret_cast<FsCallbacks *>(fsPtr);
if ((fs != nullptr) && (fs->WriteWholeFile != nullptr)) {
if ((fs_cb != nullptr) && (fs_cb->WriteWholeFile != nullptr)) {
const std::string imagefilepath = JoinPath(*basepath, *filename);
std::string writeError;
if (!fs->WriteWholeFile(&writeError, imagefilepath, data,
fs->user_data)) {
if (!fs_cb->WriteWholeFile(&writeError, imagefilepath, data,
fs_cb->user_data)) {
// Could not write image file to disc; Throw error ?
return false;
}
Expand Down Expand Up @@ -3233,6 +3233,7 @@ static std::string MimeToExt(const std::string &mimeType) {

static bool UpdateImageObject(const Image &image, std::string &baseDir,
int index, bool embedImages,
const FsCallbacks *fs_cb,
const URICallbacks *uri_cb,
const WriteImageDataFunction& WriteImageData,
void *user_data, std::string *out_uri) {
Expand Down Expand Up @@ -3266,7 +3267,7 @@ static bool UpdateImageObject(const Image &image, std::string &baseDir,
bool imageWritten = false;
if (WriteImageData != nullptr && !filename.empty() && !image.image.empty()) {
imageWritten = WriteImageData(&baseDir, &filename, &image, embedImages,
uri_cb, out_uri, user_data);
fs_cb, uri_cb, out_uri, user_data);
if (!imageWritten) {
return false;
}
Expand Down Expand Up @@ -8547,7 +8548,7 @@ bool TinyGLTF::WriteGltfSceneToStream(const Model *model, std::ostream &stream,
// we
std::string uri;
if (!UpdateImageObject(model->images[i], dummystring, int(i), true,
&uri_cb, this->WriteImageData,
&fs, &uri_cb, this->WriteImageData,
this->write_image_user_data_, &uri)) {
return false;
}
Expand Down Expand Up @@ -8655,7 +8656,7 @@ bool TinyGLTF::WriteGltfSceneToFile(const Model *model,

std::string uri;
if (!UpdateImageObject(model->images[i], baseDir, int(i), embedImages,
&uri_cb, this->WriteImageData,
&fs, &uri_cb, this->WriteImageData,
this->write_image_user_data_, &uri)) {
return false;
}
Expand Down

0 comments on commit 3245906

Please sign in to comment.