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 compressed images and RGBA data in Image class #368

Merged
merged 6 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 15 additions & 0 deletions graphics/include/gz/common/Image.hh
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ namespace gz
BAYER_BGGR8,
BAYER_GBRG8,
BAYER_GRBG8,
COMPRESSED_PNG,
PIXEL_FORMAT_COUNT
};

Expand Down Expand Up @@ -122,6 +123,14 @@ namespace gz
unsigned int _height,
Image::PixelFormatType _format);

/// \brief Set the image from compressed (i.e. png) data
/// \param[in] _data Pointer to the raw image data
/// \param[in] _size Size of the buffer
/// \param[in] _format Pixel format of the provided data
public: void SetFromCompressedData(unsigned char *_data,
unsigned int _size,
Image::PixelFormatType _format);

/// \brief Get the image as a data array
/// \param[out] _data Pointer to a NULL array of char.
/// \param[out] _count The resulting data array size
Expand All @@ -133,6 +142,12 @@ namespace gz
/// \param[out] _count The resulting data array size
public: void RGBData(unsigned char **_data, unsigned int &_count) const;

/// \brief Get the RGBA data from the image. This will add an alpha
/// channel if one is not present.
/// \param[out] _data Pointer to a NULL array of char.
/// \param[out] _count The resulting data array size
public: void RGBAData(unsigned char **_data, unsigned int &_count) const;

/// \brief Get the width
/// \return The image width
public: unsigned int Width() const;
Expand Down
39 changes: 39 additions & 0 deletions graphics/src/Image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,28 @@ void Image::SetFromData(const unsigned char *_data,
}
}

//////////////////////////////////////////////////
void Image::SetFromCompressedData(unsigned char *_data,
unsigned int _size,
Image::PixelFormatType _format)
{
if (this->dataPtr->bitmap)
FreeImage_Unload(this->dataPtr->bitmap);
this->dataPtr->bitmap = nullptr;

if (_format == COMPRESSED_PNG)
{
FIMEMORY *fiMem = FreeImage_OpenMemory(_data, _size);
this->dataPtr->bitmap = FreeImage_LoadFromMemory(FIF_PNG, fiMem);
FreeImage_CloseMemory(fiMem);
}
else
{
gzerr << "Unable to handle format[" << _format << "]\n";
return;
}
}

//////////////////////////////////////////////////
int Image::Pitch() const
{
Expand All @@ -265,6 +287,23 @@ void Image::RGBData(unsigned char **_data, unsigned int &_count) const
FreeImage_Unload(tmp2);
}

//////////////////////////////////////////////////
void Image::RGBAData(unsigned char **_data, unsigned int &_count) const
{
FIBITMAP *tmp = this->dataPtr->bitmap;
FIBITMAP *tmp2 = nullptr;
if (this->dataPtr->ShouldSwapRedBlue())
{
tmp = this->dataPtr->SwapRedBlue(this->Width(), this->Height());
tmp2 = tmp;
}
tmp = FreeImage_ConvertTo32Bits(tmp);
this->dataPtr->DataImpl(_data, _count, tmp);
FreeImage_Unload(tmp);
if (tmp2)
FreeImage_Unload(tmp2);
}

//////////////////////////////////////////////////
void Image::Data(unsigned char **_data, unsigned int &_count) const
{
Expand Down
47 changes: 47 additions & 0 deletions graphics/src/Image_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ TEST_F(ImageTest, RGBData)
}
}
}
delete[] data;
}

/////////////////////////////////////////////////
Expand Down Expand Up @@ -179,6 +180,7 @@ TEST_F(ImageTest, Data)
}
}
}
delete[] data;
}

/////////////////////////////////////////////////
Expand Down Expand Up @@ -208,6 +210,51 @@ TEST_F(ImageTest, SetFromData)
ASSERT_EQ(img2.Pixel(85, 0), math::Color::Blue);
ASSERT_EQ(img2.AvgColor(), math::Color(0.661157f, 0, 0.338843f, 1));
ASSERT_EQ(img2.MaxColor(), math::Color::Red);
delete[] data;
}

TEST_F(ImageTest, SetFromCompressedData)
{
// Open file and move to end
std::ifstream ifs(kTestData, std::ios::binary | std::ios::ate);
std::ifstream::pos_type fileEnd = ifs.tellg();
std::vector<unsigned char> fileData(fileEnd);

// Rewind to beginning of file and read data
ifs.seekg(0);
ifs.read(reinterpret_cast<char *>(&fileData[0]), fileEnd);

common::Image img;
img.SetFromCompressedData(&fileData[0], fileEnd,
common::Image::PixelFormatType::COMPRESSED_PNG);
ASSERT_TRUE(img.Valid());

unsigned char *data = nullptr;
unsigned int size = 0;
img.RGBAData(&data, size);
ASSERT_EQ(39204u, size);
ASSERT_NE(nullptr, data);

ASSERT_EQ(common::Image::PixelFormatType::RGBA_INT8, img.PixelFormat());
ASSERT_EQ(121u, img.Width());
ASSERT_EQ(81u, img.Height());
ASSERT_EQ(32u, img.BPP());
ASSERT_EQ(484, img.Pitch());
ASSERT_EQ(img.Pixel(0, 0), math::Color::Red);
ASSERT_EQ(img.Pixel(85, 0), math::Color::Blue);
ASSERT_EQ(img.AvgColor(), math::Color(0.661157f, 0, 0.338843f, 1));
ASSERT_EQ(img.MaxColor(), math::Color::Red);
delete[] data;

// Reloading the image should not cause any leaks.
img.SetFromCompressedData(&fileData[0], fileEnd,
common::Image::PixelFormatType::COMPRESSED_PNG);
ASSERT_TRUE(img.Valid());

// Loading from unsupported pixel format fails.
img.SetFromCompressedData(&fileData[0], fileEnd,
common::Image::PixelFormatType::UNKNOWN_PIXEL_FORMAT);
ASSERT_FALSE(img.Valid());
}

/*
Expand Down