Fixed wrong metadata in case 16bit image wasn't actually loaded

The fallback to 8 bit would have been broken.
This commit is contained in:
Arthur Brainville (Ybalrid) 2019-03-02 22:03:34 +01:00
parent f2addc0e44
commit 5a4c898912

View File

@ -1635,14 +1635,14 @@ void TinyGLTF::SetImageLoader(LoadImageDataFunction func, void *user_data) {
}
#ifndef TINYGLTF_NO_STB_IMAGE
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 *user_data) {
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 *user_data) {
(void)warn;
int w, h, comp, req_comp;
unsigned char* data = nullptr;
unsigned char *data = nullptr;
// force 32-bit textures for common Vulkan compatibility. It appears that
// some GPU drivers do not support 24-bit images for Vulkan
@ -1650,19 +1650,20 @@ bool LoadImageData(Image *image, const int image_idx, std::string *err, std::str
int bits = 8;
int pixel_type = TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE;
// It is possible that the image we want to load is a 16bit per channel image
// We are going to attempt to load it as 16bit per channel, and if it worked,
// set the image data accodingly. We are casting the returned pointer into
// unsigned char, because we are representing "bytes". But we are updating
// the Image metadata to signal that this image uses 2 bytes (16bits) per
// channel:
if(stbi_is_16_bit_from_memory(bytes, size))
{
data = (unsigned char*)stbi_load_16_from_memory(bytes, size, &w, &h, &comp, req_comp);
if (stbi_is_16_bit_from_memory(bytes, size)) {
data = (unsigned char *)stbi_load_16_from_memory(bytes, size, &w, &h, &comp,
req_comp);
if (data) {
bits = 16;
pixel_type = TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT;
}
}
// at this point, if data is still NULL, it means that the image wasn't
// 16bit per channel, we are going to load it as a normal 8bit per channel
@ -1673,12 +1674,13 @@ bool LoadImageData(Image *image, const int image_idx, std::string *err, std::str
// image->uri references
// an image file, it should be left as it is. Image loading should not be
// mandatory (to support other formats)
if(!data) data =
stbi_load_from_memory(bytes, size, &w, &h, &comp, req_comp);
if (!data) data = stbi_load_from_memory(bytes, size, &w, &h, &comp, req_comp);
if (!data) {
// NOTE: you can use `warn` instead of `err`
if (err) {
(*err) += "Unknown image format. STB cannot decode image data for image[" + std::to_string(image_idx) + "] name = \"" + image->name + "\".\n";
(*err) +=
"Unknown image format. STB cannot decode image data for image[" +
std::to_string(image_idx) + "] name = \"" + image->name + "\".\n";
}
return false;
}
@ -1686,7 +1688,8 @@ bool LoadImageData(Image *image, const int image_idx, std::string *err, std::str
if (w < 1 || h < 1) {
stbi_image_free(data);
if (err) {
(*err) += "Invalid image data for image[" + std::to_string(image_idx) + "] name = \"" + image->name + "\"\n";
(*err) += "Invalid image data for image[" + std::to_string(image_idx) +
"] name = \"" + image->name + "\"\n";
}
return false;
}
@ -1695,7 +1698,9 @@ bool LoadImageData(Image *image, const int image_idx, std::string *err, std::str
if (req_width != w) {
stbi_image_free(data);
if (err) {
(*err) += "Image width mismatch for image[" + std::to_string(image_idx) + "] name = \"" + image->name + "\"\n";
(*err) += "Image width mismatch for image[" +
std::to_string(image_idx) + "] name = \"" + image->name +
"\"\n";
}
return false;
}
@ -1705,7 +1710,9 @@ bool LoadImageData(Image *image, const int image_idx, std::string *err, std::str
if (req_height != h) {
stbi_image_free(data);
if (err) {
(*err) += "Image height mismatch. for image[" + std::to_string(image_idx) + "] name = \"" + image->name + "\"\n";
(*err) += "Image height mismatch. for image[" +
std::to_string(image_idx) + "] name = \"" + image->name +
"\"\n";
}
return false;
}
@ -1716,8 +1723,8 @@ bool LoadImageData(Image *image, const int image_idx, std::string *err, std::str
image->component = req_comp;
image->bits = bits;
image->pixel_type = pixel_type;
image->image.resize(static_cast<size_t>(w * h * req_comp) * (bits/8));
std::copy(data, data + w * h * req_comp * (bits/8), image->image.begin());
image->image.resize(static_cast<size_t>(w * h * req_comp) * (bits / 8));
std::copy(data, data + w * h * req_comp * (bits / 8), image->image.begin());
stbi_image_free(data);
return true;