mirror of
https://github.com/matrix-construct/construct
synced 2024-12-27 07:54:05 +01:00
modules/media/magick: Add composite thumbcropper for matrix.
This commit is contained in:
parent
c2dc298304
commit
a651952eda
2 changed files with 71 additions and 0 deletions
|
@ -20,10 +20,24 @@ namespace ircd::magick
|
|||
struct shave;
|
||||
struct scale;
|
||||
struct thumbnail;
|
||||
struct thumbcrop;
|
||||
|
||||
std::tuple<ulong, string_view> version();
|
||||
}
|
||||
|
||||
/// Composite thumbnailer to resize close to the requested dimension but
|
||||
/// preserving original aspect ratio; then crop to requested dimension.
|
||||
struct ircd::magick::thumbcrop
|
||||
{
|
||||
using dimensions = std::pair<size_t, size_t>; // x, y
|
||||
using result_closure = std::function<void (const const_buffer &)>;
|
||||
|
||||
thumbcrop(const const_buffer &in,
|
||||
const dimensions &,
|
||||
const result_closure &);
|
||||
};
|
||||
|
||||
/// Fast thumbnailer
|
||||
struct ircd::magick::thumbnail
|
||||
{
|
||||
using dimensions = std::pair<size_t, size_t>; // x, y
|
||||
|
|
|
@ -156,6 +156,63 @@ ircd::magick::version()
|
|||
return { number, string };
|
||||
}
|
||||
|
||||
//
|
||||
// thumbcrop
|
||||
//
|
||||
|
||||
ircd::magick::thumbcrop::thumbcrop(const const_buffer &in,
|
||||
const dimensions &req,
|
||||
const result_closure &out)
|
||||
{
|
||||
crop::offset offset;
|
||||
const auto scaler{[&req, &offset]
|
||||
(const auto &image)
|
||||
{
|
||||
const auto &img_p
|
||||
{
|
||||
std::get<const ::Image *>(image)
|
||||
};
|
||||
|
||||
const auto &req_x(req.first);
|
||||
const auto &req_y(req.second);
|
||||
const auto &img_x(img_p->columns);
|
||||
const auto &img_y(img_p->rows);
|
||||
|
||||
const bool aspect
|
||||
{
|
||||
req_x * img_y < req_y * img_x
|
||||
};
|
||||
|
||||
const dimensions scaled
|
||||
{
|
||||
aspect? req_y * img_x / img_y : req_x,
|
||||
aspect? req_y : req_x * img_y / img_x,
|
||||
};
|
||||
|
||||
offset =
|
||||
{
|
||||
aspect? (scaled.first - req_x) / 2.0 : 0,
|
||||
aspect? 0 : (scaled.second - req_y) / 2.0,
|
||||
};
|
||||
|
||||
return callex<::Image *>(::ThumbnailImage, img_p, scaled.first, scaled.second);
|
||||
}};
|
||||
|
||||
const auto cropper{[&req, &out, &offset]
|
||||
(const const_buffer &in)
|
||||
{
|
||||
crop
|
||||
{
|
||||
in, req, offset, out
|
||||
};
|
||||
}};
|
||||
|
||||
transform
|
||||
{
|
||||
in, cropper, scaler
|
||||
};
|
||||
}
|
||||
|
||||
//
|
||||
// thumbnail
|
||||
//
|
||||
|
|
Loading…
Reference in a new issue