11#if !BLI_MATH_DO_INLINE
20#ifndef __MATH_COLOR_BLEND_INLINE_C__
21# define __MATH_COLOR_BLEND_INLINE_C__
24# define EPS_SATURATION 0.0005f
25# define EPS_ALPHA 0.0005f
44 const int t = src2[3];
45 const int mt = 255 - t;
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);
68 const int t = src2[3];
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);
90 const int t = src2[3];
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);
112 const int t = src2[3];
113 const int mt = 255 - t;
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]);
135 const int t = src2[3];
136 const int mt = 255 - t;
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]));
158 const int t = src2[3];
159 const int mt = 255 - t;
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]));
181 const int t = src2[3];
198 const int t = src2[3];
213 const int fac = int(src2[3]);
215 const int mfac = 255 - fac;
222 temp = 255 - ((255 - 2 * (src1[i] - 127)) * (255 - src2[i]) / 255);
225 temp = (2 * src1[i] * src2[i]) >> 8;
227 dst[i] =
uchar(
min_ii((temp * fac + src1[i] * mfac) / 255, 255));
238 const int fac = int(src2[3]);
240 const int mfac = 255 - fac;
247 temp = 255 - ((255 - 2 * (src2[i] - 127)) * (255 - src1[i]) / 255);
250 temp = (2 * src2[i] * src1[i]) >> 8;
252 dst[i] =
uchar(
min_ii((temp * fac + src1[i] * mfac) / 255, 255));
263 const int fac = src2[3];
265 const int mfac = 255 - fac;
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);
281 const int fac = src2[3];
283 const int mfac = 255 - fac;
287 const int temp =
max_ii(src1[i] + src2[i] - 255, 0);
288 dst[i] =
uchar((temp * fac + src1[i] * mfac) / 255);
299 const int fac = src2[3];
301 const int mfac = 255 - fac;
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);
317 const int fac = src2[3];
319 const int mfac = 255 - fac;
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);
335 const float fac =
float(src2[3]) / 255.0f;
337 const float mfac = 1.0f - fac;
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;
358 const int fac = src2[3];
360 const int mfac = 255 - fac;
367 temp =
max_ii(2 * (src2[i] - 127), src1[i]);
370 temp =
min_ii(2 * src2[i], src1[i]);
372 dst[i] =
uchar((
min_ii(temp, 255) * fac + src1[i] * mfac) / 255);
383 const int fac = src2[3];
385 const int mfac = 255 - fac;
392 temp =
min_ii(src1[i] + 2 * (src2[i] - 127), 255);
395 temp =
max_ii(src1[i] + 2 * src2[i] - 255, 0);
397 dst[i] =
uchar((temp * fac + src1[i] * mfac) / 255);
408 const int fac = src2[3];
410 const int mfac = 255 - fac;
416 if (src2[i] == 255) {
417 temp = (src1[i] == 0) ? 127 : 255;
419 else if (src2[i] == 0) {
420 temp = (src1[i] == 255) ? 127 : 0;
422 else if (src2[i] > 127) {
423 temp =
min_ii(((src1[i]) * 255) / (2 * (255 - src2[i])), 255);
426 temp =
max_ii(255 - ((255 - src1[i]) * 255 / (2 * src2[i])), 0);
428 dst[i] =
uchar((temp * fac + src1[i] * mfac) / 255);
439 const int fac = src2[3];
441 const int mfac = 255 - fac;
445 const int temp =
abs(src1[i] - src2[i]);
446 dst[i] =
uchar((temp * fac + src1[i] * mfac) / 255);
457 const int fac = src2[3];
459 const int mfac = 255 - fac;
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);
475 const int fac = src2[3];
477 const int mfac = 255 - fac;
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);
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);
501 const int fac = src2[3];
503 const int mfac = 255 - fac;
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);
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);
526 const int fac = src2[3];
528 const int mfac = 255 - fac;
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);
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);
553 const int fac = src2[3];
555 const int mfac = 255 - fac;
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);
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);
583 const int t = int(255 * ft);
584 const int mt = 255 - t;
585 int tmp = (mt * src1[3] + t * src2[3]);
603 if (src2[3] != 0.0f) {
605 const float t = src2[3];
606 const float mt = 1.0f - t;
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;
621 if (src2[3] != 0.0f) {
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];
636 if (src2[3] != 0.0f) {
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);
651 if (src2[3] != 0.0f) {
653 const float t = src2[3];
654 const float mt = 1.0f - t;
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];
669 if (src2[3] != 0.0f) {
672 const float t = src2[3];
673 const float mt = 1.0f - t;
674 const float map_alpha = src1[3] / src2[3];
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);
689 if (src2[3] != 0.0f) {
692 const float t = src2[3];
693 const float mt = 1.0f - t;
694 const float map_alpha = src1[3] / src2[3];
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);
709 if (src2[3] != 0.0f && src1[3] > 0.0f) {
711 float alpha =
max_ff(src1[3] - src2[3], 0.0f);
718 map_alpha = alpha / src1[3];
720 dst[0] = src1[0] * map_alpha;
721 dst[1] = src1[1] * map_alpha;
722 dst[2] = src1[2] * map_alpha;
733 if (src2[3] != 0.0f && src1[3] < 1.0f) {
735 float alpha =
min_ff(src1[3] + src2[3], 1.0f);
742 map_alpha = (src1[3] > 0.0f) ? alpha / src1[3] : 1.0f;
744 dst[0] = src1[0] * map_alpha;
745 dst[1] = src1[1] * map_alpha;
746 dst[2] = src1[2] * map_alpha;
757 const float fac = src2[3];
759 const float mfac = 1.0f - fac;
765 if (src1[i] > 0.5f) {
766 temp = 1.0f - (1.0f - 2.0f * (src1[i] - 0.5f)) * (1.0f - src2[i]);
769 temp = 2.0f * src1[i] * src2[i];
771 dst[i] =
min_ff(temp * fac + src1[i] * mfac, 1.0f);
782 const float fac = src2[3];
784 const float mfac = 1.0f - fac;
790 if (src2[i] > 0.5f) {
791 temp = 1.0f - ((1.0f - 2.0f * (src2[i] - 0.5f)) * (1.0f - src1[i]));
794 temp = 2.0f * src2[i] * src1[i];
796 dst[i] =
min_ff((temp * fac + src1[i] * mfac) / 1.0f, 1.0f);
807 const float fac = src2[3];
809 const float mfac = 1.0f - fac;
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);
826 const float fac = src2[3];
828 const float mfac = 1.0f - fac;
832 const float temp =
max_ff(src1[i] + src2[i] - 1.0f, 0.0f);
833 dst[i] = (temp * fac + src1[i] * mfac);
844 const float fac = src2[3];
846 const float mfac = 1.0f - fac;
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);
862 const float fac = src2[3];
864 const float mfac = 1.0f - fac;
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);
880 const float fac = src2[3];
882 const float mfac = 1.0f - fac;
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;
899 const float fac = src2[3];
901 const float mfac = 1.0f - fac;
907 if (src2[i] > 0.5f) {
908 temp =
max_ff(2.0f * (src2[i] - 0.5f), src1[i]);
911 temp =
min_ff(2.0f * src2[i], src1[i]);
913 dst[i] = (temp * fac + src1[i] * mfac);
924 const float fac = src2[3];
926 const float mfac = 1.0f - fac;
932 if (src2[i] > 0.5f) {
933 temp =
min_ff(src1[i] + 2.0f * (src2[i] - 0.5f), 1.0f);
936 temp =
max_ff(src1[i] + 2.0f * src2[i] - 1.0f, 0.0f);
938 dst[i] = (temp * fac + src1[i] * mfac);
949 const float fac = src2[3];
951 const float mfac = 1.0f - fac;
957 if (src2[i] == 1.0f) {
958 temp = (src1[i] == 0.0f) ? 0.5f : 1.0f;
960 else if (src2[i] == 0.0f) {
961 temp = (src1[i] == 1.0f) ? 0.5f : 0.0f;
963 else if (src2[i] > 0.5f) {
964 temp =
min_ff(((src1[i]) * 1.0f) / (2.0f * (1.0f - src2[i])), 1.0f);
967 temp =
max_ff(1.0f - ((1.0f - src1[i]) * 1.0f / (2.0f * src2[i])), 0.0f);
969 dst[i] = (temp * fac + src1[i] * mfac);
980 const float fac = src2[3];
982 const float mfac = 1.0f - fac;
986 dst[i] = (
fabsf(src1[i] - src2[i]) * fac + src1[i] * mfac);
997 const float fac = src2[3];
999 const float mfac = 1.0f - fac;
1003 const float temp = 0.5f - ((2 * (src1[i] - 0.5f) * (src2[i] - 0.5f)));
1004 dst[i] = (temp * fac + src1[i] * mfac);
1015 const float fac = src2[3];
1017 const float mfac = 1.0f - fac;
1022 rgb_to_hsv(src1[0], src1[1], src1[2], &h1, &s1, &v1);
1030 dst[0] = (r * fac + src1[0] * mfac);
1031 dst[1] = (g * fac + src1[1] * mfac);
1032 dst[2] = (
b * fac + src1[2] * mfac);
1042 const float fac = src2[3];
1044 const float mfac = 1.0f - fac;
1049 rgb_to_hsv(src1[0], src1[1], src1[2], &h1, &s1, &v1);
1056 dst[0] = (r * fac + src1[0] * mfac);
1057 dst[1] = (g * fac + src1[1] * mfac);
1058 dst[2] = (
b * fac + src1[2] * mfac);
1068 const float fac = src2[3];
1070 const float mfac = 1.0f - fac;
1075 rgb_to_hsv(src1[0], src1[1], src1[2], &h1, &s1, &v1);
1083 dst[0] = (r * fac + src1[0] * mfac);
1084 dst[1] = (g * fac + src1[1] * mfac);
1085 dst[2] = (
b * fac + src1[2] * mfac);
1095 const float fac = src2[3];
1097 const float mfac = 1.0f - fac;
1102 rgb_to_hsv(src1[0], src1[1], src1[2], &h1, &s1, &v1);
1108 dst[0] = (r * fac + src1[0] * mfac);
1109 dst[1] = (g * fac + src1[1] * mfac);
1110 dst[2] = (
b * fac + src1[2] * mfac);
1119 const float src1[4],
1120 const float src2[4],
1124 const float mt = 1.0f - t;
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];
1132# undef EPS_SATURATION
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)
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])
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])
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float