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

Function to serialize compressed image to buffer #139

Merged
merged 8 commits into from
Apr 6, 2021
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
5 changes: 5 additions & 0 deletions graphics/include/ignition/common/Image.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <memory>
#include <string>
#include <vector>
#include <ignition/math/Color.hh>
#include <ignition/common/graphics/Export.hh>
#include <ignition/common/SuppressWarning.hh>
Expand Down Expand Up @@ -109,6 +110,10 @@ namespace ignition
/// \param[in] _filename The name of the saved image
public: void SavePNG(const std::string &_filename);

/// \brief Save the image in PNG format
/// \param[in] _filename The name of the saved image
public: void SavePNGToBuffer(std::vector<unsigned char> &_buffer);

/// \brief Set the image from raw data
/// \param[in] _data Pointer to the raw image data
/// \param[in] _width Width in pixels
Expand Down
17 changes: 17 additions & 0 deletions graphics/src/Image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,23 @@ void Image::SavePNG(const std::string &_filename)
PNG_DEFAULT);
}

//////////////////////////////////////////////////
void Image::SavePNGToBuffer(std::vector<unsigned char>& buffer)
{
FIMEMORY *hmem = FreeImage_OpenMemory();
FreeImage_SaveToMemory(FIF_PNG, this->dataPtr->bitmap, hmem);
unsigned char *memBuffer = nullptr;
#ifndef _WIN32
unsigned int sizeInBytes = 0;
#else
DWORD sizeInBytes = 0;
#endif
FreeImage_AcquireMemory(hmem, &memBuffer, &sizeInBytes);
buffer.resize(sizeInBytes);
std::memcpy(buffer.data(), memBuffer, sizeInBytes);
FreeImage_CloseMemory(hmem);
}

//////////////////////////////////////////////////
void Image::SetFromData(const unsigned char *_data,
unsigned int _width,
Expand Down