Skip to content

Commit

Permalink
Fix raw and separate for general AbstractArrays (incl. Overlays). Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Apr 6, 2016
1 parent 387848a commit 01801c6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
43 changes: 22 additions & 21 deletions src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -309,18 +309,10 @@ typeof( data(img) ) # return Array{UFixed{UInt8,8},2}
typeof( raw(img) ) # returns Array{UInt8,2}
```
"""
function raw(img::AbstractArray)
elemType = eltype(eltype(img))

if (elemType <: Union{}) # weird fallback case
data(img)
elseif elemType <: FixedPointNumbers.UFixed
reinterpret( FixedPointNumbers.rawtype(elemType), data(img) )
else
data(img)
end
end

raw(img::AbstractArray) = _raw(data(img), eltype(eltype(img)))
_raw{T<:UFixed}(A::Array, ::Type{T}) = reinterpret(FixedPointNumbers.rawtype(T), A)
_raw{T}(A::Array, ::Type{T}) = A
_raw{T}(A::AbstractArray, ::Type{T}) = _raw(convert(Array, A), T)

## convert
# Implementations safe for under-specified color types
Expand Down Expand Up @@ -380,7 +372,7 @@ function convert{T,N}(::Type{Array{T,N}}, img::AbstractImageDirect{T,N})
return permutedims(dat, p)
end
end
convert(::Type{Array}, img::AbstractImage) = convert(Array{eltype(img)}, img)
convert{T,N}(::Type{Array{T,N}}, img::AbstractArray) = copy!(Array{T}(size(img)), img)

convert{C<:Colorant}(::Type{Image{C}}, img::Image{C}) = img
convert{Cdest<:Colorant,Csrc<:Colorant}(::Type{Image{Cdest}}, img::Image{Csrc}) =
Expand All @@ -399,23 +391,32 @@ example returning an `m-by-n-by-3` array from an `m-by-n` array of
"""
function separate{CV<:Colorant}(img::AbstractImage{CV})
p = permutation_canonical(img)
A = _separate(data(img), p)
so = spatialorder(img)[p]
T = eltype(CV)
if n_elts(CV) > 1
A = permutedims(reinterpret(T, data(img), tuple(n_elts(CV), size(img)...)), [p+1;1])
else
A = permutedims(reinterpret(T, data(img), size(img)), p)
end
props = copy(properties(img))
props["colorspace"] = colorspace(img)
props["colordim"] = ndims(A)
props["spatialorder"] = so
Image(A, props)
end
function separate{CV<:Colorant}(A::AbstractArray{CV})
function _separate{CV}(A::Array{CV}, p)
T = eltype(CV)
permutedims(reinterpret(T, A, tuple(n_elts(CV), size(A)...)), [2:ndims(A)+1;1])
if n_elts(CV) > 1
permutedims(reinterpret(T, A, tuple(n_elts(CV), size(A)...)), [p+1;1])
else
permutedims(reinterpret(T, A, size(A)), p)
end
end
_separate{CV}(A::AbstractArray{CV}, p) = _separate(convert(Array, A), p)
function separate{CV<:Colorant}(A::Array{CV})
T = eltype(CV)
if n_elts(CV) > 1
permutedims(reinterpret(T, A, tuple(n_elts(CV), size(A)...)), [2:ndims(A)+1;1])
else
reinterpret(T, A, size(A))
end
end
separate{CV<:Colorant}(A::AbstractArray{CV}) = separate(convert(Array, A))
separate(A::AbstractArray) = A

# Image{Numbers} -> Image{Colorant} (the opposite of separate)
Expand Down
8 changes: 8 additions & 0 deletions test/overlays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ facts("Overlay") do
@fact size(ovr, 1) --> 5
@fact size(ovr, 2) --> 1
@fact nchannels(ovr) --> 2
@fact raw(ovr) --> [0x00 0x80 0xff 0xff 0xff;
0x00 0x00 0x00 0x00 0x00;
0x00 0x80 0xff 0xff 0xff]
@fact separate(ovr) --> UFixed8[0 0 0;
0.5 0 0.5;
1 0 1;
1 0 1;
1 0 1]
iob = IOBuffer()
show(iob, ovr) # exercise only
end
Expand Down

0 comments on commit 01801c6

Please sign in to comment.