Subsonic API: getCoverArt: serve raw image if size is not provided
This commit is contained in:
@@ -242,11 +242,10 @@ namespace lms::av
|
|||||||
void AudioFile::visitAttachedPictures(std::function<void(const Picture&, const MetadataMap&)> func) const
|
void AudioFile::visitAttachedPictures(std::function<void(const Picture&, const MetadataMap&)> func) const
|
||||||
{
|
{
|
||||||
static const std::unordered_map<int, std::string> codecMimeMap{
|
static const std::unordered_map<int, std::string> codecMimeMap{
|
||||||
{ AV_CODEC_ID_BMP, "image/x-bmp" },
|
{ AV_CODEC_ID_BMP, "image/bmp" },
|
||||||
{ AV_CODEC_ID_GIF, "image/gif" },
|
{ AV_CODEC_ID_GIF, "image/gif" },
|
||||||
{ AV_CODEC_ID_MJPEG, "image/jpeg" },
|
{ AV_CODEC_ID_MJPEG, "image/jpeg" },
|
||||||
{ AV_CODEC_ID_PNG, "image/png" },
|
{ AV_CODEC_ID_PNG, "image/png" },
|
||||||
{ AV_CODEC_ID_PNG, "image/x-png" },
|
|
||||||
{ AV_CODEC_ID_PPM, "image/x-portable-pixmap" },
|
{ AV_CODEC_ID_PPM, "image/x-portable-pixmap" },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
add_library(lmsimage SHARED
|
add_library(lmsimage SHARED
|
||||||
impl/SvgImage.cpp
|
impl/EncodedImage.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(lmsimage INTERFACE
|
target_include_directories(lmsimage INTERFACE
|
||||||
@@ -26,7 +26,6 @@ if (${LMS_IMAGE_BACKEND} STREQUAL "stb")
|
|||||||
|
|
||||||
target_sources(lmsimage PRIVATE
|
target_sources(lmsimage PRIVATE
|
||||||
impl/stb/Image.cpp
|
impl/stb/Image.cpp
|
||||||
impl/stb/JPEGImage.cpp
|
|
||||||
impl/stb/RawImage.cpp
|
impl/stb/RawImage.cpp
|
||||||
)
|
)
|
||||||
target_compile_options(lmsimage PRIVATE "-DSTB_IMAGE_RESIZE_VERSION=${STB_IMAGE_RESIZE_VERSION}")
|
target_compile_options(lmsimage PRIVATE "-DSTB_IMAGE_RESIZE_VERSION=${STB_IMAGE_RESIZE_VERSION}")
|
||||||
@@ -38,7 +37,6 @@ elseif (${LMS_IMAGE_BACKEND} STREQUAL "graphicsmagick")
|
|||||||
|
|
||||||
target_sources(lmsimage PRIVATE
|
target_sources(lmsimage PRIVATE
|
||||||
impl/graphicsmagick/Image.cpp
|
impl/graphicsmagick/Image.cpp
|
||||||
impl/graphicsmagick/JPEGImage.cpp
|
|
||||||
impl/graphicsmagick/RawImage.cpp
|
impl/graphicsmagick/RawImage.cpp
|
||||||
)
|
)
|
||||||
target_link_libraries(lmsimage PRIVATE PkgConfig::GraphicsMagick++)
|
target_link_libraries(lmsimage PRIVATE PkgConfig::GraphicsMagick++)
|
||||||
|
|||||||
@@ -0,0 +1,103 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2015 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 "EncodedImage.hpp"
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
#include "core/ITraceLogger.hpp"
|
||||||
|
#include "core/String.hpp"
|
||||||
|
#include "image/Exception.hpp"
|
||||||
|
|
||||||
|
namespace lms::image
|
||||||
|
{
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
std::string_view extensionToMimeType(const std::filesystem::path& extension)
|
||||||
|
{
|
||||||
|
static const std::unordered_map<std::string_view, std::string_view> mimeTypesByExtension{
|
||||||
|
{ ".bmp", "image/bmp" },
|
||||||
|
{ ".gif", "image/gif" },
|
||||||
|
{ ".jpeg", "image/jpeg" },
|
||||||
|
{ ".jpg", "image/jpeg" },
|
||||||
|
{ ".png", "image/png" },
|
||||||
|
{ ".ppm", "image/x-portable-pixmap" },
|
||||||
|
{ ".svg", "image/svg+xml" },
|
||||||
|
};
|
||||||
|
|
||||||
|
const auto it{ mimeTypesByExtension.find(core::stringUtils::stringToLower(extension.c_str())) };
|
||||||
|
if (it == std::cend(mimeTypesByExtension))
|
||||||
|
throw Exception{ "Unhandled image extension '" + extension.string() + "'" };
|
||||||
|
return it->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::byte> fileToBuffer(const std::filesystem::path& p)
|
||||||
|
{
|
||||||
|
LMS_SCOPED_TRACE_DETAILED("Image", "ReadFile");
|
||||||
|
|
||||||
|
std::ifstream ifs{ p.string(), std::ios::binary };
|
||||||
|
if (!ifs.is_open())
|
||||||
|
throw Exception{ "Cannot open file '" + p.string() + "' for reading purpose" };
|
||||||
|
|
||||||
|
std::vector<std::byte> data;
|
||||||
|
// read file content
|
||||||
|
ifs.seekg(0, std::ios::end);
|
||||||
|
std::streamsize size = ifs.tellg();
|
||||||
|
if (size < 0)
|
||||||
|
throw Exception{ "Cannot determine file size for '" + p.string() + "'" };
|
||||||
|
|
||||||
|
ifs.seekg(0, std::ios::beg);
|
||||||
|
data.resize(size);
|
||||||
|
if (!ifs.read(reinterpret_cast<char*>(data.data()), size))
|
||||||
|
throw Exception{ "Cannot read file content for '" + p.string() + "'" };
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
std::unique_ptr<IEncodedImage> readImage(const std::filesystem::path& path)
|
||||||
|
{
|
||||||
|
return std::make_unique<EncodedImage>(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<IEncodedImage> readImage(std::span<const std::byte> encodedData, std::string_view mimeType)
|
||||||
|
{
|
||||||
|
return std::make_unique<EncodedImage>(encodedData, mimeType);
|
||||||
|
}
|
||||||
|
|
||||||
|
EncodedImage::EncodedImage(std::vector<std::byte>&& data, std::string_view mimeType)
|
||||||
|
: _data{ std::move(data) }
|
||||||
|
, _mimeType(mimeType)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
EncodedImage::EncodedImage(std::span<const std::byte> data, std::string_view mimeType)
|
||||||
|
: _data(std::cbegin(data), std::cend(data))
|
||||||
|
, _mimeType(mimeType)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
EncodedImage::EncodedImage(const std::filesystem::path& p)
|
||||||
|
: EncodedImage::EncodedImage{ fileToBuffer(p), extensionToMimeType(p.extension()) }
|
||||||
|
{
|
||||||
|
}
|
||||||
|
} // namespace lms::image
|
||||||
@@ -19,23 +19,28 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "image/IEncodedImage.hpp"
|
#include "image/IEncodedImage.hpp"
|
||||||
|
|
||||||
namespace lms::image
|
namespace lms::image
|
||||||
{
|
{
|
||||||
class SvgImage : public IEncodedImage
|
class EncodedImage : public IEncodedImage
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
SvgImage(std::vector<std::byte>&& data)
|
EncodedImage(const std::filesystem::path& path);
|
||||||
: _data{ std::move(data) } {}
|
EncodedImage(std::vector<std::byte>&& data, std::string_view mimeType);
|
||||||
|
EncodedImage(std::span<const std::byte> data, std::string_view mimeType);
|
||||||
|
~EncodedImage() override = default;
|
||||||
|
EncodedImage(const EncodedImage&) = delete;
|
||||||
|
EncodedImage& operator=(const EncodedImage&) = delete;
|
||||||
|
|
||||||
const std::byte* getData() const override { return &_data.front(); }
|
std::span<const std::byte> getData() const override { return _data; }
|
||||||
std::size_t getDataSize() const override { return _data.size(); }
|
std::string_view getMimeType() const override { return _mimeType; }
|
||||||
std::string_view getMimeType() const override { return "image/svg+xml"; }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const std::vector<std::byte> _data;
|
const std::vector<std::byte> _data;
|
||||||
|
const std::string _mimeType;
|
||||||
};
|
};
|
||||||
} // namespace lms::image
|
} // namespace lms::image
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2015 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 "SvgImage.hpp"
|
|
||||||
|
|
||||||
#include <filesystem>
|
|
||||||
#include <fstream>
|
|
||||||
|
|
||||||
#include "core/ITraceLogger.hpp"
|
|
||||||
#include "image/Exception.hpp"
|
|
||||||
|
|
||||||
namespace lms::image
|
|
||||||
{
|
|
||||||
std::unique_ptr<IEncodedImage> readSvgFile(const std::filesystem::path& p)
|
|
||||||
{
|
|
||||||
LMS_SCOPED_TRACE_DETAILED("Image", "ReadSVG");
|
|
||||||
|
|
||||||
if (p.extension() != ".svg")
|
|
||||||
throw Exception{ "Unexpected file extension: '" + p.extension().string() + "', expected .svg" };
|
|
||||||
|
|
||||||
std::ifstream ifs{ p.string(), std::ios::binary };
|
|
||||||
if (!ifs.is_open())
|
|
||||||
throw Exception{ "Cannot open file '" + p.string() + "' for reading purpose" };
|
|
||||||
|
|
||||||
std::vector<std::byte> data;
|
|
||||||
// read file content
|
|
||||||
ifs.seekg(0, std::ios::end);
|
|
||||||
std::streamsize size = ifs.tellg();
|
|
||||||
if (size < 0)
|
|
||||||
throw Exception{ "Cannot determine file size for '" + p.string() + "'" };
|
|
||||||
|
|
||||||
ifs.seekg(0, std::ios::beg);
|
|
||||||
data.resize(size);
|
|
||||||
if (!ifs.read(reinterpret_cast<char*>(data.data()), size))
|
|
||||||
throw Exception{ "Cannot read file content for '" + p.string() + "'" };
|
|
||||||
|
|
||||||
return std::make_unique<SvgImage>(std::move(data));
|
|
||||||
}
|
|
||||||
} // namespace lms::image
|
|
||||||
@@ -19,26 +19,17 @@
|
|||||||
|
|
||||||
#include "image/Image.hpp"
|
#include "image/Image.hpp"
|
||||||
|
|
||||||
#include <array>
|
#include <memory>
|
||||||
|
|
||||||
#include "RawImage.hpp"
|
|
||||||
#include "core/ILogger.hpp"
|
#include "core/ILogger.hpp"
|
||||||
#include "core/ITraceLogger.hpp"
|
#include "core/ITraceLogger.hpp"
|
||||||
|
#include "image/Exception.hpp"
|
||||||
|
|
||||||
|
#include "EncodedImage.hpp"
|
||||||
|
#include "RawImage.hpp"
|
||||||
|
|
||||||
namespace lms::image
|
namespace lms::image
|
||||||
{
|
{
|
||||||
std::unique_ptr<IRawImage> decodeImage(const std::byte* encodedData, std::size_t encodedDataSize)
|
|
||||||
{
|
|
||||||
LMS_SCOPED_TRACE_DETAILED("Image", "DecodeBuffer");
|
|
||||||
return std::make_unique<GraphicsMagick::RawImage>(encodedData, encodedDataSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::unique_ptr<IRawImage> decodeImage(const std::filesystem::path& path)
|
|
||||||
{
|
|
||||||
LMS_SCOPED_TRACE_DETAILED("Image", "DecodeFile");
|
|
||||||
return std::make_unique<GraphicsMagick::RawImage>(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
void init(const std::filesystem::path& path)
|
void init(const std::filesystem::path& path)
|
||||||
{
|
{
|
||||||
Magick::InitializeMagick(path.string().c_str());
|
Magick::InitializeMagick(path.string().c_str());
|
||||||
@@ -61,4 +52,39 @@ namespace lms::image
|
|||||||
static const std::array<std::filesystem::path, 4> fileExtensions{ ".jpg", ".jpeg", ".png", ".bmp" };
|
static const std::array<std::filesystem::path, 4> fileExtensions{ ".jpg", ".jpeg", ".png", ".bmp" };
|
||||||
return fileExtensions;
|
return fileExtensions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<IRawImage> decodeImage(std::span<const std::byte> encodedData)
|
||||||
|
{
|
||||||
|
LMS_SCOPED_TRACE_DETAILED("Image", "DecodeBuffer");
|
||||||
|
return std::make_unique<GraphicsMagick::RawImage>(encodedData);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<IRawImage> decodeImage(const std::filesystem::path& path)
|
||||||
|
{
|
||||||
|
LMS_SCOPED_TRACE_DETAILED("Image", "DecodeFile");
|
||||||
|
return std::make_unique<GraphicsMagick::RawImage>(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<IEncodedImage> encodeToJPEG(const IRawImage& rawImage, unsigned quality)
|
||||||
|
{
|
||||||
|
LMS_SCOPED_TRACE_DETAILED("Image", "WriteJPEG");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Magick::Image image{ static_cast<const GraphicsMagick::RawImage&>(rawImage).getMagickImage() };
|
||||||
|
image.magick("JPEG");
|
||||||
|
image.quality(quality);
|
||||||
|
|
||||||
|
Magick::Blob blob;
|
||||||
|
image.write(&blob);
|
||||||
|
|
||||||
|
return std::make_unique<EncodedImage>(std::span{ static_cast<const std::byte*>(blob.data()), blob.length() }, "image/jpeg");
|
||||||
|
}
|
||||||
|
catch (Magick::Exception& e)
|
||||||
|
{
|
||||||
|
LMS_LOG(COVER, ERROR, "Caught Magick exception: " << e.what());
|
||||||
|
throw Exception{ std::string{ "Magick read error: " } + e.what() };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace lms::image
|
} // namespace lms::image
|
||||||
|
|||||||
@@ -1,57 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 "core/ILogger.hpp"
|
|
||||||
#include "core/ITraceLogger.hpp"
|
|
||||||
#include "image/Exception.hpp"
|
|
||||||
|
|
||||||
#include "RawImage.hpp"
|
|
||||||
|
|
||||||
namespace lms::image::GraphicsMagick
|
|
||||||
{
|
|
||||||
JPEGImage::JPEGImage(const RawImage& rawImage, unsigned quality)
|
|
||||||
{
|
|
||||||
LMS_SCOPED_TRACE_DETAILED("Image", "WriteJPEG");
|
|
||||||
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
|
|
||||||
std::size_t JPEGImage::getDataSize() const
|
|
||||||
{
|
|
||||||
return _blob.length();
|
|
||||||
}
|
|
||||||
} // namespace lms::image::GraphicsMagick
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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
|
|
||||||
|
|
||||||
#include <Magick++.h>
|
|
||||||
|
|
||||||
#include "image/IEncodedImage.hpp"
|
|
||||||
|
|
||||||
namespace lms::image::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;
|
|
||||||
};
|
|
||||||
} // namespace lms::image::GraphicsMagick
|
|
||||||
@@ -19,24 +19,19 @@
|
|||||||
|
|
||||||
#include "RawImage.hpp"
|
#include "RawImage.hpp"
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
#include <array>
|
|
||||||
|
|
||||||
#include <magick/resource.h>
|
#include <magick/resource.h>
|
||||||
|
|
||||||
#include "core/ILogger.hpp"
|
#include "core/ILogger.hpp"
|
||||||
#include "core/ITraceLogger.hpp"
|
#include "core/ITraceLogger.hpp"
|
||||||
#include "image/Exception.hpp"
|
#include "image/Exception.hpp"
|
||||||
|
|
||||||
#include "JPEGImage.hpp"
|
|
||||||
|
|
||||||
namespace lms::image::GraphicsMagick
|
namespace lms::image::GraphicsMagick
|
||||||
{
|
{
|
||||||
RawImage::RawImage(const std::byte* encodedData, std::size_t encodedDataSize)
|
RawImage::RawImage(std::span<const std::byte> encodedData)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Magick::Blob blob{ encodedData, encodedDataSize };
|
Magick::Blob blob{ encodedData.data(), encodedData.size() };
|
||||||
_image.read(blob);
|
_image.read(blob);
|
||||||
}
|
}
|
||||||
catch (Magick::WarningCoder& e)
|
catch (Magick::WarningCoder& e)
|
||||||
@@ -102,14 +97,8 @@ namespace lms::image::GraphicsMagick
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<IEncodedImage> RawImage::encodeToJPEG(unsigned quality) const
|
|
||||||
{
|
|
||||||
return std::make_unique<JPEGImage>(*this, quality);
|
|
||||||
}
|
|
||||||
|
|
||||||
Magick::Image RawImage::getMagickImage() const
|
Magick::Image RawImage::getMagickImage() const
|
||||||
{
|
{
|
||||||
return _image;
|
return _image;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace lms::image::GraphicsMagick
|
} // namespace lms::image::GraphicsMagick
|
||||||
|
|||||||
@@ -21,10 +21,10 @@
|
|||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
#include <span>
|
||||||
|
|
||||||
#include <Magick++.h>
|
#include <Magick++.h>
|
||||||
|
|
||||||
#include "image/IEncodedImage.hpp"
|
|
||||||
#include "image/IRawImage.hpp"
|
#include "image/IRawImage.hpp"
|
||||||
|
|
||||||
namespace lms::image::GraphicsMagick
|
namespace lms::image::GraphicsMagick
|
||||||
@@ -32,19 +32,17 @@ namespace lms::image::GraphicsMagick
|
|||||||
class RawImage : public IRawImage
|
class RawImage : public IRawImage
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
RawImage(const std::byte* encodedData, std::size_t encodedDataSize);
|
RawImage(std::span<const std::byte> encodedData);
|
||||||
RawImage(const std::filesystem::path& path);
|
RawImage(const std::filesystem::path& path);
|
||||||
|
|
||||||
ImageSize getWidth() const override;
|
ImageSize getWidth() const override;
|
||||||
ImageSize getHeight() const override;
|
ImageSize getHeight() const override;
|
||||||
|
|
||||||
void resize(ImageSize width) 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 getMagickImage() const;
|
||||||
|
|
||||||
|
private:
|
||||||
Magick::Image _image;
|
Magick::Image _image;
|
||||||
};
|
};
|
||||||
} // namespace lms::image::GraphicsMagick
|
} // namespace lms::image::GraphicsMagick
|
||||||
|
|||||||
@@ -21,24 +21,18 @@
|
|||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
|
||||||
#include "RawImage.hpp"
|
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||||
|
#include <stb_image_write.h>
|
||||||
|
|
||||||
#include "core/ITraceLogger.hpp"
|
#include "core/ITraceLogger.hpp"
|
||||||
|
|
||||||
|
#include "EncodedImage.hpp"
|
||||||
|
#include "RawImage.hpp"
|
||||||
|
#include "image/Exception.hpp"
|
||||||
|
|
||||||
namespace lms::image
|
namespace lms::image
|
||||||
{
|
{
|
||||||
std::unique_ptr<IRawImage> decodeImage(const std::byte* encodedData, std::size_t encodedDataSize)
|
void init(const std::filesystem::path& /*unused*/)
|
||||||
{
|
|
||||||
LMS_SCOPED_TRACE_DETAILED("Image", "DecodeBuffer");
|
|
||||||
return std::make_unique<STB::RawImage>(encodedData, encodedDataSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::unique_ptr<IRawImage> decodeImage(const std::filesystem::path& path)
|
|
||||||
{
|
|
||||||
LMS_SCOPED_TRACE_DETAILED("Image", "DecodeFile");
|
|
||||||
return std::make_unique<STB::RawImage>(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
void init(const std::filesystem::path&)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,4 +41,35 @@ namespace lms::image
|
|||||||
static const std::array<std::filesystem::path, 4> fileExtensions{ ".jpg", ".jpeg", ".png", ".bmp" };
|
static const std::array<std::filesystem::path, 4> fileExtensions{ ".jpg", ".jpeg", ".png", ".bmp" };
|
||||||
return fileExtensions;
|
return fileExtensions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<IRawImage> decodeImage(std::span<const std::byte> encodedData)
|
||||||
|
{
|
||||||
|
LMS_SCOPED_TRACE_DETAILED("Image", "DecodeBuffer");
|
||||||
|
return std::make_unique<STB::RawImage>(encodedData);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<IRawImage> decodeImage(const std::filesystem::path& path)
|
||||||
|
{
|
||||||
|
LMS_SCOPED_TRACE_DETAILED("Image", "DecodeFile");
|
||||||
|
return std::make_unique<STB::RawImage>(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<IEncodedImage> encodeToJPEG(const IRawImage& rawImage, unsigned quality)
|
||||||
|
{
|
||||||
|
LMS_SCOPED_TRACE_DETAILED("Image", "WriteJPEG");
|
||||||
|
|
||||||
|
std::vector<std::byte> encodedData;
|
||||||
|
|
||||||
|
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, &encodedData, rawImage.getWidth(), rawImage.getHeight(), 3, static_cast<const STB::RawImage&>(rawImage).getData(), quality) == 0)
|
||||||
|
throw Exception{ "Failed to export in jpeg format!" };
|
||||||
|
|
||||||
|
return std::make_unique<EncodedImage>(std::move(encodedData), "image/jpeg");
|
||||||
|
}
|
||||||
} // namespace lms::image
|
} // namespace lms::image
|
||||||
@@ -1,62 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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_image_write.h>
|
|
||||||
|
|
||||||
#include "core/ITraceLogger.hpp"
|
|
||||||
#include "image/Exception.hpp"
|
|
||||||
|
|
||||||
#include "RawImage.hpp"
|
|
||||||
|
|
||||||
namespace lms::image::STB
|
|
||||||
{
|
|
||||||
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);
|
|
||||||
} };
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
return &_data.front();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::size_t JPEGImage::getDataSize() const
|
|
||||||
{
|
|
||||||
return _data.size();
|
|
||||||
}
|
|
||||||
} // namespace lms::image::STB
|
|
||||||
@@ -39,21 +39,19 @@
|
|||||||
#include "core/ITraceLogger.hpp"
|
#include "core/ITraceLogger.hpp"
|
||||||
#include "image/Exception.hpp"
|
#include "image/Exception.hpp"
|
||||||
|
|
||||||
#include "JPEGImage.hpp"
|
|
||||||
|
|
||||||
namespace lms::image::STB
|
namespace lms::image::STB
|
||||||
{
|
{
|
||||||
RawImage::RawImage(const std::byte* encodedData, std::size_t encodedDataSize)
|
RawImage::RawImage(std::span<const std::byte> encodedData)
|
||||||
{
|
{
|
||||||
int n{};
|
int n{};
|
||||||
_data = UniquePtrFree{ ::stbi_load_from_memory(reinterpret_cast<const stbi_uc*>(encodedData), encodedDataSize, &_width, &_height, &n, 3), std::free };
|
_data = UniquePtrFree{ ::stbi_load_from_memory(reinterpret_cast<const stbi_uc*>(encodedData.data()), encodedData.size(), &_width, &_height, &n, 3), std::free };
|
||||||
if (!_data)
|
if (!_data)
|
||||||
throw Exception{ "Cannot load image from memory: " + std::string{ ::stbi_failure_reason() } };
|
throw Exception{ "Cannot load image from memory: " + std::string{ ::stbi_failure_reason() } };
|
||||||
}
|
}
|
||||||
|
|
||||||
RawImage::RawImage(const std::filesystem::path& p)
|
RawImage::RawImage(const std::filesystem::path& p)
|
||||||
{
|
{
|
||||||
int n;
|
int n{};
|
||||||
_data = UniquePtrFree{ stbi_load(p.string().c_str(), &_width, &_height, &n, 3), std::free };
|
_data = UniquePtrFree{ stbi_load(p.string().c_str(), &_width, &_height, &n, 3), std::free };
|
||||||
if (!_data)
|
if (!_data)
|
||||||
throw Exception{ "Cannot load image from file: " + std::string{ ::stbi_failure_reason() } };
|
throw Exception{ "Cannot load image from file: " + std::string{ ::stbi_failure_reason() } };
|
||||||
@@ -63,7 +61,7 @@ namespace lms::image::STB
|
|||||||
{
|
{
|
||||||
LMS_SCOPED_TRACE_DETAILED("Image", "Resize");
|
LMS_SCOPED_TRACE_DETAILED("Image", "Resize");
|
||||||
|
|
||||||
size_t height;
|
size_t height{};
|
||||||
if (_width == _height)
|
if (_width == _height)
|
||||||
{
|
{
|
||||||
height = width;
|
height = width;
|
||||||
@@ -104,11 +102,6 @@ namespace lms::image::STB
|
|||||||
_width = width;
|
_width = width;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<IEncodedImage> RawImage::encodeToJPEG(unsigned quality) const
|
|
||||||
{
|
|
||||||
return std::make_unique<JPEGImage>(*this, quality);
|
|
||||||
}
|
|
||||||
|
|
||||||
ImageSize RawImage::getWidth() const
|
ImageSize RawImage::getWidth() const
|
||||||
{
|
{
|
||||||
return _width;
|
return _width;
|
||||||
@@ -121,9 +114,7 @@ namespace lms::image::STB
|
|||||||
|
|
||||||
const std::byte* RawImage::getData() const
|
const std::byte* RawImage::getData() const
|
||||||
{
|
{
|
||||||
if (!_data)
|
assert(_data);
|
||||||
return nullptr;
|
|
||||||
|
|
||||||
return reinterpret_cast<const std::byte*>(_data.get());
|
return reinterpret_cast<const std::byte*>(_data.get());
|
||||||
}
|
}
|
||||||
} // namespace lms::image::STB
|
} // namespace lms::image::STB
|
||||||
@@ -21,8 +21,8 @@
|
|||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
#include <span>
|
||||||
|
|
||||||
#include "image/IEncodedImage.hpp"
|
|
||||||
#include "image/IRawImage.hpp"
|
#include "image/IRawImage.hpp"
|
||||||
|
|
||||||
namespace lms::image::STB
|
namespace lms::image::STB
|
||||||
@@ -30,7 +30,7 @@ namespace lms::image::STB
|
|||||||
class RawImage : public IRawImage
|
class RawImage : public IRawImage
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
RawImage(const std::byte* encodedData, std::size_t encodedDataSize);
|
RawImage(std::span<const std::byte> encodedData);
|
||||||
RawImage(const std::filesystem::path& path);
|
RawImage(const std::filesystem::path& path);
|
||||||
|
|
||||||
~RawImage() override = default;
|
~RawImage() override = default;
|
||||||
@@ -41,7 +41,6 @@ namespace lms::image::STB
|
|||||||
ImageSize getHeight() const override;
|
ImageSize getHeight() const override;
|
||||||
|
|
||||||
void resize(ImageSize width) override;
|
void resize(ImageSize width) override;
|
||||||
std::unique_ptr<IEncodedImage> encodeToJPEG(unsigned quality) const override;
|
|
||||||
|
|
||||||
const std::byte* getData() const;
|
const std::byte* getData() const;
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <span>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
namespace lms::image
|
namespace lms::image
|
||||||
@@ -31,8 +32,7 @@ namespace lms::image
|
|||||||
public:
|
public:
|
||||||
virtual ~IEncodedImage() = default;
|
virtual ~IEncodedImage() = default;
|
||||||
|
|
||||||
virtual const std::byte* getData() const = 0;
|
virtual std::span<const std::byte> getData() const = 0;
|
||||||
virtual std::size_t getDataSize() const = 0;
|
|
||||||
virtual std::string_view getMimeType() const = 0;
|
virtual std::string_view getMimeType() const = 0;
|
||||||
};
|
};
|
||||||
} // namespace lms::image
|
} // namespace lms::image
|
||||||
@@ -19,9 +19,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <memory>
|
#include "image/Types.hpp"
|
||||||
|
|
||||||
#include "image/IEncodedImage.hpp"
|
|
||||||
|
|
||||||
namespace lms::image
|
namespace lms::image
|
||||||
{
|
{
|
||||||
@@ -34,6 +32,5 @@ namespace lms::image
|
|||||||
virtual ImageSize getHeight() const = 0;
|
virtual ImageSize getHeight() const = 0;
|
||||||
|
|
||||||
virtual void resize(ImageSize width) = 0;
|
virtual void resize(ImageSize width) = 0;
|
||||||
virtual std::unique_ptr<IEncodedImage> encodeToJPEG(unsigned quality) const = 0;
|
|
||||||
};
|
};
|
||||||
} // namespace lms::image
|
} // namespace lms::image
|
||||||
|
|||||||
@@ -30,7 +30,12 @@ namespace lms::image
|
|||||||
{
|
{
|
||||||
void init(const std::filesystem::path& path);
|
void init(const std::filesystem::path& path);
|
||||||
std::span<const std::filesystem::path> getSupportedFileExtensions();
|
std::span<const std::filesystem::path> getSupportedFileExtensions();
|
||||||
std::unique_ptr<IRawImage> decodeImage(const std::byte* encodedData, std::size_t encodedDataSize);
|
|
||||||
|
std::unique_ptr<IRawImage> decodeImage(std::span<const std::byte> encodedData);
|
||||||
std::unique_ptr<IRawImage> decodeImage(const std::filesystem::path& path);
|
std::unique_ptr<IRawImage> decodeImage(const std::filesystem::path& path);
|
||||||
std::unique_ptr<IEncodedImage> readSvgFile(const std::filesystem::path& path);
|
|
||||||
|
std::unique_ptr<IEncodedImage> readImage(std::span<const std::byte> encodedData, std::string_view mimeType);
|
||||||
|
std::unique_ptr<IEncodedImage> readImage(const std::filesystem::path& path);
|
||||||
|
|
||||||
|
std::unique_ptr<IEncodedImage> encodeToJPEG(const IRawImage& rawImage, unsigned quality);
|
||||||
} // namespace lms::image
|
} // namespace lms::image
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2020 Emeric Poupon
|
* Copyright (C) 2024 Emeric Poupon
|
||||||
*
|
*
|
||||||
* This file is part of LMS.
|
* This file is part of LMS.
|
||||||
*
|
*
|
||||||
@@ -17,25 +17,9 @@
|
|||||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#include <cstddef>
|
||||||
|
|
||||||
#include <vector>
|
namespace lms::image
|
||||||
|
|
||||||
#include "image/IEncodedImage.hpp"
|
|
||||||
|
|
||||||
namespace lms::image::STB
|
|
||||||
{
|
{
|
||||||
class RawImage;
|
using ImageSize = std::size_t;
|
||||||
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"; }
|
|
||||||
|
|
||||||
std::vector<std::byte> _data;
|
|
||||||
};
|
|
||||||
} // namespace lms::image::STB
|
|
||||||
@@ -19,6 +19,8 @@
|
|||||||
|
|
||||||
#include "ArtworkService.hpp"
|
#include "ArtworkService.hpp"
|
||||||
|
|
||||||
|
#include <span>
|
||||||
|
|
||||||
#include "av/IAudioFile.hpp"
|
#include "av/IAudioFile.hpp"
|
||||||
#include "av/Types.hpp"
|
#include "av/Types.hpp"
|
||||||
#include "core/IConfig.hpp"
|
#include "core/IConfig.hpp"
|
||||||
@@ -31,6 +33,7 @@
|
|||||||
#include "database/Session.hpp"
|
#include "database/Session.hpp"
|
||||||
#include "database/Track.hpp"
|
#include "database/Track.hpp"
|
||||||
#include "image/Exception.hpp"
|
#include "image/Exception.hpp"
|
||||||
|
#include "image/IEncodedImage.hpp"
|
||||||
#include "image/Image.hpp"
|
#include "image/Image.hpp"
|
||||||
|
|
||||||
namespace lms::cover
|
namespace lms::cover
|
||||||
@@ -48,8 +51,6 @@ namespace lms::cover
|
|||||||
return std::make_unique<ArtworkService>(db, defaultReleaseCoverSvgPath, defaultArtistImageSvgPath);
|
return std::make_unique<ArtworkService>(db, defaultReleaseCoverSvgPath, defaultArtistImageSvgPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
using namespace image;
|
|
||||||
|
|
||||||
ArtworkService::ArtworkService(db::Db& db,
|
ArtworkService::ArtworkService(db::Db& db,
|
||||||
const std::filesystem::path& defaultReleaseCoverSvgPath,
|
const std::filesystem::path& defaultReleaseCoverSvgPath,
|
||||||
const std::filesystem::path& defaultArtistImageSvgPath)
|
const std::filesystem::path& defaultArtistImageSvgPath)
|
||||||
@@ -61,13 +62,13 @@ namespace lms::cover
|
|||||||
LMS_LOG(COVER, INFO, "Default release cover path = '" << defaultReleaseCoverSvgPath.string() << "'");
|
LMS_LOG(COVER, INFO, "Default release cover path = '" << defaultReleaseCoverSvgPath.string() << "'");
|
||||||
LMS_LOG(COVER, INFO, "Max cache size = " << _cache.getMaxCacheSize());
|
LMS_LOG(COVER, INFO, "Max cache size = " << _cache.getMaxCacheSize());
|
||||||
|
|
||||||
_defaultReleaseCover = image::readSvgFile(defaultReleaseCoverSvgPath); // may throw
|
_defaultReleaseCover = image::readImage(defaultReleaseCoverSvgPath); // may throw
|
||||||
_defaultArtistImage = image::readSvgFile(defaultArtistImageSvgPath); // may throw
|
_defaultArtistImage = image::readImage(defaultArtistImageSvgPath); // may throw
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<IEncodedImage> ArtworkService::getFromAvMediaFile(const av::IAudioFile& input, ImageSize width) const
|
std::unique_ptr<image::IEncodedImage> ArtworkService::getFromAvMediaFile(const av::IAudioFile& input, std::optional<image::ImageSize> width) const
|
||||||
{
|
{
|
||||||
std::unique_ptr<IEncodedImage> image;
|
std::unique_ptr<image::IEncodedImage> image;
|
||||||
|
|
||||||
input.visitAttachedPictures([&](const av::Picture& picture, const av::IAudioFile::MetadataMap& /* metadata */) {
|
input.visitAttachedPictures([&](const av::Picture& picture, const av::IAudioFile::MetadataMap& /* metadata */) {
|
||||||
if (image)
|
if (image)
|
||||||
@@ -75,9 +76,16 @@ namespace lms::cover
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::unique_ptr<IRawImage> rawImage{ decodeImage(picture.data, picture.dataSize) };
|
if (!width)
|
||||||
rawImage->resize(width);
|
{
|
||||||
image = rawImage->encodeToJPEG(_jpegQuality);
|
image = image::readImage(std::span{ picture.data, picture.dataSize }, picture.mimeType);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
auto rawImage{ image::decodeImage(std::span{ picture.data, picture.dataSize }) };
|
||||||
|
rawImage->resize(*width);
|
||||||
|
image = image::encodeToJPEG(*rawImage, _jpegQuality);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (const image::Exception& e)
|
catch (const image::Exception& e)
|
||||||
{
|
{
|
||||||
@@ -88,15 +96,22 @@ namespace lms::cover
|
|||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<IEncodedImage> ArtworkService::getFromImageFile(const std::filesystem::path& p, ImageSize width) const
|
std::unique_ptr<image::IEncodedImage> ArtworkService::getFromImageFile(const std::filesystem::path& p, std::optional<image::ImageSize> width) const
|
||||||
{
|
{
|
||||||
std::unique_ptr<IEncodedImage> image;
|
std::unique_ptr<image::IEncodedImage> image;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::unique_ptr<IRawImage> rawImage{ decodeImage(p) };
|
if (!width)
|
||||||
rawImage->resize(width);
|
{
|
||||||
image = rawImage->encodeToJPEG(_jpegQuality);
|
image = image::readImage(p);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
auto rawImage{ image::decodeImage(p) };
|
||||||
|
rawImage->resize(*width);
|
||||||
|
image = image::encodeToJPEG(*rawImage, _jpegQuality);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (const image::Exception& e)
|
catch (const image::Exception& e)
|
||||||
{
|
{
|
||||||
@@ -106,17 +121,17 @@ namespace lms::cover
|
|||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<IEncodedImage> ArtworkService::getDefaultReleaseCover()
|
std::shared_ptr<image::IEncodedImage> ArtworkService::getDefaultReleaseCover()
|
||||||
{
|
{
|
||||||
return _defaultReleaseCover;
|
return _defaultReleaseCover;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<IEncodedImage> ArtworkService::getDefaultArtistImage()
|
std::shared_ptr<image::IEncodedImage> ArtworkService::getDefaultArtistImage()
|
||||||
{
|
{
|
||||||
return _defaultArtistImage;
|
return _defaultArtistImage;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ArtworkService::checkImageFile(const std::filesystem::path& filePath) const
|
bool ArtworkService::checkImageFile(const std::filesystem::path& filePath)
|
||||||
{
|
{
|
||||||
std::error_code ec;
|
std::error_code ec;
|
||||||
|
|
||||||
@@ -132,9 +147,9 @@ namespace lms::cover
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<IEncodedImage> ArtworkService::getTrackImage(const std::filesystem::path& p, ImageSize width) const
|
std::unique_ptr<image::IEncodedImage> ArtworkService::getTrackImage(const std::filesystem::path& p, std::optional<image::ImageSize> width) const
|
||||||
{
|
{
|
||||||
std::unique_ptr<IEncodedImage> image;
|
std::unique_ptr<image::IEncodedImage> image;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -148,77 +163,81 @@ namespace lms::cover
|
|||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<IEncodedImage> ArtworkService::getTrackImage(db::TrackId trackId, ImageSize width)
|
std::shared_ptr<image::IEncodedImage> ArtworkService::getTrackImage(db::TrackId trackId, std::optional<image::ImageSize> width)
|
||||||
{
|
{
|
||||||
const ImageCache::EntryDesc cacheEntryDesc{ trackId, width };
|
const ImageCache::EntryDesc cacheEntryDesc{ trackId, width };
|
||||||
|
|
||||||
std::shared_ptr<IEncodedImage> cover{ _cache.getImage(cacheEntryDesc) };
|
std::shared_ptr<image::IEncodedImage> cover{ _cache.getImage(cacheEntryDesc) };
|
||||||
if (cover)
|
if (cover)
|
||||||
return cover;
|
return cover;
|
||||||
|
|
||||||
|
std::filesystem::path trackFile;
|
||||||
{
|
{
|
||||||
db::Session& session{ _db.getTLSSession() };
|
db::Session& session{ _db.getTLSSession() };
|
||||||
auto transaction{ session.createReadTransaction() };
|
auto transaction{ session.createReadTransaction() };
|
||||||
|
|
||||||
const db::Track::pointer track{ db::Track::find(session, trackId) };
|
const db::Track::pointer track{ db::Track::find(session, trackId) };
|
||||||
if (track && track->hasCover())
|
if (track && track->hasCover())
|
||||||
cover = getTrackImage(track->getAbsoluteFilePath(), width);
|
trackFile = track->getAbsoluteFilePath();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cover = getTrackImage(trackFile, width);
|
||||||
if (cover)
|
if (cover)
|
||||||
_cache.addImage(cacheEntryDesc, cover);
|
_cache.addImage(cacheEntryDesc, cover);
|
||||||
|
|
||||||
return cover;
|
return cover;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<IEncodedImage> ArtworkService::getReleaseCover(db::ReleaseId releaseId, ImageSize width)
|
std::shared_ptr<image::IEncodedImage> ArtworkService::getReleaseCover(db::ReleaseId releaseId, std::optional<image::ImageSize> width)
|
||||||
{
|
{
|
||||||
using namespace db;
|
|
||||||
const ImageCache::EntryDesc cacheEntryDesc{ releaseId, width };
|
const ImageCache::EntryDesc cacheEntryDesc{ releaseId, width };
|
||||||
|
|
||||||
std::shared_ptr<IEncodedImage> image{ _cache.getImage(cacheEntryDesc) };
|
std::shared_ptr<image::IEncodedImage> image{ _cache.getImage(cacheEntryDesc) };
|
||||||
if (image)
|
if (image)
|
||||||
return image;
|
return image;
|
||||||
|
|
||||||
|
std::filesystem::path imagePath;
|
||||||
{
|
{
|
||||||
Session& session{ _db.getTLSSession() };
|
db::Session& session{ _db.getTLSSession() };
|
||||||
auto transaction{ session.createReadTransaction() };
|
auto transaction{ session.createReadTransaction() };
|
||||||
|
|
||||||
const db::Release::pointer release{ db::Release::find(session, releaseId) };
|
const db::Release::pointer release{ db::Release::find(session, releaseId) };
|
||||||
if (release)
|
if (release)
|
||||||
{
|
{
|
||||||
if (const db::Image::pointer dbImage{ release->getImage() })
|
if (const db::Image::pointer dbImage{ release->getImage() })
|
||||||
image = getFromImageFile(dbImage->getAbsoluteFilePath(), width);
|
imagePath = dbImage->getAbsoluteFilePath();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
image = getFromImageFile(imagePath, width);
|
||||||
if (image)
|
if (image)
|
||||||
_cache.addImage(cacheEntryDesc, image);
|
_cache.addImage(cacheEntryDesc, image);
|
||||||
|
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<IEncodedImage> ArtworkService::getArtistImage(db::ArtistId artistId, ImageSize width)
|
std::shared_ptr<image::IEncodedImage> ArtworkService::getArtistImage(db::ArtistId artistId, std::optional<image::ImageSize> width)
|
||||||
{
|
{
|
||||||
using namespace db;
|
|
||||||
const ImageCache::EntryDesc cacheEntryDesc{ artistId, width };
|
const ImageCache::EntryDesc cacheEntryDesc{ artistId, width };
|
||||||
|
|
||||||
std::shared_ptr<IEncodedImage> artistImage{ _cache.getImage(cacheEntryDesc) };
|
std::shared_ptr<image::IEncodedImage> artistImage{ _cache.getImage(cacheEntryDesc) };
|
||||||
if (artistImage)
|
if (artistImage)
|
||||||
return artistImage;
|
return artistImage;
|
||||||
|
|
||||||
|
std::filesystem::path imagePath;
|
||||||
{
|
{
|
||||||
Session& session{ _db.getTLSSession() };
|
db::Session& session{ _db.getTLSSession() };
|
||||||
|
|
||||||
auto transaction{ session.createReadTransaction() };
|
auto transaction{ session.createReadTransaction() };
|
||||||
|
|
||||||
if (const Artist::pointer artist{ Artist::find(session, artistId) })
|
if (const db::Artist::pointer artist{ db::Artist::find(session, artistId) })
|
||||||
{
|
{
|
||||||
if (const db::Image::pointer image{ artist->getImage() })
|
if (const db::Image::pointer image{ artist->getImage() })
|
||||||
artistImage = getFromImageFile(image->getAbsoluteFilePath(), width);
|
imagePath = image->getAbsoluteFilePath();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
artistImage = getFromImageFile(imagePath, width);
|
||||||
if (artistImage)
|
if (artistImage)
|
||||||
_cache.addImage(cacheEntryDesc, artistImage);
|
_cache.addImage(cacheEntryDesc, artistImage);
|
||||||
|
|
||||||
|
|||||||
@@ -48,22 +48,20 @@ namespace lms::cover
|
|||||||
ArtworkService& operator=(const ArtworkService&) = delete;
|
ArtworkService& operator=(const ArtworkService&) = delete;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<image::IEncodedImage> getTrackImage(db::TrackId trackId, image::ImageSize width) override;
|
std::shared_ptr<image::IEncodedImage> getTrackImage(db::TrackId trackId, std::optional<image::ImageSize> width) override;
|
||||||
std::shared_ptr<image::IEncodedImage> getReleaseCover(db::ReleaseId releaseId, image::ImageSize width) override;
|
std::shared_ptr<image::IEncodedImage> getReleaseCover(db::ReleaseId releaseId, std::optional<image::ImageSize> width) override;
|
||||||
std::shared_ptr<image::IEncodedImage> getArtistImage(db::ArtistId artistId, image::ImageSize width) override;
|
std::shared_ptr<image::IEncodedImage> getArtistImage(db::ArtistId artistId, std::optional<image::ImageSize> width) override;
|
||||||
std::shared_ptr<image::IEncodedImage> getDefaultReleaseCover() override;
|
std::shared_ptr<image::IEncodedImage> getDefaultReleaseCover() override;
|
||||||
std::shared_ptr<image::IEncodedImage> getDefaultArtistImage() override;
|
std::shared_ptr<image::IEncodedImage> getDefaultArtistImage() override;
|
||||||
|
|
||||||
void flushCache() override;
|
void flushCache() override;
|
||||||
void setJpegQuality(unsigned quality) override;
|
void setJpegQuality(unsigned quality) override;
|
||||||
|
|
||||||
std::shared_ptr<image::IEncodedImage> getTrackImage(db::Session& dbSession, db::TrackId trackId, image::ImageSize width, bool allowReleaseFallback);
|
std::unique_ptr<image::IEncodedImage> getFromAvMediaFile(const av::IAudioFile& input, std::optional<image::ImageSize> width) const;
|
||||||
std::unique_ptr<image::IEncodedImage> getFromAvMediaFile(const av::IAudioFile& input, image::ImageSize width) const;
|
std::unique_ptr<image::IEncodedImage> getFromImageFile(const std::filesystem::path& p, std::optional<image::ImageSize> width) const;
|
||||||
std::unique_ptr<image::IEncodedImage> getFromImageFile(const std::filesystem::path& p, image::ImageSize width) const;
|
std::unique_ptr<image::IEncodedImage> getTrackImage(const std::filesystem::path& path, std::optional<image::ImageSize> width) const;
|
||||||
|
|
||||||
std::unique_ptr<image::IEncodedImage> getTrackImage(const std::filesystem::path& path, image::ImageSize width) const;
|
static bool checkImageFile(const std::filesystem::path& filePath);
|
||||||
|
|
||||||
bool checkImageFile(const std::filesystem::path& filePath) const;
|
|
||||||
|
|
||||||
db::Db& _db;
|
db::Db& _db;
|
||||||
|
|
||||||
|
|||||||
@@ -33,21 +33,29 @@ namespace lms::cover
|
|||||||
|
|
||||||
void ImageCache::addImage(const EntryDesc& entryDesc, std::shared_ptr<image::IEncodedImage> image)
|
void ImageCache::addImage(const EntryDesc& entryDesc, std::shared_ptr<image::IEncodedImage> image)
|
||||||
{
|
{
|
||||||
|
// cache only resized files
|
||||||
|
if (!entryDesc.size)
|
||||||
|
return;
|
||||||
|
|
||||||
const std::unique_lock lock{ _mutex };
|
const std::unique_lock lock{ _mutex };
|
||||||
|
|
||||||
while (_cacheSize + image->getDataSize() > _maxCacheSize && !_cache.empty())
|
while (_cacheSize + image->getData().size() > _maxCacheSize && !_cache.empty())
|
||||||
{
|
{
|
||||||
auto itRandom{ core::random::pickRandom(_cache) };
|
auto itRandom{ core::random::pickRandom(_cache) };
|
||||||
_cacheSize -= itRandom->second->getDataSize();
|
_cacheSize -= itRandom->second->getData().size();
|
||||||
_cache.erase(itRandom);
|
_cache.erase(itRandom);
|
||||||
}
|
}
|
||||||
|
|
||||||
_cacheSize += image->getDataSize();
|
_cacheSize += image->getData().size();
|
||||||
_cache[entryDesc] = image;
|
_cache[entryDesc] = image;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<image::IEncodedImage> ImageCache::getImage(const EntryDesc& entryDesc) const
|
std::shared_ptr<image::IEncodedImage> ImageCache::getImage(const EntryDesc& entryDesc) const
|
||||||
{
|
{
|
||||||
|
// cache only resized files
|
||||||
|
if (!entryDesc.size)
|
||||||
|
return {};
|
||||||
|
|
||||||
const std::shared_lock lock{ _mutex };
|
const std::shared_lock lock{ _mutex };
|
||||||
|
|
||||||
const auto it{ _cache.find(entryDesc) };
|
const auto it{ _cache.find(entryDesc) };
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
#include <optional>
|
||||||
#include <shared_mutex>
|
#include <shared_mutex>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <variant>
|
#include <variant>
|
||||||
@@ -40,7 +41,7 @@ namespace lms::cover
|
|||||||
{
|
{
|
||||||
using VariantType = std::variant<db::ArtistId, db::ReleaseId, db::TrackId>;
|
using VariantType = std::variant<db::ArtistId, db::ReleaseId, db::TrackId>;
|
||||||
VariantType id;
|
VariantType id;
|
||||||
std::size_t size;
|
std::optional<std::size_t> size;
|
||||||
|
|
||||||
bool operator==(const EntryDesc& other) const = default;
|
bool operator==(const EntryDesc& other) const = default;
|
||||||
};
|
};
|
||||||
@@ -60,7 +61,8 @@ namespace lms::cover
|
|||||||
{
|
{
|
||||||
std::size_t operator()(const EntryDesc& entry) const
|
std::size_t operator()(const EntryDesc& entry) const
|
||||||
{
|
{
|
||||||
return std::hash<EntryDesc::VariantType>{}(entry.id) ^ std::hash<std::size_t>{}(entry.size);
|
assert(entry.size); // should not cache unresized images
|
||||||
|
return std::hash<EntryDesc::VariantType>{}(entry.id) ^ std::hash<std::size_t>{}(*entry.size);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
|
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
#include "database/ArtistId.hpp"
|
#include "database/ArtistId.hpp"
|
||||||
#include "database/ReleaseId.hpp"
|
#include "database/ReleaseId.hpp"
|
||||||
@@ -39,13 +40,13 @@ namespace lms::cover
|
|||||||
public:
|
public:
|
||||||
virtual ~IArtworkService() = default;
|
virtual ~IArtworkService() = default;
|
||||||
|
|
||||||
virtual std::shared_ptr<image::IEncodedImage> getArtistImage(db::ArtistId artistId, image::ImageSize width) = 0;
|
virtual std::shared_ptr<image::IEncodedImage> getArtistImage(db::ArtistId artistId, std::optional<image::ImageSize> width) = 0;
|
||||||
|
|
||||||
// no logic to fallback to release here
|
// no logic to fallback to release here
|
||||||
virtual std::shared_ptr<image::IEncodedImage> getTrackImage(db::TrackId trackId, image::ImageSize width) = 0;
|
virtual std::shared_ptr<image::IEncodedImage> getTrackImage(db::TrackId trackId, std::optional<image::ImageSize> width) = 0;
|
||||||
|
|
||||||
// no logic to fallback to track here
|
// no logic to fallback to track here
|
||||||
virtual std::shared_ptr<image::IEncodedImage> getReleaseCover(db::ReleaseId releaseId, image::ImageSize width) = 0;
|
virtual std::shared_ptr<image::IEncodedImage> getReleaseCover(db::ReleaseId releaseId, std::optional<image::ImageSize> width) = 0;
|
||||||
|
|
||||||
// Svg images dont have image "size"
|
// Svg images dont have image "size"
|
||||||
virtual std::shared_ptr<image::IEncodedImage> getDefaultReleaseCover() = 0;
|
virtual std::shared_ptr<image::IEncodedImage> getDefaultReleaseCover() = 0;
|
||||||
|
|||||||
@@ -333,8 +333,9 @@ namespace lms::api::subsonic
|
|||||||
if (!trackId && !releaseId && !artistId)
|
if (!trackId && !releaseId && !artistId)
|
||||||
throw BadParameterGenericError{ "id" };
|
throw BadParameterGenericError{ "id" };
|
||||||
|
|
||||||
std::size_t size{ getParameterAs<std::size_t>(context.parameters, "size").value_or(1024) };
|
std::optional<std::size_t> size{ getParameterAs<std::size_t>(context.parameters, "size") };
|
||||||
size = core::utils::clamp(size, std::size_t{ 32 }, std::size_t{ 2048 });
|
if (size)
|
||||||
|
*size = core::utils::clamp(*size, std::size_t{ 32 }, std::size_t{ 2048 });
|
||||||
|
|
||||||
std::shared_ptr<image::IEncodedImage> cover;
|
std::shared_ptr<image::IEncodedImage> cover;
|
||||||
if (trackId)
|
if (trackId)
|
||||||
@@ -353,7 +354,7 @@ namespace lms::api::subsonic
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
response.out().write(reinterpret_cast<const char*>(cover->getData()), cover->getDataSize());
|
response.out().write(reinterpret_cast<const char*>(cover->getData().data()), cover->getData().size());
|
||||||
response.setMimeType(std::string{ cover->getMimeType() });
|
response.setMimeType(std::string{ cover->getMimeType() });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -232,7 +232,7 @@ namespace lms::ui
|
|||||||
if (image)
|
if (image)
|
||||||
{
|
{
|
||||||
response.setMimeType(std::string{ image->getMimeType() });
|
response.setMimeType(std::string{ image->getMimeType() });
|
||||||
response.out().write(reinterpret_cast<const char*>(image->getData()), image->getDataSize());
|
response.out().write(reinterpret_cast<const char*>(image->getData().data()), image->getData().size());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
response.setStatus(404);
|
response.setStatus(404);
|
||||||
|
|||||||
Reference in New Issue
Block a user