forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradient.cpp
262 lines (229 loc) · 8.24 KB
/
gradient.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Aseprite Render Library
// Copyright (c) 2019-2020 Igara Studio S.A.
// Copyright (c) 2017 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "render/gradient.h"
#include "base/vector2d.h"
#include "doc/image.h"
#include "doc/image_impl.h"
#include "render/dithering_matrix.h"
namespace render {
void render_rgba_gradient(doc::Image* img,
const gfx::Point imgPos,
const gfx::Point p0,
const gfx::Point p1,
doc::color_t c0,
doc::color_t c1,
const render::DitheringMatrix& matrix,
const GradientType type)
{
switch (type) {
case GradientType::Linear:
render_rgba_linear_gradient(img, imgPos, p0, p1, c0, c1, matrix);
break;
case GradientType::Radial:
render_rgba_radial_gradient(img, imgPos, p0, p1, c0, c1, matrix);
break;
}
}
void render_rgba_linear_gradient(doc::Image* img,
const gfx::Point imgPos,
const gfx::Point p0,
const gfx::Point p1,
doc::color_t c0,
doc::color_t c1,
const render::DitheringMatrix& matrix)
{
ASSERT(img->pixelFormat() == doc::IMAGE_RGB);
if (img->pixelFormat() != doc::IMAGE_RGB) {
return;
}
// If there is no vector defining the gradient (just one point),
// the "gradient" will be just "c0"
if (p0 == p1) {
img->clear(c0);
return;
}
base::Vector2d<double> u(p0.x, p0.y), v(p1.x, p1.y), w;
w = v - u;
const double wmag = w.magnitude();
w = w.normalize();
// As we use non-premultiplied RGB values, we need correct RGB
// values on each stop. So in case that one color has alpha=0
// (complete transparent), use the RGB values of the
// non-transparent color in the other stop point.
if (doc::rgba_geta(c0) == 0 && doc::rgba_geta(c1) != 0) {
c0 = (c1 & doc::rgba_rgb_mask);
}
else if (doc::rgba_geta(c0) != 0 && doc::rgba_geta(c1) == 0) {
c1 = (c0 & doc::rgba_rgb_mask);
}
const uint8_t r0 = doc::rgba_getr(c0);
const uint8_t g0 = doc::rgba_getg(c0);
const uint8_t b0 = doc::rgba_getb(c0);
const uint8_t a0 = doc::rgba_geta(c0);
const uint8_t r1 = doc::rgba_getr(c1);
const uint8_t g1 = doc::rgba_getg(c1);
const uint8_t b1 = doc::rgba_getb(c1);
const uint8_t a1 = doc::rgba_geta(c1);
doc::LockImageBits<doc::RgbTraits> bits(img);
auto it = bits.begin();
const int width = img->width();
const int height = img->height();
if (matrix.rows() == 1 && matrix.cols() == 1) {
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x, ++it) {
base::Vector2d<double> q(imgPos.x + x, imgPos.y + y);
q -= u;
double f = (q * w) / wmag;
doc::color_t c;
if (f < 0.0)
c = c0;
else if (f > 1.0)
c = c1;
else {
c = doc::rgba(int(r0 + f * (r1 - r0) + 1e-7),
int(g0 + f * (g1 - g0) + 1e-7),
int(b0 + f * (b1 - b0) + 1e-7),
int(a0 + f * (a1 - a0) + 1e-7));
}
*it = c;
}
}
}
else {
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x, ++it) {
base::Vector2d<double> q(imgPos.x + x, imgPos.y + y);
q -= u;
double f = (q * w) / wmag;
*it = (f * (matrix.maxValue() + 2) < matrix(y, x) + 1 ? c0 : c1);
}
}
}
}
void render_rgba_radial_gradient(doc::Image* img,
const gfx::Point imgPos,
const gfx::Point p0,
const gfx::Point p1,
doc::color_t c0,
doc::color_t c1,
const render::DitheringMatrix& matrix)
{
ASSERT(img->pixelFormat() == doc::IMAGE_RGB);
if (img->pixelFormat() != doc::IMAGE_RGB) {
return;
}
base::Vector2d<double> u(p0.x, p0.y), v(p1.x, p1.y), w;
w = (v - u) / 2;
// If there is no vector defining the gradient (just one point),
// the "gradient" will be just a solid color ("c1")
if (std::fabs(w.x) <= 0.000001 || std::fabs(w.y) <= 0.000001) {
img->clear(c1);
return;
}
// As we use non-premultiplied RGB values, we need correct RGB
// values on each stop. So in case that one color has alpha=0
// (complete transparent), use the RGB values of the
// non-transparent color in the other stop point.
if (doc::rgba_geta(c0) == 0 && doc::rgba_geta(c1) != 0) {
c0 = (c1 & doc::rgba_rgb_mask);
}
else if (doc::rgba_geta(c0) != 0 && doc::rgba_geta(c1) == 0) {
c1 = (c0 & doc::rgba_rgb_mask);
}
const uint8_t r0 = doc::rgba_getr(c0);
const uint8_t g0 = doc::rgba_getg(c0);
const uint8_t b0 = doc::rgba_getb(c0);
const uint8_t a0 = doc::rgba_geta(c0);
const uint8_t r1 = doc::rgba_getr(c1);
const uint8_t g1 = doc::rgba_getg(c1);
const uint8_t b1 = doc::rgba_getb(c1);
const uint8_t a1 = doc::rgba_geta(c1);
doc::LockImageBits<doc::RgbTraits> bits(img);
auto it = bits.begin();
const int width = img->width();
const int height = img->height();
if (matrix.rows() == 1 && matrix.cols() == 1) {
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x, ++it) {
base::Vector2d<double> q(imgPos.x + x, imgPos.y + y);
q -= (u + v) / 2;
q.x /= std::fabs(w.x);
q.y /= std::fabs(w.y);
double f = std::sqrt(q.x * q.x + q.y * q.y);
doc::color_t c;
if (f < 0.0)
c = c0;
else if (f > 1.0)
c = c1;
else {
c = doc::rgba(int(r0 + f * (r1 - r0) + 1e-7),
int(g0 + f * (g1 - g0) + 1e-7),
int(b0 + f * (b1 - b0) + 1e-7),
int(a0 + f * (a1 - a0) + 1e-7));
}
*it = c;
}
}
}
else {
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x, ++it) {
base::Vector2d<double> q(imgPos.x + x, imgPos.y + y);
q -= (u + v) / 2;
q.x /= std::fabs(w.x);
q.y /= std::fabs(w.y);
double f = std::sqrt(q.x * q.x + q.y * q.y);
*it = (f * (matrix.maxValue() + 2) < matrix(y, x) + 1 ? c0 : c1);
}
}
}
}
template<typename ImageTraits>
static void create_dithering_pattern_templ(doc::Image* pattern,
const render::DitheringMatrix& matrix,
const float f,
const doc::color_t c0,
const doc::color_t c1)
{
const int w = pattern->width();
const int h = pattern->height();
doc::LockImageBits<ImageTraits> dstBits(pattern);
auto dst = dstBits.begin();
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x, ++dst)
*dst = (f * (matrix.maxValue() + 2) < matrix(y, x) + 1 ? c0 : c1);
}
}
void convert_bitmap_brush_to_dithering_brush(doc::Brush* brush,
const doc::PixelFormat pixelFormat,
const render::DitheringMatrix& matrix,
const float f,
const doc::color_t c0,
const doc::color_t c1)
{
// Create a pattern
doc::ImageRef pattern(doc::Image::create(pixelFormat, matrix.cols(), matrix.rows()));
switch (pixelFormat) {
case doc::IMAGE_RGB:
create_dithering_pattern_templ<doc::RgbTraits>(pattern.get(), matrix, f, c0, c1);
break;
case doc::IMAGE_GRAYSCALE:
create_dithering_pattern_templ<doc::GrayscaleTraits>(pattern.get(), matrix, f, c0, c1);
break;
case doc::IMAGE_INDEXED:
create_dithering_pattern_templ<doc::IndexedTraits>(pattern.get(), matrix, f, c0, c1);
break;
}
doc::ImageRef copy(doc::Image::createCopy(brush->image()));
brush->setImage(copy.get(), copy.get());
brush->setPatternImage(pattern);
brush->setPattern(doc::BrushPattern::PAINT_BRUSH);
}
} // namespace render