1 // This file is part of Desktop App Toolkit,
2 // a set of libraries for developing nice desktop applications.
3 //
4 // For license and copyright information please follow this link:
5 // https://github.com/desktop-app/legal/blob/master/LEGAL
6 //
7 #pragma once
8 
9 #include "base/flags.h"
10 #include "ui/rect_part.h"
11 #include "ui/style/style_core.h"
12 
13 namespace Storage {
14 namespace Cache {
15 struct Key;
16 } // namespace Cache
17 } // namespace Storage
18 
19 enum class ImageRoundRadius {
20 	None,
21 	Large,
22 	Small,
23 	Ellipse,
24 };
25 
26 namespace Images {
27 
28 [[nodiscard]] QPixmap PixmapFast(QImage &&image);
29 [[nodiscard]] QImage BlurLargeImage(QImage image, int radius);
30 [[nodiscard]] QImage DitherImage(QImage image);
31 
32 [[nodiscard]] QImage GenerateGradient(
33 	QSize size,
34 	const std::vector<QColor> &colors, // colors.size() <= 4.
35 	int rotation = 0,
36 	float progress = 1.f);
37 
38 [[nodiscard]] QImage GenerateLinearGradient(
39 	QSize size,
40 	const std::vector<QColor> &colors,
41 	int rotation = 0);
42 
43 [[nodiscard]] QImage GenerateShadow(
44 	int height,
45 	int topAlpha,
46 	int bottomAlpha,
47 	QColor color = QColor(0, 0, 0));
48 
49 [[nodiscard]] const std::array<QImage, 4> &CornersMask(
50 	ImageRoundRadius radius);
51 [[nodiscard]] std::array<QImage, 4> PrepareCorners(
52 	ImageRoundRadius radius,
53 	const style::color &color);
54 
55 [[nodiscard]] std::array<QImage, 4> CornersMask(int radius);
56 [[nodiscard]] std::array<QImage, 4> PrepareCorners(
57 	int radius,
58 	const style::color &color);
59 
60 [[nodiscard]] QByteArray UnpackGzip(const QByteArray &bytes);
61 
62 // Try to read images up to 64MB.
63 inline constexpr auto kReadBytesLimit = 64 * 1024 * 1024;
64 inline constexpr auto kReadMaxArea = 12'032 * 9'024;
65 
66 struct ReadArgs {
67 	QString path;
68 	QByteArray content;
69 	QSize maxSize;
70 	bool gzipSvg = false;
71 	bool forceOpaque = false;
72 	bool returnContent = false;
73 };
74 struct ReadResult {
75 	QImage image;
76 	QByteArray content;
77 	QByteArray format;
78 	bool animated = false;
79 };
80 [[nodiscard]] ReadResult Read(ReadArgs &&args);
81 
82 QImage prepareBlur(QImage image);
83 void prepareRound(
84 	QImage &image,
85 	ImageRoundRadius radius,
86 	RectParts corners = RectPart::AllCorners,
87 	QRect target = QRect());
88 void prepareRound(
89 	QImage &image,
90 	gsl::span<const QImage, 4> cornerMasks,
91 	RectParts corners = RectPart::AllCorners,
92 	QRect target = QRect());
93 void prepareCircle(QImage &image);
94 QImage prepareColored(style::color add, QImage image);
95 QImage prepareColored(QColor add, QImage image);
96 QImage prepareOpaque(QImage image);
97 
98 enum class Option {
99 	None                  = 0,
100 	Smooth                = (1 << 0),
101 	Blurred               = (1 << 1),
102 	Circled               = (1 << 2),
103 	RoundedLarge          = (1 << 3),
104 	RoundedSmall          = (1 << 4),
105 	RoundedTopLeft        = (1 << 5),
106 	RoundedTopRight       = (1 << 6),
107 	RoundedBottomLeft     = (1 << 7),
108 	RoundedBottomRight    = (1 << 8),
109 	RoundedAll            = (None
110 		| RoundedTopLeft
111 		| RoundedTopRight
112 		| RoundedBottomLeft
113 		| RoundedBottomRight),
114 	Colored               = (1 << 9),
115 	TransparentBackground = (1 << 10),
116 };
117 using Options = base::flags<Option>;
is_flag_type(Option)118 inline constexpr auto is_flag_type(Option) { return true; };
119 
120 QImage prepare(QImage img, int w, int h, Options options, int outerw, int outerh, const style::color *colored = nullptr);
121 
122 } // namespace Images
123