# Datview object definition # Stefan Agamanolis, July 1996 ############################################################################ # This package defines an object that allows you to view a datfile in # an X window and copy frames or parts of frames to a global # clipboard. It only handles images where bytes are the samples. # (set dv (new-dat-viewer filename opt-clipboard opt-start-dset)) # You create a new dat viewer window by calling this function with a # filename, an optional clipboard, and an optional starting dataset. # This opens a window on the default display with the first dataset # showing, and returns a procedure which is your access to the object. # The window is programmed to accept different kinds of events, like # button and key clicks. Calling the returned procedure with no # arguments processes any events that have been queued and # returns. This way, you can loop through any other dat viewers and # other windows you have open and process events pending in each one. # The events the dat viewer object recognizes are: # right button or 'n': go to next dataset # left button or 'p': go to previous dataset # control key and right button: go forward 10 datasets # control key and left button: go back 10 datasets # middle button or 'r': redraw # 'c': copy current dataset to clipboard (along with position clicked) # '[': defines one corner of a rectangle in the image # ']': defines the other corner and copies rectangle to the clipboard # 'a': shows/hides the alpha channel if it exists # 'f': displays the current dataset number # 'd': displays the datfile's dimensions # 'q': close datfile, destroy window, and return 'q' # all other keys: return the character pressed # The following example creates a datfile viewer and processes events # inside of it until the quit signal ('q') is returned. # (set clipboard (new-image-clipboard)) # (set dv1 (new-dat-viewer "/mas/garden/eeny/wallpaper/stills/smile" clipboard)) # (while (!= (dv1) 'q') Null) # You can also initiate many of the same actions on the viewer by # calling the datfile viewer object with a single character argument: # (dv) # process pending events in the window # (dv 'n') # go to next dataset # (dv 'p') # go to previous dataset # (dv dataset) # go to that dataset number in the file # (dv 'r') # redraw # (dv 'a') # show alpha channel if available # (dv 'i') # show main image (hide alpha channel) # (dv 'b') # return the image buffer holding the current dataset # (dv 'f') # return the current dataset number # (dv 'd') # return number of datasets # (dv 'c') # return number of channels # (dv 'x') # return x size # (dv 'y') # return y size # (dv 'l') # return the clipboard # (dv 'w') # return the window handle # (dv 'm') # return the filename # (dv 'e') # return the input-image-datfile object used by the viewer # (dv 'q') # close and dispose of the viewer # (dv 'M' loc) # move viewer window to location ############################################################################ (load "image-datfile.isis") (load "xwin-helpers.isis") (set new-image-clipboard (proc initargs (local (clip handle time) (begin (set clip (if (length initargs) (image-copy (initargs 0)) Null)) (set handle [0 0]) (set time ((get-time) 0)) (proc args (switch (length args) (0 clip) (1 (switch (args 0) ('g' handle) ('t' time) (else (begin (if clip (free-image clip)) (set time ((get-time) 0)) (set clip (image-copy (args 0))))))) (2 (switch (args 0) ('s' (set handle (args 1))))))))))) # initialize with filename, clipboard, and start dataset (set new-dat-viewer (proc initargs (local (filename clipboard dfile dsets channels xsize ysize curdset curbuf showalpha event point1 point2 win setdset drawimage) (begin (set filename (initargs 0)) (set clipboard (if (> (length initargs) 1) (initargs 1) Null)) (set dfile (input-image-datfile filename)) (set dsets (dfile 'd')) (set channels (dfile 'c')) (set xsize (dfile 'x')) (set ysize (dfile 'y')) (set curdset (if (> (length initargs) 2) (initargs 2) 0)) (set curbuf (dfile)) (set showalpha False) (set event Null) (set point1 [0 0]) (set point2 [0 0]) (set win (xwin-create Null filename [100 100] [xsize ysize] [xsize ysize] dither-decimate 1)) (set setdset (proc (dset) (if (and (>= dset 0) (< dset dsets)) (if (!= 1 dsets) (begin (set curdset dset) (dfile dset curbuf) (drawimage) curdset)) (print "Dataset out of bounds!\n")))) (set drawimage (proc () (switch channels (1 (xwin-display-image win curbuf)) (2 (if showalpha (xwin-display-image win (isolate-channel 1 curbuf)) (xwin-display-image win curbuf))) (3 (xwin-display-image win curbuf)) (4 (if showalpha (xwin-display-image win (isolate-channel 3 curbuf)) (xwin-display-image win curbuf)))))) (xwin-select-events win event-actuator event-character) (drawimage) (proc args (switch (length args) (0 (while (and win (!= event-none ((set event (xwin-get-event win)) 0))) (switch (event 0) (event-actuator (if (event 2) (switch (event 1) (0 (setdset (- curdset (if ((event 4) 4) 10 1)))) (1 (drawimage)) (2 (setdset (+ curdset (if ((event 4) 4) 10 1))))))) (event-character (switch (event 1) ('q' (begin (free-image curbuf) (dfile 'q') (xwin-destroy win) (set win Null) 'q')) ('r' (drawimage)) ('c' (begin (if clipboard (begin (clipboard curbuf) (clipboard 's' (event 2)) (print "Image copied to clipboard\n"))))) ('[' (begin (if clipboard (begin (set point1 (event 2)) (print "Point 1 saved:") (display point1))))) (']' (begin (if clipboard (begin (set point2 (event 2)) (clipboard (isolate-sub-image point1 (+ [1 1] (- point2 point1)) # WRONG if neg curbuf)) (clipboard 's' (/ (abs (- point1 point2)) [2 2])) (print "Subimage copied to clipboard\n"))))) ('n' (setdset (+ curdset 1))) ('p' (setdset (- curdset 1))) ('a' (begin (set showalpha (not showalpha)) (drawimage))) ('d' (begin (print "Datfile dimensions are ") (display dsets channels ysize xsize))) ('f' (begin (print "Now showing dataset ") (display curdset))) (else (event 1))))))) (1 (cond ((character? (args 0)) (switch (args 0) ('q' (begin (free-image curbuf) (dfile 'q') (xwin-destroy win) (set win Null) 'q')) ('r' (begin (drawimage) curdset)) ('n' (setdset (+ curdset 1))) ('p' (setdset (- curdset 1))) ('a' (begin (set showalpha True) (drawimage))) ('i' (begin (set showalpha False) (drawimage))) ('b' curbuf) ('f' curdset) ('d' dsets) ('c' channels) ('x' xsize) ('y' ysize) ('l' clipboard) ('w' win) ('m' filename) ('e' dfile))) ((integer? (args 0)) (setdset (args 0))) ((list? (args 0)) (isolate-sub-image [((args 0) 0) ((args 0) 1)] (+ [1 1] [(- ((args 0) 2) ((args 0) 0)) (- ((args 0) 3) ((args 0) 1))]) curbuf)))) (2 (if (character? (args 0)) (switch (args 0) ('M' (xwin-move win (args 1)))) (isolate-sub-image (args 0) (args 1) curbuf)))))))))