Blender V5.3
math_color_blend_inline.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_math_base_c.hh"
10
11#if !BLI_MATH_DO_INLINE
13#endif
14
15#include "BLI_math_color_c.hh"
16#include "BLI_math_vector_c.hh"
17
18namespace blender {
19
20#ifndef __MATH_COLOR_BLEND_INLINE_C__
21# define __MATH_COLOR_BLEND_INLINE_C__
22
23/* don't add any saturation to a completely black and white image */
24# define EPS_SATURATION 0.0005f
25# define EPS_ALPHA 0.0005f
26
27/* -------------------------------------------------------------------- */
37
38/* straight alpha byte blending modes */
39
40MINLINE void blend_color_mix_byte(uchar dst[4], const uchar src1[4], const uchar src2[4])
41{
42 if (src2[3] != 0) {
43 /* straight over operation */
44 const int t = src2[3];
45 const int mt = 255 - t;
46 int tmp[4];
47
48 tmp[0] = (mt * src1[3] * src1[0]) + (t * 255 * src2[0]);
49 tmp[1] = (mt * src1[3] * src1[1]) + (t * 255 * src2[1]);
50 tmp[2] = (mt * src1[3] * src1[2]) + (t * 255 * src2[2]);
51 tmp[3] = (mt * src1[3]) + (t * 255);
52
53 dst[0] = uchar(divide_round_i(tmp[0], tmp[3]));
54 dst[1] = uchar(divide_round_i(tmp[1], tmp[3]));
55 dst[2] = uchar(divide_round_i(tmp[2], tmp[3]));
56 dst[3] = uchar(divide_round_i(tmp[3], 255));
57 }
58 else {
59 /* no op */
60 copy_v4_v4_uchar(dst, src1);
61 }
62}
63
64MINLINE void blend_color_add_byte(uchar dst[4], const uchar src1[4], const uchar src2[4])
65{
66 if (src2[3] != 0) {
67 /* straight add operation */
68 const int t = src2[3];
69 int tmp[3];
70
71 tmp[0] = (src1[0] * 255) + (src2[0] * t);
72 tmp[1] = (src1[1] * 255) + (src2[1] * t);
73 tmp[2] = (src1[2] * 255) + (src2[2] * t);
74
75 dst[0] = uchar(min_ii(divide_round_i(tmp[0], 255), 255));
76 dst[1] = uchar(min_ii(divide_round_i(tmp[1], 255), 255));
77 dst[2] = uchar(min_ii(divide_round_i(tmp[2], 255), 255));
78 dst[3] = src1[3];
79 }
80 else {
81 /* no op */
82 copy_v4_v4_uchar(dst, src1);
83 }
84}
85
86MINLINE void blend_color_sub_byte(uchar dst[4], const uchar src1[4], const uchar src2[4])
87{
88 if (src2[3] != 0) {
89 /* straight sub operation */
90 const int t = src2[3];
91 int tmp[3];
92
93 tmp[0] = (src1[0] * 255) - (src2[0] * t);
94 tmp[1] = (src1[1] * 255) - (src2[1] * t);
95 tmp[2] = (src1[2] * 255) - (src2[2] * t);
96
97 dst[0] = uchar(max_ii(divide_round_i(tmp[0], 255), 0));
98 dst[1] = uchar(max_ii(divide_round_i(tmp[1], 255), 0));
99 dst[2] = uchar(max_ii(divide_round_i(tmp[2], 255), 0));
100 dst[3] = src1[3];
101 }
102 else {
103 /* no op */
104 copy_v4_v4_uchar(dst, src1);
105 }
106}
107
108MINLINE void blend_color_mul_byte(uchar dst[4], const uchar src1[4], const uchar src2[4])
109{
110 if (src2[3] != 0) {
111 /* straight multiply operation */
112 const int t = src2[3];
113 const int mt = 255 - t;
114 int tmp[3];
115
116 tmp[0] = (mt * src1[0] * 255) + (t * src1[0] * src2[0]);
117 tmp[1] = (mt * src1[1] * 255) + (t * src1[1] * src2[1]);
118 tmp[2] = (mt * src1[2] * 255) + (t * src1[2] * src2[2]);
119
120 dst[0] = uchar(divide_round_i(tmp[0], 255 * 255));
121 dst[1] = uchar(divide_round_i(tmp[1], 255 * 255));
122 dst[2] = uchar(divide_round_i(tmp[2], 255 * 255));
123 dst[3] = src1[3];
124 }
125 else {
126 /* no op */
127 copy_v4_v4_uchar(dst, src1);
128 }
129}
130
131MINLINE void blend_color_lighten_byte(uchar dst[4], const uchar src1[4], const uchar src2[4])
132{
133 if (src2[3] != 0) {
134 /* straight lighten operation */
135 const int t = src2[3];
136 const int mt = 255 - t;
137 int tmp[3];
138
139 tmp[0] = (mt * src1[0]) + (t * max_ii(src1[0], src2[0]));
140 tmp[1] = (mt * src1[1]) + (t * max_ii(src1[1], src2[1]));
141 tmp[2] = (mt * src1[2]) + (t * max_ii(src1[2], src2[2]));
142
143 dst[0] = uchar(divide_round_i(tmp[0], 255));
144 dst[1] = uchar(divide_round_i(tmp[1], 255));
145 dst[2] = uchar(divide_round_i(tmp[2], 255));
146 dst[3] = src1[3];
147 }
148 else {
149 /* no op */
150 copy_v4_v4_uchar(dst, src1);
151 }
152}
153
154MINLINE void blend_color_darken_byte(uchar dst[4], const uchar src1[4], const uchar src2[4])
155{
156 if (src2[3] != 0) {
157 /* straight darken operation */
158 const int t = src2[3];
159 const int mt = 255 - t;
160 int tmp[3];
161
162 tmp[0] = (mt * src1[0]) + (t * min_ii(src1[0], src2[0]));
163 tmp[1] = (mt * src1[1]) + (t * min_ii(src1[1], src2[1]));
164 tmp[2] = (mt * src1[2]) + (t * min_ii(src1[2], src2[2]));
165
166 dst[0] = uchar(divide_round_i(tmp[0], 255));
167 dst[1] = uchar(divide_round_i(tmp[1], 255));
168 dst[2] = uchar(divide_round_i(tmp[2], 255));
169 dst[3] = src1[3];
170 }
171 else {
172 /* no op */
173 copy_v4_v4_uchar(dst, src1);
174 }
175}
176
177MINLINE void blend_color_erase_alpha_byte(uchar dst[4], const uchar src1[4], const uchar src2[4])
178{
179 if (src2[3] != 0) {
180 /* straight so just modify alpha channel */
181 const int t = src2[3];
182
183 dst[0] = src1[0];
184 dst[1] = src1[1];
185 dst[2] = src1[2];
186 dst[3] = uchar(max_ii(src1[3] - divide_round_i(t * src2[3], 255), 0));
187 }
188 else {
189 /* no op */
190 copy_v4_v4_uchar(dst, src1);
191 }
192}
193
194MINLINE void blend_color_add_alpha_byte(uchar dst[4], const uchar src1[4], const uchar src2[4])
195{
196 if (src2[3] != 0) {
197 /* straight so just modify alpha channel */
198 const int t = src2[3];
199
200 dst[0] = src1[0];
201 dst[1] = src1[1];
202 dst[2] = src1[2];
203 dst[3] = uchar(min_ii(src1[3] + divide_round_i(t * src2[3], 255), 255));
204 }
205 else {
206 /* no op */
207 copy_v4_v4_uchar(dst, src1);
208 }
209}
210
211MINLINE void blend_color_overlay_byte(uchar dst[4], const uchar src1[4], const uchar src2[4])
212{
213 const int fac = int(src2[3]);
214 if (fac != 0) {
215 const int mfac = 255 - fac;
216 int i = 3;
217
218 while (i--) {
219 int temp;
220
221 if (src1[i] > 127) {
222 temp = 255 - ((255 - 2 * (src1[i] - 127)) * (255 - src2[i]) / 255);
223 }
224 else {
225 temp = (2 * src1[i] * src2[i]) >> 8;
226 }
227 dst[i] = uchar(min_ii((temp * fac + src1[i] * mfac) / 255, 255));
228 }
229 }
230 else {
231 /* no op */
232 copy_v4_v4_uchar(dst, src1);
233 }
234}
235
236MINLINE void blend_color_hardlight_byte(uchar dst[4], const uchar src1[4], const uchar src2[4])
237{
238 const int fac = int(src2[3]);
239 if (fac != 0) {
240 const int mfac = 255 - fac;
241 int i = 3;
242
243 while (i--) {
244 int temp;
245
246 if (src2[i] > 127) {
247 temp = 255 - ((255 - 2 * (src2[i] - 127)) * (255 - src1[i]) / 255);
248 }
249 else {
250 temp = (2 * src2[i] * src1[i]) >> 8;
251 }
252 dst[i] = uchar(min_ii((temp * fac + src1[i] * mfac) / 255, 255));
253 }
254 }
255 else {
256 /* no op */
257 copy_v4_v4_uchar(dst, src1);
258 }
259}
260
261MINLINE void blend_color_burn_byte(uchar dst[4], const uchar src1[4], const uchar src2[4])
262{
263 const int fac = src2[3];
264 if (fac != 0) {
265 const int mfac = 255 - fac;
266 int i = 3;
267
268 while (i--) {
269 const int temp = (src2[i] == 0) ? 0 : max_ii(255 - ((255 - src1[i]) * 255) / src2[i], 0);
270 dst[i] = uchar((temp * fac + src1[i] * mfac) / 255);
271 }
272 }
273 else {
274 /* no op */
275 copy_v4_v4_uchar(dst, src1);
276 }
277}
278
279MINLINE void blend_color_linearburn_byte(uchar dst[4], const uchar src1[4], const uchar src2[4])
280{
281 const int fac = src2[3];
282 if (fac != 0) {
283 const int mfac = 255 - fac;
284 int i = 3;
285
286 while (i--) {
287 const int temp = max_ii(src1[i] + src2[i] - 255, 0);
288 dst[i] = uchar((temp * fac + src1[i] * mfac) / 255);
289 }
290 }
291 else {
292 /* no op */
293 copy_v4_v4_uchar(dst, src1);
294 }
295}
296
297MINLINE void blend_color_dodge_byte(uchar dst[4], const uchar src1[4], const uchar src2[4])
298{
299 const int fac = src2[3];
300 if (fac != 0) {
301 const int mfac = 255 - fac;
302 int i = 3;
303
304 while (i--) {
305 const int temp = (src2[i] == 255) ? 255 : min_ii((src1[i] * 255) / (255 - src2[i]), 255);
306 dst[i] = uchar((temp * fac + src1[i] * mfac) / 255);
307 }
308 }
309 else {
310 /* no op */
311 copy_v4_v4_uchar(dst, src1);
312 }
313}
314
315MINLINE void blend_color_screen_byte(uchar dst[4], const uchar src1[4], const uchar src2[4])
316{
317 const int fac = src2[3];
318 if (fac != 0) {
319 const int mfac = 255 - fac;
320 int i = 3;
321
322 while (i--) {
323 const int temp = max_ii(255 - (((255 - src1[i]) * (255 - src2[i])) / 255), 0);
324 dst[i] = uchar((temp * fac + src1[i] * mfac) / 255);
325 }
326 }
327 else {
328 /* no op */
329 copy_v4_v4_uchar(dst, src1);
330 }
331}
332
333MINLINE void blend_color_softlight_byte(uchar dst[4], const uchar src1[4], const uchar src2[4])
334{
335 const float fac = float(src2[3]) / 255.0f;
336 if (fac != 0) {
337 const float mfac = 1.0f - fac;
338 int i = 3;
339
340 while (i--) {
341 /* Using "Pegtop" formula: dst = (1 - 2b) * a^2 + 2ab where a=bottom and b=top color.
342 * See https://en.wikipedia.org/wiki/Blend_modes */
343 const float src1val = float(src1[i]) / 255.0f;
344 const float src2val = float(src2[i]) / 255.0f;
345 float screen = 1.0f - (1.0f - src1val) * (1.0f - src2val);
346 float soft_light = ((1.0f - src1val) * src2val + screen) * src1val;
347 dst[i] = round_fl_to_uchar_clamp((src1val * mfac + soft_light * fac) * 255.0f);
348 }
349 }
350 else {
351 /* no op */
352 copy_v4_v4_uchar(dst, src1);
353 }
354}
355
356MINLINE void blend_color_pinlight_byte(uchar dst[4], const uchar src1[4], const uchar src2[4])
357{
358 const int fac = src2[3];
359 if (fac != 0) {
360 const int mfac = 255 - fac;
361 int i = 3;
362
363 while (i--) {
364 int temp;
365
366 if (src2[i] > 127) {
367 temp = max_ii(2 * (src2[i] - 127), src1[i]);
368 }
369 else {
370 temp = min_ii(2 * src2[i], src1[i]);
371 }
372 dst[i] = uchar((min_ii(temp, 255) * fac + src1[i] * mfac) / 255);
373 }
374 }
375 else {
376 /* no op */
377 copy_v4_v4_uchar(dst, src1);
378 }
379}
380
381MINLINE void blend_color_linearlight_byte(uchar dst[4], const uchar src1[4], const uchar src2[4])
382{
383 const int fac = src2[3];
384 if (fac != 0) {
385 const int mfac = 255 - fac;
386 int i = 3;
387
388 while (i--) {
389 int temp;
390
391 if (src2[i] > 127) {
392 temp = min_ii(src1[i] + 2 * (src2[i] - 127), 255);
393 }
394 else {
395 temp = max_ii(src1[i] + 2 * src2[i] - 255, 0);
396 }
397 dst[i] = uchar((temp * fac + src1[i] * mfac) / 255);
398 }
399 }
400 else {
401 /* no op */
402 copy_v4_v4_uchar(dst, src1);
403 }
404}
405
406MINLINE void blend_color_vividlight_byte(uchar dst[4], const uchar src1[4], const uchar src2[4])
407{
408 const int fac = src2[3];
409 if (fac != 0) {
410 const int mfac = 255 - fac;
411 int i = 3;
412
413 while (i--) {
414 int temp;
415
416 if (src2[i] == 255) {
417 temp = (src1[i] == 0) ? 127 : 255;
418 }
419 else if (src2[i] == 0) {
420 temp = (src1[i] == 255) ? 127 : 0;
421 }
422 else if (src2[i] > 127) {
423 temp = min_ii(((src1[i]) * 255) / (2 * (255 - src2[i])), 255);
424 }
425 else {
426 temp = max_ii(255 - ((255 - src1[i]) * 255 / (2 * src2[i])), 0);
427 }
428 dst[i] = uchar((temp * fac + src1[i] * mfac) / 255);
429 }
430 }
431 else {
432 /* no op */
433 copy_v4_v4_uchar(dst, src1);
434 }
435}
436
437MINLINE void blend_color_difference_byte(uchar dst[4], const uchar src1[4], const uchar src2[4])
438{
439 const int fac = src2[3];
440 if (fac != 0) {
441 const int mfac = 255 - fac;
442 int i = 3;
443
444 while (i--) {
445 const int temp = abs(src1[i] - src2[i]);
446 dst[i] = uchar((temp * fac + src1[i] * mfac) / 255);
447 }
448 }
449 else {
450 /* no op */
451 copy_v4_v4_uchar(dst, src1);
452 }
453}
454
455MINLINE void blend_color_exclusion_byte(uchar dst[4], const uchar src1[4], const uchar src2[4])
456{
457 const int fac = src2[3];
458 if (fac != 0) {
459 const int mfac = 255 - fac;
460 int i = 3;
461
462 while (i--) {
463 const int temp = 127 - min_ii(((2 * (src1[i] - 127) * (src2[i] - 127)) / 255), 127);
464 dst[i] = uchar((temp * fac + src1[i] * mfac) / 255);
465 }
466 }
467 else {
468 /* no op */
469 copy_v4_v4_uchar(dst, src1);
470 }
471}
472
473MINLINE void blend_color_color_byte(uchar dst[4], const uchar src1[4], const uchar src2[4])
474{
475 const int fac = src2[3];
476 if (fac != 0) {
477 const int mfac = 255 - fac;
478 float h1, s1, v1;
479 float h2, s2, v2;
480 float r, g, b;
481 rgb_to_hsv(src1[0] / 255.0f, src1[1] / 255.0f, src1[2] / 255.0f, &h1, &s1, &v1);
482 rgb_to_hsv(src2[0] / 255.0f, src2[1] / 255.0f, src2[2] / 255.0f, &h2, &s2, &v2);
483
484 h1 = h2;
485 s1 = s2;
486
487 hsv_to_rgb(h1, s1, v1, &r, &g, &b);
488
489 dst[0] = uchar((int(r * 255.0f) * fac + src1[0] * mfac) / 255);
490 dst[1] = uchar((int(g * 255.0f) * fac + src1[1] * mfac) / 255);
491 dst[2] = uchar((int(b * 255.0f) * fac + src1[2] * mfac) / 255);
492 }
493 else {
494 /* no op */
495 copy_v4_v4_uchar(dst, src1);
496 }
497}
498
499MINLINE void blend_color_hue_byte(uchar dst[4], const uchar src1[4], const uchar src2[4])
500{
501 const int fac = src2[3];
502 if (fac != 0) {
503 const int mfac = 255 - fac;
504 float h1, s1, v1;
505 float h2, s2, v2;
506 float r, g, b;
507 rgb_to_hsv(src1[0] / 255.0f, src1[1] / 255.0f, src1[2] / 255.0f, &h1, &s1, &v1);
508 rgb_to_hsv(src2[0] / 255.0f, src2[1] / 255.0f, src2[2] / 255.0f, &h2, &s2, &v2);
509
510 h1 = h2;
511
512 hsv_to_rgb(h1, s1, v1, &r, &g, &b);
513
514 dst[0] = uchar((int(r * 255.0f) * fac + src1[0] * mfac) / 255);
515 dst[1] = uchar((int(g * 255.0f) * fac + src1[1] * mfac) / 255);
516 dst[2] = uchar((int(b * 255.0f) * fac + src1[2] * mfac) / 255);
517 }
518 else {
519 /* no op */
520 copy_v4_v4_uchar(dst, src1);
521 }
522}
523
524MINLINE void blend_color_saturation_byte(uchar dst[4], const uchar src1[4], const uchar src2[4])
525{
526 const int fac = src2[3];
527 if (fac != 0) {
528 const int mfac = 255 - fac;
529 float h1, s1, v1;
530 float h2, s2, v2;
531 float r, g, b;
532 rgb_to_hsv(src1[0] / 255.0f, src1[1] / 255.0f, src1[2] / 255.0f, &h1, &s1, &v1);
533 rgb_to_hsv(src2[0] / 255.0f, src2[1] / 255.0f, src2[2] / 255.0f, &h2, &s2, &v2);
534
535 if (s1 > EPS_SATURATION) {
536 s1 = s2;
537 }
538
539 hsv_to_rgb(h1, s1, v1, &r, &g, &b);
540
541 dst[0] = uchar((int(r * 255.0f) * fac + src1[0] * mfac) / 255);
542 dst[1] = uchar((int(g * 255.0f) * fac + src1[1] * mfac) / 255);
543 dst[2] = uchar((int(b * 255.0f) * fac + src1[2] * mfac) / 255);
544 }
545 else {
546 /* no op */
547 copy_v4_v4_uchar(dst, src1);
548 }
549}
550
551MINLINE void blend_color_luminosity_byte(uchar dst[4], const uchar src1[4], const uchar src2[4])
552{
553 const int fac = src2[3];
554 if (fac != 0) {
555 const int mfac = 255 - fac;
556 float h1, s1, v1;
557 float h2, s2, v2;
558 float r, g, b;
559 rgb_to_hsv(src1[0] / 255.0f, src1[1] / 255.0f, src1[2] / 255.0f, &h1, &s1, &v1);
560 rgb_to_hsv(src2[0] / 255.0f, src2[1] / 255.0f, src2[2] / 255.0f, &h2, &s2, &v2);
561
562 v1 = v2;
563
564 hsv_to_rgb(h1, s1, v1, &r, &g, &b);
565
566 dst[0] = uchar((int(r * 255.0f) * fac + src1[0] * mfac) / 255);
567 dst[1] = uchar((int(g * 255.0f) * fac + src1[1] * mfac) / 255);
568 dst[2] = uchar((int(b * 255.0f) * fac + src1[2] * mfac) / 255);
569 }
570 else {
571 /* no op */
572 copy_v4_v4_uchar(dst, src1);
573 }
574}
575
577 const uchar src1[4],
578 const uchar src2[4],
579 float ft)
580{
581 /* do color interpolation, but in pre-multiplied space so that RGB colors
582 * from zero alpha regions have no influence */
583 const int t = int(255 * ft);
584 const int mt = 255 - t;
585 int tmp = (mt * src1[3] + t * src2[3]);
586
587 if (tmp > 0) {
588 dst[0] = uchar(divide_round_i(mt * src1[0] * src1[3] + t * src2[0] * src2[3], tmp));
589 dst[1] = uchar(divide_round_i(mt * src1[1] * src1[3] + t * src2[1] * src2[3], tmp));
590 dst[2] = uchar(divide_round_i(mt * src1[2] * src1[3] + t * src2[2] * src2[3], tmp));
591 dst[3] = uchar(divide_round_i(tmp, 255));
592 }
593 else {
594 copy_v4_v4_uchar(dst, src1);
595 dst[3] = 0;
596 }
597}
598
599/* pre-multiplied alpha float blending modes */
600
601MINLINE void blend_color_mix_float(float dst[4], const float src1[4], const float src2[4])
602{
603 if (src2[3] != 0.0f) {
604 /* Pre-multiply over operation. */
605 const float t = src2[3];
606 const float mt = 1.0f - t;
607
608 dst[0] = mt * src1[0] + src2[0];
609 dst[1] = mt * src1[1] + src2[1];
610 dst[2] = mt * src1[2] + src2[2];
611 dst[3] = mt * src1[3] + t;
612 }
613 else {
614 /* no op */
615 copy_v4_v4(dst, src1);
616 }
617}
618
619MINLINE void blend_color_add_float(float dst[4], const float src1[4], const float src2[4])
620{
621 if (src2[3] != 0.0f) {
622 /* un-pre-multiplied > add > pre-multiplied, simplified. */
623 dst[0] = src1[0] + src2[0] * src1[3];
624 dst[1] = src1[1] + src2[1] * src1[3];
625 dst[2] = src1[2] + src2[2] * src1[3];
626 dst[3] = src1[3];
627 }
628 else {
629 /* no op */
630 copy_v4_v4(dst, src1);
631 }
632}
633
634MINLINE void blend_color_sub_float(float dst[4], const float src1[4], const float src2[4])
635{
636 if (src2[3] != 0.0f) {
637 /* un-pre-multiplied > subtract > pre-multiplied, simplified. */
638 dst[0] = max_ff(src1[0] - src2[0] * src1[3], 0.0f);
639 dst[1] = max_ff(src1[1] - src2[1] * src1[3], 0.0f);
640 dst[2] = max_ff(src1[2] - src2[2] * src1[3], 0.0f);
641 dst[3] = src1[3];
642 }
643 else {
644 /* no op */
645 copy_v4_v4(dst, src1);
646 }
647}
648
649MINLINE void blend_color_mul_float(float dst[4], const float src1[4], const float src2[4])
650{
651 if (src2[3] != 0.0f) {
652 /* un-pre-multiplied > multiply > pre-multiplied, simplified. */
653 const float t = src2[3];
654 const float mt = 1.0f - t;
655
656 dst[0] = mt * src1[0] + src1[0] * src2[0] * src1[3];
657 dst[1] = mt * src1[1] + src1[1] * src2[1] * src1[3];
658 dst[2] = mt * src1[2] + src1[2] * src2[2] * src1[3];
659 dst[3] = src1[3];
660 }
661 else {
662 /* no op */
663 copy_v4_v4(dst, src1);
664 }
665}
666
667MINLINE void blend_color_lighten_float(float dst[4], const float src1[4], const float src2[4])
668{
669 if (src2[3] != 0.0f) {
670 /* remap src2 to have same alpha as src1 pre-multiplied, take maximum of
671 * src1 and src2, then blend it with src1 */
672 const float t = src2[3];
673 const float mt = 1.0f - t;
674 const float map_alpha = src1[3] / src2[3];
675
676 dst[0] = mt * src1[0] + t * max_ff(src1[0], src2[0] * map_alpha);
677 dst[1] = mt * src1[1] + t * max_ff(src1[1], src2[1] * map_alpha);
678 dst[2] = mt * src1[2] + t * max_ff(src1[2], src2[2] * map_alpha);
679 dst[3] = src1[3];
680 }
681 else {
682 /* no op */
683 copy_v4_v4(dst, src1);
684 }
685}
686
687MINLINE void blend_color_darken_float(float dst[4], const float src1[4], const float src2[4])
688{
689 if (src2[3] != 0.0f) {
690 /* remap src2 to have same alpha as src1 pre-multiplied, take minimum of
691 * src1 and src2, then blend it with src1 */
692 const float t = src2[3];
693 const float mt = 1.0f - t;
694 const float map_alpha = src1[3] / src2[3];
695
696 dst[0] = mt * src1[0] + t * min_ff(src1[0], src2[0] * map_alpha);
697 dst[1] = mt * src1[1] + t * min_ff(src1[1], src2[1] * map_alpha);
698 dst[2] = mt * src1[2] + t * min_ff(src1[2], src2[2] * map_alpha);
699 dst[3] = src1[3];
700 }
701 else {
702 /* no op */
703 copy_v4_v4(dst, src1);
704 }
705}
706
707MINLINE void blend_color_erase_alpha_float(float dst[4], const float src1[4], const float src2[4])
708{
709 if (src2[3] != 0.0f && src1[3] > 0.0f) {
710 /* subtract alpha and remap RGB channels to match */
711 float alpha = max_ff(src1[3] - src2[3], 0.0f);
712 float map_alpha;
713
714 if (alpha <= EPS_ALPHA) {
715 alpha = 0.0f;
716 }
717
718 map_alpha = alpha / src1[3];
719
720 dst[0] = src1[0] * map_alpha;
721 dst[1] = src1[1] * map_alpha;
722 dst[2] = src1[2] * map_alpha;
723 dst[3] = alpha;
724 }
725 else {
726 /* no op */
727 copy_v4_v4(dst, src1);
728 }
729}
730
731MINLINE void blend_color_add_alpha_float(float dst[4], const float src1[4], const float src2[4])
732{
733 if (src2[3] != 0.0f && src1[3] < 1.0f) {
734 /* add alpha and remap RGB channels to match */
735 float alpha = min_ff(src1[3] + src2[3], 1.0f);
736 float map_alpha;
737
738 if (alpha >= 1.0f - EPS_ALPHA) {
739 alpha = 1.0f;
740 }
741
742 map_alpha = (src1[3] > 0.0f) ? alpha / src1[3] : 1.0f;
743
744 dst[0] = src1[0] * map_alpha;
745 dst[1] = src1[1] * map_alpha;
746 dst[2] = src1[2] * map_alpha;
747 dst[3] = alpha;
748 }
749 else {
750 /* no op */
751 copy_v4_v4(dst, src1);
752 }
753}
754
755MINLINE void blend_color_overlay_float(float dst[4], const float src1[4], const float src2[4])
756{
757 const float fac = src2[3];
758 if (fac != 0.0f) {
759 const float mfac = 1.0f - fac;
760 int i = 3;
761
762 while (i--) {
763 float temp;
764
765 if (src1[i] > 0.5f) {
766 temp = 1.0f - (1.0f - 2.0f * (src1[i] - 0.5f)) * (1.0f - src2[i]);
767 }
768 else {
769 temp = 2.0f * src1[i] * src2[i];
770 }
771 dst[i] = min_ff(temp * fac + src1[i] * mfac, 1.0f);
772 }
773 }
774 else {
775 /* no op */
776 copy_v4_v4(dst, src1);
777 }
778}
779
780MINLINE void blend_color_hardlight_float(float dst[4], const float src1[4], const float src2[4])
781{
782 const float fac = src2[3];
783 if (fac != 0.0f) {
784 const float mfac = 1.0f - fac;
785 int i = 3;
786
787 while (i--) {
788 float temp;
789
790 if (src2[i] > 0.5f) {
791 temp = 1.0f - ((1.0f - 2.0f * (src2[i] - 0.5f)) * (1.0f - src1[i]));
792 }
793 else {
794 temp = 2.0f * src2[i] * src1[i];
795 }
796 dst[i] = min_ff((temp * fac + src1[i] * mfac) / 1.0f, 1.0f);
797 }
798 }
799 else {
800 /* no op */
801 copy_v4_v4(dst, src1);
802 }
803}
804
805MINLINE void blend_color_burn_float(float dst[4], const float src1[4], const float src2[4])
806{
807 const float fac = src2[3];
808 if (fac != 0.0f) {
809 const float mfac = 1.0f - fac;
810 int i = 3;
811
812 while (i--) {
813 const float temp = (src2[i] == 0.0f) ? 0.0f :
814 max_ff(1.0f - ((1.0f - src1[i]) / src2[i]), 0.0f);
815 dst[i] = (temp * fac + src1[i] * mfac);
816 }
817 }
818 else {
819 /* no op */
820 copy_v4_v4(dst, src1);
821 }
822}
823
824MINLINE void blend_color_linearburn_float(float dst[4], const float src1[4], const float src2[4])
825{
826 const float fac = src2[3];
827 if (fac != 0.0f) {
828 const float mfac = 1.0f - fac;
829 int i = 3;
830
831 while (i--) {
832 const float temp = max_ff(src1[i] + src2[i] - 1.0f, 0.0f);
833 dst[i] = (temp * fac + src1[i] * mfac);
834 }
835 }
836 else {
837 /* no op */
838 copy_v4_v4(dst, src1);
839 }
840}
841
842MINLINE void blend_color_dodge_float(float dst[4], const float src1[4], const float src2[4])
843{
844 const float fac = src2[3];
845 if (fac != 0.0f) {
846 const float mfac = 1.0f - fac;
847 int i = 3;
848
849 while (i--) {
850 const float temp = (src2[i] >= 1.0f) ? 1.0f : min_ff(src1[i] / (1.0f - src2[i]), 1.0f);
851 dst[i] = (temp * fac + src1[i] * mfac);
852 }
853 }
854 else {
855 /* no op */
856 copy_v4_v4(dst, src1);
857 }
858}
859
860MINLINE void blend_color_screen_float(float dst[4], const float src1[4], const float src2[4])
861{
862 const float fac = src2[3];
863 if (fac != 0.0f) {
864 const float mfac = 1.0f - fac;
865 int i = 3;
866
867 while (i--) {
868 const float temp = max_ff(1.0f - ((1.0f - src1[i]) * (1.0f - src2[i])), 0.0f);
869 dst[i] = (temp * fac + src1[i] * mfac);
870 }
871 }
872 else {
873 /* no op */
874 copy_v4_v4(dst, src1);
875 }
876}
877
878MINLINE void blend_color_softlight_float(float dst[4], const float src1[4], const float src2[4])
879{
880 const float fac = src2[3];
881 if (fac != 0.0f) {
882 const float mfac = 1.0f - fac;
883 int i = 3;
884
885 while (i--) {
886 float screen = 1.0f - (1.0f - src1[i]) * (1.0f - src2[i]);
887 float soft_light = ((1.0f - src1[i]) * src2[i] + screen) * src1[i];
888 dst[i] = src1[i] * mfac + soft_light * fac;
889 }
890 }
891 else {
892 /* no op */
893 copy_v4_v4(dst, src1);
894 }
895}
896
897MINLINE void blend_color_pinlight_float(float dst[4], const float src1[4], const float src2[4])
898{
899 const float fac = src2[3];
900 if (fac != 0.0f) {
901 const float mfac = 1.0f - fac;
902 int i = 3;
903
904 while (i--) {
905 float temp;
906
907 if (src2[i] > 0.5f) {
908 temp = max_ff(2.0f * (src2[i] - 0.5f), src1[i]);
909 }
910 else {
911 temp = min_ff(2.0f * src2[i], src1[i]);
912 }
913 dst[i] = (temp * fac + src1[i] * mfac);
914 }
915 }
916 else {
917 /* no op */
918 copy_v4_v4(dst, src1);
919 }
920}
921
922MINLINE void blend_color_linearlight_float(float dst[4], const float src1[4], const float src2[4])
923{
924 const float fac = src2[3];
925 if (fac != 0.0f) {
926 const float mfac = 1.0f - fac;
927 int i = 3;
928
929 while (i--) {
930 float temp;
931
932 if (src2[i] > 0.5f) {
933 temp = min_ff(src1[i] + 2.0f * (src2[i] - 0.5f), 1.0f);
934 }
935 else {
936 temp = max_ff(src1[i] + 2.0f * src2[i] - 1.0f, 0.0f);
937 }
938 dst[i] = (temp * fac + src1[i] * mfac);
939 }
940 }
941 else {
942 /* no op */
943 copy_v4_v4(dst, src1);
944 }
945}
946
947MINLINE void blend_color_vividlight_float(float dst[4], const float src1[4], const float src2[4])
948{
949 const float fac = src2[3];
950 if (fac != 0.0f) {
951 const float mfac = 1.0f - fac;
952 int i = 3;
953
954 while (i--) {
955 float temp;
956
957 if (src2[i] == 1.0f) {
958 temp = (src1[i] == 0.0f) ? 0.5f : 1.0f;
959 }
960 else if (src2[i] == 0.0f) {
961 temp = (src1[i] == 1.0f) ? 0.5f : 0.0f;
962 }
963 else if (src2[i] > 0.5f) {
964 temp = min_ff(((src1[i]) * 1.0f) / (2.0f * (1.0f - src2[i])), 1.0f);
965 }
966 else {
967 temp = max_ff(1.0f - ((1.0f - src1[i]) * 1.0f / (2.0f * src2[i])), 0.0f);
968 }
969 dst[i] = (temp * fac + src1[i] * mfac);
970 }
971 }
972 else {
973 /* no op */
974 copy_v4_v4(dst, src1);
975 }
976}
977
978MINLINE void blend_color_difference_float(float dst[4], const float src1[4], const float src2[4])
979{
980 const float fac = src2[3];
981 if (fac != 0.0f) {
982 const float mfac = 1.0f - fac;
983 int i = 3;
984
985 while (i--) {
986 dst[i] = (fabsf(src1[i] - src2[i]) * fac + src1[i] * mfac);
987 }
988 }
989 else {
990 /* no op */
991 copy_v4_v4(dst, src1);
992 }
993}
994
995MINLINE void blend_color_exclusion_float(float dst[4], const float src1[4], const float src2[4])
996{
997 const float fac = src2[3];
998 if (fac != 0.0f) {
999 const float mfac = 1.0f - fac;
1000 int i = 3;
1001
1002 while (i--) {
1003 const float temp = 0.5f - ((2 * (src1[i] - 0.5f) * (src2[i] - 0.5f)));
1004 dst[i] = (temp * fac + src1[i] * mfac);
1005 }
1006 }
1007 else {
1008 /* no op */
1009 copy_v4_v4(dst, src1);
1010 }
1011}
1012
1013MINLINE void blend_color_color_float(float dst[4], const float src1[4], const float src2[4])
1014{
1015 const float fac = src2[3];
1016 if (fac != 0.0f) {
1017 const float mfac = 1.0f - fac;
1018 float h1, s1, v1;
1019 float h2, s2, v2;
1020 float r, g, b;
1021
1022 rgb_to_hsv(src1[0], src1[1], src1[2], &h1, &s1, &v1);
1023 rgb_to_hsv(src2[0], src2[1], src2[2], &h2, &s2, &v2);
1024
1025 h1 = h2;
1026 s1 = s2;
1027
1028 hsv_to_rgb(h1, s1, v1, &r, &g, &b);
1029
1030 dst[0] = (r * fac + src1[0] * mfac);
1031 dst[1] = (g * fac + src1[1] * mfac);
1032 dst[2] = (b * fac + src1[2] * mfac);
1033 }
1034 else {
1035 /* no op */
1036 copy_v4_v4(dst, src1);
1037 }
1038}
1039
1040MINLINE void blend_color_hue_float(float dst[4], const float src1[4], const float src2[4])
1041{
1042 const float fac = src2[3];
1043 if (fac != 0.0f) {
1044 const float mfac = 1.0f - fac;
1045 float h1, s1, v1;
1046 float h2, s2, v2;
1047 float r, g, b;
1048
1049 rgb_to_hsv(src1[0], src1[1], src1[2], &h1, &s1, &v1);
1050 rgb_to_hsv(src2[0], src2[1], src2[2], &h2, &s2, &v2);
1051
1052 h1 = h2;
1053
1054 hsv_to_rgb(h1, s1, v1, &r, &g, &b);
1055
1056 dst[0] = (r * fac + src1[0] * mfac);
1057 dst[1] = (g * fac + src1[1] * mfac);
1058 dst[2] = (b * fac + src1[2] * mfac);
1059 }
1060 else {
1061 /* no op */
1062 copy_v4_v4(dst, src1);
1063 }
1064}
1065
1066MINLINE void blend_color_saturation_float(float dst[4], const float src1[4], const float src2[4])
1067{
1068 const float fac = src2[3];
1069 if (fac != 0.0f) {
1070 const float mfac = 1.0f - fac;
1071 float h1, s1, v1;
1072 float h2, s2, v2;
1073 float r, g, b;
1074
1075 rgb_to_hsv(src1[0], src1[1], src1[2], &h1, &s1, &v1);
1076 rgb_to_hsv(src2[0], src2[1], src2[2], &h2, &s2, &v2);
1077
1078 if (s1 > EPS_SATURATION) {
1079 s1 = s2;
1080 }
1081 hsv_to_rgb(h1, s1, v1, &r, &g, &b);
1082
1083 dst[0] = (r * fac + src1[0] * mfac);
1084 dst[1] = (g * fac + src1[1] * mfac);
1085 dst[2] = (b * fac + src1[2] * mfac);
1086 }
1087 else {
1088 /* no op */
1089 copy_v4_v4(dst, src1);
1090 }
1091}
1092
1093MINLINE void blend_color_luminosity_float(float dst[4], const float src1[4], const float src2[4])
1094{
1095 const float fac = src2[3];
1096 if (fac != 0.0f) {
1097 const float mfac = 1.0f - fac;
1098 float h1, s1, v1;
1099 float h2, s2, v2;
1100 float r, g, b;
1101
1102 rgb_to_hsv(src1[0], src1[1], src1[2], &h1, &s1, &v1);
1103 rgb_to_hsv(src2[0], src2[1], src2[2], &h2, &s2, &v2);
1104
1105 v1 = v2;
1106 hsv_to_rgb(h1, s1, v1, &r, &g, &b);
1107
1108 dst[0] = (r * fac + src1[0] * mfac);
1109 dst[1] = (g * fac + src1[1] * mfac);
1110 dst[2] = (b * fac + src1[2] * mfac);
1111 }
1112 else {
1113 /* no op */
1114 copy_v4_v4(dst, src1);
1115 }
1116}
1117
1119 const float src1[4],
1120 const float src2[4],
1121 float t)
1122{
1123 /* interpolation, colors are pre-multiplied so it goes fine */
1124 const float mt = 1.0f - t;
1125
1126 dst[0] = mt * src1[0] + t * src2[0];
1127 dst[1] = mt * src1[1] + t * src2[1];
1128 dst[2] = mt * src1[2] + t * src2[2];
1129 dst[3] = mt * src1[3] + t * src2[3];
1130}
1131
1132# undef EPS_SATURATION
1133# undef EPS_ALPHA
1134
1135#endif /* __MATH_COLOR_BLEND_INLINE_C__ */
1136
1138
1139} // namespace blender
#define EPS_SATURATION
#define MINLINE
#define abs
#define EPS_ALPHA
BMesh Iterators.
MINLINE float min_ff(float a, float b)
MINLINE void blend_color_vividlight_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_luminosity_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_add_alpha_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4])
MINLINE void blend_color_linearburn_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
MINLINE void blend_color_linearlight_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
MINLINE void blend_color_mix_float(float dst[4], const float src1[4], const float src2[4])
void hsv_to_rgb(float h, float s, float v, float *r_r, float *r_g, float *r_b)
Definition math_color.cc:25
MINLINE void blend_color_add_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4])
MINLINE void blend_color_saturation_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_erase_alpha_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4])
MINLINE void blend_color_sub_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4])
MINLINE void blend_color_mix_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4])
MINLINE void blend_color_darken_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4])
MINLINE void blend_color_pinlight_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_overlay_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_hardlight_float(float dst[4], const float src1[4], const float src2[4])
MINLINE int max_ii(int a, int b)
MINLINE void blend_color_hue_float(float dst[4], const float src1[4], const float src2[4])
void rgb_to_hsv(float r, float g, float b, float *r_h, float *r_s, float *r_v)
MINLINE void blend_color_darken_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_exclusion_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
MINLINE void blend_color_sub_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_difference_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
MINLINE void blend_color_luminosity_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
MINLINE void blend_color_color_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
MINLINE void blend_color_color_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_difference_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_interpolate_float(float dst[4], const float src1[4], const float src2[4], float t)
MINLINE void blend_color_add_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_erase_alpha_float(float dst[4], const float src1[4], const float src2[4])
MINLINE int divide_round_i(int a, int b)
MINLINE void blend_color_overlay_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
unsigned char uchar
Definition BKE_text.h:58
MINLINE void blend_color_hardlight_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
MINLINE void blend_color_exclusion_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_screen_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
MINLINE void blend_color_saturation_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
MINLINE void blend_color_dodge_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
MINLINE void copy_v4_v4_uchar(unsigned char r[4], const unsigned char a[4])
MINLINE void blend_color_hue_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
MINLINE void blend_color_screen_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_mul_float(float dst[4], const float src1[4], const float src2[4])
ATTR_WARN_UNUSED_RESULT const BMVert * v2
MINLINE void blend_color_burn_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
MINLINE float max_ff(float a, float b)
MINLINE unsigned char round_fl_to_uchar_clamp(float a)
MINLINE void blend_color_lighten_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_linearburn_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_vividlight_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
MINLINE void copy_v4_v4(float r[4], const float a[4])
MINLINE void blend_color_lighten_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4])
MINLINE void blend_color_burn_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_softlight_float(float dst[4], const float src1[4], const float src2[4])
MINLINE int min_ii(int a, int b)
MINLINE void blend_color_interpolate_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4], float ft)
MINLINE void blend_color_pinlight_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
MINLINE void blend_color_mul_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4])
MINLINE void blend_color_add_alpha_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_softlight_byte(unsigned char dst[4], const uchar src1[4], const uchar src2[4])
MINLINE void blend_color_linearlight_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void blend_color_dodge_float(float dst[4], const float src1[4], const float src2[4])
#define fabsf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float