Skip to content

Commit

Permalink
Merge pull request #9 from JuliaIO/ARGB_support_fix
Browse files Browse the repository at this point in the history
Argb support fix
  • Loading branch information
IanButterworth authored Mar 17, 2020
2 parents 69d5cbc + 465cd86 commit f22be62
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 39 deletions.
9 changes: 4 additions & 5 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ function save(
filters,
compression_level,
compression_strategy,
gamma,
typeof(image)
)

Expand Down Expand Up @@ -271,13 +270,13 @@ _prepare_buffer(x::AbstractMatrix{<:T}) where {T<:RGB{<:AbstractFloat}} =
_prepare_buffer(x::AbstractMatrix{<:T}) where {T<:RGBA{<:AbstractFloat}} =
convert(Array{RGBA{N0f8}}, x)
_prepare_buffer(x::AbstractMatrix{<:T}) where {T<:BGR{<:AbstractFloat}} =
convert(Array{RGB{N0f8}}, x)
convert(Array{BGR{N0f8}}, x)
_prepare_buffer(x::AbstractMatrix{<:T}) where {T<:BGRA{<:AbstractFloat}} =
convert(Array{RGBA{N0f8}}, x)
convert(Array{BGRA{N0f8}}, x)
_prepare_buffer(x::AbstractMatrix{<:T}) where {T<:ARGB{<:AbstractFloat}} =
convert(Array{RGBA{N0f8}}, x)
convert(Array{ARGB{N0f8}}, x)
_prepare_buffer(x::AbstractMatrix{<:T}) where {T<:ABGR{<:AbstractFloat}} =
convert(Array{RGBA{N0f8}}, x)
convert(Array{ABGR{N0f8}}, x)
_prepare_buffer(x::AbstractMatrix{<:T}) where {T<:Union{AbstractFloat,Bool}} =
reinterpret(Gray{N0f8}, convert(Array{N0f8}, x))
_prepare_buffer(x::AbstractArray{T,3}) where {T<:Union{AbstractFloat,Bool}} =
Expand Down
42 changes: 26 additions & 16 deletions test/test_synthetic_images.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,20 @@ synth_imgs = [
"GrayA" => rand(GrayA, 127, 257),
"RGB" => rand(RGB, 127, 257),
"RGBA" => rand(RGBA, 127, 257),
"GrayN0f8" => rand(Gray{N0f8}, 127, 257),
"GrayAN0f8" => rand(GrayA{N0f8}, 127, 257),
"RGBN0f8" => rand(RGB{N0f8}, 127, 257),
"RGBAN0f8" => rand(RGBA{N0f8}, 127, 257),
"GrayN0f16" => rand(Gray{N0f16}, 127, 257),
"GrayAN0f16" => rand(GrayA{N0f16}, 127, 257),
"RGBN0f16" => rand(RGB{N0f16}, 127, 257),
"RGBAN0f16" => rand(RGBA{N0f16}, 127, 257),
"Gray-N0f8" => rand(Gray{N0f8}, 127, 257),
"GrayA-N0f8" => rand(GrayA{N0f8}, 127, 257),
"RGB-N0f8" => rand(RGB{N0f8}, 127, 257),
"RGBA-N0f8" => rand(RGBA{N0f8}, 127, 257),
"Gray-N0f16" => rand(Gray{N0f16}, 127, 257),
"GrayA-N0f16" => rand(GrayA{N0f16}, 127, 257),
"RGB-N0f16" => rand(RGB{N0f16}, 127, 257),
"RGBA-N0f16" => rand(RGBA{N0f16}, 127, 257),
"Gray-N0f8" => rand(Gray{N0f8}, 127, 257),
"GrayA-N0f8" => rand(GrayA{N0f8}, 127, 257),
"Gray-Float64" => rand(Gray{Float64}, 127, 257),
"GrayA-Float64" => rand(GrayA{Float64}, 127, 257),
"RGB-Float64" => rand(RGB{Float64}, 127, 257),
"RGBA-Float64" => rand(RGBA{Float64}, 127, 257),
]

