File size: 122,836 Bytes
d4035c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
/*
 *      Coffeeright (c) 2002-2003 by J. Luis
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; version 2 of the License.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      Contact info: w3.ualg.pt/~jluis
 *--------------------------------------------------------------------*/
/* Program:	cvlib_mex.c
 * Purpose:	matlab callable routine to interface with some OpenCV library functions
 *
 * Revision 1.0  31/08/2006 Joaquim Luis
 * Revision 2.0  19/10/2006 JL	Edge 'laplace' needed exlicit kernel input
 * Revision 3.0  27/10/2006 JL	Updated cvHoughCircles call to 1.0
 * Revision 4.0  07/11/2006 JL	Erode & Diltate in cvHoughCircles  (almost the same shit)
 * Revision 5.0  29/11/2006 JL	Added half a dozen of functions more (line, rect, circ, poly, ellip, inpaint)
 * Revision 6.0  02/12/2006 JL	Added FillPoly and FillConvexPoly
 *
 */

#include <math.h>
#include "mex.h"
#include <cv.h>

#define	TRUE	1
#define	FALSE	0

struct CV_CTRL {
	/* active is TRUE if the option has been activated */
	struct UInt8 {			/* Declare byte pointers */
		int active;
		unsigned char *img_out, *img_in, *tmp_img_in, *tmp_img_out;
	} UInt8;
	struct Int8 {			/* Declare byte pointers */
		int active;
		char *img_out, *img_in, *tmp_img_in, *tmp_img_out;
	} Int8;
	struct UInt16 {			/* Declare short int pointers */
		int active;
		unsigned short int *img_out, *img_in, *tmp_img_in, *tmp_img_out;
	} UInt16;
	struct Int16 {			/* Declare unsigned short int pointers */
		int active;
		short int *img_out, *img_in, *tmp_img_in, *tmp_img_out;
	} Int16;
	struct Int32 {			/* Declare unsigned int pointers */
		int active;
		int *img_out, *img_in, *tmp_img_in, *tmp_img_out;
	} Int32;
	struct Float {			/* Declare float pointers */
		int active;
		float *img_out, *img_in, *tmp_img_in, *tmp_img_out;
	} Float;
	struct Double {			/* Declare double pointers */
		int active;
		double *img_out, *img_in, *tmp_img_in, *tmp_img_out;
	} Double;
};

void JapproxPoly(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]);
void Jresize(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]);
void Jfloodfill(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]);
void JgoodFeatures(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]);
void JhoughLines2(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]);
void JhoughCircles(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]);
void Jedge(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[], const char *method);
void JerodeDilate(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[], const char *method);
void JmorphologyEx(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]);
void Jcolor(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]);
void Jarithm(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[], const char *op);
void JaddWeighted(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]);
void Jflip(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]);
void JGetQuadrangleSubPix(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]);
void Jfilter(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]);
void Jsmooth(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]);
void Jegipt(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[], const char *op);
void Jshapes(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[], const char *op);
void Jinpaint(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]);

void Set_pt_Ctrl_in (struct CV_CTRL *Ctrl, const mxArray *pi , mxArray *pit, int interl);
void Set_pt_Ctrl_out1 ( struct CV_CTRL *Ctrl, mxArray *pi );
void Set_pt_Ctrl_out2 (struct CV_CTRL *Ctrl, mxArray *po, int interl);
int getNK(const mxArray *p, int which);

void interleave(struct CV_CTRL *Ctrl, int nx, int ny, int nBands, int dir);
void interleaveUI8(struct CV_CTRL *Ctrl, int nx, int ny, int nBands, int dir);
void interleaveI8(struct CV_CTRL *Ctrl, int nx, int ny, int nBands, int dir);
void interleaveUI16(struct CV_CTRL *Ctrl, int nx, int ny, int nBands, int dir);
void interleaveI16(struct CV_CTRL *Ctrl, int nx, int ny, int nBands, int dir);
void interleaveI32(struct CV_CTRL *Ctrl, int nx, int ny, int nBands, int dir);
void interleaveF32(struct CV_CTRL *Ctrl, int nx, int ny, int nBands, int dir);
void interleaveF64(struct CV_CTRL *Ctrl, int nx, int ny, int nBands, int dir);
void interleaveBlind(unsigned char in[], unsigned char out[], int nx, int ny, int nBands, int dir);
void interleaveDouble(double in[], double out[], int nx, int ny);
void localSetData(struct CV_CTRL *Ctrl, IplImage* img, int dir, int step);
void getDataType(struct CV_CTRL *Ctrl, const mxArray *prhs[], int *nBytes, int *img_depth);
void cvResizeUsage(), floodFillUsage(), goodFeaturesUsage(), houghLines2Usage();
void cannyUsage(), sobelUsage(), laplaceUsage(), erodeUsage(), dilateUsage();
void morphologyexUsage(), colorUsage(), flipUsage(), filterUsage(), findContoursUsage();
void JfindContours(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]);
void arithmUsage(), addWeightedUsage(), pyrDUsage(), pyrUUsage(), houghCirclesUsage();
void smoothUsage(), lineUsage(), plineUsage(), rectUsage(), circUsage(), eBoxUsage();
void inpaintUsage(), fillConvUsage(), fillPlineUsage();



/* --------------------------------------------------------------------------- */
/* Matlab Gateway routine */
void mexFunction(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]) {
	const char *funName;

	if (n_in == 0) {
		mexPrintf("List of currently coded OPENCV functions (original name in parentesis):\n");
		mexPrintf("To get a short online help type cvlib_mex(funname)\n");
		mexPrintf("E.G. cvlib_mex('resize')\n\n");
		mexPrintf("\tadd (cvAdd)\n");
		mexPrintf("\taddS (cvAddS)\n");
		mexPrintf("\taddweighted (cvAddWeighted)\n");
		//mexPrintf("\taffine2 (cvWarpAffine)\n");	// No help yet
		mexPrintf("\tcanny (cvCanny)\n");
		mexPrintf("\tcircle (cvCircle)\n");
		mexPrintf("\tcolor (cvCvtColor)\n");
		mexPrintf("\tcontours (cvFindContours)\n");
		mexPrintf("\tdilate (cvDilate)\n");
		mexPrintf("\tdiv (cvDiv)\n");
		mexPrintf("\teBox (cvEllipseBox)\n");
		mexPrintf("\terode (cvErode)\n");
		mexPrintf("\tfilter (cvFilter2D)\n");
		mexPrintf("\tfillpoly (cvFillPoly)\n");
		mexPrintf("\tflip (cvFlip)\n");
		mexPrintf("\tfloodfill (cvFloodFill)\n");
		mexPrintf("\tgoodfeatures (cvGoodFeaturesToTrack)\n");
		mexPrintf("\thoughlines2 (cvHoughLines2)\n");
		mexPrintf("\thoughcircles (cvHoughCircles)\n");
		mexPrintf("\tinpaint (cvInpaint)\n");
		mexPrintf("\tlaplace (cvLaplace)\n");
		mexPrintf("\tline (cvLine)\n");
		mexPrintf("\tmorphologyex (cvMorphologyEx)\n");
		mexPrintf("\tmul (cvMul)\n");
		mexPrintf("\tpyrD (cvPyrDown)\n");
		mexPrintf("\tpyrU (cvPyrUp)\n");
		mexPrintf("\trectangle (cvRectangle)\n");
		mexPrintf("\tresize (cvResize)\n");
		mexPrintf("\tsmooth (cvSmooth)\n");
		mexPrintf("\tsobel (cvSobel)\n");
		mexPrintf("\tsub (cvSub)\n");
		mexPrintf("\tsubS (cvSubS)\n");
		return;
	}

	if(!mxIsChar(prhs[0]))
		mexErrMsgTxt("CVLIB_MEX: First argument must be a string with the function to call!");
	else
		funName = (char *)mxArrayToString(prhs[0]);

	if (!strcmp(funName,"resize"))
		Jresize(n_out, plhs, n_in, prhs);

	else if (!strcmp(funName,"dp"))
		JapproxPoly(n_out, plhs, n_in, prhs);

	else if (!strcmp(funName,"floodfill"))
		Jfloodfill(n_out, plhs, n_in, prhs);

	else if (!strncmp(funName,"lin",3) || !strncmp(funName,"rec",3) || !strncmp(funName,"cir",3) ||
		!strcmp(funName,"eBox"))
		Jshapes(n_out, plhs, n_in, prhs, funName);


	else if (!strcmp(funName,"goodfeatures"))
		JgoodFeatures(n_out, plhs, n_in, prhs);

	else if (!strcmp(funName,"houghlines2"))
		JhoughLines2(n_out, plhs, n_in, prhs);

	else if (!strcmp(funName,"houghcircles"))
		JhoughCircles(n_out, plhs, n_in, prhs);

	else if (!strcmp(funName,"inpaint"))
		Jinpaint(n_out, plhs, n_in, prhs);

	else if (!strcmp(funName,"contours"))
		JfindContours(n_out, plhs, n_in, prhs);

	else if (!strcmp(funName,"sobel") || !strcmp(funName,"laplace") || !strcmp(funName,"canny"))
		Jedge(n_out, plhs, n_in, prhs, funName);

	else if (!strcmp(funName,"erode") || !strcmp(funName,"dilate"))
		JerodeDilate(n_out, plhs, n_in, prhs, funName);

	else if (!strcmp(funName,"morphologyex"))
		JmorphologyEx(n_out, plhs, n_in, prhs);

	else if (!strcmp(funName,"color"))
		Jcolor(n_out, plhs, n_in, prhs);

	else if (!strcmp(funName,"affine2"))
		JGetQuadrangleSubPix(n_out, plhs, n_in, prhs);

	else if ( !strcmp(funName,"add") || !strcmp(funName,"sub") || !strcmp(funName,"mul") ||
		 !strcmp(funName,"div") || !strcmp(funName,"addS") || !strcmp(funName,"subS") )
		Jarithm(n_out, plhs, n_in, prhs, funName);

	else if (!strcmp(funName,"addweighted"))
		JaddWeighted(n_out, plhs, n_in, prhs);

	else if (!strcmp(funName,"flip"))
		Jflip(n_out, plhs, n_in, prhs);

	else if (!strcmp(funName,"filter"))
		Jfilter(n_out, plhs, n_in, prhs);

	else if (!strcmp(funName,"smooth"))
		Jsmooth(n_out, plhs, n_in, prhs);

	else if (!strcmp(funName,"pyrU") || !strcmp(funName,"pyrD") )
		Jegipt(n_out, plhs, n_in, prhs, funName);

	else
		mexErrMsgTxt("CVLIB_MEX: unrecognized function name!");
}


/* --------------------------------------------------------------------------- */
void Jresize(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]) {
	int nx, ny, nBands, out_dims[3], nx_out, ny_out, nBytes, isMN, img_depth;
	int cv_code = CV_INTER_LINEAR;
	double	*ptr_d, size_fac;
	const char *interpMethod;
	IplImage* src_img = 0;
	IplImage* dst_img = 0;
	mxArray *ptr_in, *ptr_out;

	struct CV_CTRL *Ctrl;
	void *New_Cv_Ctrl (), Free_Cv_Ctrl (struct CV_CTRL *C);

	/* ---- Check for errors in user's call to function.  -------------------- */
	if (n_in == 1) { cvResizeUsage(); return; }
	if (n_out > 2)
		mexErrMsgTxt("RESIZE returns only one argument!");
	if (n_in < 3 || n_in > 4 )
		mexErrMsgTxt("RESIZE needs two or three input arguments!");

	ptr_d = mxGetPr(prhs[2]);
	if (mxGetM(prhs[2]) * mxGetN(prhs[2]) == 2) {
		ny_out = (int)ptr_d[0];		nx_out = (int)ptr_d[1];
		isMN = 1;
	}
	else if (mxGetM(prhs[2]) * mxGetN(prhs[2]) == 1) {
		size_fac = *ptr_d;
		isMN = 0;
	}
	else
		mexErrMsgTxt("RESIZE: second argument must be a scalar or a two elements vector!");

	if (n_in == 4) {
		if(!mxIsChar(prhs[3]))
			mexErrMsgTxt("RESIZE: Third argument must be a valid string!");
		else
			interpMethod = (char *)mxArrayToString(prhs[3]);
		if (!strcmp(interpMethod,"nearest"))
			cv_code = CV_INTER_NN;
		else if (!strcmp(interpMethod,"bilinear"))
			cv_code = CV_INTER_LINEAR;
		else if (!strcmp(interpMethod,"bicubic"))
			cv_code = CV_INTER_CUBIC;
		else if (!strcmp(interpMethod,"area"))
			cv_code = CV_INTER_AREA;
		else
			mexErrMsgTxt("RESIZE: Unknow interpolation method!");
	}

	if ( cv_code != CV_INTER_NN && !(mxIsLogical(prhs[1]) || mxIsUint8(prhs[1]) ||
		 mxIsUint16(prhs[1]) || mxIsSingle(prhs[1])) ) {
		mexPrintf("RESIZE ERROR: Interpolation methods other than nearest-neighbor accept only\n");
		mexErrMsgTxt("           the folloowing input data types: logical, uint8, uint16 & single.\n");
	}

	/* -------------------- End of parsing input ------------------------------------- */

	ny = mxGetM(prhs[1]);	nx = getNK(prhs[1],1);	nBands = getNK(prhs[1],2);
	if (!isMN) {
		nx_out = cvRound(nx * size_fac);
		ny_out = cvRound(ny * size_fac);
	}
	out_dims[0] = ny_out;
	out_dims[1] = nx_out;
	out_dims[2] = nBands;
	/* Allocate and initialize defaults in a new control structure */
	Ctrl = (struct CV_CTRL *) New_Cv_Ctrl ();
	getDataType(Ctrl, prhs, &nBytes, &img_depth);

	/* ------ Create pointers for output and temporary arrays ------------------------ */
	ptr_in  = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		  mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
	ptr_out = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		  out_dims, mxGetClassID(prhs[1]), mxREAL);
	/* ------------------------------------------------------------------------------- */ 

	Set_pt_Ctrl_in ( Ctrl, prhs[1], ptr_in, 1 ); 	/* Set pointer & interleave */
	src_img = cvCreateImageHeader( cvSize(nx, ny), img_depth, nBands );
	localSetData( Ctrl, src_img, 1, nx * nBands * nBytes );

	Set_pt_Ctrl_out1 ( Ctrl, ptr_out ); 
	//dst_img = cvCreateImage(cvSize(nx_out, ny_out), img_depth , nBands );
	dst_img = cvCreateImageHeader(cvSize(nx_out, ny_out), img_depth , nBands );
	localSetData( Ctrl, dst_img, 2, nx_out * nBands * nBytes );

	cvResize(src_img,dst_img,cv_code);

	cvReleaseImageHeader( &src_img );
	mxDestroyArray(ptr_in);

	plhs[0] = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		  out_dims, mxGetClassID(prhs[1]), mxREAL);
	Set_pt_Ctrl_out2 ( Ctrl, plhs[0], 1 ); 		/* Set pointer & desinterleave */

	cvReleaseImageHeader( &dst_img ); //cvCreateImage is ued to initialize it, why not deallocate it accordingly?
	//cvReleaseImage( &dst_img );
	mxDestroyArray(ptr_out); 
	Free_Cv_Ctrl (Ctrl);	/* Deallocate control structure */
}

/* --------------------------------------------------------------------------- */
void Jfloodfill(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]) {
	unsigned char *mask_img, *tmp_mask;
	int nx, ny, nx2, ny2, nBands, m, n, nx_var, c = 0, nBytes, img_depth;

	int lo, up, flags, r, g, b, x, y;
	int ffill_case = 1;
	int lo_diff = 20, up_diff;
	int connectivity = 4;
	int is_mask = 0;
	int new_mask_val = 255;
	double *ptr_d;
	IplImage* src_img = 0;
	IplImage* mask = 0;
	CvPoint seed;
	CvConnectedComp comp;
	CvScalar color;
	CvScalar brightness;
	CvSize roi = {0,0};
	mxArray *ptr_in, *mx_ptr;

	struct CV_CTRL *Ctrl;
	void *New_Cv_Ctrl (), Free_Cv_Ctrl (struct CV_CTRL *C);

	/* ---- Check for errors in user's call to function.  -------------------- */
	if (n_in == 1) { floodFillUsage(); return; }
	else if (n_out > 2 || n_out == 0)
		mexErrMsgTxt("FLOODFILL returns one or two output arguments!");
	else if (n_out == 2)
		is_mask = 1;


	/* Check that input image is of type UInt8 */
	if (!mxIsUint8(prhs[1]))
		mexErrMsgTxt("FLOODFILL ERROR: Invalid input data type. Only valid type is: UInt8.\n");

	if (n_in != 3)
		mexErrMsgTxt("FLOODFILL requires 2 input arguments!");

	else if (mxIsStruct(prhs[2])) {
		mx_ptr = mxGetField(prhs[2], 0, "Point");
		if (mx_ptr == NULL)
			mexErrMsgTxt("FLOODFILL 'Point' field not provided");
		ptr_d = mxGetPr(mx_ptr);
		x = (int)ptr_d[0];	y = (int)ptr_d[1];

		mx_ptr = mxGetField(prhs[2], 0, "Tolerance");
		if (mx_ptr != NULL) {
			ptr_d = mxGetPr(mx_ptr);
			lo_diff = (int)ptr_d[0];
		}

		mx_ptr = mxGetField(prhs[2], 0, "Connect");
		if (mx_ptr != NULL) {
			ptr_d = mxGetPr(mx_ptr);
			connectivity = (int)ptr_d[0];
		}

		mx_ptr = mxGetField(prhs[2], 0, "FillColor");
		if (mx_ptr != NULL) {
			ptr_d = mxGetPr(mx_ptr);
			r = (int)ptr_d[0];
			g = (int)ptr_d[1];
			b = (int)ptr_d[2];
		}
		else {
			b = rand() & 255, g = rand() & 255, r = rand() & 255;
		}
	}
	else {		/* cvfill_mex(img,[x y]) mode */
		if (mxGetM(prhs[2]) * mxGetN(prhs[2]) != 2)
			mexErrMsgTxt("FLOODFILL: Seed point error. Must be a 2 elements vector.");
		ptr_d = mxGetPr(prhs[2]);
		x = (int)ptr_d[0];		y = (int)ptr_d[1];
		b = rand() & 255, g = rand() & 255, r = rand() & 255;
	}
	/* -------------------- End of parsing input ------------------------------------- */

	ny = mxGetM(prhs[1]);	nx = getNK(prhs[1],1);	nBands = getNK(prhs[1],2);
	/* Allocate and initialize defaults in a new control structure */
	Ctrl = (struct CV_CTRL *) New_Cv_Ctrl ();
	getDataType(Ctrl, prhs, &nBytes, &img_depth);

	up_diff = lo_diff;	/* Don't see any reason to have them different */

	/* ------ Create pointer for temporary array ------------------------------------- */
	ptr_in  = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		 mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
	/* ------------------------------------------------------------------------------- */ 

	Set_pt_Ctrl_in ( Ctrl, prhs[1], ptr_in, 1 ); 	/* Set pointer & interleave */

	src_img = cvCreateImageHeader( cvSize(nx, ny), img_depth, nBands );
	localSetData( Ctrl, src_img, 1, nx * nBands * nBytes );

	if( is_mask ) {
		nx2 = nx + 2;	ny2 = ny + 2;
		mask = cvCreateImage( cvSize(nx2, ny2), IPL_DEPTH_8U, 1 );
		cvZero( mask );
	}

	seed = cvPoint(x,y);
	lo = ffill_case == 0 ? 0 : lo_diff;
	up = ffill_case == 0 ? 0 : up_diff;
	flags = connectivity + (new_mask_val << 8) + (ffill_case == 1 ? CV_FLOODFILL_FIXED_RANGE : 0);

	if( nBands == 3 ) {
		color = CV_RGB( r, g, b );
		cvFloodFill( src_img, seed, color, CV_RGB( lo, lo, lo ),
			CV_RGB( up, up, up ), &comp, flags, is_mask ? mask : NULL );
	}
	else {
		brightness = cvRealScalar((r*2 + g*7 + b + 5)/10);
		cvFloodFill( src_img, seed, brightness, cvRealScalar(lo),
			cvRealScalar(up), &comp, flags, is_mask ? mask : NULL );
	}

	plhs[0] = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		 mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);

	/* desinterleave */
	interleaveBlind (Ctrl->UInt8.tmp_img_in, (unsigned char *)mxGetData(plhs[0]), nx, ny, nBands, -1);

	cvReleaseImageHeader( &src_img );
	mxDestroyArray(ptr_in);
	Free_Cv_Ctrl (Ctrl);	/* Deallocate control structure */

	if ( is_mask ) {
		nx_var = mask->widthStep;	/* BUG, this value is changed inside cvFloodFill */
		tmp_mask = (unsigned char *)mxCalloc((nx*ny),sizeof(char));

		/* Crop the mask such that it will have the same size as original image */
		for (m = 1, c = 0; m < ny+1; m++)
			for (n = 1; n < nx+1; n++)
				tmp_mask[c++] = (unsigned char)(mask->imageData[m*nx_var + n] & 1); 

		plhs[1] = mxCreateNumericMatrix(ny, nx, mxLOGICAL_CLASS, mxREAL);
		mask_img = mxGetData(plhs[1]);

		interleaveBlind (tmp_mask, mask_img, nx, ny, 1, -1); /* Change from C to ML order */
		mxFree((void *)tmp_mask);
		cvReleaseImage( &mask );
	}
}


