Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support loading glb with compressed jpeg textures #545

Merged
merged 3 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -542,16 +542,33 @@
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;

Check warning on line 568 in graphics/src/AssimpLoader.cc

View check run for this annotation

Codecov / codecov/patch

graphics/src/AssimpLoader.cc#L566-L568

Added lines #L566 - L568 were not covered by tests
}
}
return img;
return ImagePtr();

Check warning on line 571 in graphics/src/AssimpLoader.cc

View check run for this annotation

Codecov / codecov/patch

graphics/src/AssimpLoader.cc#L571

Added line #L571 was not covered by tests
}

//////////////////////////////////////////////////
Expand Down
34 changes: 34 additions & 0 deletions graphics/src/AssimpLoader_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,40 @@ 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(math::Color(0.0f, 0.0f, 0.0f, 1.0f), mat->Specular());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a duplicate call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed duplicate call, 76be6e6

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.
Loading