-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraphictools.cpp
204 lines (181 loc) · 7.16 KB
/
graphictools.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "graphictools.h"
#include <QGraphicsBlurEffect>
#include <QGraphicsEffect>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <QPainter>
QImage applyEffectToImage(QImage src, QGraphicsEffect *effect, int extent)
{
if(src.isNull()) return QImage();
if(!effect) return src;
QGraphicsScene scene;
QGraphicsPixmapItem item;
item.setPixmap(QPixmap::fromImage(src));
item.setGraphicsEffect(effect);
scene.addItem(&item);
QImage res(src.size()+QSize(extent*2, extent*2), QImage::Format_ARGB32);
res.fill(Qt::transparent);
QPainter ptr(&res);
scene.render(&ptr, QRectF(), QRectF( -extent, -extent, src.width()+extent*2, src.height()+extent*2 ) );
return res;
}
void rotateImage(QImage &image, double angle)
{
QPoint center = image.rect().center();
QMatrix matrix;
matrix.translate(center.x(), center.y());
matrix.rotate(angle);
image = image.transformed(matrix);
}
QImage transformAlpha(QImage input, Qt::Edge edge)
{
// Rotate to let the algo be good with the edge we want
switch (edge) {
case Qt::Edge::BottomEdge: break;
case Qt::Edge::RightEdge: rotateImage(input, 90); break;
case Qt::Edge::TopEdge: rotateImage(input, 180); break;
case Qt::Edge::LeftEdge: rotateImage(input, -90); break;
}
int x_len = 0, y_len = 0, useAsHeight = input.height(), useAsWidth = input.width();
// Alpha gradient THE transform
while(x_len<useAsWidth) {
y_len = 0;
while(y_len<useAsHeight) {
QRgb pixel = input.pixel(x_len, y_len);
input.setPixel(x_len, y_len, qRgba(qRed(pixel), qGreen(pixel), qBlue(pixel), int((255 * (float(y_len))/float(useAsHeight))) ) );
y_len++;
}
x_len++;
}
//Switch back to the right rotation
switch (edge) {
case Qt::Edge::BottomEdge: break;
case Qt::Edge::RightEdge: rotateImage(input, -90); break;
case Qt::Edge::TopEdge: rotateImage(input, -180); break;
case Qt::Edge::LeftEdge: rotateImage(input, 90); break;
}
return input;
}
//Red excludes green
//Blue exludes red
//Green excludes blue
//When using inclusion it is the opposite
QRgb pixelBlender(QRgb src, Dtk::Addons::VisualEffect::ChannelSplitMode mode, int channel)
{
QRgb output = 0;
switch (mode) {
case Dtk::Addons::VisualEffect::ChannelSplitMode::Exclude: {
output = src;
if ((Dtk::Addons::VisualEffect::ColorChannel::Red & channel) == Dtk::Addons::VisualEffect::Red) {
output = QRgb(qRgba(qRed(0), qGreen(output), qBlue(output), qAlpha(output)));
} if ((Dtk::Addons::VisualEffect::ColorChannel::Green & channel) == Dtk::Addons::VisualEffect::Green) {
output = QRgb(qRgba(qRed(output), qGreen(0), qBlue(output), qAlpha(output)));
} if ((Dtk::Addons::VisualEffect::ColorChannel::Blue & channel) == Dtk::Addons::VisualEffect::Blue) {
output = QRgb(qRgba(qRed(output), qGreen(output), qBlue(0), qAlpha(output)));
} if ((Dtk::Addons::VisualEffect::ColorChannel::Alpha & channel) == Dtk::Addons::VisualEffect::Alpha) {
output = QRgb(qRgba(qRed(output), qGreen(output), qBlue(output), qAlpha(0)));
}
break;
}
case Dtk::Addons::VisualEffect::ChannelSplitMode::Include: {
output = qRgba(0,0,0,0);
if ((Dtk::Addons::VisualEffect::ColorChannel::Red & channel) == Dtk::Addons::VisualEffect::Red) {
output = QRgb(qRgba(qRed(src), qGreen(output), qBlue(output), qAlpha(output)));
} if ((Dtk::Addons::VisualEffect::ColorChannel::Green & channel) == Dtk::Addons::VisualEffect::Green) {
output = QRgb(qRgba(qRed(output), qGreen(src), qBlue(output), qAlpha(output)));
} if ((Dtk::Addons::VisualEffect::ColorChannel::Blue & channel) == Dtk::Addons::VisualEffect::Blue) {
output = QRgb(qRgba(qRed(output), qGreen(output), qBlue(src), qAlpha(output)));
} if ((Dtk::Addons::VisualEffect::ColorChannel::Alpha & channel) == Dtk::Addons::VisualEffect::Alpha) {
output = QRgb(qRgba(qRed(output), qGreen(output), qBlue(output), qAlpha(src)));
}
break;
}
}
return output;
}
QImage blendChannel(QImage src, int channel, Dtk::Addons::VisualEffect::ChannelSplitMode mode, int radius)
{
int x_len = 0, y_len = 0, useAsHeight = src.height(), useAsWidth = src.width();
QImage output = src;
QGraphicsBlurEffect *blur = new QGraphicsBlurEffect;
blur->setBlurRadius(radius);
output = applyEffectToImage(src, blur);
while(x_len<useAsWidth) {
y_len = 0;
while(y_len<useAsHeight) {
output.setPixel(x_len, y_len, pixelBlender(output.pixel(x_len, y_len), mode, channel));
y_len++;
}
x_len++;
}
return output;
}
QImage splitChannel(QImage src, int channel, Dtk::Addons::VisualEffect::ChannelSplitMode mode, Dtk::Addons::VisualEffect::ChannelSplitDirection direction, int padding)
{
int x_len = 0, y_len = 0, useAsHeight = src.height(), useAsWidth = src.width();
QImage output = src;
int pad = padding;
bool hor = true;
switch (direction) {
case Dtk::Addons::VisualEffect::ChannelSplitDirection::Top: hor = false; break;
case Dtk::Addons::VisualEffect::ChannelSplitDirection::Bottom: hor = false; pad = -padding; break;
case Dtk::Addons::VisualEffect::ChannelSplitDirection::Left: pad = -padding; break;
case Dtk::Addons::VisualEffect::ChannelSplitDirection::Right: break;
}
while(x_len<useAsWidth) {
y_len = 0;
while(y_len<useAsHeight) {
QRgb pix = 0;
bool skip = false;
if (hor != true) {
if ((y_len+pad) > -1 && (y_len + pad) < useAsHeight) {
pix = src.pixel(x_len, y_len + pad);
} else {
skip = true;
}
} else {
if ((x_len+pad) > -1 && (x_len + pad) < useAsWidth) {
pix = src.pixel(x_len + pad, y_len);
} else {
skip = true;
}
}
if (skip == false) {
output.setPixel(x_len, y_len, pixelBlender(pix, mode, channel));
}
y_len++;
}
x_len++;
}
return output;
}
QImage bindToGS(QImage src)
{
int x_len = 0, y_len = 0, useAsHeight = src.height(), useAsWidth = src.width();
while(x_len<useAsWidth) {
y_len = 0;
while(y_len<useAsHeight) {
QRgb pix = src.pixel(x_len, y_len);
int flatten = (int(qRed(pix)) + int(qGreen(pix)) + int(qBlue(pix))) / 3;
src.setPixel(x_len, y_len, QRgb(qRgba(flatten, flatten, flatten, 255)));
y_len++;
}
x_len++;
}
return src;
}
QImage bindToGSA(QImage src)
{
int x_len = 0, y_len = 0, useAsHeight = src.height(), useAsWidth = src.width();
while(x_len<useAsWidth) {
y_len = 0;
while(y_len<useAsHeight) {
QRgb pix = src.pixel(x_len, y_len);
int flatten = (int(qRed(pix)) + int(qGreen(pix)) + int(qBlue(pix))) / 3;
src.setPixel(x_len, y_len, QRgb(qRgba(flatten, flatten, flatten, qAlpha(pix))));
y_len++;
}
x_len++;
}
return src;
}