Switched from gil to imagemagick++
This commit is contained in:
+56
-42
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2013 Emeric Poupon
|
||||
* Copyright (C) 2015 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
@@ -16,65 +16,79 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <boost/gil/image.hpp>
|
||||
#include <boost/gil/typedefs.hpp>
|
||||
#include <boost/gil/extension/io/jpeg_io.hpp>
|
||||
#include <boost/gil/extension/numeric/sampler.hpp>
|
||||
#include <boost/gil/extension/numeric/resample.hpp>
|
||||
#include <boost/gil/extension/io_new/jpeg_all.hpp>
|
||||
|
||||
#include "logger/Logger.hpp"
|
||||
|
||||
#include "CoverArt.hpp"
|
||||
|
||||
|
||||
namespace CoverArt {
|
||||
|
||||
static
|
||||
std::string format_to_magick(Format format)
|
||||
{
|
||||
switch (format)
|
||||
{
|
||||
case Format::JPEG: return "JPEG";
|
||||
}
|
||||
|
||||
return "JPEG";
|
||||
}
|
||||
|
||||
std::string format_to_mimeType(Format format)
|
||||
{
|
||||
switch (format)
|
||||
{
|
||||
case Format::JPEG: return "JPEG";
|
||||
}
|
||||
|
||||
return "application/octet-stream";
|
||||
}
|
||||
|
||||
void
|
||||
CoverArt::init(const char *path)
|
||||
{
|
||||
Magick::InitializeMagick(path);
|
||||
}
|
||||
|
||||
CoverArt::CoverArt(const std::vector<unsigned char>& rawData)
|
||||
{
|
||||
Magick::Blob blob(&rawData[0], rawData.size());
|
||||
_image.read(blob);
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
CoverArt::scale(std::size_t size)
|
||||
{
|
||||
bool res = false;
|
||||
|
||||
if (!size)
|
||||
return false;
|
||||
|
||||
try {
|
||||
try
|
||||
{
|
||||
_image.resize( Magick::Geometry(size, size ) );
|
||||
|
||||
boost::gil::rgb8_image_t source;
|
||||
boost::gil::rgb8_image_t dest(size, size);
|
||||
|
||||
// Read source
|
||||
{
|
||||
std::istringstream iss( std::string(_data.begin(), _data.end()));
|
||||
boost::gil::read_image(iss, source, boost::gil::jpeg_tag());
|
||||
}
|
||||
|
||||
if (source.width() == static_cast<int>(size)
|
||||
&& source.height() == static_cast<int>(size))
|
||||
return true;
|
||||
|
||||
// Resize
|
||||
boost::gil::resize_view(boost::gil::const_view(source),
|
||||
boost::gil::view(dest),
|
||||
boost::gil::bilinear_sampler());
|
||||
|
||||
// Output to dest
|
||||
{
|
||||
std::ostringstream oss;
|
||||
boost::gil::write_view(oss, boost::gil::const_view(dest), boost::gil::jpeg_tag());
|
||||
|
||||
std::string output = oss.str();
|
||||
_data.assign(output.begin(), output.end());
|
||||
}
|
||||
|
||||
res = true;
|
||||
return true;
|
||||
}
|
||||
catch(std::exception& e)
|
||||
catch (Magick::Exception& e)
|
||||
{
|
||||
LMS_LOG(COVER, ERROR) << "Caught exception: " << e.what();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
void
|
||||
CoverArt::getData(std::vector<unsigned char>& data, Format format) const
|
||||
{
|
||||
|
||||
Magick::Image outputImage(_image);
|
||||
|
||||
outputImage.magick( format_to_magick(format));
|
||||
|
||||
Magick::Blob blob;
|
||||
outputImage.write(&blob);
|
||||
|
||||
unsigned char *charBuf = (unsigned char*)blob.data();
|
||||
data.assign( charBuf, charBuf + blob.length() );
|
||||
}
|
||||
|
||||
} // namespace CoverArt
|
||||
|
||||
+15
-11
@@ -23,30 +23,34 @@
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include <Magick++.h>
|
||||
|
||||
namespace CoverArt
|
||||
{
|
||||
|
||||
enum class Format
|
||||
{
|
||||
JPEG,
|
||||
};
|
||||
|
||||
std::string format_to_mimeType(Format format);
|
||||
|
||||
class CoverArt
|
||||
{
|
||||
public:
|
||||
typedef std::vector<unsigned char> data_type;
|
||||
static void init(const char *path);
|
||||
|
||||
CoverArt() {}
|
||||
CoverArt(const std::string& mime, const data_type& data) : _mimeType(mime), _data(data) {}
|
||||
|
||||
const std::string& getMimeType() const { return _mimeType; }
|
||||
const data_type& getData() const { return _data; }
|
||||
|
||||
void setMimeType(const std::string& mimeType) { _mimeType = mimeType;}
|
||||
void setData(const data_type& data) { _data = data; }
|
||||
CoverArt(const std::vector<unsigned char>& rawData);
|
||||
|
||||
// Operations on cover arts
|
||||
bool scale(std::size_t size);
|
||||
|
||||
private:
|
||||
// output
|
||||
void getData(std::vector<unsigned char>& data, Format format) const;
|
||||
|
||||
std::string _mimeType;
|
||||
data_type _data;
|
||||
private:
|
||||
Magick::Image _image;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -61,7 +61,7 @@ getFromAvMediaFile(const Av::MediaFile& input, std::size_t nbMaxCovers)
|
||||
std::vector<CoverArt> res;
|
||||
|
||||
for (Av::Picture& picture : input.getAttachedPictures(nbMaxCovers))
|
||||
res.push_back( CoverArt(picture.mimeType, picture.data) );
|
||||
res.push_back( CoverArt(picture.data) );
|
||||
|
||||
return res;
|
||||
}
|
||||
@@ -83,8 +83,7 @@ Grabber::getFromDirectory(const boost::filesystem::path& p, std::size_t nbMaxCov
|
||||
while (file.get(c))
|
||||
data.push_back(c);
|
||||
|
||||
// TODO handle other formats
|
||||
res.push_back(CoverArt("image/jpeg", data));
|
||||
res.push_back(CoverArt(data));
|
||||
}
|
||||
|
||||
return res;
|
||||
|
||||
Reference in New Issue
Block a user