raised mpicbg dependency version to 0.6.1-SNAPSHOT
[trakem2.git] / test / patch.clj
blobc5466697cb38a2367a7e8f96cc2d115b0d1308c9
1 ; A set of test functions for Patch class
3 ; In fiji, load this file to the Clojure Interpter like:
4 ; (load-file (str (System/getProperty "user.dir") "/TrakEM2/test/patch.clj"))
6 ; ... and then enter its namespace with:
7 ; (in-ns 'fiji.trakem2.tests.patch)
9 ; Then just execute any of the functions.
12 (ns test.patch
13   (:import (ij.gui OvalRoi Roi ShapeRoi)
14            (ij IJ ImagePlus)
15            (ij.process ByteProcessor)
16            (ini.trakem2.display Display Patch Selection)
17            (mpicbg.trakem2.transform MovingLeastSquaresTransform)))
19 (defn punch-alpha-hole
20   "Create an alpha hole right in the middle of an image."
21   [patch]
22   (let [ip (.getImageProcessor patch)
23         w (int (.getOWidth patch))
24         h (int (.getOHeight patch))
25         mask (ByteProcessor. w h)
26         sroi (.not (ShapeRoi. (Roi. 0 0 w h))
27                    (ShapeRoi. (OvalRoi. (/ w 4) (/ h 4) (/ w 2) (/ h 2))))]
28     (doto mask
29       (.setRoi sroi)
30       (.setValue 255)
31       (.fill (.getMask mask)))
32     (.setAlphaMask patch mask)))
35 (defn punch-hole
36   "Punch an alpha hole into the selected and active Patch, if any,
37   and update its mipmaps and repaint."
38   []
39   (if-let [front (Display/getFront)]
40     (if-let [p (.getActive front)]
41       (if (isa? (class p) Patch)
42         (do
43           (punch-alpha-hole p)
44           (.updateMipmaps p)
45           (Display/repaint))
46         (println "Not a Patch:" p))
47       (println "Nothing selected"))
48     (println "No Display open")))
50 (defn redo-mipmaps
51   "Regenerate mipmaps for all selected Patch, if any."
52   []
53   (if-let [front (Display/getFront)]
54     (if-let [patches (.. front getSelection (getSelected Patch))]
55       (if (empty? patches)
56         (println "No Patch selected")
57         (do
58           (doseq [p patches]
59             (.updateMipmaps p))
60           (Display/repaint))))
61     (println "No Display open")))
63 (defn get-active
64   "Returns the active Patch, if any."
65   []
66   (if-let [front (Display/getFront)]
67     (if-let [a (.getActive front)]
68       (if (isa? (class a) Patch)
69         a
70         (println "Active is not a Patch."))
71       (println "No active!"))
72     (println "No Display open")))
74 (defn full
75   "Puts a new CoordinateTransform, a new Alpha mask and min,max values as desired."
76   [patch ip-min ip-max]
77   (punch-alpha-hole patch)
78   (.setMinAndMax patch (double ip-min) (double ip-max))
79   (let [w (int (.getOWidth patch))
80         h (int (.getOHeight patch))
81         mls (MovingLeastSquaresTransform.)]
82     ; Intrude top-left and bottom-right corners by 100,100 pixels
83     (.init mls (str "rigid 1 "
84                     "0 0 100 100 "
85                     w " " h " " (- w 100) " " (- h 100)))
86     (.setCoordinateTransform patch mls)))
88 (defmacro exec
89   "Executes the function on the active patch, if any,
90   and then updates the mipmaps and repaints.
91   For example, call:
92   (exec full 0 255)"
93   [f & args]
94   `(if-let [patch# (get-active)]
95      (do
96        (~f patch# ~@args)
97        (.updateMipmaps patch#)
98        (Display/repaint))))
101 (defn restore
102   "Restores an image min,max, removes any alpha,
103   and removes any coordinate transform."
104   [patch]
105   ; 1 - Set min and max to defaults
106   (let [type (.getType patch)]
107       (if (or
108             (== type ImagePlus/GRAY8)
109             (== type ImagePlus/COLOR_256)
110             (== type ImagePlus/COLOR_RGB))
111         (.setMinAndMax patch 0 255)
112         (let [ip (.getImageProcessor patch)]
113           (.findMinAndMax ip)
114           (.setMinAndMax patch (.getMin ip) (.getMax ip)))))
115   ; Remove alpha mask
116   (.setAlphaMask patch nil)
117   ; Remove CoordinateTransform
118   (.setCoordinateTransform patch nil))