Skip to content

Commit 18b821a

Browse files
authored
Make clamp01 optional in AdjustBrightness and AdjustContrast (#77)
* Add optional clamp flag to AdjustBrightness and AdjustContrast transforms * Add colortransforms.jl to the tests
1 parent ff3e46a commit 18b821a

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

src/colortransforms.jl

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ from `f ∈ [1-δ, 1+δ]` by multiplying each color channel by `f`.
99
You can also pass any `Distributions.Sampleable` from which the
1010
factor 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
"""
2426
struct AdjustBrightness{S<:Sampleable} <: Transform
2527
dist::S
28+
clamp::Bool
2629
end
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

3033
getrandstate(tfm::AdjustBrightness) = rand(tfm.dist)
3134

3235

3336
function 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))
3639
end
3740

3841
function 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
4245
end
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)
4750
end
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
5760
end
5861

@@ -70,6 +73,8 @@ of the image.
7073
You can also pass any `Distributions.Sampleable` from which the
7174
factor 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
"""
8590
struct AdjustContrast{S<:Sampleable} <: Transform
8691
dist::S
92+
clamp::Bool
8793
end
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

9197
getrandstate(tfm::AdjustContrast) = rand(tfm.dist)
9298

9399

94100
function 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))
97103
end
98104

99105
function 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
103109
end
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)
108114
end
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
117123
end
118124

119-
adjustcontrast!(img, factor) = adjustcontrast!(img, img, factor)
125+
adjustcontrast!(img, factor, clamp) = adjustcontrast!(img, img, factor, clamp)

test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,7 @@ include("./imports.jl")
4747
@testset ExtendedTestSet "rowtransforms.jl" begin
4848
include("rowtransforms.jl")
4949
end
50+
@testset ExtendedTestSet "colortransforms.jl" begin
51+
include("colortransforms.jl")
52+
end
5053
end

0 commit comments

Comments
 (0)