summaryrefslogtreecommitdiffstats
path: root/src/image/iface.cpp
blob: 9f00cda6af98294e4f041d59b627b9458462511f (plain) (blame)
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
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
/******************************************************************************
 *	Copyright (C) 2018	Alejandro Colomar Andrés		      *
 *	SPDX-License-Identifier:	GPL-2.0-only			      *
 ******************************************************************************/


/******************************************************************************
 ******* headers **************************************************************
 ******************************************************************************/
#include "vision-artificial/image/iface.hpp"

#include <cerrno>
#include <cinttypes>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstdio>

#include <vector>

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/ximgproc.hpp>

#define ALX_NO_PREFIX
#include <alx/base/compiler.hxx>
#include <alx/base/stdio.hxx>
#include <alx/cv/cv.hxx>
#include <alx/ocr/ocr.hxx>
#include <alx/zbar/zbar.hxx>

#include "vision-artificial/image/calib3d.hpp"
#include "vision-artificial/image/cv.hpp"
#include "vision-artificial/save/save.hpp"
#include "vision-artificial/user/iface.hpp"


/******************************************************************************
 ******* macros ***************************************************************
 ******************************************************************************/
#define IMG_MEM_SIZE	(20)

#define WIN_NAME_IMG	"Image"
#define WIN_NAME_HIST	"Hist"
#define WIN_TIMEOUT	(500)


/******************************************************************************
 ******* enums ****************************************************************
 ******************************************************************************/


/******************************************************************************
 ******* structs / unions *****************************************************
 ******************************************************************************/


/******************************************************************************
 ******* variables ************************************************************
 ******************************************************************************/
/* Global --------------------------------------------------------------------*/
	char				img_ocr_text [OCR_TEXT_MAX];
/* Static --------------------------------------------------------------------*/
static	class cv::Mat					image_copy_old;
static	class cv::Mat					image_copy_tmp;
static	class cv::Mat					image_mem[IMG_MEM_SIZE];
static	class cv::Mat					image_ref;
static	class cv::Mat					histogram_c0;
static	class cv::Mat					histogram_c1;
static	class cv::Mat					histogram_c2;
static	class cv::Mat					hist_img_c1;
static	class cv::Mat					hist_img_c3;
static	class std::vector <class std::vector <cv::Point_ <int>>>  contours;
static	double						area[CONTOURS_MAX];
static	double						perimeter[CONTOURS_MAX];
static	ptrdiff_t					ctr_x[CONTOURS_MAX];
static	ptrdiff_t					ctr_y[CONTOURS_MAX];
static	class cv::Mat					hierarchy;
static	class cv::Rect_ <int>				rectangle;
static	class cv::RotatedRect				rectangle_rot;
static	class std::vector <class cv::Vec <float, 3>>	circles;
static	class cv::Mat					intrinsic_mat;
static	class cv::Mat					dist_coefs;
static	class std::vector <class cv::Mat>		rvecs;
static	class std::vector <class cv::Mat>		tvecs;


/******************************************************************************
 ******* static functions (prototypes) ****************************************
 ******************************************************************************/
	/* alx */
static	void	img_iface_local_max		(void);
static	void	img_iface_skeleton		(void);
static	void	img_iface_lines_horizontal	(void);
static	void	img_iface_lines_vertical	(void);
static	void	img_iface_mean_horizontal	(void);
static	void	img_iface_mean_vertical		(void);
static	void	img_iface_median_horizontal	(void);
static	void	img_iface_median_vertical	(void);
static	void	img_iface_bkgd_mask		(void);
static	void	img_iface_bkgd_fill		(void);
static	void	img_iface_holes_mask		(void);
static	void	img_iface_holes_fill		(void);
static	void	img_iface_white_mask		(void);
static	void	img_iface_black_mask		(void);
static	void	img_iface_gray_mask		(void);
	/* cv */
		/* Core: The core functionality */
			/* Pixel */
static	void	img_iface_pixel_get		(void);
static	void	img_iface_pixel_set		(void);
			/* ROI */
static	void	img_iface_set_ROI		(void);
static	void	img_iface_set_ROI_2rect		(void);
			/* Operations on Arrays */
static	void	img_iface_and_2ref		(void);
static	void	img_iface_not			(void);
static	void	img_iface_or_2ref		(void);
static	void	img_iface_component		(void);
		/* Imgproc: Image processing */
			/* Image filtering */
static	void	img_iface_dilate		(void);
static	void	img_iface_erode			(void);
static	void	img_iface_dilate_erode		(void);
static	void	img_iface_erode_dilate		(void);
static	void	img_iface_smooth		(void);
static	void	img_iface_sobel			(void);
static	void	img_iface_border		(void);
			/* Geometric image transformations */
static	void	img_iface_mirror		(void);
static	void	img_iface_rotate_orto		(void);
static	void	img_iface_rotate		(void);
static	void	img_iface_rotate_2rect		(void);
			/* Miscellaneous image transformations */
static	void	img_iface_adaptive_thr		(void);
static	void	img_iface_cvt_color		(void);
static	void	img_iface_distance_transform	(void);
static	void	img_iface_threshold		(void);
			/* Histograms */
static	void	img_iface_histogram1D		(void);
static	void	img_iface_histogram3D		(void);
			/* Structural analysis and shape descriptors */
static	void	img_iface_contours		(void);
static	void	img_iface_contours_dimensions	(void);
static	void	img_iface_bounding_rect		(void);
static	void	img_iface_fit_ellipse		(void);
static	void	img_iface_min_area_rect		(void);
			/* Feature detection */
static	void	img_iface_hough_circles		(void);
	/* orb */
static	void	img_iface_align			(void);
	/* calib3d */