edge_case_imgs = [
Expand All @@ -54,14 +60,18 @@ edge_case_imgs = [
("N4f12_2", x -> _convert(GrayA, N0f16, x), rand(N4f12, 127, 257, 2)),
("N4f12_3", x -> _convert(RGB, N0f16, x), rand(N4f12, 127, 257, 3)),
("N4f12_4", x -> _convert(RGBA, N0f16, x), rand(N4f12, 127, 257, 4)),
("BGRN0f8", identity, rand(BGR{N0f8}, 127, 257)),
("BGRAN0f8", identity, rand(BGRA{N0f8}, 127, 257)),
("BGRN0f16", identity, rand(BGR{N0f16}, 127, 257)),
("BGRAN0f16", identity, rand(BGRA{N0f16}, 127, 257)),
("ABGRN0f8", identity, rand(ABGR{N0f8}, 127, 257)),
("ABGRN0f16", identity, rand(ABGR{N0f16}, 127, 257)),
("ARGBN0f8", identity, rand(ARGB{N0f8}, 127, 257)),
("ARGBN0f16", identity, rand(ARGB{N0f16}, 127, 257)),
("BGR-N0f8", identity, rand(BGR{N0f8}, 127, 257)),
("BGR-N01f16", identity, rand(BGR{N0f16}, 127, 257)),
("BGR-Float64", identity, BGR{Float64}.(rand(BGR{N0f8}, 127, 257))),
("BGRA-N0f8", identity, rand(BGRA{N0f8}, 127, 257)),
("BGRA-N0f16", identity, rand(BGRA{N0f16}, 127, 257)),
("BGRA-Float64", identity, BGRA{Float64}.(rand(BGRA{N0f8}, 127, 257))),
("ABGR-N0f8", identity, rand(ABGR{N0f8}, 127, 257)),
("ABGR-N0f16", identity, rand(ABGR{N0f16}, 127, 257)),
("ABGR-Float64", identity, ABGR{Float64}.(rand(ABGR{N0f8}, 127, 257))),
("ARGB-N0f8", identity, rand(ARGB{N0f8}, 127, 257)),
("ARGB-N0f16", identity, rand(ARGB{N0f16}, 127, 257)),
("ARGB-Float64", identity, ARGB{Float64}.(rand(ARGB{N0f8}, 127, 257))),
]

@testset "synthetic images" begin
Expand Down
36 changes: 18 additions & 18 deletions test/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ function plotdiffs(p, i)
--------------|---------------|----------------------|---------------------
...
"""
hborder = hcat(
fill(RGBA{Float64}(0, 0, 0, 1), size(o, 1) + size(p, 1), 1),
fill(RGBA{Float64}(1, 1, 1, 1), size(o, 1) + size(p, 1), 1),
fill(RGBA{Float64}(0, 0, 0, 1), size(o, 1) + size(p, 1), 1),
)
_p = convert.(RGBA{Float64}, p)
__p = collect(channelview(_p))
__p[4, :, :] .= 0.0
Expand All @@ -47,6 +42,11 @@ function plotdiffs(p, i)
__i[4, :, :] .= 1.0 # make the difference in alpha be 1 so it is visible
d = collect(colorview(RGBA{Float64}, absdiff.(__p, __i)))
o = _plotdiffs(p, i)
hborder = hcat(
fill(RGBA{Float64}(0, 0, 0, 1), size(o, 1) + size(p, 1), 1),
fill(RGBA{Float64}(1, 1, 1, 1), size(o, 1) + size(p, 1), 1),
fill(RGBA{Float64}(0, 0, 0, 1), size(o, 1) + size(p, 1), 1),
)

collect(hcat(
vcat(_p, _plotchannels(p)),
Expand All @@ -59,7 +59,7 @@ function plotdiffs(p, i)
))
end

vboarder(p) = vcat(
vborder(p) = vcat(
fill(RGBA{Float64}(0, 0, 0, 1), 1, size(p, 2)),
fill(RGBA{Float64}(1, 1, 1, 1), 1, size(p, 2)),
fill(RGBA{Float64}(0, 0, 0, 1), 1, size(p, 2)),
Expand All @@ -79,37 +79,37 @@ end
function _plotdiffs(p::AbstractArray{<:RGBA}, i::AbstractArray{<:RGBA})
a = absdiff.(alpha.(i), alpha.(p))
vcat(
vboarder(p),
vborder(p),
RGBA{Float64}.(absdiff.(red.(i), red.(p)), 0, 0, 1),
vboarder(p),
vborder(p),
RGBA{Float64}.(0, absdiff.(green.(i), green.(p)), 0, 1),
vboarder(p),
vborder(p),
RGBA{Float64}.(0, 0, absdiff.(blue.(i), blue.(p)), 1),
vboarder(p),
vborder(p),
RGBA{Float64}.(a, a, a, 1),
)
end

function _plotchannels(p)
vcat(
vboarder(p),
vborder(p),
RGBA{Float64}.(red.(p), 0, 0, 1),
vboarder(p),
vborder(p),
RGBA{Float64}.(0, green.(p), 0, 1),
vboarder(p),
vborder(p),
RGBA{Float64}.(0, 0, blue.(p), 1),
)
end

function _plotchannels(p::AbstractArray{<:RGBA})
vcat(
vboarder(p),
vborder(p),
RGBA{Float64}.(red.(p), 0, 0, 1),
vboarder(p),
vborder(p),
RGBA{Float64}.(0, green.(p), 0, 1),
vboarder(p),
vborder(p),
RGBA{Float64}.(0, 0, blue.(p), 1),
vboarder(p),
vborder(p),
RGBA{Float64}.(alpha.(p), alpha.(p), alpha.(p), 1),
)
end
Expand All @@ -118,7 +118,7 @@ function _rescale(x::AbstractArray)
c = channelview(x)
m = maximum(c)
m = iszero(m) ? one(m) : m
colorview(RGBA{Float64}, c ./ m)
colorview(RGBA{Float64}, 100 * c ./ m)
end


Expand Down

0 comments on commit f22be62

Please sign in to comment.