Skip to content

Commit

Permalink
Merge 4b2a7e6 into 9a3483d
Browse files Browse the repository at this point in the history
  • Loading branch information
aquatiko committed Aug 20, 2019
2 parents 9a3483d + 4b2a7e6 commit 7b19c9f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
1 change: 1 addition & 0 deletions Project.toml
Expand Up @@ -12,6 +12,7 @@ Interact = "c601a237-2ae4-5e1e-952c-7a85b0c7eef1"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
Reproject = "d1dcc2e6-806e-11e9-2897-3f99785db2ae"
WCS = "15f3aee2-9e10-537f-b834-a6fb8bdb944d"
MappedArrays = "dbb5928d-eab1-5f90-85c2-b9b0edb7c900"

[compat]
julia = "^1.0.0"
Expand Down
45 changes: 36 additions & 9 deletions src/AstroImages.jl
Expand Up @@ -2,7 +2,7 @@ __precompile__()

module AstroImages

using FITSIO, FileIO, Images, Interact, Reproject, WCS
using FITSIO, FileIO, Images, Interact, Reproject, WCS, MappedArrays

export load, AstroImage, ccd2rgb

Expand Down Expand Up @@ -85,9 +85,21 @@ for n in (8, 16, 32, 64)
end
end

struct AstroImage{T<:Real,C<:Color, N}
mutable struct Properties{P <: Union{AbstractFloat, FixedPoint}}
rgb_image::MappedArrays.MultiMappedArray{RGB{P},2,Tuple{Array{P,2},Array{P,2},Array{P,2}},Type{RGB{P}},typeof(ImageCore.extractchannels)}
function Properties{P}(;kvs...) where P
obj = new{P}()
for (k,v) in kvs
setproperty!(obj, k, v)
end
return obj
end
end

struct AstroImage{T<:Real,C<:Color, N, P}
data::NTuple{N, Matrix{T}}
wcs::NTuple{N, WCSTransform}
property::Properties{P}
end

"""
Expand All @@ -97,13 +109,26 @@ end
Construct an `AstroImage` object of `data`, using `color` as color map, `Gray` by default.
"""
AstroImage(color::Type{<:Color}, data::Matrix{T}, wcs::WCSTransform) where {T<:Real} =
AstroImage{T,color, 1}((data,), (wcs,))
AstroImage(color::Type{<:Color}, data::NTuple{N, Matrix{T}}, wcs::NTuple{N, WCSTransform}) where {T<:Real, N} =
AstroImage{T,color, N}(data, wcs)
AstroImage(data::Matrix{T}) where {T<:Real} = AstroImage{T,Gray,1}((data,), (WCSTransform(2),))
AstroImage(data::NTuple{N, Matrix{T}}) where {T<:Real, N} = AstroImage{T,Gray,N}(data, ntuple(i-> WCSTransform(2), N))
AstroImage(data::Matrix{T}, wcs::WCSTransform) where {T<:Real} = AstroImage{T,Gray,1}((data,), (wcs,))
AstroImage(data::NTuple{N, Matrix{T}}, wcs::NTuple{N, WCSTransform}) where {T<:Real, N} = AstroImage{T,Gray,N}(data, wcs)
AstroImage{T,color, 1, Float64}((data,), (wcs,), Properties{Float64}())
function AstroImage(color::Type{<:AbstractRGB}, data::NTuple{N, Matrix{T}}, wcs::NTuple{N, WCSTransform}) where {T <: Union{AbstractFloat, FixedPoint}, N}
if N == 3
img = ccd2rgb((data[1], wcs[1]),(data[2], wcs[2]),(data[3], wcs[3]))
return AstroImage{T,color,N, widen(T)}(data, wcs, Properties{widen(T)}(rgb_image = img))
end
end
function AstroImage(color::Type{<:AbstractRGB}, data::NTuple{N, Matrix{T}}, wcs::NTuple{N, WCSTransform}) where {T<:Real, N}
if N == 3
img = ccd2rgb((data[1], wcs[1]),(data[2], wcs[2]),(data[3], wcs[3]))
return AstroImage{T,color,N, Float64}(data, wcs, Properties{Float64}(rgb_image = img))
end
end
function AstroImage(color::Type{<:Color}, data::NTuple{N, Matrix{T}}, wcs::NTuple{N, WCSTransform}) where {T<:Real, N}
return AstroImage{T,color, N, Float64}(data, wcs, Properties{Float64}())
end
AstroImage(data::Matrix{T}) where {T<:Real} = AstroImage{T,Gray,1, Float64}((data,), (WCSTransform(2),), Properties{Float64}())
AstroImage(data::NTuple{N, Matrix{T}}) where {T<:Real, N} = AstroImage{T,Gray,N, Float64}(data, ntuple(i-> WCSTransform(2), N), Properties{Float64}())
AstroImage(data::Matrix{T}, wcs::WCSTransform) where {T<:Real} = AstroImage{T,Gray,1, Float64}((data,), (wcs,), Properties{Float64}())
AstroImage(data::NTuple{N, Matrix{T}}, wcs::NTuple{N, WCSTransform}) where {T<:Real, N} = AstroImage{T,Gray,N, Float64}(data, wcs, Properties{Float64}())

"""
AstroImage([color=Gray,] filename::String, n::Int=1)
Expand All @@ -130,6 +155,8 @@ AstroImage(color::Type{<:Color}, fits::NTuple{N, FITS}, ext::NTuple{N, Int}) whe

AstroImage(files::NTuple{N,String}) where {N} =
AstroImage(Gray, load(files)...)
AstroImage(color::Type{<:Color}, files::NTuple{N,String}) where {N} =
AstroImage(color, load(files)...)
AstroImage(file::String) = AstroImage((file,))

# Lazily render the image as a Matrix{Color}, upon request.
Expand Down
7 changes: 7 additions & 0 deletions test/ccd2rgb.jl
Expand Up @@ -44,4 +44,11 @@ end
@test isapprox(red.(asinh_res), red.(asinh_ans), nans = true, rtol = 3e-5)
@test isapprox(blue.(asinh_res), blue.(asinh_ans), nans = true, rtol = 3e-5)
@test isapprox(green.(asinh_res), green.(asinh_ans), nans = true, rtol = 3e-5)

@testset "AstroImage using ccd2rgb" begin
img = AstroImage(RGB, (joinpath("data","casa_0.5-1.5keV.fits"), joinpath("data","casa_1.5-3.0keV.fits"),
joinpath("data","casa_4.0-6.0keV.fits")))

@test RGB.(img.property.rgb_image) isa Array{RGB{Float64},2}
end
end

0 comments on commit 7b19c9f

Please sign in to comment.