static	void	img_iface_calibrate		(void);
static	void	img_iface_undistort		(void);
	/* zbar */
static	void	img_iface_decode		(void);
	/* ocr */
static	void	img_iface_read			(void);
	/* iface */
static	void	img_iface_apply			(void);
static	void	img_iface_discard		(void);
static	void	img_iface_save_mem		(void);
static	void	img_iface_load_mem		(void);
static	void	img_iface_save_ref		(void);
	/* save/save */
static	void	img_iface_save_file		(void);
static	void	img_iface_save_update		(void);


/******************************************************************************
 ******* global functions *****************************************************
 ******************************************************************************/
void	img_iface_init		(void)
{

	cv::namedWindow(WIN_NAME_HIST, cv::WINDOW_NORMAL);
	cv::namedWindow(WIN_NAME_IMG, cv::WINDOW_NORMAL);

	hist_img_c1.release();
	hist_img_c1	= cv::Mat::zeros(cv::Size(256, 100), CV_8UC3);
	hist_img_c3.release();
	hist_img_c3	= cv::Mat::zeros(cv::Size(256 * 3, 100), CV_8UC3);
}

void	img_iface_deinit	(void)
{

	for (ptrdiff_t i = 0; i < IMG_MEM_SIZE; i++)
		image_mem[i].release();
	image_ref.release();

	cv::destroyAllWindows();
}

void	img_iface_load		(const char *fpath, const char *fname)
{
	const char	*path;
	char		file_name[FILENAME_MAX];

	if (!fpath)
		path = saved_path;
	else
		path = fpath;

	if (!fname)
		user_iface_fname(fpath, file_name);
	else
		ALX_UNUSED(sbprintf(file_name, NULL, "%s", fname));

	errno	= 0;
	load_image_file(path, file_name);
	if (errno)
		return;

	image.copyTo(image_copy_old);
	image.copyTo(image_copy_tmp);
}

void	img_iface_cleanup	(void)
{

	image_copy_old.release();
	image_copy_tmp.release();
	histogram_c0.release();
	histogram_c1.release();
	histogram_c2.release();
	hist_img_c1.release();
	hist_img_c3.release();
}

void	img_iface_act		(int action)
{

	switch (action) {
	/* img_alx */
	case IMG_IFACE_ACT_LOCAL_MAX:
		img_iface_local_max();
		break;
	case IMG_IFACE_ACT_SKELETON:
		img_iface_skeleton();
		break;
	case IMG_IFACE_ACT_LINES_HORIZONTAL:
		img_iface_lines_horizontal();
		break;
	case IMG_IFACE_ACT_LINES_VERTICAL:
		img_iface_lines_vertical();
		break;
	case IMG_IFACE_ACT_MEAN_HORIZONTAL:
		img_iface_mean_horizontal();
		break;
	case IMG_IFACE_ACT_MEAN_VERTICAL:
		img_iface_mean_vertical();
		break;
	case IMG_IFACE_ACT_MEDIAN_HORIZONTAL:
		img_iface_median_horizontal();
		break;
	case IMG_IFACE_ACT_MEDIAN_VERTICAL:
		img_iface_median_vertical();
		break;
	case IMG_IFACE_ACT_BKGD_MASK:
		img_iface_bkgd_mask();
		break;
	case IMG_IFACE_ACT_BKGD_FILL:
		img_iface_bkgd_fill();
		break;
	case IMG_IFACE_ACT_HOLES_MASK:
		img_iface_holes_mask();
		break;
	case IMG_IFACE_ACT_HOLES_FILL:
		img_iface_holes_fill();
		break;
	case IMG_IFACE_ACT_WHITE_MASK:
		img_iface_white_mask();
		break;
	case IMG_IFACE_ACT_BLACK_MASK:
		img_iface_black_mask();
		break;
	case IMG_IFACE_ACT_GRAY_MASK:
		img_iface_gray_mask();
		break;
	/* img_cv */
		/* Core: The core functionality */
			/* Pixel */
	case IMG_IFACE_ACT_PIXEL_GET:
		img_iface_pixel_get();
		break;
	case IMG_IFACE_ACT_PIXEL_SET:
		img_iface_pixel_set();
		break;
			/* ROI */
	case IMG_IFACE_ACT_SET_ROI:
		img_iface_set_ROI();
		break;
	case IMG_IFACE_ACT_SET_ROI_2RECT:
		img_iface_set_ROI_2rect();
		break;
			/* Operations on Arrays */
	case IMG_IFACE_ACT_AND_2REF:
		img_iface_and_2ref();
		break;
	case IMG_IFACE_ACT_NOT:
		img_iface_not();
		break;
	case IMG_IFACE_ACT_OR_2REF:
		img_iface_or_2ref();
		break;
	case IMG_IFACE_ACT_COMPONENT:
		img_iface_component();
		break;
		/* Imgproc: Image processing */
			/* Image filtering */
	case IMG_IFACE_ACT_DILATE:
		img_iface_dilate();
		break;
	case IMG_IFACE_ACT_ERODE:
		img_iface_erode();
		break;
	case IMG_IFACE_ACT_DILATE_ERODE:
		img_iface_dilate_erode();
		break;
	case IMG_IFACE_ACT_ERODE_DILATE:
		img_iface_erode_dilate();
		break;
	case IMG_IFACE_ACT_SMOOTH:
		img_iface_smooth();
		break;
	case IMG_IFACE_ACT_SOBEL:
		img_iface_sobel();
		break;
	case IMG_IFACE_ACT_BORDER:
		img_iface_border();
		break;
			/* Geometric image transformations */
	case IMG_IFACE_ACT_MIRROR:
		img_iface_mirror();
		break;
	case IMG_IFACE_ACT_ROTATE_ORTO:
		img_iface_rotate_orto();
		break;
	case IMG_IFACE_ACT_ROTATE:
		img_iface_rotate();
		break;
	case IMG_IFACE_ACT_ROTATE_2RECT:
		img_iface_rotate_2rect();
		break;
			/* Miscellaneous image transformations */
	case IMG_IFACE_ACT_ADAPTIVE_THRESHOLD:
		img_iface_adaptive_thr();
		break;
	case IMG_IFACE_ACT_CVT_COLOR:
		img_iface_cvt_color();
		break;
	case IMG_IFACE_ACT_DISTANCE_TRANSFORM:
		img_iface_distance_transform();
		break;
	case IMG_IFACE_ACT_THRESHOLD:
		img_iface_threshold();
		break;
			/* Histograms */
	case IMG_IFACE_ACT_HISTOGRAM:
		img_iface_histogram1D();
		break;
	case IMG_IFACE_ACT_HISTOGRAM_C3:
		img_iface_histogram3D();
		break;
			/* Structural analysis and shape descriptors */
	case IMG_IFACE_ACT_CONTOURS:
		img_iface_contours();
		break;
	case IMG_IFACE_ACT_CONTOURS_SIZE:
		img_iface_contours_dimensions();
		break;
	case IMG_IFACE_ACT_BOUNDING_RECT:
		img_iface_bounding_rect();
		break;
	case IMG_IFACE_ACT_FIT_ELLIPSE:
		img_iface_fit_ellipse();
		break;
	case IMG_IFACE_ACT_MIN_AREA_RECT:
		img_iface_min_area_rect();
		break;
			/* Feature detection */
	case IMG_IFACE_ACT_HOUGH_CIRCLES:
		img_iface_hough_circles();
		break;
	/* img_orb */
	case IMG_IFACE_ACT_ALIGN:
		img_iface_align();
		break;
	/* img_calib3d */
	case IMG_IFACE_ACT_CALIBRATE:
		img_iface_calibrate();
		break;
	case IMG_IFACE_ACT_UNDISTORT:
		img_iface_undistort();
		break;
	/* img_zbar */
	case IMG_IFACE_ACT_DECODE:
		img_iface_decode();
		break;
	/* img_ocr */
	case IMG_IFACE_ACT_READ:
		img_iface_read();
		break;
	/* img_iface */
	case IMG_IFACE_ACT_APPLY:
		img_iface_apply();
		break;
	case IMG_IFACE_ACT_DISCARD:
		img_iface_discard();
		break;
	case IMG_IFACE_ACT_SAVE_MEM:
		img_iface_save_mem();
		break;
	case IMG_IFACE_ACT_LOAD_MEM:
		img_iface_load_mem();
		break;
	case IMG_IFACE_ACT_SAVE_REF:
		img_iface_save_ref();
		break;
	/* save */
	case IMG_IFACE_ACT_SAVE_FILE:
		img_iface_save_file();
		break;
	case IMG_IFACE_ACT_SAVE_UPDT:
		img_iface_save_update();
		break;
	default:
		/* Invalid action */
		break;
	}
}