/* --------------------------------------------------------------------------- */
void Jshapes(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[], const char *method) {
	int nx, ny, nBands, nBytes, img_depth, inplace = FALSE;
	int r, g, b, radius = 0, thickness = 1, line_type = 8;
	int ind_opt = 4;	/* Index of first optional argument in prhs */
	double *ptr_d;
	IplImage *src_img = 0, *dst = 0;
	CvPoint pt1, pt2;
	CvScalar color;
	CvBox2D	box;
	mxArray *ptr_in, *mx_ptr;

	struct CV_CTRL *Ctrl;
	void *New_Cv_Ctrl (), Free_Cv_Ctrl (struct CV_CTRL *C);

	/* ---- Check for input and errors in user's call to function. ----------------- */
	if (n_in == 1) { 
		if (!strncmp(method,"lin",3)) lineUsage();
		else if (!strncmp(method,"rec",3)) rectUsage();
		else if (!strncmp(method,"cir",3)) circUsage();
		else if (!strcmp(method,"eBox")) eBoxUsage();
		return;
	}
	else if (n_out > 1 )
		mexErrMsgTxt("SHAPES returns either one or zero arguments!");

	/* Check that input image is of type UInt8 */
	if (!mxIsUint8(prhs[1]))
		mexErrMsgTxt("SHAPES ERROR: Invalid input data type. Only valid type is: UInt8.\n");

	if (!strcmp(method,"eBox") && n_in < 3)
		mexErrMsgTxt("EllipseBox requires at least 2 input arguments!");
	else if (strcmp(method,"eBox") && n_in < 4)
		mexErrMsgTxt("SHAPES requires at least 3 input arguments!");

	if (!strncmp(method,"lin",3) || !strncmp(method,"rec",3)) {
		/* Those are mandatory */
		if (mxGetM(prhs[2]) * mxGetN(prhs[2]) != 2)
			mexErrMsgTxt("SHAPES: First point error. Must be a 2 elements vector.");
		if (mxGetM(prhs[3]) * mxGetN(prhs[3]) != 2)
			mexErrMsgTxt("SHAPES: Second point error. Must be a 2 elements vector.");

		/* OK, now read the two points and assign them to the cvPoint structs */
		ptr_d = (double *)mxGetData(prhs[2]);
		pt1.x = (int)ptr_d[0];		pt1.y = (int)ptr_d[1];
		ptr_d = (double *)mxGetData(prhs[3]);
		pt2.x = (int)ptr_d[0];		pt2.y = (int)ptr_d[1];
	}
	else if (!strncmp(method,"cir",3)) {
		if (mxGetM(prhs[2]) * mxGetN(prhs[2]) != 2)
			mexErrMsgTxt("CIRCLE: First point error - the CENTER. Must be a 2 elements vector.");
		ptr_d = (double *)mxGetData(prhs[2]);
		pt1.x = (int)ptr_d[0];		pt1.y = (int)ptr_d[1];
		ptr_d = (double *)mxGetData(prhs[3]);
		radius = (int)ptr_d[0];
	}
	else if (!strcmp(method,"eBox")) {
		if (mxIsStruct(prhs[2])) {
			mx_ptr = mxGetField(prhs[2], 0, "center");
			if (mx_ptr == NULL)
				mexErrMsgTxt("EllipseBox 'center' field not provided");
			if (mxGetM(mxGetField(prhs[2],0,"center")) * mxGetN(mxGetField(prhs[2],0,"center")) != 2)
				mexErrMsgTxt("EllipseBox: 'center' must contain a 2 elements vector");
			ptr_d = mxGetPr(mx_ptr);
			box.center.x = (float)ptr_d[0];
			box.center.y = (float)ptr_d[1];

			mx_ptr = mxGetField(prhs[2], 0, "size");
			if (ptr_d == NULL)
				mexErrMsgTxt("EllipseBox 'size' field not provided");
			if (mxGetM(mxGetField(prhs[2],0,"size")) * mxGetN(mxGetField(prhs[2],0,"size")) != 2)
				mexErrMsgTxt("EllipseBox: 'size' must contain a 2 elements vector");
			ptr_d = mxGetPr(mx_ptr);
			box.size.width = (float)ptr_d[1];	/* On purpose change of width & height. The man is again */
			box.size.height = (float)ptr_d[0];	/* very confuse. I think they mixed up one and the other */

			mx_ptr = mxGetField(prhs[2], 0, "angle");
			if (mx_ptr == NULL)
				box.angle = 0.0f;
			else {
				ptr_d = mxGetPr(mx_ptr);
				box.angle = (float)ptr_d[0];
			}

			n_in++;		/* Since cvEllipseBox has one less input arg than cvLine, etc */
			ind_opt = 3;
		}
		else
			mexErrMsgTxt("EllipseBox: Second argument must be a structure.");
	}

	color = cvScalarAll(255);	/* Default to a white line */

	if (n_in > 4 && !mxIsEmpty(prhs[ind_opt])) {			/* Line color */
		ptr_d = (double *)mxGetData(prhs[ind_opt]);
		if (mxGetM(prhs[ind_opt]) * mxGetN(prhs[ind_opt]) == 1) {	/* Gray line */
			r = (int)ptr_d[0];
			color = CV_RGB( r, r, r );
		}
		else if (mxGetM(prhs[ind_opt]) * mxGetN(prhs[ind_opt]) == 3) {	/* Color line */
			r = (int)ptr_d[0];	g = (int)ptr_d[1];	b = (int)ptr_d[2];
			color = CV_RGB( r, g, b );
		}
		else
			mexErrMsgTxt("LINE: Fourth argument must be a 1 or a 3 elements vector.");

		ind_opt++;
	}
	if (n_in > 5 && !mxIsEmpty(prhs[ind_opt]))			/* Line thickness */
		thickness = (int)(*mxGetPr(prhs[ind_opt++]));
	if (n_in > 6 && !mxIsEmpty(prhs[ind_opt]))			/* Line type */
		line_type = (int)(*mxGetPr(prhs[ind_opt]));

	if (thickness < 0) thickness = -1;		/* Bug in OpenCV */

	if (n_out == 0)
		inplace = TRUE;
	/* -------------------- End of parsing input ------------------------------------- */

	ny = mxGetM(prhs[1]);	nx = getNK(prhs[1],1);	nBands = getNK(prhs[1],2);
	/* Allocate and initialize defaults in a new control structure */
	Ctrl = (struct CV_CTRL *) New_Cv_Ctrl ();
	getDataType(Ctrl, prhs, &nBytes, &img_depth);

	/* ------ Create pointer for temporary array ------------------------------------- */
	ptr_in  = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		 mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
	/* ------------------------------------------------------------------------------- */ 

	Set_pt_Ctrl_in ( Ctrl, prhs[1], ptr_in, 1 ); 	/* Set pointer & interleave */

	src_img = cvCreateImageHeader( cvSize(nx, ny), img_depth, nBands );
	localSetData( Ctrl, src_img, 1, nx * nBands * nBytes );

	if (!inplace) {
		plhs[0] = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		  			mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
		dst = cvCreateImageHeader( cvSize(nx, ny), img_depth, nBands );
		cvSetImageData( dst, (void *)mxGetData(plhs[0]), nx * nBytes * nBands );
		localSetData( Ctrl, dst, 1, nx * nBands * nBytes );
		if (!strncmp(method,"lin",3))
			cvLine( dst, pt1, pt2, color, thickness, line_type, 0 );
		else if (!strncmp(method,"rec",3))
			cvRectangle( dst, pt1, pt2, color, thickness, line_type, 0 );
		else if (!strncmp(method,"cir",3))
			cvCircle( dst, pt1, radius, color, thickness, line_type, 0 );
		else if (!strcmp(method,"eBox")) 
			cvEllipseBox( dst, box, color, thickness, line_type, 0 );
		interleaveBlind (Ctrl->UInt8.tmp_img_in, (unsigned char *)mxGetData(plhs[0]), nx, ny, nBands, -1);
		cvReleaseImageHeader( &dst );
	}
	else {
		if (!strncmp(method,"lin",3))
			cvLine( src_img, pt1, pt2, color, thickness, line_type, 0 );
		else if (!strncmp(method,"rec",3))
			cvRectangle( src_img, pt1, pt2, color, thickness, line_type, 0 );
		else if (!strncmp(method,"cir",3))
			cvCircle( src_img, pt1, radius, color, thickness, line_type, 0 );
		else if (!strcmp(method,"eBox")) 
			cvEllipseBox( src_img, box, color, thickness, line_type, 0 );
		/* desinterleave */
		interleaveBlind (Ctrl->UInt8.tmp_img_in, (unsigned char *)mxGetData(prhs[1]), nx, ny, nBands, -1);
	}

	cvReleaseImageHeader( &src_img );
	mxDestroyArray(ptr_in);
	Free_Cv_Ctrl (Ctrl);	/* Deallocate control structure */
}

/* --------------------------------------------------------------------------- */
void JgoodFeatures(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]) {
	const int max_features = 100000;
	int nx, ny, n, i, nBytes, img_depth, nBands, corner_count, nOut_corners = max_features;
	unsigned char *ptr_gray;

	double *ptr_d, quality_level = 0.1, min_distance = 10;
	IplImage *src_img = 0, *eig_image, *temp_image, *src_gray;
	CvSize roi = {0,0};
	CvPoint2D32f* corners;
	mxArray *ptr_in;

	struct CV_CTRL *Ctrl;
	void *New_Cv_Ctrl (), Free_Cv_Ctrl (struct CV_CTRL *C);

	/* ---- Check for errors in user's call to function.  -------------------- */
	if (n_in == 1) { goodFeaturesUsage(); return; }
	/* Check that input image is of type UInt8 */
	if ( !mxIsUint8(prhs[1]) )
		mexErrMsgTxt("GOODFEATURES: Invalid input data type. Valid type is: UInt8.\n");
	if (n_out > 1)
		mexErrMsgTxt("GOODFEATURES returns only one output argument!");

	if (n_in >= 3 && !mxIsEmpty(prhs[2]))
		nOut_corners = (int)(*mxGetPr(prhs[2]));
	if (n_in >= 4 && !mxIsEmpty(prhs[3]))
		quality_level = *mxGetPr(prhs[3]);
	if (n_in >= 5 && !mxIsEmpty(prhs[4]))
		min_distance = *mxGetPr(prhs[4]);
	/* -------------------- End of parsing input ------------------------------------- */

	ny = mxGetM(prhs[1]);	nx = getNK(prhs[1],1);	nBands = getNK(prhs[1],2);
	/* Allocate and initialize defaults in a new control structure */
	Ctrl = (struct CV_CTRL *) New_Cv_Ctrl ();
	getDataType(Ctrl, prhs, &nBytes, &img_depth);

	/* ------ Create pointer for temporary array ------------------------------------- */
	ptr_in  = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		  mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
	/* ------------------------------------------------------------------------------- */ 

	Set_pt_Ctrl_in ( Ctrl, prhs[1], ptr_in, 1 ); 	/* Set pointer & interleave */

	src_img = cvCreateImageHeader( cvSize(nx, ny), img_depth, nBands );
	localSetData( Ctrl, src_img, 1, nx * nBands * nBytes );

	if (nBands == 3) {			/* Convert to GRAY */
		src_gray = cvCreateImageHeader( cvSize(nx, ny), 8, 1 );
		ptr_gray = (unsigned char *)mxCalloc (nx*ny, sizeof (unsigned char));
		cvSetImageData( src_gray, (void *)ptr_gray, nx );
		cvCvtColor(src_img, src_gray, CV_BGR2GRAY);
		for (i = 0; i < nx*ny; i++)	/* Copy the transformed image into Ctrl field */
			Ctrl->UInt8.tmp_img_in[i] = ptr_gray[i]; 
		mxFree(ptr_gray);
		cvReleaseImageHeader( &src_gray );

		/* Here we're going to cheat the src_img in order to pretend that its 2D */
		src_img->nChannels = 1;
		src_img->widthStep = nx * nBytes;
		src_img->imageSize = ny * src_img->widthStep;
	}

	eig_image = cvCreateImage( cvSize(nx, ny), 32, 1 );
	temp_image = cvCreateImage( cvSize(nx, ny), 32, 1 );

	corners = (CvPoint2D32f*)cvAlloc(max_features*sizeof(corners));

	quality_level = 0.1; 
	corner_count  = max_features; 
	cvGoodFeaturesToTrack( src_img, eig_image, temp_image, corners,
		&corner_count, quality_level, min_distance, NULL, 3, 0, 0.04 );

	/*if (corner_count > 0 )
		cvFindCornerSubPix( src_img, corners, corner_count,
			cvSize(10,10), cvSize(-1,-1),
			cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03));*/

	cvReleaseImage( &eig_image );
	cvReleaseImage( &temp_image );
	cvReleaseImageHeader( &src_img );
	mxDestroyArray(ptr_in);

	/* ------ GET OUTPUT DATA --------------------------- */ 
	nOut_corners = MIN(nOut_corners,corner_count);
	plhs[0] = mxCreateNumericMatrix(nOut_corners, 2, mxDOUBLE_CLASS, mxREAL);
	ptr_d = mxGetPr(plhs[0]);	
	for ( n = 0; n < nOut_corners; n++ ) {
		ptr_d[n] 	      = corners[n].x + 1;	/* +1 because ML is one-based */
		ptr_d[n+nOut_corners] = corners[n].y + 1;
	}
	cvFree((void **)&corners );
	Free_Cv_Ctrl (Ctrl);	/* Deallocate control structure */
}

