Skip to content

Commit

Permalink
Merge pull request #72 from GenericMappingTools/truecolor-floats
Browse files Browse the repository at this point in the history
Add a truecolor method for floats.
  • Loading branch information
joa-quim authored Apr 22, 2024
2 parents ec27f34 + aedd9e0 commit f101002
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/spectral_indices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,12 @@ msavi(cube::Union{GMT.GMTimage{UInt16, 3}, AbstractArray{<:AbstractFloat, 3}};
Normalised Burn Ratio Index. Garcia 1991
NBRI = (nir - swir3) / (nir + swir3)
NBRI = (nir - swir2) / (nir + swir2)
"""
nbri(nir, swir3; kw...) = sp_indices(swir3, nir; index="NBRI", kw...)
nbri(nir, swir2; kw...) = sp_indices(swir2, nir; index="NBRI", kw...)
nbri(cube::Union{GMT.GMTimage{UInt16, 3}, AbstractArray{<:AbstractFloat, 3}};
bands::Vector{Int}=Int[], layers::Vector{Int}=Int[], bandnames::Vector{String}=String[], kw...) =
helper_si_method(cube, "NBRI"; bands=bands, layers=layers, bandnames=bandnames, defbandnames=["nir", "swir3"], kw...)
helper_si_method(cube, "NBRI"; bands=bands, layers=layers, bandnames=bandnames, defbandnames=["nir", "swir2"], kw...)

# ----------------------------------------------------------------------------------------------------------
"""
Expand Down
30 changes: 27 additions & 3 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Return an auto-stretched UInt8 RGB GMTimage
Irgb = truecolor(cube::String, [bands::Vector{Int}], [bandnames::Vector{String}], [raw=false])
Make an RGB composition of 3 bands from the `cube` file holding a UInt16 multi-layered array (often created with `cutcube`)
The band selection can be made with `bands` vector, case in which we will search for bands named "Band band[k]"
The band selection can be made with `bands` vector, case in which we will search for bands named "Band[k]"
or where the bands description contain the contents of `bandnames`. If none of `bands` or `bandnames` is used
we search for a made up `bandnames=["red", "green", "blue"]`.
Expand All @@ -75,7 +75,7 @@ Return an auto-stretched UInt8 RGB GMTimage OR a GMTimage{UInt16,3} if the `raw`
Irgb = truecolor(cube::GMTgrid, [bands|layers::Vector{Int}], [bandnames::Vector{String}], [type=UInt8])
Make an RGB composition of 3 bands from the `cube` file holding a Float32 multi-layered array.
The band selection can be made with `bands` vector, case in which we will search for bands named "Band band[k]"
The band selection can be made with `bands` vector, case in which we will search for bands named "Band[k]"
or where the bands description contain the contents of `bandnames`. If none of `bands` or `bandnames` is used
we search for a made up `bandnames=["red", "green", "blue"]`.
Expand Down Expand Up @@ -117,6 +117,29 @@ function truecolor(bndR, bndG, bndB)
Io
end

# ----------------------------------------------------------------------------------------------------------
# This method fall into the description of the general 'truecolor(bndR, bndG, bndB)'
function truecolor(bandR::T, bandG::T, bandB::T) where {T<:Union{GMTgrid{<:AbstractFloat, 2},Matrix{<:AbstractFloat}}}
@assert size(bandR) == size(bandG) == size(bandB)
n1 = length(bandR); n2 = 2 * n1; n3 = 3 * n1
img = Array{UInt8}(undef, size(bandR,1), size(bandR,2), 3)
mima = isa(bandR, GMTgrid) ? bandR.range[5:6] : GMT.entrema_nan(bndR)
mima == [0.0,1.0] ? (for k = 1:n1 img[k] = round(UInt8, bandR[k] * 255) end) :
(d = 255.0 / (mima[2] - mima[1]); for k = 1:n1 img[k] = round(UInt8, (bandR[k] - mima[1]) * d) end)

mima = isa(bandG, GMTgrid) ? bandG.range[5:6] : GMT.entrema_nan(bndG)
n = 0
mima == [0.0,1.0] ? (for k = n1+1:n2 img[k] = round(UInt8, bandG[n+=1] * 255) end) :
(d = 255.0 / (mima[2] - mima[1]); for k = n1+1:n2 img[k] = round(UInt8, (bandG[n+=1] - mima[1]) * d) end)

mima = isa(bandB, GMTgrid) ? bandB.range[5:6] : GMT.entrema_nan(bndB)
n = 0
mima == [0.0,1.0] ? (for k = n2+1:n3 img[k] = round(UInt8, bandB[n+=1] * 255) end) :
(d = 255.0 / (mima[2] - mima[1]); for k = n2+1:n3 img[k] = round(UInt8, (bandB[n+=1] - mima[1]) * d) end)

return isa(bandR, GMTgrid) ? mat2img(img, bandR) : mat2img(img)
end

truecolor(cube::GMT.GMTimage{UInt16, 3}, layers::Vector{Int}) = truecolor(cube, layers=layers)
function truecolor(cube::GMT.GMTimage{UInt16, 3}; layers::Vector{Int}=Int[])
(length(layers) != 3) && error("For an RGB composition 'bands' must be a 3 elements array and not $(length(layers))")
Expand Down Expand Up @@ -618,7 +641,7 @@ $dns_doc
# Example:
Compute the radiance TOA of Band 2 file.
```
R = dn2temperature("LC08_L1TP_204033_20210525_20210529_02_T1_B2.TIF")
R = dn2radiance("LC08_L1TP_204033_20210525_20210529_02_T1_B2.TIF")
```
"""
function dn2radiance(fname::String; band::Int=0, bandname::String="", bandnames::String="", mtl::String="", save::String="")
Expand Down Expand Up @@ -675,6 +698,7 @@ function dn2aux(fname::String, fun::String; band::Int=0, bandname::String="", ba
end
G = mat2grid(mat, G)
G.names = bdnames
G.v = collect(1:length(bdnames))
else
G = helper_fun(fname, band, bandname, bandnames, mtl)
end
Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,6 @@ G2 = slavi(R, NIR, SW2); println(" slavi 2")
G3 = slavi(cube); println(" slavi 3")
@test G1.range[5:6] == G2.range[5:6]
@test G1.range[5:6] == G3.range[5:6]

G1 = mat2grid(rand(Float32, 16,16)); G2 = mat2grid(rand(Float32, 16,16)); G3 = mat2grid(rand(Float32, 16,16));
I = truecolor(G1,G2,G3);

0 comments on commit f101002

Please sign in to comment.