Skip to content

Commit

Permalink
Support loading glb with compressed jpeg textures (#545)
Browse files Browse the repository at this point in the history
Signed-off-by: Ian Chen <[email protected]>
  • Loading branch information
iche033 committed Oct 25, 2023
1 parent afc1ab0 commit 3742a5a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 7 deletions.
6 changes: 5 additions & 1 deletion graphics/include/gz/common/Image.hh
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ namespace gz
BAYER_GBRG8,
BAYER_GRBG8,
COMPRESSED_PNG,
PIXEL_FORMAT_COUNT
PIXEL_FORMAT_COUNT,
// \todo(iche033) COMPRESSED_JPEG is added at the end to
// preserve ABI compatibility. Move this enum up when merging
// forward to main
COMPRESSED_JPEG
};


Expand Down
25 changes: 21 additions & 4 deletions graphics/src/AssimpLoader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -551,16 +551,33 @@ std::pair<ImagePtr, ImagePtr>
ImagePtr AssimpLoader::Implementation::LoadEmbeddedTexture(
const aiTexture* _texture) const
{
auto img = std::make_shared<Image>();
if (_texture->mHeight == 0)
{
Image::PixelFormatType format = Image::PixelFormatType::UNKNOWN_PIXEL_FORMAT;
if (_texture->CheckFormat("png"))
{
img->SetFromCompressedData((unsigned char*)_texture->pcData,
_texture->mWidth, Image::PixelFormatType::COMPRESSED_PNG);
format = Image::PixelFormatType::COMPRESSED_PNG;
}
else if (_texture->CheckFormat("jpg"))
{
format = Image::PixelFormatType::COMPRESSED_JPEG;
}
if (format != Image::PixelFormatType::UNKNOWN_PIXEL_FORMAT)
{
auto img = std::make_shared<Image>();
img->SetFromCompressedData(
reinterpret_cast<unsigned char *>(_texture->pcData),
_texture->mWidth, format);
return img;
}
else
{
gzerr << "Unable to load embedded texture. "
<< "Unsupported compressed image format"
<< std::endl;
}
}
return img;
return ImagePtr();
}

//////////////////////////////////////////////////
Expand Down
33 changes: 33 additions & 0 deletions graphics/src/AssimpLoader_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,39 @@ TEST_F(AssimpLoader, LoadGlTF2BoxExternalTexture)
EXPECT_EQ(testTextureFile, mat->TextureImage());
}

/////////////////////////////////////////////////
// This test loads a box glb mesh with embedded compressed jpeg texture
TEST_F(AssimpLoader, LoadGlTF2BoxWithJPEGTexture)
{
common::AssimpLoader loader;
common::Mesh *mesh = loader.Load(
common::testing::TestFile("data", "box_texture_jpg.glb"));

EXPECT_STREQ("unknown", mesh->Name().c_str());
EXPECT_EQ(math::Vector3d(1, 1, 1), mesh->Max());
EXPECT_EQ(math::Vector3d(-1, -1, -1), mesh->Min());

EXPECT_EQ(24u, mesh->VertexCount());
EXPECT_EQ(24u, mesh->NormalCount());
EXPECT_EQ(36u, mesh->IndexCount());
EXPECT_EQ(24u, mesh->TexCoordCount());
EXPECT_EQ(1u, mesh->SubMeshCount());
EXPECT_EQ(1u, mesh->MaterialCount());

// Make sure we can read the submesh name
EXPECT_STREQ("Cube", mesh->SubMeshByIndex(0).lock()->Name().c_str());

const common::MaterialPtr mat = mesh->MaterialByIndex(0u);
ASSERT_TRUE(mat.get());

// Make sure we read the material color values
EXPECT_EQ(math::Color(0.4f, 0.4f, 0.4f, 1.0f), mat->Ambient());
EXPECT_EQ(math::Color(1.0f, 1.0f, 1.0f, 1.0f), mat->Diffuse());
EXPECT_EQ(math::Color(0.0f, 0.0f, 0.0f, 1.0f), mat->Specular());
EXPECT_EQ("Cube_Material_Diffuse", mat->TextureImage());
EXPECT_NE(nullptr, mat->TextureData());
}

/////////////////////////////////////////////////
// Use a fully featured glb test asset, including PBR textures, emissive maps
// embedded textures, lightmaps, animations to test advanced glb features
Expand Down
16 changes: 14 additions & 2 deletions graphics/src/Image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,22 @@ void Image::SetFromCompressedData(unsigned char *_data,
FreeImage_Unload(this->dataPtr->bitmap);
this->dataPtr->bitmap = nullptr;

if (_format == COMPRESSED_PNG)
FREE_IMAGE_FORMAT format = FIF_UNKNOWN;
switch (_format)
{
case COMPRESSED_PNG:
format = FIF_PNG;
break;
case COMPRESSED_JPEG:
format = FIF_JPEG;
break;
default:
break;
}
if (format != FIF_UNKNOWN)
{
FIMEMORY *fiMem = FreeImage_OpenMemory(_data, _size);
this->dataPtr->bitmap = FreeImage_LoadFromMemory(FIF_PNG, fiMem);
this->dataPtr->bitmap = FreeImage_LoadFromMemory(format, fiMem);
FreeImage_CloseMemory(fiMem);
}
else
Expand Down
Binary file added test/data/box_texture_jpg.glb
Binary file not shown.

0 comments on commit 3742a5a

Please sign in to comment.