Skip to content

Commit

Permalink
Refactor types
Browse files Browse the repository at this point in the history
  • Loading branch information
adrhill committed Sep 14, 2024
1 parent 682401f commit 8d09bf2
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 34 deletions.
16 changes: 7 additions & 9 deletions src/DitherPunk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@ using UnicodeGraphics: uprint, ustring

abstract type AbstractDither end

const BinaryGray = AbstractGray{Bool}
const NumberLike = Union{Number,AbstractGray}
const BinaryLike = Union{Bool,BinaryGray}
const Pixel = Union{Number,Colorant}
const ColorLike = Union{Number,Colorant}
const GrayLike = Union{Number,AbstractGray}
const BinaryLike = Union{Bool,AbstractGray{Bool}}

const GenericBinaryImage{T<:BinaryLike} = Union{BitMatrix,AbstractArray{T,2}}
const GenericGrayImage{T<:NumberLike} = AbstractArray{T,2}
const GenericImage{T<:Pixel} = AbstractArray{T,2}
const ColorArray{T<:Pixel,N} = AbstractArray{T,N}
const ColorVector{T<:Pixel} = AbstractVector{T}
const ColorVector{T<:ColorLike} = AbstractArray{T,1}
const GenericImage{T<:ColorLike} = AbstractArray{T,2}
const GrayImage{T<:GrayLike} = AbstractArray{T,2}
const BinaryImage{T<:BinaryLike} = AbstractArray{T,2}

include("colorschemes.jl")
include("utils.jl")
Expand Down
12 changes: 4 additions & 8 deletions src/api/binary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function dither(::Type{T}, img::GenericImage, alg::AbstractDither; kwargs...) wh
end

# ...and defaults to the type of the input image.
function dither(img::GenericImage{T,N}, alg::AbstractDither; kwargs...) where {T<:Pixel,N}
function dither(img::GenericImage{T}, alg::AbstractDither; kwargs...) where {T<:ColorLike}
return dither(T, img, alg; kwargs...)
end

Expand All @@ -53,20 +53,16 @@ end
# Dispatch to binary dithering on grayscale images
# when no color palette is provided
function _binarydither!(
out::GenericGrayImage,
img::GenericGrayImage,
alg::AbstractDither;
to_linear=false,
kwargs...,
out::GrayImage, img::GrayImage, alg::AbstractDither; to_linear=false, kwargs...
)
to_linear && (img = srgb2linear.(img))
return binarydither!(alg, out, img; kwargs...)
end

# Dispatch to per-channel dithering on color images when no color palette is provided
function _binarydither!(
out::GenericImage{T,2},
img::GenericImage{T,2},
out::GenericImage{T},
img::GenericImage{T},
alg::AbstractDither;
to_linear=false,
kwargs...,
Expand Down
8 changes: 4 additions & 4 deletions src/api/color.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ end

# ...and defaults to the type of the input image.
function dither(
img::GenericImage{T,N}, alg::AbstractDither, arg; kwargs...
) where {T<:Pixel,N}
img::GenericImage{T}, alg::AbstractDither, arg; kwargs...
) where {T<:ColorLike}
return _colordither(T, img, alg, arg; kwargs...)
end

