@@ -9,6 +9,8 @@ from `f ∈ [1-δ, 1+δ]` by multiplying each color channel by `f`.
99You can also pass any `Distributions.Sampleable` from which the
1010factor is selected.
1111
12+ Pixels are clamped to [0,1] unless `clamp=false` is passed.
13+
1214## Example
1315
1416{cell=AdjustBrightness}
@@ -23,36 +25,37 @@ showgrid(titems; ncol = 4, npad = 16)
2325"""
2426struct AdjustBrightness{S<: Sampleable } <: Transform
2527 dist:: S
28+ clamp:: Bool
2629end
2730
28- AdjustBrightness (f:: Real ) = AdjustBrightness (Uniform (max (0 , 1 - f), 1 + f))
31+ AdjustBrightness (f:: Real ; clamp :: Bool = true ) = AdjustBrightness (Uniform (max (0 , 1 - f), 1 + f), clamp )
2932
3033getrandstate (tfm:: AdjustBrightness ) = rand (tfm. dist)
3134
3235
3336function apply (tfm:: AdjustBrightness , item:: Image ; randstate = getrandstate (tfm))
3437 factor = randstate
35- return setdata (item, adjustbrightness (itemdata (item), factor))
38+ return setdata (item, adjustbrightness (itemdata (item), factor, tfm . clamp ))
3639end
3740
3841function apply! (buf, tfm:: AdjustBrightness , item:: Image ; randstate = getrandstate (tfm))
3942 factor = randstate
40- adjustbrightness! (itemdata (buf), itemdata (item), factor)
43+ adjustbrightness! (itemdata (buf), itemdata (item), factor, tfm . clamp )
4144 return buf
4245end
4346
4447
45- function adjustbrightness (img, factor)
46- return adjustbrightness! (copy (img), factor)
48+ function adjustbrightness (img, factor, clamp )
49+ return adjustbrightness! (copy (img), factor, clamp )
4750end
4851
4952
50- adjustbrightness! (img, factor) = adjustbrightness! (img, img, factor)
53+ adjustbrightness! (img, factor, clamp ) = adjustbrightness! (img, img, factor, clamp )
5154
5255# TODO : add methods for non-RGB/Gray images
53- function adjustbrightness! (dst:: AbstractArray{U} , img:: AbstractArray{T} , factor) where {T, U}
56+ function adjustbrightness! (dst:: AbstractArray{U} , img:: AbstractArray{T} , factor, clamp ) where {T, U}
5457 map! (dst, img) do x
55- convert (U, clamp01 (x * factor))
58+ convert (U, clamp ? clamp01 (x * factor) : x * factor )
5659 end
5760end
5861
@@ -70,6 +73,8 @@ of the image.
7073You can also pass any `Distributions.Sampleable` from which the
7174factor is selected.
7275
76+ Pixels are clamped to [0,1] unless `clamp=false` is passed.
77+
7378## Example
7479
7580{cell=AdjustBrightness}
@@ -84,36 +89,37 @@ showgrid(titems; ncol = 4, npad = 16)
8489"""
8590struct AdjustContrast{S<: Sampleable } <: Transform
8691 dist:: S
92+ clamp:: Bool
8793end
8894
89- AdjustContrast (f:: Real ) = AdjustContrast (Uniform (max (0 , 1 - f), 1 + f))
95+ AdjustContrast (f:: Real ; clamp :: Bool = true ) = AdjustContrast (Uniform (max (0 , 1 - f), 1 + f), clamp )
9096
9197getrandstate (tfm:: AdjustContrast ) = rand (tfm. dist)
9298
9399
94100function apply (tfm:: AdjustContrast , item:: Image ; randstate = getrandstate (tfm))
95101 factor = randstate
96- return setdata (item, adjustcontrast (itemdata (item), factor))
102+ return setdata (item, adjustcontrast (itemdata (item), factor, tfm . clamp ))
97103end
98104
99105function apply! (buf, tfm:: AdjustContrast , item:: Image ; randstate = getrandstate (tfm))
100106 factor = randstate
101- adjustcontrast! (itemdata (buf), itemdata (item), factor)
107+ adjustcontrast! (itemdata (buf), itemdata (item), factor, tfm . clamp )
102108 return buf
103109end
104110
105111
106- function adjustcontrast (img, factor)
107- return adjustcontrast! (copy (img), factor)
112+ function adjustcontrast (img, factor, clamp )
113+ return adjustcontrast! (copy (img), factor, clamp )
108114end
109115
110116
111117# TODO : add methods for non-RGB/Gray images
112- function adjustcontrast! (dst:: AbstractArray{U} , img:: AbstractArray{T} , factor) where {T, U}
118+ function adjustcontrast! (dst:: AbstractArray{U} , img:: AbstractArray{T} , factor, clamp ) where {T, U}
113119 μ = mean (img)
114120 map! (dst, img) do x
115- convert (U, clamp01 (x + μ * (1 - factor)))
121+ convert (U, clamp ? clamp01 (x + μ * (1 - factor)) : x + μ * ( 1 - factor ))
116122 end
117123end
118124
119- adjustcontrast! (img, factor) = adjustcontrast! (img, img, factor)
125+ adjustcontrast! (img, factor, clamp ) = adjustcontrast! (img, img, factor, clamp )
0 commit comments