|  | 
|  | 1 | +# usage example for package developer: | 
|  | 2 | +# | 
|  | 3 | +#     import ContrastAdjustmentAPI: AbstractIntensityAdjustmentAlgorithm, | 
|  | 4 | +#                             adjust_intensity, adjust_intensity! | 
|  | 5 | + | 
|  | 6 | +""" | 
|  | 7 | +    AbstractIntensityAdjustmentAlgorithm <: AbstractImageFilter | 
|  | 8 | +
 | 
|  | 9 | +A root type for `ImageContrastAdjustment` package that relates to algorithms | 
|  | 10 | +that manipulate contrast without operating on intensity histograms. | 
|  | 11 | +
 | 
|  | 12 | +Any concrete intensity adjustment algorithm shall subtype it to support | 
|  | 13 | +[`adjust_intensity`](@ref) and [`adjust_intensity!`](@ref) APIs. | 
|  | 14 | +
 | 
|  | 15 | +# Examples | 
|  | 16 | +
 | 
|  | 17 | +All intensity adjustment algorithms in ImageContrastAdjustment are called in the | 
|  | 18 | +following pattern: | 
|  | 19 | +
 | 
|  | 20 | +```julia | 
|  | 21 | +# first generate an algorithm instance | 
|  | 22 | +f = LinearStretching() | 
|  | 23 | +
 | 
|  | 24 | +# then pass the algorithm to `adjust_intensity` | 
|  | 25 | +img_adjusted = adjust_intensity(img, f) | 
|  | 26 | +
 | 
|  | 27 | +# or use in-place version `adjust_intensity!` | 
|  | 28 | +img_adjusted = similar(img) | 
|  | 29 | +adjust_intensity!(img_adjusted, img, f) | 
|  | 30 | +``` | 
|  | 31 | +
 | 
|  | 32 | +Some algorithms also receive additional information as an argument, | 
|  | 33 | +e.g., `gamma` of `GammaCorrection`. | 
|  | 34 | +
 | 
|  | 35 | +```julia | 
|  | 36 | +# you can explicit specify the parameters | 
|  | 37 | +f = GammaCorrection(gamma = 1.4) | 
|  | 38 | +``` | 
|  | 39 | +
 | 
|  | 40 | +For more examples, please check [`adjust_intensity`](@ref), | 
|  | 41 | +[`adjust_intensity!`](@ref) and concrete algorithms. | 
|  | 42 | +""" | 
|  | 43 | +abstract type AbstractIntensityAdjustmentAlgorithm <: AbstractImageFilter end | 
|  | 44 | + | 
|  | 45 | +adjust_intensity!(out::Union{GenericGrayImage, AbstractArray{<:Color3}}, | 
|  | 46 | +          img, | 
|  | 47 | +          f::AbstractIntensityAdjustmentAlgorithm, | 
|  | 48 | +          args...; kwargs...) = | 
|  | 49 | +    f(out, img, args...; kwargs...) | 
|  | 50 | + | 
|  | 51 | +# TODO: Relax this to all color types | 
|  | 52 | +function adjust_intensity!(img::Union{GenericGrayImage, AbstractArray{<:Color3}}, | 
|  | 53 | +                   f::AbstractIntensityAdjustmentAlgorithm, | 
|  | 54 | +                   args...; kwargs...) | 
|  | 55 | +    tmp = copy(img) | 
|  | 56 | +    f(img, tmp, args...; kwargs...) | 
|  | 57 | +    return img | 
|  | 58 | +end | 
|  | 59 | + | 
|  | 60 | +function adjust_intensity(::Type{T}, | 
|  | 61 | +                  img, | 
|  | 62 | +                  f::AbstractIntensityAdjustmentAlgorithm, | 
|  | 63 | +                  args...; kwargs...) where T | 
|  | 64 | +    out = similar(Array{T}, axes(img)) | 
|  | 65 | +    adjust_intensity!(out, img, f, args...; kwargs...) | 
|  | 66 | +    return out | 
|  | 67 | +end | 
|  | 68 | + | 
|  | 69 | +adjust_intensity(img::AbstractArray{T}, | 
|  | 70 | +                 f::AbstractIntensityAdjustmentAlgorithm, | 
|  | 71 | +                 args...; kwargs...) where T <: Colorant = | 
|  | 72 | +         adjust_intensity(T, img, f, args...; kwargs...) | 
|  | 73 | + | 
|  | 74 | +# Do not promote Number to Gray{<:Number} | 
|  | 75 | +adjust_intensity(img::AbstractArray{T}, | 
|  | 76 | +                 f::AbstractIntensityAdjustmentAlgorithm, | 
|  | 77 | +                 args...; kwargs...) where T <: Number = | 
|  | 78 | +        adjust_intensity(T, img, f, args...; kwargs...) | 
|  | 79 | + | 
|  | 80 | + | 
|  | 81 | +# Handle instance where the input is a sequence of images. | 
|  | 82 | +adjust_intensity!(out_sequence::Vector{T}, | 
|  | 83 | +          img_sequence, | 
|  | 84 | +          f::AbstractIntensityAdjustmentAlgorithm, | 
|  | 85 | +          args...; kwargs...) where T <: Union{GenericGrayImage, AbstractArray{<:Color3}} = | 
|  | 86 | +    f(out_sequence, img_sequence, args...; kwargs...) | 
|  | 87 | + | 
|  | 88 | +# TODO: Relax this to all color types | 
|  | 89 | +function adjust_intensity!(img_sequence::Vector{T}, | 
|  | 90 | +                   f::AbstractIntensityAdjustmentAlgorithm, | 
|  | 91 | +                   args...; kwargs...) where T <: Union{GenericGrayImage, AbstractArray{<:Color3}} | 
|  | 92 | +    tmp = copy(img_sequence) | 
|  | 93 | +    f(img_sequence, tmp, args...; kwargs...) | 
|  | 94 | +    return img_sequence | 
|  | 95 | +end | 
|  | 96 | + | 
|  | 97 | +function adjust_intensity(::Type{T}, | 
|  | 98 | +                  img_sequence::Vector{<:AbstractArray}, | 
|  | 99 | +                  f::AbstractIntensityAdjustmentAlgorithm, | 
|  | 100 | +                  args...; kwargs...) where T | 
|  | 101 | +    N  = length(img_sequence) | 
|  | 102 | +    out_sequence = [similar(Array{T}, axes(img_sequence[n])) for n = 1:N] | 
|  | 103 | +    adjust_intensity!(out_sequence, img_sequence, f, args...; kwargs...) | 
|  | 104 | +    return out_sequence | 
|  | 105 | +end | 
|  | 106 | + | 
|  | 107 | +adjust_intensity(img_sequence::Vector{<:AbstractArray{T}}, | 
|  | 108 | +                 f::AbstractIntensityAdjustmentAlgorithm, | 
|  | 109 | +                 args...; kwargs...) where T <: Colorant = | 
|  | 110 | +         adjust_intensity(T, img_sequence, f, args...; kwargs...) | 
|  | 111 | + | 
|  | 112 | +# Do not promote Number to Gray{<:Number} | 
|  | 113 | +adjust_intensity(img_sequence::Vector{<:AbstractArray{T}}, | 
|  | 114 | +                 f::AbstractIntensityAdjustmentAlgorithm, | 
|  | 115 | +                 args...; kwargs...) where T <: Number = | 
|  | 116 | +        adjust_intensity(T, img_sequence, f, args...; kwargs...) | 
|  | 117 | + | 
|  | 118 | +### Docstrings | 
|  | 119 | + | 
|  | 120 | +""" | 
|  | 121 | +    adjust_intensity!([out,] img, f::AbstractIntensityAdjustmentAlgorithm, args...; kwargs...) | 
|  | 122 | +
 | 
|  | 123 | +Adjust intensity of `img` using algorithm `f`. | 
|  | 124 | +
 | 
|  | 125 | +# Output | 
|  | 126 | +
 | 
|  | 127 | +If `out` is specified, it will be changed in place. Otherwise `img` will be changed in place. | 
|  | 128 | +
 | 
|  | 129 | +# Examples | 
|  | 130 | +
 | 
|  | 131 | +Just simply pass an algorithm to `adjust_intensity!`: | 
|  | 132 | +
 | 
|  | 133 | +```julia | 
|  | 134 | +img_adjusted = similar(img) | 
|  | 135 | +adjust_intensity!(img_adjusted, img, f) | 
|  | 136 | +``` | 
|  | 137 | +
 | 
|  | 138 | +For cases you just want to change `img` in place, you don't necessarily need to manually | 
|  | 139 | +allocate `img_adjusted`; just use the convenient method: | 
|  | 140 | +
 | 
|  | 141 | +```julia | 
|  | 142 | +adjust_intensity!(img, f) | 
|  | 143 | +``` | 
|  | 144 | +
 | 
|  | 145 | +See also: [`adjust_intensity`](@ref) | 
|  | 146 | +""" | 
|  | 147 | +adjust_intensity! | 
|  | 148 | + | 
|  | 149 | +""" | 
|  | 150 | +    adjust_intensity([T::Type,] img, f::AbstractIntensityAdjustmentAlgorithm, args...; kwargs...) | 
|  | 151 | +
 | 
|  | 152 | +Adjust intensity of `img` using algorithm `f`. | 
|  | 153 | +
 | 
|  | 154 | +# Output | 
|  | 155 | +
 | 
|  | 156 | +The return image `img_adjusted` is an `Array{T}`. | 
|  | 157 | +
 | 
|  | 158 | +If `T` is not specified, then it's inferred. | 
|  | 159 | +# Examples | 
|  | 160 | +
 | 
|  | 161 | +Just simply pass the input image and algorithm to `adjust_intensity` | 
|  | 162 | +
 | 
|  | 163 | +```julia | 
|  | 164 | +img_adjusted = adjust_intensity(img, f) | 
|  | 165 | +``` | 
|  | 166 | +
 | 
|  | 167 | +This reads as "`adjust_intensity` of image `img` using algorithm `f`". | 
|  | 168 | +
 | 
|  | 169 | +You can also explicitly specify the return type: | 
|  | 170 | +
 | 
|  | 171 | +```julia | 
|  | 172 | +img_adjusted_float32 = adjust_intensity(Gray{Float32}, img, f) | 
|  | 173 | +``` | 
|  | 174 | +
 | 
|  | 175 | +See also [`adjust_intensity!`](@ref) for in-place intensity adjustment. | 
|  | 176 | +""" | 
|  | 177 | +adjust_intensity | 
0 commit comments