2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
2018-10-23 09:44:42 +00:00
|
|
|
#include "ui/image/image_prepare.h"
|
2015-11-26 17:34:52 +00:00
|
|
|
|
2016-11-28 15:45:07 +00:00
|
|
|
namespace Images {
|
2014-05-30 08:53:19 +00:00
|
|
|
namespace {
|
|
|
|
|
2018-07-28 21:08:29 +00:00
|
|
|
TG_FORCE_INLINE uint64 blurGetColors(const uchar *p) {
|
2016-11-28 15:45:07 +00:00
|
|
|
return (uint64)p[0] + ((uint64)p[1] << 16) + ((uint64)p[2] << 32) + ((uint64)p[3] << 48);
|
2016-03-18 10:18:30 +00:00
|
|
|
}
|
|
|
|
|
2019-03-28 08:51:08 +00:00
|
|
|
const QImage &circleMask(QSize size) {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(Global::started());
|
2016-11-21 20:26:54 +00:00
|
|
|
|
2019-03-28 08:51:08 +00:00
|
|
|
uint64 key = (uint64(uint32(size.width())) << 32)
|
|
|
|
| uint64(uint32(size.height()));
|
|
|
|
|
|
|
|
static auto masks = base::flat_map<uint64, QImage>();
|
|
|
|
const auto i = masks.find(key);
|
|
|
|
if (i != end(masks)) {
|
|
|
|
return i->second;
|
2014-11-12 20:18:00 +00:00
|
|
|
}
|
2019-03-28 08:51:08 +00:00
|
|
|
auto mask = QImage(
|
|
|
|
size,
|
|
|
|
QImage::Format_ARGB32_Premultiplied);
|
|
|
|
mask.fill(Qt::transparent);
|
|
|
|
{
|
|
|
|
Painter p(&mask);
|
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
p.setBrush(Qt::white);
|
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
p.drawEllipse(QRect(QPoint(), size));
|
|
|
|
}
|
|
|
|
return masks.emplace(key, std::move(mask)).first->second;
|
2014-11-12 20:18:00 +00:00
|
|
|
}
|
|
|
|
|
2016-11-28 15:45:07 +00:00
|
|
|
} // namespace
|
2014-09-20 21:35:46 +00:00
|
|
|
|
2018-01-02 19:10:49 +00:00
|
|
|
QPixmap PixmapFast(QImage &&image) {
|
|
|
|
Expects(image.format() == QImage::Format_ARGB32_Premultiplied
|
|
|
|
|| image.format() == QImage::Format_RGB32);
|
|
|
|
|
|
|
|
return QPixmap::fromImage(std::move(image), Qt::NoFormatConversion);
|
|
|
|
}
|
|
|
|
|
2016-11-28 15:45:07 +00:00
|
|
|
QImage prepareBlur(QImage img) {
|
2019-01-25 14:37:28 +00:00
|
|
|
if (img.isNull()) {
|
|
|
|
return img;
|
|
|
|
}
|
2019-02-06 14:38:37 +00:00
|
|
|
const auto ratio = img.devicePixelRatio();
|
|
|
|
const auto fmt = img.format();
|
2015-01-05 20:17:33 +00:00
|
|
|
if (fmt != QImage::Format_RGB32 && fmt != QImage::Format_ARGB32_Premultiplied) {
|
2019-02-06 14:38:37 +00:00
|
|
|
img = std::move(img).convertToFormat(QImage::Format_ARGB32_Premultiplied);
|
2017-05-24 14:36:58 +00:00
|
|
|
img.setDevicePixelRatio(ratio);
|
2014-08-11 09:03:45 +00:00
|
|
|
}
|
2014-09-20 21:35:46 +00:00
|
|
|
|
|
|
|
uchar *pix = img.bits();
|
|
|
|
if (pix) {
|
2015-01-05 20:17:33 +00:00
|
|
|
int w = img.width(), h = img.height(), wold = w, hold = h;
|
|
|
|
const int radius = 3;
|
2014-09-20 21:35:46 +00:00
|
|
|
const int r1 = radius + 1;
|
|
|
|
const int div = radius * 2 + 1;
|
2015-01-05 20:17:33 +00:00
|
|
|
const int stride = w * 4;
|
2014-09-20 21:35:46 +00:00
|
|
|
if (radius < 16 && div < w && div < h && stride <= w * 4) {
|
2015-01-05 20:17:33 +00:00
|
|
|
bool withalpha = img.hasAlphaChannel();
|
|
|
|
if (withalpha) {
|
|
|
|
QImage imgsmall(w, h, img.format());
|
|
|
|
{
|
2016-12-03 12:10:35 +00:00
|
|
|
Painter p(&imgsmall);
|
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
|
2015-01-05 20:17:33 +00:00
|
|
|
p.setCompositionMode(QPainter::CompositionMode_Source);
|
2016-10-31 12:29:26 +00:00
|
|
|
p.fillRect(0, 0, w, h, Qt::transparent);
|
2015-01-05 20:17:33 +00:00
|
|
|
p.drawImage(QRect(radius, radius, w - 2 * radius, h - 2 * radius), img, QRect(0, 0, w, h));
|
|
|
|
}
|
2017-05-24 14:36:58 +00:00
|
|
|
imgsmall.setDevicePixelRatio(ratio);
|
|
|
|
auto was = img;
|
|
|
|
img = std::move(imgsmall);
|
2015-01-05 20:17:33 +00:00
|
|
|
imgsmall = QImage();
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(!img.isNull());
|
2016-02-28 13:23:03 +00:00
|
|
|
|
2015-01-05 20:17:33 +00:00
|
|
|
pix = img.bits();
|
|
|
|
if (!pix) return was;
|
|
|
|
}
|
2014-09-20 21:35:46 +00:00
|
|
|
uint64 *rgb = new uint64[w * h];
|
|
|
|
|
|
|
|
int x, y, i;
|
|
|
|
|
|
|
|
int yw = 0;
|
|
|
|
const int we = w - r1;
|
|
|
|
for (y = 0; y < h; y++) {
|
2016-11-28 15:45:07 +00:00
|
|
|
uint64 cur = blurGetColors(&pix[yw]);
|
2014-09-20 21:35:46 +00:00
|
|
|
uint64 rgballsum = -radius * cur;
|
|
|
|
uint64 rgbsum = cur * ((r1 * (r1 + 1)) >> 1);
|
|
|
|
|
|
|
|
for (i = 1; i <= radius; i++) {
|
2016-11-28 15:45:07 +00:00
|
|
|
uint64 cur = blurGetColors(&pix[yw + i * 4]);
|
2014-09-20 21:35:46 +00:00
|
|
|
rgbsum += cur * (r1 - i);
|
|
|
|
rgballsum += cur;
|
2014-08-11 09:03:45 +00:00
|
|
|
}
|
2014-09-20 21:35:46 +00:00
|
|
|
|
|
|
|
x = 0;
|
|
|
|
|
|
|
|
#define update(start, middle, end) \
|
2015-01-05 20:17:33 +00:00
|
|
|
rgb[y * w + x] = (rgbsum >> 4) & 0x00FF00FF00FF00FFLL; \
|
2016-11-28 15:45:07 +00:00
|
|
|
rgballsum += blurGetColors(&pix[yw + (start) * 4]) - 2 * blurGetColors(&pix[yw + (middle) * 4]) + blurGetColors(&pix[yw + (end) * 4]); \
|
2014-09-22 03:52:37 +00:00
|
|
|
rgbsum += rgballsum; \
|
|
|
|
x++;
|
2014-09-20 21:35:46 +00:00
|
|
|
|
|
|
|
while (x < r1) {
|
|
|
|
update(0, x, x + r1);
|
|
|
|
}
|
|
|
|
while (x < we) {
|
|
|
|
update(x - r1, x, x + r1);
|
|
|
|
}
|
|
|
|
while (x < w) {
|
|
|
|
update(x - r1, x, w - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef update
|
|
|
|
|
|
|
|
yw += stride;
|
2014-08-11 09:03:45 +00:00
|
|
|
}
|
2014-09-20 21:35:46 +00:00
|
|
|
|
|
|
|
const int he = h - r1;
|
|
|
|
for (x = 0; x < w; x++) {
|
|
|
|
uint64 rgballsum = -radius * rgb[x];
|
|
|
|
uint64 rgbsum = rgb[x] * ((r1 * (r1 + 1)) >> 1);
|
|
|
|
for (i = 1; i <= radius; i++) {
|
|
|
|
rgbsum += rgb[i * w + x] * (r1 - i);
|
|
|
|
rgballsum += rgb[i * w + x];
|
|
|
|
}
|
|
|
|
|
|
|
|
y = 0;
|
|
|
|
int yi = x * 4;
|
|
|
|
|
|
|
|
#define update(start, middle, end) \
|
2015-01-05 20:17:33 +00:00
|
|
|
uint64 res = rgbsum >> 4; \
|
2014-09-22 03:52:37 +00:00
|
|
|
pix[yi] = res & 0xFF; \
|
|
|
|
pix[yi + 1] = (res >> 16) & 0xFF; \
|
|
|
|
pix[yi + 2] = (res >> 32) & 0xFF; \
|
2015-01-05 20:17:33 +00:00
|
|
|
pix[yi + 3] = (res >> 48) & 0xFF; \
|
2014-09-22 03:52:37 +00:00
|
|
|
rgballsum += rgb[x + (start) * w] - 2 * rgb[x + (middle) * w] + rgb[x + (end) * w]; \
|
|
|
|
rgbsum += rgballsum; \
|
|
|
|
y++; \
|
|
|
|
yi += stride;
|
2014-09-20 21:35:46 +00:00
|
|
|
|
|
|
|
while (y < r1) {
|
|
|
|
update(0, y, y + r1);
|
|
|
|
}
|
|
|
|
while (y < he) {
|
|
|
|
update(y - r1, y, y + r1);
|
|
|
|
}
|
|
|
|
while (y < h) {
|
|
|
|
update(y - r1, y, h - 1);
|
2014-08-11 09:03:45 +00:00
|
|
|
}
|
2014-09-20 21:35:46 +00:00
|
|
|
|
2016-11-28 15:45:07 +00:00
|
|
|
#undef update
|
|
|
|
}
|
|
|
|
|
|
|
|
delete[] rgb;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return img;
|
|
|
|
}
|
|
|
|
|
2019-02-06 14:38:37 +00:00
|
|
|
QImage BlurLargeImage(QImage image, int radius) {
|
|
|
|
const auto width = image.width();
|
|
|
|
const auto height = image.height();
|
|
|
|
if (width <= radius || height <= radius || radius < 1) {
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (image.format() != QImage::Format_RGB32
|
|
|
|
&& image.format() != QImage::Format_ARGB32_Premultiplied) {
|
|
|
|
image = std::move(image).convertToFormat(
|
|
|
|
QImage::Format_ARGB32_Premultiplied);
|
|
|
|
}
|
|
|
|
const auto pixels = image.bits();
|
|
|
|
|
2019-02-07 16:36:30 +00:00
|
|
|
const auto width_m1 = width - 1;
|
|
|
|
const auto height_m1 = height - 1;
|
|
|
|
const auto widthxheight = width * height;
|
2019-02-06 14:38:37 +00:00
|
|
|
const auto div = 2 * radius + 1;
|
2019-02-07 16:36:30 +00:00
|
|
|
const auto radius_p1 = radius + 1;
|
|
|
|
const auto divsum = radius_p1 * radius_p1;
|
2019-02-06 14:38:37 +00:00
|
|
|
|
|
|
|
const auto dvcount = 256 * divsum;
|
|
|
|
const auto buffers = (div * 3) // stack
|
|
|
|
+ std::max(width, height) // vmin
|
2019-02-07 16:36:30 +00:00
|
|
|
+ widthxheight * 3 // rgb
|
2019-02-06 14:38:37 +00:00
|
|
|
+ dvcount; // dv
|
|
|
|
auto storage = std::vector<int>(buffers);
|
|
|
|
auto taken = 0;
|
|
|
|
const auto take = [&](int size) {
|
|
|
|
const auto result = gsl::make_span(storage).subspan(taken, size);
|
|
|
|
taken += size;
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Small buffers
|
|
|
|
const auto stack = take(div * 3).data();
|
|
|
|
const auto vmin = take(std::max(width, height)).data();
|
|
|
|
|
|
|
|
// Large buffers
|
2019-02-07 16:36:30 +00:00
|
|
|
const auto rgb = take(widthxheight * 3).data();
|
2019-02-06 14:38:37 +00:00
|
|
|
const auto dvs = take(dvcount);
|
|
|
|
|
2019-02-07 16:36:30 +00:00
|
|
|
auto &&ints = ranges::view::ints;
|
|
|
|
for (auto &&[value, index] : ranges::view::zip(dvs, ints(0))) {
|
2019-02-06 14:38:37 +00:00
|
|
|
value = (index / divsum);
|
|
|
|
}
|
|
|
|
const auto dv = dvs.data();
|
|
|
|
|
|
|
|
// Variables
|
|
|
|
auto stackpointer = 0;
|
2019-02-07 16:36:30 +00:00
|
|
|
for (const auto x : ints(0, width)) {
|
|
|
|
vmin[x] = std::min(x + radius_p1, width_m1);
|
|
|
|
}
|
|
|
|
for (const auto y : ints(0, height)) {
|
2019-02-06 14:38:37 +00:00
|
|
|
auto rinsum = 0;
|
|
|
|
auto ginsum = 0;
|
|
|
|
auto binsum = 0;
|
|
|
|
auto routsum = 0;
|
|
|
|
auto goutsum = 0;
|
|
|
|
auto boutsum = 0;
|
|
|
|
auto rsum = 0;
|
|
|
|
auto gsum = 0;
|
|
|
|
auto bsum = 0;
|
|
|
|
|
2019-02-07 16:36:30 +00:00
|
|
|
const auto y_width = y * width;
|
|
|
|
for (const auto i : ints(-radius, radius + 1)) {
|
2019-02-06 14:38:37 +00:00
|
|
|
const auto sir = &stack[(i + radius) * 3];
|
2019-02-07 16:36:30 +00:00
|
|
|
const auto x = std::clamp(i, 0, width_m1);
|
|
|
|
const auto offset = (y_width + x) * 4;
|
2019-02-06 14:38:37 +00:00
|
|
|
sir[0] = pixels[offset];
|
|
|
|
sir[1] = pixels[offset + 1];
|
|
|
|
sir[2] = pixels[offset + 2];
|
|
|
|
|
2019-02-07 16:36:30 +00:00
|
|
|
const auto rbs = radius_p1 - std::abs(i);
|
2019-02-06 14:38:37 +00:00
|
|
|
rsum += sir[0] * rbs;
|
|
|
|
gsum += sir[1] * rbs;
|
|
|
|
bsum += sir[2] * rbs;
|
2019-02-07 16:36:30 +00:00
|
|
|
|
2019-02-06 14:38:37 +00:00
|
|
|
if (i > 0) {
|
|
|
|
rinsum += sir[0];
|
|
|
|
ginsum += sir[1];
|
|
|
|
binsum += sir[2];
|
|
|
|
} else {
|
|
|
|
routsum += sir[0];
|
|
|
|
goutsum += sir[1];
|
|
|
|
boutsum += sir[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stackpointer = radius;
|
|
|
|
|
2019-02-07 16:36:30 +00:00
|
|
|
for (const auto x : ints(0, width)) {
|
|
|
|
const auto position = (y_width + x) * 3;
|
|
|
|
rgb[position] = dv[rsum];
|
|
|
|
rgb[position + 1] = dv[gsum];
|
|
|
|
rgb[position + 2] = dv[bsum];
|
2019-02-06 14:38:37 +00:00
|
|
|
|
|
|
|
rsum -= routsum;
|
|
|
|
gsum -= goutsum;
|
|
|
|
bsum -= boutsum;
|
|
|
|
|
2019-02-07 16:36:30 +00:00
|
|
|
const auto stackstart = (stackpointer - radius + div) % div;
|
|
|
|
const auto sir = &stack[stackstart * 3];
|
2019-02-06 14:38:37 +00:00
|
|
|
|
|
|
|
routsum -= sir[0];
|
|
|
|
goutsum -= sir[1];
|
|
|
|
boutsum -= sir[2];
|
|
|
|
|
2019-02-07 16:36:30 +00:00
|
|
|
const auto offset = (y_width + vmin[x]) * 4;
|
2019-02-06 14:38:37 +00:00
|
|
|
sir[0] = pixels[offset];
|
|
|
|
sir[1] = pixels[offset + 1];
|
|
|
|
sir[2] = pixels[offset + 2];
|
|
|
|
rinsum += sir[0];
|
|
|
|
ginsum += sir[1];
|
|
|
|
binsum += sir[2];
|
|
|
|
|
|
|
|
rsum += rinsum;
|
|
|
|
gsum += ginsum;
|
|
|
|
bsum += binsum;
|
|
|
|
{
|
|
|
|
stackpointer = (stackpointer + 1) % div;
|
2019-02-07 16:36:30 +00:00
|
|
|
const auto sir = &stack[stackpointer * 3];
|
2019-02-06 14:38:37 +00:00
|
|
|
|
|
|
|
routsum += sir[0];
|
|
|
|
goutsum += sir[1];
|
|
|
|
boutsum += sir[2];
|
|
|
|
|
|
|
|
rinsum -= sir[0];
|
|
|
|
ginsum -= sir[1];
|
|
|
|
binsum -= sir[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 16:36:30 +00:00
|
|
|
for (const auto y : ints(0, height)) {
|
|
|
|
vmin[y] = std::min(y + radius_p1, height_m1) * width;
|
|
|
|
}
|
|
|
|
for (const auto x : ints(0, width)) {
|
2019-02-06 14:38:37 +00:00
|
|
|
auto rinsum = 0;
|
|
|
|
auto ginsum = 0;
|
|
|
|
auto binsum = 0;
|
|
|
|
auto routsum = 0;
|
|
|
|
auto goutsum = 0;
|
|
|
|
auto boutsum = 0;
|
|
|
|
auto rsum = 0;
|
|
|
|
auto gsum = 0;
|
|
|
|
auto bsum = 0;
|
2019-02-07 16:36:30 +00:00
|
|
|
for (const auto i : ints(-radius, radius + 1)) {
|
|
|
|
const auto y = std::clamp(i, 0, height_m1);
|
|
|
|
const auto position = (y * width + x) * 3;
|
2019-02-06 14:38:37 +00:00
|
|
|
const auto sir = &stack[(i + radius) * 3];
|
|
|
|
|
2019-02-07 16:36:30 +00:00
|
|
|
sir[0] = rgb[position];
|
|
|
|
sir[1] = rgb[position + 1];
|
|
|
|
sir[2] = rgb[position + 2];
|
2019-02-06 14:38:37 +00:00
|
|
|
|
2019-02-07 16:36:30 +00:00
|
|
|
const auto rbs = radius_p1 - std::abs(i);
|
|
|
|
rsum += sir[0] * rbs;
|
|
|
|
gsum += sir[1] * rbs;
|
|
|
|
bsum += sir[2] * rbs;
|
2019-02-06 14:38:37 +00:00
|
|
|
if (i > 0) {
|
|
|
|
rinsum += sir[0];
|
|
|
|
ginsum += sir[1];
|
|
|
|
binsum += sir[2];
|
|
|
|
} else {
|
|
|
|
routsum += sir[0];
|
|
|
|
goutsum += sir[1];
|
|
|
|
boutsum += sir[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stackpointer = radius;
|
2019-02-07 16:36:30 +00:00
|
|
|
for (const auto y : ints(0, height)) {
|
|
|
|
const auto offset = (y * width + x) * 4;
|
2019-02-06 14:38:37 +00:00
|
|
|
pixels[offset] = dv[rsum];
|
|
|
|
pixels[offset + 1] = dv[gsum];
|
|
|
|
pixels[offset + 2] = dv[bsum];
|
|
|
|
rsum -= routsum;
|
|
|
|
gsum -= goutsum;
|
|
|
|
bsum -= boutsum;
|
|
|
|
|
2019-02-07 16:36:30 +00:00
|
|
|
const auto stackstart = (stackpointer - radius + div) % div;
|
|
|
|
const auto sir = &stack[stackstart * 3];
|
2019-02-06 14:38:37 +00:00
|
|
|
|
|
|
|
routsum -= sir[0];
|
|
|
|
goutsum -= sir[1];
|
|
|
|
boutsum -= sir[2];
|
|
|
|
|
2019-02-07 16:36:30 +00:00
|
|
|
const auto position = (vmin[y] + x) * 3;
|
|
|
|
sir[0] = rgb[position];
|
|
|
|
sir[1] = rgb[position + 1];
|
|
|
|
sir[2] = rgb[position + 2];
|
2019-02-06 14:38:37 +00:00
|
|
|
|
|
|
|
rinsum += sir[0];
|
|
|
|
ginsum += sir[1];
|
|
|
|
binsum += sir[2];
|
|
|
|
|
|
|
|
rsum += rinsum;
|
|
|
|
gsum += ginsum;
|
|
|
|
bsum += binsum;
|
|
|
|
{
|
|
|
|
stackpointer = (stackpointer + 1) % div;
|
|
|
|
const auto sir = &stack[stackpointer * 3];
|
|
|
|
|
|
|
|
routsum += sir[0];
|
|
|
|
goutsum += sir[1];
|
|
|
|
boutsum += sir[2];
|
|
|
|
|
|
|
|
rinsum -= sir[0];
|
|
|
|
ginsum -= sir[1];
|
|
|
|
binsum -= sir[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
2016-11-28 15:45:07 +00:00
|
|
|
void prepareCircle(QImage &img) {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(!img.isNull());
|
2016-11-28 15:45:07 +00:00
|
|
|
|
|
|
|
img = img.convertToFormat(QImage::Format_ARGB32_Premultiplied);
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(!img.isNull());
|
2016-11-28 15:45:07 +00:00
|
|
|
|
|
|
|
Painter p(&img);
|
|
|
|
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
|
2019-03-28 08:51:08 +00:00
|
|
|
p.drawImage(
|
|
|
|
QRect(QPoint(), img.size() / img.devicePixelRatio()),
|
|
|
|
circleMask(img.size()));
|
2016-11-28 15:45:07 +00:00
|
|
|
}
|
|
|
|
|
2017-12-25 19:26:08 +00:00
|
|
|
void prepareRound(
|
|
|
|
QImage &image,
|
|
|
|
QImage *cornerMasks,
|
|
|
|
RectParts corners,
|
|
|
|
QRect target) {
|
|
|
|
if (target.isNull()) {
|
|
|
|
target = QRect(QPoint(), image.size());
|
|
|
|
} else {
|
|
|
|
Assert(QRect(QPoint(), image.size()).contains(target));
|
2016-11-28 15:45:07 +00:00
|
|
|
}
|
2017-07-13 14:42:46 +00:00
|
|
|
auto cornerWidth = cornerMasks[0].width();
|
|
|
|
auto cornerHeight = cornerMasks[0].height();
|
2016-11-28 15:45:07 +00:00
|
|
|
auto imageWidth = image.width();
|
|
|
|
auto imageHeight = image.height();
|
|
|
|
if (imageWidth < 2 * cornerWidth || imageHeight < 2 * cornerHeight) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
constexpr auto imageIntsPerPixel = 1;
|
|
|
|
auto imageIntsPerLine = (image.bytesPerLine() >> 2);
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(image.depth() == static_cast<int>((imageIntsPerPixel * sizeof(uint32)) << 3));
|
|
|
|
Assert(image.bytesPerLine() == (imageIntsPerLine << 2));
|
2016-11-28 15:45:07 +00:00
|
|
|
|
|
|
|
auto ints = reinterpret_cast<uint32*>(image.bits());
|
2017-12-25 19:26:08 +00:00
|
|
|
auto intsTopLeft = ints + target.x() + target.y() * imageWidth;
|
|
|
|
auto intsTopRight = ints + target.x() + target.width() - cornerWidth + target.y() * imageWidth;
|
|
|
|
auto intsBottomLeft = ints + target.x() + (target.y() + target.height() - cornerHeight) * imageWidth;
|
|
|
|
auto intsBottomRight = ints + target.x() + target.width() - cornerWidth + (target.y() + target.height() - cornerHeight) * imageWidth;
|
2018-04-07 08:47:08 +00:00
|
|
|
auto maskCorner = [&](uint32 *imageInts, const QImage &mask) {
|
2017-07-13 14:42:46 +00:00
|
|
|
auto maskWidth = mask.width();
|
|
|
|
auto maskHeight = mask.height();
|
|
|
|
auto maskBytesPerPixel = (mask.depth() >> 3);
|
|
|
|
auto maskBytesPerLine = mask.bytesPerLine();
|
2016-11-28 15:45:07 +00:00
|
|
|
auto maskBytesAdded = maskBytesPerLine - maskWidth * maskBytesPerPixel;
|
2017-07-13 14:42:46 +00:00
|
|
|
auto maskBytes = mask.constBits();
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(maskBytesAdded >= 0);
|
|
|
|
Assert(mask.depth() == (maskBytesPerPixel << 3));
|
2016-11-28 15:45:07 +00:00
|
|
|
auto imageIntsAdded = imageIntsPerLine - maskWidth * imageIntsPerPixel;
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(imageIntsAdded >= 0);
|
2016-11-28 15:45:07 +00:00
|
|
|
for (auto y = 0; y != maskHeight; ++y) {
|
|
|
|
for (auto x = 0; x != maskWidth; ++x) {
|
|
|
|
auto opacity = static_cast<anim::ShiftedMultiplier>(*maskBytes) + 1;
|
|
|
|
*imageInts = anim::unshifted(anim::shifted(*imageInts) * opacity);
|
|
|
|
maskBytes += maskBytesPerPixel;
|
|
|
|
imageInts += imageIntsPerPixel;
|
|
|
|
}
|
|
|
|
maskBytes += maskBytesAdded;
|
|
|
|
imageInts += imageIntsAdded;
|
|
|
|
}
|
|
|
|
};
|
2017-12-18 17:50:25 +00:00
|
|
|
if (corners & RectPart::TopLeft) maskCorner(intsTopLeft, cornerMasks[0]);
|
|
|
|
if (corners & RectPart::TopRight) maskCorner(intsTopRight, cornerMasks[1]);
|
|
|
|
if (corners & RectPart::BottomLeft) maskCorner(intsBottomLeft, cornerMasks[2]);
|
|
|
|
if (corners & RectPart::BottomRight) maskCorner(intsBottomRight, cornerMasks[3]);
|
2016-11-28 15:45:07 +00:00
|
|
|
}
|
|
|
|
|
2017-12-25 19:26:08 +00:00
|
|
|
void prepareRound(
|
|
|
|
QImage &image,
|
|
|
|
ImageRoundRadius radius,
|
|
|
|
RectParts corners,
|
|
|
|
QRect target) {
|
|
|
|
if (!static_cast<int>(corners)) {
|
|
|
|
return;
|
|
|
|
} else if (radius == ImageRoundRadius::Ellipse) {
|
|
|
|
Assert((corners & RectPart::AllCorners) == RectPart::AllCorners);
|
|
|
|
Assert(target.isNull());
|
|
|
|
prepareCircle(image);
|
2019-03-28 08:51:08 +00:00
|
|
|
return;
|
2017-12-25 19:26:08 +00:00
|
|
|
}
|
|
|
|
Assert(!image.isNull());
|
|
|
|
|
|
|
|
image.setDevicePixelRatio(cRetinaFactor());
|
|
|
|
image = std::move(image).convertToFormat(QImage::Format_ARGB32_Premultiplied);
|
|
|
|
Assert(!image.isNull());
|
|
|
|
|
|
|
|
auto masks = App::cornersMask(radius);
|
|
|
|
prepareRound(image, masks, corners, target);
|
|
|
|
}
|
|
|
|
|
2016-12-23 13:21:01 +00:00
|
|
|
QImage prepareColored(style::color add, QImage image) {
|
2019-05-13 21:57:59 +00:00
|
|
|
return prepareColored(add->c, std::move(image));
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage prepareColored(QColor add, QImage image) {
|
|
|
|
const auto format = image.format();
|
2016-11-28 15:45:07 +00:00
|
|
|
if (format != QImage::Format_RGB32 && format != QImage::Format_ARGB32_Premultiplied) {
|
2017-02-21 13:45:56 +00:00
|
|
|
image = std::move(image).convertToFormat(QImage::Format_ARGB32_Premultiplied);
|
2016-11-28 15:45:07 +00:00
|
|
|
}
|
|
|
|
|
2019-05-13 21:57:59 +00:00
|
|
|
if (const auto pix = image.bits()) {
|
|
|
|
const auto ca = int(add.alphaF() * 0xFF);
|
|
|
|
const auto cr = int(add.redF() * 0xFF);
|
|
|
|
const auto cg = int(add.greenF() * 0xFF);
|
|
|
|
const auto cb = int(add .blueF() * 0xFF);
|
|
|
|
const auto w = image.width();
|
|
|
|
const auto h = image.height();
|
|
|
|
const auto size = w * h * 4;
|
|
|
|
for (auto i = index_type(); i != size; i += 4) {
|
2016-11-28 15:45:07 +00:00
|
|
|
int b = pix[i], g = pix[i + 1], r = pix[i + 2], a = pix[i + 3], aca = a * ca;
|
|
|
|
pix[i + 0] = uchar(b + ((aca * (cb - b)) >> 16));
|
|
|
|
pix[i + 1] = uchar(g + ((aca * (cg - g)) >> 16));
|
|
|
|
pix[i + 2] = uchar(r + ((aca * (cr - r)) >> 16));
|
|
|
|
pix[i + 3] = uchar(a + ((aca * (0xFF - a)) >> 16));
|
|
|
|
}
|
|
|
|
}
|
2017-02-21 14:37:53 +00:00
|
|
|
return image;
|
2016-11-28 15:45:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QImage prepareOpaque(QImage image) {
|
|
|
|
if (image.hasAlphaChannel()) {
|
2017-02-21 13:45:56 +00:00
|
|
|
image = std::move(image).convertToFormat(QImage::Format_ARGB32_Premultiplied);
|
2016-11-28 15:45:07 +00:00
|
|
|
auto ints = reinterpret_cast<uint32*>(image.bits());
|
|
|
|
auto bg = anim::shifted(st::imageBgTransparent->c);
|
|
|
|
auto width = image.width();
|
|
|
|
auto height = image.height();
|
|
|
|
auto addPerLine = (image.bytesPerLine() / sizeof(uint32)) - width;
|
|
|
|
for (auto y = 0; y != height; ++y) {
|
|
|
|
for (auto x = 0; x != width; ++x) {
|
|
|
|
auto components = anim::shifted(*ints);
|
2016-12-13 17:07:56 +00:00
|
|
|
*ints++ = anim::unshifted(components * 256 + bg * (256 - anim::getAlpha(components)));
|
2016-11-28 15:45:07 +00:00
|
|
|
}
|
2016-12-13 17:07:56 +00:00
|
|
|
ints += addPerLine;
|
2016-11-28 15:45:07 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-21 14:37:53 +00:00
|
|
|
return image;
|
2016-11-28 15:45:07 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:15:13 +00:00
|
|
|
QImage prepare(QImage img, int w, int h, Images::Options options, int outerw, int outerh, const style::color *colored) {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(!img.isNull());
|
2017-08-31 16:28:58 +00:00
|
|
|
if (options & Images::Option::Blurred) {
|
2017-05-24 14:36:58 +00:00
|
|
|
img = prepareBlur(std::move(img));
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(!img.isNull());
|
2016-11-28 15:45:07 +00:00
|
|
|
}
|
|
|
|
if (w <= 0 || (w == img.width() && (h <= 0 || h == img.height()))) {
|
|
|
|
} else if (h <= 0) {
|
2017-08-31 16:28:58 +00:00
|
|
|
img = img.scaledToWidth(w, (options & Images::Option::Smooth) ? Qt::SmoothTransformation : Qt::FastTransformation);
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(!img.isNull());
|
2016-11-28 15:45:07 +00:00
|
|
|
} else {
|
2017-08-31 16:28:58 +00:00
|
|
|
img = img.scaled(w, h, Qt::IgnoreAspectRatio, (options & Images::Option::Smooth) ? Qt::SmoothTransformation : Qt::FastTransformation);
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(!img.isNull());
|
2016-11-28 15:45:07 +00:00
|
|
|
}
|
|
|
|
if (outerw > 0 && outerh > 0) {
|
|
|
|
outerw *= cIntRetinaFactor();
|
|
|
|
outerh *= cIntRetinaFactor();
|
|
|
|
if (outerw != w || outerh != h) {
|
|
|
|
img.setDevicePixelRatio(cRetinaFactor());
|
2017-06-06 09:16:56 +00:00
|
|
|
auto result = QImage(outerw, outerh, QImage::Format_ARGB32_Premultiplied);
|
2016-11-28 15:45:07 +00:00
|
|
|
result.setDevicePixelRatio(cRetinaFactor());
|
2017-06-06 09:16:56 +00:00
|
|
|
if (options & Images::Option::TransparentBackground) {
|
|
|
|
result.fill(Qt::transparent);
|
|
|
|
}
|
2016-11-28 15:45:07 +00:00
|
|
|
{
|
|
|
|
QPainter p(&result);
|
2019-01-29 16:26:19 +00:00
|
|
|
if (!(options & Images::Option::TransparentBackground)) {
|
|
|
|
if (w < outerw || h < outerh) {
|
|
|
|
p.fillRect(0, 0, result.width(), result.height(), st::imageBg);
|
|
|
|
}
|
2016-11-28 15:45:07 +00:00
|
|
|
}
|
|
|
|
p.drawImage((result.width() - img.width()) / (2 * cIntRetinaFactor()), (result.height() - img.height()) / (2 * cIntRetinaFactor()), img);
|
|
|
|
}
|
|
|
|
img = result;
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(!img.isNull());
|
2016-11-28 15:45:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
auto corners = [](Images::Options options) {
|
2017-12-18 17:50:25 +00:00
|
|
|
return ((options & Images::Option::RoundedTopLeft) ? RectPart::TopLeft : RectPart::None)
|
|
|
|
| ((options & Images::Option::RoundedTopRight) ? RectPart::TopRight : RectPart::None)
|
|
|
|
| ((options & Images::Option::RoundedBottomLeft) ? RectPart::BottomLeft : RectPart::None)
|
|
|
|
| ((options & Images::Option::RoundedBottomRight) ? RectPart::BottomRight : RectPart::None);
|
2016-11-28 15:45:07 +00:00
|
|
|
};
|
2017-08-31 16:28:58 +00:00
|
|
|
if (options & Images::Option::Circled) {
|
2016-11-28 15:45:07 +00:00
|
|
|
prepareCircle(img);
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(!img.isNull());
|
2017-08-31 16:28:58 +00:00
|
|
|
} else if (options & Images::Option::RoundedLarge) {
|
2016-11-28 15:45:07 +00:00
|
|
|
prepareRound(img, ImageRoundRadius::Large, corners(options));
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(!img.isNull());
|
2017-08-31 16:28:58 +00:00
|
|
|
} else if (options & Images::Option::RoundedSmall) {
|
2016-11-28 15:45:07 +00:00
|
|
|
prepareRound(img, ImageRoundRadius::Small, corners(options));
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(!img.isNull());
|
2016-11-28 15:45:07 +00:00
|
|
|
}
|
2017-08-31 16:28:58 +00:00
|
|
|
if (options & Images::Option::Colored) {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(colored != nullptr);
|
2017-06-06 09:15:13 +00:00
|
|
|
img = prepareColored(*colored, std::move(img));
|
|
|
|
}
|
2016-11-28 15:45:07 +00:00
|
|
|
img.setDevicePixelRatio(cRetinaFactor());
|
2017-02-21 14:37:53 +00:00
|
|
|
return img;
|
2016-11-28 15:45:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Images
|