/* --------------------------------------------------------------------------- */
void JhoughLines2(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]) {
	int nx, ny, i, nBytes, img_depth, nBands, np, method = CV_HOUGH_PROBABILISTIC, thresh = 50;
	unsigned char *ptr_gray;
	const char *method_s;

	double *ptr_d, *lineS, rho = 1, theta = CV_PI/180, par1 = 50, par2 = 15, a, b, x0, y0;
	IplImage *src_img = 0, *dst_img, *src_gray;
	CvPoint *line;
        CvSeq* lines = 0;
        CvMemStorage* storage;
	mxArray *ptr_in, *ptr_out, *mx_ptr;

	struct CV_CTRL *Ctrl;
	void *New_Cv_Ctrl (), Free_Cv_Ctrl (struct CV_CTRL *C);

	/* ---- Check for input and errors in user's call to function. ----------------- */
	if (n_in == 1) { houghLines2Usage(); return; }
	/* Check that input image is of type UInt8 */
	if ( !(mxIsUint8(prhs[1]) || mxIsLogical(prhs[1])) )
		mexErrMsgTxt("HOUGHLINES2: Invalid input data type. Valid types are: UInt8 OR Logical.\n");

	if (n_out != 1)
		mexErrMsgTxt("HOUGHLINES2 returns one (and one only) output!");

	if (n_in > 2) {
		if(!mxIsChar(prhs[2]))
			 mexErrMsgTxt("CVLIB_MEX: Third argument must contain the METHOD string!");
		else
			method_s = (char *)mxArrayToString(prhs[2]);
		if (strcmp(method_s,"standard") && strcmp(method_s,"probabilistic"))
			mexErrMsgTxt("CVLIB_MEX: Unknown METHOD!");
		if (!strcmp(method_s,"standard"))
			method = CV_HOUGH_STANDARD;
		if (n_in == 8) {		/* We don't do any error tests in this case */
			ptr_d = (double *)(mxGetData(prhs[3]));		rho = ptr_d[0];
			ptr_d = (double *)(mxGetData(prhs[4])); 	theta = ptr_d[0];
			ptr_d = (double *)(mxGetData(prhs[5]));		thresh = (int)ptr_d[0];
			ptr_d = (double *)(mxGetData(prhs[6]));		par1 = ptr_d[0];
			ptr_d = (double *)(mxGetData(prhs[7]));		par2 = ptr_d[0];
		}
	}
	/* -------------------- End of parsing input ------------------------------------- */

	ny = mxGetM(prhs[1]);	nx = getNK(prhs[1],1);	nBands = getNK(prhs[1],2);
	/* Allocate and initialize defaults in a new control structure */
	Ctrl = (struct CV_CTRL *) New_Cv_Ctrl ();
	getDataType(Ctrl, prhs, &nBytes, &img_depth);

	/* ------ Create pointers for output and temporary arrays ---------- */
	ptr_in  = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		  mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
	if (!mxIsLogical(prhs[1]))
		ptr_out = mxCreateNumericMatrix(ny, nx, mxGetClassID(prhs[1]), mxREAL);
	/* ------------------------------------------------------------------------------- */ 

	Set_pt_Ctrl_in ( Ctrl, prhs[1], ptr_in, 1 ); 	/* Set pointer & interleave */

	src_img = cvCreateImageHeader( cvSize(nx, ny), 8, nBands );
	localSetData( Ctrl, src_img, 1, nx * nBands * nBytes );

	if (nBands == 3) {			/* Convert to GRAY */
		src_gray = cvCreateImageHeader( cvSize(nx, ny), 8, 1 );
		ptr_gray = (unsigned char *)mxCalloc (nx*ny, sizeof (unsigned char));
		cvSetImageData( src_gray, (void *)ptr_gray, nx );
		cvCvtColor(src_img, src_gray, CV_BGR2GRAY);
		for (i = 0; i < nx*ny; i++)	/* Copy the transformed image into Ctrl field */
			Ctrl->UInt8.tmp_img_in[i] = ptr_gray[i]; 
		mxFree(ptr_gray);
		cvReleaseImageHeader( &src_gray );

		/* Here we're going to cheat the src_img in order to pretend that its 2D */
		src_img->nChannels = 1;
		src_img->widthStep = nx * nBytes;
		src_img->imageSize = ny * src_img->widthStep;
	}

       	storage = cvCreateMemStorage(0);

	if (!mxIsLogical(prhs[1])) {
		dst_img = cvCreateImageHeader( cvSize(nx, ny), 8, 1 );
		Set_pt_Ctrl_out1 ( Ctrl, ptr_out ); 
		localSetData( Ctrl, dst_img, 2, nx * nBytes );

        	cvCanny( src_img, dst_img, 50, 200, 3 );

		lines = cvHoughLines2( dst_img, storage, method, rho, theta, thresh, par1, par2 );
		cvReleaseImageHeader( &dst_img );
		mxDestroyArray(ptr_out);
	}
	else 		/* Input was already a mask array */
		lines = cvHoughLines2( src_img, storage, method, rho, theta, thresh, par1, par2 );

	cvReleaseImageHeader( &src_img );
	mxDestroyArray(ptr_in);

	/* ------ GET OUTPUT DATA --------------------------- */ 
	np = lines->total;		/* total number of pairs of points */

	plhs[0] = mxCreateCellMatrix(np, 1);
	mx_ptr = mxCreateNumericMatrix(2, 2, mxDOUBLE_CLASS, mxREAL);
	ptr_d = mxGetPr(mx_ptr);
	if (method == CV_HOUGH_PROBABILISTIC) {
		for( i = 0; i < np; i++ ) {
			line = (CvPoint *)cvGetSeqElem(lines,i);
			ptr_d[0] = (double)line[0].y + 1;	/* +1 because ML is one-based */
			ptr_d[1] = (double)line[1].y + 1;
			ptr_d[2] = (double)line[0].x + 1;
			ptr_d[3] = (double)line[1].x + 1;
			mxSetCell(plhs[0],i,mxDuplicateArray(mx_ptr));
		}
	}
	else {		/* standard --> STUPID OUTPUT - NOT WORKING */
		for( i = 0; i < np; i++ ) {
			lineS = (double *)cvGetSeqElem(lines,i);
			a = cos(lineS[1]), b = sin(lineS[1]); 
			x0 = a*lineS[0], y0 = b*lineS[0]; 
			//mexPrintf("Merda i=%d\t%f \t%f \tx0=%f \ty0=%f\n",i,lineS[0],lineS[1],x0,y0);
			ptr_d[0] = cvRound(y0 + 1000*(a));
			ptr_d[1] = cvRound(y0 - 1000*(a)); 
			ptr_d[2] = cvRound(x0 + 1000*(-b)); 
			ptr_d[3] = cvRound(x0 - 1000*(-b));
			mxSetCell(plhs[0],i,mxDuplicateArray(mx_ptr));
		}
	}
	mxDestroyArray(mx_ptr);

	Free_Cv_Ctrl (Ctrl);	/* Deallocate control structure */
}

/* --------------------------------------------------------------------------- */
void JhoughCircles(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]) {
	int nx, ny, i, nBytes, img_depth, nBands, np, thresh = 50, min_radius = 5, max_radius=0;
	unsigned char *ptr_gray;

	float	*circ;
	double	*ptr_d, dp = 1, min_dist = 20, par1 = 50, par2 = 60;
	IplImage *src_img = 0, *src_gray;
	IplConvKernel *element = 0;
        CvSeq* circles = 0;
        CvMemStorage* storage;
	mxArray *ptr_in;

	struct CV_CTRL *Ctrl;
	void *New_Cv_Ctrl (), Free_Cv_Ctrl (struct CV_CTRL *C);

	/* ---- Check for input and errors in user's call to function. ----------------- */
	if (n_in == 1) { houghCirclesUsage(); return; }
	/* Check that input image is of type UInt8 */
	if ( !(mxIsUint8(prhs[1]) || mxIsLogical(prhs[1])) )
		mexErrMsgTxt("HOUGHCIRCLES: Invalid input data type. Valid types are: Logical or UInt8.\n");

	if (n_out != 1)
		mexErrMsgTxt("HOUGHCIRCLES returns one (and one only) output!");

	if (n_in >= 3 && !mxIsEmpty(prhs[2]))
		dp = *mxGetPr(prhs[2]);
	if (n_in >= 4 && !mxIsEmpty(prhs[3]))
		min_dist = *mxGetPr(prhs[3]);
	if (n_in >= 5 && !mxIsEmpty(prhs[4]))
		par1 = *mxGetPr(prhs[4]);
	if (n_in >= 6 && !mxIsEmpty(prhs[5]))
		par2 = *mxGetPr(prhs[5]);
	if (n_in >= 7 && !mxIsEmpty(prhs[6]))
		min_radius = (int)*mxGetPr(prhs[6]);
	if (n_in >= 8 && !mxIsEmpty(prhs[7]))
		max_radius = (int)*mxGetPr(prhs[7]);
	/* -------------------- End of parsing input ------------------------------------- */

	ny = mxGetM(prhs[1]);	nx = getNK(prhs[1],1);	nBands = getNK(prhs[1],2);
	/* Allocate and initialize defaults in a new control structure */
	Ctrl = (struct CV_CTRL *) New_Cv_Ctrl ();
	getDataType(Ctrl, prhs, &nBytes, &img_depth);

	/* ------ Create pointer for temporary arrays ------------------------------------ */
	ptr_in  = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		  mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
	/* ------------------------------------------------------------------------------- */ 

	Set_pt_Ctrl_in ( Ctrl, prhs[1], ptr_in, 1 ); 	/* Set pointer & interleave */

	src_img = cvCreateImageHeader( cvSize(nx, ny), 8, nBands );
	localSetData( Ctrl, src_img, 1, nx * nBands * nBytes );

	/* Don't really know hoe effective the next commands are, but at least it worked ONCE */
       	element = cvCreateStructuringElementEx( 4*2+1, 4*2+1, 4, 4, CV_SHAPE_ELLIPSE, 0 );
	cvErode(src_img,src_img,element,1);
	cvDilate(src_img,src_img,element,1);

	if (nBands == 3) {			/* Convert to GRAY */
		src_gray = cvCreateImageHeader( cvSize(nx, ny), 8, 1 );
		ptr_gray = (unsigned char *)mxCalloc (nx*ny, sizeof (unsigned char));
		cvSetImageData( src_gray, (void *)ptr_gray, nx );
		cvCvtColor(src_img, src_gray, CV_BGR2GRAY);
		for (i = 0; i < nx*ny; i++)	/* Copy the transformed image into Ctrl field */
			Ctrl->UInt8.tmp_img_in[i] = ptr_gray[i]; 
		mxFree(ptr_gray);
		cvReleaseImageHeader( &src_gray );

		/* Here we're going to cheat the src_img in order to pretend that its 2D */
		src_img->nChannels = 1;
		src_img->widthStep = nx * nBytes;
		src_img->imageSize = ny * src_img->widthStep;
	}

       	storage = cvCreateMemStorage(0);

	/* smooth it, otherwise a lot of false circles may be detected */
	cvSmooth( src_img, src_img, CV_GAUSSIAN, 5, 5, 0, 0 );
	circles = cvHoughCircles( src_img, storage, CV_HOUGH_GRADIENT, dp, min_dist,
				 par1, par2, min_radius, max_radius );

	cvReleaseImageHeader( &src_img );
	mxDestroyArray(ptr_in);

	/* ------------------- GET OUTPUT DATA --------------------------- */ 
	np = circles->total;		/* total number of pairs of points */

	plhs[0] = mxCreateNumericMatrix(np, 3, mxDOUBLE_CLASS, mxREAL);
	ptr_d = mxGetPr(plhs[0]);
	for( i = 0; i < np; i++ ) {
		circ = (float *)cvGetSeqElem(circles,i);
		ptr_d[i]      = (double)circ[0] + 1;	/* +1 because ML is one-based */
		ptr_d[i+np]   = (double)circ[1] + 1;
		ptr_d[i+2*np] = (double)circ[2];
	}

	Free_Cv_Ctrl (Ctrl);	/* Deallocate control structure */
}

/* --------------------------------------------------------------------------- */
void JfindContours(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]) {
	int nx, ny, i, j, nBytes, img_depth, nBands, np, ncont = 0;
	unsigned char *ptr_gray;

	double *ptr_d;
	IplImage *src_img = 0, *dst_img, *src_gray;
	CvPoint *PointArray;
        CvSeq *contours = 0, *cont = 0;
        CvMemStorage* storage;
	mxArray *ptr_in, *ptr_out, *mx_ptr;

	struct CV_CTRL *Ctrl;
	void *New_Cv_Ctrl (), Free_Cv_Ctrl (struct CV_CTRL *C);

	/* ---- Check for input and errors in user's call to function. ----------------- */
	if (n_in == 1) { findContoursUsage(); return; }
	/* Check that input image is of type UInt8 */
	if ( !(mxIsUint8(prhs[1]) || mxIsLogical(prhs[1])) )
		mexErrMsgTxt("FINDCONTOURS: Invalid input data type. Valid types are: UInt8 OR Logical.\n");

	if (n_out != 1)
		mexErrMsgTxt("FINDCONTOURS returns one (and one only) output!");
	/* -------------------- End of parsing input ------------------------------------- */

	ny = mxGetM(prhs[1]);	nx = getNK(prhs[1],1);	nBands = getNK(prhs[1],2);
	/* Allocate and initialize defaults in a new control structure */
	Ctrl = (struct CV_CTRL *) New_Cv_Ctrl ();
	getDataType(Ctrl, prhs, &nBytes, &img_depth);

	/* ------ Create pointers for output and temporary arrays ---------- */
	ptr_in  = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		  mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
	if (!mxIsLogical(prhs[1]))
		ptr_out = mxCreateNumericMatrix(ny, nx, mxGetClassID(prhs[1]), mxREAL);
	/* ------------------------------------------------------------------------------- */ 

	Set_pt_Ctrl_in ( Ctrl, prhs[1], ptr_in, 1 ); 	/* Set pointer & interleave */

	src_img = cvCreateImageHeader( cvSize(nx, ny), 8, nBands );
	localSetData( Ctrl, src_img, 1, nx * nBands * nBytes );

	/*
	if (nBands == 3) {			// Convert to GRAY

		mexPrintf("Converting image to gray in cvlib_mex\n");
		src_gray = cvCreateImageHeader( cvSize(nx, ny), 8, 1 );
		ptr_gray = (unsigned char *)mxCalloc (nx*ny, sizeof (unsigned char));
		cvSetImageData( src_gray, (void *)ptr_gray, nx );
		cvCvtColor(src_img, src_gray, CV_BGR2GRAY);
		for (i = 0; i < nx*ny; i++)	// Copy the transformed image into Ctrl field
			Ctrl->UInt8.tmp_img_in[i] = ptr_gray[i]; 
		mxFree(ptr_gray);
		cvReleaseImageHeader( &src_gray );

		// Here we're going to cheat the src_img in order to pretend that its 2D
		src_img->nChannels = 1;
		src_img->widthStep = nx * nBytes;
		src_img->imageSize = ny * src_img->widthStep;
	}
	*/
       	storage = cvCreateMemStorage(0);
	/*
	if (!mxIsLogical(prhs[1])) {

		mexPrintf("Input image is not logical, running canny\n");
		dst_img = cvCreateImageHeader( cvSize(nx, ny), 8, 1 );
		Set_pt_Ctrl_out1 ( Ctrl, ptr_out ); 
		localSetData( Ctrl, dst_img, 2, nx * nBytes );

        	cvCanny( src_img, dst_img, 50, 200, 3 );

		ncont = cvFindContours( dst_img, storage, &contours, sizeof(CvContour),
				CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cvPoint(1,1) );
		cvReleaseImageHeader( &dst_img );
		mxDestroyArray(ptr_out);
	}
	else{ 

		*/
		/* Input was already a mask array */
		//cvFindContours( src_img, storage, &contours, sizeof(CvContour),
		//		CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
		//mexPrintf("Input image is logical\n");
	    ncont = cvFindContours( src_img, storage, &contours, sizeof(CvContour),
				CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cvPoint(1,1) ); //jhhays, changed to 1,1 offset
	//}

	//mexPrintf("Merda2 ncont = %d\n", ncont);

	cvReleaseImageHeader( &src_img );  //why only releasing the header?  because that's all that was created
	mxDestroyArray(ptr_in);

	/* ------ GET OUTPUT DATA --------------------------- */ 
	//for(ncont = 0; contours = contours->h_next; ncont++);	/* count number of contours */
    //mexPrintf("Merda2 ncont = %d\n", ncont);
	//for(i = 0; i < 2; contours = contours->h_prev);	/* rewind the contours sequence - STUPID, but with this CV manual ... */
	
	//i = 0;
	//while(contours = contours->h_prev){
	//	mexPrintf("back to node = %d\n", ncont - i);
	//	i++;
	//}

	//ncont = 523;

	plhs[0] = mxCreateCellMatrix(ncont, 1);
	for( i = 0; i < ncont; i++ ) {
		np = contours->total;		/* This is the number points in contour */
		PointArray = (CvPoint*)malloc( np*sizeof(CvPoint) );	/* Alloc memory for contour point set */
		cvCvtSeqToArray(contours, PointArray, CV_WHOLE_SEQ);	/* Get contour point set. */
		mx_ptr = mxCreateNumericMatrix(np, 2, mxDOUBLE_CLASS, mxREAL);
		ptr_d = mxGetPr(mx_ptr);
		for( j = 0; j < np; j++ ) {
			ptr_d[j] = (double)PointArray[j].y;
			ptr_d[j+np] = (double)PointArray[j].x;
		}
 
		mxSetCell(plhs[0],i,mxDuplicateArray(mx_ptr));
		mxDestroyArray(mx_ptr);   //this seems ok, but the memory leak still exists
		free(PointArray);
		contours = contours->h_next;
	}

	//mxDestroyArray(mx_ptr);
	cvReleaseMemStorage(&storage);
	Free_Cv_Ctrl (Ctrl);	/* Deallocate control structure */
}

/* --------------------------------------------------------------------------- */
void JapproxPoly(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]) {
	int nx, ny, i, j, nBytes, nz, np, ncont = 0;

	double *ptr_d, par1 = 3;
        CvMat *map_matrix = 0, *cont = 0;
	CvMemStorage* storage = cvCreateMemStorage(0);
	mxArray *ptr_in, *ptr_out, *mx_ptr;

	struct CV_CTRL *Ctrl;
	void *New_Cv_Ctrl (), Free_Cv_Ctrl (struct CV_CTRL *C);

	/* ---- Check for input and errors in user's call to function. ------- */
	if (n_in == 1) { findContoursUsage(); return; }
	ptr_d = mxGetPr(prhs[1]);
	if (n_in == 3)
		par1 = *mxGetPr(prhs[2]);
	/* -------------------- End of parsing input ------------------------------------- */

	ny = mxGetM(prhs[1]);	nx = getNK(prhs[1],1);	nz = getNK(prhs[1],2);

	map_matrix = cvCreateMatHeader( ny, nx, CV_64FC1 );
	cvSetData( map_matrix, (void *)ptr_d, nx*8 );

	cont = cvApproxPoly( map_matrix, sizeof(CvMat), storage, CV_POLY_APPROX_DP, par1, 0 );
return;

	/* ------------------- GET OUTPUT DATA --------------------------- */ 
	np = cont->rows;		/* total number of surviving points */

	plhs[0] = mxCreateNumericMatrix(np, nx, mxGetClassID(prhs[1]), mxREAL);
	ptr_d = mxGetPr(plhs[0]);
	//cvGetData( cont, (void *)ptr_d, cont->cols*8 );
	ptr_d = cont->data.db;

	cvReleaseMatHeader( &map_matrix );
}

