Refactor Images manipulating methods.
This commit is contained in:
parent
0488688592
commit
87e62b7737
2 changed files with 333 additions and 208 deletions
|
|
@ -18,6 +18,7 @@
|
||||||
#include "zlib.h"
|
#include "zlib.h"
|
||||||
#include <QtCore/QFile>
|
#include <QtCore/QFile>
|
||||||
#include <QtCore/QBuffer>
|
#include <QtCore/QBuffer>
|
||||||
|
#include <QtCore/QMutex>
|
||||||
#include <QtGui/QImageReader>
|
#include <QtGui/QImageReader>
|
||||||
#include <QtSvg/QSvgRenderer>
|
#include <QtSvg/QSvgRenderer>
|
||||||
|
|
||||||
|
|
@ -27,19 +28,26 @@ namespace {
|
||||||
// They should be smaller.
|
// They should be smaller.
|
||||||
constexpr auto kMaxGzipFileSize = 5 * 1024 * 1024;
|
constexpr auto kMaxGzipFileSize = 5 * 1024 * 1024;
|
||||||
|
|
||||||
TG_FORCE_INLINE uint64 blurGetColors(const uchar *p) {
|
TG_FORCE_INLINE uint64 BlurGetColors(const uchar *p) {
|
||||||
return (uint64)p[0] + ((uint64)p[1] << 16) + ((uint64)p[2] << 32) + ((uint64)p[3] << 48);
|
return (uint64)p[0]
|
||||||
|
+ ((uint64)p[1] << 16)
|
||||||
|
+ ((uint64)p[2] << 32)
|
||||||
|
+ ((uint64)p[3] << 48);
|
||||||
}
|
}
|
||||||
|
|
||||||
const QImage &circleMask(QSize size) {
|
const QImage &CircleMask(QSize size) {
|
||||||
uint64 key = (uint64(uint32(size.width())) << 32)
|
const auto key = (uint64(uint32(size.width())) << 32)
|
||||||
| uint64(uint32(size.height()));
|
| uint64(uint32(size.height()));
|
||||||
|
|
||||||
static auto masks = base::flat_map<uint64, QImage>();
|
static auto Masks = base::flat_map<uint64, QImage>();
|
||||||
const auto i = masks.find(key);
|
static auto Mutex = QMutex();
|
||||||
if (i != end(masks)) {
|
auto lock = QMutexLocker(&Mutex);
|
||||||
|
const auto i = Masks.find(key);
|
||||||
|
if (i != end(Masks)) {
|
||||||
return i->second;
|
return i->second;
|
||||||
}
|
}
|
||||||
|
lock.unlock();
|
||||||
|
|
||||||
auto mask = QImage(
|
auto mask = QImage(
|
||||||
size,
|
size,
|
||||||
QImage::Format_ARGB32_Premultiplied);
|
QImage::Format_ARGB32_Premultiplied);
|
||||||
|
|
@ -51,7 +59,9 @@ const QImage &circleMask(QSize size) {
|
||||||
p.setPen(Qt::NoPen);
|
p.setPen(Qt::NoPen);
|
||||||
p.drawEllipse(QRect(QPoint(), size));
|
p.drawEllipse(QRect(QPoint(), size));
|
||||||
}
|
}
|
||||||
return masks.emplace(key, std::move(mask)).first->second;
|
|
||||||
|
lock.relock();
|
||||||
|
return Masks.emplace(key, std::move(mask)).first->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::array<QImage, 4> PrepareCornersMask(int radius) {
|
std::array<QImage, 4> PrepareCornersMask(int radius) {
|
||||||
|
|
@ -280,7 +290,10 @@ template <int kBits> // 4 means 16x16, 3 means 8x8
|
||||||
auto exact = GenerateSmallComplexGradient(colors, rotation, progress);
|
auto exact = GenerateSmallComplexGradient(colors, rotation, progress);
|
||||||
return (exact.size() == size)
|
return (exact.size() == size)
|
||||||
? exact
|
? exact
|
||||||
: exact.scaled(size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
|
: exact.scaled(
|
||||||
|
size,
|
||||||
|
Qt::IgnoreAspectRatio,
|
||||||
|
Qt::SmoothTransformation);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
@ -449,101 +462,135 @@ ReadResult Read(ReadArgs &&args) {
|
||||||
if (args.forceOpaque
|
if (args.forceOpaque
|
||||||
&& result.format != qstr("jpg")
|
&& result.format != qstr("jpg")
|
||||||
&& result.format != qstr("jpeg")) {
|
&& result.format != qstr("jpeg")) {
|
||||||
result.image = prepareOpaque(std::move(result.image));
|
result.image = Opaque(std::move(result.image));
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
QImage prepareBlur(QImage img) {
|
[[nodiscard]] Options RoundOptions(
|
||||||
if (img.isNull()) {
|
ImageRoundRadius radius,
|
||||||
return img;
|
RectParts corners) {
|
||||||
|
const auto withCorners = [&](Option rounding) {
|
||||||
|
if (rounding == Option::None) {
|
||||||
|
return Options();
|
||||||
|
}
|
||||||
|
const auto corner = [&](RectPart part, Option skip) {
|
||||||
|
return !(corners & part) ? skip : Option();
|
||||||
|
};
|
||||||
|
return rounding
|
||||||
|
| corner(RectPart::TopLeft, Option::RoundSkipTopLeft)
|
||||||
|
| corner(RectPart::TopRight, Option::RoundSkipTopRight)
|
||||||
|
| corner(RectPart::BottomLeft, Option::RoundSkipBottomLeft)
|
||||||
|
| corner(RectPart::BottomRight, Option::RoundSkipBottomRight);
|
||||||
|
};
|
||||||
|
return withCorners((radius == ImageRoundRadius::Large)
|
||||||
|
? Option::RoundLarge
|
||||||
|
: (radius == ImageRoundRadius::Small)
|
||||||
|
? Option::RoundSmall
|
||||||
|
: (radius == ImageRoundRadius::Ellipse)
|
||||||
|
? Option::RoundCircle
|
||||||
|
: Option::None);
|
||||||
|
}
|
||||||
|
|
||||||
|
QImage Blur(QImage &&image) {
|
||||||
|
if (image.isNull()) {
|
||||||
|
return std::move(image);
|
||||||
}
|
}
|
||||||
const auto ratio = img.devicePixelRatio();
|
const auto ratio = image.devicePixelRatio();
|
||||||
const auto fmt = img.format();
|
const auto format = image.format();
|
||||||
if (fmt != QImage::Format_RGB32 && fmt != QImage::Format_ARGB32_Premultiplied) {
|
if (format != QImage::Format_RGB32
|
||||||
img = std::move(img).convertToFormat(QImage::Format_ARGB32_Premultiplied);
|
&& format != QImage::Format_ARGB32_Premultiplied) {
|
||||||
img.setDevicePixelRatio(ratio);
|
image = std::move(image).convertToFormat(
|
||||||
|
QImage::Format_ARGB32_Premultiplied);
|
||||||
|
image.setDevicePixelRatio(ratio);
|
||||||
}
|
}
|
||||||
|
|
||||||
uchar *pix = img.bits();
|
auto pix = image.bits();
|
||||||
if (pix) {
|
if (!pix) {
|
||||||
int w = img.width(), h = img.height();
|
return std::move(image);
|
||||||
const int radius = 3;
|
}
|
||||||
const int r1 = radius + 1;
|
const auto w = image.width();
|
||||||
const int div = radius * 2 + 1;
|
const auto h = image.height();
|
||||||
const int stride = w * 4;
|
const auto radius = 3;
|
||||||
if (radius < 16 && div < w && div < h && stride <= w * 4) {
|
const auto r1 = radius + 1;
|
||||||
bool withalpha = img.hasAlphaChannel();
|
const auto div = radius * 2 + 1;
|
||||||
if (withalpha) {
|
const auto stride = w * 4;
|
||||||
QImage imgsmall(w, h, img.format());
|
if (radius >= 16 || div >= w || div >= h || stride > w * 4) {
|
||||||
{
|
return std::move(image);
|
||||||
QPainter p(&imgsmall);
|
}
|
||||||
PainterHighQualityEnabler hq(p);
|
const auto withalpha = image.hasAlphaChannel();
|
||||||
|
if (withalpha) {
|
||||||
|
auto smaller = QImage(image.size(), image.format());
|
||||||
|
{
|
||||||
|
QPainter p(&smaller);
|
||||||
|
PainterHighQualityEnabler hq(p);
|
||||||
|
|
||||||
p.setCompositionMode(QPainter::CompositionMode_Source);
|
p.setCompositionMode(QPainter::CompositionMode_Source);
|
||||||
p.fillRect(0, 0, w, h, Qt::transparent);
|
p.fillRect(0, 0, w, h, Qt::transparent);
|
||||||
p.drawImage(QRect(radius, radius, w - 2 * radius, h - 2 * radius), img, QRect(0, 0, w, h));
|
p.drawImage(
|
||||||
}
|
QRect(radius, radius, w - 2 * radius, h - 2 * radius),
|
||||||
imgsmall.setDevicePixelRatio(ratio);
|
image,
|
||||||
auto was = img;
|
QRect(0, 0, w, h));
|
||||||
img = std::move(imgsmall);
|
}
|
||||||
imgsmall = QImage();
|
smaller.setDevicePixelRatio(ratio);
|
||||||
Assert(!img.isNull());
|
auto was = std::exchange(image, base::take(smaller));
|
||||||
|
Assert(!image.isNull());
|
||||||
|
|
||||||
pix = img.bits();
|
pix = image.bits();
|
||||||
if (!pix) return was;
|
if (!pix) return was;
|
||||||
}
|
}
|
||||||
uint64 *rgb = new uint64[w * h];
|
const auto buffer = std::make_unique<uint64[]>(w * h);
|
||||||
|
const auto rgb = buffer.get();
|
||||||
|
|
||||||
int x, y, i;
|
int x, y, i;
|
||||||
|
|
||||||
int yw = 0;
|
int yw = 0;
|
||||||
const int we = w - r1;
|
const int we = w - r1;
|
||||||
for (y = 0; y < h; y++) {
|
for (y = 0; y < h; y++) {
|
||||||
uint64 cur = blurGetColors(&pix[yw]);
|
uint64 cur = BlurGetColors(&pix[yw]);
|
||||||
uint64 rgballsum = -radius * cur;
|
uint64 rgballsum = -radius * cur;
|
||||||
uint64 rgbsum = cur * ((r1 * (r1 + 1)) >> 1);
|
uint64 rgbsum = cur * ((r1 * (r1 + 1)) >> 1);
|
||||||
|
|
||||||
for (i = 1; i <= radius; i++) {
|
for (i = 1; i <= radius; i++) {
|
||||||
uint64 cur = blurGetColors(&pix[yw + i * 4]);
|
uint64 cur = BlurGetColors(&pix[yw + i * 4]);
|
||||||
rgbsum += cur * (r1 - i);
|
rgbsum += cur * (r1 - i);
|
||||||
rgballsum += cur;
|
rgballsum += cur;
|
||||||
}
|
}
|
||||||
|
|
||||||
x = 0;
|
x = 0;
|
||||||
|
|
||||||
#define update(start, middle, end) \
|
#define update(start, middle, end) \
|
||||||
rgb[y * w + x] = (rgbsum >> 4) & 0x00FF00FF00FF00FFLL; \
|
rgb[y * w + x] = (rgbsum >> 4) & 0x00FF00FF00FF00FFLL; \
|
||||||
rgballsum += blurGetColors(&pix[yw + (start) * 4]) - 2 * blurGetColors(&pix[yw + (middle) * 4]) + blurGetColors(&pix[yw + (end) * 4]); \
|
rgballsum += BlurGetColors(&pix[yw + (start) * 4]) - 2 * BlurGetColors(&pix[yw + (middle) * 4]) + BlurGetColors(&pix[yw + (end) * 4]); \
|
||||||
rgbsum += rgballsum; \
|
rgbsum += rgballsum; \
|
||||||
x++;
|
x++;
|
||||||
|
|
||||||
while (x < r1) {
|
while (x < r1) {
|
||||||
update(0, x, x + r1);
|
update(0, x, x + r1);
|
||||||
}
|
}
|
||||||
while (x < we) {
|
while (x < we) {
|
||||||
update(x - r1, x, x + r1);
|
update(x - r1, x, x + r1);
|
||||||
}
|
}
|
||||||
while (x < w) {
|
while (x < w) {
|
||||||
update(x - r1, x, w - 1);
|
update(x - r1, x, w - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#undef update
|
#undef update
|
||||||
|
|
||||||
yw += stride;
|
yw += stride;
|
||||||
}
|
}
|
||||||
|
|
||||||
const int he = h - r1;
|
const int he = h - r1;
|
||||||
for (x = 0; x < w; x++) {
|
for (x = 0; x < w; x++) {
|
||||||
uint64 rgballsum = -radius * rgb[x];
|
uint64 rgballsum = -radius * rgb[x];
|
||||||
uint64 rgbsum = rgb[x] * ((r1 * (r1 + 1)) >> 1);
|
uint64 rgbsum = rgb[x] * ((r1 * (r1 + 1)) >> 1);
|
||||||
for (i = 1; i <= radius; i++) {
|
for (i = 1; i <= radius; i++) {
|
||||||
rgbsum += rgb[i * w + x] * (r1 - i);
|
rgbsum += rgb[i * w + x] * (r1 - i);
|
||||||
rgballsum += rgb[i * w + x];
|
rgballsum += rgb[i * w + x];
|
||||||
}
|
}
|
||||||
|
|
||||||
y = 0;
|
y = 0;
|
||||||
int yi = x * 4;
|
int yi = x * 4;
|
||||||
|
|
||||||
#define update(start, middle, end) \
|
#define update(start, middle, end) \
|
||||||
uint64 res = rgbsum >> 4; \
|
uint64 res = rgbsum >> 4; \
|
||||||
|
|
@ -556,30 +603,28 @@ rgbsum += rgballsum; \
|
||||||
y++; \
|
y++; \
|
||||||
yi += stride;
|
yi += stride;
|
||||||
|
|
||||||
while (y < r1) {
|
while (y < r1) {
|
||||||
update(0, y, y + r1);
|
update(0, y, y + r1);
|
||||||
}
|
}
|
||||||
while (y < he) {
|
while (y < he) {
|
||||||
update(y - r1, y, y + r1);
|
update(y - r1, y, y + r1);
|
||||||
}
|
}
|
||||||
while (y < h) {
|
while (y < h) {
|
||||||
update(y - r1, y, h - 1);
|
update(y - r1, y, h - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#undef update
|
#undef update
|
||||||
}
|
|
||||||
|
|
||||||
delete[] rgb;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return img;
|
|
||||||
|
delete[] rgb;
|
||||||
|
return std::move(image);
|
||||||
}
|
}
|
||||||
|
|
||||||
QImage BlurLargeImage(QImage image, int radius) {
|
[[nodiscard]] QImage BlurLargeImage(QImage &&image, int radius) {
|
||||||
const auto width = image.width();
|
const auto width = image.width();
|
||||||
const auto height = image.height();
|
const auto height = image.height();
|
||||||
if (width <= radius || height <= radius || radius < 1) {
|
if (width <= radius || height <= radius || radius < 1) {
|
||||||
return image;
|
return std::move(image);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (image.format() != QImage::Format_RGB32
|
if (image.format() != QImage::Format_RGB32
|
||||||
|
|
@ -790,7 +835,7 @@ QImage BlurLargeImage(QImage image, int radius) {
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] QImage DitherImage(QImage image) {
|
[[nodiscard]] QImage DitherImage(const QImage &image) {
|
||||||
Expects(image.bytesPerLine() == image.width() * 4);
|
Expects(image.bytesPerLine() == image.width() * 4);
|
||||||
|
|
||||||
const auto width = image.width();
|
const auto width = image.width();
|
||||||
|
|
@ -925,21 +970,32 @@ QImage GenerateShadow(
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void prepareCircle(QImage &img) {
|
QImage Circle(QImage &&image, QRect target) {
|
||||||
Assert(!img.isNull());
|
Expects(!image.isNull());
|
||||||
|
|
||||||
img = img.convertToFormat(QImage::Format_ARGB32_Premultiplied);
|
if (target.isNull()) {
|
||||||
Assert(!img.isNull());
|
target = QRect(QPoint(), image.size());
|
||||||
|
} else {
|
||||||
|
Assert(QRect(QPoint(), image.size()).contains(target));
|
||||||
|
}
|
||||||
|
|
||||||
QPainter p(&img);
|
image = std::move(image).convertToFormat(
|
||||||
|
QImage::Format_ARGB32_Premultiplied);
|
||||||
|
Assert(!image.isNull());
|
||||||
|
|
||||||
|
const auto ratio = image.devicePixelRatio();
|
||||||
|
auto p = QPainter(&image);
|
||||||
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
|
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
|
||||||
p.drawImage(
|
p.drawImage(
|
||||||
QRect(QPoint(), img.size() / img.devicePixelRatio()),
|
QRectF(target.topLeft() / ratio, target.size() / ratio),
|
||||||
circleMask(img.size()));
|
CircleMask(target.size()));
|
||||||
|
p.end();
|
||||||
|
|
||||||
|
return std::move(image);
|
||||||
}
|
}
|
||||||
|
|
||||||
void prepareRound(
|
QImage Round(
|
||||||
QImage &image,
|
QImage &&image,
|
||||||
gsl::span<const QImage, 4> cornerMasks,
|
gsl::span<const QImage, 4> cornerMasks,
|
||||||
RectParts corners,
|
RectParts corners,
|
||||||
QRect target) {
|
QRect target) {
|
||||||
|
|
@ -953,7 +1009,7 @@ void prepareRound(
|
||||||
auto targetWidth = target.width();
|
auto targetWidth = target.width();
|
||||||
auto targetHeight = target.height();
|
auto targetHeight = target.height();
|
||||||
if (targetWidth < cornerWidth || targetHeight < cornerHeight) {
|
if (targetWidth < cornerWidth || targetHeight < cornerHeight) {
|
||||||
return;
|
return std::move(image);
|
||||||
}
|
}
|
||||||
|
|
||||||
// We need to detach image first (if it is shared), before we
|
// We need to detach image first (if it is shared), before we
|
||||||
|
|
@ -996,20 +1052,20 @@ void prepareRound(
|
||||||
if (corners & RectPart::TopRight) maskCorner(intsTopRight, cornerMasks[1]);
|
if (corners & RectPart::TopRight) maskCorner(intsTopRight, cornerMasks[1]);
|
||||||
if (corners & RectPart::BottomLeft) maskCorner(intsBottomLeft, cornerMasks[2]);
|
if (corners & RectPart::BottomLeft) maskCorner(intsBottomLeft, cornerMasks[2]);
|
||||||
if (corners & RectPart::BottomRight) maskCorner(intsBottomRight, cornerMasks[3]);
|
if (corners & RectPart::BottomRight) maskCorner(intsBottomRight, cornerMasks[3]);
|
||||||
|
|
||||||
|
return std::move(image);
|
||||||
}
|
}
|
||||||
|
|
||||||
void prepareRound(
|
QImage Round(
|
||||||
QImage &image,
|
QImage &&image,
|
||||||
ImageRoundRadius radius,
|
ImageRoundRadius radius,
|
||||||
RectParts corners,
|
RectParts corners,
|
||||||
QRect target) {
|
QRect target) {
|
||||||
if (!static_cast<int>(corners)) {
|
if (!static_cast<int>(corners)) {
|
||||||
return;
|
return std::move(image);
|
||||||
} else if (radius == ImageRoundRadius::Ellipse) {
|
} else if (radius == ImageRoundRadius::Ellipse) {
|
||||||
Assert((corners & RectPart::AllCorners) == RectPart::AllCorners);
|
Assert((corners & RectPart::AllCorners) == RectPart::AllCorners);
|
||||||
Assert(target.isNull());
|
return Circle(std::move(image), target);
|
||||||
prepareCircle(image);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
Assert(!image.isNull());
|
Assert(!image.isNull());
|
||||||
|
|
||||||
|
|
@ -1018,17 +1074,40 @@ void prepareRound(
|
||||||
Assert(!image.isNull());
|
Assert(!image.isNull());
|
||||||
|
|
||||||
const auto masks = CornersMask(radius);
|
const auto masks = CornersMask(radius);
|
||||||
prepareRound(image, masks, corners, target);
|
return Round(std::move(image), masks, corners, target);
|
||||||
}
|
}
|
||||||
|
|
||||||
QImage prepareColored(style::color add, QImage image) {
|
QImage Round(QImage &&image, Options options, QRect target) {
|
||||||
return prepareColored(add->c, std::move(image));
|
if (options & Option::RoundCircle) {
|
||||||
|
return Circle(std::move(image), target);
|
||||||
|
} else if (!(options & (Option::RoundLarge | Option::RoundSmall))) {
|
||||||
|
return std::move(image);
|
||||||
|
}
|
||||||
|
const auto corner = [&](Option skip, RectPart part) {
|
||||||
|
return !(options & skip) ? part : RectPart::None;
|
||||||
|
};
|
||||||
|
return Round(
|
||||||
|
std::move(image),
|
||||||
|
((options & Option::RoundLarge)
|
||||||
|
? ImageRoundRadius::Large
|
||||||
|
: ImageRoundRadius::Small),
|
||||||
|
(corner(Option::RoundSkipTopLeft, RectPart::TopLeft)
|
||||||
|
| corner(Option::RoundSkipTopRight, RectPart::TopRight)
|
||||||
|
| corner(Option::RoundSkipBottomLeft, RectPart::BottomLeft)
|
||||||
|
| corner(Option::RoundSkipBottomRight, RectPart::BottomRight)),
|
||||||
|
target);
|
||||||
}
|
}
|
||||||
|
|
||||||
QImage prepareColored(QColor add, QImage image) {
|
QImage Colored(QImage &&image, style::color add) {
|
||||||
|
return Colored(std::move(image), add->c);
|
||||||
|
}
|
||||||
|
|
||||||
|
QImage Colored(QImage &&image, QColor add) {
|
||||||
const auto format = image.format();
|
const auto format = image.format();
|
||||||
if (format != QImage::Format_RGB32 && format != QImage::Format_ARGB32_Premultiplied) {
|
if (format != QImage::Format_RGB32
|
||||||
image = std::move(image).convertToFormat(QImage::Format_ARGB32_Premultiplied);
|
&& format != QImage::Format_ARGB32_Premultiplied) {
|
||||||
|
image = std::move(image).convertToFormat(
|
||||||
|
QImage::Format_ARGB32_Premultiplied);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (const auto pix = image.bits()) {
|
if (const auto pix = image.bits()) {
|
||||||
|
|
@ -1040,28 +1119,34 @@ QImage prepareColored(QColor add, QImage image) {
|
||||||
const auto h = image.height();
|
const auto h = image.height();
|
||||||
const auto size = w * h * 4;
|
const auto size = w * h * 4;
|
||||||
for (auto i = index_type(); i != size; i += 4) {
|
for (auto i = index_type(); i != size; i += 4) {
|
||||||
int b = pix[i], g = pix[i + 1], r = pix[i + 2], a = pix[i + 3], aca = a * ca;
|
const auto b = pix[i];
|
||||||
|
const auto g = pix[i + 1];
|
||||||
|
const auto r = pix[i + 2];
|
||||||
|
const auto a = pix[i + 3];
|
||||||
|
const auto aca = a * ca;
|
||||||
pix[i + 0] = uchar(b + ((aca * (cb - b)) >> 16));
|
pix[i + 0] = uchar(b + ((aca * (cb - b)) >> 16));
|
||||||
pix[i + 1] = uchar(g + ((aca * (cg - g)) >> 16));
|
pix[i + 1] = uchar(g + ((aca * (cg - g)) >> 16));
|
||||||
pix[i + 2] = uchar(r + ((aca * (cr - r)) >> 16));
|
pix[i + 2] = uchar(r + ((aca * (cr - r)) >> 16));
|
||||||
pix[i + 3] = uchar(a + ((aca * (0xFF - a)) >> 16));
|
pix[i + 3] = uchar(a + ((aca * (0xFF - a)) >> 16));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return image;
|
return std::move(image);
|
||||||
}
|
}
|
||||||
|
|
||||||
QImage prepareOpaque(QImage image) {
|
QImage Opaque(QImage &&image) {
|
||||||
if (image.hasAlphaChannel()) {
|
if (image.hasAlphaChannel()) {
|
||||||
image = std::move(image).convertToFormat(QImage::Format_ARGB32_Premultiplied);
|
image = std::move(image).convertToFormat(
|
||||||
|
QImage::Format_ARGB32_Premultiplied);
|
||||||
auto ints = reinterpret_cast<uint32*>(image.bits());
|
auto ints = reinterpret_cast<uint32*>(image.bits());
|
||||||
auto bg = anim::shifted(st::imageBgTransparent->c);
|
const auto bg = anim::shifted(QColor(Qt::white));
|
||||||
auto width = image.width();
|
const auto width = image.width();
|
||||||
auto height = image.height();
|
const auto height = image.height();
|
||||||
auto addPerLine = (image.bytesPerLine() / sizeof(uint32)) - width;
|
const auto addPerLine = (image.bytesPerLine() / sizeof(uint32)) - width;
|
||||||
for (auto y = 0; y != height; ++y) {
|
for (auto y = 0; y != height; ++y) {
|
||||||
for (auto x = 0; x != width; ++x) {
|
for (auto x = 0; x != width; ++x) {
|
||||||
auto components = anim::shifted(*ints);
|
const auto components = anim::shifted(*ints);
|
||||||
*ints++ = anim::unshifted(components * 256 + bg * (256 - anim::getAlpha(components)));
|
*ints++ = anim::unshifted(components * 256
|
||||||
|
+ bg * (256 - anim::getAlpha(components)));
|
||||||
}
|
}
|
||||||
ints += addPerLine;
|
ints += addPerLine;
|
||||||
}
|
}
|
||||||
|
|
@ -1069,66 +1154,72 @@ QImage prepareOpaque(QImage image) {
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
QImage prepare(QImage img, int w, int h, Images::Options options, int outerw, int outerh, const style::color *colored) {
|
QImage Prepare(QImage image, int w, int h, const PrepareArgs &args) {
|
||||||
Assert(!img.isNull());
|
Expects(!image.isNull());
|
||||||
if (options & Images::Option::Blurred) {
|
|
||||||
img = prepareBlur(std::move(img));
|
if (args.options & Option::Blur) {
|
||||||
Assert(!img.isNull());
|
image = Blur(std::move(image));
|
||||||
|
Assert(!image.isNull());
|
||||||
}
|
}
|
||||||
if (w <= 0 || (w == img.width() && (h <= 0 || h == img.height()))) {
|
if (w <= 0
|
||||||
|
|| (w == image.width() && (h <= 0 || h == image.height()))) {
|
||||||
} else if (h <= 0) {
|
} else if (h <= 0) {
|
||||||
img = img.scaledToWidth(w, (options & Images::Option::Smooth) ? Qt::SmoothTransformation : Qt::FastTransformation);
|
image = image.scaledToWidth(
|
||||||
Assert(!img.isNull());
|
w,
|
||||||
|
((args.options & Images::Option::FastTransform)
|
||||||
|
? Qt::FastTransformation
|
||||||
|
: Qt::SmoothTransformation));
|
||||||
|
Assert(!image.isNull());
|
||||||
} else {
|
} else {
|
||||||
img = img.scaled(w, h, Qt::IgnoreAspectRatio, (options & Images::Option::Smooth) ? Qt::SmoothTransformation : Qt::FastTransformation);
|
image = image.scaled(
|
||||||
Assert(!img.isNull());
|
w,
|
||||||
|
h,
|
||||||
|
Qt::IgnoreAspectRatio,
|
||||||
|
((args.options & Images::Option::FastTransform)
|
||||||
|
? Qt::FastTransformation
|
||||||
|
: Qt::SmoothTransformation));
|
||||||
|
Assert(!image.isNull());
|
||||||
}
|
}
|
||||||
if (outerw > 0 && outerh > 0) {
|
auto outer = args.outer;
|
||||||
const auto pixelRatio = style::DevicePixelRatio();
|
if (!outer.isEmpty()) {
|
||||||
outerw *= pixelRatio;
|
const auto ratio = style::DevicePixelRatio();
|
||||||
outerh *= pixelRatio;
|
outer *= ratio;
|
||||||
if (outerw != w || outerh != h) {
|
if (outer != QSize(w, h)) {
|
||||||
img.setDevicePixelRatio(pixelRatio);
|
image.setDevicePixelRatio(ratio);
|
||||||
auto result = QImage(outerw, outerh, QImage::Format_ARGB32_Premultiplied);
|
auto result = QImage(outer, QImage::Format_ARGB32_Premultiplied);
|
||||||
result.setDevicePixelRatio(pixelRatio);
|
result.setDevicePixelRatio(ratio);
|
||||||
if (options & Images::Option::TransparentBackground) {
|
if (args.options & Images::Option::TransparentBackground) {
|
||||||
result.fill(Qt::transparent);
|
result.fill(Qt::transparent);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
QPainter p(&result);
|
QPainter p(&result);
|
||||||
if (!(options & Images::Option::TransparentBackground)) {
|
if (!(args.options & Images::Option::TransparentBackground)) {
|
||||||
if (w < outerw || h < outerh) {
|
if (w < outer.width() || h < outer.height()) {
|
||||||
p.fillRect(0, 0, result.width(), result.height(), st::imageBg);
|
p.fillRect(
|
||||||
|
QRect({}, result.size() / ratio),
|
||||||
|
Qt::black);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
p.drawImage((result.width() - img.width()) / (2 * pixelRatio), (result.height() - img.height()) / (2 * pixelRatio), img);
|
p.drawImage(
|
||||||
|
(result.width() - image.width()) / (2 * ratio),
|
||||||
|
(result.height() - image.height()) / (2 * ratio),
|
||||||
|
image);
|
||||||
}
|
}
|
||||||
img = result;
|
image = std::move(result);
|
||||||
Assert(!img.isNull());
|
Assert(!image.isNull());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
auto corners = [](Images::Options options) {
|
|
||||||
return ((options & Images::Option::RoundedTopLeft) ? RectPart::TopLeft : RectPart::None)
|
if (args.options
|
||||||
| ((options & Images::Option::RoundedTopRight) ? RectPart::TopRight : RectPart::None)
|
& (Option::RoundCircle | Option::RoundLarge | Option::RoundSmall)) {
|
||||||
| ((options & Images::Option::RoundedBottomLeft) ? RectPart::BottomLeft : RectPart::None)
|
image = Round(std::move(image), args.options);
|
||||||
| ((options & Images::Option::RoundedBottomRight) ? RectPart::BottomRight : RectPart::None);
|
Assert(!image.isNull());
|
||||||
};
|
|
||||||
if (options & Images::Option::Circled) {
|
|
||||||
prepareCircle(img);
|
|
||||||
Assert(!img.isNull());
|
|
||||||
} else if (options & Images::Option::RoundedLarge) {
|
|
||||||
prepareRound(img, ImageRoundRadius::Large, corners(options));
|
|
||||||
Assert(!img.isNull());
|
|
||||||
} else if (options & Images::Option::RoundedSmall) {
|
|
||||||
prepareRound(img, ImageRoundRadius::Small, corners(options));
|
|
||||||
Assert(!img.isNull());
|
|
||||||
}
|
}
|
||||||
if (options & Images::Option::Colored) {
|
if (args.colored) {
|
||||||
Assert(colored != nullptr);
|
image = Colored(std::move(image), *args.colored);
|
||||||
img = prepareColored(*colored, std::move(img));
|
|
||||||
}
|
}
|
||||||
img.setDevicePixelRatio(style::DevicePixelRatio());
|
image.setDevicePixelRatio(style::DevicePixelRatio());
|
||||||
return img;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Images
|
} // namespace Images
|
||||||
|
|
|
||||||
|
|
@ -26,8 +26,8 @@ enum class ImageRoundRadius {
|
||||||
namespace Images {
|
namespace Images {
|
||||||
|
|
||||||
[[nodiscard]] QPixmap PixmapFast(QImage &&image);
|
[[nodiscard]] QPixmap PixmapFast(QImage &&image);
|
||||||
[[nodiscard]] QImage BlurLargeImage(QImage image, int radius);
|
[[nodiscard]] QImage BlurLargeImage(QImage &&image, int radius);
|
||||||
[[nodiscard]] QImage DitherImage(QImage image);
|
[[nodiscard]] QImage DitherImage(const QImage &image);
|
||||||
|
|
||||||
[[nodiscard]] QImage GenerateGradient(
|
[[nodiscard]] QImage GenerateGradient(
|
||||||
QSize size,
|
QSize size,
|
||||||
|
|
@ -79,44 +79,78 @@ struct ReadResult {
|
||||||
};
|
};
|
||||||
[[nodiscard]] ReadResult Read(ReadArgs &&args);
|
[[nodiscard]] ReadResult Read(ReadArgs &&args);
|
||||||
|
|
||||||
QImage prepareBlur(QImage image);
|
|
||||||
void prepareRound(
|
|
||||||
QImage &image,
|
|
||||||
ImageRoundRadius radius,
|
|
||||||
RectParts corners = RectPart::AllCorners,
|
|
||||||
QRect target = QRect());
|
|
||||||
void prepareRound(
|
|
||||||
QImage &image,
|
|
||||||
gsl::span<const QImage, 4> cornerMasks,
|
|
||||||
RectParts corners = RectPart::AllCorners,
|
|
||||||
QRect target = QRect());
|
|
||||||
void prepareCircle(QImage &image);
|
|
||||||
QImage prepareColored(style::color add, QImage image);
|
|
||||||
QImage prepareColored(QColor add, QImage image);
|
|
||||||
QImage prepareOpaque(QImage image);
|
|
||||||
|
|
||||||
enum class Option {
|
enum class Option {
|
||||||
None = 0,
|
None = 0,
|
||||||
Smooth = (1 << 0),
|
FastTransform = (1 << 0),
|
||||||
Blurred = (1 << 1),
|
Blur = (1 << 1),
|
||||||
Circled = (1 << 2),
|
RoundCircle = (1 << 2),
|
||||||
RoundedLarge = (1 << 3),
|
RoundLarge = (1 << 3),
|
||||||
RoundedSmall = (1 << 4),
|
RoundSmall = (1 << 4),
|
||||||
RoundedTopLeft = (1 << 5),
|
RoundSkipTopLeft = (1 << 5),
|
||||||
RoundedTopRight = (1 << 6),
|
RoundSkipTopRight = (1 << 6),
|
||||||
RoundedBottomLeft = (1 << 7),
|
RoundSkipBottomLeft = (1 << 7),
|
||||||
RoundedBottomRight = (1 << 8),
|
RoundSkipBottomRight = (1 << 8),
|
||||||
RoundedAll = (None
|
Colorize = (1 << 9),
|
||||||
| RoundedTopLeft
|
|
||||||
| RoundedTopRight
|
|
||||||
| RoundedBottomLeft
|
|
||||||
| RoundedBottomRight),
|
|
||||||
Colored = (1 << 9),
|
|
||||||
TransparentBackground = (1 << 10),
|
TransparentBackground = (1 << 10),
|
||||||
};
|
};
|
||||||
using Options = base::flags<Option>;
|
using Options = base::flags<Option>;
|
||||||
inline constexpr auto is_flag_type(Option) { return true; };
|
inline constexpr auto is_flag_type(Option) { return true; };
|
||||||
|
|
||||||
QImage prepare(QImage img, int w, int h, Options options, int outerw, int outerh, const style::color *colored = nullptr);
|
[[nodiscard]] Options RoundOptions(
|
||||||
|
ImageRoundRadius radius,
|
||||||
|
RectParts corners = RectPart::AllCorners);
|
||||||
|
|
||||||
|
[[nodiscard]] QImage Blur(QImage &&image);
|
||||||
|
[[nodiscard]] QImage Round(
|
||||||
|
QImage &&image,
|
||||||
|
ImageRoundRadius radius,
|
||||||
|
RectParts corners = RectPart::AllCorners,
|
||||||
|
QRect target = QRect());
|
||||||
|
[[nodiscard]] QImage Round(
|
||||||
|
QImage &&image,
|
||||||
|
gsl::span<const QImage, 4> cornerMasks,
|
||||||
|
RectParts corners = RectPart::AllCorners,
|
||||||
|
QRect target = QRect());
|
||||||
|
[[nodiscard]] QImage Round(
|
||||||
|
QImage &&image,
|
||||||
|
Options options,
|
||||||
|
QRect target = QRect());
|
||||||
|
|
||||||
|
[[nodiscard]] QImage Circle(QImage &&image, QRect target = QRect());
|
||||||
|
[[nodiscard]] QImage Colored(QImage &&image, style::color add);
|
||||||
|
[[nodiscard]] QImage Colored(QImage &&image, QColor add);
|
||||||
|
[[nodiscard]] QImage Opaque(QImage &&image);
|
||||||
|
|
||||||
|
struct PrepareArgs {
|
||||||
|
const style::color *colored = nullptr;
|
||||||
|
Options options;
|
||||||
|
QSize outer;
|
||||||
|
|
||||||
|
[[nodiscard]] PrepareArgs blurred() const {
|
||||||
|
auto result = *this;
|
||||||
|
result.options |= Option::Blur;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
[[nodiscard]] QImage Prepare(
|
||||||
|
QImage image,
|
||||||
|
int w,
|
||||||
|
int h,
|
||||||
|
const PrepareArgs &args);
|
||||||
|
|
||||||
|
[[nodiscard]] inline QImage Prepare(
|
||||||
|
QImage image,
|
||||||
|
int w,
|
||||||
|
const PrepareArgs &args) {
|
||||||
|
return Prepare(std::move(image), w, 0, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] inline QImage Prepare(
|
||||||
|
QImage image,
|
||||||
|
QSize size,
|
||||||
|
const PrepareArgs &args) {
|
||||||
|
return Prepare(std::move(image), size.width(), size.height(), args);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace Images
|
} // namespace Images
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue