Skip to content

Commit

Permalink
Subsonic API: getCoverArt: serve raw image if size is not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
epoupon committed Dec 7, 2024
1 parent 350e213 commit def4d8c
Show file tree
Hide file tree
Showing 25 changed files with 300 additions and 367 deletions.
3 changes: 1 addition & 2 deletions src/libs/av/impl/AudioFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,10 @@ namespace lms::av
void AudioFile::visitAttachedPictures(std::function<void(const Picture&, const MetadataMap&)> func) const
{
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_MJPEG, "image/jpeg" },
{ AV_CODEC_ID_PNG, "image/png" },
{ AV_CODEC_ID_PNG, "image/x-png" },
{ AV_CODEC_ID_PPM, "image/x-portable-pixmap" },
};

Expand Down
4 changes: 1 addition & 3 deletions src/libs/image/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

add_library(lmsimage SHARED
impl/SvgImage.cpp
impl/EncodedImage.cpp
)

target_include_directories(lmsimage INTERFACE
Expand All @@ -26,7 +26,6 @@ if (${LMS_IMAGE_BACKEND} STREQUAL "stb")

target_sources(lmsimage PRIVATE
impl/stb/Image.cpp
impl/stb/JPEGImage.cpp
impl/stb/RawImage.cpp
)
target_compile_options(lmsimage PRIVATE "-DSTB_IMAGE_RESIZE_VERSION=${STB_IMAGE_RESIZE_VERSION}")
Expand All @@ -38,7 +37,6 @@ elseif (${LMS_IMAGE_BACKEND} STREQUAL "graphicsmagick")

target_sources(lmsimage PRIVATE
impl/graphicsmagick/Image.cpp
impl/graphicsmagick/JPEGImage.cpp
impl/graphicsmagick/RawImage.cpp
)
target_link_libraries(lmsimage PRIVATE PkgConfig::GraphicsMagick++)
Expand Down
103 changes: 103 additions & 0 deletions src/libs/image/impl/EncodedImage.cpp
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,28 @@

#pragma once

#include <filesystem>
#include <vector>

#include "image/IEncodedImage.hpp"

namespace lms::image
{
class SvgImage : public IEncodedImage
class EncodedImage : public IEncodedImage
{
public:
SvgImage(std::vector<std::byte>&& data)
: _data{ std::move(data) } {}
EncodedImage(const std::filesystem::path& path);
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::size_t getDataSize() const override { return _data.size(); }
std::string_view getMimeType() const override { return "image/svg+xml"; }
std::span<const std::byte> getData() const override { return _data; }
std::string_view getMimeType() const override { return _mimeType; }

private:
const std::vector<std::byte> _data;
const std::string _mimeType;
};
} // namespace lms::image
55 changes: 0 additions & 55 deletions src/libs/image/impl/SvgImage.cpp

This file was deleted.

54 changes: 40 additions & 14 deletions src/libs/image/impl/graphicsmagick/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,17 @@

#include "image/Image.hpp"

#include <array>
#include <memory>

#include "RawImage.hpp"
#include "core/ILogger.hpp"
#include "core/ITraceLogger.hpp"
#include "image/Exception.hpp"

#include "EncodedImage.hpp"
#include "RawImage.hpp"

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)
{
Magick::InitializeMagick(path.string().c_str());
Expand All @@ -61,4 +52,39 @@ namespace lms::image
static const std::array<std::filesystem::path, 4> fileExtensions{ ".jpg", ".jpeg", ".png", ".bmp" };
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
57 changes: 0 additions & 57 deletions src/libs/image/impl/graphicsmagick/JPEGImage.cpp

This file was deleted.

Loading

0 comments on commit def4d8c

Please sign in to comment.