diff --git a/Project.toml b/Project.toml index 85f4581..f869672 100644 --- a/Project.toml +++ b/Project.toml @@ -16,13 +16,13 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" [compat] AxisArrays = "0.4.3,0.4.6" -Colors = "0.12.7, 0.12.8" +Colors = "0.12.7, 0.12.8, 0.13" ImageCore = "0.8.21, 0.9, 0.10" JavaCall = "0.7.7, 0.7.8, 0.8" +NDTools = "0.4, 0.5, 0.6, 0.7" StaticArrays = "1" Unitful = "1" julia = "1, 1.6, 1.7, 1.8, 1.9, 1.10" -NDTools = "0.4, 0.5, 0.6, 0.7" [extras] JavaCall = "494afd89-becb-516b-aafa-70d2670c0337" diff --git a/src/shorthands.jl b/src/shorthands.jl index dbc7b3f..3a34e75 100644 --- a/src/shorthands.jl +++ b/src/shorthands.jl @@ -13,16 +13,16 @@ This is just a shorthand for the function `view5d`. See `view5d` for arguments d See documentation of `view5d` for explanation of the parameters. """ function vv(data, viewer=nothing; gamma=nothing, mode::DisplayMode =DisplNew, element=0, time=0, show_phase=false, keep_zero=false, name=nothing, title=nothing) -view5d(data, viewer; gamma=gamma, mode=mode, element=element, time=time, show_phase=show_phase, keep_zero=keep_zero, name=name, title=title) + view5d(data, viewer; gamma=gamma, mode=mode, element=element, time=time, show_phase=show_phase, keep_zero=keep_zero, name=name, title=title) end function display_array(arr, name, disp=vv, viewer=nothing) # AbstractArray{N,T} where {N,T} -v=disp(arr,viewer; name=name) -if isnothing(v) - v= nothing # repr(begin local value = arr end) # returns a representation (a String) -end -return v -# return "in_view5d" + v=disp(arr,viewer; name=name) + if isnothing(v) + v= nothing # repr(begin local value = arr end) # returns a representation (a String) + end + return v + # return "in_view5d" end # function display_array(ex, name, disp=vv, viewer=nothing) @@ -60,13 +60,13 @@ See documentation of `view5d` for explanation of the parameters. `elements_linked`: determines wether all elements are linked together (no indidual scaling and same color) """ function ve(data, viewer=nothing; gamma=nothing, element=0, time=0, show_phase=false, keep_zero=false, name=nothing, title=nothing, elements_linked=false) -viewer = get_viewer(viewer, ignore_nothing=true) -if isnothing(viewer) - vv(data, viewer; gamma=gamma, mode=DisplAddElement, element=element, time=time, show_phase=show_phase, keep_zero=keep_zero, name=name, title=title) -else - set_elements_linked(elements_linked, viewer) - vv(data, viewer; gamma=gamma, mode=DisplAddElement, element=element, time=time, show_phase=show_phase, keep_zero=keep_zero, name=name, title=title) -end + viewer = get_viewer(viewer, ignore_nothing=true) + if isnothing(viewer) + vv(data, viewer; gamma=gamma, mode=DisplAddElement, element=element, time=time, show_phase=show_phase, keep_zero=keep_zero, name=name, title=title) + else + set_elements_linked(elements_linked, viewer) + vv(data, viewer; gamma=gamma, mode=DisplAddElement, element=element, time=time, show_phase=show_phase, keep_zero=keep_zero, name=name, title=title) + end end """ @@ -166,7 +166,7 @@ end ## just a non-exported helper function to be used in the various macros below -function do_start(exs;mystarter=vv) +function do_start(exs; mystarter=vv) blk = Expr(:block) alt_name=nothing viewer=nothing # by default the active viewer is used diff --git a/src/viewer_core.jl b/src/viewer_core.jl index 0293731..ebad841 100644 --- a/src/viewer_core.jl +++ b/src/viewer_core.jl @@ -10,7 +10,8 @@ export set_display_size, set_active_viewer, set_properties, clear_active export get_viewer_history, close_all, hide_all, to_front_all export export_marker_lists, import_marker_lists, delete_all_marker_lists, export_markers_string, empty_marker_list export DisplayMode, DisplAddElement, DisplAddTime, DisplNew, DisplReplace -export set_view5d_default_size +export set_view5d_default_size, add_colormap +export get_active_element, get_active_time, set_position, set_colormap_no # export list_methods, java_memory # export init_layout, invalidate @@ -170,6 +171,73 @@ function set_gamma(gamma=1.0, myviewer=nothing; element=0) update_panels(myviewer) end +""" + add_colormap(mymap::Vector{RGB{T}}, myviewer=get_viewer()) where {T} +adds a colormap to the viewer. + +# Arguments: +* mymap: a colormap as a Vector of RGBs. See https://juliagraphics.github.io/Colors.jl/stable/colormapsandcolorscales/ +* myviewer: the viewer to apply this to. By default the active viewer is used. +* N: the number of colors in the colormap. Default is 255. + +For unclear reasons the use-defined colormaps do not show up in the menu, but are accessible via "c". +# Example: +```jldoctest +julia> v = @vv 2 .*(rand(10,10,10).-0.5) +julia> add_colormap(diverging_palette(0, 200, 32)) +julia> set_min_max_thresh(-1.0, 1.0, v) +``` + +""" +function add_colormap(mymap::Vector{RGB{T}}, myviewer=get_viewer()) where {T} + sz = length(mymap); + red = zeros(UInt8, sz) + green = zeros(UInt8, sz) + blue = zeros(UInt8, sz) + red .= (round(UInt8, mymap[p].r*255) for p in 1:sz) + green .= (round(UInt8, mymap[p].g*255) for p in 1:sz) + blue .= (round(UInt8, mymap[p].b*255) for p in 1:sz) + rb = reinterpret(Int8, red) + gb = reinterpret(Int8, green) + bb = reinterpret(Int8, blue) + + jbyteArr = Vector{jbyte} + jcall(myviewer, "add_colormap", Nothing, (jint, jbyteArr, jbyteArr, jbyteArr), sz, rb, gb, bb); + update_panels(myviewer) +end + +function set_colormap_no(no, elem= nothing, myviewer=get_viewer()) + if isnothing(elem) + elem=get_active_element(myviewer) + end + jcall(myviewer, "set_colormap_no", Nothing, (jint, jint), no, elem); +end + +""" + add_colormap(cmap_name="RdBu", myviewer=get_viewer(); N=255) +adds a colormap ny name to the viewer. + +# Arguments: +* cmap_name: a name that the function colormaps in the Colors package accepts. See https://juliagraphics.github.io/Colors.jl/stable/colormapsandcolorscales/ for possible names. +* myviewer: the viewer to apply this to. By default the active viewer is used. +* N: the number of colors in the colormap. Default is 255. + +For unclear reasons the use-defined colormaps do not show up in the menu, but are accessible via "c". +# Example: +```jldoctest +julia> v = @vv 2 .*(rand(10,10,10).-0.5) +julia> add_colormap("RdBu") +``` +""" +function add_colormap(cmap_name="RdBu", myviewer=get_viewer(); N=255) + mymap = colormap(cmap_name, N); + add_colormap(mymap, myviewer) + if cmap_name == "RdBu" + set_min_max_thresh(-1.0, 1.0, myviewer) + update_panels(myviewer) + end +end + """ set_time(mytime=-1, myviewer=nothing) @@ -325,6 +393,28 @@ function get_num_elements(myviewer=nothing) num_elem=jcall(myviewer, "getNumElements", jint, ()); end +""" + get_active_element(myviewer=get_viewer(myviewer)) +gets the currently active element number in the viewer + +# Arguments +* `myviewer`: the viewer to apply this to. By default the active viewer is used. +""" +function get_active_element(myviewer=get_viewer(myviewer)) + return jcall(myviewer, "get_active_element", jint, ()); +end + +""" + get_active_time(myviewer=get_viewer(myviewer)) +gets the currently active time number in the viewer + +# Arguments +* `myviewer`: the viewer to apply this to. By default the active viewer is used. +""" +function get_active_time(myviewer=get_viewer(myviewer)) + return jcall(myviewer, "get_active_time", jint, ()); +end + """ set_axis_scales_and_units(pixelsize=(1.0,1.0,1.0,1.0,1.0), value_name = "intensity",value_unit = "a.u.", @@ -1069,6 +1159,7 @@ function start_viewer(viewer, myJArr, jtype="jfloat", mode::DisplayMode = DisplN throw(ArgumentError("Added elements, the number of times $nt in the viewer need to correspond to the time dimension of this data $sizeT.")) end size3d = sizeX*sizeY*sizeZ + e_offset = get_num_elements(viewer) for e in 0:sizeE-1 dummy=jcall(viewer, command, V, (jArr, jint, jint, jint, jint, jint), myJArr[e*size3d+1:end],sizeX, sizeY, sizeZ, sizeE, sizeT); @@ -1079,7 +1170,7 @@ function start_viewer(viewer, myJArr, jtype="jfloat", mode::DisplayMode = DisplN process_keys("t",viewer) if !isnothing(name) # E = get_num_elements()-1 - set_element_name(e, name, viewer) + set_element_name(e+e_offset, name, viewer) end myviewer = viewer if !isnothing(properties) diff --git a/test/runtests.jl b/test/runtests.jl index 1a9c9a7..ff44482 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -89,3 +89,15 @@ end @test nothing == update_panels() @test nothing == close_all() end + +@testset "get functions" begin + v = view5d(rand(Float32,2,2,2,2,3)) + @test 3 == get_num_times(v) + @test 2 == get_num_elements(v) + +end + +@testset "set functions" begin + v = view5d(rand(Float32,2,2,2,2,3)) + set_colormap_no(13, v) +end \ No newline at end of file