28 inline void INPLACESWAP(T& a, T& b)
42 if (FreeImage_GetImageType(freeImage) != FIT_BITMAP)
45 const unsigned bytesperpixel = FreeImage_GetBPP(freeImage) / 8;
46 if (bytesperpixel > 4 || bytesperpixel < 3)
49 const unsigned height = FreeImage_GetHeight(freeImage);
50 const unsigned pitch = FreeImage_GetPitch(freeImage);
51 const unsigned lineSize = FreeImage_GetLine(freeImage);
53 BYTE* line = FreeImage_GetBits(freeImage);
54 for (
unsigned y = 0; y < height; ++y, line += pitch)
55 for (BYTE* pixel = line; pixel < line + lineSize; pixel += bytesperpixel)
57 INPLACESWAP(pixel[0], pixel[2]);
64 FreeImage_SetOutputMessage([](FREE_IMAGE_FORMAT,
const char* message) {
throw std::runtime_error(message); });
66 auto fif = format ==
"jpg" ? FIF_JPEG : FreeImage_GetFIFFromFormat(format.c_str());
68 image.reset(FreeImage_ConvertTo24Bits(image.get()));
69 else if (fif == FIF_UNKNOWN)
70 throw std::runtime_error(
"Unknown format: " + format);
78 FreeImage_SaveToMemory(fif, image.get(), memory.get(), flags);
82 FreeImage_AcquireMemory(memory.get(), &pixels, &numPixels);
88 int width, height, bbp;
89 width = height = bbp = 0;
90 for (
const auto& image : images)
92 width += FreeImage_GetWidth(image.get());
93 height = FreeImage_GetHeight(image.get());
94 bbp = FreeImage_GetBPP(image.get());
97 FreeImage_SetOutputMessage([](FREE_IMAGE_FORMAT,
const char* message) {
throw std::runtime_error(message); });
99 ImagePtr mergedImage{FreeImage_Allocate(width, height, bbp)};
101 for (
const auto& image : images)
103 const auto imageWidth = FreeImage_GetWidth(image.get());
104 FreeImage_Paste(mergedImage.get(), image.get(), offset, 0, 255);
105 offset += imageWidth;
std::string base64_encode(unsigned char const *bytes_to_encode, unsigned int in_len)
std::unique_ptr< FIBITMAP, ImageDeleter > ImagePtr
bool SwapRedBlue32(FIBITMAP *freeImage)
std::string getBase64Image(ImagePtr image, const std::string &format, const int quality)
std::unique_ptr< FIMEMORY, MemoryDeleter > MemoryPtr
ImagePtr mergeImages(const std::vector< ImagePtr > &images)