Skip to content

Commit

Permalink
[Tydra] Support loading 16bit and 32bit images.
Browse files Browse the repository at this point in the history
  • Loading branch information
syoyo committed Apr 17, 2024
1 parent f847db1 commit eeac474
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 13 deletions.
7 changes: 6 additions & 1 deletion src/image-loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,17 @@ bool GetImageInfoSTB(const uint8_t *bytes, const size_t size,
bool DecodeImageEXR(const uint8_t *bytes, const size_t size,
const std::string &uri, Image *image,
std::string *err) {
// TODO(syoyo): Multi-channel EXR
// TODO(syoyo):
// - [ ] Read fp16 image as fp16
// - [ ] Read int16 image as int16
// - [ ] Read int32 image as int32
// - [ ] Multi-channel EXR

float *rgba = nullptr;
int width;
int height;
const char *exrerr = nullptr;
// LoadEXRFromMemory always load EXR image as fp32 x RGBA
int ret = LoadEXRFromMemory(&rgba, &width, &height, bytes, size, &exrerr);

if (exrerr) {
Expand Down
21 changes: 17 additions & 4 deletions src/image-types.hh
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ namespace tinyusdz {
// No colorspace conversion will be applied when decoding image data
// (e.g. from .jpg, .png).
struct Image {
// NOTE: Actual pixel value format is determined with combination of PixelFormat x bpp
// e.g. Float + 16 bpp = fp16
enum class PixelFormat {
UInt, // LDR and HDR image
Int, // For normal/displacement map
Float // HDR image
// TODO
// Half
Int, // For ao/normal/displacement map, DNG photo
Float, // HDR image
};

std::string uri; // filename or uri;
Expand All @@ -30,6 +30,19 @@ struct Image {
PixelFormat format{PixelFormat::UInt};

std::vector<uint8_t> data; // Raw data.

std::string colorspace; // Colorspace metadata in the image. Optional.
};

inline std::string to_string(Image::PixelFormat fmt) {
std::string s{"[[InvalidPixelFormat]]"};
switch (fmt) {
case Image::PixelFormat::UInt: { s = "uint"; break; }
case Image::PixelFormat::Int: { s = "int"; break; }
case Image::PixelFormat::Float: { s = "float"; break; }
}

return s;
}

} // namespace tinyusdz
48 changes: 40 additions & 8 deletions src/tydra/render-data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "image-loader.hh"
#include "image-util.hh"
#include "image-types.hh"
#include "linear-algebra.hh"
#include "math-util.inc"
#include "pprinter.hh"
Expand Down Expand Up @@ -4117,18 +4118,19 @@ bool RenderSceneConverter::ConvertUVTexture(const RenderSceneConverterEnv &env,
env.material_config.texture_image_loader_function_userdata, &warn,
&err);

if (!tex_ok && !env.material_config.allow_texture_load_failure) {
PUSH_ERROR_AND_RETURN("Failed to load texture image: " + err);
}

if (warn.size()) {
DCOUT("WARN: " << warn);
PushWarn(warn);
}

if (!tex_ok && !env.material_config.allow_texture_load_failure) {
PUSH_ERROR_AND_RETURN(fmt::format("Failed to load texture image: `{}` err = {}", assetPath.GetAssetPath(), err));
}


if (err.size()) {
// report as warning.
PushWarn(err);
// report as warn.
PUSH_WARN(fmt::format("Failed to load texture image: `{}`. Skip loading. reason = {} ", assetPath.GetAssetPath(), err));
}

// store unresolved asset path.
Expand All @@ -4145,6 +4147,7 @@ bool RenderSceneConverter::ConvertUVTexture(const RenderSceneConverterEnv &env,
// look into `inputs:sourceColorSpace' attribute.
// When both `colorSpace` metadata and `inputs:sourceColorSpace' attribute
// exists, `inputs:sourceColorSpace` wins.
// FIXME: Change to `colorSpace` metadata supersedes 'inputs:sourceColorSpace'?
bool inferColorSpaceFailed = false;
if (texture.file.metas().has_colorSpace()) {
ColorSpace cs;
Expand Down Expand Up @@ -5347,13 +5350,42 @@ bool DefaultTextureImageLoaderFunction(
texImage.asset_identifier = resolvedPath;
texImage.channels = result.value().image.channels;

if (result.value().image.bpp == 8) {
const auto &imgret = result.value();

if (imgret.image.bpp == 8) {
// assume uint8
texImage.assetTexelComponentType = ComponentType::UInt8;
} else if (imgret.image.bpp == 16) {
if (imgret.image.format == Image::PixelFormat::UInt) {
texImage.assetTexelComponentType = ComponentType::UInt16;
} else if (imgret.image.format == Image::PixelFormat::Int) {
texImage.assetTexelComponentType = ComponentType::Int16;
} else if (imgret.image.format == Image::PixelFormat::Float) {
texImage.assetTexelComponentType = ComponentType::Half;
} else {
if (err) {
(*err) += "Invalid image.pixelformat: " + tinyusdz::to_string(imgret.image.format) + "\n";
}
return false;
}

} else if (imgret.image.bpp == 16) {
if (imgret.image.format == Image::PixelFormat::UInt) {
texImage.assetTexelComponentType = ComponentType::UInt32;
} else if (imgret.image.format == Image::PixelFormat::Int) {
texImage.assetTexelComponentType = ComponentType::Int32;
} else if (imgret.image.format == Image::PixelFormat::Float) {
texImage.assetTexelComponentType = ComponentType::Float;
} else {
if (err) {
(*err) += "Invalid image.pixelformat: " + tinyusdz::to_string(imgret.image.format) + "\n";
}
return false;
}
} else {
DCOUT("TODO: bpp = " << result.value().image.bpp);
if (err) {
(*err) = "TODO or unsupported bpp: " +
(*err) += "TODO or unsupported bpp: " +
std::to_string(result.value().image.bpp) + "\n";
}
return false;
Expand Down

0 comments on commit eeac474

Please sign in to comment.