/* --------------------------------------------------------------------------- */
void Jedge(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[], const char *method) {
	int nx, ny, nBands, i, nBytes, img_depth, kernel = 3, xord = 1, yord = 0;
	unsigned char *ptr_gray;
	double thresh1 = 40, thresh2 = 200, *ptr_d;

	IplImage *src_img = 0, *dst_img, *src_gray, *dst_16;
	mxArray *ptr_in, *ptr_out, *ptr_16;

	struct CV_CTRL *Ctrl;
	void *New_Cv_Ctrl (), Free_Cv_Ctrl (struct CV_CTRL *C);

	/* ---- Check for input and errors in user's call to function. ----------------- */
	if (!strcmp(method,"canny")) {
		if (n_in == 1) { cannyUsage();	return; }
		if (!(mxIsUint8(prhs[1]) || mxIsLogical(prhs[1])))
			mexErrMsgTxt("CANNY requires that input image is of uint8 or logical type!");
		if (n_in == 5) {
			ptr_d = mxGetPr(prhs[2]);	thresh1 = (double)ptr_d[0];
			ptr_d = mxGetPr(prhs[3]);	thresh2 = (double)ptr_d[0];
			ptr_d = mxGetPr(prhs[4]);	kernel = (int)ptr_d[0];
		}
	}
	else if (!strcmp(method,"sobel")) {
		if (n_in == 1) { sobelUsage();	return; }
		if ( !( mxIsUint8(prhs[1]) || mxIsSingle(prhs[1]) ) )
			mexErrMsgTxt("SOBEL requires image of uint8 or single type!");
		if (n_in == 5) {
			ptr_d = mxGetPr(prhs[2]);	xord = (int)ptr_d[0];
			ptr_d = mxGetPr(prhs[3]);	yord = (int)ptr_d[0];
			ptr_d = mxGetPr(prhs[4]);	kernel = (int)ptr_d[0];
		}
	}
	else {
		if (n_in == 1) { laplaceUsage();	return; }
		if ( !( mxIsUint8(prhs[1]) || mxIsSingle(prhs[1]) ) )
			mexErrMsgTxt("LAPLACE requires image of uint8 or single type!");
		if (n_in == 3) { 
			ptr_d = mxGetPr(prhs[2]);	kernel = (int)ptr_d[0];
		}
	}
	if (!(kernel == -1 || kernel == 1 || kernel == 3 || kernel == 5 || kernel == 7) )
		mexErrMsgTxt("CANNY/SOBEL/LAPLACE: Wrong kernel size!");

	if (n_out != 1) {
		mexPrintf("%s returns one (and one only) output\n", method);
		mexErrMsgTxt("");
	}
	/* -------------------- End of parsing input ------------------------------------- */

	ny = mxGetM(prhs[1]);	nx = getNK(prhs[1],1);	nBands = getNK(prhs[1],2);
	/* Allocate and initialize defaults in a new control structure */
	Ctrl = (struct CV_CTRL *) New_Cv_Ctrl ();
	getDataType(Ctrl, prhs, &nBytes, &img_depth);

	/* ------ Create pointers for output and temporary arrays ------------------------ */
	ptr_in  = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		  mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
	ptr_out = mxCreateNumericMatrix(ny, nx, mxGetClassID(prhs[1]), mxREAL);
	/* ------------------------------------------------------------------------------- */ 

	Set_pt_Ctrl_in ( Ctrl, prhs[1], ptr_in, 1 ); 	/* Set pointer & interleave */

	src_img = cvCreateImageHeader( cvSize(nx, ny), img_depth, nBands );
	localSetData( Ctrl, src_img, 1, nx * nBands * nBytes );

	if (nBands == 3) {			/* Convert to GRAY */
		src_gray = cvCreateImageHeader( cvSize(nx, ny), 8, 1 );
		ptr_gray = (unsigned char *)mxCalloc (nx*ny, sizeof (unsigned char));
		cvSetImageData( src_gray, (void *)ptr_gray, nx );
		cvCvtColor(src_img, src_gray, CV_BGR2GRAY);
		for (i = 0; i < nx*ny; i++)	/* Copy the transformed image into Ctrl field */
			Ctrl->UInt8.tmp_img_in[i] = ptr_gray[i]; 
		mxFree(ptr_gray);
		cvReleaseImageHeader( &src_gray );

		/* Here we're going to cheat the src_img in order to pretend that its 2D */
		src_img->nChannels = 1;
		src_img->widthStep = nx * nBytes;
		src_img->imageSize = ny * src_img->widthStep;
	}

	dst_img = cvCreateImageHeader( cvSize(nx, ny), img_depth, 1 );
	Set_pt_Ctrl_out1 ( Ctrl, ptr_out ); 
	localSetData( Ctrl, dst_img, 2, nx * nBytes );

	if (!strcmp(method,"canny"))
		cvCanny(src_img,dst_img, thresh1, thresh2, kernel);
	else if (Ctrl->UInt8.active) {
 		Ctrl->UInt8.active = FALSE;
 		Ctrl->Int16.active = TRUE;
		ptr_16 = mxCreateNumericMatrix(ny, nx, mxINT16_CLASS, mxREAL);
		dst_16 = cvCreateImageHeader( cvSize(nx, ny), IPL_DEPTH_16S, 1 );
		Set_pt_Ctrl_out1 ( Ctrl, ptr_16 ); 
		localSetData( Ctrl, dst_16, 2, nx * 2 );
		if (!strcmp(method,"laplace"))
			cvLaplace(src_img,dst_16, kernel);
		else
			cvSobel(src_img,dst_16, xord, yord, kernel);
		// Now we have to convert the dst_16 back to UInt8 
		cvConvertScaleAbs(dst_16, dst_img, 1, 0); 
		cvReleaseImageHeader( &dst_16 );
		mxDestroyArray(ptr_16);
 		Ctrl->UInt8.active = TRUE;
 		Ctrl->Int16.active = FALSE;
	}
	else if (!strcmp(method,"laplace"))	/* When src_img is not of uint8 type */
		cvLaplace(src_img,dst_img, kernel);
	else if (!strcmp(method,"sobel"))
		cvSobel(src_img,dst_img, xord, yord, kernel);


	if (Ctrl->UInt8.active == TRUE ) {
		for (i = 0; i < nx*ny; i++)
			Ctrl->UInt8.tmp_img_out[i] = Ctrl->UInt8.tmp_img_out[i] & 1; 
	}

	cvReleaseImageHeader( &src_img );
	mxDestroyArray(ptr_in);

	if (Ctrl->UInt8.active == TRUE )
		plhs[0] = mxCreateNumericMatrix(ny, nx, mxLOGICAL_CLASS, mxREAL);
	else
		plhs[0] = mxCreateNumericMatrix(ny, nx, mxGetClassID(prhs[1]), mxREAL);

	Set_pt_Ctrl_out2 ( Ctrl, plhs[0], 1 ); 		/* Set pointer & desinterleave */

	Free_Cv_Ctrl (Ctrl);	/* Deallocate control structure */
	cvReleaseImageHeader( &dst_img );
	mxDestroyArray(ptr_out);
}

/* --------------------------------------------------------------------------- */
void JmorphologyEx(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]) {
	int nx, ny, nBands, nBytes, img_depth, cv_code, iterations = 1;
	const char *operation;
	double *ptr_d;

	IplImage *src_img = 0, *dst_img, *tmp_img;
	mxArray *ptr_in, *ptr_out;

	struct CV_CTRL *Ctrl;
	void *New_Cv_Ctrl (), Free_Cv_Ctrl (struct CV_CTRL *C);

	/* ---- Check for input and errors in user's call to function. ----------------- */
	if (n_in == 1) { morphologyexUsage();	return; }

	if ( !( mxIsUint8(prhs[1]) || mxIsSingle(prhs[1]) || mxIsLogical(prhs[1]) ) )
		mexErrMsgTxt("MORPHOLOGYEX requires image of uint8 or single type!");

	if (n_in < 3)
		mexErrMsgTxt("MORPHOLOGYEX requires at least 2 input arguments!");

	if(!mxIsChar(prhs[2]))
		mexErrMsgTxt("MORPHOLOGYEX: Second argument must be a valid string!");
	else
		operation = (char *)mxArrayToString(prhs[2]);

	if (!strcmp(operation,"open"))
		cv_code = CV_MOP_OPEN;
	else if (!strcmp(operation,"close"))
		cv_code = CV_MOP_CLOSE;
	else if (!strcmp(operation,"gradient"))
		cv_code = CV_MOP_GRADIENT;
	else if (!strcmp(operation,"tophat"))
		cv_code = CV_MOP_TOPHAT;
	else if (!strcmp(operation,"blackhat"))
		cv_code = CV_MOP_BLACKHAT;
	else
		mexErrMsgTxt("MORPHOLOGYEX: Unknow operation!");

	if (n_out != 1)
		mexErrMsgTxt("MORPHOLOGYEX: returns one (and one only) output");

	if (n_in == 4) {
		ptr_d = mxGetPr(prhs[3]);	iterations = (int)ptr_d[0];
	}
	/* -------------------- End of parsing input ------------------------------------- */

	ny = mxGetM(prhs[1]);	nx = getNK(prhs[1],1);	nBands = getNK(prhs[1],2);
	/* Allocate and initialize defaults in a new control structure */
	Ctrl = (struct CV_CTRL *) New_Cv_Ctrl ();
	getDataType(Ctrl, prhs, &nBytes, &img_depth);

	/* ------ Create pointers for output and temporary arrays ------------------------ */
	ptr_in  = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		  mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
	ptr_out = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		  mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
	/* ------------------------------------------------------------------------------- */ 

	Set_pt_Ctrl_in ( Ctrl, prhs[1], ptr_in, 1 ); 	/* Set pointer & interleave */
	src_img = cvCreateImageHeader( cvSize(nx, ny), img_depth, nBands );
	localSetData( Ctrl, src_img, 1, nx * nBands * nBytes );

	Set_pt_Ctrl_out1 ( Ctrl, ptr_out ); 
	dst_img = cvCreateImage(cvSize(nx, ny), img_depth , nBands );
	localSetData( Ctrl, dst_img, 2, nx * nBands * nBytes );

	if (cv_code == CV_MOP_GRADIENT || (iterations > 1 && (cv_code == CV_MOP_TOPHAT || cv_code == CV_MOP_BLACKHAT) )) {
		tmp_img = cvCreateImage( cvSize(nx, ny), img_depth, nBands );
	}
	else
		tmp_img = NULL;

	cvMorphologyEx(src_img,dst_img,tmp_img,NULL,cv_code,iterations);

	if (cv_code == CV_MOP_GRADIENT || (iterations > 1 && (cv_code == CV_MOP_TOPHAT || cv_code == CV_MOP_BLACKHAT) ))
		cvReleaseImage( &tmp_img );

	cvReleaseImageHeader( &src_img );
	mxDestroyArray(ptr_in);

	plhs[0] = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		  mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
	Set_pt_Ctrl_out2 ( Ctrl, plhs[0], 1 ); 		/* Set pointer & desinterleave */

	cvReleaseImageHeader( &dst_img );
	mxDestroyArray(ptr_out);
	Free_Cv_Ctrl (Ctrl);	/* Deallocate control structure */
}

/* --------------------------------------------------------------------------- */
void JerodeDilate(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[], const char *method) {
	int nx, ny, nBands, nBytes, img_depth, kernel = 3, iterations = 1;
	double *ptr_d;

	IplImage *src_img = 0, *dst_img;
	mxArray *ptr_in;

	struct CV_CTRL *Ctrl;
	void *New_Cv_Ctrl (), Free_Cv_Ctrl (struct CV_CTRL *C);

	/* ---- Check for input and errors in user's call to function. ----------------- */
	if (!strcmp(method,"erode"))
		if (n_in == 1) { erodeUsage();	return; }
	else if (!strcmp(method,"dilate"))
		if (n_in == 1) { dilateUsage();	return; }

	if ( !( mxIsUint8(prhs[1]) || mxIsSingle(prhs[1]) || mxIsLogical(prhs[1])) ) {
		mexPrintf("%s requires image of uint8 or single type!\n", method);
		mexErrMsgTxt("");
	}

	if (n_out != 1)
		mexErrMsgTxt("ERODE/DILATE: returns one (and one only) output");

	if (n_in == 3) {
		ptr_d = mxGetPr(prhs[2]);	iterations = (int)ptr_d[0];
	}
	/* -------------------- End of parsing input ------------------------------------- */

	ny = mxGetM(prhs[1]);	nx = getNK(prhs[1],1);	nBands = getNK(prhs[1],2);
	/* Allocate and initialize defaults in a new control structure */
	Ctrl = (struct CV_CTRL *) New_Cv_Ctrl ();
	getDataType(Ctrl, prhs, &nBytes, &img_depth);

	/* ------ Create pointers for output and temporary arrays ------------------------ */
	ptr_in  = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		  mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
	//ptr_out = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		  //mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
	/* ------------------------------------------------------------------------------- */ 

	Set_pt_Ctrl_in ( Ctrl, prhs[1], ptr_in, 1 ); 	/* Set pointer & interleave */
	src_img = cvCreateImageHeader( cvSize(nx, ny), img_depth, nBands );
	localSetData( Ctrl, src_img, 1, nx * nBands * nBytes );

	//Set_pt_Ctrl_out1 ( Ctrl, ptr_out ); 
	Set_pt_Ctrl_out1 ( Ctrl, ptr_in ); 		/* Reuse memory */
	dst_img = cvCreateImage(cvSize(nx, ny), img_depth , nBands );
	localSetData( Ctrl, dst_img, 2, nx * nBands * nBytes );

	if (!strcmp(method,"erode"))
		cvErode(src_img,dst_img,NULL,iterations);
	else
		cvDilate(src_img,dst_img,NULL,iterations);

	//cvReleaseImageHeader( &src_img );
	//mxDestroyArray(ptr_in);

	plhs[0] = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		  mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
	Set_pt_Ctrl_out2 ( Ctrl, plhs[0], 1 ); 		/* Set pointer & desinterleave */

	cvReleaseImageHeader( &src_img );
	cvReleaseImageHeader( &dst_img );
	mxDestroyArray(ptr_in);
	//mxDestroyArray(ptr_out);
	Free_Cv_Ctrl (Ctrl);	/* Deallocate control structure */
}

/* --------------------------------------------------------------------------- */
void Jcolor(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]) {
	int nx, ny, nBands, nBands_out, nBytes, img_depth, cv_code, out_dims[3];
	char	*argv;

	IplImage *src_img = 0, *dst_img;
	mxArray *ptr_in, *ptr_out;

	struct CV_CTRL *Ctrl;
	void *New_Cv_Ctrl (), Free_Cv_Ctrl (struct CV_CTRL *C);

	/* ---- Check for input and errors in user's call to function. ----------------- */
	if (n_in == 1) { colorUsage();	return; }
	if (n_out > 1)
		mexErrMsgTxt("COLOR returns only one output argument!");
	if (n_in != 3)
		mexErrMsgTxt("COLOR requires 2 input arguments!");

	if(!mxIsChar(prhs[2]))
		mexErrMsgTxt("COLOR Second argument must be a valid string!");
	else {
		argv = (char *)mxArrayToString(prhs[2]);
		//<X>/<Y> = RGB, BGR, GRAY, HSV, YCrCb, XYZ, Lab, Luv, HLS
		if (!strcmp(argv,"rgb2lab"))
			cv_code = CV_BGR2Lab;
		else if (!strcmp(argv,"lab2rgb"))
			cv_code = CV_Lab2BGR;

		else if (!strcmp(argv,"rgb2luv"))
			cv_code = CV_BGR2Luv;
		else if (!strcmp(argv,"luv2rgb"))
			cv_code = CV_Luv2BGR;

		else if (!strcmp(argv,"rgb2xyz"))
			cv_code = CV_BGR2XYZ;
		else if (!strcmp(argv,"xyz2rgb"))
			cv_code = CV_XYZ2BGR;

		else if (!strcmp(argv,"rgb2yiq") || !strcmp(argv,"rgb2gray"))
			cv_code = CV_BGR2GRAY;

		else if (!strcmp(argv,"rgb2hsv"))
			cv_code = CV_BGR2HSV;
		else if (!strcmp(argv,"hsv2rgb"))
			cv_code = CV_HSV2BGR;

		else if (!strcmp(argv,"rgb2hls"))
			cv_code = CV_BGR2HLS;
		else if (!strcmp(argv,"hls2rgb"))
			cv_code = CV_HLS2BGR;

		else if (!strcmp(argv,"rgb2YCrCb"))
			cv_code = CV_BGR2YCrCb;
		else if (!strcmp(argv,"YCrCb2rgb"))
			cv_code = CV_YCrCb2BGR;

		else {
			mexPrintf("CVCOLOR ERROR: Unknown conversion type string.\n");
			mexPrintf("Valid types: rgb2lab,lab2rgb, rgb2luv,luv2rgb, rgb2xyz,xyz2rgb\n");
			mexPrintf("             rgb2yiq,yiq2rgb, rgb2hsv,luv2hsv, rgb2gray,gray2rgb\n");
			mexErrMsgTxt("             rgb2hsl,hsl2rgb, rgb2YCrCb,YCrCb2rgb.\n");
		}
	}

	if ( !(mxIsUint8(prhs[1]) || mxIsUint16(prhs[1]) || mxIsSingle(prhs[1])) )
		mexErrMsgTxt("COLOR ERROR: Invalid type. Valid types are: uint8, uint16 or single.\n");
	if (n_out != 1)
		mexErrMsgTxt("COLOR: returns one (and one only) output");
	/* -------------------- End of parsing input ------------------------------------- */

	ny = mxGetM(prhs[1]);	nx = getNK(prhs[1],1);	nBands = getNK(prhs[1],2);
	if (nBands == 1)
		mexErrMsgTxt("COLOR ERROR: input must be a MxNx3 array.\n");

	out_dims[0] = ny;	out_dims[1] = nx;
	if (cv_code == CV_RGB2GRAY || cv_code == CV_BGR2GRAY) {	/* rgb2gray */
		nBands_out = 1;
		out_dims[2] = nBands_out;
	}
	else {
		nBands_out = nBands;
		out_dims[2] = nBands_out;
	}
	
	/* Allocate and initialize defaults in a new control structure */
	Ctrl = (struct CV_CTRL *) New_Cv_Ctrl ();
	getDataType(Ctrl, prhs, &nBytes, &img_depth);

	/* ------ Create pointers for output and temporary arrays ------------------------ */
	ptr_in  = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		  			mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
	ptr_out = mxCreateNumericArray((nBands_out == 1) ? 2 : 3,
		  			out_dims, mxGetClassID(prhs[1]), mxREAL);
	/* ------------------------------------------------------------------------------- */ 

	Set_pt_Ctrl_in ( Ctrl, prhs[1], ptr_in, 1 ); 	/* Set pointer & interleave */
	src_img = cvCreateImageHeader( cvSize(nx, ny), img_depth, nBands );
	localSetData( Ctrl, src_img, 1, nx * nBands * nBytes );

	Set_pt_Ctrl_out1 ( Ctrl, ptr_out ); 
	dst_img = cvCreateImageHeader( cvSize(nx, ny), img_depth, nBands_out );
	localSetData( Ctrl, dst_img, 2, nx * nBands_out * nBytes );

	cvCvtColor(src_img, dst_img, cv_code);

	cvReleaseImageHeader( &src_img );
	mxDestroyArray(ptr_in);

	plhs[0] = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]), out_dims, mxGetClassID(prhs[1]), mxREAL);

	Set_pt_Ctrl_out2 ( Ctrl, plhs[0], 1 ); 		/* Set pointer & desinterleave */

	Free_Cv_Ctrl (Ctrl);	/* Deallocate control structure */
	cvReleaseImageHeader( &dst_img );
	mxDestroyArray(ptr_out);
}

