Skip to content

Commit

Permalink
Merge pull request #321 from psd-tools/blending-division-stability
Browse files Browse the repository at this point in the history
Improve blending numerical stability
  • Loading branch information
kyamagu committed Jun 18, 2022
2 parents 56eea2e + d613ec1 commit 93cd3fc
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/psd_tools/composite/blend.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ def color_dodge(Cb, Cs, s=1.0):
B[Cs == 1] = 1
B[Cb == 0] = 0
index = (Cs != 1) & (Cb != 0)
B[index] = np.minimum(1, Cb[index] / (s * (1 - Cs[index])))
B[index] = np.minimum(1, Cb[index] / (s * (1 - Cs[index] + 1e-9)))
return B


def color_burn(Cb, Cs, s=1.0):
B = np.zeros_like(Cb, dtype=np.float32)
B[Cb == 1] = 1
index = (Cb != 1) & (Cs != 0)
B[index] = 1 - np.minimum(1, (1 - Cb[index]) / (s * Cs[index]))
B[index] = 1 - np.minimum(1, (1 - Cb[index]) / (s * Cs[index] + 1e-9))
return B


Expand Down Expand Up @@ -162,7 +162,7 @@ def divide(Cb, Cs):
Looks at the color information in each channel and divides the blend color
from the base color.
"""
B = Cb / (Cs + 1e-6)
B = Cb / (Cs + 1e-9)
B[B > 1] = 1
return B

Expand Down Expand Up @@ -199,7 +199,7 @@ def _rgb2cmy(C, K):
K = np.repeat(K, 3, axis=2)
color = np.zeros((C.shape[0], C.shape[1], 3))
index = K < 1.
color[index] = (1. - C[index] - K[index]) / (1. - K[index])
color[index] = (1. - C[index] - K[index]) / (1. - K[index] + 1e-9)
return color


Expand Down Expand Up @@ -262,11 +262,11 @@ def _clip_color(C):

index = C_min < 0.
L_i = L[index]
C[index] = L_i + (C[index] - L_i) * L_i / (L_i - C_min[index])
C[index] = L_i + (C[index] - L_i) * L_i / (L_i - C_min[index] + 1e-9)

index = C_max > 1.
L_i = L[index]
C[index] = L_i + (C[index] - L_i) * (1 - L_i) / (C_max[index] - L_i)
C[index] = L_i + (C[index] - L_i) * (1 - L_i) / (C_max[index] - L_i + 1e-9)

# For numerical stability.
C[C < 0.] = 0
Expand Down Expand Up @@ -294,7 +294,7 @@ def _set_sat(C, s):

index = index_mid & index_diff
B[index] = C_mid[index] - C_min[index] * \
s[index] / (C_max[index] - C_min[index])
s[index] / (C_max[index] - C_min[index] + 1e-9)
index = index_max & index_diff
B[index] = s[index]

Expand Down

0 comments on commit 93cd3fc

Please sign in to comment.