Auto reformatted the base, ref #470

This commit is contained in:
emeric
2024-05-24 23:31:52 +02:00
parent 83b868673c
commit 39941d90a3
460 changed files with 8583 additions and 8514 deletions
+2 -1
View File
@@ -20,6 +20,7 @@
#include "SvgImage.hpp"
#include <fstream>
#include "core/ITraceLogger.hpp"
#include "image/Exception.hpp"
@@ -50,4 +51,4 @@ namespace lms::image
return std::make_unique<SvgImage>(std::move(data));
}
}
} // namespace lms::image
+3 -2
View File
@@ -30,7 +30,8 @@ namespace lms::image
class SvgImage : public IEncodedImage
{
public:
SvgImage(std::vector<std::byte>&& data) : _data{ std::move(data) } {}
SvgImage(std::vector<std::byte>&& data)
: _data{ std::move(data) } {}
const std::byte* getData() const { return &_data.front(); }
std::size_t getDataSize() const { return _data.size(); }
@@ -39,4 +40,4 @@ namespace lms::image
private:
const std::vector<std::byte> _data;
};
}
} // namespace lms::image
@@ -19,37 +19,36 @@
#include "JPEGImage.hpp"
#include "RawImage.hpp"
#include "image/Exception.hpp"
#include "core/ILogger.hpp"
#include "image/Exception.hpp"
#include "RawImage.hpp"
namespace lms::image::GraphicsMagick
{
JPEGImage::JPEGImage(const RawImage& rawImage, unsigned quality)
{
try
{
Magick::Image image {rawImage.getMagickImage()};
image.magick("JPEG");
image.quality(quality);
image.write(&_blob);
}
catch (Magick::Exception& e)
{
LMS_LOG(COVER, ERROR, "Caught Magick exception: " << e.what());
throw Exception {std::string {"Magick read error: "} + e.what()};
}
}
JPEGImage::JPEGImage(const RawImage& rawImage, unsigned quality)
{
try
{
Magick::Image image{ rawImage.getMagickImage() };
image.magick("JPEG");
image.quality(quality);
image.write(&_blob);
}
catch (Magick::Exception& e)
{
LMS_LOG(COVER, ERROR, "Caught Magick exception: " << e.what());
throw Exception{ std::string{ "Magick read error: " } + e.what() };
}
}
const std::byte*
JPEGImage::getData() const
{
return reinterpret_cast<const std::byte*>(_blob.data());
}
const std::byte* JPEGImage::getData() const
{
return reinterpret_cast<const std::byte*>(_blob.data());
}
std::size_t
JPEGImage::getDataSize() const
{
return _blob.length();
}
}
std::size_t JPEGImage::getDataSize() const
{
return _blob.length();
}
} // namespace lms::image::GraphicsMagick
@@ -25,17 +25,17 @@
namespace lms::image::GraphicsMagick
{
class RawImage;
class JPEGImage : public IEncodedImage
{
public:
JPEGImage(const RawImage& rawImage, unsigned quality);
class RawImage;
class JPEGImage : public IEncodedImage
{
public:
JPEGImage(const RawImage& rawImage, unsigned quality);
private:
const std::byte* getData() const override;
std::size_t getDataSize() const override;
std::string_view getMimeType() const override { return "image/jpeg"; }
private:
const std::byte* getData() const override;
std::size_t getDataSize() const override;
std::string_view getMimeType() const override { return "image/jpeg"; }
Magick::Blob _blob;
};
}
Magick::Blob _blob;
};
} // namespace lms::image::GraphicsMagick
+87 -91
View File
@@ -21,114 +21,110 @@
#include <magick/resource.h>
#include "JPEGImage.hpp"
#include "image/Exception.hpp"
#include "core/ILogger.hpp"
#include "image/Exception.hpp"
#include "JPEGImage.hpp"
namespace lms::image
{
std::unique_ptr<IRawImage> decodeImage(const std::byte* encodedData, std::size_t encodedDataSize)
{
return std::make_unique<GraphicsMagick::RawImage>(encodedData, encodedDataSize);
}
std::unique_ptr<IRawImage> decodeImage(const std::byte* encodedData, std::size_t encodedDataSize)
{
return std::make_unique<GraphicsMagick::RawImage>(encodedData, encodedDataSize);
}
std::unique_ptr<IRawImage> decodeImage(const std::filesystem::path& path)
{
return std::make_unique<GraphicsMagick::RawImage>(path);
}
std::unique_ptr<IRawImage> decodeImage(const std::filesystem::path& path)
{
return std::make_unique<GraphicsMagick::RawImage>(path);
}
void
init(const std::filesystem::path& path)
{
Magick::InitializeMagick(path.string().c_str());
void init(const std::filesystem::path& path)
{
Magick::InitializeMagick(path.string().c_str());
if (auto nbThreads {MagickLib::GetMagickResourceLimit(MagickLib::ThreadsResource)}; nbThreads != 1)
LMS_LOG(COVER, WARNING, "Consider setting env var OMP_NUM_THREADS=1 to save resources");
if (auto nbThreads{ MagickLib::GetMagickResourceLimit(MagickLib::ThreadsResource) }; nbThreads != 1)
LMS_LOG(COVER, WARNING, "Consider setting env var OMP_NUM_THREADS=1 to save resources");
if (!MagickLib::SetMagickResourceLimit(MagickLib::ThreadsResource, 1))
LMS_LOG(COVER, ERROR, "Cannot set Magick thread resource limit to 1!");
if (!MagickLib::SetMagickResourceLimit(MagickLib::ThreadsResource, 1))
LMS_LOG(COVER, ERROR, "Cannot set Magick thread resource limit to 1!");
if (!MagickLib::SetMagickResourceLimit(MagickLib::DiskResource, 0))
LMS_LOG(COVER, ERROR, "Cannot set Magick disk resource limit to 0!");
if (!MagickLib::SetMagickResourceLimit(MagickLib::DiskResource, 0))
LMS_LOG(COVER, ERROR, "Cannot set Magick disk resource limit to 0!");
LMS_LOG(COVER, INFO, "Magick threads resource limit = " << GetMagickResourceLimit(MagickLib::ThreadsResource));
LMS_LOG(COVER, INFO, "Magick Disk resource limit = " << GetMagickResourceLimit(MagickLib::DiskResource));
}
}
LMS_LOG(COVER, INFO, "Magick threads resource limit = " << GetMagickResourceLimit(MagickLib::ThreadsResource));
LMS_LOG(COVER, INFO, "Magick Disk resource limit = " << GetMagickResourceLimit(MagickLib::DiskResource));
}
} // namespace lms::image
namespace lms::image::GraphicsMagick
{
RawImage::RawImage(const std::byte* encodedData, std::size_t encodedDataSize)
{
try
{
Magick::Blob blob {encodedData, encodedDataSize};
_image.read(blob);
}
catch (Magick::WarningCoder& e)
{
LMS_LOG(COVER, WARNING, "Caught Magick WarningCoder: " << e.what());
}
catch (Magick::Warning& e)
{
LMS_LOG(COVER, WARNING, "Caught Magick warning: " << e.what());
throw Exception {std::string {"Magick read warning: "} + e.what()};
}
catch (Magick::Exception& e)
{
LMS_LOG(COVER, ERROR, "Caught Magick exception: " << e.what());
throw Exception {std::string {"Magick read error: "} + e.what()};
}
}
RawImage::RawImage(const std::byte* encodedData, std::size_t encodedDataSize)
{
try
{
Magick::Blob blob{ encodedData, encodedDataSize };
_image.read(blob);
}
catch (Magick::WarningCoder& e)
{
LMS_LOG(COVER, WARNING, "Caught Magick WarningCoder: " << e.what());
}
catch (Magick::Warning& e)
{
LMS_LOG(COVER, WARNING, "Caught Magick warning: " << e.what());
throw Exception{ std::string{ "Magick read warning: " } + e.what() };
}
catch (Magick::Exception& e)
{
LMS_LOG(COVER, ERROR, "Caught Magick exception: " << e.what());
throw Exception{ std::string{ "Magick read error: " } + e.what() };
}
}
RawImage::RawImage(const std::filesystem::path& p)
{
try
{
_image.read(p.string().c_str());
}
catch (Magick::WarningCoder& e)
{
LMS_LOG(COVER, WARNING, "Caught Magick WarningCoder: " << e.what());
}
catch (Magick::Warning& e)
{
LMS_LOG(COVER, WARNING, "Caught Magick warning: " << e.what());
throw Exception {std::string {"Magick read warning: "} + e.what()};
}
catch (Magick::Exception& e)
{
LMS_LOG(COVER, ERROR, "Caught Magick exception: " << e.what());
throw Exception {std::string {"Magick read error: "} + e.what()};
}
}
RawImage::RawImage(const std::filesystem::path& p)
{
try
{
_image.read(p.string().c_str());
}
catch (Magick::WarningCoder& e)
{
LMS_LOG(COVER, WARNING, "Caught Magick WarningCoder: " << e.what());
}
catch (Magick::Warning& e)
{
LMS_LOG(COVER, WARNING, "Caught Magick warning: " << e.what());
throw Exception{ std::string{ "Magick read warning: " } + e.what() };
}
catch (Magick::Exception& e)
{
LMS_LOG(COVER, ERROR, "Caught Magick exception: " << e.what());
throw Exception{ std::string{ "Magick read error: " } + e.what() };
}
}
void
RawImage::resize(ImageSize width)
{
try
{
_image.resize(Magick::Geometry {static_cast<unsigned int>(width), static_cast<unsigned int>(width)});
}
catch (Magick::Exception& e)
{
LMS_LOG(COVER, ERROR, "Caught Magick exception while resizing: " << e.what());
throw Exception {std::string {"Magick resize error: "} + e.what()};
}
}
void RawImage::resize(ImageSize width)
{
try
{
_image.resize(Magick::Geometry{ static_cast<unsigned int>(width), static_cast<unsigned int>(width) });
}
catch (Magick::Exception& e)
{
LMS_LOG(COVER, ERROR, "Caught Magick exception while resizing: " << e.what());
throw Exception{ std::string{ "Magick resize error: " } + e.what() };
}
}
std::unique_ptr<IEncodedImage>
RawImage::encodeToJPEG(unsigned quality) const
{
return std::make_unique<JPEGImage>(*this, quality);
}
std::unique_ptr<IEncodedImage> RawImage::encodeToJPEG(unsigned quality) const
{
return std::make_unique<JPEGImage>(*this, quality);
}
Magick::Image
RawImage::getMagickImage() const
{
return _image;
}
Magick::Image RawImage::getMagickImage() const
{
return _image;
}
} // namespace lms::image::GraphicsMagick
+15 -16
View File
@@ -19,30 +19,29 @@
#pragma once
#include <Magick++.h>
#include <cstddef>
#include <filesystem>
#include <Magick++.h>
#include "image/IEncodedImage.hpp"
#include "image/IRawImage.hpp"
namespace lms::image::GraphicsMagick
{
class RawImage : public IRawImage
{
public:
RawImage(const std::byte* encodedData, std::size_t encodedDataSize);
RawImage(const std::filesystem::path& path);
class RawImage : public IRawImage
{
public:
RawImage(const std::byte* encodedData, std::size_t encodedDataSize);
RawImage(const std::filesystem::path& path);
void resize(ImageSize width) override;
std::unique_ptr<IEncodedImage> encodeToJPEG(unsigned quality) const override;
void resize(ImageSize width) override;
std::unique_ptr<IEncodedImage> encodeToJPEG(unsigned quality) const override;
private:
friend class JPEGImage;
Magick::Image getMagickImage() const;
Magick::Image _image;
};
}
private:
friend class JPEGImage;
Magick::Image getMagickImage() const;
Magick::Image _image;
};
} // namespace lms::image::GraphicsMagick
+28 -28
View File
@@ -24,41 +24,41 @@
#include "core/ITraceLogger.hpp"
#include "image/Exception.hpp"
#include "RawImage.hpp"
namespace lms::image::STB
{
JPEGImage::JPEGImage(const RawImage& rawImage, unsigned quality)
{
JPEGImage::JPEGImage(const RawImage& rawImage, unsigned quality)
{
LMS_SCOPED_TRACE_DETAILED("Image", "WriteJPEG");
auto writeCb {[](void* ctx, void* writeData, int writeSize)
{
auto& output {*reinterpret_cast<std::vector<std::byte>*>(ctx)};
const std::size_t currentOutputSize {output.size()};
output.resize(currentOutputSize + writeSize);
std::copy(reinterpret_cast<const std::byte*>(writeData), reinterpret_cast<const std::byte*>(writeData) + writeSize, output.data() + currentOutputSize);
}};
auto writeCb{ [](void* ctx, void* writeData, int writeSize) {
auto& output{ *reinterpret_cast<std::vector<std::byte>*>(ctx) };
const std::size_t currentOutputSize{ output.size() };
output.resize(currentOutputSize + writeSize);
std::copy(reinterpret_cast<const std::byte*>(writeData), reinterpret_cast<const std::byte*>(writeData) + writeSize, output.data() + currentOutputSize);
} };
if (stbi_write_jpg_to_func(writeCb, &_data, rawImage.getWidth(), rawImage.getHeight(), 3, rawImage.getData(), quality) == 0)
{
_data.clear();
throw Exception {"Failed to export in jpeg format!"};
}
}
if (stbi_write_jpg_to_func(writeCb, &_data, rawImage.getWidth(), rawImage.getHeight(), 3, rawImage.getData(), quality) == 0)
{
_data.clear();
throw Exception{ "Failed to export in jpeg format!" };
}
}
const std::byte*
JPEGImage::getData() const
{
if (_data.empty())
return nullptr;
const std::byte*
JPEGImage::getData() const
{
if (_data.empty())
return nullptr;
return &_data.front();
}
return &_data.front();
}
std::size_t
JPEGImage::getDataSize() const
{
return _data.size();
}
}
std::size_t
JPEGImage::getDataSize() const
{
return _data.size();
}
} // namespace lms::image::STB
+12 -12
View File
@@ -25,17 +25,17 @@
namespace lms::image::STB
{
class RawImage;
class JPEGImage : public IEncodedImage
{
public:
JPEGImage(const RawImage& rawImage, unsigned quality);
class RawImage;
class JPEGImage : public IEncodedImage
{
public:
JPEGImage(const RawImage& rawImage, unsigned quality);
private:
const std::byte* getData() const override;
std::size_t getDataSize() const override;
std::string_view getMimeType() const override { return "image/jpeg"; }
private:
const std::byte* getData() const override;
std::size_t getDataSize() const override;
std::string_view getMimeType() const override { return "image/jpeg"; }
std::vector<std::byte> _data;
};
}
std::vector<std::byte> _data;
};
} // namespace lms::image::STB
+14 -11
View File
@@ -22,22 +22,23 @@
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#define STBIR_DEFAULT_FILTER_DOWNSAMPLE STBIR_FILTER_MITCHELL
#define STBIR_DEFAULT_FILTER_UPSAMPLE STBIR_FILTER_CATMULLROM
#define STBIR_DEFAULT_FILTER_DOWNSAMPLE STBIR_FILTER_MITCHELL
#define STBIR_DEFAULT_FILTER_UPSAMPLE STBIR_FILTER_CATMULLROM
#define STBI_FAILURE_USERMSG
#include <stb_image.h>
#if STB_IMAGE_RESIZE_VERSION == 1
#include <stb_image_resize.h>
#include <stb_image_resize.h>
#elif STB_IMAGE_RESIZE_VERSION == 2
#include <stb_image_resize2.h>
#include <stb_image_resize2.h>
#else
#error "Unhandled STB image resize version"!
#error "Unhandled STB image resize version"!
#endif
#include "core/ITraceLogger.hpp"
#include "image/Exception.hpp"
#include "JPEGImage.hpp"
namespace lms::image
@@ -57,7 +58,7 @@ namespace lms::image
void init(const std::filesystem::path&)
{
}
}
} // namespace lms::image
namespace lms::image::STB
{
@@ -102,12 +103,14 @@ namespace lms::image::STB
#if STB_IMAGE_RESIZE_VERSION == 1
if (::stbir_resize_uint8_srgb(reinterpret_cast<const unsigned char*>(_data.get()), _width, _height, 0,
reinterpret_cast<unsigned char*>(resizedData.get()), width, height, 0,
3, STBIR_ALPHA_CHANNEL_NONE, 0) == 0)
reinterpret_cast<unsigned char*>(resizedData.get()), width, height, 0,
3, STBIR_ALPHA_CHANNEL_NONE, 0)
== 0)
#elif STB_IMAGE_RESIZE_VERSION == 2
if (::stbir_resize_uint8_srgb(reinterpret_cast<const unsigned char*>(_data.get()), _width, _height, 0,
reinterpret_cast<unsigned char*>(resizedData.get()), width, height, 0,
STBIR_RGB) == 0)
reinterpret_cast<unsigned char*>(resizedData.get()), width, height, 0,
STBIR_RGB)
== 0)
#else
#error "Unhandled STB image resize version"!
#endif
@@ -142,4 +145,4 @@ namespace lms::image::STB
return reinterpret_cast<const std::byte*>(_data.get());
}
}
} // namespace lms::image::STB
+1 -2
View File
@@ -46,5 +46,4 @@ namespace lms::image::STB
using UniquePtrFree = std::unique_ptr<unsigned char, decltype(&std::free)>;
UniquePtrFree _data{ nullptr, std::free };
};
}
} // namespace lms::image::STB
+3 -3
View File
@@ -25,7 +25,7 @@ namespace lms::image
{
class Exception : public core::LmsException
{
public:
using LmsException::LmsException;
public:
using LmsException::LmsException;
};
} // namespace lms::cover
} // namespace lms::image
@@ -35,4 +35,4 @@ namespace lms::image
virtual std::size_t getDataSize() const = 0;
virtual std::string_view getMimeType() const = 0;
};
}
} // namespace lms::image
+1 -2
View File
@@ -30,5 +30,4 @@ namespace lms::image
virtual void resize(ImageSize width) = 0;
virtual std::unique_ptr<IEncodedImage> encodeToJPEG(unsigned quality) const = 0;
};
}
} // namespace lms::image
+1 -1
View File
@@ -31,4 +31,4 @@ namespace lms::image
std::unique_ptr<IRawImage> decodeImage(const std::byte* encodedData, std::size_t encodedDataSize);
std::unique_ptr<IRawImage> decodeImage(const std::filesystem::path& path);
std::unique_ptr<IEncodedImage> readSvgFile(const std::filesystem::path& path);
}
} // namespace lms::image