Subsonic API: getCoverArt: serve raw image if size is not provided
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
|
||||
add_library(lmsimage SHARED
|
||||
impl/SvgImage.cpp
|
||||
impl/EncodedImage.cpp
|
||||
)
|
||||
|
||||
target_include_directories(lmsimage INTERFACE
|
||||
@@ -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}")
|
||||
@@ -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++)
|
||||
|
||||
@@ -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
|
||||
|
||||
#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
|
||||
@@ -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 <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());
|
||||
@@ -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
|
||||
|
||||
@@ -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 <algorithm>
|
||||
#include <array>
|
||||
|
||||
#include <magick/resource.h>
|
||||
|
||||
#include "core/ILogger.hpp"
|
||||
#include "core/ITraceLogger.hpp"
|
||||
#include "image/Exception.hpp"
|
||||
|
||||
#include "JPEGImage.hpp"
|
||||
|
||||
namespace lms::image::GraphicsMagick
|
||||
{
|
||||
RawImage::RawImage(const std::byte* encodedData, std::size_t encodedDataSize)
|
||||
RawImage::RawImage(std::span<const std::byte> encodedData)
|
||||
{
|
||||
try
|
||||
{
|
||||
Magick::Blob blob{ encodedData, encodedDataSize };
|
||||
Magick::Blob blob{ encodedData.data(), encodedData.size() };
|
||||
_image.read(blob);
|
||||
}
|
||||
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
|
||||
{
|
||||
return _image;
|
||||
}
|
||||
|
||||
} // namespace lms::image::GraphicsMagick
|
||||
|
||||
@@ -21,10 +21,10 @@
|
||||
|
||||
#include <cstddef>
|
||||
#include <filesystem>
|
||||
#include <span>
|
||||
|
||||
#include <Magick++.h>
|
||||
|
||||
#include "image/IEncodedImage.hpp"
|
||||
#include "image/IRawImage.hpp"
|
||||
|
||||
namespace lms::image::GraphicsMagick
|
||||
@@ -32,19 +32,17 @@ namespace lms::image::GraphicsMagick
|
||||
class RawImage : public IRawImage
|
||||
{
|
||||
public:
|
||||
RawImage(const std::byte* encodedData, std::size_t encodedDataSize);
|
||||
RawImage(std::span<const std::byte> encodedData);
|
||||
RawImage(const std::filesystem::path& path);
|
||||
|
||||
ImageSize getWidth() const override;
|
||||
ImageSize getHeight() const override;
|
||||
|
||||
void resize(ImageSize width) override;
|
||||
std::unique_ptr<IEncodedImage> encodeToJPEG(unsigned quality) const override;
|
||||
|
||||
private:
|
||||
friend class JPEGImage;
|
||||
Magick::Image getMagickImage() const;
|
||||
|
||||
private:
|
||||
Magick::Image _image;
|
||||
};
|
||||
} // namespace lms::image::GraphicsMagick
|
||||
|
||||
@@ -21,24 +21,18 @@
|
||||
|
||||
#include <array>
|
||||
|
||||
#include "RawImage.hpp"
|
||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||
#include <stb_image_write.h>
|
||||
|
||||
#include "core/ITraceLogger.hpp"
|
||||
|
||||
#include "EncodedImage.hpp"
|
||||
#include "RawImage.hpp"
|
||||
#include "image/Exception.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<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&)
|
||||
void init(const std::filesystem::path& /*unused*/)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -47,4 +41,35 @@ 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<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
|
||||
@@ -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 "image/Exception.hpp"
|
||||
|
||||
#include "JPEGImage.hpp"
|
||||
|
||||
namespace lms::image::STB
|
||||
{
|
||||
RawImage::RawImage(const std::byte* encodedData, std::size_t encodedDataSize)
|
||||
RawImage::RawImage(std::span<const std::byte> encodedData)
|
||||
{
|
||||
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)
|
||||
throw Exception{ "Cannot load image from memory: " + std::string{ ::stbi_failure_reason() } };
|
||||
}
|
||||
|
||||
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 };
|
||||
if (!_data)
|
||||
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");
|
||||
|
||||
size_t height;
|
||||
size_t height{};
|
||||
if (_width == _height)
|
||||
{
|
||||
height = width;
|
||||
@@ -104,11 +102,6 @@ namespace lms::image::STB
|
||||
_width = width;
|
||||
}
|
||||
|
||||
std::unique_ptr<IEncodedImage> RawImage::encodeToJPEG(unsigned quality) const
|
||||
{
|
||||
return std::make_unique<JPEGImage>(*this, quality);
|
||||
}
|
||||
|
||||
ImageSize RawImage::getWidth() const
|
||||
{
|
||||
return _width;
|
||||
@@ -121,9 +114,7 @@ namespace lms::image::STB
|
||||
|
||||
const std::byte* RawImage::getData() const
|
||||
{
|
||||
if (!_data)
|
||||
return nullptr;
|
||||
|
||||
assert(_data);
|
||||
return reinterpret_cast<const std::byte*>(_data.get());
|
||||
}
|
||||
} // namespace lms::image::STB
|
||||
@@ -21,8 +21,8 @@
|
||||
|
||||
#include <cstddef>
|
||||
#include <filesystem>
|
||||
#include <span>
|
||||
|
||||
#include "image/IEncodedImage.hpp"
|
||||
#include "image/IRawImage.hpp"
|
||||
|
||||
namespace lms::image::STB
|
||||
@@ -30,7 +30,7 @@ namespace lms::image::STB
|
||||
class RawImage : public IRawImage
|
||||
{
|
||||
public:
|
||||
RawImage(const std::byte* encodedData, std::size_t encodedDataSize);
|
||||
RawImage(std::span<const std::byte> encodedData);
|
||||
RawImage(const std::filesystem::path& path);
|
||||
|
||||
~RawImage() override = default;
|
||||
@@ -41,7 +41,6 @@ namespace lms::image::STB
|
||||
ImageSize getHeight() const override;
|
||||
|
||||
void resize(ImageSize width) override;
|
||||
std::unique_ptr<IEncodedImage> encodeToJPEG(unsigned quality) const override;
|
||||
|
||||
const std::byte* getData() const;
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <span>
|
||||
#include <string_view>
|
||||
|
||||
namespace lms::image
|
||||
@@ -31,8 +32,7 @@ namespace lms::image
|
||||
public:
|
||||
virtual ~IEncodedImage() = default;
|
||||
|
||||
virtual const std::byte* getData() const = 0;
|
||||
virtual std::size_t getDataSize() const = 0;
|
||||
virtual std::span<const std::byte> getData() const = 0;
|
||||
virtual std::string_view getMimeType() const = 0;
|
||||
};
|
||||
} // namespace lms::image
|
||||
@@ -19,9 +19,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "image/IEncodedImage.hpp"
|
||||
#include "image/Types.hpp"
|
||||
|
||||
namespace lms::image
|
||||
{
|
||||
@@ -34,6 +32,5 @@ namespace lms::image
|
||||
virtual ImageSize getHeight() const = 0;
|
||||
|
||||
virtual void resize(ImageSize width) = 0;
|
||||
virtual std::unique_ptr<IEncodedImage> encodeToJPEG(unsigned quality) const = 0;
|
||||
};
|
||||
} // namespace lms::image
|
||||
|
||||
@@ -30,7 +30,12 @@ namespace lms::image
|
||||
{
|
||||
void init(const std::filesystem::path& path);
|
||||
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<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
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Emeric Poupon
|
||||
* Copyright (C) 2024 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
@@ -17,25 +17,9 @@
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <cstddef>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "image/IEncodedImage.hpp"
|
||||
|
||||
namespace lms::image::STB
|
||||
namespace lms::image
|
||||
{
|
||||
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"; }
|
||||
|
||||
std::vector<std::byte> _data;
|
||||
};
|
||||
} // namespace lms::image::STB
|
||||
using ImageSize = std::size_t;
|
||||
}
|
||||
Reference in New Issue
Block a user