1
2Matrix_build_item = class
3	Menupullright "_New" "make a new matrix of some sort" {
4
5	Plain_item = class
6		Menuaction "_Plain" "make a new plain matrix widget" {
7		action = Matrix (identity_matrix 3);
8	}
9
10	Convolution_item = class
11		Menuaction "_Convolution" "make a new convolution matrix widget" {
12		action = Matrix_con 1 0 [[0, 0, 0], [0, 1, 0], [0, 0, 0]];
13	}
14
15	Recombination_item = class
16		Menuaction "_Recombination"
17			"make a new recombination matrix widget" {
18		action = Matrix_rec (identity_matrix 3);
19	}
20
21	Morphology_item = class
22		Menuaction "_Morphology" "make a new morphology matrix widget" {
23		action = Matrix_mor [[0, 0, 0], [0, 255, 0], [0, 0, 0]];
24	}
25
26	sep1 = Menuseparator;
27
28	Matrix_identity_item = class
29		Menuaction "_Identity" "make an identity matrix" {
30		action = identity (identity_matrix 5);
31
32		identity v = class
33			_result {
34			_vislevel = 3;
35
36			s = Expression "Size" (len v);
37
38			_result
39				= Matrix (identity_matrix (to_real s)), to_real s != len v;
40				= Matrix v;
41
42			Matrix_vips value scale offset filename display = identity value;
43		}
44	}
45
46	Matrix_series_item = class
47		Menuaction "_Series" "make a series" {
48		action = series (mkseries 0 1 5);
49
50		mkseries s t e
51			= transpose [[to_real s, to_real s + to_real t .. to_real e]];
52
53		series v = class
54			_result {
55			_vislevel = 3;
56
57			_s = v?0?0;
58			_t = v?1?0 - v?0?0;
59			_e = (last v)?0;
60
61			s = Expression "Start value" _s;
62			t = Expression "Step by" _t;
63			e = Expression "End value" _e;
64
65			_result
66				= Matrix (mkseries s t e),
67						to_real s != _s || to_real t != _t || to_real e != _e
68				= Matrix v;
69
70			Matrix_vips value scale offset filename display = series value;
71		}
72	}
73
74	Matrix_square_item = class
75		Menuaction "_Square" "make a square matrix" {
76		action = square (mksquare 5);
77
78		mksquare s = replicate s (take s [1, 1 ..]);
79
80		square v = class
81			_result {
82			_vislevel = 3;
83
84			s = Expression "Size" (len v);
85
86			_result
87				= Matrix_con (sum v) 0 v, len v == to_real s
88				= Matrix_con (sum m) 0 m
89			{
90				m = mksquare (to_real s);
91			}
92
93			Matrix_vips value scale offset filename display = square value;
94		}
95	}
96
97	Matrix_circular_item = class
98		Menuaction "_Circular" "make a circular matrix" {
99		action = circle (mkcircle 3);
100
101		mkcircle r
102				= map2 (map2 pyth) xes yes
103		{
104			line = [-r .. r];
105			xes = replicate (2 * r + 1) line;
106			yes = transpose xes;
107			pyth a b
108					= 1, (a**2 + b**2) ** 0.5 <= r
109					= 0;
110		}
111
112		circle v = class
113			_result {
114			_vislevel = 3;
115
116			r = Expression "Radius" ((len v - 1) / 2);
117
118			_result
119				= Matrix_con (sum v) 0 v, len v == r.expr * 2 + 1
120				= Matrix_con (sum m) 0 m
121			{
122				m = mkcircle (to_real r);
123			}
124
125			Matrix_vips value scale offset filename display = circle value;
126		}
127	}
128
129	Matrix_gaussian_item = class
130		Menuaction "_Gaussian" "make a gaussian matrix" {
131		action = class
132			_result {
133			_vislevel = 3;
134
135			s = Scale "Sigma" 0.001 10 1;
136			ma = Scale "Minimum amplitude" 0 1 0.2;
137			integer = Toggle "Integer" false;
138
139			_result
140				= fn s.value ma.value
141			{
142				fn
143					= im_gauss_imask, integer
144					= im_gauss_dmask;
145			}
146		}
147	}
148
149	Matrix_laplacian_item = class
150		Menuaction "_Laplacian" "make the Laplacian of a Gaussian matrix" {
151		action = class
152			_result {
153			_vislevel = 3;
154
155			s = Scale "Sigma" 0.001 10 1.5;
156			ma = Scale "Minimum amplitude" 0 1 0.1;
157			integer = Toggle "Integer" false;
158
159			_result
160				= fn s.value ma.value
161			{
162				fn
163					= im_log_imask, integer
164					= im_log_dmask;
165			}
166		}
167	}
168
169}
170
171Matrix_to_matrix_item = class
172	Menuaction "Con_vert to Matrix" "convert anything to a matrix" {
173	action x = to_matrix x;
174}
175
176#separator
177
178Matrix_extract_item = class
179	Menupullright "_Extract" "extract rows or columns from a matrix" {
180	Rows_item = class
181		Menuaction "_Rows" "extract rows" {
182		action x = class
183			_result {
184			_vislevel = 3;
185
186			first = Expression "Extract from row" 0;
187			number = Expression "Extract this many rows" 1;
188
189			_result
190				= map_unary process x
191			{
192				process x
193					= extract_area 0 first (get_width x) number x;
194			}
195		}
196	}
197
198	Columns_item = class
199		Menuaction "_Columns" "extract columns" {
200		action x = class
201			_result {
202			_vislevel = 3;
203
204			first = Expression "Extract from column" 0;
205			number = Expression "Extract this many columns" 1;
206
207			_result
208				= map_unary process x
209			{
210				process mat
211					= extract_area first 0 number (get_height x) x;
212			}
213		}
214	}
215
216	Area_item = class
217		Menuaction "_Area" "extract area" {
218		action x = class
219			_result {
220			_vislevel = 3;
221
222			left = Expression "First column" 0;
223			top = Expression "First row" 0;
224			width = Expression "Number of columns" 1;
225			height = Expression "Number of rows" 1;
226
227			_result
228				= map_unary process x
229			{
230				process mat
231					= extract_area left top width height x;
232			}
233		}
234	}
235
236	Diagonal_item = class
237		Menuaction "_Diagonal" "extract diagonal" {
238		action x = class
239			_result {
240			_vislevel = 3;
241
242			which = Option "Extract" [
243				"Leading Diagonal",
244				"Trailing Diagonal"
245			] 0;
246
247			_result
248				= map_unary process x
249			{
250				process mat
251					= mat.Matrix_base (map2 extr [0..] mat.value),
252						which == 0
253					= mat.Matrix_base (map2 extr
254						[mat.width - 1, mat.width - 2 .. 0] mat.value);
255				extr n l = [l?n];
256			}
257		}
258	}
259}
260
261Matrix_insert_item = class
262	Menupullright "_Insert" "insert rows or columns into a matrix" {
263	// make a new 8-bit uchar image of wxh with pixels set to p
264	// use to generate new cells
265	newim w h p
266		= image_new w h 1
267			Image_format.UCHAR Image_coding.NOCODING Image_type.B_W p 0 0;
268
269	Rows_item = class
270		Menuaction "_Rows" "insert rows" {
271		action x = class
272			_result {
273			_vislevel = 3;
274
275			first = Expression "Insert at row" 0;
276			number = Expression "Insert this many rows" 1;
277			item = Expression "Set new cells to" 0;
278
279			_result
280				= map_unary process x
281			{
282				process x
283					= foldl1 join_tb (concat [top, new, bottom])
284				{
285					top
286						= [extract_area 0 0 w f x], f > 0
287						= [];
288					new = [(if is_Matrix x then to_matrix else id)
289						(newim w number item.expr)];
290					bottom
291						= [extract_area 0 f w (h - f) x], f < h
292						= [];
293
294					f = to_real first;
295					w = get_width x;
296					h = get_height x;
297				}
298			}
299		}
300	}
301
302	Columns_item = class
303		Menuaction "_Columns" "insert columns" {
304		action x = class
305			_result {
306			_vislevel = 3;
307
308			first = Expression "Insert at column" 0;
309			number = Expression "Insert this many columns" 1;
310			item = Expression "Set new cells to" 0;
311
312			_result
313				= map_unary process x
314			{
315				process x
316					= foldl1 join_lr (concat [left, new, right])
317				{
318					left
319						= [extract_area 0 0 f h x], f > 0
320						= [];
321					new = [(if is_Matrix x then to_matrix else id)
322						(newim number h item.expr)];
323					right
324						= [extract_area f 0 (w - f) h x], f < w
325						= [];
326
327					f = to_real first;
328					w = get_width x;
329					h = get_height x;
330				}
331			}
332		}
333	}
334}
335
336Matrix_delete_item = class
337	Menupullright "_Delete" "delete rows or columns from a matrix" {
338	// remove number of items, starting at first
339	delete first number l = take (to_real first) l ++
340		drop (to_real first + to_real number) l;
341
342	Rows_item = class
343		Menuaction "_Rows" "delete rows" {
344
345		action x = class
346			_result {
347			_vislevel = 3;
348
349			first = Expression "Delete from row" 0;
350			number = Expression "Delete this many rows" 1;
351
352			_result
353				= map_unary process x
354			{
355				process x
356					= foldl1 join_tb (concat [top, bottom])
357				{
358					top
359						= [extract_area 0 0 w f x], f > 0
360						= [];
361					bottom
362						= [extract_area 0 b w (h - b) x], b < h
363						= [];
364
365					f = to_real first;
366					n = to_real number;
367					b = f + n;
368					w = get_width x;
369					h = get_height x;
370				}
371			}
372		}
373	}
374
375	Columns_item = class
376		Menuaction "_Columns" "delete columns" {
377		action x = class
378			_result {
379			_vislevel = 3;
380
381			first = Expression "Delete from column" 0;
382			number = Expression "Delete this many columns" 1;
383
384			_result
385				= map_unary process x
386			{
387				process x
388					= foldl1 join_lr (concat [left, right])
389				{
390					left
391						= [extract_area 0 0 f h x], f > 0
392						= [];
393					right
394						= [extract_area r 0 (w - r) h x], r < w
395						= [];
396
397					f = to_real first;
398					n = to_real number;
399					r = f + n;
400					w = get_width x;
401					h = get_height x;
402				}
403			}
404		}
405	}
406}
407
408Matrix_join = class
409	Menupullright "_Join" "join two matricies" {
410	Left_right_item = class
411		Menuaction "_Left to Right" "join two matricies left-right" {
412		action a b = map_binary join_lr a b;
413	}
414
415	Top_bottom_item = class
416		Menuaction "_Top to Bottom" "joiin two matricies top-bottom" {
417		action a b = map_binary join_tb a b;
418	}
419}
420
421Matrix_rotate_item = class
422	Menupullright "_Rotate" "clockwise rotation by fixed angles" {
423
424	rot90 = Image_transform_item.Rotate_item.Fixed_item.Rot90_item;
425
426	rot180 = Image_transform_item.Rotate_item.Fixed_item.Rot180_item;
427
428	rot270 = Image_transform_item.Rotate_item.Fixed_item.Rot270_item;
429
430	Matrix_rot45_item = class
431		Menuaction "_45 Degrees"
432			"45 degree rotate (square, odd-length-sides only)" {
433		action x = map_unary rot45 x;
434	}
435}
436
437Matrix_flip_item = Image_transform_item.Flip_item;
438
439Matrix_sort_item = class
440	Menuaction "_Sort" "sort by column or row" {
441	action x = class
442		_result {
443		_vislevel = 3;
444
445		o = Option (_ "Orientation") [
446			_ "Sort by column",
447			_ "Sort by row"
448		] 0;
449		i = Expression (_ "Sort on index") 0;
450		d = Option (_ "Direction") [
451			_ "Ascending",
452			_ "Descending"
453		] 0;
454
455		_result
456			= map_unary sort x
457		{
458			idx = to_real i;
459			pred a b
460				= a?idx <= b?idx, d == 0
461				= a?idx >= b?idx;
462			sort x
463				= (x.Matrix_base @ sortc pred) x.value,
464					o == 0
465				= (x.Matrix_base @ transpose @ sortc pred @ transpose) x.value;
466		}
467	}
468}
469
470#separator
471
472Matrix_invert_item = class
473	Menuaction "In_vert" "calculate inverse matrix" {
474	action x = map_unary (converse power (-1)) x;
475}
476
477Matrix_transpose_item = class
478	Menuaction "_Transpose" "swap rows and columns" {
479	action x = map_unary transpose x;
480}
481
482#separator
483
484Matrix_plot_scatter_item = class
485	Menuaction "_Plot Scatter"
486		"plot a scatter graph of a matrix of [x,y1,y2,..] coordinates" {
487	action x = class
488		_result {
489		_check_args = [
490			[x, "x", check_Matrix]
491		];
492		_vislevel = 3;
493
494		auto = Toggle "Auto Range" true;
495		xmin = Expression "X range minimum" 0;
496		xmax = Expression "X range maximum" 1;
497		ymin = Expression "Y range minimum" 0;
498		ymax = Expression "Y range maximum" 1;
499
500		_result
501			= Plot options ((x2b @ get_image @ to_image) x)
502		{
503			options
504				= [$style => Plot_style.POINT, $format => Plot_format.XYYY] ++
505					range;
506			range
507				= [], auto
508				= [$xmin => xmin.expr, $xmax => xmax.expr,
509					$ymin => ymin.expr, $ymax => ymax.expr];
510
511			// matrix to image makes a 1-band mxn image
512			// we need to put columns into bands
513			x2b im
514				= bandjoin (map extract_col [0 .. w - 1])
515			{
516				w = get_width im;
517				h = get_height im;
518				b = get_bands im;
519				extract_col x = extract_area x 0 1 h im;
520			}
521		}
522	}
523}
524
525Matrix_plot_item = Hist_plot_item;
526
527Matrix_buildlut_item = class
528	Menuaction "_Build LUT From Scatter"
529		"make a lookup table from a matrix of [x,y1,y2..] coordinates" {
530	action x = class
531		_result {
532		_check_args = [
533			[x, "x", check_Matrix]
534		];
535		_result = buildlut x;
536	}
537}
538