Skip to content

Commit

Permalink
Merge pull request #711 from zygmuntszpak/fix_adjust_gamma
Browse files Browse the repository at this point in the history
Fixes adjust_gamma so that it works on offset arrays
  • Loading branch information
zygmuntszpak authored Mar 29, 2018
2 parents c1054eb + eb75dd1 commit bffc92a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/exposure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -371,21 +371,24 @@ adjust_gamma(img::AbstractArray{T}, gamma::Number) where {T<:Number} = _adjust_g
adjust_gamma(img::AbstractArray{T}, gamma::Number) where {T<:Colorant} = _adjust_gamma(img, gamma, T)

function _adjust_gamma(img::AbstractArray, gamma::Number, C::Type)
gamma_corrected_img = zeros(C, size(img))
gamma_corrected_img = _fill(oneunit(C), indices(img))
for I in eachindex(img)
gamma_corrected_img[I] = _gamma_pixel_rescale(img[I], gamma)
end
gamma_corrected_img
end

function adjust_gamma(img::AbstractArray{T}, gamma::Number, minval::Number, maxval::Number) where T<:Number
gamma_corrected_img = zeros(Float64, size(img))
gamma_corrected_img = _fill(oneunit(T), indices(img))
for I in eachindex(img)
gamma_corrected_img[I] = _gamma_pixel_rescale(img[I], gamma, minval, maxval)
end
gamma_corrected_img
end

_fill(val, dim) = fill(val, dim) # fallback
_fill(val, dim::NTuple{N,Base.OneTo}) where {N} = fill(val, map(length, dim))

"""
```
hist_matched_img = histmatch(img, oimg, nbins)
Expand Down
42 changes: 41 additions & 1 deletion test/exposure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,39 +125,79 @@ using Base.Test, Images, Colors, FixedPointNumbers
@test img == ret
@test eltype(ret) == eltype(img)

imgp = padarray(img, Fill(0, (2,2)))
retp = adjust_gamma(imgp, 1)
@test imgp == retp
@test eltype(retp) == eltype(imgp)

img = oneunits(Gray{N0f8}, 10, 10)
ret = adjust_gamma(img, 1)
@test img == ret
@test eltype(ret) == eltype(img)

imgp = padarray(img, Fill(0, (2,2)))
retp = adjust_gamma(imgp, 1)
@test imgp == retp
@test eltype(retp) == eltype(imgp)

img = oneunits(Gray{N0f16}, 10, 10)
ret = adjust_gamma(img, 1)
@test eltype(ret) == eltype(img)

imgp = padarray(img, Fill(0, (2,2)))
retp = adjust_gamma(imgp, 1)
@test imgp == retp
@test eltype(retp) == eltype(imgp)

img = oneunits(AGray{N0f8}, 10, 10)
ret = adjust_gamma(img, 1)
@test img == ret
@test eltype(ret) == eltype(img)

imgp = padarray(img, Fill(0, (2,2)))
retp = adjust_gamma(imgp, 1)
@test imgp == retp
@test eltype(retp) == eltype(imgp)

img = oneunits(RGB{N0f8}, 10, 10)
ret = adjust_gamma(img, 1)
@test img == ret
@test eltype(ret) == eltype(img)

imgp = padarray(img, Fill(zero(eltype(img)), (2,2)))
retp = adjust_gamma(imgp, 1)
@test imgp == retp
@test eltype(retp) == eltype(imgp)

img = oneunits(RGB{N0f16}, 10, 10)
ret = adjust_gamma(img, 1)
@test img == ret
@test eltype(ret) == eltype(img)

imgp = padarray(img, Fill(zero(eltype(img)), (2,2)))
retp = adjust_gamma(imgp, 1)
@test imgp == retp
@test eltype(retp) == eltype(imgp)

img = oneunits(RGB{Float64}, 10, 10)
ret = adjust_gamma(img, 1)
@test all(map((i, r) -> isapprox(i, r), img, ret))
@test eltype(ret) == eltype(img)

imgp = padarray(img, Fill(zero(eltype(img)), (2,2)))
retp = adjust_gamma(imgp, 1)
@test all(map((i, r) -> isapprox(i, r), imgp, retp))
@test eltype(retp) == eltype(imgp)

img = oneunits(ARGB{N0f8}, 10, 10)
ret = adjust_gamma(img, 1)
@test img == ret

imgp = padarray(img, Fill(zero(eltype(img)), (2,2)))
retp = adjust_gamma(imgp, 1)
@test imgp == retp


#Working

img = reshape(1:1:100, 10, 10)
Expand Down Expand Up @@ -361,7 +401,7 @@ using Base.Test, Images, Colors, FixedPointNumbers
@test complement(Gray(0.2)) == Gray(0.8)
@test all(complement.(img) .== 1 - img)
# deprecated (#690)
@test all(complement(img) .== 1 - img)
@test all(complement.(img) .== 1 - img)

hist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Expand Down

0 comments on commit bffc92a

Please sign in to comment.