/* --------------------------------------------------------------------------- */
void JGetQuadrangleSubPix(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]) {
	int nx, ny, nBands, nBytes, img_depth;
	double *ptr_d;

	IplImage *src, *dst;
	CvMat *map_matrix;
	mxArray *ptr_in, *ptr_out;

	struct CV_CTRL *Ctrl;
	void *New_Cv_Ctrl (), Free_Cv_Ctrl (struct CV_CTRL *C);

	/* ---- Check for errors in user's call to function. ----------------------------- */
	ptr_d = mxGetPr(prhs[2]);
	/* -------------------- End of parsing input ------------------------------------- */

	ny = mxGetM(prhs[1]);	nx = getNK(prhs[1],1);	nBands = getNK(prhs[1],2);

	/* Allocate and initialize defaults in a new control structure */
	Ctrl = (struct CV_CTRL *) New_Cv_Ctrl ();
	getDataType(Ctrl, prhs, &nBytes, &img_depth);

	/* ------ Create pointers for output and temporary arrays ------------------------ */
	ptr_in  = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		  			mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
	ptr_out = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		  			mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
	/* ------------------------------------------------------------------------------- */ 

	Set_pt_Ctrl_in ( Ctrl, prhs[1], ptr_in, 1 ); 	/* Set pointer & interleave */
	src = cvCreateImageHeader( cvSize(nx, ny), img_depth, nBands );
	localSetData( Ctrl, src, 1, nx * nBands * nBytes );

	map_matrix = cvCreateMatHeader( 2, 3, CV_64FC1 );
	//map_matrix = cvMat( 2, 3, CV_64FC1, (void *)ptr_d );
	cvSetData( map_matrix, (void *)ptr_d, 3*8 );

	Set_pt_Ctrl_out1 ( Ctrl, ptr_out ); 
	dst = cvCreateImageHeader( cvSize(nx, ny), img_depth, nBands );
	localSetData( Ctrl, dst, 2, nx * nBands * nBytes );

	//cvGetQuadrangleSubPix( src,  dst, map_matrix );
	cvWarpAffine( src,  dst, map_matrix,CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS,cvScalarAll(0) );

	cvReleaseImageHeader( &src );
	mxDestroyArray(ptr_in);

	plhs[0] = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		  			mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
	Set_pt_Ctrl_out2 ( Ctrl, plhs[0], 1 ); 		/* Set pointer & desinterleave */

	cvReleaseImageHeader( &dst );
	mxDestroyArray(ptr_out);
	cvReleaseMatHeader( &map_matrix );
	//cvReleaseMat( &map_matrix );
	Free_Cv_Ctrl (Ctrl);	/* Deallocate control structure */
}

/* --------------------------------------------------------------------------- */
void Jfilter(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]) {
	int nx, ny, nBands, nBytes, img_depth, nFiltRows, nFiltCols;
	double *ptr_d, *kernel;

	IplImage *src, *dst;
	CvMat *filter;
	mxArray *ptr_in;

	struct CV_CTRL *Ctrl;
	void *New_Cv_Ctrl (), Free_Cv_Ctrl (struct CV_CTRL *C);

	/* ---- Check for errors in user's call to function. ----------------------------- */
	if (n_in == 1) { filterUsage();	return; }
	if (n_in != 3)
		mexErrMsgTxt("FILTER: requires 2 input arguments!");
	ptr_d = mxGetPr(prhs[2]);
	if ( mxIsDouble(prhs[1]) || mxIsInt8(prhs[1]) || mxIsInt32(prhs[1]) )
		mexErrMsgTxt("FILTER: IMG type not supported (supported: uint8, int16, uint16 or single)!");
	if ( !mxIsDouble(prhs[2]) )
		mexErrMsgTxt("FILTER: second argument must be a double!");
	if (n_out != 1)
		mexErrMsgTxt("FILTER: returns one (and one only) output");
	nFiltRows = mxGetM(prhs[2]);
	nFiltCols = mxGetN(prhs[2]);
	/* -------------------- End of parsing input ------------------------------------- */

	ny = mxGetM(prhs[1]);	nx = getNK(prhs[1],1);	nBands = getNK(prhs[1],2);
	
	/* Allocate and initialize defaults in a new control structure */
	Ctrl = (struct CV_CTRL *) New_Cv_Ctrl ();
	getDataType(Ctrl, prhs, &nBytes, &img_depth);

	/* ------ Create pointers for output and temporary arrays ------------------------ */
	ptr_in  = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		  			mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
	kernel = (double *)mxCalloc(nFiltCols * nFiltRows, sizeof(double));
	interleaveDouble(ptr_d, kernel, nFiltCols, nFiltRows);
	/* ------------------------------------------------------------------------------- */ 

	Set_pt_Ctrl_in ( Ctrl, prhs[1], ptr_in, 1 ); 	/* Set pointer & interleave */
	src = cvCreateImageHeader( cvSize(nx, ny), img_depth, nBands );
	localSetData( Ctrl, src, 1, nx * nBands * nBytes );

	filter = cvCreateMatHeader( nFiltRows, nFiltCols, CV_64FC1 );
	cvSetData( filter, (void *)kernel, nFiltCols*8 );

	Set_pt_Ctrl_out1 ( Ctrl, ptr_in ); 	/* Reuse the same memory */
	dst = cvCreateImageHeader( cvSize(nx, ny), img_depth, nBands );
	localSetData( Ctrl, dst, 2, nx * nBands * nBytes );

	cvFilter2D( src, dst, filter, cvPoint(-1,-1) ); 

	plhs[0] = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
	  			mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
	Set_pt_Ctrl_out2 ( Ctrl, plhs[0], 1 ); 		/* Set pointer & desinterleave */

	cvReleaseImageHeader( &dst );
	cvReleaseImageHeader( &src );
	cvReleaseMatHeader( &filter );
	mxFree((void *)kernel);
	mxDestroyArray(ptr_in);
	Free_Cv_Ctrl (Ctrl);	/* Deallocate control structure */
}

/* --------------------------------------------------------------------------- */
void Jsmooth(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]) {
	int nx, ny, nBands, nBytes, img_depth, par1 = 5, par2 = 0, method = CV_GAUSSIAN;
	double par3 = 0, par4 = 0;
	const char *method_s;
	IplImage *src, *dst;
	mxArray *ptr_in;
	struct CV_CTRL *Ctrl;
	void *New_Cv_Ctrl (), Free_Cv_Ctrl (struct CV_CTRL *C);

	/* ---- Check for errors in user's call to function. ----------------------------- */
	if (n_in == 1) { smoothUsage();	return; }

	if (n_in > 2) {
		if(!mxIsChar(prhs[2]))
			mexErrMsgTxt("CVLIB_MEX: Third argument must contain the smoothtype METHOD string!");
		else
			method_s = (char *)mxArrayToString(prhs[2]);

		if (!strcmp(method_s,"blur"))
			method = CV_BLUR;
		else if (!strcmp(method_s,"gaussian"))
			method = CV_GAUSSIAN;
		else if (!strcmp(method_s,"median"))
			method = CV_MEDIAN;
		else if (!strcmp(method_s,"bilateral"))
			method = CV_BILATERAL;
		else
			mexErrMsgTxt("SMOOTH: Unknown METHOD!");

		if (n_in >= 4 && !mxIsEmpty(prhs[3]))
			par1 = (int)(*mxGetPr(prhs[3]));
		if (n_in >= 5 && !mxIsEmpty(prhs[4]))
			par2 = (int)(*mxGetPr(prhs[4]));
		if (n_in >= 6 && !mxIsEmpty(prhs[5]))
			par3 = *mxGetPr(prhs[5]);
		if (n_in == 7 && !mxIsEmpty(prhs[6]))
			par4 = *mxGetPr(prhs[6]);
	}

	if ( !(mxIsSingle(prhs[1]) || mxIsUint8(prhs[1]) || mxIsLogical(prhs[1])) )
		mexErrMsgTxt("SMOOTH: IMG type not supported (supported: logical, uint8 or single)!");

	if ( !(mxIsUint8(prhs[1]) || mxIsLogical(prhs[1])) && (method == CV_MEDIAN || method == CV_BILATERAL) )
		mexErrMsgTxt("SMOOTH: When smoothingtype is 'median' or 'bilateral' IMG type must be logical or uint8!");

	if (n_out != 1)
		mexErrMsgTxt("SMOOTH: returns one (and one only) output");

	if ( method == CV_BILATERAL && par1 == 5 && par2 == 0 ) 	/* Use default bilateral values */
		par2 = 50;
	/* -------------------- End of parsing input ------------------------------------- */

	ny = mxGetM(prhs[1]);	nx = getNK(prhs[1],1);	nBands = getNK(prhs[1],2);
	
	/* Allocate and initialize defaults in a new control structure */
	Ctrl = (struct CV_CTRL *) New_Cv_Ctrl ();
	getDataType(Ctrl, prhs, &nBytes, &img_depth);

	/* ------ Create pointers for output and temporary arrays ------------------------ */
	ptr_in  = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		  			mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
	/* ------------------------------------------------------------------------------- */ 

	Set_pt_Ctrl_in ( Ctrl, prhs[1], ptr_in, 1 ); 	/* Set pointer & interleave */
	src = cvCreateImageHeader( cvSize(nx, ny), img_depth, nBands );
	localSetData( Ctrl, src, 1, nx * nBands * nBytes );

	Set_pt_Ctrl_out1 ( Ctrl, ptr_in ); 	/* Reuse the same memory */
	dst = cvCreateImageHeader( cvSize(nx, ny), img_depth, nBands );
	localSetData( Ctrl, dst, 2, nx * nBands * nBytes );

	cvSmooth( src, dst, method, par1, par2, par3, par4 );	

	plhs[0] = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
	  			mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
	Set_pt_Ctrl_out2 ( Ctrl, plhs[0], 1 ); 		/* Set pointer & desinterleave */

	cvReleaseImageHeader( &dst );
	cvReleaseImageHeader( &src );
	mxDestroyArray(ptr_in);
	Free_Cv_Ctrl (Ctrl);	/* Deallocate control structure */
}

/* --------------------------------------------------------------------------- */
void Jegipt(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[], const char *op) {
	int nx, ny, nxOut, nyOut, nBands, nBytes, img_depth, out_dims[3];

	IplImage *src, *dst;
	mxArray *ptr_in, *ptr_out;

	struct CV_CTRL *Ctrl;
	void *New_Cv_Ctrl (), Free_Cv_Ctrl (struct CV_CTRL *C);

	/* ---- Check for errors in user's call to function. ----------------------------- */
	if (!strcmp(op,"pyrU"))
		if (n_in == 1) { pyrUUsage();	return; }
	else if (!strcmp(op,"pyrD"))
		if (n_in == 1) { pyrDUsage();	return; }

	if ( mxIsInt8(prhs[1]) || mxIsUint32(prhs[1]) || mxIsInt32(prhs[1]) )
		mexErrMsgTxt("EGIPT: IMG type not supported (supported: uint8, uint16, int16, single or double)!");
	if (n_out != 1)
		mexErrMsgTxt("EGIPT: returns one (and one only) output");
	/* -------------------- End of parsing input ------------------------------------- */

	ny = mxGetM(prhs[1]);	nx = getNK(prhs[1],1);	nBands = getNK(prhs[1],2);
	
	/* Allocate and initialize defaults in a new control structure */
	Ctrl = (struct CV_CTRL *) New_Cv_Ctrl ();
	getDataType(Ctrl, prhs, &nBytes, &img_depth);

	/* ------ Create pointer for temporary array ------------------------------------- */
	ptr_in  = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		  			mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
	/* ------------------------------------------------------------------------------- */ 

	Set_pt_Ctrl_in ( Ctrl, prhs[1], ptr_in, 1 ); 	/* Set pointer & interleave */
	src = cvCreateImageHeader( cvSize(nx, ny), img_depth, nBands );
	localSetData( Ctrl, src, 1, nx * nBands * nBytes );

	if (!strcmp(op,"pyrU")) {
		nxOut = 2*nx;		nyOut = 2*ny;
	} 
	else {
		nxOut = (int)(nx/2);	nyOut = (int)(ny/2);
	}

	/* ------ Create pointer for output array ---------------------------------------- */
	out_dims[0] = nyOut;	out_dims[1] = nxOut;	out_dims[2] = nBands;
	ptr_out = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		  			out_dims, mxGetClassID(prhs[1]), mxREAL);
	/* ------------------------------------------------------------------------------- */ 

	Set_pt_Ctrl_out1 ( Ctrl, ptr_out ); 	/* Set correct type pointer */
	dst = cvCreateImageHeader(cvSize(nxOut, nyOut), img_depth, nBands );
	localSetData( Ctrl, dst, 2, nxOut * nBands * nBytes );

	if (!strcmp(op,"pyrU"))
		cvPyrUp(src, dst, CV_GAUSSIAN_5x5);
	else
		cvPyrDown(src, dst, CV_GAUSSIAN_5x5);

	cvReleaseImageHeader( &src );
	mxDestroyArray(ptr_in);

	plhs[0] = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
					out_dims, mxGetClassID(prhs[1]), mxREAL);

	Set_pt_Ctrl_out2 ( Ctrl, plhs[0], 1 ); 		/* Set pointer & desinterleave */

	Free_Cv_Ctrl (Ctrl);	/* Deallocate control structure */
	cvReleaseImageHeader( &dst );
	mxDestroyArray(ptr_out);
}

/* --------------------------------------------------------------------------- */
void Jarithm(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[], const char *op) {
	int nx, nx2, ny, ny2, nBands, nBands2, nBytes, img_depth, inplace = FALSE, error = 0;
	IplImage *src1, *src2, *dst;
	CvScalar value;
	struct CV_CTRL *Ctrl;
	void *New_Cv_Ctrl (), Free_Cv_Ctrl (struct CV_CTRL *C);

	/* ---- Check for errors in user's call to function. ----------------------------- */
	if (n_in == 1) { arithmUsage();	return; }
	ny = mxGetM(prhs[1]);	nx = getNK(prhs[1],1);	nBands = getNK(prhs[1],2);
	ny2 = mxGetM(prhs[2]);	nx2 = getNK(prhs[2],1);	nBands2 = getNK(prhs[2],2);
	if (nx*ny == 1)
		mexErrMsgTxt("CVLIB_MEX: First argument cannot be a scalar!");
	if (ny != ny2 && ny2 != 1) error++;
	if (nx != nx2 && nx2 != 1) error++;
	if (error)
		mexErrMsgTxt("CVLIB_MEX: Matrix dimensions must agree!");

	if (nx2*ny2 == 1 && (strcmp(op,"addS") && strcmp(op,"subS")) )
		mexErrMsgTxt("CVLIB_MEX: only 'addS' or 'subS' are allowed when second arg is a scalar!");

	if (n_out == 0)
		inplace = TRUE;
	/* -------------------- End of parsing input ------------------------------------- */

	/* Allocate and initialize defaults in a new control structure */
	Ctrl = (struct CV_CTRL *) New_Cv_Ctrl ();
	getDataType(Ctrl, prhs, &nBytes, &img_depth);

	src1 = cvCreateImageHeader( cvSize(nx, ny), img_depth, nBands );
	cvSetImageData( src1, (void *)mxGetData(prhs[1]), nx * nBytes * nBands );
	if (nx2*ny2 != 1) { 
		src2 = cvCreateImageHeader( cvSize(nx, ny), img_depth, nBands );
		cvSetImageData( src2, (void *)mxGetData(prhs[2]), nx * nBytes * nBands );
	}
	else
		value.val[0] = *(double *)mxGetData(prhs[2]);

	if (!inplace) {
		plhs[0] = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		  			mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
		dst = cvCreateImageHeader( cvSize(nx, ny), img_depth, nBands );
		cvSetImageData( dst, (void *)mxGetData(plhs[0]), nx * nBytes * nBands );
		if (!strcmp(op,"add"))
			cvAdd( src1, src2, dst, NULL ); 
		else if (!strcmp(op,"addS"))
			cvAddS( src1, value, dst, NULL ); 
		else if (!strcmp(op,"sub"))
			cvSub( src1, src2, dst, NULL ); 
		else if (!strcmp(op,"subS"))
			cvSubS( src1, value, dst, NULL ); 
		else if (!strcmp(op,"mul"))
			cvMul( src1, src2, dst, 1 ); 
		else if (!strcmp(op,"div"))
			cvDiv( src1, src2, dst, 1 ); 

		cvReleaseImageHeader( &dst );
	}
	else {
		if (!strcmp(op,"add"))
			cvAdd( src1, src2, src1, NULL ); 
		else if (!strcmp(op,"addS"))
			cvAddS( src1, value, src1, NULL ); 
		else if (!strcmp(op,"sub"))
			cvSub( src1, src2, src1, NULL ); 
		else if (!strcmp(op,"subS"))
			cvSubS( src1, value, src1, NULL ); 
		else if (!strcmp(op,"mul"))
			cvMul( src1, src2, src1, 1 ); 
		else if (!strcmp(op,"div"))
			cvDiv( src1, src2, src1, 1 ); 
	}

	cvReleaseImageHeader( &src1 );
	if (nx2*ny2 != 1)
		cvReleaseImageHeader( &src2 );
	Free_Cv_Ctrl (Ctrl);	/* Deallocate control structure */
}

/* --------------------------------------------------------------------------- */
void JaddWeighted(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]) {
	int nx, ny, nx2, ny2, nz2, nBands, nBytes, img_depth, inplace = FALSE;
	double *ptr_d, alpha, beta, gamma = 0;
	struct CV_CTRL *Ctrl;
	void *New_Cv_Ctrl (), Free_Cv_Ctrl (struct CV_CTRL *C);
	IplImage *src1, *src2, *dst;

	/* ---- Check for errors in user's call to function. ----------------------------- */
	if (n_in == 1) { addWeightedUsage();	return; }
	ny = mxGetM(prhs[1]);	nx = getNK(prhs[1],1);	nBands = getNK(prhs[1],2);
	ny2 = mxGetM(prhs[3]);	nx2 = getNK(prhs[3],1);	nz2 = getNK(prhs[3],2);
	if (nx != nx2 || ny != ny2 || nBands != nz2)
		mexErrMsgTxt("ADDWEIGHTED ERROR: Matrix dimensions must agree!");
	if (n_in < 5)
		mexErrMsgTxt("ADDWEIGHTED ERROR: not enough input arguments!");
	ptr_d = (double *)mxGetData(prhs[2]);	alpha = ptr_d[0];
	ptr_d = (double *)mxGetData(prhs[4]);	beta  = ptr_d[0];
	if (n_in == 6) {
		ptr_d = (double *)mxGetData(prhs[5]);	gamma = ptr_d[0];
	}
	if (n_out == 0)
		inplace = TRUE;
	/* -------------------- End of parsing input ------------------------------------- */

	/* Allocate and initialize defaults in a new control structure */
	Ctrl = (struct CV_CTRL *) New_Cv_Ctrl ();
	getDataType(Ctrl, prhs, &nBytes, &img_depth);

	src1 = cvCreateImageHeader( cvSize(nx, ny), img_depth, nBands );
	cvSetImageData( src1, (void *)mxGetData(prhs[1]), nx * nBytes * nBands );
	src2 = cvCreateImageHeader( cvSize(nx, ny), img_depth, nBands );
	cvSetImageData( src2, (void *)mxGetData(prhs[3]), nx * nBytes * nBands );

	if (!inplace) {
		plhs[0] = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		  			mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
		dst = cvCreateImageHeader( cvSize(nx, ny), img_depth, nBands );
		cvSetImageData( dst, (void *)mxGetData(plhs[0]), nx * nBytes * nBands );
		cvAddWeighted( src1, alpha, src2, beta, gamma, dst );	
		cvReleaseImageHeader( &dst );
	}
	else
		cvAddWeighted( src1, alpha, src2, beta, gamma, src1 );	

	cvReleaseImageHeader( &src1 );
	cvReleaseImageHeader( &src2 );
	Free_Cv_Ctrl (Ctrl);	/* Deallocate control structure */
}