Expand All @@ -55,7 +55,7 @@ function _colordither(
::Type{T},
img::GenericImage,
alg::AbstractDither,
cs::AbstractVector{<:Pixel};
cs::AbstractVector{<:ColorLike};
metric::DifferenceMetric=DEFAULT_METRIC,
to_linear=false,
kwargs...,
Expand All @@ -79,7 +79,7 @@ function _colordither(
metric::DifferenceMetric=DEFAULT_METRIC,
to_linear=false,
kwargs...,
) where {T<:NumberLike}
) where {T<:ColorLike}
return _colordither(
eltype(cs), img, alg, cs; metric=metric, to_linear=to_linear, kwargs...
)
Expand Down
4 changes: 2 additions & 2 deletions src/braille.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function braille(img::GenericImage, alg::AbstractDither; kwargs...)
return braille(img, alg; kwargs...)
end
function braille(
img::GenericGrayImage,
img::GrayImage,
alg::AbstractDither;
invert::Bool=false,
to_string::Bool=false,
Expand All @@ -38,7 +38,7 @@ end
# Enable direct printing of Binary images:
braille(img::AbstractMatrix{Bool}; kwargs...) = _braille(img; kwargs...)
braille(img::BitMatrix; kwargs...) = _braille(img; kwargs...)
function braille(img::AbstractMatrix{<:BinaryGray}; kwargs...)
function braille(img::AbstractMatrix{<:AbstractGray{Bool}}; kwargs...)
return _braille(channelview(img); kwargs...)
end

Expand Down
7 changes: 5 additions & 2 deletions src/closest_color.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ Technically this not a dithering algorithm as the quatization error is not "rand
"""
struct ClosestColor <: AbstractDither end

function binarydither!(::ClosestColor, out::GenericGrayImage, img::GenericGrayImage)
function binarydither!(::ClosestColor, out::GrayImage, img::GrayImage)
threshold = eltype(img)(0.5)
return out .= img .> threshold
end

function colordither(
::ClosestColor, img::GenericImage, cs::AbstractVector{<:Pixel}, metric::DifferenceMetric
::ClosestColor,
img::GenericImage,
cs::AbstractVector{<:ColorLike},
metric::DifferenceMetric,
)::Matrix{Int}
cs_lab = Lab.(cs)
return map(px -> _closest_color_idx(px, cs_lab, metric), img)
Expand Down
7 changes: 4 additions & 3 deletions src/color_picker.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
abstract type AbstractColorPicker end

(l::AbstractColorPicker)(c::Colorant) = get_closest_color_index(l, c)

"""
Expand All @@ -9,14 +10,14 @@ Select closest color in `colorscheme` during runtime.
Used by default if `dither` is called without a color picker.
"""
struct RuntimeColorPicker{M<:DifferenceMetric,T::Pixel} <: AbstractColorPicker
struct RuntimeColorPicker{M<:DifferenceMetric,T<:ColorLike} <: AbstractColorPicker
metric::M
colorscheme::Vector{T}

function RuntimeColorPicker(colorscheme, metric::DifferenceMetric)
function RuntimeColorPicker(colorscheme, metric::M) where {M<:DifferenceMetric}
T = colorspace(metric)
colorscheme = convert.(T, colorscheme)
return new{typeof{M},T}(metric, colorscheme)
return new{M,T}(metric, colorscheme)
end
end

Expand Down
4 changes: 2 additions & 2 deletions src/error_diffusion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function inner_range(img, alg::ErrorDiffusion)
end

function binarydither!(
alg::ErrorDiffusion, out::GenericGrayImage, img::GenericGrayImage; clamp_error=true
alg::ErrorDiffusion, out::GrayImage, img::GrayImage; clamp_error=true
)
# This function does not yet support OffsetArray
require_one_based_indexing(img)
Expand Down Expand Up @@ -95,7 +95,7 @@ end
function colordither(
alg::ErrorDiffusion,
img::GenericImage,
cs::AbstractVector{<:Pixel},
cs::AbstractVector{<:ColorLike},
metric::DifferenceMetric;
clamp_error=true,
)
Expand Down
4 changes: 2 additions & 2 deletions src/ordered.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct OrderedDither{I<:Integer,M<:AbstractMatrix{<:I},R<:Real} <: AbstractDithe
end
end

function binarydither!(alg::OrderedDither, out::GenericGrayImage, img::GenericGrayImage)
function binarydither!(alg::OrderedDither, out::GrayImage, img::GrayImage)
# eagerly promote to the same eltype to make for-loop faster
FT = floattype(eltype(img))
mat = FT.(alg.mat / alg.max)
Expand Down Expand Up @@ -55,7 +55,7 @@ end
function colordither(
alg::OrderedDither,
img::GenericImage,
cs::AbstractVector{<:Pixel},
cs::AbstractVector{<:ColorLike},
metric::DifferenceMetric,
)
cs_lab = Lab.(cs)
Expand Down
4 changes: 2 additions & 2 deletions src/threshold.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Use white noise as a threshold map.
"""
struct WhiteNoiseThreshold <: AbstractThresholdDither end

function binarydither!(::WhiteNoiseThreshold, out::GenericGrayImage, img::GenericGrayImage)
function binarydither!(::WhiteNoiseThreshold, out::GrayImage, img::GrayImage)
tmap = rand(eltype(img), size(img))
return out .= img .> tmap
end
Expand All @@ -28,7 +28,7 @@ struct ConstantThreshold{T<:Real} <: AbstractThresholdDither
end
end

function binarydither!(alg::ConstantThreshold, out::GenericGrayImage, img::GenericGrayImage)
function binarydither!(alg::ConstantThreshold, out::GrayImage, img::GrayImage)
threshold = eltype(img)(alg.threshold)
return out .= img .> threshold
end

0 comments on commit 8d09bf2

Please sign in to comment.