void	img_iface_show_img	(void)
{

	cv::imshow(WIN_NAME_IMG, image_copy_tmp);
	cv::waitKey(WIN_TIMEOUT);
}

void	img_iface_show_hist_c1	(void)
{

	cv::imshow(WIN_NAME_HIST, hist_img_c1);
	cv::waitKey(WIN_TIMEOUT);
}

void	img_iface_show_hist_c3	(void)
{

	cv::imshow(WIN_NAME_HIST, hist_img_c3);
	cv::waitKey(WIN_TIMEOUT);
}


/******************************************************************************
 ******* static functions *****************************************************
 ******************************************************************************/
/* alx -----------------------------------------------------------------------*/
static	void	img_iface_local_max		(void)
{

	if (alx::CV::local_max(&image_copy_tmp))
		goto err;

	user_iface_log_write(1, "Local maxima");
	return;
err:
	user_iface_log_write(1, "! Invalid input (Must be 1 channel)");
}

static	void	img_iface_skeleton		(void)
{

	if (alx::CV::thinning(&image_copy_tmp, cv::ximgproc::THINNING_ZHANGSUEN))
		goto err;

	user_iface_log_write(1, "Skeleton");
	return;
err:
	user_iface_log_write(1, "! Invalid input (Must be 1 channel)");
}

static	void	img_iface_lines_horizontal	(void)
{

	if (alx::CV::lines_horizontal(&image_copy_tmp))
		goto err;

	user_iface_log_write(1, "Horizontal lines");
	return;
err:
	user_iface_log_write(1, "! Invalid input (Must be 1 channel)");
}

static	void	img_iface_lines_vertical	(void)
{

	if (alx::CV::lines_vertical(&image_copy_tmp))
		goto err;

	user_iface_log_write(1, "Vertical lines");
	return;
err:
	user_iface_log_write(1, "! Invalid input (Must be 1 channel)");
}

static	void	img_iface_mean_horizontal	(void)
{

	if (alx::CV::mean_horizontal(&image_copy_tmp))
		goto err;

	user_iface_log_write(1, "Horizontal mean");
	return;
err:
	user_iface_log_write(1, "! Invalid input (Must be 1 channel)");
}

static	void	img_iface_mean_vertical		(void)
{

	if (alx::CV::mean_vertical(&image_copy_tmp))
		goto err;

	user_iface_log_write(1, "Vertical mean");
	return;
err:
	user_iface_log_write(1, "! Invalid input (Must be 1 channel)");
}

static	void	img_iface_median_horizontal	(void)
{

	if (alx::CV::median_horizontal(&image_copy_tmp))
		goto err;

	user_iface_log_write(1, "Horizontal median");
	return;
err:
	user_iface_log_write(1, "! Invalid input (Must be 1 channel)");
}

