This file is indexed.

/usr/share/nip2/start/Tasks.def is in nip2 8.2-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
Tasks_capture_item = class
	Menupullright "_Capture" 
		"useful stuff for capturing and preprocessing images" {

	Csv_import_item = class
		Menuaction "_CSV Import" "read a file of comma-separated values" {
		action = class 
			_result {
			_vislevel = 3;

			path = Pathname "File to load" "empty";
			start_line = Expression "Start at line" 1;
			rows = Expression "Lines to read (-1 for whole file)" (-1);
			whitespace = String "Whitespace characters" " \"";
			separator = String "Separator characters" ",;\t";

			_result
				= Image blank, path.value == "empty"
				= Image (im_csv2vips filename)
			{
				filename = search (expand path.value) ++ ":" ++
					"skip:" ++ print (start_line.expr - 1) ++ "," ++
					"whi:" ++ escape whitespace.value ++ "," ++
					"sep:" ++ escape separator.value ++ "," ++
					"line:" ++ print rows.expr;

				// prefix any ',' with a '\' in the separators line
				escape x 
					= foldr prefix [] x
				{
					prefix x l
						= '\\' : x : l, x == ','
						= x : l;
				}

				blank = image_new 1 1 1 
					Image_format.DOUBLE Image_coding.NOCODING Image_type.B_W 
					0 0 0;
			}
		}
	}

	Raw_import_item = class
		Menuaction "_Raw Import" "read a file of binary values" {
		action = class 
			_result {
			_vislevel = 3;

			path = Pathname "File to load" "empty";
			across = Expression "Pixels across" 100;
			down = Expression "Pixels down" 100;
			bytes = Expression "Bytes per pixel" 3;
			skip = Expression "Skip over initial bytes" 0;

			_result
				= Image blank, path.value == "empty"
				= Image (im_binfile path.value 
					across.expr down.expr bytes.expr skip.expr)
			{
				blank = image_new 1 1 1 
					Image_format.DOUBLE Image_coding.NOCODING Image_type.B_W 
					0 0 0;
			}
		}
	}

	// interpret Analyze header for layout and calibration
	Analyze7_header_item = class
		Menuaction "_Interpret Analyze 7 Header" 
			"examine the Analyze header and set layout and value" {
		action x 
			= x'''
		{
			// read bits of header
			dim n = get_header ("dsr-image_dimension.dim[" ++ print n ++ "]");
			dim0 = dim 0 x;
			dim1 = dim 1 x;
			dim2 = dim 2 x;
			dim3 = dim 3 x;
			dim4 = dim 4 x;
			dim5 = dim 5 x;
			dim6 = dim 6 x;
			dim7 = dim 7 x;
			glmax = get_header "dsr-image_dimension.glmax" x;
			cal_max = get_header "dsr-image_dimension.cal_max" x;
	
			// oops, now a nop
			x' = x;
	
			// lay out higher dimensions width-ways
			x'' 
				= grid dim2 dim3 1 x', dim0 == 3
				= grid dim2 dim3 dim4 x', dim0 == 4
				= grid (dim2 * dim4) dim5 1 (grid dim2 dim3 dim4) x', dim0 == 5
				= grid (dim2 * dim4) dim5 dim6 (grid dim2 dim3 dim4) x', dim0 == 6
				= grid (dim2 * dim4 * dim6) dim7 1 
					(grid (dim2 * dim4) dim5 dim6 (grid dim2 dim3 dim4)) x', 
						dim0 == 7
				= error (_ "unsupported dimension " ++ dim0);
	
			// multiply by scale factor to get kBeq
			x''' = x'' * (cal_max / glmax);
		}
	}

	Video_item = class 
		Menuaction "Capture _Video Frame" "capture a frame of still video" {
		// shortcut to prefs
		prefs = Workspaces.Preferences;

		action = class
			_result {
			_vislevel = 3;

			device = prefs.VIDEO_DEVICE;
			channel = Option "Input channel" [
				"TV",
				"Composite 1",
				"Composite 2",
				"Composite 3"
			] prefs.VIDEO_CHANNEL;
			b = Scale "Brightness" 0 32767 prefs.VIDEO_BRIGHTNESS;
			col = Scale "Colour" 0 32767 prefs.VIDEO_COLOUR;
			con = Scale "Contrast" 0 32767 prefs.VIDEO_CONTRAST;
			hue = Scale "Hue" 0 32767 prefs.VIDEO_HUE;
			frames = Scale "Frames to average" 0 100 prefs.VIDEO_FRAMES;
			mono = Toggle "Monochrome grab" prefs.VIDEO_MONO;
			crop = Toggle "Crop image" prefs.VIDEO_CROP;

			// grab, but hide it ... if we let the crop edit
			_raw_grab = Image (im_video_v4l1 device channel.value
				b.value col.value con.value 
				hue.value frames.value);

			edit_crop
				= Region _raw_grab left top width height
			{
				left = prefs.VIDEO_CROP_LEFT;
				top = prefs.VIDEO_CROP_TOP;
				width = min_pair 
					prefs.VIDEO_CROP_WIDTH (_raw_grab.width + left);
				height = min_pair
					prefs.VIDEO_CROP_HEIGHT (_raw_grab.height + top);
			}

			aspect_ratio = Expression "Stretch vertically by"
				prefs.VIDEO_ASPECT;

			_result 
				= frame'
			{
				frame 
					= edit_crop, crop
					= _raw_grab;

				frame' 
					= colour_transform_to Image_type.B_W frame, mono
					= frame;
			}
		}
	}

	Smooth_image_item = class 
		Menuaction "_Smooth" "remove small features from image" {
		action in = class
			_result {
			_vislevel = 3;

			feature = Scale "Minimum feature size" 1 50 20;

			_result = map_unary (smooth feature.value) in;
		}
	}

	Light_correct_item = class
		Menuaction "_Flatfield" "use white image w to flatfield image i" {
		action w i
			= map_binary wc w i
		{
			wc w i
				= clip2fmt i.format (w' * i)
			{
				fac = mean w / max w;
				w' = fac * (max w / w);
			}
		}
	}

	Image_rank_item = Filter_rank_item.Image_rank_item;

	Tilt_item = Filter_tilt_item;

	sep1 = Menuseparator;

	White_balance_item = class
		Menuaction "_White Balance" 
			"use average of small image to set white of large image" {
		action a b = class
			_result {
			_vislevel = 3;

			white_hint = "Set image white to:";
			white = Colour_picker "Lab" [100, 0, 0];

			_result
					= map_binary wb a b
			{
				wb a b
					= colour_transform_to (get_type image) image_xyz'
				{
					area x = x.width * x.height;
					larger x y = area x > area y;
					[image, patch] = sortc larger [a, b];
					to_xyz = colour_transform_to Image_type.XYZ;
			
					// white balance in XYZ
					patch_xyz = to_colour (to_xyz patch);
					white_xyz = to_xyz white;

					facs = (mean patch_xyz / mean white_xyz) * 
						(white_xyz / patch_xyz);

					image_xyz = to_xyz image;
					image_xyz' = image_xyz * facs;
				}
			}
		}
	}

	Gamma_item = Image_levels_item.Gamma_item;

	Tone_item = Image_levels_item.Tone_item;

	sep2 = Menuseparator;

	Crop_item = Image_crop_item;

	Rotate_item = Image_transform_item.Rotate_item;

	Flip_item = Image_transform_item.Flip_item;

	Resize_item = Image_transform_item.Resize_item;

	Rubber_item = Image_transform_item.Image_rubber_item;

	sep3 = Menuseparator;

	ICC_item = Colour_icc_item;

	Temp_item = Colour_temperature_item;

	Find_calib_item = class 
		Menuaction "Find _Colour Calibration" 
			"find an RGB -> XYZ transform from an image of a colour chart" {
		action image = class
			_result {
			_check_args = [
				[image, "image", check_Image]
			];
			_vislevel = 3;

			measure = Scale (_ "Measure area (%)") 1 100 50; 

			sample = measure_draw 6 4 (to_real measure) image;

			// get macbeth data file to use
			macbeth = Pathname "Pick a Macbeth data file" 
				"$VIPSHOME/share/$PACKAGE/data/macbeth_lab_d65.mat";

			mode = Option "Input LUT" [
				"Linearize from chart greyscale",
				"Fit intercept from chart greyscale",
				"Linear input, set brightness from chart",
				"Linear input"
			] 0;

			// get max of input image
			_max_value = Image_format.maxval image.format;

			// measure chart image
			_camera = measure_sample 6 4 (to_real measure) image;

			// load true values
			_true_Lab = Matrix_file macbeth.value;
			_true_XYZ = colour_transform 
				Image_type.LAB Image_type.XYZ _true_Lab;

			// get Ys of greyscale
			_true_grey_Y = map (extract 1) (drop 18 _true_XYZ.value);

			// camera greyscale (all bands)
			_camera_grey = drop 18 _camera.value;

			// normalise both to 0-1 and combine
			_camera_grey' = map (map (multiply (1 / _max_value))) _camera_grey;
			_true_grey_Y' = map (multiply (1 / 100)) _true_grey_Y;
			_comb 
				= Matrix (map2 cons _true_grey_Y' _camera_grey'), mode == 0
				= Matrix [0: intercepts, replicate (_camera.width + 1) 1], 
					mode == 1
				= Matrix [[0, 0], [1, 1]]
			{
				intercepts = [(linreg _true_grey_Y' cam).intercept :: 
					cam <- transpose _camera_grey'];
			}

			// make a linearising lut ... zero on left
			_linear_lut = im_invertlut _comb (_max_value + 1);

			// and display it
			// plot from 0 explicitly so we see the effect of mode1 (intercept
			// from greyscale)
			linearising_lut = Plot [$ymin => 0] _linear_lut;

			// map an image though the lineariser
			linear x
				= hist_map linearising_lut.value x, mode == 0 || mode == 1
				= x;

			// map the chart measurements though the lineariser
			_camera' = (to_matrix @ linear @ to_image) _camera;

			// solve for RGB -> XYZ
			// normalise: the 2nd row is what makes Y, so divide by that to
			// get Y in 0-1.
			_pinv = (transpose _camera' * _camera') ** -1;
			_full_M = transpose (_pinv * (transpose _camera' * _true_XYZ));
			M = _full_M / scale;
			scale = sum _full_M.value?1;

			// now turn the camera to LAB and calculate dE76
			_camera'' = (to_matrix @ 
				colour_transform Image_type.XYZ Image_type.LAB @ 
				recomb M @ 
				multiply scale @
				to_image) _camera';

			_dEs = map abs_vec (_camera'' - _true_Lab).value;
			avg_dE76 = mean _dEs;
			_max_dE = foldr max_pair 0 _dEs;
			_worst = index (equal _max_dE) _dEs;
			worst_patch 
				= name _worst ++ " (patch " ++ 
					print (_worst + 1) ++ ", " ++ 
					print _max_dE ++ " dE)"
			{
				name i 
					= macbeth_names?i, i >= 0 && i < len macbeth_names
					= "Unknown";
			}

			// normalise brightness ... in linear mode, we optionally don't
			// set the brightness from the Macbeth chart
			norm x 
				= x * scale, mode != 3
				= x;

			// convert RGB camera to Lab
			_result = (Image @
				colour_transform Image_type.XYZ Image_type.LAB @
				norm @
				recomb M @
				cast_float @
				linear) image.value;
		}
	}

	Apply_calib_item = class 
		Menuaction "_Apply Colour Calibration" 
			"apply an RGB -> LAB transform to an image" {
		action a b = class
			(map_binary process a b) {
			process a b
				= result, is_instanceof calib_name calib && is_Image image
				= error (_ "bad arguments to " ++ "Calibrate_image")
			{
				// the name of the calib object we need
				calib_name = "Tasks_capture_item.Find_calib_item.action";

				// get the Calibrate_chart arg first
				[image, calib] = sortc (const (is_instanceof calib_name)) 
					[a, b];

				result = (Image @
					colour_transform Image_type.XYZ Image_type.LAB @
					calib.norm @
					recomb calib.M @
					cast_float @
					calib.linear) image.value;
			}
		}
	}

	sep4 = Menuseparator;

	Graph_hist_item = Hist_find_item;

	Graph_bands_item = class
		Menuaction "Plot _Bands" "show image bands as a graph" {
		action x = class
			_result {
			_vislevel = 3;

			style = Option_enum "Style" Plot_style.names "Line";

			auto = Toggle "Auto Range" true;
			ymin = Expression "Y range minimum" 0;
			ymax = Expression "Y range maximum" 1;

			_result
				= Plot options (to_image (bands (image x))).value
			{
				options
					= [$style => style.value] ++ 
						if auto then [] else 
							[$ymin => ymin.expr, $ymax => ymax.expr]; 

				// try to make something image-like from it
				image x
					= extract_area x.left x.top 1 1 x.image, is_Mark x
					= get_image x, has_image x
					= get_image (to_image x);

				// get as [[1],[2],[3]]
				bands x
					= transpose [map mean (bandsplit x)];
			}
		}
	}
}

Tasks_mosaic_item = class
	Menupullright "_Mosaic" "build image mosaics" {
	/* Check and group a point list by image.
	 */
	mosaic_sort_test l
		= error "mosaic: not all points",
			!is_listof is_Mark l
		= error "mosaic: points not on two images",
			!is_list_len 2 images
		= error "mosaic: images do not match in format and coding",
			!all_equal (map get_format l) || !all_equal (map get_coding l)
		= error "mosaic: not same number of points on each image",
			!foldr1 equal (map len l') 
		= l'
	{
		// test for all elements of a list equal
		all_equal l = all (map (equal (hd l)) (tl l));
	
		// all the different images
		images = mkset pointer_equal (map get_image l);
	
		// find all points defined on image
		test_image image p = (get_image p) === image;
		find l image = filter (test_image image) l;
	
		// group point list by image
		l' = map (find l) images;
	}

	/* Sort a point group to get right before left, and within each group to 
	 * get above before below.
	 */
	mosaic_sort_lr l
		= l''
	{
		// sort to get upper point first
		above a b = a.top < b.top;
		l' = map (sortc above) l;
	
		// sort to get right group before left group
		right a b = a?0.left > b?0.left;
		l'' = sortc right l';
	}

	/* Sort a point group to get top before bottom, and within each group to 
	 * get left before right.
	 */
	mosaic_sort_tb l
		= l''
	{
		// sort to get upper point first
		left a b = a.left < b.left;
		l' = map (sortc left) l;
	
		// sort to get right group before left group
		below a b = a?0.top > b?0.top;
		l'' = sortc below l';
	}
	
	/* Put 'em together! Group by image, sort vertically (or horizontally) with
	 * one of the above, transpose to get pairs matched up, and flatten again.
	 */
	mosaic_sort fn = concat @ transpose @ fn @ mosaic_sort_test;

	Mosaic_1point_item = class 
		Menupullright "_One Point" "join two images with a single tie point" {
		check_ab_args a b = [
			[a, "a", check_Mark],
			[b, "b", check_Mark]
		];
	
		// shortcut to prefs
		prefs = Workspaces.Preferences;
		search_area = prefs.MOSAIC_WINDOW_SIZE;
		object_size = prefs.MOSAIC_OBJECT_SIZE;
		blend_width_widget = Scale "Maximum blend width" 0 100 prefs.MOSAIC_MAX_BLEND_WIDTH;
		refine_widget = Toggle "Refine selected tie-points" prefs.MOSAIC_REFINE;

		lr_mos _refine a b = class 
			Image _result {
			_check_args = check_ab_args a b;
	
			bw = blend_width_widget;
			refine = _refine;
	
			_result 
				= im_lrmosaic a'.image.value b'.image.value 0 
					a'.left a'.top b'.left b'.top
					(object_size / 2) (search_area / 2) 0 bw.value,
						refine
				= im_lrmerge a'.image.value b'.image.value
					(b'.left - a'.left) (b'.top - a'.top) bw.value
			{
				[a', b'] = mosaic_sort mosaic_sort_lr [a, b];
			}
		}

		tb_mos _refine a b = class
			Image _result {
			_check_args = check_ab_args a b;
	
			bw = blend_width_widget;
			refine = _refine;
	
			_result 
				= im_tbmosaic a'.image.value b'.image.value 0 
					a'.left a'.top b'.left b'.top
					(object_size / 2) (search_area / 2) 0 bw.value,
						refine
				= im_tbmerge a'.image.value b'.image.value
					(b'.left - a'.left) (b'.top - a'.top) bw.value
			{
				[a', b'] = mosaic_sort mosaic_sort_tb [a, b];
			}
		}

		Left_right_item = class 
			Menuaction "_Left to Right" 
				"join two images left-right with a single tie point" {
			action a b = lr_mos refine_widget a b;
		}

		Top_bottom_item = class 
			Menuaction "_Top to Bottom" 
				"join two images top-bottom with a single tie point" {
			action a b = tb_mos refine_widget a b;
		}
	
		sep1 = Menuseparator;

		Left_right_manual_item = class 
			Menuaction "Manual L_eft to Right" 
				"join left-right, no auto-adjust of tie points" {
			action a b = lr_mos false a b;
		}

		Top_bottom_manual_item = class 
			Menuaction "Manual T_op to Bottom" 
				"join top-bottom, no auto-adjust of tie points" {
			action a b = tb_mos false a b;
		}
	}

	Mosaic_2point_item = class 
		Menupullright "_Two Point" "join two images with two tie points" {
		check_abcd_args a b c d = [
			[a, "a", check_Mark],
			[b, "b", check_Mark],
			[c, "c", check_Mark],
			[d, "d", check_Mark]
		];
	
		// shortcut to prefs
		prefs = Workspaces.Preferences;
		search_area = prefs.MOSAIC_WINDOW_SIZE;
		object_size = prefs.MOSAIC_OBJECT_SIZE;
		blend_width_widget = Scale "Maximum blend width" 0 100 prefs.MOSAIC_MAX_BLEND_WIDTH;
		refine_widget = Toggle "Refine selected tie-points" prefs.MOSAIC_REFINE;

		Left_right_item = class 
			Menuaction "_Left to Right" 
				"join two images left-right with a pair of tie points" {
			action a b c d = class 
				Image _result {
				_check_args = check_abcd_args a b c d;
	
				bw = blend_width_widget;
				refine = refine_widget;
	
				_result 
					= im_lrmosaic1 a'.image.value b'.image.value 0
						a'.left a'.top b'.left b'.top
						c'.left c'.top d'.left d'.top
						(object_size / 2) (search_area / 2)
						0 bw.value,
							refine
					= im_lrmerge1 a'.image.value b'.image.value
						a'.left a'.top b'.left b'.top
						c'.left c'.top d'.left d'.top
						bw.value
				{
					[a', b', c', d'] = mosaic_sort mosaic_sort_lr [a, b, c, d];
				}
			}
		}

		Top_bottom_item = class 
			Menuaction "_Top to Bottom" 
				"join two images top-bottom with a pair of tie points" {
			action a b c d = class 
				Image _result {
				_check_args = check_abcd_args a b c d;
	
				bw = blend_width_widget;
				refine = refine_widget;
	
				_result 
					= im_tbmosaic1 a'.image.value b'.image.value 0
						a'.left a'.top b'.left b'.top
						c'.left c'.top d'.left d'.top
						(object_size / 2) (search_area / 2)
						0 bw.value,
							refine
					= im_tbmerge1 a'.image.value b'.image.value
						a'.left a'.top b'.left b'.top
						c'.left c'.top d'.left d'.top
						bw.value
				{
					[a', b', c', d'] = mosaic_sort mosaic_sort_tb [a, b, c, d];
				}
			}
		}
	}
	
	sep1 = Menuseparator;

	Balance_item = class 
		Menuaction "Mosaic _Balance" 
			"disassemble mosaic, scale brightness to match, reassemble" {
		action x 
			= map_unary balance x
		{
			balance x
				= oo_unary_function balance_op x, is_class x
				= im_global_balancef x 
					Workspaces.Preferences.MOSAIC_BALANCE_GAMMA, 
					is_image x
				= error (_ "bad arguments to " ++ "balance")
			{
				balance_op = Operator "balance" balance 
					Operator_type.COMPOUND_REWRAP false;
			}
		}
	}

////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
Manual_balance_item = class
	Menupullright "Manual B_alance" "balance tonality of user defined areas" {
	prefs = Workspaces.Preferences;
	
	////////////////////////////////////////////////////////////////////////////////////
	Balance_find_item = class
		Menuaction "_Find Values"
			 "calculates values required to scale and offset balance user defined areas in a given image"
			 /* Outputs a matrix of scale and offset values. Eg. Values required to balance the secondary 
			  * structure in an X-ray image.  Takes an X-ray image an 8-bit control mask and a list of 
			  * 8-bit reference masks, where the masks are white on a black background.
			  */
	{
		action im_in m_control m_group = class
			Matrix values{
			_vislevel = 1;

			_control_im = if m_control then im_in else 0;
			_control_meanmax = so_meanmax _control_im;
			_group_check = is_Group m_group;
			_m_list = m_group.value, _group_check
		     		= m_group;

			process m_current mat_in = mat_out
				{so_values  = so_calculate _control_meanmax im_in m_current;
				 mat_out = join [so_values] mat_in;}
		
			values = (foldr process [] _m_list);
		}
	}
	
	////////////////////////////////////////////////////////////////////////////////////
	Balance_check_item = class 
		Menuaction "_Check Values"
			 "allows calculated set of scale and offset values to be checked and adjusted if required" 
			 /* Outputs adjusted matrix of scale and offset values and scale and offset image maps.
			  * Eg. Check values required to balance the secondary structure in an X-ray image.  
			  * Takes an X-ray image an 8-bit control mask and a list of 8-bit reference masks, 
			  * where the masks are white on a black background. 
			  */
	{
		action im_in m_matrix m_group = class
			Image value 
			{
			_vislevel = 3;
			
			blur = Scale "Blur" 1 10 1;
			_blur = (blur.value/2 + 0.5), blur.value > 1
				  =  1;
		
			_group_check = is_Group m_group;
			_m_list = m_group.value, _group_check
		     		= m_group;
						
			adjust = Matrix_rec mat_a
				{
				no_masks = len _m_list;
				mat_a = replicate no_masks [0, 0];
				}
			
		// Apply the user defined adjustments to the inputted matrix of scale and offset values	 
			_adjusted = map2 fn_adjust m_matrix.value adjust.value;
				fn_adjust a b = [(a?0 + b?0), (a?1 + (a?1 * b?1))];
				
			_scaled_ims = map (fn_so_apply im_in) _adjusted;
				fn_so_apply im so = map_unary adj im
					{adj im = im * (so?0) + (so?1);}			
			_im_pairs = zip2 _m_list _scaled_ims;
			
		// Prepare black images as starting point. ////////////
			_blank = image_new (_m_list?0).width (_m_list?0).height 1 6 Image_coding.NOCODING 1 0 0 0;
			_pair_start = [(_blank + 1), _blank];
			
			Build = Toggle "Build Scale and Offset Correction Images" false;
			
			Output = class
				{ 
				_vislevel = 1;
				scale_im = _build?0;
				offset_im = _build?1;	
				so_values = Matrix _adjusted;
				
				_build = [Image so_images?0, Image so_images?1], Build
				       = ["Scale image not built.", "Offset image not built."]
					{
					m_list' = transpose [_m_list];			
					m_all = map2 join m_list' _adjusted;					
					so_images = foldr process_2 _pair_start m_all;					
					}
				}
				  
			value = (foldr process_1 im_in_b _im_pairs).value
				{im_in_b = map_unary cast_float im_in;}
				
			process_1 m_current im_start 
				= im_out
				{ 
				bl_mask    = convsep (matrix_blur _blur) (get_image m_current?0);
				blended_im  = im_blend bl_mask (m_current?1).value im_start.value;
				im_out      = Image (clip2fmt im_start.format blended_im);
				}			
			
			// Process for building scale and offset image. 
			process_2 current p_start 
				= p_out
				{ 
				im_s = if ((current?0) > 128) then current?1 else _blank;
				im_o = if ((current?0) > 128) then current?2 else _blank;
				
				im_s' = convsep (matrix_blur _blur) (im_s != 0);
				im_o' = convsep (matrix_blur _blur) (im_o != 0);
				
				im_s'' = im_blend im_s'.value im_s.value p_start?0;
				im_o'' = im_blend im_o'.value im_o.value p_start?1;
				
				p_out  = [im_s'', im_o''];
				}
		}
	}
	
	////////////////////////////////////////////////////////////////////////////////////
	Balance_apply_item = class 
		Menuaction "_Apply Values" 
			 "apply scale and offset corrections, defined as image maps, to a given image" 
			 /* Outputs the balanced image. Eg. Balance the secondary structure in an X-ray image.  Takes an 
			  * X-ray image an 32-bit float scale image and a 32-bit offset image.
			  */
	{
		action im_in scale_im offset_im = class
			Image value 
			{
			_vislevel = 1;

			xfactor = im_in.width/scale_im.width;
			yfactor = im_in.height/scale_im.height;
			
			_scale_im = resize Interpolate_bilinear xfactor yfactor scale_im;
			_offset_im = resize Interpolate_bilinear xfactor yfactor offset_im;
			
			value = get_image ( clip2fmt im_in.format ( ( im_in * _scale_im ) +  _offset_im ) ); 	
			}
		}
}

    Tilt_item = Filter_tilt_item;

	sep2 = Menuseparator;

	Rebuild_item = class 
		Menuaction "_Rebuild" 
			"disassemble mosaic, substitute image files and reassemble" {
		action x = class 
			_result {
			_vislevel = 3;
	
			old = String "In each filename, replace" "foo";
			new = String "With" "bar";
	
			_result
				= map_unary remosaic x
			{
				remosaic image 
					= Image (im_remosaic image.value old.value new.value);
			}
		}
	}

	sep3 = Menuseparator;

Clone_area_item = class
   Menuaction "_Clone Area"
       "replace dark or light section of im1 with pixels from im2"
   {
   action im1 im2 = class
       _result {
       _check_args = [
           [im1, "im1", check_Image],
           [im2, "im2", check_Image]
       ];
                 _vislevel = 3;                            /* Region on first
image placed in the top left hand corner,
        * positioned and size relative to the height and width of im1.
        */
       r1 = Region_relative im1 0.05 0.05 0.05 0.05;
             /* Mark on second image placed in the top left hand corner,
        * positioned relative to the height and width of im2. Used to
        * define _r2, the region from which the section of image is cloned
        * from.
        */
       p2 = Mark_relative im2 0.05 0.05;              _r2 = Region im2 p2.left
p2.top r1.width r1.height;
             mask = [r1 <= Options.sc, r1 >=
Options.sc]?(Options.replace);
                 Options = class
           {
           _vislevel = 3;
                     pause = Toggle "Pause process" true;
                         /* Option toggle used to define whether the user is
            * replacing a dark or a light area.
            */
           replace = Option "Replace" [ "A Dark Area", "A Light Area" ] 1;

           // Used to select the area to be replaced.
            sc = Scale "Scale cutoff" 0.01 mx (mx / 2)
               {mx = Image_format.maxval im1.format;}
             //Allows replacement with scale&offset balanced gaussian noise.
           balance = Toggle "Balance cloned data to match surroundings." true;
                             //Allows replacement with scale&offset balanced
                             //gaussian noise.
           process = Toggle "Replace area with Gaussian noise." false;
           }
                     _result    = im1, Options.pause
               = Image (im_insert im1.value patch r1.left r1.top)
           {              r2 = Region im2 p2.left p2.top r1.width r1.height;
           ref_meanmax = so_meanmax (if mask then 0 else r1);
                     mask8 = Matrix_mor [[255, 255, 255], [255, 255, 255],
[255, 255, 255]];
           mask_a = map_unary (dilate mask8) mask;
           mask_b = convsep (matrix_blur 2) mask_a;
                     patch = so_balance ref_meanmax r1 r2 mask_b
Options.process, Options.balance
                 = so_balance ref_meanmax r1 r2 mask_b Options.process,
Options.process
                 = im_blend (get_image mask_b) (get_image r2) (get_image r1);
           }
       }
   } 
}

Tasks_frame_item = Frame_item;

Tasks_print_item = class
	Menupullright "_Print" "useful stuff for image output" {

	Rotate_item = Image_transform_item.Rotate_item;

	Flip_item = Image_transform_item.Flip_item;

	Resize_item = Image_transform_item.Resize_item;

	Tone_item = Image_levels_item.Tone_item; 

	Sharpen_item = class 
		Menuaction "_Sharpen" 
			"unsharp filter tuned for typical inkjet printers" {
		action x = class 
			_result {
			_vislevel = 3;
	
			target_dpi = Option "Sharpen for print at" [
				"400 dpi",
				"300 dpi",
				"150 dpi",
				"75 dpi"
			] 1;
	
			_result
				= map_unary process x
			{
				process image
					= sharpen params?0 params?1 
						params?2 params?3 params?4 params?5
						(colour_transform_to Image_type.LABQ image)
				{
					// sharpen params for various dpi
					// just change the size of the area we search
					param_table = [
						[7, 2.5, 40, 20, 0.5, 1.5],
						[5, 2.5, 40, 20, 0.5, 1.5],
						[3, 2.5, 40, 20, 0.5, 1.5],
						[11, 2.5, 40, 20, 0.5, 1.5]
					];
					params = param_table?target_dpi;
				}
			}
		}
	}

	sep1 = Menuseparator;

	Temp_item = Colour_temperature_item;

	ICC_item = Colour_icc_item;
}