Made STB the default image manipulation library to reduce memory usage (GraphicsMagick can still be selected). closes #93
This commit is contained in:
@@ -25,58 +25,71 @@
|
||||
#include "database/Session.hpp"
|
||||
#include "database/Track.hpp"
|
||||
|
||||
#if LMS_SUPPORT_IMAGE_STB
|
||||
#include "stb/RawImage.hpp"
|
||||
using RawImage = CoverArt::STB::RawImage;
|
||||
#elif LMS_SUPPORT_IMAGE_GM
|
||||
#include "graphicsmagick/RawImage.hpp"
|
||||
using RawImage = CoverArt::GraphicsMagick::RawImage;
|
||||
#endif
|
||||
|
||||
#include "utils/Logger.hpp"
|
||||
#include "utils/Random.hpp"
|
||||
#include "utils/Utils.hpp"
|
||||
#include "Exception.hpp"
|
||||
|
||||
#include "CoverArt.hpp"
|
||||
|
||||
namespace {
|
||||
namespace CoverArt {
|
||||
|
||||
static
|
||||
bool
|
||||
isFileSupported(const std::filesystem::path& file, const std::vector<std::filesystem::path>& extensions)
|
||||
{
|
||||
return (std::find(std::cbegin(extensions), std::cend(extensions), file.extension()) != std::cend(extensions));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace CoverArt {
|
||||
|
||||
std::unique_ptr<IGrabber> createGrabber(const std::filesystem::path& execPath, std::size_t maxCacheSize, std::size_t maxFileSize)
|
||||
std::unique_ptr<IGrabber>
|
||||
createGrabber(const std::filesystem::path& execPath,
|
||||
const std::filesystem::path& defaultCoverPath,
|
||||
std::size_t maxCacheSize, std::size_t maxFileSize, unsigned jpegQuality)
|
||||
{
|
||||
return std::make_unique<Grabber>(execPath, maxCacheSize, maxFileSize);
|
||||
return std::make_unique<Grabber>(execPath, defaultCoverPath, maxCacheSize, maxFileSize, jpegQuality);
|
||||
}
|
||||
|
||||
Grabber::Grabber(const std::filesystem::path& execPath,
|
||||
const std::filesystem::path& defaultCoverPath,
|
||||
std::size_t maxCacheSize,
|
||||
std::size_t maxFileSize)
|
||||
: _maxCacheSize {maxCacheSize}
|
||||
std::size_t maxFileSize,
|
||||
unsigned jpegQuality)
|
||||
: _defaultCoverPath {defaultCoverPath}
|
||||
, _maxCacheSize {maxCacheSize}
|
||||
, _maxFileSize {maxFileSize}
|
||||
, _jpegQuality {clamp<unsigned>(jpegQuality, 1, 100)}
|
||||
{
|
||||
LMS_LOG(COVER, INFO) << "Default cover path = '" << _defaultCoverPath.string() << "'";
|
||||
LMS_LOG(COVER, INFO) << "Max cache size = " << _maxCacheSize;
|
||||
LMS_LOG(COVER, INFO) << "Max file size = " << _maxFileSize;
|
||||
init(execPath);
|
||||
}
|
||||
LMS_LOG(COVER, INFO) << "JPEG export quality = " << _jpegQuality;
|
||||
|
||||
#if LMS_SUPPORT_IMAGE_GM
|
||||
GraphicsMagick::init(execPath);
|
||||
#else
|
||||
(void)execPath;
|
||||
#endif
|
||||
|
||||
void
|
||||
Grabber::setDefaultCover(const std::filesystem::path& p)
|
||||
{
|
||||
try
|
||||
{
|
||||
RawImage defaultCover {p};
|
||||
_defaultCover = defaultCover.encode();
|
||||
LMS_LOG(COVER, INFO) << "Successfully read default cover image!";
|
||||
getDefault(512);
|
||||
}
|
||||
catch (const ImageException& e)
|
||||
{
|
||||
throw LmsException("Cannot read default cover file '" + p.string() + "'");
|
||||
throw LmsException("Cannot read default cover file '" + _defaultCoverPath.string() + "': " + e.what());
|
||||
}
|
||||
}
|
||||
|
||||
static std::optional<EncodedImage>
|
||||
getFromAvMediaFile(const Av::MediaFile& input, Width width)
|
||||
std::unique_ptr<IEncodedImage>
|
||||
Grabber::getFromAvMediaFile(const Av::MediaFile& input, ImageSize width) const
|
||||
{
|
||||
std::optional<EncodedImage> image;
|
||||
std::unique_ptr<IEncodedImage> image;
|
||||
|
||||
input.visitAttachedPictures([&](const Av::Picture& picture)
|
||||
{
|
||||
@@ -85,12 +98,9 @@ getFromAvMediaFile(const Av::MediaFile& input, Width width)
|
||||
|
||||
try
|
||||
{
|
||||
EncodedImage encodedImage {picture.data, picture.dataSize};
|
||||
|
||||
RawImage rawImage {encodedImage};
|
||||
rawImage.scale(width);
|
||||
|
||||
image = rawImage.encode();
|
||||
RawImage rawImage {picture.data, picture.dataSize};
|
||||
rawImage.resize(width);
|
||||
image = rawImage.encodeToJPEG(_jpegQuality);
|
||||
}
|
||||
catch (const ImageException& e)
|
||||
{
|
||||
@@ -101,17 +111,16 @@ getFromAvMediaFile(const Av::MediaFile& input, Width width)
|
||||
return image;
|
||||
}
|
||||
|
||||
static std::optional<EncodedImage>
|
||||
getFromFile(const std::filesystem::path& p, Width width)
|
||||
std::unique_ptr<IEncodedImage>
|
||||
Grabber::getFromFile(const std::filesystem::path& p, ImageSize width) const
|
||||
{
|
||||
std::optional<EncodedImage> image;
|
||||
std::unique_ptr<IEncodedImage> image;
|
||||
|
||||
try
|
||||
{
|
||||
RawImage rawImage {p};
|
||||
rawImage.scale(width);
|
||||
|
||||
image = rawImage.encode();
|
||||
rawImage.resize(width);
|
||||
image = rawImage.encodeToJPEG(_jpegQuality);
|
||||
}
|
||||
catch (const ImageException& e)
|
||||
{
|
||||
@@ -121,53 +130,50 @@ getFromFile(const std::filesystem::path& p, Width width)
|
||||
return image;
|
||||
}
|
||||
|
||||
EncodedImage
|
||||
Grabber::getDefault(Width width)
|
||||
std::shared_ptr<IEncodedImage>
|
||||
Grabber::getDefault(ImageSize width)
|
||||
{
|
||||
{
|
||||
std::shared_lock lock {_cacheMutex};
|
||||
|
||||
if (auto it {_defaultCache.find(width)}; it != std::cend(_defaultCache))
|
||||
if (auto it {_defaultCoverCache.find(width)}; it != std::cend(_defaultCoverCache))
|
||||
return it->second;
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_lock lock {_cacheMutex};
|
||||
|
||||
if (auto it {_defaultCache.find(width)}; it != std::cend(_defaultCache))
|
||||
if (auto it {_defaultCoverCache.find(width)}; it != std::cend(_defaultCoverCache))
|
||||
return it->second;
|
||||
|
||||
RawImage rawImage {*_defaultCover};
|
||||
rawImage.scale(width);
|
||||
EncodedImage res {rawImage.encode()};
|
||||
std::shared_ptr<IEncodedImage> image {getFromFile(_defaultCoverPath, width)};
|
||||
_defaultCoverCache[width] = image;
|
||||
LMS_LOG(COVER, DEBUG) << "Default cache entries = " << _defaultCoverCache.size();
|
||||
|
||||
_defaultCache[width] = res;
|
||||
LMS_LOG(COVER, DEBUG) << "Default cache entries = " << _defaultCache.size();
|
||||
|
||||
return res;
|
||||
return image;
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<EncodedImage>
|
||||
Grabber::getFromDirectory(const std::filesystem::path& p, std::string_view preferredFileName, Width width) const
|
||||
std::unique_ptr<IEncodedImage>
|
||||
Grabber::getFromDirectory(const std::filesystem::path& p, std::string_view preferredFileName, ImageSize width) const
|
||||
{
|
||||
const std::multimap<std::string, std::filesystem::path> coverPaths {getCoverPaths(p)};
|
||||
|
||||
auto tryLoadImageFromFilename = [&](std::string_view fileName)
|
||||
{
|
||||
std::optional<EncodedImage> image;
|
||||
std::unique_ptr<IEncodedImage> image;
|
||||
|
||||
auto range {coverPaths.equal_range(std::string {fileName})};
|
||||
for (auto it {range.first}; it != range.second; ++it)
|
||||
{
|
||||
image = getFromFile(it->second, width);
|
||||
if (!image)
|
||||
continue;
|
||||
if (image)
|
||||
break;
|
||||
}
|
||||
return image;
|
||||
};
|
||||
|
||||
std::optional<EncodedImage> image;
|
||||
std::unique_ptr<IEncodedImage> image;
|
||||
|
||||
if (!preferredFileName.empty())
|
||||
{
|
||||
@@ -225,10 +231,10 @@ Grabber::getCoverPaths(const std::filesystem::path& directoryPath) const
|
||||
return res;
|
||||
}
|
||||
|
||||
std::optional<EncodedImage>
|
||||
Grabber::getFromTrack(const std::filesystem::path& p, Width width) const
|
||||
std::unique_ptr<IEncodedImage>
|
||||
Grabber::getFromTrack(const std::filesystem::path& p, ImageSize width) const
|
||||
{
|
||||
std::optional<EncodedImage> image;
|
||||
std::unique_ptr<IEncodedImage> image;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -244,16 +250,16 @@ Grabber::getFromTrack(const std::filesystem::path& p, Width width) const
|
||||
return image;
|
||||
}
|
||||
|
||||
EncodedImage
|
||||
Grabber::getFromTrackInternal(Database::Session& dbSession, Database::IdType trackId, Width width)
|
||||
std::shared_ptr<IEncodedImage>
|
||||
Grabber::getFromTrack(Database::Session& dbSession, Database::IdType trackId, ImageSize width)
|
||||
{
|
||||
using namespace Database;
|
||||
|
||||
const CacheEntryDesc cacheEntryDesc {CacheEntryDesc::Type::Track, trackId, width};
|
||||
std::optional<EncodedImage> cover {loadFromCache(cacheEntryDesc)};
|
||||
|
||||
std::shared_ptr<IEncodedImage> cover {loadFromCache(cacheEntryDesc)};
|
||||
if (cover)
|
||||
return *cover;
|
||||
return cover;
|
||||
|
||||
bool hasCover {};
|
||||
bool isMultiDisc {};
|
||||
@@ -289,19 +295,20 @@ Grabber::getFromTrackInternal(Database::Session& dbSession, Database::IdType tra
|
||||
if (!cover)
|
||||
cover = getDefault(width);
|
||||
|
||||
saveToCache(cacheEntryDesc, *cover);
|
||||
if (cover)
|
||||
saveToCache(cacheEntryDesc, cover);
|
||||
|
||||
return *cover;
|
||||
return cover;
|
||||
}
|
||||
|
||||
EncodedImage
|
||||
Grabber::getFromReleaseInternal(Database::Session& session, Database::IdType releaseId, Width width)
|
||||
std::shared_ptr<IEncodedImage>
|
||||
Grabber::getFromRelease(Database::Session& session, Database::IdType releaseId, ImageSize width)
|
||||
{
|
||||
const CacheEntryDesc cacheEntryDesc {CacheEntryDesc::Type::Release, releaseId, width};
|
||||
std::optional<EncodedImage> cover {loadFromCache(cacheEntryDesc)};
|
||||
|
||||
std::shared_ptr<IEncodedImage> cover {loadFromCache(cacheEntryDesc)};
|
||||
if (cover)
|
||||
return *cover;
|
||||
return cover;
|
||||
|
||||
std::optional<Database::IdType> trackId;
|
||||
{
|
||||
@@ -317,16 +324,14 @@ Grabber::getFromReleaseInternal(Database::Session& session, Database::IdType rel
|
||||
}
|
||||
|
||||
if (trackId)
|
||||
{
|
||||
cover = getFromTrackInternal(session, *trackId, width);
|
||||
}
|
||||
|
||||
if (!cover)
|
||||
cover = getFromTrack(session, *trackId, width);
|
||||
else
|
||||
cover = getDefault(width);
|
||||
|
||||
saveToCache(cacheEntryDesc, *cover);
|
||||
if (cover)
|
||||
saveToCache(cacheEntryDesc, cover);
|
||||
|
||||
return *cover;
|
||||
return cover;
|
||||
}
|
||||
|
||||
void
|
||||
@@ -341,36 +346,23 @@ Grabber::flushCache()
|
||||
_cache.clear();
|
||||
}
|
||||
|
||||
std::unique_ptr<ICoverArt>
|
||||
Grabber::getFromTrack(Database::Session& session, Database::IdType trackId, std::size_t width)
|
||||
{
|
||||
CoverArt toto {getFromTrackInternal(session, trackId, width)};
|
||||
return std::make_unique<CoverArt>(getFromTrackInternal(session, trackId, width));
|
||||
}
|
||||
|
||||
std::unique_ptr<ICoverArt>
|
||||
Grabber::getFromRelease(Database::Session& session, Database::IdType releaseId, std::size_t width)
|
||||
{
|
||||
return std::make_unique<CoverArt>(getFromReleaseInternal(session, releaseId, width));
|
||||
}
|
||||
|
||||
void
|
||||
Grabber::saveToCache(const CacheEntryDesc& entryDesc, const EncodedImage& image)
|
||||
Grabber::saveToCache(const CacheEntryDesc& entryDesc, std::shared_ptr<IEncodedImage> image)
|
||||
{
|
||||
std::unique_lock lock {_cacheMutex};
|
||||
|
||||
while (_cacheSize + image.getDataSize() > _maxCacheSize && !_cache.empty())
|
||||
while (_cacheSize + image->getDataSize() > _maxCacheSize && !_cache.empty())
|
||||
{
|
||||
auto it {Random::pickRandom(_cache)};
|
||||
_cacheSize -= it->second.getDataSize();
|
||||
_cache.erase(it);
|
||||
auto itRandom {Random::pickRandom(_cache)};
|
||||
_cacheSize -= itRandom->second->getDataSize();
|
||||
_cache.erase(itRandom);
|
||||
}
|
||||
|
||||
_cacheSize += image.getDataSize();
|
||||
_cacheSize += image->getDataSize();
|
||||
_cache[entryDesc] = image;
|
||||
}
|
||||
|
||||
std::optional<EncodedImage>
|
||||
std::shared_ptr<IEncodedImage>
|
||||
Grabber::loadFromCache(const CacheEntryDesc& entryDesc)
|
||||
{
|
||||
std::shared_lock lock {_cacheMutex};
|
||||
@@ -379,7 +371,7 @@ Grabber::loadFromCache(const CacheEntryDesc& entryDesc)
|
||||
if (it == std::cend(_cache))
|
||||
{
|
||||
++_cacheMisses;
|
||||
return std::nullopt;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
++_cacheHits;
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
|
||||
#include <atomic>
|
||||
#include <filesystem>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <shared_mutex>
|
||||
#include <string_view>
|
||||
@@ -28,14 +29,19 @@
|
||||
#include <vector>
|
||||
|
||||
#include "cover/ICoverArtGrabber.hpp"
|
||||
#include "cover/IEncodedImage.hpp"
|
||||
#include "database/Types.hpp"
|
||||
#include "Image.hpp"
|
||||
|
||||
namespace Database
|
||||
{
|
||||
class Session;
|
||||
}
|
||||
|
||||
namespace Av
|
||||
{
|
||||
class MediaFile;
|
||||
}
|
||||
|
||||
namespace CoverArt
|
||||
{
|
||||
struct CacheEntryDesc
|
||||
@@ -83,7 +89,11 @@ namespace CoverArt
|
||||
class Grabber : public IGrabber
|
||||
{
|
||||
public:
|
||||
Grabber(const std::filesystem::path& execPath, std::size_t maxCacheEntries, std::size_t maxFileSize);
|
||||
Grabber(const std::filesystem::path& execPath,
|
||||
const std::filesystem::path& defaultCoverPath,
|
||||
std::size_t maxCacheEntries,
|
||||
std::size_t maxFileSize,
|
||||
unsigned jpegQuality);
|
||||
|
||||
Grabber(const Grabber&) = delete;
|
||||
Grabber& operator=(const Grabber&) = delete;
|
||||
@@ -91,38 +101,34 @@ namespace CoverArt
|
||||
Grabber& operator=(Grabber&&) = delete;
|
||||
|
||||
private:
|
||||
|
||||
void setDefaultCover(const std::filesystem::path& defaultCoverPath) override;
|
||||
std::unique_ptr<ICoverArt> getFromTrack(Database::Session& dbSession, Database::IdType trackId, Width width) override;
|
||||
std::unique_ptr<ICoverArt> getFromRelease(Database::Session& dbSession, Database::IdType releaseId, Width width) override;
|
||||
std::shared_ptr<IEncodedImage> getFromTrack(Database::Session& dbSession, Database::IdType trackId, ImageSize width) override;
|
||||
std::shared_ptr<IEncodedImage> getFromRelease(Database::Session& dbSession, Database::IdType releaseId, ImageSize width) override;
|
||||
void flushCache() override;
|
||||
|
||||
EncodedImage getFromTrackInternal(Database::Session& dbSession, Database::IdType trackId, Width width);
|
||||
EncodedImage getFromReleaseInternal(Database::Session& dbSession, Database::IdType releaseId, Width width);
|
||||
std::unique_ptr<IEncodedImage> getFromAvMediaFile(const Av::MediaFile& input, ImageSize width) const;
|
||||
std::unique_ptr<IEncodedImage> getFromFile(const std::filesystem::path& p, ImageSize width) const;
|
||||
|
||||
std::optional<EncodedImage> getFromTrack(const std::filesystem::path& path, Width width) const;
|
||||
std::unique_ptr<IEncodedImage> getFromTrack(const std::filesystem::path& path, ImageSize width) const;
|
||||
std::multimap<std::string, std::filesystem::path> getCoverPaths(const std::filesystem::path& directoryPath) const;
|
||||
std::optional<EncodedImage> getFromDirectory(const std::filesystem::path& path, std::string_view preferredFileName, Width width) const;
|
||||
EncodedImage getDefault(Width width);
|
||||
|
||||
EncodedImage resizeCoverOrFallback(EncodedImage image, Width width) const;
|
||||
|
||||
std::optional<EncodedImage> _defaultCover; // optional to defer initializing
|
||||
std::unique_ptr<IEncodedImage> getFromDirectory(const std::filesystem::path& path, std::string_view preferredFileName, ImageSize width) const;
|
||||
std::shared_ptr<IEncodedImage> getDefault(ImageSize width);
|
||||
|
||||
std::shared_mutex _cacheMutex;
|
||||
std::unordered_map<CacheEntryDesc, EncodedImage> _cache;
|
||||
std::unordered_map<Width, EncodedImage> _defaultCache;
|
||||
std::unordered_map<CacheEntryDesc, std::shared_ptr<IEncodedImage>> _cache;
|
||||
std::unordered_map<ImageSize, std::shared_ptr<IEncodedImage>> _defaultCoverCache;
|
||||
std::atomic<std::size_t> _cacheMisses {};
|
||||
std::atomic<std::size_t> _cacheHits {};
|
||||
std::size_t _cacheSize {};
|
||||
|
||||
void saveToCache(const CacheEntryDesc& entryDesc, const EncodedImage& image);
|
||||
std::optional<EncodedImage> loadFromCache(const CacheEntryDesc& entryDesc);
|
||||
void saveToCache(const CacheEntryDesc& entryDesc, std::shared_ptr<IEncodedImage> image);
|
||||
std::shared_ptr<IEncodedImage> loadFromCache(const CacheEntryDesc& entryDesc);
|
||||
|
||||
const std::filesystem::path _defaultCoverPath;
|
||||
const std::size_t _maxCacheSize;
|
||||
static inline const std::vector<std::filesystem::path> _fileExtensions {".jpg", ".jpeg", ".png", ".bmp"}; // TODO parametrize
|
||||
const std::size_t _maxFileSize;
|
||||
static inline const std::vector<std::string> _preferredFileNames {"cover", "front"}; // TODO parametrize
|
||||
const unsigned _jpegQuality;
|
||||
};
|
||||
|
||||
} // namespace CoverArt
|
||||
|
||||
@@ -19,17 +19,10 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <vector>
|
||||
|
||||
#include <Magick++.h>
|
||||
|
||||
#include "utils/Exception.hpp"
|
||||
|
||||
namespace CoverArt
|
||||
{
|
||||
void init(const std::filesystem::path& path);
|
||||
|
||||
// internal use only
|
||||
class ImageException : public LmsException
|
||||
{
|
||||
@@ -37,37 +30,5 @@ namespace CoverArt
|
||||
using LmsException::LmsException;
|
||||
};
|
||||
|
||||
class EncodedImage
|
||||
{
|
||||
public:
|
||||
EncodedImage() = default;
|
||||
EncodedImage(const std::byte* data, std::size_t dataSize);
|
||||
|
||||
const std::byte* getData() const;
|
||||
std::size_t getDataSize() const;
|
||||
|
||||
private:
|
||||
friend class RawImage;
|
||||
EncodedImage(Magick::Blob blob);
|
||||
|
||||
Magick::Blob _blob;
|
||||
};
|
||||
|
||||
class RawImage
|
||||
{
|
||||
public:
|
||||
RawImage(const std::filesystem::path& p);
|
||||
RawImage(const EncodedImage& encodedImage);
|
||||
|
||||
// Operations
|
||||
void scale(std::size_t width);
|
||||
|
||||
// output
|
||||
EncodedImage encode() const;
|
||||
|
||||
private:
|
||||
Magick::Image _image;
|
||||
};
|
||||
|
||||
} // namespace CoverArt
|
||||
|
||||
@@ -17,31 +17,19 @@
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "CoverArt.hpp"
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "cover/IEncodedImage.hpp"
|
||||
|
||||
namespace CoverArt
|
||||
{
|
||||
|
||||
CoverArt::CoverArt(EncodedImage image)
|
||||
: _image {image}
|
||||
{}
|
||||
|
||||
const std::byte*
|
||||
CoverArt::getData() const
|
||||
class IRawImage
|
||||
{
|
||||
return _image.getData();
|
||||
}
|
||||
public:
|
||||
virtual void resize(ImageSize width) = 0;
|
||||
virtual std::unique_ptr<IEncodedImage> encodeToJPEG(unsigned quality) const = 0;
|
||||
};
|
||||
}
|
||||
|
||||
std::size_t
|
||||
CoverArt::getDataSize() const
|
||||
{
|
||||
return _image.getDataSize();
|
||||
}
|
||||
|
||||
std::string_view
|
||||
CoverArt::getMimeType() const
|
||||
{
|
||||
return "image/jpeg";
|
||||
}
|
||||
|
||||
} // namespace CoverArt
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "JPEGImage.hpp"
|
||||
|
||||
#include "Exception.hpp"
|
||||
#include "RawImage.hpp"
|
||||
#include "utils/Logger.hpp"
|
||||
|
||||
namespace CoverArt::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 ImageException {std::string {"Magick read error: "} + e.what()};
|
||||
}
|
||||
}
|
||||
|
||||
const std::byte*
|
||||
JPEGImage::getData() const
|
||||
{
|
||||
return reinterpret_cast<const std::byte*>(_blob.data());
|
||||
}
|
||||
|
||||
std::size_t
|
||||
JPEGImage::getDataSize() const
|
||||
{
|
||||
return _blob.length();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef LMS_SUPPORT_IMAGE_GM
|
||||
#error "Bad configuration"
|
||||
#endif
|
||||
|
||||
#include <Magick++.h>
|
||||
|
||||
#include "cover/IEncodedImage.hpp"
|
||||
|
||||
namespace CoverArt::GraphicsMagick
|
||||
{
|
||||
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"; }
|
||||
|
||||
Magick::Blob _blob;
|
||||
};
|
||||
}
|
||||
@@ -17,22 +17,24 @@
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "Image.hpp"
|
||||
|
||||
#include <atomic>
|
||||
#include <fstream>
|
||||
#include "RawImage.hpp"
|
||||
|
||||
#include <magick/resource.h>
|
||||
|
||||
#include "utils/Logger.hpp"
|
||||
#include "JPEGImage.hpp"
|
||||
#include "Exception.hpp"
|
||||
|
||||
namespace CoverArt {
|
||||
namespace CoverArt::GraphicsMagick {
|
||||
|
||||
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 (!MagickLib::SetMagickResourceLimit(MagickLib::ThreadsResource, 1))
|
||||
LMS_LOG(COVER, ERROR) << "Cannot set Magick thread resource limit to 1!";
|
||||
|
||||
@@ -43,26 +45,27 @@ init(const std::filesystem::path& path)
|
||||
LMS_LOG(COVER, INFO) << "Magick Disk resource limit = " << GetMagickResourceLimit(MagickLib::DiskResource);
|
||||
}
|
||||
|
||||
EncodedImage::EncodedImage(const std::byte* data, std::size_t dataSize)
|
||||
: _blob {data, dataSize}
|
||||
RawImage::RawImage(const std::byte* encodedData, std::size_t encodedDataSize)
|
||||
{
|
||||
}
|
||||
|
||||
EncodedImage::EncodedImage(Magick::Blob blob)
|
||||
: _blob {blob}
|
||||
{
|
||||
}
|
||||
|
||||
const std::byte*
|
||||
EncodedImage::getData() const
|
||||
{
|
||||
return reinterpret_cast<const std::byte*>(_blob.data());
|
||||
}
|
||||
|
||||
std::size_t
|
||||
EncodedImage::getDataSize() const
|
||||
{
|
||||
return _blob.length();
|
||||
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 ImageException {std::string {"Magick read warning: "} + e.what()};
|
||||
}
|
||||
catch (Magick::Exception& e)
|
||||
{
|
||||
LMS_LOG(COVER, ERROR) << "Caught Magick exception: " << e.what();
|
||||
throw ImageException {std::string {"Magick read error: "} + e.what()};
|
||||
}
|
||||
}
|
||||
|
||||
RawImage::RawImage(const std::filesystem::path& p)
|
||||
@@ -73,79 +76,45 @@ RawImage::RawImage(const std::filesystem::path& p)
|
||||
}
|
||||
catch (Magick::WarningCoder& e)
|
||||
{
|
||||
LMS_LOG(COVER, WARNING) << "Caught Magick WarningCoder while loading image '" << p.string() << "': " << e.what();
|
||||
LMS_LOG(COVER, WARNING) << "Caught Magick WarningCoder: " << e.what();
|
||||
}
|
||||
catch (Magick::Warning& e)
|
||||
{
|
||||
LMS_LOG(COVER, WARNING) << "Caught Magick warning while loading raw image '" << p.string() << "': " << e.what();
|
||||
LMS_LOG(COVER, WARNING) << "Caught Magick warning: " << e.what();
|
||||
throw ImageException {std::string {"Magick read warning: "} + e.what()};
|
||||
}
|
||||
catch (Magick::Exception& e)
|
||||
{
|
||||
LMS_LOG(COVER, ERROR) << "Caught Magick exception while loading raw image '" << p.string() << "': " << e.what();
|
||||
throw ImageException {std::string {"Magick read error: "} + e.what()};
|
||||
}
|
||||
}
|
||||
|
||||
RawImage::RawImage(const EncodedImage& encodedImage)
|
||||
{
|
||||
try
|
||||
{
|
||||
_image.read(encodedImage._blob);
|
||||
}
|
||||
catch (Magick::WarningCoder& e)
|
||||
{
|
||||
LMS_LOG(COVER, WARNING) << "Caught Magick WarningCoder while loading raw image: " << e.what();
|
||||
}
|
||||
catch (Magick::Warning& e)
|
||||
{
|
||||
LMS_LOG(COVER, WARNING) << "Caught Magick warning while loading raw image: " << e.what();
|
||||
throw ImageException {std::string {"Magick read warning: "} + e.what()};
|
||||
}
|
||||
catch (Magick::Exception& e)
|
||||
{
|
||||
LMS_LOG(COVER, ERROR) << "Caught Magick exception while loading raw image: " << e.what();
|
||||
LMS_LOG(COVER, ERROR) << "Caught Magick exception: " << e.what();
|
||||
throw ImageException {std::string {"Magick read error: "} + e.what()};
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
RawImage::scale(std::size_t width)
|
||||
RawImage::resize(ImageSize width)
|
||||
{
|
||||
if (width == 0)
|
||||
throw ImageException {"Bad width = 0"};
|
||||
|
||||
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 during scale: " << e.what();
|
||||
LMS_LOG(COVER, ERROR) << "Caught Magick exception while resizing: " << e.what();
|
||||
throw ImageException {std::string {"Magick resize error: "} + e.what()};
|
||||
}
|
||||
}
|
||||
|
||||
EncodedImage
|
||||
RawImage::encode() const
|
||||
std::unique_ptr<IEncodedImage>
|
||||
RawImage::encodeToJPEG(unsigned quality) const
|
||||
{
|
||||
try
|
||||
{
|
||||
Magick::Image outputImage {_image};
|
||||
|
||||
outputImage.magick("JPEG");
|
||||
|
||||
Magick::Blob blob;
|
||||
outputImage.write(&blob);
|
||||
|
||||
return EncodedImage {blob};
|
||||
}
|
||||
catch (Magick::Exception& e)
|
||||
{
|
||||
LMS_LOG(COVER, ERROR) << "Caught Magick exception while encoding raw image: " << e.what();
|
||||
throw ImageException {std::string {"Magick encode error: "} + e.what()};
|
||||
}
|
||||
return std::make_unique<JPEGImage>(*this, quality);
|
||||
}
|
||||
|
||||
} // namespace CoverArt
|
||||
Magick::Image
|
||||
RawImage::getMagickImage() const
|
||||
{
|
||||
return _image;
|
||||
}
|
||||
|
||||
} // namespace CoverArt::GraphicsMagick
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef LMS_SUPPORT_IMAGE_GM
|
||||
#error "Bad configuration"
|
||||
#endif
|
||||
|
||||
#include <Magick++.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <filesystem>
|
||||
|
||||
#include "cover/IEncodedImage.hpp"
|
||||
#include "IRawImage.hpp"
|
||||
|
||||
namespace CoverArt::GraphicsMagick
|
||||
{
|
||||
void init(const std::filesystem::path& path);
|
||||
|
||||
class RawImage : 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;
|
||||
|
||||
private:
|
||||
friend class JPEGImage;
|
||||
Magick::Image getMagickImage() const;
|
||||
|
||||
Magick::Image _image;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "JPEGImage.hpp"
|
||||
|
||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||
#include <stb/stb_image_write.h>
|
||||
|
||||
#include "RawImage.hpp"
|
||||
#include "Exception.hpp"
|
||||
|
||||
namespace CoverArt::STB
|
||||
{
|
||||
JPEGImage::JPEGImage(const RawImage& rawImage, unsigned quality)
|
||||
{
|
||||
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 ImageException {"Failed to export in jpeg format!"};
|
||||
}
|
||||
}
|
||||
|
||||
const std::byte*
|
||||
JPEGImage::getData() const
|
||||
{
|
||||
if (_data.empty())
|
||||
return nullptr;
|
||||
|
||||
return &_data.front();
|
||||
}
|
||||
|
||||
std::size_t
|
||||
JPEGImage::getDataSize() const
|
||||
{
|
||||
return _data.size();
|
||||
}
|
||||
}
|
||||
@@ -17,25 +17,23 @@
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <vector>
|
||||
|
||||
#include "cover/ICoverArt.hpp"
|
||||
#include "Image.hpp"
|
||||
#include "cover/IEncodedImage.hpp"
|
||||
|
||||
namespace CoverArt
|
||||
namespace CoverArt::STB
|
||||
{
|
||||
|
||||
class CoverArt : public ICoverArt
|
||||
class RawImage;
|
||||
class JPEGImage : public IEncodedImage
|
||||
{
|
||||
public:
|
||||
CoverArt(EncodedImage image);
|
||||
|
||||
const std::byte* getData() const override;
|
||||
std::size_t getDataSize() const override;
|
||||
std::string_view getMimeType() const override;
|
||||
JPEGImage(const RawImage& rawImage, unsigned quality);
|
||||
|
||||
private:
|
||||
EncodedImage _image;
|
||||
};
|
||||
const std::byte* getData() const override;
|
||||
std::size_t getDataSize() const override;
|
||||
std::string_view getMimeType() const override { return "image/jpeg"; }
|
||||
|
||||
} // namespace CoverArt
|
||||
std::vector<std::byte> _data;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "RawImage.hpp"
|
||||
|
||||
#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
|
||||
|
||||
#include <stb/stb_image.h>
|
||||
#include <stb/stb_image_resize.h>
|
||||
|
||||
#include "JPEGImage.hpp"
|
||||
|
||||
#include "Exception.hpp"
|
||||
|
||||
namespace CoverArt::STB
|
||||
{
|
||||
RawImage::RawImage(const std::byte* encodedData, std::size_t encodedDataSize)
|
||||
{
|
||||
int n;
|
||||
_data = UniquePtrFree {stbi_load_from_memory(reinterpret_cast<const stbi_uc*>(encodedData), encodedDataSize, &_width, &_height, &n, 3), std::free};
|
||||
if (!_data)
|
||||
throw ImageException {"Cannot load image from memory"};
|
||||
}
|
||||
|
||||
RawImage::RawImage(const std::filesystem::path& p)
|
||||
{
|
||||
int n;
|
||||
_data = UniquePtrFree {stbi_load(p.string().c_str(), &_width, &_height, &n, 3), std::free};
|
||||
if (!_data)
|
||||
throw ImageException {"Cannot load image from memory"};
|
||||
}
|
||||
|
||||
void
|
||||
RawImage::resize(ImageSize width)
|
||||
{
|
||||
size_t height;
|
||||
if (_width == _height)
|
||||
{
|
||||
height = width;
|
||||
}
|
||||
else if (_width > _height)
|
||||
{
|
||||
height = (size_t)((float)width/_width*_height);
|
||||
}
|
||||
else
|
||||
{
|
||||
height = width;
|
||||
width = (size_t)((float)height/_height*_width);
|
||||
}
|
||||
|
||||
UniquePtrFree resizedData {reinterpret_cast<unsigned char*>(malloc(width*height*3)), std::free};
|
||||
if (!resizedData)
|
||||
throw ImageException {"Cannot allocate memory for resized image!"};
|
||||
|
||||
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)
|
||||
{
|
||||
throw ImageException {"Failed to resize image!"};
|
||||
}
|
||||
|
||||
_data = std::move(resizedData);
|
||||
_height = height;
|
||||
_width = width;
|
||||
}
|
||||
|
||||
std::unique_ptr<IEncodedImage>
|
||||
RawImage::encodeToJPEG(unsigned quality) const
|
||||
{
|
||||
return std::make_unique<JPEGImage>(*this, quality);
|
||||
}
|
||||
|
||||
ImageSize
|
||||
RawImage::getWidth() const
|
||||
{
|
||||
return _width;
|
||||
}
|
||||
|
||||
ImageSize
|
||||
RawImage::getHeight() const
|
||||
{
|
||||
return _height;
|
||||
}
|
||||
|
||||
const std::byte*
|
||||
RawImage::getData() const
|
||||
{
|
||||
if (!_data)
|
||||
return nullptr;
|
||||
|
||||
return reinterpret_cast<const std::byte*>(_data.get());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef LMS_SUPPORT_IMAGE_STB
|
||||
#error "Bad configuration"
|
||||
#endif
|
||||
|
||||
#include <cstddef>
|
||||
#include <filesystem>
|
||||
|
||||
#include "cover/IEncodedImage.hpp"
|
||||
#include "IRawImage.hpp"
|
||||
|
||||
namespace CoverArt::STB
|
||||
{
|
||||
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;
|
||||
|
||||
ImageSize getWidth() const;
|
||||
ImageSize getHeight() const;
|
||||
const std::byte* getData() const;
|
||||
|
||||
private:
|
||||
int _width;
|
||||
int _height;
|
||||
using UniquePtrFree = std::unique_ptr<unsigned char, decltype(&std::free)>;
|
||||
UniquePtrFree _data {nullptr, std::free};
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user