/* --------------------------------------------------------------------------- */
void Jinpaint(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]) {
	int nx, ny, nx2, ny2, nBands, nBytes, img_depth, inplace = FALSE;
	struct CV_CTRL *Ctrl;
	void *New_Cv_Ctrl (), Free_Cv_Ctrl (struct CV_CTRL *C);
	mxArray *ptr_in1, *ptr_in2;
	IplImage *src1, *src2, *dst;

	/* ---- Check for errors in user's call to function. ----------------------------- */
	if (n_in == 1) { inpaintUsage();	return; }

	/* Check that input image is of type UInt8 */
	if (!mxIsUint8(prhs[1]))
		mexErrMsgTxt("INPAINT ERROR: Invalid first input. Data type must be: UInt8.\n");
	if ( !(mxIsUint8(prhs[2]) || mxIsLogical(prhs[2])) )
		mexErrMsgTxt("INPAINT ERROR: Invalid second input. Data type must be Uint8 or Logical.\n");

	if (n_in < 3)
		mexErrMsgTxt("INPAINT ERROR: not enough input arguments!");
	ny = mxGetM(prhs[1]);	nx = getNK(prhs[1],1);	nBands = getNK(prhs[1],2);
	ny2 = mxGetM(prhs[2]);	nx2 = getNK(prhs[2],1);
	if (nx != nx2 || ny != ny2)
		mexErrMsgTxt("INPAINT ERROR: Matrix dimensions must agree!");
	if (getNK(prhs[2],2) != 1)
		mexErrMsgTxt("INPAINT ERROR: Second arg must be a mask array, that is with only two dimensions!");

	if (n_out == 0)
		inplace = TRUE;
	/* -------------------- End of parsing input ------------------------------------- */

	/* Allocate and initialize defaults in a new control structure */
	Ctrl = (struct CV_CTRL *) New_Cv_Ctrl ();
	getDataType(Ctrl, prhs, &nBytes, &img_depth);

	/* ------ Create pointer for temporary array ------------------------------------- */
	ptr_in1  = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		  			mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
	ptr_in2  = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[2]),
		  			mxGetDimensions(prhs[2]), mxGetClassID(prhs[2]), mxREAL);
	/* ------------------------------------------------------------------------------- */ 

	Set_pt_Ctrl_in( Ctrl, prhs[1], ptr_in1, 1 ); 	/* Set pointer & interleave */
	src1 = cvCreateImageHeader( cvSize(nx, ny), img_depth, nBands );
	localSetData( Ctrl, src1, 1, nx * nBands * nBytes );

	Set_pt_Ctrl_in( Ctrl, prhs[2], ptr_in2, 1 ); 	/* Set pointer & interleave */
	src2 = cvCreateImageHeader( cvSize(nx, ny), img_depth, 1 );
	localSetData( Ctrl, src2, 1, nx * nBytes );

	if (!inplace) {
		Set_pt_Ctrl_out1 ( Ctrl, ptr_in1 ); 		/* Reuse memory */
		dst = cvCreateImage(cvSize(nx, ny), img_depth , nBands );
		localSetData( Ctrl, dst, 2, nx * nBands * nBytes );
		cvInpaint( src1, src2, dst, 3, CV_INPAINT_TELEA); 
		plhs[0] = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		  			mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
		Set_pt_Ctrl_out2 ( Ctrl, plhs[0], 1 ); 		/* Set pointer & desinterleave */
	}
	else {
		cvInpaint( src1, src2, src1, 3, CV_INPAINT_TELEA); 
	}

	cvReleaseImageHeader( &src1 );
	cvReleaseImageHeader( &src2 );
	mxDestroyArray(ptr_in1);
	mxDestroyArray(ptr_in2);
	Free_Cv_Ctrl (Ctrl);	/* Deallocate control structure */
}

/* --------------------------------------------------------------------------- */
void Jflip(int n_out, mxArray *plhs[], int n_in, const mxArray *prhs[]) {
	int nx, ny, nBands, nBytes, img_depth, inplace = FALSE, flip_mode = 1;
	char	*argv;
	IplImage *src, *dst;
	struct CV_CTRL *Ctrl;
	void *New_Cv_Ctrl (), Free_Cv_Ctrl (struct CV_CTRL *C);

	/* ---- Check for errors in user's call to function. ----------------------------- */
	if (n_in == 1) { flipUsage();	return; }
	if(!mxIsChar(prhs[2]))
		mexErrMsgTxt("FLIP Second argument must be a valid string!");
	else {
		argv = (char *)mxArrayToString(prhs[2]);
		if (!strcmp(argv,"ud") || !strcmp(argv,"UD"))
			flip_mode = 1;
		else if (!strcmp(argv,"lr") || !strcmp(argv,"LR")) 
			flip_mode = 0;
		else if (!strcmp(argv,"both")) 
			flip_mode = -1;
		else
			mexErrMsgTxt("FLIP ERROR: unknown flipping type string.\n");
	}
	if (n_out == 0)
		inplace = TRUE;
	/* -------------------- End of parsing input ------------------------------------- */

	ny = mxGetM(prhs[1]);	nx = getNK(prhs[1],1);	nBands = getNK(prhs[1],2);

	/* Allocate and initialize defaults in a new control structure */
	Ctrl = (struct CV_CTRL *) New_Cv_Ctrl ();
	getDataType(Ctrl, prhs, &nBytes, &img_depth);

	src = cvCreateImageHeader( cvSize(nx, ny), img_depth, nBands );
	cvSetImageData( src, (void *)mxGetData(prhs[1]), nx * nBytes * nBands );

	if (!inplace) {
		plhs[0] = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[1]),
		  			mxGetDimensions(prhs[1]), mxGetClassID(prhs[1]), mxREAL);
		dst = cvCreateImageHeader( cvSize(nx, ny), img_depth, nBands );
		cvSetImageData( dst, (void *)mxGetData(plhs[0]), nx * nBytes * nBands );
		cvFlip( src, dst, flip_mode ); 
		cvReleaseImageHeader( &src );
	}
	else {
		cvFlip( src, NULL, flip_mode ); 
	}
	Free_Cv_Ctrl (Ctrl);	/* Deallocate control structure */
}
	
/* -------------------------------------------------------------------------------------------- */
void localSetData(struct CV_CTRL *Ctrl, IplImage* img, int dir, int step) {
	if (Ctrl->UInt8.active == TRUE)
		if (dir == 1)
			cvSetImageData( img, (void *)Ctrl->UInt8.tmp_img_in, step );
		else
			cvSetImageData( img, (void *)Ctrl->UInt8.tmp_img_out, step );
	else if (Ctrl->Int8.active == TRUE)
		if (dir == 1)
			cvSetImageData( img, (void *)Ctrl->Int8.tmp_img_in, step );
		else
			cvSetImageData( img, (void *)Ctrl->Int8.tmp_img_out, step );
	else if (Ctrl->UInt16.active == TRUE)
		if (dir == 1)
			cvSetImageData( img, (void *)Ctrl->UInt16.tmp_img_in, step );
		else
			cvSetImageData( img, (void *)Ctrl->UInt16.tmp_img_out, step );
	else if (Ctrl->Int16.active == TRUE)
		if (dir == 1)
			cvSetImageData( img, (void *)Ctrl->Int16.tmp_img_in, step );
		else
			cvSetImageData( img, (void *)Ctrl->Int16.tmp_img_out, step );
	else if (Ctrl->Int32.active == TRUE)
		if (dir == 1)
			cvSetImageData( img, (void *)Ctrl->Int32.tmp_img_in, step );
		else
			cvSetImageData( img, (void *)Ctrl->Int32.tmp_img_out, step );
	else if (Ctrl->Float.active == TRUE)
		if (dir == 1)
			cvSetImageData( img, (void *)Ctrl->Float.tmp_img_in, step );
		else
			cvSetImageData( img, (void *)Ctrl->Float.tmp_img_out, step );
	else if (Ctrl->Double.active == TRUE)
		if (dir == 1)
			cvSetImageData( img, (void *)Ctrl->Double.tmp_img_in, step );
		else
			cvSetImageData( img, (void *)Ctrl->Double.tmp_img_out, step );
}

/* -------------------------------------------------------------------------------------------- */
void interleave(struct CV_CTRL *Ctrl, int nx, int ny, int nBands, int dir) {
	if (Ctrl->UInt8.active == TRUE)
		interleaveUI8(Ctrl, nx, ny, nBands, dir);
	else if (Ctrl->Int8.active == TRUE)
		interleaveI8(Ctrl, nx, ny, nBands, dir);
	else if (Ctrl->UInt16.active == TRUE)
		interleaveUI16(Ctrl, nx, ny, nBands, dir);
	else if (Ctrl->Int16.active == TRUE)
		interleaveI16(Ctrl, nx, ny, nBands, dir);
	else if (Ctrl->Int32.active == TRUE)
		interleaveI32(Ctrl, nx, ny, nBands, dir);
	else if (Ctrl->Float.active == TRUE)
		interleaveF32(Ctrl, nx, ny, nBands, dir);
	else if (Ctrl->Double.active == TRUE)
		interleaveF64(Ctrl, nx, ny, nBands, dir);
}

/* -------------------------------------------------------------------------------------------- */
void interleaveDouble(double in[], double out[], int nx, int ny) {
	/* Version to be used with 2D double vars that are not in the control struct */
	int m, n, c = 0;

	for (m = 0; m < ny; m++) 
		for (n = 0; n < nx; n++)
			out[c++] = in[m + n*ny];
}

/* -------------------------------------------------------------------------------------------- */
void interleaveBlind(unsigned char in[], unsigned char out[], int nx, int ny, int nBands, int dir) {
	/* This a version to be used with vars that are not in the control struct */
	int n_xy, m, n, k, i, c = 0;

	if (dir == 1) {		/* Matlab order to b0,g0,r0, g1,g1,r1, etc ... */
		n_xy = nx*ny;
		for (m = 0; m < ny; m++) 
			for (n = 0; n < nx; n++)
				for (k = 0, i = nBands-1; k < nBands; k++, i--)
					out[c++] = in[m + n*ny + i*n_xy];
	}
	else {			/* b0,g0,r0, b1,g1,r1, etc ...  to Matlab order */
		for (k = 0, i = nBands-1; k < nBands; k++, i--)
			for (n = 0; n < nx; n++)
				for (m = 0; m < ny; m++) 
					out[c++] = in[nBands * (n + m*nx) + i];
	}
}

/* -------------------------------------------------------------------------------------------- */
void interleaveUI8(struct CV_CTRL *Ctrl, int nx, int ny, int nBands, int dir) {
	int n_xy, m, n, k, i, c = 0;

	if (dir == 1) {		/* Matlab order to b0,g0,r0, g1,g1,r1, etc ... */
		n_xy = nx*ny;
		for (m = 0; m < ny; m++) 
			for (n = 0; n < nx; n++)
				for (k = 0, i = nBands-1; k < nBands; k++, i--)
					Ctrl->UInt8.tmp_img_in[c++] = Ctrl->UInt8.img_in[m + n*ny + i*n_xy];
	}
	else {			/* b0,g0,r0, b1,g1,r1, etc ...  to Matlab order */
		for (k = 0, i = nBands-1; k < nBands; k++, i--)
			for (n = 0; n < nx; n++)
				for (m = 0; m < ny; m++) 
					Ctrl->UInt8.img_out[c++] = Ctrl->UInt8.tmp_img_out[nBands * (n + m*nx) + i];
	}
}

/* -------------------------------------------------------------------------------------------- */
void interleaveI8(struct CV_CTRL *Ctrl, int nx, int ny, int nBands, int dir) {
	int n_xy, m, n, k, i, c = 0;

	if (dir == 1) {		/* Matlab order to b0,g0,r0, g1,g1,r1, etc ... */
		n_xy = nx*ny;
		for (m = 0; m < ny; m++) 
			for (n = 0; n < nx; n++)
				for (k = 0, i = nBands-1; k < nBands; k++, i--)
					Ctrl->Int8.tmp_img_in[c++] = Ctrl->Int8.img_in[m + n*ny + i*n_xy];
	}
	else {			/* b0,g0,r0, b1,g1,r1, etc ...  to Matlab order */
		for (k = 0, i = nBands-1; k < nBands; k++, i--)
			for (n = 0; n < nx; n++)
				for (m = 0; m < ny; m++) 
					Ctrl->Int8.img_out[c++] = Ctrl->Int8.tmp_img_out[nBands * (n + m*nx) + i];
	}
}

/* -------------------------------------------------------------------------------------------- */
void interleaveUI16(struct CV_CTRL *Ctrl, int nx, int ny, int nBands, int dir) {
	int n_xy, m, n, k, i, c = 0;

	if (dir == 1) {		/* Matlab order to b0,g0,r0, g1,g1,r1, etc ... */
		n_xy = nx*ny;
		for (m = 0; m < ny; m++) 
			for (n = 0; n < nx; n++)
				for (k = 0, i = nBands-1; k < nBands; k++, i--)
					Ctrl->UInt16.tmp_img_in[c++] = Ctrl->UInt16.img_in[(m + n*ny + i*n_xy)];
	}
	else {			/* b0,g0,r0, b1,g1,r1, etc ...  to Matlab order */
		for (k = 0, i = nBands-1; k < nBands; k++, i--)
			for (n = 0; n < nx; n++)
				for (m = 0; m < ny; m++) 
					Ctrl->UInt16.img_out[c++] = Ctrl->UInt16.tmp_img_out[nBands * (n + m*nx) + i];
	}
}

/* -------------------------------------------------------------------------------------------- */
void interleaveI16(struct CV_CTRL *Ctrl, int nx, int ny, int nBands, int dir) {
	int n_xy, m, n, k, i, c = 0;

	if (dir == 1) {		/* Matlab order to b0,g0,r0, g1,g1,r1, etc ... */
		n_xy = nx*ny;
		for (m = 0; m < ny; m++) 
			for (n = 0; n < nx; n++)
				for (k = 0, i = nBands-1; k < nBands; k++, i--)
					Ctrl->Int16.tmp_img_in[c++] = Ctrl->Int16.img_in[(m + n*ny + i*n_xy)];
	}
	else {			/* b0,g0,r0, b1,g1,r1, etc ...  to Matlab order */
		for (k = 0, i = nBands-1; k < nBands; k++, i--)
			for (n = 0; n < nx; n++)
				for (m = 0; m < ny; m++) 
					Ctrl->Int16.img_out[c++] = Ctrl->Int16.tmp_img_out[nBands * (n + m*nx) + i];
	}
}

/* -------------------------------------------------------------------------------------------- */
void interleaveI32(struct CV_CTRL *Ctrl, int nx, int ny, int nBands, int dir) {
	int n_xy, m, n, k, i, c = 0;

	if (dir == 1) {		/* Matlab order to b0,g0,r0, g1,g1,r1, etc ... */
		n_xy = nx*ny;
		for (m = 0; m < ny; m++) 
			for (n = 0; n < nx; n++)
				for (k = 0, i = nBands-1; k < nBands; k++, i--)
					Ctrl->Int32.tmp_img_in[c++] = Ctrl->Int32.img_in[(m + n*ny + i*n_xy)];
	}
	else {			/* b0,g0,r0, b1,g1,r1, etc ...  to Matlab order */
		for (k = 0, i = nBands-1; k < nBands; k++, i--)
			for (n = 0; n < nx; n++)
				for (m = 0; m < ny; m++) 
					Ctrl->Int32.img_out[c++] = Ctrl->Int32.tmp_img_out[nBands * (n + m*nx) + i];
	}
}

/* -------------------------------------------------------------------------------------------- */
void interleaveF32(struct CV_CTRL *Ctrl, int nx, int ny, int nBands, int dir) {
	int n_xy, m, n, k, i, c = 0;

	if (dir == 1) {		/* Matlab order to b0,g0,r0, g1,g1,r1, etc ... */
		n_xy = nx*ny;
		for (m = 0; m < ny; m++) 
			for (n = 0; n < nx; n++)
				for (k = 0, i = nBands-1; k < nBands; k++, i--)
					Ctrl->Float.tmp_img_in[c++] = Ctrl->Float.img_in[(m + n*ny + i*n_xy)];
	}
	else {			/* b0,g0,r0, b1,g1,r1, etc ...  to Matlab order */
		for (k = 0, i = nBands-1; k < nBands; k++, i--)
			for (n = 0; n < nx; n++)
				for (m = 0; m < ny; m++) 
					Ctrl->Float.img_out[c++] = Ctrl->Float.tmp_img_out[nBands * (n + m*nx) + i];
	}
}

/* -------------------------------------------------------------------------------------------- */
void interleaveF64(struct CV_CTRL *Ctrl, int nx, int ny, int nBands, int dir) {
	int n_xy, m, n, k, i, c = 0;

	if (dir == 1) {		/* Matlab order to b0,g0,r0, g1,g1,r1, etc ... */
		n_xy = nx*ny;
		for (m = 0; m < ny; m++) 
			for (n = 0; n < nx; n++)
				for (k = 0, i = nBands-1; k < nBands; k++, i--)
					Ctrl->Double.tmp_img_in[c++] = Ctrl->Double.img_in[(m + n*ny + i*n_xy)];
	}
	else {			/* b0,g0,r0, b1,g1,r1, etc ...  to Matlab order */
		for (k = 0, i = nBands-1; k < nBands; k++, i--)
			for (n = 0; n < nx; n++)
				for (m = 0; m < ny; m++) 
					Ctrl->Double.img_out[c++] = Ctrl->Double.tmp_img_out[nBands * (n + m*nx) + i];
	}
}

/* ---------------------------------------------------------------------- */
void *New_Cv_Ctrl () {	/* Allocate and initialize a new control structure */
	struct CV_CTRL *C;
	
	C = (struct CV_CTRL *) mxCalloc (1, sizeof (struct CV_CTRL));
	return ((void *)C);
}

/* ---------------------------------------------------------------------- */
void Free_Cv_Ctrl (struct CV_CTRL *C) {	/* Deallocate control structure */
	mxFree ((void *)C);	
}

