Support image/ktx format for textures.

PiperOrigin-RevId: 761926074
Change-Id: I6a312634b5bdc706ea5166bc3b2be273e14c1c8a
This commit is contained in:
Google DeepMind
2025-05-22 05:46:49 -07:00
committed by Copybara-Service
parent 3e9bc79b54
commit 373b4c0437
4 changed files with 52 additions and 19 deletions
+1 -1
View File
@@ -2406,7 +2406,7 @@ const char* mj_validateReferences(const mjModel* m) {
}
}
for (int i=0; i < m->ntex; i++) {
int tex_adr = m->tex_adr[i] + 3*m->tex_height[i]*m->tex_width[i];
int tex_adr = m->tex_adr[i] + m->tex_nchannel[i]*m->tex_height[i]*m->tex_width[i];
if (tex_adr > m->ntexdata || m->tex_adr[i] < 0) {
return "Invalid model: tex_adr out of bounds.";
}
+28 -1
View File
@@ -4283,6 +4283,28 @@ void mjCTexture::LoadPNG(mjResource* resource,
image = png_image.MoveData();
}
// load KTX file
void mjCTexture::LoadKTX(mjResource* resource,
std::vector<unsigned char>& image, unsigned int& w,
unsigned int& h, bool& is_srgb) {
const void* buffer = 0;
int buffer_sz = mju_readResource(resource, &buffer);
// still not found
if (buffer_sz < 0) {
throw mjCError(this, "could not read texture file '%s'", resource->name);
} else if (!buffer_sz) {
throw mjCError(this, "texture file is empty: '%s'", resource->name);
}
w = buffer_sz;
h = 1;
is_srgb = false;
image.resize(buffer_sz);
memcpy(image.data(), buffer, buffer_sz);
}
// load custom file
void mjCTexture::LoadCustom(mjResource* resource,
std::vector<unsigned char>& image,
@@ -4336,7 +4358,7 @@ void mjCTexture::LoadFlip(std::string filename, const mjVFS* vfs,
asset_type = "image/vnd.mujoco.texture";
}
if (asset_type != "image/png" && asset_type != "image/vnd.mujoco.texture") {
if (asset_type != "image/png" && asset_type != "image/ktx" && asset_type != "image/vnd.mujoco.texture") {
throw mjCError(this, "unsupported content type: '%s'", asset_type.c_str());
}
@@ -4345,6 +4367,11 @@ void mjCTexture::LoadFlip(std::string filename, const mjVFS* vfs,
try {
if (asset_type == "image/png") {
LoadPNG(resource, image, w, h, is_srgb);
} else if (asset_type == "image/ktx") {
if (hflip || vflip) {
throw mjCError(this, "cannot flip KTX textures");
}
LoadKTX(resource, image, w, h, is_srgb);
} else {
LoadCustom(resource, image, w, h, is_srgb);
}
+3
View File
@@ -1333,6 +1333,9 @@ class mjCTexture : public mjCTexture_, private mjsTexture {
void LoadPNG(mjResource* resource,
std::vector<unsigned char>& image,
unsigned int& w, unsigned int& h, bool& is_srgb);
void LoadKTX(mjResource* resource,
std::vector<unsigned char>& image,
unsigned int& w, unsigned int& h, bool& is_srgb);
void LoadCustom(mjResource* resource,
std::vector<unsigned char>& image,
unsigned int& w, unsigned int& h, bool& is_srgb);