Skip to content

Commit

Permalink
Add type-asserts for runtime-dispatched calls
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Feb 25, 2021
1 parent 49903ee commit 1b106b1
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/files.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ function Base.read!(file::TiffFile, arr::BitArray)
arr
end

Base.write(file::TiffFile, t) = write(file.io.io, t)
Base.write(file::TiffFile, arr::AbstractVector{Any}) = write(file.io.io, Array{UInt8}(arr))
Base.write(file::TiffFile, t) = write(file.io.io, t)::Int
Base.write(file::TiffFile, arr::AbstractVector{Any}) = write(file.io.io, Array{UInt8}(arr))::Int

Base.seek(file::TiffFile, n::Integer) = seek(file.io, n)

Expand Down
8 changes: 4 additions & 4 deletions src/ifds.jl
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ function Base.read!(target::AbstractArray{T, N}, tf::TiffFile, ifd::IFD) where {
if layout.compression != COMPRESSION_NONE
# strip_nbytes is the number of bytes pre-inflation so we need to
# calculate the expected size once decompressed and update the values
strip_nbytes = fill(rowsperstrip*layout.ncols, length(strip_nbytes))
strip_nbytes = fill(rowsperstrip*layout.ncols, length(strip_nbytes)::Int)
strip_nbytes[end] = (layout.nrows - (rowsperstrip * (nstrips-1))) * layout.ncols
end

Expand All @@ -189,13 +189,13 @@ function Base.read!(target::AbstractArray{T, N}, tf::TiffFile, ifd::IFD) where {
if nstrips > 1
startbyte = 1
for i in 1:nstrips
seek(tf, strip_offsets[i])
nbytes = Int(strip_nbytes[i] / sizeof(T))
seek(tf, strip_offsets[i]::Core.BuiltinInts)
nbytes = Int(strip_nbytes[i]::Core.BuiltinInts / sizeof(T))
read!(tf, view(target, startbyte:(startbyte+nbytes-1)), layout.compression)
startbyte += nbytes
end
else
seek(tf, strip_offsets[1])
seek(tf, strip_offsets[1]::Core.BuiltinInts)
read!(tf, target, layout.compression)
end
end
Expand Down
12 changes: 6 additions & 6 deletions src/layout.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
nrows(ifd::IFD) = Int(ifd[IMAGELENGTH].data)
ncols(ifd::IFD) = Int(ifd[IMAGEWIDTH].data)
nsamples(ifd::IFD) = Int(ifd[SAMPLESPERPIXEL].data)
nrows(ifd::IFD) = Int(ifd[IMAGELENGTH].data)::Int
ncols(ifd::IFD) = Int(ifd[IMAGEWIDTH].data)::Int
nsamples(ifd::IFD) = Int(ifd[SAMPLESPERPIXEL].data)::Int

"""
interpretation(ifd)
Expand Down Expand Up @@ -36,16 +36,16 @@ interpretation(::Val{PHOTOMETRIC_YCBCR}) = YCbCr
interpretation(::Val{PHOTOMETRIC_CIELAB}) = Lab

function interpretation(p::PhotometricInterpretations, extrasamples::ExtraSamples, samplesperpixel::Int)
interp = interpretation(p)
len = length(interp)
interp = interpretation(p)::Type{<:Colorant}
len = length(interp)::Int
if len + 1 == samplesperpixel
return interpretation(p, extrasamples, Val(samplesperpixel))
elseif len == samplesperpixel
return interp, false
elseif len < samplesperpixel
return interp, true
else
error("TIFF file says it contains $interp values, but only has $samplesperpixel samples per pixel instead of the minimum required $(length(interp))")
error("TIFF file says it contains $interp values, but only has $samplesperpixel samples per pixel instead of the minimum required $len")
end
end
_pad(::Type{RGB}) = RGBX
Expand Down
2 changes: 1 addition & 1 deletion src/load.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function load(tf::TiffFile, ifds::AbstractVector{<:IFD}, ::Nothing; verbose = tr
cache = getcache(ifd)
read!(cache, tf, ifd)

return Array(cache')
return Matrix(cache')
end

function load(tf::TiffFile, ifds::AbstractVector{<:IFD}, N; verbose = true)
Expand Down
13 changes: 7 additions & 6 deletions src/tags.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ load(tf::TiffFile{O}, t::Tag{RemoteData{O}}) where {O <: Unsigned} = _load(tf.io
function _load(io::Stream, tf::TiffFile{O}, t::Tag{RemoteData{O}}) where {O <: Unsigned}
T = t.data.datatype
N = t.data.count
rawdata = Vector{UInt8}(undef, bytes(T)*N)
nb = bytes(T)::Int
rawdata = Vector{UInt8}(undef, nb*N)

pos = position(io)
seek(io, t.data.position)
Expand All @@ -55,7 +56,7 @@ function _load(io::Stream, tf::TiffFile{O}, t::Tag{RemoteData{O}}) where {O <: U
# if this datatype is comprised of multiple bytes and this file needs to be
# bitswapped then we'll need to reverse the byte order inside each datatype
# unit
if tf.need_bswap && bytes(T) >= 2
if tf.need_bswap && nb >= 2
reverse!(rawdata)
data = Array{T}(reverse(reinterpret(T, rawdata)))
elseif T === String
Expand All @@ -71,7 +72,7 @@ function _load(io::Stream, tf::TiffFile{O}, t::Tag{RemoteData{O}}) where {O <: U
end
seek(io, pos)

Tag(t.tag, data)
Tag(t.tag, data)::Tag
end

bytes(x::Type) = sizeof(x)
Expand Down Expand Up @@ -101,9 +102,9 @@ function _read(io::Stream, tf::TiffFile{O}, ::Type{Tag}) where O <: Unsigned
if T === String
return Tag(tag, String(data))
elseif T === Any
return Tag(tag, Array{Any}(data))
return Tag(tag, Vector{Any}(data))
elseif count == 1
return Tag(tag, first(reinterpret(T, data)))
return Tag(tag, first(reinterpret(T, data)))::Tag
else
return Tag(tag, reinterpret(T, data)[1:Int(count)])
end
Expand Down Expand Up @@ -159,7 +160,7 @@ function Base.write(tf::TiffFile{O}, t::Tag{T}) where {O <: Unsigned, T}
write(tf, t.tag)
write(tf, julian_to_tiff[eltype(t)])
write(tf, O(length(t)))
nbytes = write(tf.io, data)
nbytes = write(tf.io, data)::Int

# write padding
if nbytes < sizeof(O)
Expand Down
2 changes: 1 addition & 1 deletion src/types/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function Base.write(io::Stream, img::DenseTaggedImage)
ifd[COMPRESSION] = COMPRESSION_NONE
ifd[STRIPOFFSETS] = O(data_pos)
ifd[STRIPBYTECOUNTS] = O(ifd_pos-data_pos)
ifd[SOFTWARE] = "$(parentmodule(IFD)).jl v$PKGVERSION"
ifd[SOFTWARE] = "$(parentmodule(IFD)::Module).jl v$PKGVERSION"
sort!(ifd.tags)

seek(io, ifd_pos)
Expand Down

0 comments on commit 1b106b1

Please sign in to comment.