/* ---------------------------------------------------------------------- */
void Set_pt_Ctrl_in (struct CV_CTRL *Ctrl, const mxArray *pi, mxArray *pit, int interl) {
	/* Set input image pointers to correct type and, optionaly, do the interleaving */ 
	if (Ctrl->UInt8.active == TRUE) {
		Ctrl->UInt8.img_in      = (unsigned char *)mxGetData(pi);
		Ctrl->UInt8.tmp_img_in  = (unsigned char *)mxGetData(pit);
	}
	else if (Ctrl->Int8.active == TRUE) {
		Ctrl->Int8.img_in      = (char *)mxGetData(pi);
		Ctrl->Int8.tmp_img_in  = (char *)mxGetData(pit);
	}
	else if (Ctrl->UInt16.active == TRUE) {
		Ctrl->UInt16.img_in      = (unsigned short int *)mxGetData(pi);
		Ctrl->UInt16.tmp_img_in  = (unsigned short int *)mxGetData(pit);
	}
	else if (Ctrl->Int16.active == TRUE) {
		Ctrl->Int16.img_in      = (short int *)mxGetData(pi);
		Ctrl->Int16.tmp_img_in  = (short int *)mxGetData(pit);
	}
	else if (Ctrl->Int32.active == TRUE) {
		Ctrl->Int32.img_in      = (int *)mxGetData(pi);
		Ctrl->Int32.tmp_img_in  = (int *)mxGetData(pit);
	}
	else if (Ctrl->Float.active == TRUE) {
		Ctrl->Float.img_in      = (float *)mxGetData(pi);
		Ctrl->Float.tmp_img_in  = (float *)mxGetData(pit);
	}
	else if (Ctrl->Double.active == TRUE) {
		Ctrl->Double.img_in      = (double *)mxGetData(pi);
		Ctrl->Double.tmp_img_in  = (double *)mxGetData(pit);
	}

	if (interl)
		interleave (Ctrl, getNK(pi,1), mxGetM(pi), getNK(pi,2), 1);
}

/* ---------------------------------------------------------------------- */
void Set_pt_Ctrl_out1 ( struct CV_CTRL *Ctrl, mxArray *pi ) {
	/* Set output tmp image pointer to correct type */ 
	if (Ctrl->UInt8.active == TRUE) {
		Ctrl->UInt8.tmp_img_out  = (unsigned char *)mxGetData(pi);
	}
	else if (Ctrl->Int8.active == TRUE) {
		Ctrl->Int8.tmp_img_out   = (char *)mxGetData(pi);
	}
	else if (Ctrl->UInt16.active == TRUE) {
		Ctrl->UInt16.tmp_img_out = (unsigned short int *)mxGetData(pi);
	}
	else if (Ctrl->Int16.active == TRUE) {
		Ctrl->Int16.tmp_img_out  = (short int *)mxGetData(pi);
	}
	else if (Ctrl->Int32.active == TRUE) {
		Ctrl->Int32.tmp_img_out  = (int *)mxGetData(pi);
	}
	else if (Ctrl->Float.active == TRUE) {
		Ctrl->Float.tmp_img_out  = (float *)mxGetData(pi);
	}
	else if (Ctrl->Double.active == TRUE) {
		Ctrl->Double.tmp_img_out = (double *)mxGetData(pi);
	}
}

/* ---------------------------------------------------------------------- */
void Set_pt_Ctrl_out2 (struct CV_CTRL *Ctrl, mxArray *po, int interl) {
	/* Set output image pointers to correct type and, optionaly, do the interleaving */ 
	if (Ctrl->UInt8.active == TRUE) {
		Ctrl->UInt8.img_out  = (unsigned char *)mxGetData(po);
	}
	else if (Ctrl->Int8.active == TRUE) {
		Ctrl->Int8.img_out   = (char *)mxGetData(po);
	}
	else if (Ctrl->UInt16.active == TRUE) {
		Ctrl->UInt16.img_out = (unsigned short int *)mxGetData(po);
	}
	else if (Ctrl->Int16.active == TRUE) {
		Ctrl->Int16.img_out  = (short int *)mxGetData(po);
	}
	else if (Ctrl->Int32.active == TRUE) {
		Ctrl->Int32.img_out  = (int *)mxGetData(po);
	}
	else if (Ctrl->Float.active == TRUE) {
		Ctrl->Float.img_out  = (float *)mxGetData(po);
	}
	else if (Ctrl->Double.active == TRUE) {
		Ctrl->Double.img_out = (double *)mxGetData(po);
	}

	if (interl)
		interleave (Ctrl, getNK(po,1), mxGetM(po), getNK(po,2), -1);
}

/* ---------------------------------------------------------------------- */
int getNK(const mxArray *p, int which) {
	/* Get number of columns or number of bands of a mxArray */
	int nx, nBands, nDims;
	const int *dim_array;

	nDims     = mxGetNumberOfDimensions(p);
	dim_array = mxGetDimensions(p);
	nx = dim_array[1];
	nBands = dim_array[2];
	if (nDims == 2) 	/* Otherwise it would stay undefined */
		nBands = 1;

	if (which == 1)
		return(nx);
	else if (which == 2)
		return(nBands);
	else
		mexErrMsgTxt("getNK: Bad dimension number!");
}

/* ---------------------------------------------------------------------- */
void getDataType(struct CV_CTRL *Ctrl, const mxArray *prhs[], int *nBytes, int *img_depth) {
	/* Find out in which data type was given the input array */
	int error = 0;

	if (mxIsComplex(prhs[1]) || mxIsSparse(prhs[1]))
		mexErrMsgTxt("Input image must be a real matrix");

	if (mxIsLogical(prhs[1])) {		/* Logicals take precedence over UInt8 */
		*img_depth = IPL_DEPTH_8U;
		*nBytes    = 1;
		Ctrl->UInt8.active = TRUE;
	}
	else if (mxIsUint8(prhs[1])) {
		*img_depth = IPL_DEPTH_8U;
		*nBytes    = 1;
		Ctrl->UInt8.active = TRUE;
	}
	else if (mxIsInt8(prhs[1])) {
		*img_depth = IPL_DEPTH_8S;
		*nBytes    = 1;
		Ctrl->Int8.active = TRUE;
		error++;
	}
	else if (mxIsUint16(prhs[1])) {
		*img_depth = IPL_DEPTH_16U;
		*nBytes    = 2;
		Ctrl->UInt16.active = TRUE;
	}
	else if (mxIsInt16(prhs[1])) {
		*img_depth = IPL_DEPTH_16S;
		*nBytes    = 2;
		Ctrl->Int16.active = TRUE;
		error++;
	}
	else if (mxIsInt32(prhs[1])) {
		*img_depth = IPL_DEPTH_32S;
		*nBytes    = 4;
		Ctrl->Int32.active = TRUE;
		error++;
	}
	else if (mxIsSingle(prhs[1])) {
		*img_depth = IPL_DEPTH_32F;
		*nBytes    = 4;
		Ctrl->Float.active = TRUE;
	}
	else if (mxIsDouble(prhs[1])) {
		*img_depth = IPL_DEPTH_64F;
		*nBytes    = 8;
		Ctrl->Double.active = TRUE;
		error++;
	}
	else {
		mexPrintf("CVLIB_MEX ERROR: Invalid input data type.\n");
		mexErrMsgTxt("Valid types are: double, single, Int32, UInt16, Int16, and Uint8.\n");
	}
	//mexPrintf("Merda %d %d %d %d\n",(int)IPL_DEPTH_8S, IPL_DEPTH_16S, IPL_DEPTH_32S, IPL_DEPTH_SIGN);

	/* For a very obscure reason the types Int8,Int16,Int32,Double are reported illegal
	   in cxarray (line 3346), but the most strange is that they are foreseen there */
	/*if (error) {
		mexPrintf("CVRESIZE_MEX ERROR: Invalid input data type.\n");
		mexErrMsgTxt("Valid types are: double, single, UInt16, Int16, and Uint8.\n");
	}*/
}

/* ---------------------------------------------------------------------- */
void cvResizeUsage() {
	mexPrintf("Usage: B = cvlib_mex('resize',A,M,METHOD);\n");
	mexPrintf("       returns an image that is M times the size of A.\n");
	mexPrintf("       If M is between 0 and 1.0, B is smaller than A. If\n");
	mexPrintf("       M is greater than 1.0, B is larger than A. If METHOD is\n");
	mexPrintf("       omitted, the bilinear interpolation is used.\n\n");
 
	mexPrintf("B = cvlib_mex('resize',A,[MROWS MCOLS],METHOD) returns an image of size\n");
	mexPrintf("       MROWS-by-MCOLS. The available METHODS are:\n");
	mexPrintf("       'bilinear' (default) bilinear interpolation\n");
	mexPrintf("       'bicubic'  bicubic interpolation\n");
	mexPrintf("       'nearest'  nearest neighbor interpolation\n");
	mexPrintf("       'area'     resampling using pixel area relation. It is preferred method\n");
	mexPrintf("             for image decimation that gives moire-free results.\n\n");

	mexPrintf("       Class support: The finest example of OpenCV data type support mess:\n");
	mexPrintf("             If interpolation METHOD is 'nearest' all data types are supported:\n");
	mexPrintf("             Otherwise: logical, uint8, uint16 and single!\n");
	mexPrintf("       Memory overhead: 1 copy of IMG and 1 copy of B.\n");
}

/* -------------------------------------------------------------------------------------------- */
void floodFillUsage() {
	mexPrintf("Usage: B = cvlib_mex('floodfill',IMG,PARAMS_STRUCT);\n");
	mexPrintf("       where IMG is a uint8 MxNx3 rgb OR a MxN intensity image:\n");
	mexPrintf("       PARAMS_STRUCT is a structure with the following fields (only Point is mandatory):\n");
	mexPrintf("       Point -> a 1x2 vector with the X,Y coordinates (in the pixel\n");
	mexPrintf("                reference frame) of the selected image point\n");
	mexPrintf("       Tolerance -> a scalar in the [0 255] interval [Default is 20]\n");
	mexPrintf("       Connect   -> a scalar with the connectivity, either 4 or 8 [Default is 4]\n");
	mexPrintf("       FillColor -> a 1x3 vector with the fill color [default picks a random color]\n\n");
	mexPrintf("       Alternatively you may also call cvfill_mex in this way:\n");
	mexPrintf("       B = cvfill_mex('floodfill',IMG,POINT);\n");
	mexPrintf("       where POINT is a 1x2 vector with the X,Y coordinates as described above\n\n");

	mexPrintf("       Class support: logical or uint8.\n");
	mexPrintf("       Memory overhead: 1 copy of IMG.\n");
}

/* -------------------------------------------------------------------------------------------- */
void lineUsage() {
	mexPrintf("Usage: cvlib_mex('line',IMG,PT1,PT2,[COLOR,THICK,LINE_TYPE]);\n");
	mexPrintf("       where IMG is a uint8 MxNx3 rgb OR a MxN intensity image:\n");
	mexPrintf("       draws, inplace, the line segment between PT1 and PT2 points in the image.\n");
	mexPrintf("       IM2 = cvlib_mex('line',IMG,PT1,PT2,[COLOR,THICK,LINE_TYPE]);\n");
	mexPrintf("       Returns the drawing in the the new array IM2.\n\n");
	mexPrintf("       Terms inside brakets are optional and can be empty,\n");
	mexPrintf("       e.g (...,[],[],LINE_TYPE) or (...,[],5) are allowed.\n");
	mexPrintf("	  PT1 & PT2 -> Start and end points of the line segment. Note PT is a 1x2 vector e.g [x y]\n");
	mexPrintf("       COLOR -> Line color. Can be a 1x3 vector, e.g. the default [255 255 255], or a scalar (gray).\n");
	mexPrintf("       THICK -> Line thickness (default 1)\n");
	mexPrintf("       LINE_TYPE -> Type of line. 8 - 8-connected line (default), 4 - 4-connected, 16 - antialiased.\n\n");

	mexPrintf("       Class support: uint8.\n");
	mexPrintf("       Memory overhead: 1 copy of IMG.\n");
}

/* -------------------------------------------------------------------------------------------- */
void rectUsage() {
	mexPrintf("Usage: cvlib_mex('rectangle',IMG,PT1,PT2,[COLOR,THICK,LINE_TYPE]);\n");
	mexPrintf("       where IMG is a uint8 MxNx3 rgb OR a MxN intensity image:\n");
	mexPrintf("       draws, inplace, a rectangle between the oposit corners PT1 and PT2 in the image.\n");
	mexPrintf("       IM2 = cvlib_mex('rectangle',IMG,PT1,PT2,[COLOR,THICK,LINE_TYPE]);\n");
	mexPrintf("       Returns the drawing in the the new array IM2.\n\n");
	mexPrintf("       Terms inside brakets are optional and can be empty,\n");
	mexPrintf("       e.g (...,[],[],LINE_TYPE) or (...,[],5) are allowed.\n");
	mexPrintf("	  PT1 & PT2 -> Oposit rectangle vertices. Note PT is a 1x2 vector e.g [x y]\n");
	mexPrintf("       COLOR -> Line color. Can be a 1x3 vector, e.g. the default [255 255 255], or a scalar (gray).\n");
	mexPrintf("       THICK -> Line thickness (default 1). If negative a filled rectangle is drawn.\n");
	mexPrintf("       LINE_TYPE -> Type of line. 8 - 8-connected line (default), 4 - 4-connected, 16 - antialiased.\n\n");

	mexPrintf("       Class support: uint8.\n");
	mexPrintf("       Memory overhead: 1 copy of IMG.\n");
}
/* -------------------------------------------------------------------------------------------- */
void circUsage() {
	mexPrintf("Usage: cvlib_mex('circle',IMG,CENTER,RADIUS,[COLOR,THICK,LINE_TYPE]);\n");
	mexPrintf("       where IMG is a uint8 MxNx3 rgb OR a MxN intensity image:\n");
	mexPrintf("       draws, inplace, a simple or filled circle with given center and radius.\n");
	mexPrintf("       IM2 = cvlib_mex('circle',IMG,CENTER,RADIUS,[COLOR,THICK,LINE_TYPE]);\n");
	mexPrintf("       Returns the drawing in the the new array IM2.\n\n");
	mexPrintf("       Terms inside brakets are optional and can be empty,\n");
	mexPrintf("       e.g (...,[],[],LINE_TYPE) or (...,[],5) are allowed.\n");
	mexPrintf("	  CENTER & RADIUS -> Note, they are both 1x2 vectors e.g [x y]\n");
	mexPrintf("       COLOR -> Line color. Can be a 1x3 vector, e.g. the default [255 255 255], or a scalar (gray).\n");
	mexPrintf("       THICK -> Line thickness (default 1). If negative a filled circle is drawn.\n");
	mexPrintf("       LINE_TYPE -> Type of line. 8 - 8-connected line (default), 4 - 4-connected, 16 - antialiased.\n\n");

	mexPrintf("       Class support: uint8.\n");
	mexPrintf("       Memory overhead: 1 copy of IMG.\n");
}

/* -------------------------------------------------------------------------------------------- */
void eBoxUsage() {
	mexPrintf("Usage: cvlib_mex('eBox',IMG,BOX,[COLOR,THICK,LINE_TYPE]);\n");
	mexPrintf("       where IMG is a uint8 MxNx3 rgb OR a MxN intensity image:\n");
	mexPrintf("       draws, inplace, draws a simple or thick ellipse outline, or fills an ellipse.\n");
	mexPrintf("	  BOX -> Structure with the following fields;\n");
	mexPrintf("	         'center' -> a 2 element vector with the ellipse center coords (decimal pixeis);\n");
	mexPrintf("	         'size'   -> a 2 element vector with the ellipse WIDTH and HEIGHT (decimal pixeis);\n");
	mexPrintf("	         'angle'  -> the angle between the horizontal axis and the WIDTH (degrees);\n");
	mexPrintf("	                     If this field is not present, angle defaults to zero.\n");
	mexPrintf("       IM2 = cvlib_mex('eBox',IMG,BOX,[COLOR,THICK,LINE_TYPE]);\n");
	mexPrintf("       Returns the drawing in the the new array IM2.\n\n");
	mexPrintf("       Terms inside brakets are optional and can be empty,\n");
	mexPrintf("       e.g (...,[],[],LINE_TYPE) or (...,[],5) are allowed.\n");
	mexPrintf("       COLOR -> Line color. Can be a 1x3 vector, e.g. the default [255 255 255], or a scalar (gray).\n");
	mexPrintf("       THICK -> Line thickness (default 1). If negative a filled ellipse is drawn.\n");
	mexPrintf("       LINE_TYPE -> Type of line. 8 - 8-connected line (default), 4 - 4-connected, 16 - antialiased.\n\n");

	mexPrintf("       Class support: uint8.\n");
	mexPrintf("       Memory overhead: 1 copy of IMG.\n");
}

/* -------------------------------------------------------------------------------------------- */
void plineUsage() {
	mexPrintf("Usage: cvlib_mex('polyline',IMG,PT,[COLOR,THICK,LINE_TYPE]);\n");
	mexPrintf("       where IMG is a uint8 MxNx3 rgb OR a MxN intensity image:\n");
	mexPrintf("       draws, inplace, the polyline whose vertex are contained in the Mx2 or 2xN PT array.\n");
	mexPrintf("       If PT is a cell vector with N elements, draws N polylines in the image.\n");
	mexPrintf("       Each cell element must contain a Mx2 OR 2xN array with the x,y pixel coords\n");
	mexPrintf("       of the polyline to be ploted.\n");
	mexPrintf("       IM2 = cvlib_mex('polyline',IMG,PT,[COLOR,THICK,LINE_TYPE]);\n");
	mexPrintf("       Returns the drawing in the the new array IM2.\n\n");
	mexPrintf("       Terms inside brakets are optional and can be empty,\n");
	mexPrintf("       e.g (...,[],[],LINE_TYPE) or (...,[],5) are allowed.\n");
	mexPrintf("       COLOR -> Line color. Can be a 1x3 vector, e.g. the default [255 255 255], or a scalar (gray).\n");
	mexPrintf("       THICK -> Line thickness (default 1)\n");
	mexPrintf("       LINE_TYPE -> Type of line. 8 - 8-connected line (default), 4 - 4-connected, 16 - antialiased.\n\n");

	mexPrintf("       Class support: uint8.\n");
	mexPrintf("       Memory overhead: 1 copy of IMG.\n");
}

/* -------------------------------------------------------------------------------------------- */
void fillPlineUsage() {
	mexPrintf("Usage: cvlib_mex('fillpoly',IMG,PT,[COLOR,THICK,LINE_TYPE]);\n");
	mexPrintf("       where IMG is a uint8 MxNx3 rgb OR a MxN intensity image:\n");
	mexPrintf("       fills an area bounded by several polygonal contours inplace.\n");
	mexPrintf("       The polyligonal contour vertex are contained in the Mx2 or 2xN PT array.\n");
	mexPrintf("       If PT is a cell vector with N elements, fills N polygons in the image.\n");
	mexPrintf("       Each cell element must contain a Mx2 OR 2xN array with the x,y pixel coords\n");
	mexPrintf("       of the polygon to be filled.\n");
	mexPrintf("       IM2 = cvlib_mex('polyline',IMG,PT,[COLOR,THICK,LINE_TYPE]);\n");
	mexPrintf("       Returns the drawing in the the new array IM2.\n\n");
	mexPrintf("       Terms inside brakets are optional and can be empty,\n");
	mexPrintf("       e.g (...,[],[],LINE_TYPE) or (...,[],5) are allowed.\n");
	mexPrintf("       COLOR -> Line color. Can be a 1x3 vector, e.g. the default [255 255 255], or a scalar (gray).\n");
	mexPrintf("       THICK -> Line thickness (default 1)\n");
	mexPrintf("       LINE_TYPE -> Type of line. 8 - 8-connected line (default), 4 - 4-connected, 16 - antialiased.\n\n");

	mexPrintf("       Class support: uint8.\n");
	mexPrintf("       Memory overhead: 1 copy of IMG.\n");
}

