Skip to content

Instantly share code, notes, and snippets.

@gregzuro
Created June 3, 2013 08:26
Show Gist options
  • Save gregzuro/5696839 to your computer and use it in GitHub Desktop.
Save gregzuro/5696839 to your computer and use it in GitHub Desktop.
Julia: display.jl button1press and bindwheel calls commented out to fix wonkiness on OSX
function display{A<:AbstractArray}(img::A; proplist...)
# Convert keyword list to dictionary
props = Dict{Symbol,Any}()
sizehint(props, length(proplist))
for (k,v) in proplist
props[k] = v
end
# Extract relevant information from the image and properties
img2 = ImageSlice2d(img, props)
imgc = ImageCanvas(cairo_format(img), props)
w = width(img2)
h = height(img2)
zmax = sizez(img2)
tmax = sizet(img2)
havecontrols = zmax > 1 || tmax > 1
# Determine the desired window size
# (the actual size may be smaller, depending on screen size)
ww, wh = rendersize(w, h, imgc.aspect_x_per_y)
whfull = wh
# if havecontrols
# btnsz, pad = Navigation.widget_size()
# whfull += btnsz[2] + 2*pad
# end
# Create the window and the canvas for displaying the image
win = Toplevel(get(props, "name", "ImageView"), ww, whfull, false)
c = Canvas(win, ww, wh)
imgc.c = c
# Place the canvas and set its resize properties
grid(c, 1, 1, sticky="nsew") # fill the edges of its cell on all 4 sides
grid_rowconfigure(win, 1, weight=1) # scale this cell when the window resizes
grid_columnconfigure(win, 1, weight=1)
# If necessary, create the navigation controls
if havecontrols
ctrls = NavigationControls()
state = NavigationState(zmax, tmax)
showframe = state -> reslice(imgc, img2, state)
fctrls = Frame(win)
grid(fctrls, 2, 1, sticky="ew") # place the controls below the image
init_navigation!(fctrls, ctrls, state, showframe)
if zmax > 1
try
# Replace the z label with the name of the z coordinate
cs = coords_spatial(img)
ilabel = setdiff(cs, [img2.xdim,img2.ydim])[1]
ilabel = find(cs .== ilabel)[1]
set_value(ctrls.textz, spatialorder(img)[ilabel]*":")
catch
end
end
# Bind mousewheel events to navigation
bindwheel(c, "Alt", (path,delta)->reslicet(imgc,img2,ctrls,state,int(delta)))
bindwheel(c, "Alt-Control", (path,delta)->reslicez(imgc,img2,ctrls,state,int(delta)))
end
# Set up the rendering
set_visible(win, true)
ctx = getgc(c) # force initialization of canvas
allocate_surface!(imgc, w, h)
# Set up the drawing callbacks
c.redraw = x -> resize(imgc, img2)
# Bind mouse clicks to zoom
# c.mouse.button1press = (c, x, y) -> rubberband_start(c, x, y, (c, bb) -> zoombb(imgc, img2, bb))
# bind(c, "<Double-Button-1>", (path,x,y)->zoom_reset(imgc, img2))
# # Bind mousewheel events to zoom
# bindwheel(c, "Control", (path,delta,x,y)->zoomwheel(imgc,img2,int(delta),int(x),int(y)), "%x %y")
# # Bind mousewheel events to pan
# bindwheel(c, "", (path,delta)->panvert(imgc,img2,int(delta)))
# bindwheel(c, "Shift", (path,delta)->panhorz(imgc,img2,int(delta)))
# render the initial state
rerender(imgc, img2)
resize(imgc, img2)
imgc, img2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment