Skip to content

Commit

Permalink
invokelatest converts (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
IanButterworth authored Jun 2, 2021
1 parent bae0852 commit 1f69855
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/ImageIO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ for FMT in (
end
@inline canonical_type(::Formatted{T}, data) where T = canonical_type(T(), data)

function enforece_canonical_type(f, data)
AT = canonical_type(f, data)
# This may not be type stable if `AT` is not a concrete type,
# but it's not an issue for `load`; it can never be type stable.

# work around the invokelatest overhead with an eager type check
if data isa AT
return data
else
# the backend might provide its own convert method
# use invokelatest to avoid world age issues
# See issue #34
return Base.invokelatest(convert, AT, data)
end
end

## PNGs

const load_locker = Threads.ReentrantLock()
Expand All @@ -33,11 +49,11 @@ end

function load(f::File{DataFormat{:PNG}}; kwargs...)
data = Base.invokelatest(checked_import(idPNGFiles).load, f.filename, kwargs...)
return convert(canonical_type(f, data), data)
return enforece_canonical_type(f, data)
end
function load(s::Stream{DataFormat{:PNG}}; kwargs...)
data = Base.invokelatest(checked_import(idPNGFiles).load, stream(s), kwargs...)
return convert(canonical_type(s, data), data)
return enforece_canonical_type(s, data)
end

function save(f::File{DataFormat{:PNG}}, image::S; kwargs...) where {T, S<:Union{AbstractMatrix, AbstractArray{T,3}}}
Expand All @@ -60,12 +76,12 @@ for NETPBMFORMAT in (:PBMBinary, :PGMBinary, :PPMBinary, :PBMText, :PGMText, :PP
@eval begin
function load(f::File{DataFormat{$(Expr(:quote,NETPBMFORMAT))}})
data = Base.invokelatest(checked_import(idNetpbm).load, f)
return convert(canonical_type(f, data), data)
return enforece_canonical_type(f, data)
end

function load(s::Stream{DataFormat{$(Expr(:quote,NETPBMFORMAT))}})
data = Base.invokelatest(checked_import(idNetpbm).load, s)
return convert(canonical_type(s, data), data)
return enforece_canonical_type(s, data)
end

function save(f::File{DataFormat{$(Expr(:quote,NETPBMFORMAT))}}, image::S; kwargs...) where {S<:AbstractMatrix}
Expand All @@ -82,11 +98,11 @@ end

function load(f::File{DataFormat{:TIFF}}; kwargs...)
data = Base.invokelatest(checked_import(idTiffImages).load, f.filename, kwargs...)
return convert(canonical_type(f, data), data)
return enforece_canonical_type(f, data)
end
function load(s::Stream{DataFormat{:TIFF}}; kwargs...)
data = Base.invokelatest(checked_import(idTiffImages).load, stream(s), kwargs...)
return convert(canonical_type(s, data), data)
return enforece_canonical_type(s, data)
end

function save(f::File{DataFormat{:TIFF}}, image::S) where {T, S<:Union{AbstractMatrix, AbstractArray{T,3}}}
Expand Down

0 comments on commit 1f69855

Please sign in to comment.