/* -------------------------------------------------------------------------------------------- */
void fillConvUsage() {
	mexPrintf("Usage: cvlib_mex('fillconvex',IMG,PT,[COLOR,THICK,LINE_TYPE]);\n");
	mexPrintf("       where IMG is a uint8 MxNx3 rgb OR a MxN intensity image:\n");
	mexPrintf("       fills an area bounded by a convex polygonal interior inplace.\n");
	mexPrintf("       The polyligonal contour vertex are contained in the Mx2 or 2xN PT array.\n");
	mexPrintf("       IM2 = cvlib_mex('polyline',IMG,PT,[COLOR,THICK,LINE_TYPE]);\n");
	mexPrintf("       Returns the drawing in the the new array IM2.\n\n");
	mexPrintf("       Terms inside brakets are optional and can be empty,\n");
	mexPrintf("       e.g (...,[],[],LINE_TYPE) or (...,[],5) are allowed.\n");
	mexPrintf("       COLOR -> Line color. Can be a 1x3 vector, e.g. the default [255 255 255], or a scalar (gray).\n");
	mexPrintf("       THICK -> Line thickness (default 1)\n");
	mexPrintf("       LINE_TYPE -> Type of line. 8 - 8-connected line (default), 4 - 4-connected, 16 - antialiased.\n\n");

	mexPrintf("       Class support: uint8.\n");
	mexPrintf("       Memory overhead: 1 copy of IMG.\n");
}

/* -------------------------------------------------------------------------------------------- */
void goodFeaturesUsage() {
	mexPrintf("Usage: B = cvlib_mex('goodfeatures',IMG,[,M,QUALITY,DIST]);\n");
	mexPrintf("       where IMG is a uint8 MxNx3 or a MxN intensity image:\n");
	mexPrintf("       returns strong corners on image.\n");
	mexPrintf("       Terms inside brakets are optional and can be empty,\n");
	mexPrintf("       e.g (...,[],[],DIST) is allowed.\n");
	mexPrintf("       M  - number of maximum output corners [default is all up to 10000]\n");
	mexPrintf("       QUALITY - only those corners are selected, which minimal eigen value is\n");
	mexPrintf("           non-less than maximum of minimal eigen values on the image,\n");
	mexPrintf("           multiplied by quality_level. For example, quality_level = 0.1\n");
	mexPrintf("           means that selected corners must be at least 1/10 as good as\n");
	mexPrintf("           the best corner. [Default is 0.1]\n");
	mexPrintf("       DIST - The selected corners(after thresholding using quality_level)\n");
	mexPrintf("           are rerified such that pair-wise distance between them is\n");
	mexPrintf("           non-less than min_distance [Default is 10]\n\n");

	mexPrintf("       Class support: uint8.\n");
	mexPrintf("       Memory overhead: 2 arrays (single) with size MxN.\n");
}

/* -------------------------------------------------------------------------------------------- */
void houghLines2Usage() {
	mexPrintf("Usage: B = cvlib_mex('houghlines2',IMG);\n");
	mexPrintf("       where IMG is a uint8 MxNx3 or MxN intensity image OR a MxN logical (mask):\n");
	mexPrintf("       B is a P-by-1 cell array where P is the number of detected lines.\n");
	mexPrintf("       Each cell in the cell array contains a 2x2 matrix. Each row in the\n");
	mexPrintf("       matrix contains the row and column pixel coordinates of the line.\n\n");

	mexPrintf("       Class support: logical or uint8.\n");
	mexPrintf("       Memory overhead: 1 copy of IMG and, if IMG is not of type logical, a MxN.\n");
}

/* -------------------------------------------------------------------------------------------- */
void houghCirclesUsage() {
	mexPrintf("Usage: B = cvlib_mex('houghcircles',IMG [,DP,MIN_DIST,PAR1,PAR2,R0,R1]);\n");
	mexPrintf("       where IMG is a uint8 MxNx3 or MxN intensity image.\n");
	mexPrintf("       B is a P-by-3 matrix where P is the number of detected circles.\n");
	mexPrintf("       and each row of B contains (x,y,radius)\n");
	mexPrintf("       With the options below you may provide empties ([]) to use the default value.\n");
	mexPrintf("	  DP -> Resolution of the accumulator used to detect centers of the circles. For example\n");
	mexPrintf("	        if it is 1, the accumulator will have the same resolution as the input image,\n");
	mexPrintf("	        if it is 2 - accumulator will have twice smaller width and height, etc. [Default = 1]\n");
	mexPrintf("	  MIN_DIST -> Minimum distance between centers of the detected circles. If the parameter\n");
	mexPrintf("	        is too small, multiple neighbor circles may be falsely detected in addition\n");
	mexPrintf("	        to a true one. If it is too large, some circles may be missed [Default = 20]\n");
	mexPrintf("	  PAR1 -> higher threshold of the two passed to Canny edge detector [Default = 50]\n");
	mexPrintf("	  PAR2 -> accumulator threshold at the center detection stage. The smaller it is,\n");
	mexPrintf("	        the more false circles may be detected. Circles, corresponding to the\n");
	mexPrintf("	        larger accumulator values, will be returned first. [Default = 60]\n\n");
	mexPrintf("	  R0 -> Minimal radius of the circles to search for (pixels) [Default = 5]\n");
	mexPrintf("	  R1 -> Maximal radius of the circles to search for. By default the maximal radius\n");
	mexPrintf("	        is set to max(image_width, image_height). [Default = 0]\n");

	mexPrintf("       Class support: uint8.\n");
	mexPrintf("       Memory overhead: 1 copy of IMG.\n");
}

/* -------------------------------------------------------------------------------------------- */
void findContoursUsage() {
	mexPrintf("Usage: B = cvlib_mex('contours',IMG);\n");
	mexPrintf("       where IMG is a uint8 MxNx3 or MxN intensity image OR a MxN logical (mask):\n");
	mexPrintf("       B is a P-by-1 cell array where P is the number of detected lines.\n");
	mexPrintf("       Each cell in the cell array contains a Qx2 matrix. Each row in the\n");
	mexPrintf("       matrix contains the row and column pixel coordinates of the line.\n\n");

	mexPrintf("       Class support: logical or uint8.\n");
	mexPrintf("       Memory overhead: 1 copy of IMG and, if IMG is not of type logical, a MxN.\n");
}

/* -------------------------------------------------------------------------------------------- */
void cannyUsage() {
	mexPrintf("Usage: C = cvlib_mex('canny',IMG);\n");
	mexPrintf("       where IMG is a uint8 MxNx3 rgb OR a MxN intensity image:\n");
	mexPrintf("       C = cvlib_mex('canny',IMG,threshold1,threshold2,aperture_size);\n");
	mexPrintf("       If not provided threshold1 = 40, threshold2 = 200, aperture_size = 3;\n\n");

	mexPrintf("       Class support: uint8.\n");
	mexPrintf("       Memory overhead: 1 copy of IMG and a MxN uint8 matrix.\n");
}

/* -------------------------------------------------------------------------------------------- */
void sobelUsage() {
	mexPrintf("Usage: C = cvlib_mex('sobel',IMG);\n");
	mexPrintf("       where IMG is a uint8 MxNx3 rgb OR a MxN intensity image OR a MxN single:\n");
	mexPrintf("       C = cvlib_mex('sobel',IMG,xorder,yorder,aperture_size);\n");
	mexPrintf("       If not provided xorder = 1, yorder = 0, aperture_size = 3;\n\n");

	mexPrintf("       Class support: uint8 or single.\n");
	mexPrintf("       Memory overhead: 1 copy of IMG and a MxN matrix of the same type as IMG.\n");
}

/* -------------------------------------------------------------------------------------------- */
void laplaceUsage() {
	mexPrintf("Usage: C = cvlib_mex('laplace',IMG);\n");
	mexPrintf("       where IMG is a uint8 MxNx3 rgb OR a MxN intensity image OR a MxN single:\n");
	mexPrintf("       C = cvlib_mex('laplace',IMG,aperture_size);\n");
	mexPrintf("       If not provided aperture_size = 3;\n\n");

	mexPrintf("       Class support: uint8 or single.\n");
	mexPrintf("       Memory overhead: 1 copy of IMG and a MxN matrix of the same type as IMG.\n");
}

/* -------------------------------------------------------------------------------------------- */
void erodeUsage() {
	mexPrintf("Usage: C = cvlib_mex('erode',IMG);\n");
	mexPrintf("       where IMG is a uint8 MxNx3 rgb OR a MxN intensity image OR a MxN single:\n");
	mexPrintf("       C = cvlib_mex('erode',IMG,iterations);\n");
	mexPrintf("       If not provided iterations = 1;\n\n");

	mexPrintf("       Class support: logical, uint8 or single.\n");
	mexPrintf("       Memory overhead: 1 copy of IMG.\n");
}

/* -------------------------------------------------------------------------------------------- */
void dilateUsage() {
	mexPrintf("Usage: C = cvlib_mex('dilate',IMG);\n");
	mexPrintf("       where IMG is a uint8 MxNx3 rgb OR a MxN intensity image OR a MxN single:\n");
	mexPrintf("       C = cvlib_mex('dilate',IMG,iterations);\n");
	mexPrintf("       If not provided iterations = 1;\n\n");

	mexPrintf("       Class support: logical, uint8 or single.\n");
	mexPrintf("       Memory overhead: 1 copy of IMG.\n");
}

/* -------------------------------------------------------------------------------------------- */
void morphologyexUsage() {
	mexPrintf("Usage: C = cvlib_mex('morphologyex',IMG,OPERATION);\n");
	mexPrintf("       where IMG is a uint8 MxNx3 rgb OR a MxN intensity image OR a MxN single:\n");
	mexPrintf("	  The available OPERATIONS are:\n");
	mexPrintf("	  'open'\n");
	mexPrintf("	  'close'\n");
	mexPrintf("	  'gradient'  dilate - erode\n");
	mexPrintf("	  'tophat'    IMG - open\n");
	mexPrintf("	  'blackhat'  close - IMG\n\n");
	mexPrintf("       C = cvlib_mex('morphologyex',IMG,OPERATION,iterations);\n");
	mexPrintf("       If not provided iterations = 1;\n\n");

	mexPrintf("       Class support: logical, uint8 or single.\n");
	mexPrintf("       Memory overhead: 2 copies of IMG.\n");
}

/* -------------------------------------------------------------------------------------------- */
void colorUsage() {
	mexPrintf("Usage: B = cvlib_mex('color',IMG,'TRF');\n");
	mexPrintf("       where IMG is a MxNx3 image of type: uint8, uint16 OR single (0..1 interval).\n");
	mexPrintf("       TRF is a string controling the transformation. Possibilities are:\n");
	mexPrintf("       rgb2lab,lab2rgb, rgb2luv,luv2rgb, rgb2xyz,xyz2rgb\n");
	mexPrintf("       rgb2yiq,yiq2rgb, rgb2hsv,luv2hsv, rgb2hsl,hsl2rgb, rgb2YCrCb,YCrCb2rgb\n\n");

	mexPrintf("       Class support: uint8, uint16 or single.\n");
	mexPrintf("       Memory overhead: 1 copy of IMG and 1 copy of B.\n");
}

/* -------------------------------------------------------------------------------------------- */
void flipUsage() {
	mexPrintf("Usage: B = cvlib_mex('flip',IMG,'DIR');\n");
	mexPrintf("       Flip a 2D array around vertical, horizontall or both axis\n");
	mexPrintf("       where IMG is an array of any type and DIR = 'ud', 'lr' or 'both'\n");
	mexPrintf("       for Up-Down, Left-Right or Both.\n\n");
	mexPrintf("       cvlib_mex('flip',IMG,'DIR');\n");
	mexPrintf("       Flips IMG inplace.\n");
}

/* -------------------------------------------------------------------------------------------- */
void filterUsage() {
	mexPrintf("Usage: B = cvlib_mex('filter',IMG,FILTER);\n");
	mexPrintf("       Convolves image IMG with the kernel FILTER\n\n");

	mexPrintf("       Class support: uint8, int16, uint16 or single.\n");
	mexPrintf("       Memory overhead: 1 copy of IMG and 1 copy of FILTER.\n");
}

/* -------------------------------------------------------------------------------------------- */
void smoothUsage() {
	mexPrintf("Usage: B = cvlib_mex('smooth',IMG [,TYPE,PAR1,PAR2,PAR3,PAR4]);\n");
	mexPrintf("       Smooths the MxNx3 or MxN IMG in one of several ways\n");
	mexPrintf("       The available TYPE of smoothing are:\n");
	mexPrintf("       'blur' summation over a pixel PAR1�PAR2 neighborhood with subsequent scaling by 1/(PAR1xPAR2).\n");
	mexPrintf("       'gaussian' convolves image with PAR1�PAR2 Gaussian kernel [Default].\n");
	mexPrintf("       'median' finding median of PAR1�PAR1 neighborhood (i.e. the neighborhood is square)\n");
	mexPrintf("       'bilateral' apply bilateral 3x3 filtering with color sigma=PAR1 and space sigma=PAR2\n");
	mexPrintf("	          If PAR1 & PAR2 are not provided they default to 5 and 50.\n");
	mexPrintf("	          Information about bilateral filtering can be found at.\n");
	mexPrintf("	          www.dai.ed.ac.uk/CVonline/LOCAL_COPIES/MANDUCHI1/Bilateral_Filtering.html\n");
	mexPrintf("	  PAR1 -> The first parameter of smoothing operation [Default = 5]\n");
	mexPrintf("	  PAR2 -> The second parameter of smoothing operation [Default = PAR1]\n");
	mexPrintf("	  PAR3 -> In case of Gaussian kernel this parameter may specify Gaussian sigma\n");
	mexPrintf("	          (standard deviation). If it is zero, it is calculated from the kernel size\n");
	mexPrintf("	  PAR4 -> In case of non-square Gaussian kernel the parameter may be used to specify\n");
	mexPrintf("	          a different (from param3) sigma in the vertical direction.\n\n");
	mexPrintf("       B = cvlib_mex('smooth',IMG);\n");
	mexPrintf("           Does a gaussian filtering with a 5x5 kernel.\n");

	mexPrintf("       Class support: logical, uint8 or single.\n");
	mexPrintf("       Memory overhead: 1 copy of IMG.\n");
}

/* -------------------------------------------------------------------------------------------- */
void pyrUUsage() {
	mexPrintf("Usage: B = cvlib_mex('pyrU',IMG);\n");
	mexPrintf("       performs up-sampling step of Gaussian pyramid decomposition.\n");
	mexPrintf("       First it upsamples the source image by injecting even zero rows and\n");
	mexPrintf("       and then convolves result with a 5x5 Gaussian filter multiplied by 4\n");
	mexPrintf("       for interpolation. So the destination image is four times larger than\n");
	mexPrintf("       the source image.\n\n");

	mexPrintf("       Class support: logical, uint8, uint16, int16, single or double.\n");
	mexPrintf("       Memory overhead: 1 copy of IMG and 1 copy of B.\n");
}

/* -------------------------------------------------------------------------------------------- */
void pyrDUsage() {
	mexPrintf("Usage: B = cvlib_mex('pyrD',IMG);\n");
	mexPrintf("       performs downsampling step of Gaussian pyramid decomposition.\n");
	mexPrintf("       First it convolves source image with a 5x5 Gaussian filter and then\n");
	mexPrintf("       downsamples the image by rejecting even rows and columns\n\n");

	mexPrintf("       Class support: logical, uint8, uint16, int16, single or double.\n");
	mexPrintf("       Memory overhead: 1 copy of IMG and 1 copy of B.\n");
}

/* -------------------------------------------------------------------------------------------- */
void arithmUsage() {
	mexPrintf("Usage: B = cvlib_mex(OP,IMG1,IMG2);\n");
	mexPrintf("       Apply per element arithmetics betweem IMG1 & IMG2. OP can be one of:\n");
	mexPrintf("       add -> B = IMG1 + IMG2\n");
	mexPrintf("       sub -> B = IMG1 - IMG2\n");
	mexPrintf("       mul -> B = IMG1 * IMG2\n");
	mexPrintf("       div -> B = IMG1 / IMG2\n\n");
	mexPrintf("       If IMG2 is a scalar than OP can also take these values:\n");
	mexPrintf("       addS -> B = IMG1 + IMG2\n");
	mexPrintf("       subS -> B = IMG1 - IMG2\n\n");

	mexPrintf("The form (that is, with no output)\n: cvlib_mex(OP,IMG1,IMG2);\n");
	mexPrintf("       does the above operation in-place and stores the result in IMG1\n\n");

	mexPrintf("       Class support: all but uint32.\n");
	mexPrintf("       Memory overhead: none.\n");
}

/* -------------------------------------------------------------------------------------------- */
void addWeightedUsage() {
	mexPrintf("Usage: B = cvlib_mex('addweighted',IMG1,alpha,IMG2,beta[,gamma]);\n");
	mexPrintf("       The function addweighted calculates weighted sum of two arrays as following:\n");
	mexPrintf("       B(i) = img1(i)*alpha + img2(i)*beta + gamma\n");
	mexPrintf("       If gamma is not provided, gamma = 0\n\n");
	mexPrintf("The form (that is, with no output)\n: cvlib_mex('addweighted',IMG1,alpha,IMG2,beta[,gamma]);\n");
	mexPrintf("       does the above operation in-place and stores the result in IMG1\n\n");

	mexPrintf("       Class support: all but uint32.\n");
	mexPrintf("       Memory overhead: none.\n");
}

/* -------------------------------------------------------------------------------------------- */
void inpaintUsage() {
	mexPrintf("Usage: cvlib_mex('inpaint',IMG1,IMG2);\n");
	mexPrintf("       The function inpaint reconstructs IMG1 image area from the pixel near the area boundary.\n");
	mexPrintf("       The function may be used to remove scratches or undesirable objects from images:\n");
	mexPrintf("       IMG1 is a uint8 MxNx3 rgb OR a MxN intensity image and IMG2 is a mask array\n");
	mexPrintf("       of the same size (MxN) of IMG1. It can be a logical array or a uint8 array.\n");
	mexPrintf("       IMG = cvlib_mex('polyline',IMG1,IMG2);\n");
	mexPrintf("       Returns the drawing in the the new array IMG.\n\n");

	mexPrintf("       Class support: uint8.\n");
	mexPrintf("       Memory overhead: 1 copy of IMG and a MxN matrix of the same type as IMG.\n");
}