pro mg_ximage_loadimage, pstate, new=new
compile_opt strictarr
if (keyword_set(new)) then begin
(*pstate).imageDims = size(*(*pstate).image, /dimensions)
if ((*pstate).pixmapId gt 0L) then wdelete, (*pstate).pixmapId
window, /free, /pixmap, $
xsize=(*pstate).imageDims[0], ysize=(*pstate).imageDims[1]
(*pstate).pixmapId = !d.window
endif else begin
wset, (*pstate).pixmapId
endelse
tvscl, *(*pstate).image
end
pro mg_ximage_refreshgraphics, pstate
compile_opt strictarr
if (n_elements((*pstate).image) eq 0L) then return
wset, (*pstate).winId
device, copy=[(*pstate).position, (*pstate).screenDims, 0L, 0L, (*pstate).pixmapId]
end
pro mg_ximage_setstatus, pstate, msg
compile_opt strictarr
status = widget_info((*pstate).tlb, find_by_uname='status')
widget_control, status, set_value=msg
end
pro mg_ximage_resize, pstate, x, y
compile_opt strictarr
status = widget_info((*pstate).tlb, find_by_uname='status')
draw = widget_info((*pstate).tlb, find_by_uname='draw')
(*pstate).screenDims += ([x, y] - (*pstate).tlbSize)
(*pstate).screenDims <= (*pstate).imageDims
widget_control, draw, $
scr_xsize=(*pstate).screenDims[0], $
scr_ysize=(*pstate).screenDims[1]
widget_control, status, scr_xsize=(*pstate).screenDims[0]
tlbG = widget_info((*pstate).tlb, /geometry)
(*pstate).tlbSize = [tlbG.scr_xsize, tlbG.scr_ysize]
mg_ximage_refreshgraphics, pstate
end
pro mg_ximage_open, pstate
compile_opt strictarr
filename = dialog_pickfile(dialog_parent=(*pstate).tlb, $
path=(*pstate).lastDirectory, $
get_path=path)
if (filename eq '') then return
(*pstate).lastDirectory = path
mg_ximage_openfile, pstate, filename
end
pro mg_ximage_openfile, pstate, filename
compile_opt strictarr
im = read_image(filename)
*(*pstate).image = im
*(*pstate).originalImage = im
mg_ximage_loadimage, pstate, /new
mg_ximage_refreshgraphics, pstate
mg_ximage_setstatus, pstate, string(filename, format='(%"Opened %s.")')
end
pro mg_ximage_save, pstate
compile_opt strictarr
saved = dialog_write_image(*(*pstate).image, $
dialog_parent=(*pstate).tlb, $
path=(*pstate).lastDirectory, $
options=options)
if (~saved) then return
(*pstate).lastDirectory = file_dirname(options.filename)
mg_ximage_setstatus, pstate, string(options.filename, format='(%"Saved %s.")')
end
pro mg_ximage_dooperation, pstate, uname
compile_opt strictarr
case uname of
'sobel': *(*pstate).image = sobel(*(*pstate).image)
'roberts': *(*pstate).image = roberts(*(*pstate).image)
'smooth': *(*pstate).image = smooth(*(*pstate).image, 5, /edge_truncate)
'revert': *(*pstate).image = *(*pstate).originalImage
endcase
mg_ximage_loadimage, pstate
mg_ximage_refreshgraphics, pstate
mg_ximage_setstatus, pstate, string(uname, format='(%"Performed %s operation.")')
end
pro mg_ximage_drawevent, pstate, event
compile_opt strictarr
case event.type of
3: begin
(*pstate).position = [event.x, event.y]
mg_ximage_refreshgraphics, pstate
end
4: begin
mg_ximage_refreshgraphics, pstate
end
else:
endcase
end
pro mg_ximage_event, event
compile_opt strictarr
widget_control, event.top, get_uvalue=pstate
uname = widget_info(event.id, /uname)
case uname of
'tlb': mg_ximage_resize, pstate, event.x, event.y
'open': mg_ximage_open, pstate
'save': mg_ximage_save, pstate
'exit': widget_control, event.top, /destroy
'sobel': mg_ximage_dooperation, pstate, uname
'roberts': mg_ximage_dooperation, pstate, uname
'smooth': mg_ximage_dooperation, pstate, uname
'revert': mg_ximage_dooperation, pstate, uname
'draw': mg_ximage_drawevent, pstate, event
endcase
end
pro mg_ximage_cleanup, tlb
compile_opt strictarr
widget_control, tlb, get_uvalue=pstate
wdelete, (*pstate).pixmapId
ptr_free, (*pstate).originalImage, (*pstate).image, pstate
end
pro mg_ximage, im
compile_opt strictarr
screenDims = [400, 400]
imageDims = n_elements(im) gt 0L ? size(im, /dimensions) : [400, 400]
tlb = widget_base(title='Image processing application', /column, $
mbar=menubar, uname='tlb', $
/tlb_size_events)
fileMenu = widget_button(menubar, value='File', /menu)
openItem = widget_button(filemenu, value='Open...', uname='open')
saveItem = widget_button(filemenu, value='Save...', uname='save')
exitItem = widget_button(filemenu, value='Exit', uname='exit', /separator)
operationsMenu = widget_button(menubar, value='Operations', /menu)
sobelItem = widget_button(operationsMenu, value='Sobel', uname='sobel')
robertsItem = widget_button(operationsMenu, value='Roberts', uname='roberts')
smoothItem = widget_button(operationsMenu, value='Smooth', uname='smooth')
revertItem = widget_button(operationsMenu, value='Revert to original', $
uname='revert', /separator)
draw = widget_draw(tlb, uname='draw', $
xsize=imageDims[0], ysize=imageDims[1], $
/app_scroll, retain=0, $
x_scroll_size=screenDims[0], y_scroll_size=screenDims[1], $
/viewport_events, /expose_events)
status = widget_label(tlb, value='Ready.', scr_xsize=screenDims[0], $
uname='status', /align_left)
widget_control, tlb, /realize
widget_control, draw, get_value=winId
tlbG = widget_info(tlb, /geometry)
state = { tlb: tlb, $
originalImage: ptr_new(im), $
tlbSize: [tlbG.scr_xsize, tlbG.scr_ysize], $
screenDims: screenDims, $
imageDims: imageDims, $
position: lonarr(2), $
image: ptr_new(im), $
winId: winId, $
pixmapId: -1L, $
lastDirectory: filepath('', subdir=['examples', 'data']) $
}
pstate = ptr_new(state, /no_copy)
widget_control, tlb, set_uvalue=pstate
mg_ximage_loadimage, pstate, /new
mg_ximage_refreshgraphics, pstate
widget_control, draw, set_draw_view=(*pstate).position
xmanager, 'mg_ximage', tlb, /no_block, $
event_handler='mg_ximage_event', cleanup='mg_ximage_cleanup'
end