static	void	img_iface_median_vertical	(void)
{

	if (alx::CV::median_vertical(&image_copy_tmp))
		goto err;

	user_iface_log_write(1, "Vertical median");
	return;
err:
	user_iface_log_write(1, "! Invalid input (Must be 1 channel)");
}

static	void	img_iface_bkgd_mask	(void)
{

	if (alx::CV::bkgd_mask(&image_copy_tmp))
		goto err;
	user_iface_log_write(1, "Bkgd mask");
	return;
err:	user_iface_log_write(1, "! Invalid input (Must be 1 channel)");
}

static	void	img_iface_bkgd_fill	(void)
{

	if (alx::CV::bkgd_fill(&image_copy_tmp))
		goto err;
	user_iface_log_write(1, "Bkgd fill");
	return;
err:	user_iface_log_write(1, "! Invalid input (Must be 1 channel)");
}

static	void	img_iface_holes_mask	(void)
{

	if (alx::CV::holes_mask(&image_copy_tmp))
		goto err;
	user_iface_log_write(1, "Holes mask");
	return;
err:	user_iface_log_write(1, "! Invalid input (Must be 1 channel)");
}

static	void	img_iface_holes_fill	(void)
{

	if (alx::CV::holes_fill(&image_copy_tmp))
		goto err;
	user_iface_log_write(1, "Holes fill");
	return;
err:	user_iface_log_write(1, "! Invalid input (Must be 1 channel)");
}

static	void	img_iface_white_mask	(void)
{
	uint8_t	s_tolerance, l_tolerance, sl_tolerance;
	char	txt[LOG_LINE_LEN];

	sl_tolerance	= user_iface_getint(0, 0, UINT8_MAX, "SL tolerance:", NULL);
	l_tolerance	= user_iface_getint(0, 0, UINT8_MAX, "L tolerance:", NULL);
	s_tolerance	= user_iface_getint(0, 0, UINT8_MAX, "S tolerance:", NULL);
	if (alx::CV::white_mask(&image_copy_tmp, sl_tolerance, l_tolerance, s_tolerance))
		goto err;

	if (sbprintf(txt, NULL, "White mask: SL_tol=%" PRIu8 "; L_tol=%" PRIu8 "; S_tol=%" PRIu8 "",
					sl_tolerance, l_tolerance, s_tolerance) < 0)
		return;
	user_iface_log_write(1, txt);
	return;
err:	user_iface_log_write(1, "! Invalid input (Must be 1 channel)");
}

static	void	img_iface_black_mask	(void)
{
	uint8_t	s_tolerance, l_tolerance, sl_tolerance;
	char	txt[LOG_LINE_LEN];

	sl_tolerance	= user_iface_getint(0, 0, UINT8_MAX, "SL tolerance:", NULL);
	l_tolerance	= user_iface_getint(0, 0, UINT8_MAX, "L tolerance:", NULL);
	s_tolerance	= user_iface_getint(0, 0, UINT8_MAX, "S tolerance:", NULL);
	if (alx::CV::black_mask(&image_copy_tmp, sl_tolerance, l_tolerance, s_tolerance))
		goto err;

	if (sbprintf(txt, NULL, "Black mask: SL_tol=%" PRIu8 "; L_tol=%" PRIu8 "; S_tol=%" PRIu8 "",
					sl_tolerance, l_tolerance, s_tolerance) < 0)
		return;
	user_iface_log_write(1, txt);
	return;
err:	user_iface_log_write(1, "! Invalid input (Must be 1 channel)");
}

static	void	img_iface_gray_mask	(void)
{
	uint8_t	s_tolerance, l_tolerance, sl_tolerance;
	char	txt[LOG_LINE_LEN];

	sl_tolerance	= user_iface_getint(0, 0, UINT8_MAX, "SL tolerance:", NULL);
	l_tolerance	= user_iface_getint(0, 0, UINT8_MAX, "L tolerance:", NULL);
	s_tolerance	= user_iface_getint(0, 0, UINT8_MAX, "S tolerance:", NULL);
	if (alx::CV::gray_mask(&image_copy_tmp, sl_tolerance, l_tolerance, s_tolerance))
		goto err;

	if (sbprintf(txt, NULL, "Gray mask: SL_tol=%" PRIu8 "; L_tol=%" PRIu8 "; S_tol=%" PRIu8 "",
					sl_tolerance, l_tolerance, s_tolerance) < 0)
		return;
	user_iface_log_write(1, txt);
	return;
err:	user_iface_log_write(1, "! Invalid input (Must be 1 channel)");
}

/* cv ------------------------------------------------------------------------*/
/* ----- Core: The core functionality */
/* ----- ------- Pixel */
static	void	img_iface_pixel_get		(void)
{
	unsigned char	val;
	ptrdiff_t	x, y;
	char		txt[LOG_LINE_LEN];

	x	= user_iface_getint(0, 0, image_copy_tmp.cols, "x:", NULL);
	y	= user_iface_getint(0, 0, image_copy_tmp.rows, "y:", NULL);

	if (alx::CV::pixel_get_u8(&image_copy_tmp, &val, x, y))
		goto err;

	if (sbprintf(txt, NULL, "Pixel get:  (%ti, %ti): %hhu", x, y, val) < 0)
		return;
	user_iface_log_write(1, txt);
	return;
err:
	user_iface_log_write(1, "! Invalid input (Must be 1 channel)");
}

static	void	img_iface_pixel_set		(void)
{
	unsigned char	val;
	ptrdiff_t	x, y;
	char		txt[LOG_LINE_LEN];

	x	= user_iface_getint(0, 0, image_copy_tmp.cols, "x:", NULL);
	y	= user_iface_getint(0, 0, image_copy_tmp.rows, "y:", NULL);
	val	= user_iface_getint(0, 0, UINT8_MAX, "val:", NULL);

	if (alx::CV::pixel_set_u8(&image_copy_tmp, val, x, y))
		goto err;

	if (sbprintf(txt, NULL, "Pixel set:  (%ti, %ti): %hhu", x, y, val) < 0)
		return;
	user_iface_log_write(1, txt);
	return;
err:
	user_iface_log_write(1, "! Invalid input (Must be 1 channel)");
}

/* ----- ------- ROI */
static	void	img_iface_set_ROI		(void)
{
	class cv::Rect_ <int>	rect;
	ptrdiff_t	tmp;
	char		txt[LOG_LINE_LEN];

	tmp		= image_copy_tmp.cols;
	rect.x		= user_iface_getint(0, 0, tmp, "Origin:  x:", NULL);
	tmp		= image_copy_tmp.rows;
	rect.y		= user_iface_getint(0, 0, tmp, "Origin:  y:", NULL);
	tmp		= image_copy_tmp.cols - rect.x;
	rect.width	= user_iface_getint(1, tmp, tmp, "Width:", NULL);
	tmp		= image_copy_tmp.rows - rect.y;
	rect.height	= user_iface_getint(1, tmp, tmp, "Height:", NULL);

	alx::CV::roi_set(&image_copy_tmp, &rect);

	if (sbprintf(txt, NULL, "ROI: (%i,%i) w=%i,h=%i",
				rect.x, rect.y, rect.width, rect.height) < 0)
		return;
	user_iface_log_write(1, txt);
}

static	void	img_iface_set_ROI_2rect		(void)
{
	char	txt[LOG_LINE_LEN];

	alx::CV::roi_set(&image_copy_tmp, &rectangle);

	if (sbprintf(txt, NULL, "ROI: (%i,%i) w=%i,h=%i",
				rectangle.x, rectangle.y,
				rectangle.width, rectangle.height) < 0)
		return;
	user_iface_log_write(1, txt);
}

/* ----- ------- Operations on arrays */
static	void	img_iface_and_2ref		(void)
{

	if (alx::CV::and_2ref(&image_copy_tmp, &image_ref))
		goto err;

	user_iface_log_write(1, "Bitwise AND");
	return;
err:
	user_iface_log_write(1, "! Invalid input (Channels do not match ref)");
}

static	void	img_iface_not			(void)
{

	alx::CV::invert(&image_copy_tmp);

	user_iface_log_write(1, "Invert color");
}

static	void	img_iface_or_2ref		(void)
{

	if (alx::CV::or_2ref(&image_copy_tmp, &image_ref))
		goto err;

	user_iface_log_write(1, "Bitwise OR");
	return;
err:
	user_iface_log_write(1, "! Invalid input (Channels do not match ref)");
}

static	void	img_iface_component		(void)
{
	ptrdiff_t	cmp, tmp;
	char		txt[LOG_LINE_LEN];

	tmp	= image_copy_tmp.channels() - 1;
	cmp	= user_iface_getint(0, 0, tmp, "Component:", NULL);

	if (alx::CV::component(&image_copy_tmp, cmp))
		goto err;

	if (sbprintf(txt, NULL, "Component %ti", cmp) < 0)
		return;
	user_iface_log_write(1, txt);
	return;
err:
	user_iface_log_write(1, "! Invalid input (channels)");
}

/* ----- Imgproc: Image processing */
/* ----- ------- Image filtering */
static	void	img_iface_dilate		(void)
{
	ptrdiff_t	i;
	char		txt[LOG_LINE_LEN];

	i	= user_iface_getint(1, 1, INFINITY, "Iterations:", NULL);

	if (alx::CV::dilate(&image_copy_tmp, i))
		goto err;

	if (sbprintf(txt, NULL, "Dilate i = %ti", i) < 0)
		return;
	user_iface_log_write(1, txt);
	return;
err:
	user_iface_log_write(1, "! Invalid input (iterations)");
}

static	void	img_iface_erode			(void)
{
	ptrdiff_t	i;
	char		txt[LOG_LINE_LEN];

	i	= user_iface_getint(1, 1, INFINITY, "Iterations:", NULL);

	if (alx::CV::erode(&image_copy_tmp, i))
		goto err;

	if (sbprintf(txt, NULL, "Erode i = %ti", i) < 0)
		return;
	user_iface_log_write(1, txt);
	return;
err:
	user_iface_log_write(1, "! Invalid input (iterations)");
}

static	void	img_iface_dilate_erode		(void)
{
	ptrdiff_t	i;
	char		txt[LOG_LINE_LEN];

	i	= user_iface_getint(1, 1, INFINITY, "Iterations:", NULL);

	if (alx::CV::dilate_erode(&image_copy_tmp, i))
		goto err;

	if (sbprintf(txt, NULL, "Dilate-erode i = %ti", i) < 0)
		return;
	user_iface_log_write(1, txt);
	return;
err:
	user_iface_log_write(1, "! Invalid input (iterations)");
}

static	void	img_iface_erode_dilate		(void)
{
	ptrdiff_t	i;
	char		txt[LOG_LINE_LEN];

	i	= user_iface_getint(1, 1, INFINITY, "Iterations:", NULL);

	if (alx::CV::erode_dilate(&image_copy_tmp, i))
		goto err;

	if (sbprintf(txt, NULL, "Erode-dilate i = %ti", i) < 0)
		return;
	user_iface_log_write(1, txt);
	return;
err:
	user_iface_log_write(1, "! Invalid input (iterations)");
}

static	void	img_iface_smooth		(void)
{
	int		method;
	ptrdiff_t	ksize;
	char		txt[LOG_LINE_LEN];

	method	= user_iface_getint(1, 3, 3,
				"Method: MEAN=1, GAUSS=2, MEDIAN=3", NULL);
	ksize	= user_iface_getint(3, 3, INFINITY,
				"Kernel size: 3, 5, 7, ...", NULL);

	if (alx::CV::smooth(&image_copy_tmp, method, ksize))
		goto err;

	if (sbprintf(txt, NULL, "Smooth mth = %i [%ti x %ti]",
						method, ksize, ksize) < 0)
		return;
	user_iface_log_write(1, txt);
	return;
err:
	user_iface_log_write(1, "! Invalid input");
}

static	void	img_iface_sobel			(void)
{
	ptrdiff_t	dx, dy, ksize;
	char		txt[LOG_LINE_LEN];

	dx	= user_iface_getint(0, 1, 10,
			"Order of the derivative x", NULL);
	dy	= user_iface_getint(0, 1, 10,
			"Order of the derivative y", NULL);
	ksize	= user_iface_getint(-1, 3, 7,
			"Size of the extended Sobel kernel (Scharr: -1)", NULL);

	if (alx::CV::sobel(&image_copy_tmp, dx, dy, ksize))
		goto err;

	if (sbprintf(txt, NULL, "Sobel dx = %ti; dy = %ti [ks = %ti]",
						dx, dy, ksize) < 0)
		return;
	user_iface_log_write(1, txt);
	return;
err:
	user_iface_log_write(1, "! Invalid input (Must be 1 channel)");
}

static	void	img_iface_border		(void)
{
	ptrdiff_t	size;
	char		txt[LOG_LINE_LEN];

	size	= user_iface_getint(1, 1, INT16_MAX, "Size",NULL);

	alx::CV::border_black(&image_copy_tmp, size);

	if (sbprintf(txt, NULL, "Border size = %ti", size) < 0)
		return;
	user_iface_log_write(1, txt);
	return;
}

/* ----- ------- Geometric image transformations */
static	void	img_iface_mirror		(void)
{
	int	axis;
	char	txt[LOG_LINE_LEN];

	axis	= user_iface_getint(0, 1, 1, "Axis:  0=x;  1=y", NULL);

	if (alx::CV::mirror(&image_copy_tmp, axis))
		goto err;

	if (sbprintf(txt, NULL, "Mirror axis: %i", axis) < 0)
		return;
	user_iface_log_write(1, txt);
	return;
err:
	user_iface_log_write(1, "! Invalid input");
}

static	void	img_iface_rotate_orto		(void)
{
	int	n;
	char	txt[LOG_LINE_LEN];

	n	= user_iface_getint(1, 1, 3,
			"Rotate (counterclockwise) n * pi/2 rad;  n:", NULL);

	if (alx::CV::rotate_orto(&image_copy_tmp, n))
		goto err;

	if (sbprintf(txt, NULL, "Rotate %i * pi/2 rad", n) < 0)
		return;
	user_iface_log_write(1, txt);
	return;
err:
	user_iface_log_write(1, "! Invalid input");
}

static	void	img_iface_rotate		(void)
{
	double		x, y;
	double		angle;
	char		txt[LOG_LINE_LEN];

	x	= user_iface_getdbl(0, 0, INFINITY, "Center:  x:", NULL);
	y	= user_iface_getdbl(0, 0, INFINITY, "Center:  y:", NULL);
	angle = user_iface_getdbl(-INFINITY, 0, INFINITY, "Angle: (deg)", NULL);

	if (alx::CV::rotate(&image_copy_tmp, x, y, angle))
		goto err;

	if (sbprintf(txt, NULL, "Rotate (%.2lf,%.2lf) %lfº", x, y, angle) < 0)
		return;
	user_iface_log_write(1, txt);
	return;
err:
	user_iface_log_write(1, "! Invalid input");
}

static	void	img_iface_rotate_2rect		(void)
{

	if (alx::CV::rotate_2rect(&image_copy_tmp, &rectangle_rot, NULL))
		goto err;

	user_iface_log_write(1, "Rotate to rectangle");
	return;
err:
	user_iface_log_write(1, "! Invalid input");
}

/* ----- ------- Miscellaneous image transformations */
static	void	img_iface_adaptive_thr		(void)
{
	int		method, thr_typ, c;
	ptrdiff_t	ksize;
	char		txt[LOG_LINE_LEN];

	method	= user_iface_getint(0, 1, 1, "Method: MEAN=0, GAUSS=1", NULL);
	thr_typ = user_iface_getint(0, 0, 1, "Type: BIN=0, BIN_INV=1", NULL);
	ksize	= user_iface_getint(3, 3, INFINITY,
					"Kernel size: 3, 5, 7, ...", NULL);
	c	= user_iface_getint(-INFINITY, 1, INFINITY, "c:", NULL);

	if (alx::CV::adaptive_thr(&image_copy_tmp, method, thr_typ, ksize, c))
		goto err;

	if (sbprintf(txt, NULL, "Threshold mth=%i, typ=%i, ks=%ti, c=%i",
						method, thr_typ, ksize, c) < 0)
		return;
	user_iface_log_write(1, txt);
	return;
err:
	user_iface_log_write(1, "! Invalid input (Must be 1 channel)");
}

static	void	img_iface_cvt_color		(void)
{
	int	method;
	char	txt[LOG_LINE_LEN];

	method	= user_iface_getint(0, 0, cv::COLOR_COLORCVT_MAX,
				"Method: BGR2GRAY = 6, BGR2HSV = 40, BGR2HLS = 52", NULL);

	if (alx::CV::cvt_color(&image_copy_tmp, method))
		goto err;

	if (sbprintf(txt, NULL, "Convert color %i", method) < 0)
		return;
	user_iface_log_write(1, txt);
	return;
err:
	user_iface_log_write(1, "! Invalid input");
}

static	void	img_iface_distance_transform	(void)
{

	if (alx::CV::distance_transform_8b(&image_copy_tmp))
		goto err;

	user_iface_log_write(1, "Distance transform");
	return;
err:
	user_iface_log_write(1, "! Invalid input (Must be 1 channel)");
}

static	void	img_iface_threshold		(void)
{
	int	thr_typ, thr_val;
	char	txt[LOG_LINE_LEN];

	thr_typ	= user_iface_getint(0, 0, 4,
			"Type: BIN=0, BIN_INV=1, TRUNC=2, TOZ=3, TOZ_INV=4",
									NULL);
	thr_val = user_iface_getint(-1, 0, UINT8_MAX,
			"Value: 0 to 255 (or -1 for Otsu's algorithm)", NULL);

	if (alx::CV::threshold(&image_copy_tmp, thr_typ, thr_val))
		goto err;

	if (sbprintf(txt, NULL, "Threshold typ = %i, val = %i",
							thr_typ, thr_val) < 0)
		return;
	user_iface_log_write(1, txt);
	return;
err:
	user_iface_log_write(1, "! Invalid input (Must be 1 channel)");
}

/* ----- ------- Histograms */
static	void	img_iface_histogram1D		(void)
{

	if (alx::CV::draw_hist1D(&hist_img_c1, &image_copy_tmp))
		goto err;

	user_iface_log_write(1, "Histogram");
	return;
err:
	user_iface_log_write(1, "! Invalid input (Must be 1 channel)");
}

static	void	img_iface_histogram3D		(void)
{

	if (alx::CV::draw_hist3D(&hist_img_c3, &image_copy_tmp))
		goto err;

	user_iface_log_write(1, "Histogram (3 channels)");
	return;
err:
	user_iface_log_write(1, "! Invalid input (Must be 3 channels)");
}

/* ----- ------- Structural analysis and shape descriptors */
static	void	img_iface_contours		(void)
{
	char	txt[LOG_LINE_LEN];

	if (alx::CV::contours(&image_copy_tmp, &contours))
		goto err;

	if (sbprintf(txt, NULL, "Contours n = %zu", contours.size()) < 0)
		return;
	user_iface_log_write(1, txt);
	return;
err:
	user_iface_log_write(1, "! Invalid input (Must be 1 channel)");
}

static	void	img_iface_contours_dimensions	(void)
{
	char	txt[LOG_LINE_LEN];

	user_iface_log_write(1, "Contours size:");
	for (size_t i = 0; i < contours.size(); i++) {
		alx::CV::contour_dimensions(&contours[i],
				&area[i], &perimeter[i], &ctr_x[i], &ctr_y[i]);
		if (sbprintf(txt, NULL, "cnt[%zu]: A = %lf; P = %lf; ctr_x = %ti; ctr_y = %ti",
				i, area[i], perimeter[i], ctr_x[i], ctr_y[i]) < 0)
			return;
		user_iface_log_write(2, txt);
	}
}

static	void	img_iface_bounding_rect		(void)
{

	if(!contours.size())
		goto err;

	alx::CV::bounding_rect(&rectangle, &contours[0]);
	alx::CV::draw_rect(&image_copy_tmp, &rectangle);

	user_iface_log_write(1, "Bounding rectangle");
	return;
err:
	user_iface_log_write(1, "! Invalid input (No contours saved)");
}

static	void	img_iface_fit_ellipse		(void)
{

	if(!contours.size())
		goto err;

	alx::CV::fit_ellipse(&rectangle_rot, &contours[0]);
	alx::CV::draw_rect_rot(&image_copy_tmp, &rectangle_rot);

	user_iface_log_write(1, "Fit ellipse");
	return;
err:
	user_iface_log_write(1, "! Invalid input (No contours saved)");
}

static	void	img_iface_min_area_rect		(void)
{

	if(!contours.size())
		goto err;

	alx::CV::min_area_rect(&rectangle_rot, &contours[0]);
	alx::CV::draw_rect_rot(&image_copy_tmp, &rectangle_rot);

	user_iface_log_write(1, "Min area rectangle");
	return;
err:
	user_iface_log_write(1, "! Invalid input (No contours saved)");
}

/* ----- ------- Feature detection */
static	void	img_iface_hough_circles		(void)
{
	struct Img_Iface_Data_Hough_Circles	data;
	char	txt[LOG_LINE_LEN];

	if (image_copy_tmp.channels() != 1)
		goto err;

	data.circles	= &circles;
	data.dist_min	= user_iface_getdbl(0, 5, INFINITY,
						"Minimum distance:", NULL);
	data.param_1	= user_iface_getdbl(0, 200, INFINITY, "param 1:", NULL);
	data.param_2	= user_iface_getdbl(0, 100, INFINITY, "param 2:", NULL);
	data.radius_min	= user_iface_getint(0, 10, INFINITY,
						"Minimum radius", NULL);
	data.radius_max	= user_iface_getint(0, 0, INFINITY,
						"Maximum radius", NULL);

	img_cv_act(&image_copy_tmp, IMG_CV_ACT_HOUGH_CIRCLES, &data);

	if (sbprintf(txt, NULL, "Circles n = %zu",data.circles->size()) < 0)
		return;
	user_iface_log_write(1, txt);
	return;
err:
	user_iface_log_write(1, "! Invalid input (Must be 1 channel)");
}

/* orb -----------------------------------------------------------------------*/
static	void	img_iface_align			(void)
{

	if (image_ref.empty())
		goto err;

	alx::CV::orb_align(&image_ref, &image_copy_tmp, NULL);

	user_iface_log_write(1, "Align to reference");
	return;
err:
	user_iface_log_write(1, "! Reference is NULL");
}

