Skip to content

Remove freeimage dependency #1235

@solonovamax

Description

@solonovamax

Freeimage has several different vulnerabilities in it and the library is no longer maintained and has not been updated since 2018.
Because of this, it has been dropped from many different distributions, such as nixpkgs: NixOS/nixpkgs#454867

This makes building gazebo on those distributions more difficult.
It does not seem that freeimage is used that extensively within gazebo, it is basically only present in a single place:

optix::Buffer OptixTextureFactory::CreateBuffer(const std::string &_filename)
{
if (_filename.empty())
{
gzerr << "Cannot load texture from empty filename" << std::endl;
return this->CreateBuffer();
}
FREE_IMAGE_FORMAT format = FreeImage_GetFileType(_filename.c_str(), 0);
FIBITMAP *image = FreeImage_Load(format, _filename.c_str());
if (!image)
{
gzerr << "Unable to load texture: " << _filename << std::endl;
return this->CreateBuffer();
}
FIBITMAP *temp = image;
image = FreeImage_ConvertTo32Bits(image);
unsigned w = FreeImage_GetWidth(image);
unsigned h = FreeImage_GetHeight(image);
// freeimage stores data as BGR[A] on little endian architecture
// reverse pixel values if needed
#if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR
unsigned p = FreeImage_GetPitch(image);
unsigned bpp = FreeImage_GetBPP(image) / 8;
unsigned lineSize = FreeImage_GetLine(image);
BYTE* line = FreeImage_GetBits(image);
for (unsigned y = 0; y < h; ++y, line += p)
{
for (BYTE* pixel = line; pixel < line + lineSize; pixel += bpp)
{
// in- place swap
pixel[0] ^= pixel[2]; pixel[2] ^= pixel[0]; pixel[0] ^= pixel[2];
}
}
#endif
FreeImage_Unload(temp);
optix::Context optixContext = this->scene->OptixContext();
optix::Buffer buffer = optixContext->createBuffer(RT_BUFFER_INPUT);
buffer->setFormat(RT_FORMAT_UNSIGNED_BYTE4);
buffer->setSize(w, h);
// get raw bits after flipping vertical axis (last bool arg)
// as free image stores data upside down in memory
FreeImage_ConvertToRawBits(reinterpret_cast<BYTE *>(buffer->map()),
image, FreeImage_GetLine(image), FreeImage_GetBPP(image),
FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, true);
buffer->unmap();
FreeImage_Unload(image);
return buffer;
}

so it should be relatively straightforward to replace freeimage which supports a bunch of other image formats.

Here is a list of all of the known unfixed vulnerabilities in freeimage:

in order to replace freeimage, you'd want to find some other library that supports a bunch of similar formats to the ones freeimage supports.

freeimage supports the following file formats:

  • bmp
  • ico
  • jpeg
  • jng
  • koala
  • lbm/iff
  • mng
  • pbm/pbmraw
  • pcd
  • pcx
  • pgm/pgmraw
  • png
  • ppm/ppmraw
  • ras
  • targa
  • tiff
  • wbmp
  • psd
  • cut
  • xbm
  • xpm
  • dds
  • gif
  • hdr
  • faxg3
  • sgi
  • exr
  • j2k
  • jp2
  • pfm
  • pict
  • raw
  • webp
  • jxf

one option would be to use opencv, which supports the following file formats:

  • bmp
  • ico
  • jpeg
  • jng
  • koala
  • lbm/iff
  • mng
  • pbm/pbmraw
  • pcd
  • pcx
  • pgm/pgmraw
  • png
  • ppm/ppmraw
  • ras
  • targa
  • tiff
  • wbmp
  • psd
  • cut
  • xbm
  • xpm
  • dds
  • gif
  • hdr
  • faxg3
  • sgi
  • exr
  • j2k
  • jp2
  • pfm
  • pict
  • raw
  • webp
  • jxf

as well as several niche formats that are not supported by freeimage.

out of the formats which are not supported by opencv, they seem to be extremely niche, and are likely very seldom used.

alternatively, another library which supports all those formats could be found.

related: gazebosim/gz-common#388 (didn't see this issue until afterwards, oopsies)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Inbox

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions