Skip to content

Commit

Permalink
handle Fixed types correctly for BPS reporting (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrstphrbrns authored Mar 26, 2024
1 parent 58afaec commit 3d2c838
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
12 changes: 6 additions & 6 deletions src/layout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ interpretation(p::PhotometricInterpretations, ::Val{EXTRASAMPLE_UNSPECIFIED}, ::
interpretation(p::PhotometricInterpretations, ::Val{EXTRASAMPLE_ASSOCALPHA}, @nospecialize(::Val)) = coloralpha(interpretation(p)), false
interpretation(p::PhotometricInterpretations, ::Val{EXTRASAMPLE_UNASSALPHA}, nsamples::Val) = interpretation(p, Val(EXTRASAMPLE_ASSOCALPHA), nsamples)

_mappedtype(::Type{T}, bpp) where {T} = T
_mappedtype(::Type{T}, bpp) where {T <: Unsigned} = Normed{T, bpp}
_mappedtype(::Type{T}, bpp) where {T <: Signed} = Fixed{T, bpp - 1}
_mappedtype(::Type{T}, bps) where {T} = T
_mappedtype(::Type{T}, bps) where {T <: Unsigned} = Normed{T, bps}
_mappedtype(::Type{T}, bps) where {T <: Signed} = Fixed{T, bps - 1}

function rawtype(ifd::IFD)
samplesperpixel = nsamples(ifd)
Expand Down Expand Up @@ -125,13 +125,13 @@ Allocate a cache for this IFD with correct type and size.
function getcache(ifd::IFD)
T = rawtype(ifd)
colortype, extras = interpretation(ifd)
bpp = bitspersample(ifd)
bps = bitspersample(ifd)
if istiled(ifd)
tile_width = tilecols(ifd)
tile_height = tilerows(ifd)
return Array{colortype{_mappedtype(T, bpp)}}(undef, cld(ncols(ifd), tile_width) * tile_width, cld(nrows(ifd), tile_height) * tile_height)
return Array{colortype{_mappedtype(T, bps)}}(undef, cld(ncols(ifd), tile_width) * tile_width, cld(nrows(ifd), tile_height) * tile_height)
else
return Array{colortype{_mappedtype(T, bpp)}}(undef, ncols(ifd), nrows(ifd))
return Array{colortype{_mappedtype(T, bps)}}(undef, ncols(ifd), nrows(ifd))
end
end

Expand Down
7 changes: 4 additions & 3 deletions src/types/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ samplesperpixel(::Type{<: Colorant{T, N}}) where {T, N} = N

bitspersample(img::AbstractArray) = bitspersample(eltype(img))
bitspersample(::Type{<: Colorant{T, N}}) where {T, N} = sizeof(T) * 8
bitspersample(::Type{<: Colorant{<: FixedPoint{T, S}, N}}) where {T, S, N} = S
bitspersample(::Type{<: Colorant{<: Normed{T, S}, N}}) where {T, S, N} = S
bitspersample(::Type{<: Colorant{<: Fixed{T, S}, N}}) where {T, S, N} = S + 1

sampleformat(img::AbstractArray) = sampleformat(eltype(img))
sampleformat(::Type{<: Colorant{T, N}}) where {T <: AbstractFloat, N} = SAMPLEFORMAT_IEEEFP
sampleformat(::Type{<: Colorant{<: FixedPoint{T, S}, N}}) where {T <: Unsigned, N, S} = SAMPLEFORMAT_UINT
sampleformat(::Type{<: Colorant{<: FixedPoint{T, S}, N}}) where {T <: Signed, N, S} = SAMPLEFORMAT_INT
sampleformat(::Type{<: Colorant{<: Normed{T, S}, N}}) where {T, N, S} = SAMPLEFORMAT_UINT
sampleformat(::Type{<: Colorant{<: Fixed{T, S}, N}}) where {T, N, S} = SAMPLEFORMAT_INT
sampleformat(::Type{<: Colorant{Complex{T}, N}}) where {T <: AbstractFloat, N} = SAMPLEFORMAT_COMPLEXIEEEFP
sampleformat(::Type{<: Colorant{Complex{T}, N}}) where {T <: Signed, N} = SAMPLEFORMAT_COMPLEXINT

Expand Down
4 changes: 2 additions & 2 deletions src/types/mmapped.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ end
function MmappedTIFF(file::TiffFile{O}, ifds::Vector{IFD{O}}) where {O <: Unsigned}
ifd = first(ifds)
T = rawtype(ifd)
bpp = bitspersample(ifd)
bps = bitspersample(ifd)
colortype, _ = interpretation(ifd)
return MmappedTIFF{colortype{_mappedtype(T, bpp)}}(file, ifds)
return MmappedTIFF{colortype{_mappedtype(T, bps)}}(file, ifds)
end

function getchunk(::Type{T}, raw::Vector{UInt8}, sz::Dims{2}, ifd::IFD) where T
Expand Down
6 changes: 6 additions & 0 deletions test/layouts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@
ifd[TiffImages.EXTRASAMPLES] = TiffImages.EXTRASAMPLE_ASSOCALPHA

@test TiffImages.interpretation(ifd) == (RGBA, false)
end

@testset "Sample Types" begin
@test TiffImages.bitspersample(RGB{Float32}) == 32
@test TiffImages.bitspersample(RGB{N1f7}) == 7
@test TiffImages.bitspersample(RGB{Q4f11}) == 12
end

2 comments on commit 3d2c838

@tlnagy
Copy link
Owner

@tlnagy tlnagy commented on 3d2c838 Mar 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/103672

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.9.1 -m "<description of version>" 3d2c8384c3ac971c2c0fc50bcd0aef596388d16f
git push origin v0.9.1

Please sign in to comment.