/* calib3d -------------------------------------------------------------------*/
static	void	img_iface_calibrate		(void)
{
	struct Img_Iface_Data_Calibrate	data;

	if (image_copy_tmp.channels() != 1)
		goto err;

	data.intrinsic_mat	= &intrinsic_mat;
	data.dist_coefs		= &dist_coefs;
	data.rvecs		= &rvecs;
	data.tvecs		= &tvecs;

	img_calib3d_act(&image_copy_tmp, IMG_CALIB3D_ACT_CALIBRATE, &data);

	user_iface_log_write(1, "Calibrate");
	return;
err:
	user_iface_log_write(1, "! Invalid input (Must be 1 channel)");
}

static	void	img_iface_undistort		(void)
{
	struct Img_Iface_Data_Undistort	data;

	data.intrinsic_mat	= &intrinsic_mat;
	data.dist_coefs		= &dist_coefs;

	img_calib3d_act(&image_copy_tmp, IMG_CALIB3D_ACT_UNDISTORT, &data);

	user_iface_log_write(1, "Undistort");
}

/* zbar ----------------------------------------------------------------------*/
static	void	img_iface_decode		(void)
{
	void		*imgdata;
	ptrdiff_t	w, h;
	int		type;
	char		bcode_type[BUFSIZ];
	char		bcode_data[BUFSIZ];
	char		txt[LOG_LINE_LEN];

	type	= user_iface_getint(0, 0, INT_MAX,
					"Type of code: (0 for all)", NULL);

	alx_cv_extract_imgdata(&image_copy_tmp, &imgdata, &w, &h,
							NULL, NULL, NULL);
	if (alx_zbar_read(ARRAY_SIZE(bcode_data), bcode_data, bcode_type,
							imgdata, h, w, type))
		goto err;

	if (sbprintf(txt, NULL, "Detect bcodes (type = %i)", type) < 0)
		return;
	user_iface_log_write(1, txt);

	if (sbprintf(txt, NULL, "%s -- '%s'", bcode_type, bcode_data) < 0)
		return;
	user_iface_log_write(2, txt);
	return;
err:
	user_iface_log_write(2, "! No code detected");
}

/* ocr -----------------------------------------------------------------------*/
static	void	img_iface_read			(void)
{
	void		*imgdata;
	ptrdiff_t	w, h, B_per_pix, B_per_line;
	int		lang, conf;
	char		txt[LOG_LINE_LEN];

	lang = user_iface_getint(alx::ocr::LANG_ENG, 0,
				alx::ocr::LANG_DIGITS_COMMA,
				"Language: ENG = 0, SPA = 1, CAT = 2", NULL);
	conf = user_iface_getint(alx::ocr::CONF_NONE, 1,
				alx::ocr::CONF_PRICE_USD,
				"Config: none = 0, Price = 1", NULL);

	alx_cv_extract_imgdata(&image_copy_tmp, &imgdata, &w, &h,
					&B_per_pix, &B_per_line, NULL);
	if (alx_ocr_read(ARRAY_SIZE(img_ocr_text), img_ocr_text, imgdata, w, h,
					B_per_pix, B_per_line, lang, conf))
		goto err;

	if (sbprintf(txt, NULL, "OCR (lang = %i) [conf = %i]", lang, conf) < 0)
		return;
	user_iface_log_write(1, txt);
	if (img_ocr_text[0] == '\0')
		goto err;
	return;
err:
	user_iface_log_write(2, "! No text detected");
}

/* iface ---------------------------------------------------------------------*/
static	void	img_iface_apply			(void)
{

	image_copy_old.release();
	image_copy_tmp.copyTo(image_copy_old);

	user_iface_log_write(1, "Apply changes");
}

static	void	img_iface_discard		(void)
{

	image_copy_tmp.release();
	image_copy_old.copyTo(image_copy_tmp);

	user_iface_log_write(1, "Discard changes");
}

static	void	img_iface_save_mem		(void)
{
	ptrdiff_t	m;
	char		txt[LOG_LINE_LEN];

	m = user_iface_getint(0, 0, IMG_MEM_SIZE - 1, "mem[m];  m:", NULL);

	image_mem[m].release();
	image_copy_tmp.copyTo(image_mem[m]);

	if (sbprintf(txt, NULL, "Save to mem[%ti]", m) < 0)
		return;
	user_iface_log_write(1, txt);
}

static	void	img_iface_load_mem		(void)
{
	ptrdiff_t	m;
	char		txt[LOG_LINE_LEN];

	m = user_iface_getint(0, 0, IMG_MEM_SIZE - 1, "mem[m];  m:", NULL);

	if (image_mem[m].empty())
		goto err;

	image_copy_tmp.release();
	image_mem[m].copyTo(image_copy_tmp);

	if (sbprintf(txt, NULL, "Load from mem[%ti]", m) < 0)
		return;
	user_iface_log_write(1, txt);
	return;
err:
	user_iface_log_write(1, "! Empty memory");
}

static	void	img_iface_save_ref		(void)
{

	image_ref.release();
	image_copy_tmp.copyTo(image_ref);

	user_iface_log_write(1, "Save to reference");
}

/* save/save -----------------------------------------------------------------*/
static	void	img_iface_save_file		(void)
{

	user_iface_log_write(1, "Save as...");

	image.release();
	image_copy_tmp.copyTo(image);
	save_image_file(NULL, NULL);
}

static	void	img_iface_save_update		(void)
{

	image.release();
	image_copy_tmp.copyTo(image);

	user_iface_log_write(1, "Save: update img");
}


/******************************************************************************
 ******* end of file **********************************************************
 ******************************************************************************/