Skip to content

Commit

Permalink
Merge pull request #676 from zeux/gltf-imgerr
Browse files Browse the repository at this point in the history
gltfpack: Replace images that can't be found or encoded with invalid URI
  • Loading branch information
zeux authored Apr 10, 2024
2 parents b936f0a + e20baef commit 78567c8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
4 changes: 2 additions & 2 deletions gltf/gltfpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,9 @@ static void process(cgltf_data* data, const char* input_path, const char* output
if (encoded_images.size() && !encoded_images[i].empty())
{
if (encoded_images[i].compare(0, 5, "error") == 0)
fprintf(stderr, "Warning: unable to encode image %d (%s), skipping (%s)\n", int(i), image.uri ? image.uri : "?", encoded_images[i].c_str());
writeImageError(json_images, "encode", int(i), image.uri, encoded_images[i].c_str());
else
writeEncodedImage(json_images, views, image, encoded_images[i], images[i], output_path, settings);
writeEncodedImage(json_images, views, image, encoded_images[i], images[i], i, output_path, settings);

encoded_images[i] = std::string(); // reclaim memory early
}
Expand Down
3 changes: 2 additions & 1 deletion gltf/gltfpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,8 @@ void writeMaterial(std::string& json, const cgltf_data* data, const cgltf_materi
void writeBufferView(std::string& json, BufferView::Kind kind, StreamFormat::Filter filter, size_t count, size_t stride, size_t bin_offset, size_t bin_size, BufferView::Compression compression, size_t compressed_offset, size_t compressed_size);
void writeSampler(std::string& json, const cgltf_sampler& sampler);
void writeImage(std::string& json, std::vector<BufferView>& views, const cgltf_image& image, const ImageInfo& info, size_t index, const char* input_path, const char* output_path, const Settings& settings);
void writeEncodedImage(std::string& json, std::vector<BufferView>& views, const cgltf_image& image, const std::string& encoded, const ImageInfo& info, const char* output_path, const Settings& settings);
void writeImageError(std::string& json, const char* action, size_t index, const char* uri, const char* reason = NULL);
void writeEncodedImage(std::string& json, std::vector<BufferView>& views, const cgltf_image& image, const std::string& encoded, const ImageInfo& info, size_t index, const char* output_path, const Settings& settings);
void writeTexture(std::string& json, const cgltf_texture& texture, const ImageInfo* info, cgltf_data* data, const Settings& settings);
void writeMeshAttributes(std::string& json, std::vector<BufferView>& views, std::string& json_accessors, size_t& accr_offset, const Mesh& mesh, int target, const QuantizationPosition& qp, const QuantizationTexture& qt, const Settings& settings);
size_t writeMeshIndices(std::vector<BufferView>& views, std::string& json_accessors, size_t& accr_offset, const Mesh& mesh, const Settings& settings);
Expand Down
21 changes: 15 additions & 6 deletions gltf/write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ void writeSampler(std::string& json, const cgltf_sampler& sampler)
}
}

static void writeImageData(std::string& json, std::vector<BufferView>& views, const char* uri, const char* mime_type, const std::string& contents, const char* output_path, TextureKind kind, bool embed)
static void writeImageData(std::string& json, std::vector<BufferView>& views, size_t index, const char* uri, const char* mime_type, const std::string& contents, const char* output_path, TextureKind kind, bool embed)
{
bool dataUri = uri && strncmp(uri, "data:", 5) == 0;

Expand All @@ -913,7 +913,7 @@ static void writeImageData(std::string& json, std::vector<BufferView>& views, co
}
else
{
fprintf(stderr, "Warning: unable to save image %s to %s, skipping\n", uri, file_path.c_str());
writeImageError(json, "save", int(index), uri, file_path.c_str());
}
}
else
Expand All @@ -939,16 +939,25 @@ void writeImage(std::string& json, std::vector<BufferView>& views, const cgltf_i
std::string mime_type;
if (!readImage(image, input_path, img_data, mime_type))
{
fprintf(stderr, "Warning: unable to read image %d (%s), skipping\n", int(index), image.uri ? image.uri : "?");
writeImageError(json, "read", index, image.uri);
return;
}

writeImageData(json, views, image.uri, mime_type.c_str(), img_data, output_path, info.kind, settings.texture_embed);
writeImageData(json, views, index, image.uri, mime_type.c_str(), img_data, output_path, info.kind, settings.texture_embed);
}

void writeEncodedImage(std::string& json, std::vector<BufferView>& views, const cgltf_image& image, const std::string& encoded, const ImageInfo& info, const char* output_path, const Settings& settings)
void writeImageError(std::string& json, const char* action, size_t index, const char* uri, const char* reason)
{
writeImageData(json, views, image.uri, "image/ktx2", encoded, output_path, info.kind, settings.texture_embed);
append(json, "\"uri\":\"");
append(json, "data:image/png;base64,ERR/");
append(json, "\"");

fprintf(stderr, "Warning: unable to %s image %d (%s), skipping%s%s%s\n", action, int(index), uri ? uri : "embedded", reason ? " (" : "", reason ? reason : "", reason ? ")" : "");
}

void writeEncodedImage(std::string& json, std::vector<BufferView>& views, const cgltf_image& image, const std::string& encoded, const ImageInfo& info, size_t index, const char* output_path, const Settings& settings)
{
writeImageData(json, views, index, image.uri, "image/ktx2", encoded, output_path, info.kind, settings.texture_embed);
}

void writeTexture(std::string& json, const cgltf_texture& texture, const ImageInfo* info, cgltf_data* data, const Settings& settings)
Expand Down

0 comments on commit 78567c8

Please sign in to comment.