Skip to content

Commit

Permalink
add support for writing Normed{T,N} data (#182)
Browse files Browse the repository at this point in the history
* add support for writing Normed{T,N} data

* rename functions
  • Loading branch information
chrstphrbrns authored Jan 23, 2025
1 parent 8cd9370 commit 4ffd442
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TiffImages"
uuid = "731e570b-9d59-4bfa-96dc-6df516fadf69"
authors = ["Tamas Nagy <github@tamasnagy.com>"]
version = "0.11.2"
version = "0.11.3"

[deps]
ColorTypes = "3da002f7-5984-5a60-b8a6-cbb66c0b333f"
Expand Down
18 changes: 9 additions & 9 deletions src/ifds.jl
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ function Base.read!(target::AbstractArray{T, N}, tf::TiffFile{O, S}, ifd::IFD{O}
sz = uncompressed_size(ifd, cls, rws)
read!(tfs, view(reinterpret(UInt8, vec(arr)), 1:sz), comp)
if is_irregular_bps(ifd)
arr .= recode(arr, rws, cls, bps)
arr .= unpack_integers(arr, rws, cls, bps)
end
reverse_prediction!(tfs.ifd, arr)
end
Expand Down Expand Up @@ -543,24 +543,24 @@ end
end
end

recode(v::AbstractVector, n::Integer) = recode(v, 1, length(v), n)
unpack_integers(v::AbstractVector, n::Integer) = unpack_integers(v, 1, length(v), n)

recode(v::AbstractVector, c::Integer, n::Integer) = recode(v, fld(length(v), c), c, n)
unpack_integers(v::AbstractVector, c::Integer, n::Integer) = unpack_integers(v, fld(length(v), c), c, n)

# recode `r` rows of `c` n-bit integers to useful word-sized integers
recode(v::AbstractVector, r, c, n::Integer) = recode(v, r, c, Val(n))
# unpack_integers `r` rows of `c` n-bit integers to useful word-sized integers
unpack_integers(v::AbstractVector, r, c, n::Integer) = unpack_integers(v, r, c, Val(n))

# for SIMD, must have (c % M) == 0 && (M * N) % (K * 8) == 0, where M is
# the vector width used by the SIMD algorithm; M = 32 doesn't work for
# {27, 29, 31} (K = 8), so we step up to the next power of two
for N in (27, 29, 31)
@eval recode(v::AbstractVector, r, c, n::Val{$N}) = c % 64 == 0 ? recode_simd(v, n) : recode_slow(v, r, c, $N)
@eval unpack_integers(v::AbstractVector, r, c, n::Val{$N}) = c % 64 == 0 ? unpack_integers_simd(v, n) : unpack_integers_slow(v, r, c, $N)
end

recode(v::AbstractVector, r, c, n::Val{N}) where N = c % 32 == 0 ? recode_simd(v, n) : recode_slow(v, r, c, N)
unpack_integers(v::AbstractVector, r, c, n::Val{N}) where N = c % 32 == 0 ? unpack_integers_simd(v, n) : unpack_integers_slow(v, r, c, N)

# {AAA, ABB, BBC, CCC} => {AAAA, BBBB, CCCC}
function recode_slow(v::AbstractVector{T}, rows::Integer, columns::Integer, n::Integer) where T
function unpack_integers_slow(v::AbstractVector{T}, rows::Integer, columns::Integer, n::Integer) where T
@debug "recoding from $n bits per sample"

vb::Vector{UInt8} = reinterpret(UInt8, vec(v))
Expand Down Expand Up @@ -589,7 +589,7 @@ end
const nice_n = filter(x -> !(max(nextpow(2, x), 8) - x in [0,1,2,3,5]), 1:31)

# {AAA, ABB, BBC, CCC} => {AAAA, BBBB, CCCC}
@generated function recode_simd(A::AbstractVector{T}, n::Val{N}) where {T, N}
@generated function unpack_integers_simd(A::AbstractVector{T}, n::Val{N}) where {T, N}
nice = N in nice_n
TT = nice ? T : widen(T)
width = max(32, sizeof(TT) * 8) # SIMD vector width
Expand Down
56 changes: 55 additions & 1 deletion src/types/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,15 @@ Base.write(io::IOStream, img::DenseTaggedImage) = write(getstream(format"TIFF",
function _writeslice(pagecache, tf::TiffFile{O, S}, slice, ifd, prev_ifd_record) where {O, S}
data_pos = position(tf.io) # start of data
plain_data_view = reshape(PermutedDimsArray(slice, (2, 1)), :)
pagecache .= reinterpret(UInt8, plain_data_view)

if is_irregular_bps(ifd)
temp = collect(reinterpret(eltype(interpretation(ifd)), plain_data_view))
columns = ncols(ifd) * nsamples(ifd)
pagecache = pack_integers(temp, columns)
else
pagecache .= reinterpret(UInt8, plain_data_view)
end

write(tf, pagecache) # write data
ifd_pos = position(tf.io)

Expand Down Expand Up @@ -211,3 +219,49 @@ function save(filepath::String, data)
end

Base.push!(A::DenseTaggedImage{T, N, O, AA}, data) where {T, N, O, AA} = error("push! is only supported for memory mapped images. See `LazyBufferedTIFF`.")

pack_integers(A::AbstractVector{<: Normed{T, N}}, columns::Int) where {T, N} = pack_integers(reinterpret(T, A), columns, N)

# {AAXX, BBXX, CCXX} => {AAB, BCC}
function pack_integers(A::AbstractVector{T}, columns::Int, N::Int) where T <: Unsigned
if N == 8 || N == 16 || N == 32 || N == 64
return reinterpret(UInt8, A)
end

@debug "encoding data"

rows = cld(length(A), columns)
out::Vector{UInt8} = Vector{UInt8}(undef, rows * cld(columns * N, 8))
out_index = 1
in_index = 1
mask = (T(1) << N) - 1
in_length = length(A)
for _ in 1:rows
buffer::UInt64 = 0
bitcount = 0
values = 0
while values < columns
while bitcount < 8 && in_index <= in_length && values < columns
buffer = Base.shl_int(buffer, N)
@inbounds buffer = buffer | (A[in_index] & mask)
bitcount += N
in_index += 1
values += 1
if values == columns && bitcount % 8 != 0
buffer = buffer << (8 - (bitcount % 8))
bitcount = cld(bitcount, 8) * 8
end
end

while bitcount >= 8
out[out_index] = convert(UInt8, Base.lshr_int(buffer, bitcount - 8) & 0xff)
bitcount -= 8
out_index += 1
end
end

@assert bitcount == 0
end

out
end
20 changes: 10 additions & 10 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -286,37 +286,37 @@ end
xs = [rand(UInt8) & 0x7f for _ in 1:160];
bytes = parse.(UInt8, partition(reduce(*,string.(xs; base=2, pad=7)),8); base=2)
resize!(bytes, fld(length(bytes) * 8, 7))
recoded = TiffImages.recode_simd(bytes, Val(7))
unpacked = TiffImages.unpack_integers_simd(bytes, Val(7))

@test xs == recoded
@test xs == unpacked

xs = [rand(UInt32) & 0x1fffffff for _ in 1:256];
bytes = parse.(UInt8, partition(reduce(*,string.(xs; base=2, pad=29)),8); base=2)
resize!(bytes, fld(length(bytes) * 32, 29))
recoded = TiffImages.recode_simd(reinterpret(UInt32, bytes), Val(29))
unpacked = TiffImages.unpack_integers_simd(reinterpret(UInt32, bytes), Val(29))

@test xs == recoded
@test xs == unpacked

xs = [rand(UInt8) & 0x0f for _ in 1:128];
bytes = parse.(UInt8, partition(reduce(*,string.(xs; base=2, pad=4)),8); base=2)
resize!(bytes, fld(length(bytes) * 8, 4))
recoded = TiffImages.recode_simd(bytes, Val(4))
unpacked = TiffImages.unpack_integers_simd(bytes, Val(4))

@test xs == recoded
@test xs == unpacked

xs = [rand(UInt16) & 0x1ff for _ in 1:124];
bytes = parse.(UInt8, partition(rpad(reduce(*,string.(xs; base=2, pad=9)), cld(124 * 9, 8) * 8, '0'),8); base=2)
resize!(bytes, fld(length(bytes) * 16, 9))
recoded = TiffImages.recode_slow(reinterpret(UInt16, bytes), 1, 124, 9)
unpacked = TiffImages.unpack_integers_slow(reinterpret(UInt16, bytes), 1, 124, 9)

@test xs == recoded
@test xs == unpacked

xs = [rand(UInt32) & 0x7ffffff for _ in 1:124];
bytes = parse.(UInt8, partition(rpad(reduce(*,string.(xs; base=2, pad=27)), cld(124 * 27, 8) * 8, '0'),8); base=2)
resize!(bytes, fld(length(bytes) * 32, 27))
recoded = TiffImages.recode(reinterpret(UInt32, bytes), 27)
unpacked = TiffImages.unpack_integers(reinterpret(UInt32, bytes), 27)

@test xs == recoded
@test xs == unpacked
end

@testset "predictor == 3" begin
Expand Down
16 changes: 16 additions & 0 deletions test/writer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,19 @@ end
img3 = TiffImages.load(path)
@test occursin("test;", ifds(img3)[TiffImages.SOFTWARE].data)
end

@testset "DenseTaggedImage with unusual bit depth (#181)" begin
filepath = tempname()
for C in (Gray, RGB)
for T in (N0f8, N3f5, N4f12, N13f19, N12f52)
for size in (1,10,100,567)
data = C{T}.(rand(size, size))
TiffImages.save(filepath, data)

data_saveload = TiffImages.load(filepath)

@test data_saveload.data == data
end
end
end
end

2 comments on commit 4ffd442

@tlnagy
Copy link
Owner

@tlnagy tlnagy commented on 4ffd442 Jan 24, 2025

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/123647

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.11.3 -m "<description of version>" 4ffd4426d45b9e8a4c4948e93a05256c80bd9da6
git push origin v0.11.3

Please sign in to comment.