A
download edge.c
Language: C
License: GPL
Copyright: (C) 1998 Gloria Bueno (C) 1998,1999,2000,2001,2002 Yohann Sulaiman. (C) 2002 Free Software Foundation, Inc.
LOC: 1135
Project Info
MultiSlice RTP Environment(yhs)
Server: SourceForge
Type: cvs
...eForge\y\yhs\yhs\root\root\
   app_glob.c
   command_run.c
   command_slice.c
   ctomass.c
   edge.c
   editing_tools.c
   exit.c
   FILE_ID.DIZ
   file_viewer.c
   file2.raw
   filters.c
   glxdino.c
   glxmotif.c
   glxsimple.c
   gpl.html
   icon.h
   initapp.c
   inject.c
   label.c
   log.h
   logmen.h
   logo.h
   main.h
   makefile.32
   makefile.64
   makefile.f32
   measure.c
   menu.c
   multislice.c
   neighbour.c
   open.c
   open.old.c
   paperplane.c
   print.c
   program1.c
   region.c
   region_ops.c
   save.c
   slice.c
   split_merge.c
   textfun.c
   version.c
   viewslice.c
   wals.study
   watershed.c
   xbutton.c
   xmenu.c
   xwins.h
   xwinutil.c
   ys_ext.c
   zoom.c
   zoom2.c

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
  /*

MultiSlice RTP Environment
(C) 1998,1999,2000,2001,2002 Yohann Sulaiman.
(C) 1998 Gloria Bueno
(C) 2002  Free Software Foundation, Inc. 


    This file is part of the source code of the MultiSlice RTP Environment.

    MultiSlice RTP Environment 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; either version 2 of the License, or
    (at your option) any later version.

    MultiSlice RTP Environment  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.

    You should have received a copy of the GNU General Public License
    along with MultiSlice RTP Environment; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/




/*-------------------------------------------------------------*/
/*                                                             */
/*     File: edge.c                                            */
/*                                                             */
/*     C-T Image Application Program                           */
/*                                                             */
/*     OSF/Motif version.                                      */
/*                                                             */
/*-------------------------------------------------------------*/

#include "main.h"


#include <Xm/Xm.h>
#include <Xm/SelectioB.h>

#define THRESHOLD_MESSAGE "Enter threshold value (0-255):"
#define SMOOTH_MESSAGE "Enter threshold value (0-255):"

/* Extern variables */
extern char *action;
extern int selection;
static int threshold;
extern Widget draw_2;
extern GC image_gc_2;
extern Pixmap thePixmap_2;
extern XImage *theXImage_1;
extern XImage *theXImage_2;
extern int file_not_loaded;
extern void refresh_action(void);
extern unsigned long fg, bg;
extern int array1[512][512];
extern int array2[512][512];
extern int yhs_files_open;
extern int yhs_filename[11];
extern char *yhs_filename1;
extern int run_once;
extern int running;
extern int file_loader;
extern char          *file_yhs;
extern char          *tempfileold;
extern char          *tempfilenew;
extern char          *addcharac;

extern int range ( int n, int m);

/* Variables for setting resources */
static Arg args[MAXARGS];
static Cardinal argcount;

static Widget threshold_dialog = (Widget) NULL;
static Widget smooth_t_dialog = (Widget) NULL;
static Widget hough_dialog = (Widget) NULL;


/* Function prototypes */

void edge_sobel_callback(Widget w, XtPointer client_data,
			 XmAnyCallbackStruct *call_data);
void edge_kirsch_callback(Widget w, XtPointer client_data,
			 XmAnyCallbackStruct *call_data);
void edge_prewitt_callback(Widget w, XtPointer client_data,
			 XmAnyCallbackStruct *call_data);
void edge_frei_callback(Widget w, XtPointer client_data,
			 XmAnyCallbackStruct *call_data);
void line_frei_callback(Widget w, XtPointer client_data,
			 XmAnyCallbackStruct *call_data);
void edge_marr_callback(Widget w, XtPointer client_data,
			 XmAnyCallbackStruct *call_data);
void edge_roberts_callback(Widget w, XtPointer client_data,
			 XmAnyCallbackStruct *call_data);
void edge_vert_callback(Widget w, XtPointer client_data,
			 XmAnyCallbackStruct *call_data);
void create_edge_hv_dialog(Widget parent);
void activate_edge_hv_dialog(Widget w, XtPointer client_data,
			       XmAnyCallbackStruct *call_data);
void deactivate_edge_hv_dialog(void);
static void ok_edge_hvbutton_callback(Widget w, XtPointer client_data,
			       XmSelectionBoxCallbackStruct *call_data);
static void cancel_edge_hv_callback(Widget w, XtPointer client_data,
				   XmSelectionBoxCallbackStruct *call_data);
void edge_hv(int iterations);

void create_edge_dilate_dialog(Widget parent);
void activate_edge_dilate_dialog(Widget w, XtPointer client_data,
			       XmAnyCallbackStruct *call_data);
void deactivate_edge_dilate_dialog(void);
static void ok_edge_dilatebutton_callback(Widget w, XtPointer client_data,
			       XmSelectionBoxCallbackStruct *call_data);
static void cancel_edge_dilate_callback(Widget w, XtPointer client_data,
				   XmSelectionBoxCallbackStruct *call_data);
void edge_dilate(int loop);
void edge_unsharp_callback(Widget w, XtPointer client_data,
			 XmAnyCallbackStruct *call_data);

void mean_filter_callback(Widget w, XtPointer client_data,
			 XmAnyCallbackStruct *call_data);

void create_smooth_t_dialog(Widget parent);
void activate_smooth_t_dialog(Widget w, XtPointer client_data,
				XmAnyCallbackStruct *call_data);
void deactivate_smooth_t_dialog(void);
static void ok_smooth_tbutton_callback(Widget w, XtPointer client_data,
				XmSelectionBoxCallbackStruct *call_data);
static void cancel_smooth_t_callback(Widget w, XtPointer client_data,
				XmSelectionBoxCallbackStruct *call_data);

void smooth_t(int t);

void smooth_mask1_callback(Widget w, XtPointer client_data,
			 XmAnyCallbackStruct *call_data);
void smooth_mask2_callback(Widget w, XtPointer client_data,
			 XmAnyCallbackStruct *call_data);
void median_filter_callback(Widget w, XtPointer client_data,
			 XmAnyCallbackStruct *call_data);
void median_vf_callback(Widget w, XtPointer client_data,
			 XmAnyCallbackStruct *call_data);

void create_hough_dialog(Widget parent);
void activate_hough_dialog(Widget w, XtPointer client_data,
				XmAnyCallbackStruct *call_data);
void deactivate_hough_dialog(void);
static void ok_hough_callback(Widget w, XtPointer client_data,
				XmSelectionBoxCallbackStruct *call_data);
static void cancel_hough_callback(Widget w, XtPointer client_data,
				XmSelectionBoxCallbackStruct *call_data);
void hough(int t);


void create_threshold_dialog(Widget parent);
void activate_threshold_dialog(Widget w, XtPointer client_data,
				XmAnyCallbackStruct *call_data);
void deactivate_threshold_dialog(void);
static void ok_threshbutton_callback(Widget w, XtPointer client_data,
				XmSelectionBoxCallbackStruct *call_data);
static void cancel_threshbutton_callback(Widget w, XtPointer client_data,
				XmSelectionBoxCallbackStruct *call_data);
static void thresholding(int t);


void histogram_callback(Widget w, XtPointer client_data,
				XmAnyCallbackStruct *call_data);
void histo_eq_callback(Widget w, XtPointer client_data,
				XmAnyCallbackStruct *call_data);


void convolve (int y[3][3]);
int max4 (int a, int b, int c, int d);
int min4 (int a, int b, int c, int d);
void medsort(int *arr, int n);
Widget edge_hv_dialog;
Widget edge_dilate_dialog;

/* Function definition */

/* Apply a Sobel edge mask to the image theXImage_1 */
void edge_sobel_callback(Widget w, XtPointer client_data,
			 XmAnyCallbackStruct *call_data) {
  int i,j;
  float rng;
  unsigned long n, m, k, rmax, rmin;

  if (file_not_loaded) return;

  selection = EDGE_SOBEL;
  action = SELECT;
  refresh_action();

  /* Locate MAX and MIN for rescaling */
  rmax = XGetPixel(theXImage_1,0,0);
  rmin = rmax;
  for (i=1; i<IMAGE_WIDTH-1; i++)
    for (j=1; j<IMAGE_HEIGHT-1; j++) {
      n = ( XGetPixel(theXImage_1,i-1,j+1) +
	    2*XGetPixel(theXImage_1,i,j+1) +
	    XGetPixel(theXImage_1,i+1,j+1) ) -
	  ( XGetPixel(theXImage_1,i-1,j-1) +
	    2*XGetPixel(theXImage_1,i,j-1) +
	    XGetPixel(theXImage_1,i+1,j-1) );
      m = ( XGetPixel(theXImage_1,i+1,j-1) +
	    2*XGetPixel(theXImage_1,i+1,j) +
	    XGetPixel(theXImage_1,i+1,j+1) ) -
	  ( XGetPixel(theXImage_1,i-1,j-1) +
	    2*XGetPixel(theXImage_1,i-1,j) +
	    XGetPixel(theXImage_1,i-1,j+1) );
      k = abs(n)+abs(m);
      if (k>rmax) rmax = k;
      if (k<rmin) rmin = k;
    }
  rng = (float)(rmax-rmin);

  /* Now compute the convolution, scaling */
  for (i=1; i<IMAGE_WIDTH-1; i++)
    for (j=1; j<IMAGE_HEIGHT-1; j++) {
      n = ( XGetPixel(theXImage_1,i-1,j+1) +
            2*XGetPixel(theXImage_1,i,j+1) +
	    XGetPixel(theXImage_1,i+1,j+1) ) -
          ( XGetPixel(theXImage_1,i-1,j-1) +
            2*XGetPixel(theXImage_1,i,j-1) +
	    XGetPixel(theXImage_1,i+1,j-1) );
      m = ( XGetPixel(theXImage_1,i+1,j-1) +
	    2*XGetPixel(theXImage_1,i+1,j) +
            XGetPixel(theXImage_1,i+1,j+1) ) -
          ( XGetPixel(theXImage_1,i-1,j-1) +
            2*XGetPixel(theXImage_1,i-1,j) +
            XGetPixel(theXImage_1,i-1,j+1) );
      k = abs(n)+abs(m);
      XPutPixel(theXImage_2,i,j,(unsigned long) (((float)(k-rmin)/rng)*256.0));
    }

  /* Copy image into pixmap */
  XPutImage(XtDisplay(draw_2), thePixmap_2, image_gc_2, theXImage_2,
              0, 0, 0, 0, theXImage_2->width, theXImage_2->height);

  /* Clear the drawing window so the image is displayed again */
  XClearArea(XtDisplay(w),XtWindow(draw_2),0,0,0,0,True);
}

/*      Apply a Laplacian edge mask to the image X      */
void edge_laplac_callback(Widget w, XtPointer client_data,
                         XmAnyCallbackStruct *call_data) {

	int mask[3][3];

  if (file_not_loaded) return;

  selection = EDGE_LAPLAC;
  action = SELECT;
  refresh_action();



	mask[0][0]=0; mask[0][1]=1; mask[0][2]=0;
	mask[1][0] = 1; mask[1][1] = -4; mask[1][2]=1;
	mask[2][0]=0; mask[2][1]=1; mask[2][2]=0;
	convolve ( mask);

/* Clear the drawing window so the image is displayed again */
  XClearArea(XtDisplay(w),XtWindow(draw_2),0,0,0,0,True);

}


/*      Do a simple convolution of the image X with the 3x3 mask Y      */

void convolve (int y[3][3])
{
	int i,j,n,m,k,rmax,rmin;
	
	float rng;



/* Locate MAX and MIN for rescaling */
	rmax = XGetPixel(theXImage_1,0,0); rmin = rmax;
	for (i=1; i<IMAGE_WIDTH-1; i++)
	   for (j=1; j<IMAGE_HEIGHT-1; j++) {
		k = 0;
		for (n=0; n<3; n++)
		   for (m=0; m<3; m++)
		      k += y[n][m]*XGetPixel(theXImage_1,i+n-1,j+m-1);
		if (k>rmax) rmax = k;
		if (k<rmin) rmin = k;
	   }
	rng = (float)(rmax-rmin);

/* Now compute the convolution, scaling */
	for (i=1; i<IMAGE_WIDTH-1; i++)
	   for (j=1; j<IMAGE_HEIGHT-1; j++) {
		k = 0;
		for (n=0; n<3; n++)
		   for (m=0; m<3; m++)
		      k += y[n][m]*XGetPixel(theXImage_1,i+n-1,j+m-1);
		XPutPixel(theXImage_2,i,j,(unsigned long) (((float)(k-rmin)/rng)*256.0));
	   }

	XPutImage(XtDisplay(draw_2), thePixmap_2,image_gc_2, theXImage_2, 0, 0, 0, 0, theXImage_2->width, theXImage_2->height);
	

}


/*      Apply a Kirsch edge mask to the image X */

void edge_kirsch_callback(Widget w, XtPointer client_data,
                         XmAnyCallbackStruct *call_data)
{


	int i,j,k1,k2,k3,k4,k,rmax,rmin;
	
	float rng;

  if (file_not_loaded) return;

  selection = EDGE_KIRSCH;
  action = SELECT;
  refresh_action();
	

/* Locate MAX and MIN for rescaling */
	rmax = XGetPixel(theXImage_1,0,0); rmin = rmax;
	for (i=1; i<IMAGE_WIDTH-1; i++)
	   for (j=1; j<IMAGE_HEIGHT-1; j++) {
		k1 = XGetPixel(theXImage_1,i,j+1) + XGetPixel(theXImage_1,i-1,j+1) + XGetPixel(theXImage_1,i+1,j+1) -
		    (XGetPixel(theXImage_1,i,j-1) + XGetPixel(theXImage_1,i-1,j-1) + XGetPixel(theXImage_1,i+1,j-1));
		k2 = XGetPixel(theXImage_1,i-1,j-1) + XGetPixel(theXImage_1,i-1,j) + XGetPixel(theXImage_1,i-1,j+1) -
		    (XGetPixel(theXImage_1,i+1,j-1) + XGetPixel(theXImage_1,i+1,j) + XGetPixel(theXImage_1,i+1,j+1));
		k3 = XGetPixel(theXImage_1,i-1,j) + XGetPixel(theXImage_1,i-1,j+1) + XGetPixel(theXImage_1,i,j+1) -
		    (XGetPixel(theXImage_1,i,j-1) + XGetPixel(theXImage_1,i+1,j-1) + XGetPixel(theXImage_1,i+1,j));
		k4 = XGetPixel(theXImage_1,i,j-1) + XGetPixel(theXImage_1,i-1,j-1) + XGetPixel(theXImage_1,i-1,j) -
		    (XGetPixel(theXImage_1,i,j+1) + XGetPixel(theXImage_1,i+1,j+1) + XGetPixel(theXImage_1,i+1,j));
		k = (k1+k2+k3+k4)/4;
		if (rmax < k) rmax = k;
		if (rmin > k) rmin = k;
	   }
	rng = (float)(rmax-rmin);

/* Now compute the convolution, scaling */
	for (i=1; i<IMAGE_WIDTH-1; i++)
	   for (j=1; j<IMAGE_HEIGHT-1; j++) {
		k1 = XGetPixel(theXImage_1,i,j+1) + XGetPixel(theXImage_1,i-1,j+1) + XGetPixel(theXImage_1,i+1,j+1) -
		    (XGetPixel(theXImage_1,i,j-1) + XGetPixel(theXImage_1,i-1,j-1) + XGetPixel(theXImage_1,i+1,j-1));
		k2 = XGetPixel(theXImage_1,i-1,j-1) + XGetPixel(theXImage_1,i-1,j) + XGetPixel(theXImage_1,i-1,j+1) -
		    (XGetPixel(theXImage_1,i+1,j-1) + XGetPixel(theXImage_1,i+1,j) + XGetPixel(theXImage_1,i+1,j+1));
		k3 = XGetPixel(theXImage_1,i-1,j) + XGetPixel(theXImage_1,i-1,j+1) + XGetPixel(theXImage_1,i,j+1) -
		    (XGetPixel(theXImage_1,i,j-1) + XGetPixel(theXImage_1,i+1,j-1) + XGetPixel(theXImage_1,i+1,j));
		k4 = XGetPixel(theXImage_1,i,j-1) + XGetPixel(theXImage_1,i-1,j-1) + XGetPixel(theXImage_1,i-1,j) -
		    (XGetPixel(theXImage_1,i,j+1) + XGetPixel(theXImage_1,i+1,j+1) + XGetPixel(theXImage_1,i+1,j));
		k = (k1+k2+k3+k4)/4;
		XPutPixel(theXImage_2,i,j, (unsigned long) (((float)(k-rmin)/rng)*256.0));
	   }

	XPutImage(XtDisplay(draw_2), thePixmap_2, image_gc_2,theXImage_2, 0, 0, 0, 0,theXImage_2->width,theXImage_2->height);

	XClearArea(XtDisplay(w), XtWindow(draw_2), 0, 0, 0, 0, True);

}

void edge_prewitt_callback(Widget w, XtPointer client_data,
                         XmAnyCallbackStruct *call_data)
{
 	int i,j,n,m,k,rmax,rmin;
	
	float rng;

  if (file_not_loaded) return;

  selection = EDGE_PREWITT;
  action = SELECT;
  refresh_action();

	

/* Locate MAX and MIN for rescaling */
	rmax = XGetPixel(theXImage_1,0,0); rmin = rmax;
	for (i=1; i<IMAGE_WIDTH-1; i++)
	   for (j=1; j<IMAGE_HEIGHT-1; j++) {
		n = (XGetPixel(theXImage_1,i-1,j+1)+XGetPixel(theXImage_1,i,j+1)+XGetPixel(theXImage_1,i+1,j+1)) -
		    (XGetPixel(theXImage_1,i-1,j-1)+XGetPixel(theXImage_1,i,j-1)+XGetPixel(theXImage_1,i+1,j-1));
		m = (XGetPixel(theXImage_1,i+1,j-1)+XGetPixel(theXImage_1,i+1,j)+XGetPixel(theXImage_1,i+1,j+1))-
		    (XGetPixel(theXImage_1,i-1,j-1)+XGetPixel(theXImage_1,i-1,j)+XGetPixel(theXImage_1,i-1,j+1));
		k = abs(n)+abs(m);
		if (k>rmax) rmax = k;
		if (k<rmin) rmin = k;
	   }
	rng = (float)(rmax-rmin);

/* Now compute the convolution, scaling */
	for (i=1; i<IMAGE_WIDTH-1; i++)
	   for (j=1; j<IMAGE_HEIGHT-1; j++) {
		n = (XGetPixel(theXImage_1,i-1,j+1)+XGetPixel(theXImage_1,i,j+1)+XGetPixel(theXImage_1,i+1,j+1)) -
		    (XGetPixel(theXImage_1,i-1,j-1)+XGetPixel(theXImage_1,i,j-1)+XGetPixel(theXImage_1,i+1,j-1));
		m = (XGetPixel(theXImage_1,i+1,j-1)+XGetPixel(theXImage_1,i+1,j)+XGetPixel(theXImage_1,i+1,j+1))-
		    (XGetPixel(theXImage_1,i-1,j-1)+XGetPixel(theXImage_1,i-1,j)+XGetPixel(theXImage_1,i-1,j+1));
		k = abs(n)+abs(m);
		XPutPixel(theXImage_2,i,j, (unsigned long) (((float)(k-rmin)/rng)*256.0));
	   }


       	XPutImage(XtDisplay(draw_2), thePixmap_2, image_gc_2,theXImage_2, 0, 0, 0, 0,theXImage_2->width,theXImage_2->height);
	XClearArea(XtDisplay(w), XtWindow(draw_2), 0, 0, 0, 0, True);
}

/*      Apply Frei-Chen edge masks to the image X       */

void edge_frei_callback(Widget w, XtPointer client_data,
			 XmAnyCallbackStruct *call_data)
{
	int i,j,n,m,k,rmax,rmin;
	
	float rng,sq2;

  if (file_not_loaded) return;

  selection = EDGE_FREI;
  action = SELECT;
  refresh_action();
		
  sq2 = /*sqrt(2.0);*/ 1.41421 ; /* si pongo sqrt da unresolved ????  */
	

/* Locate MAX and MIN for rescaling */
	rmax =XGetPixel(theXImage_1,0,0); rmin = rmax;
	for (i=1; i<IMAGE_WIDTH-1; i++)
	   for (j=1; j<IMAGE_HEIGHT-1; j++) {
		n = (XGetPixel(theXImage_1,i-1,j+1)+sq2*XGetPixel(theXImage_1,i,j+1)+XGetPixel(theXImage_1,i+1,j+1)) -
		    (XGetPixel(theXImage_1,i-1,j-1)+sq2*XGetPixel(theXImage_1,i,j-1)+XGetPixel(theXImage_1,i+1,j-1));
		m = (XGetPixel(theXImage_1,i+1,j-1)+sq2*XGetPixel(theXImage_1,i+1,j)+XGetPixel(theXImage_1,i+1,j+1))-
		    (XGetPixel(theXImage_1,i-1,j-1)+sq2*XGetPixel(theXImage_1,i-1,j)+XGetPixel(theXImage_1,i-1,j+1));
		k = abs(n)+abs(m);
		if (k>rmax) rmax = k;
		if (k<rmin) rmin = k;
	   }
	rng = (float)(rmax-rmin);

/* Now compute the convolution, scaling */
	for (i=1; i<IMAGE_WIDTH-1; i++)
	   for (j=1; j<IMAGE_HEIGHT-1; j++) {
		n = (XGetPixel(theXImage_1,i-1,j+1)+sq2*XGetPixel(theXImage_1,i,j+1)+XGetPixel(theXImage_1,i+1,j+1)) -
		    (XGetPixel(theXImage_1,i-1,j-1)+sq2*XGetPixel(theXImage_1,i,j-1)+XGetPixel(theXImage_1,i+1,j-1));
		m = (XGetPixel(theXImage_1,i+1,j-1)+sq2*XGetPixel(theXImage_1,i+1,j)+XGetPixel(theXImage_1,i+1,j+1))-
		    (XGetPixel(theXImage_1,i-1,j-1)+sq2*XGetPixel(theXImage_1,i-1,j)+XGetPixel(theXImage_1,i-1,j+1));
		k = abs(n)+abs(m);
		XPutPixel(theXImage_2,i,j, (unsigned long) (((float)(k-rmin)/rng)*256.0));
	   }


       	XPutImage(XtDisplay(draw_2), thePixmap_2, image_gc_2,theXImage_2, 0, 0, 0, 0,theXImage_2->width,theXImage_2->height);
	XClearArea(XtDisplay(w), XtWindow(draw_2), 0, 0, 0, 0, True);
	
}

/*      Apply the Frei-Chen line masks to the image X   */

void line_frei_callback(Widget w, XtPointer client_data,
			 XmAnyCallbackStruct *call_data)
{
 
	int i,j,k,rmax,rmin;
	float rng,sq2;
	int f1,f2,f3,f4;


  if (file_not_loaded) return;

  selection = LINE_FREI;
  action = SELECT;
  refresh_action();



	sq2 = /*sqrt(2.0)*/ 1.41421;
	

/* Locate MAX and MIN for rescaling */
	rmax = (XGetPixel(theXImage_1,0,0)); rmin = rmax;
	for (i=1; i<IMAGE_WIDTH-1; i++)
	   for (j=1; j<IMAGE_HEIGHT-1; j++) {
	     f1=XGetPixel(theXImage_1,i-1,j)+XGetPixel(theXImage_1,i+1,j)- XGetPixel(theXImage_1,i,j-1)+
XGetPixel(theXImage_1,i,j+1);
	     f2 = XGetPixel(theXImage_1,i-1,j+1)+ XGetPixel(theXImage_1,i+1,j-1)- XGetPixel(theXImage_1,i-1,j-1)+
XGetPixel(theXImage_1,i+1,j+1);
	     f3 = XGetPixel(theXImage_1,i-1,j-1)+ XGetPixel(theXImage_1,i-1,j+1)+ XGetPixel(theXImage_1,i+1,j-1)+ 
XGetPixel(theXImage_1,i+1,j+1) -2.0*XGetPixel(theXImage_1,i-1,j)+XGetPixel(theXImage_1,i+1,j)+
XGetPixel(theXImage_1,i,j-1)+XGetPixel(theXImage_1,i,j+1)+XGetPixel(theXImage_1,i,j)*4.0;
	     f4 = XGetPixel(theXImage_1,i,j-1)+XGetPixel(theXImage_1,i,j+1)+XGetPixel(theXImage_1,i-1,j)+
XGetPixel(theXImage_1,i+1,j) -2.0*XGetPixel(theXImage_1,i-1,j-1)+XGetPixel(theXImage_1,i-1,j+1)+
XGetPixel(theXImage_1,i+1,j-1)+ XGetPixel(theXImage_1,i+1,j+1)+ 4.0 * XGetPixel(theXImage_1,i,j);
		k = abs(f1)+abs(f2)+abs(f3)+abs(f4);
		k = (f1,f2,f3,f4);
		if (k>rmax) rmax = k;
		if (k<rmin) rmin = k;
	   }
	rng = (float)(rmax-rmin);

/* Now compute the convolution, scaling */
	for (i=1; i<IMAGE_WIDTH-1; i++)
	   for (j=1; j<IMAGE_HEIGHT-1; j++) {
	     f1= XGetPixel(theXImage_1,i-1,j)+ XGetPixel(theXImage_1,i+1,j)- XGetPixel(theXImage_1,i,j-1)+
XGetPixel(theXImage_1,i,j+1);
	     f2 = XGetPixel(theXImage_1,i-1,j+1)+XGetPixel(theXImage_1,i+1,j-1)- XGetPixel(theXImage_1,i-1,j-1)+
XGetPixel(theXImage_1,i+1,j+1);
	     f3 = XGetPixel(theXImage_1,i-1,j-1)+XGetPixel(theXImage_1,i-1,j+1)+XGetPixel(theXImage_1,i+1,j-1)+
XGetPixel(theXImage_1,i+1,j+1) -2.0* XGetPixel(theXImage_1,i-1,j)+ XGetPixel(theXImage_1,i+1,j)+ 
XGetPixel(theXImage_1,i,j-1)+ XGetPixel(theXImage_1,i,j+1)+ XGetPixel(theXImage_1,i,j)*4.0;
	     f4 = XGetPixel(theXImage_1,i,j-1)+XGetPixel(theXImage_1,i,j+1)+XGetPixel(theXImage_1,i-1,j)+
XGetPixel(theXImage_1,i+1,j) -2.0*XGetPixel(theXImage_1,i-1,j-1)+ XGetPixel(theXImage_1,i-1,j+1)+
XGetPixel(theXImage_1,i+1,j-1)+	XGetPixel(theXImage_1,i+1,j+1)+4.0* XGetPixel(theXImage_1,i,j);
		k = abs(f1)+abs(f2)+abs(f3)+abs(f4);
		k = max4 (f1,f2,f3,f4);
		XPutPixel(theXImage_2,i,j, (unsigned long)  (((float)(k-rmin)/rng)*256.0));
	   }



	XPutImage(XtDisplay(draw_2), thePixmap_2, image_gc_2,theXImage_2, 0, 0, 0, 0,theXImage_2->width,theXImage_2->height);
	XClearArea(XtDisplay(w), XtWindow(draw_2), 0, 0, 0, 0, True);
}

/*      Apply the Marr edge detector to the image X     */

void edge_marr_callback(Widget w, XtPointer client_data,
                         XmAnyCallbackStruct *call_data)
{

	int i,j,n,m,k,rmax,rmin;
	int h[11][11];
	
	float rng;
 
  if (file_not_loaded) return;

  selection = EDGE_MARR;
  action = SELECT;
  refresh_action();

	
	

	/* pone la imagen 2 a blanmco. la mia sta ya a negro */ 
	/*for (i=0; i<IMAGE_WIDTH; i++)
	   for (j=0; j<IMAGE_HEIGHT; j++)
		z->data[i,j] = 255;*/

/* Initialize the Laplacian of Gaussian mask */

/* Initialize the Laplacian of Gaussian mask */
h[0][0]=0; h[0][1]=0; h[0][2]=0; h[0][3]=0; h[0][4]=0; h[0][5]=-1; 
h[0][6]=0; h[0][7]=0; h[0][8]=0; h[0][9]=0; h[0][10]=0;
h[1][0]=0; h[1][1]=0; h[1][2]=2; h[1][3]=4; h[1][4]=-2; h[1][5]=-10; 
h[1][6]=-2; h[1][7]=4; h[1][8]=2; h[1][9]=0; h[1][10]=0;
h[2][0]=0; h[2][1]=2; h[2][2]=13; h[2][3]=20; h[2][4]=-13; h[2][5]=-46; 
h[2][6]=-13; h[2][7]=20; h[2][8]=13; h[2][9]=2; h[2][10]=0; 
h[3][0]=0; h[3][1]=4; h[3][2]=20; h[3][3]=30; h[3][4]=-19; h[3][5]=-71; 
h[3][6]=-19; h[3][7]=30; h[3][8]=20; h[3][9]=4; h[3][10]=0; 
h[4][0]=0; h[4][1]=-2; h[4][2]=-13; h[4][3]=-19; h[4][4]=13; h[4][5]=46;
h[4][6]=13; h[4][7]=-19; h[4][8]=-13; h[4][9]=-2; h[4][10]=0; 
h[5][0]=-1; h[5][1]=-10; h[5][2]=-46; h[5][3]=-71; h[5][4]=46; h[5][5]=166; 
h[5][6]=46; h[5][7]=-71; h[5][8]=-46; h[5][9]=-10; h[5][10]=-1;
h[6][0]=0; h[6][1]=-2; h[6][2]=-13; h[6][3]=-19; h[6][4]=13; h[6][5]=46; 
h[6][6]=13; h[6][7]=-19; h[6][8]=-13; h[6][9]=-2; h[6][10]=0;
h[7][0]=0; h[7][1]=4; h[7][2]=20; h[7][3]=30; h[7][4]=-19; h[7][5]=-71; 
h[7][6]=-19; h[7][7]=30; h[7][8]=20; h[7][9]=4; h[7][10]=0; 
h[8][0]=0; h[8][1]=2; h[8][2]=13; h[8][3]=20; h[8][4]=-13; h[8][5]=-46; 
h[8][6]=-13; h[8][7]=20; h[8][8]=13; h[8][9]=2; h[8][10]=0; 
h[9][0]=0; h[9][1]=0; h[9][2]=2; h[9][3]=4; h[9][4]=-2; h[9][5]=-10; 
h[9][6]=-2; h[9][7]=4; h[9][8]=2; h[9][9]=0; h[9][10]=0;
h[10][0]=0; h[10][1]=0; h[10][2]=0; h[10][3]=0; h[10][4]=0; h[10][5]=-1; 
h[10][6]=0; h[10][7]=0; h[10][8]=0; h[10][9]=0; h[10][10]=0;

/* Locate MAX and MIN for rescaling */
	rmax = (XGetPixel(theXImage_1,0,0)); rmin = rmax;
	for (i=5; i<IMAGE_WIDTH-5; i++)
	   for (j=5; j<IMAGE_HEIGHT-5; j++) {
		k = 0;
		for (n=0; n<11; n++)
		   for (m=0; m<11; m++)
		      k += h[n][m]* XGetPixel(theXImage_1,i+n-5,j+m-5);
		if (k>rmax) rmax = k;
		if (k<rmin) rmin = k;
	   }
	rng = (double)(rmax-rmin);

	printf ("Max=%d  Min = %d\n",rmax, rmin);

/* Now compute the convolution, scaling */
	for (i=5; i<IMAGE_WIDTH-5; i++)
	   for (j=5; j<IMAGE_HEIGHT-5; j++) {
		k = 0;
		for (n=0; n<11; n++)
		   for (m=0; m<11; m++)
		      k += h[n][m]* (XGetPixel(theXImage_1,i+n-5,j+m-5));
		XPutPixel(theXImage_2,i,j, (unsigned long)(((float)(k-rmin)/rng)*256.0));
	   }


	XPutImage(XtDisplay(draw_2), thePixmap_2, image_gc_2,theXImage_2, 0, 0, 0, 0,theXImage_2->width,theXImage_2->height);
	XClearArea(XtDisplay(w), XtWindow(draw_2), 0, 0, 0, 0, True);
}

/* void edge_marr2_callback(Widget w, XtPointer client_data,
			 XmAnyCallbackStruct *call_data)
{

  if (file_not_loaded) return;

  selection = EDGE_MARR2;
  action = SELECT;
  refresh_action();

	int i,j,n,m,k,rmax,rmin;
	int h[11,11];
	int zval, nr, nc;
	FILE *z;


	nr = IMAGE_WIDTH; nc = IMAGE_HEIGHT;
	z = make_vai ("d:z.dat", nr, nc);
	if (z == 0) {
	   *error_code = OUT_OF_STORAGE;
	   return;
	}

	for (i=0; i<nr; i++)
	   for (j=0; j<nc; j++)
		if (put_vai (z, i, j, nr, nc, 255) == 0) {
		   *error_code = IO_ERROR;
		   fclose (z); unlink ("d:z.dat");
		   return;
		}
h[0][0]=0; h[0][1]=0; h[0][2]=0;h[0][3]= -1; h[0][4] = -1; h[0][5] = -2;
 h[0][6] = -1; h[0][7] = -1; h[0][8]=0; h[0][9]=0;h[0][10] = 0;
h[1][0]=0; h[1][1]=0; h[1][2]= -2; h[1][3]= -4; h[1][4] = -8;
 h[1][5] = -9;
 h[1][6] = -8; h[1][7] = -4; h[1][8]= -2; h[1][9]=0;h[1][10] = 0;
h[2][0]=0;h[2][1]= -2;h[2][2]= -7;h[2][3]= -15;h[2][4] = -22;h[2][5]= -23;
 h[2][6] = -22;h[2][7] = -15;h[2][8]= -7;h[2][9]= -2;h[2][10] = 0;
h[3][0]= -1;h[3][1]= -4;h[3][2]= -15;h[3][3]= -24;h[3][4] = -14;h[3][5]= -1;
 h[3][6] = -14;h[3][7] = -24;h[3][8]= -15;h[3][9]= -4;h[3][10] = -1;
h[4][0]= -1;h[4][1]= -8;h[4][2]= -22;h[4][3]= -14;h[4][4] = 52;h[4][5]= 103;
 h[4][6] = 52;h[4][7] = -14;h[4][8]= -22;h[4][9]= -8;h[4][10] = -1;
h[5][0]= -2;h[5][1]= -9;h[5][2]= -23;h[5][3]= -1;h[5][4] = 103;h[5][5]= 178;
 h[5][6] = 103;h[5][7] = -1;h[5][8]= -23;h[5][9]= -9;h[5][10] = -2;
h[6][0]= -1;h[6][1]= -8;h[6][2]= -22;h[6][3]= -14;h[6][4] = 52;h[6][5]= 103;
 h[6][6] = 52;h[6][7] = -14;h[6][8]= -22;h[6][9]= -8;h[6][10] = -1;
h[7][0]= -1;h[7][1]= -4;h[7][2]= -15;h[7][3]= -24;h[7][4] = -14;h[7][5]= -1;
 h[7][6] = -14;h[7][7] = -24;h[7][8]= -15;h[7][9]= -4;h[7][10] = -1;
h[8][0]=0;h[8][1]= -2;h[8][2]= -7;h[8][3]= -15;h[8][4] = -22;h[8][5]= -23;
 h[8][6] = -22;h[8][7] = -15;h[8][8]= -7;h[8][9]= -2;h[8][10] = 0;
h[9][0]=0; h[9][1]=0; h[9][2]= -2; h[9][3]= -4; h[9][4] = -8;
 h[9][5] = -9;
 h[9][6] = -8; h[9][7] = -4; h[9][8]= -2; h[9][9]=0;h[9][10] = 0;
h[10][0]=0; h[10][1]=0; h[10][2]=0;h[0][3]= -1; h[10][4] = -1; h[10][5] = -2;
 h[10][6] = -1; h[10][7] = -1; h[10][8]=0; h[10][9]=0;h[10][10] = 0;

	for (i=5; i<IMAGE_WIDTH-5; i++)
	   for (j=5; j<IMAGE_HEIGHT-5; j++) {
		k = 0;
		for (n=0; n<11; n++)
		   for (m=0; m<11; m++)
		      k += h[n,m]*(XGetPixel(theXImage_1,i+n-5,j+m-5);
		if (put_vai (z, i, j, nr, nc, k)==0) {
		   *error_code = IO_ERROR;
		   fclose(z); unlink ("d:z.dat");
		   return;
		}
	   }

	for (i=5; i<IMAGE_WIDTH-5; i++)
	   for (j=5; j<IMAGE_HEIGHT-5; j++) {
		   rmin = (XGetPixel(theXImage_1,i-1,j); rmax = rmin;
		   for (n= -1; n<=1; n++)
		      for (m= -1; m<=1; m++)
			 if (n!=0 || m!=0) {
			   if (get_vai (z, i+n,j+m, nr,nc, &zval)== 0) {
			      *error_code = IO_ERROR;
			      fclose (z);  unlink ("d:z.dat");
			      return;
			   }
			   if (zval > rmax) rmax = zval;
			   if (zval < rmin) rmin = zval;
			 }
		   if (get_vai (z, i, j, nr, nc, &k) == 0) {
		      *error_code = IO_ERROR;
		      fclose (z);  unlink ("d:z.dat");
		      return;
		   }
		   if ((k>t &&rmin< -t) || (k< -t && rmax>t))
			(XGetPixel(theXImage_1,i,j) = 0;
		   else (XGetPixel(theXImage_1,i,j) = 255;
	   }
	   fclose (z);
	   unlink ("d:z.dat");
}*/


/*      Apply the Roberts edge detector to the image X  */

void edge_roberts_callback(Widget w, XtPointer client_data,
			 XmAnyCallbackStruct *call_data)
{
 int i,j,n,m;

  if (file_not_loaded) return;

  selection = EDGE_ROBERTS;
  action = SELECT;
  refresh_action();



	for (i=0; i<IMAGE_WIDTH-1; i++)
	   for (j=0; j<IMAGE_HEIGHT-1; j++) {
		n = XGetPixel(theXImage_1,i+1,j+1)-XGetPixel(theXImage_1,i,j);
		m = XGetPixel(theXImage_1,i,j+1)- XGetPixel(theXImage_1,i+1,j);
		m = (abs(n)+abs(m))/2;
	}
	XPutPixel(theXImage_2,i,j, (unsigned char)m);
	XPutImage(XtDisplay(draw_2), thePixmap_2, image_gc_2,theXImage_2, 0, 0, 0, 0,theXImage_2->width,theXImage_2->height);
	XClearArea(XtDisplay(w), XtWindow(draw_2), 0, 0, 0, 0, True);

}

/*      Locate vertical edges in X using a simple gradient      */

void edge_vert_callback(Widget w, XtPointer client_data,
                         XmAnyCallbackStruct *call_data)
{


	int mask[3][3];

  if (file_not_loaded) return;

  selection = EDGE_VERT;
  action = SELECT;
  refresh_action();


	mask[0][0]=0; mask[0][1]=0; mask[0][2]=0;
	mask[1][0] = -1; mask[1][1] = 1; mask[1][2]=0;
	mask[2][0]=0; mask[2][1]=0; mask[2][2]=0;
	convolve (mask);

/* Despues de usar convolve, hacer un XClear () para que pinte la imagen*/

	XClearArea(XtDisplay(w), XtWindow(draw_2), 0, 0, 0, 0, True);
}

/*      Locate horizontal edges in X using a simple gradient    */



/*----------------------------------------------------------------------*/
/*   				Dilate 		   			*/
/*									*/
/*  						   			*/
/*----------------------------------------------------------------------*/

void create_edge_hv_dialog(Widget parent) {

  XmString message;
  Widget temp_widget = parent;

  /* Ensure the parent of the dialog is a shell widget */
  while ( !XtIsShell(temp_widget) ) {
    temp_widget = XtParent(temp_widget);
  }

  message = XmStringLtoRCreate("Enter no. of iterations", XmSTRING_DEFAULT_CHARSET);

  argcount = 0;
  XtSetArg(args[argcount], XmNselectionLabelString, message); argcount++;
  edge_hv_dialog = XmCreatePromptDialog(temp_widget, "Iterations",
					  args, argcount);

  /* Remove the help button from the dialog */
  temp_widget = XmSelectionBoxGetChild(edge_hv_dialog, XmDIALOG_HELP_BUTTON);
  XtUnmanageChild(temp_widget);

  /* Add the actions to the buttons */
  XtAddCallback(edge_hv_dialog, XmNokCallback,
		(XtCallbackProc) ok_edge_hvbutton_callback, (XtPointer) NULL);
  XtAddCallback(edge_hv_dialog, XmNcancelCallback,
		(XtCallbackProc) cancel_edge_hv_callback, (XtPointer) NULL);

  XmStringFree(message);
}

void activate_edge_hv_dialog(Widget w, XtPointer client_data,
			       XmAnyCallbackStruct *call_data) {
  if (yhs_files_open > 0 && yhs_files_open < 20 && running == 0 && yhs_filename[0] != 0){
   selection = EDGE_HV;
 
  action = SELECT;
  refresh_action();
  XtManageChild(edge_hv_dialog);}
}

void deactivate_edge_hv_dialog(void) {
  /* null - no actions at present dialog is auto unmanaged *
   * whenever any of its buttons are pressed.              */
}

static void ok_edge_hvbutton_callback(Widget w, XtPointer client_data,
			       XmSelectionBoxCallbackStruct *call_data) {
  int iterations;
  char *thresh;

  /* Get threshold value from user's selection */
  XmStringGetLtoR(call_data->value, XmSTRING_DEFAULT_CHARSET, &thresh);
   XtUnmanageChild(edge_hv_dialog);
   XFlush(XtDisplay(edge_hv_dialog));
  iterations = atoi(thresh);
  if (yhs_files_open > 0 && yhs_files_open < 20 && running == 0)
{
  if ( iterations<1 || iterations>500 )
    XBell(XtDisplay(w),100);
  else
    edge_hv(iterations);
}}

static void cancel_edge_hv_callback(Widget w, XtPointer client_data,
				   XmSelectionBoxCallbackStruct *call_data) {
  deactivate_edge_hv_dialog();
}


void edge_hv(int iterations)
{
        Arg al[10];
    int ac,s,t;
    int current;
    FILE *p_file;
int i,j,n;
    int a,b;
	unsigned long  max=0, aux[512][512], aux1[512][512];
fprintf(stderr,"\nDilating...\n");
    for (a=0;a<512;a++){for (b=0;b<512;b++) { array1[a][b]=0; array2[a][b]=0; }}

  if (yhs_files_open > 0 && yhs_files_open < 20 && running == 0 && yhs_filename[0] != 0)
{
running=1;

    if((p_file = fopen(yhs_filename1,"r")) == NULL)
    {
        fprintf(stderr, "Cannot open: %s\n",yhs_filename1);
        exit(1);
    }
    for (a=0;a<512;a++){
    for (b=0;b<512;b++)
    { array1[a][b]=getc(p_file); }}
    fclose(p_file);

for (i=0; i<IMAGE_WIDTH; i++)
	   for (j=0; j<IMAGE_HEIGHT; j++)
	     {aux1[i][j]= array1[i][j];
	      aux[i][j]= array1[i][j];
	     }

for (n=0; n<iterations; n++)
 {
     for (i=0; i<IMAGE_WIDTH; i++)
	   for (j=0; j<IMAGE_HEIGHT; j++) 
     {
         for (s= -1; s<=1; s++)
	for (t= -1; t<=1; t++) 
	 if ( (i+s)>=0 && (i+s)<IMAGE_WIDTH && (j+t)>=0 && (j+t)<IMAGE_HEIGHT )
		 if (aux[i+s][j+t] > max) max= aux[i+s][j+t];   
    
      aux1[i][j]=max;
      max=0;
   }
    for (i=1; i<IMAGE_WIDTH-1; i++)
	for (j=1; j<IMAGE_HEIGHT-1; j++)
	    aux[i][j]= aux1[i][j];
 }


for (i=0; i<IMAGE_WIDTH; i++)
	   for (j=0; j<IMAGE_HEIGHT; j++)
		array2[i][j]=aux1[i][j];

  /* Copy image into pixmap */
/*  XPutImage(XtDisplay(draw_2), thePixmap_2, image_gc_2, theXImage_2,
              0, 0, 0, 0, theXImage_2->width, theXImage_2->height); */

  /* Clear the drawing window so the image is displayed again */
       strcpy(tempfileold,tempfilenew); /* copy the new filename to the old file i.e. save the old file */
       strcat(tempfilenew,addcharac); /* new free temp file */
  if((p_file = fopen(tempfileold,"w")) == NULL)
    {
        fprintf(stderr, "Cannot open: %s - Temporary file for writing.\n",tempfileold);
        exit(1);
    }
    for (a=0;a<512;a++){
    for (b=0;b<512;b++)
    { putc(array2[a][b],p_file); }}
    fclose(p_file);
    run_once=0;
    file_loader=1;
    file_yhs=tempfileold;
/* XClearArea(XtDisplay(draw_2), XtWindow(draw_2), 0, 0, 0, 0, True); */
running=0;
}}

/*----------------------------------------------------------------------*/
/*   				Erode		   			*/
/*									*/
/*  						   			*/
/*----------------------------------------------------------------------*/

void create_edge_dilate_dialog(Widget parent) {

  XmString message;
  Widget temp_widget = parent;

  /* Ensure the parent of the dialog is a shell widget */
  while ( !XtIsShell(temp_widget) ) {
    temp_widget = XtParent(temp_widget);
  }

  message = XmStringLtoRCreate("Enter no. of iterations", XmSTRING_DEFAULT_CHARSET);

  argcount = 0;
  XtSetArg(args[argcount], XmNselectionLabelString, message); argcount++;
  edge_dilate_dialog = XmCreatePromptDialog(temp_widget, "Iterations",
					  args, argcount);

  /* Remove the help button from the dialog */
  temp_widget = XmSelectionBoxGetChild(edge_dilate_dialog, XmDIALOG_HELP_BUTTON);
  XtUnmanageChild(temp_widget);

  /* Add the actions to the buttons */
  XtAddCallback(edge_dilate_dialog, XmNokCallback,
		(XtCallbackProc) ok_edge_dilatebutton_callback, (XtPointer) NULL);
  XtAddCallback(edge_dilate_dialog, XmNcancelCallback,
		(XtCallbackProc) cancel_edge_dilate_callback, (XtPointer) NULL);
  
  XmStringFree(message);
}

void activate_edge_dilate_dialog(Widget w, XtPointer client_data,
			       XmAnyCallbackStruct *call_data) {
  if (yhs_files_open > 0 && yhs_files_open < 20 && yhs_filename[0] != 0){
   selection = EDGE_HV;
 
  action = SELECT;
  refresh_action();
  running=0;
  XtManageChild(edge_dilate_dialog);}
}

void deactivate_edge_dilate_dialog(void) {
  /* null - no actions at present dialog is auto unmanaged *
   * whenever any of its buttons are pressed.              */
}

static void ok_edge_dilatebutton_callback(Widget w, XtPointer client_data,
			       XmSelectionBoxCallbackStruct *call_data) {
  int iterations;
  char *thresh;
  if (yhs_files_open > 0 && yhs_files_open < 20 && running==0 && yhs_filename[0] != 0)
{
running=1;
  /* Get threshold value from user's selection */
  XmStringGetLtoR(call_data->value, XmSTRING_DEFAULT_CHARSET, &thresh);

  iterations = atoi(thresh);
  XtUnmanageChild(edge_dilate_dialog);
  XFlush(XtDisplay(edge_dilate_dialog));
  if ( iterations<1 || iterations>500 )
    XBell(XtDisplay(w),100);
  else
    edge_dilate(iterations);
running=0;
}}

static void cancel_edge_dilate_callback(Widget w, XtPointer client_data,
				   XmSelectionBoxCallbackStruct *call_data) {
  deactivate_edge_dilate_dialog();
}

void edge_dilate(int loop)
{
     Arg al[10];
    int ac,s,t;
    int lp;
    int current;
    FILE *p_file;
    int a,b;
	int mask[3][3], i, j;
	unsigned long aux[512][512], min=255;unsigned long aux1[512][512];
fprintf(stderr,"Eroding...\n");
    for (a=0;a<512;a++){for (b=0;b<512;b++) { array1[a][b]=0; array2[a][b]=0; }}  
running=1;
  selection = EDGE_HORIZ;
  action = SELECT;
  refresh_action();
   if((p_file = fopen(yhs_filename1,"r")) == NULL)
    {
        fprintf(stderr, "Cannot open: %s\n",yhs_filename1);
        exit(1);
    }
    for (a=0;a<512;a++){
    for (b=0;b<512;b++)
    { array1[a][b]=getc(p_file); }}
    fclose(p_file);

	for (i=0; i<512; i++)
	   for (j=0; j<512; j++)
	     {aux[i][j]= array1[i][j];
		aux1[i][j]= array1[i][j];}

for (lp=0;lp<loop+1;lp++)
{
 for (i=0; i<512; i++)
	   for (j=0; j<512; j++) 
     {
        for (s= -1; s<=1; s++)
	for (t= -1; t<=1; t++) 
	 if ( (i+s)>=0 && (i+s)<512 && (j+t)>=0 && (j+t)<512 )
		 if (aux[i+s][j+t] <min) min=aux[i+s][j+t];   
    
      aux1[i][j]=min;
      min=255;
   }
 for (i=0; i<512; i++)
	for (j=0; j<512; j++)
	    aux[i][j]= aux1[i][j];
 }
   for (a=0;a<512;a++){
    for (b=0;b<512;b++)
    { array2[a][b]=aux1[a][b]; }}

      strcpy(tempfileold,tempfilenew); /* copy the new filename to the old file i.e. save the old file */
       strcat(tempfilenew,addcharac); /* new free temp file */
  if((p_file = fopen(tempfileold,"w")) == NULL)
    {
        fprintf(stderr, "Cannot open: %s - Temporary file for writing.\n",tempfileold);
        exit(1);
    }
    for (a=0;a<512;a++){
    for (b=0;b<512;b++)
    { putc(array2[a][b],p_file); }}
    fclose(p_file);
    run_once=0;
    file_loader=1;
    file_yhs=tempfileold;
running=0;
}


/*      Apply unsharp masking to the image X    */

void edge_unsharp_callback(Widget w, XtPointer client_data,
			 XmAnyCallbackStruct *call_data)
{

	int mask[3][3];

  if (file_not_loaded) return;

  selection = EDGE_UNSHARP;
  action = SELECT;
  refresh_action();


	mask[0][0]= -1; mask[0][1]= -1; mask[0][2]= -1;
	mask[1][0] =  -1; mask[1][1] = 8; mask[1][2]= -1;
	mask[2][0]= -1; mask[2][1]= -1; mask[2][2]= -1;
	convolve (mask);
	XClearArea(XtDisplay(w), XtWindow(draw_2), 0, 0, 0, 0, True); 
}

int max4 (int a, int b, int c, int d)
{

	if (b>a) a = b;
	if (d>c) c = d;
	if (a>c) return a;
	return c;
}

int min4 (int a, int b, int c, int d)
{
	if (b < a) a = b;
	if (d < c) c = d;
	if (a < c) return a;
	return c;
}

/*      Replace each pixel by the mean of the surrounding pixels        */

void mean_filter_callback(Widget w, XtPointer client_data,
                         XmAnyCallbackStruct *call_data)
{
 
	int i,j,ii,jj,n, sum;

  if (file_not_loaded) return;

  selection = MEAN_FILTER;
  action = SELECT;
  refresh_action();



	for (i=0; i<IMAGE_WIDTH; i++)
	   for (j=0; j<IMAGE_HEIGHT; j++) {
	      n = 0; sum = 0;
	      for (ii= -1; ii<=1; ii++) 
		 for (jj = -1; jj<=1; jj++) 
		    if (range ( i+ii, j+jj)) {
			sum += XGetPixel(theXImage_1,i+ii,j+jj);
			n++;
		    }
	      if (n) XPutPixel(theXImage_2,i,j, (unsigned long) sum/n);
	      else XPutPixel(theXImage_2,i,j, 0);
	   }
	
	XPutImage(XtDisplay(draw_2), thePixmap_2, image_gc_2,theXImage_2, 0, 0, 0, 0,theXImage_2->width,theXImage_2->height);
	XClearArea(XtDisplay(w), XtWindow(draw_2), 0, 0, 0, 0, True);
}


/*      Noise reduction using the mask:   1 1 1
					  1 2 1
					  1 1 1                 */

void smooth_mask1_callback(Widget w, XtPointer client_data,
                         XmAnyCallbackStruct *call_data)
{
 
	int mask[3][3],i,j,n,m,k;


  if (file_not_loaded) return;

  selection = SMOOTH_MASK1;
  action = SELECT;
  refresh_action();



/* Initialize the mask */
	for (i=0; i<3; i++)
	   for (j=0; j<3; j++) mask[i][j] = 1;
	mask[1][1] = 2;

/* Compute the mask value at each pixel (can't be done in place) */
	for (i=1; i<IMAGE_WIDTH-1-1; i++)
	   for (j=1; j<IMAGE_HEIGHT-1-1; j++) {
		k = 0;
		for (n=0; n<3; n++)
		   for (m=0; m<3; m++)
		      k += mask[n][m]*XGetPixel(theXImage_1,i+n-1,j+m-1);
		k = k/10;
		XPutPixel(theXImage_2,i,j, (unsigned long)k);
	   }

	XPutImage(XtDisplay(draw_2), thePixmap_2, image_gc_2,theXImage_2, 0, 0, 0, 0,theXImage_2->width,theXImage_2->height);
	XClearArea(XtDisplay(w), XtWindow(draw_2), 0, 0, 0, 0, True);
}

/*      Noise reduction using the mask:    1 2 1
					   2 4 2
					   1 2 1        */

void smooth_mask2_callback(Widget w, XtPointer client_data,
			 XmAnyCallbackStruct *call_data)
{
 
	int mask[3][3],i,j,n,m,k;

  if (file_not_loaded) return;

  selection = SMOOTH_MASK2;
  action = SELECT;
  refresh_action();

/* Initialize the mask */
	for (i=0; i<3; i++)
	   for (j=0; j<3; j++) mask[i][j] = 1;
	mask[1][1] = 4;
	mask[1][0] = 2; mask[1][2] = 2; mask[0][1] = 2; mask[2][1] = 2;
	
/* Apply the mask to each pixel */
	for (i=1; i<IMAGE_WIDTH-1-1; i++)
	   for (j=1; j<IMAGE_HEIGHT-1-1; j++) {
		k = 0;
		for (n=0; n<3; n++)
		   for (m=0; m<3; m++)
		      k += mask[n][m]*XGetPixel(theXImage_1,i+n-1,j+m-1);
		k = k/16;
		XPutPixel(theXImage_2,i,j, (unsigned long)k);
	   }


	XPutImage(XtDisplay(draw_2), thePixmap_2, image_gc_2,theXImage_2, 0, 0, 0, 0,theXImage_2->width,theXImage_2->height);
	XClearArea(XtDisplay(w), XtWindow(draw_2), 0, 0, 0, 0, True);

}


void create_smooth_t_dialog(Widget parent) {

  XmString message;
  Widget temp_widget = parent;

  /* Ensure the parent of the dialog is a shell widget */
  while ( !XtIsShell(temp_widget) ) {
    temp_widget = XtParent(temp_widget);
  }

  message = XmStringLtoRCreate(SMOOTH_MESSAGE, XmSTRING_DEFAULT_CHARSET);

  argcount = 0;
  XtSetArg(args[argcount], XmNselectionLabelString, message); argcount++;
  smooth_t_dialog = XmCreatePromptDialog(temp_widget, "threshold dialog",
					  args, argcount);

  /* Remove the help button from the dialog */
  temp_widget = XmSelectionBoxGetChild(smooth_t_dialog, XmDIALOG_HELP_BUTTON);
  XtUnmanageChild(temp_widget);

  /* Add the actions to the buttons */
  XtAddCallback(smooth_t_dialog, XmNokCallback,
		(XtCallbackProc) ok_smooth_tbutton_callback, (XtPointer) NULL);
  XtAddCallback(smooth_t_dialog, XmNcancelCallback,
		(XtCallbackProc) cancel_smooth_t_callback, (XtPointer) NULL);

  XmStringFree(message);
}

void activate_smooth_t_dialog(Widget w, XtPointer client_data,
			       XmAnyCallbackStruct *call_data) {
  if (file_not_loaded) return;
  selection = SMOOTH_T;
  action = SELECT;
  refresh_action();
  XtManageChild(smooth_t_dialog);
}

void deactivate_smooth_t_dialog(void) {
  /* null - no actions at present dialog is auto unmanaged *
   * whenever any of its buttons are pressed.              */
}

static void ok_smooth_tbutton_callback(Widget w, XtPointer client_data,
			       XmSelectionBoxCallbackStruct *call_data) {
  int t;
  char *thresh;

  /* Get threshold value from user's selection */
  XmStringGetLtoR(call_data->value, XmSTRING_DEFAULT_CHARSET, &thresh);

  t = atoi(thresh);

  if ( t<0 || t>255 )
    XBell(XtDisplay(w),100);
  else
    smooth_t(t);
}

static void cancel_smooth_t_callback(Widget w, XtPointer client_data,
				   XmSelectionBoxCallbackStruct *call_data) {
  deactivate_smooth_t_dialog();
}

/*      Smooth an image using a threshold. If the difference between the
	pixel and the mean of the 3x3 region around that pixel is less
	than the threshold T then replace the pixel by the mean.        */

void smooth_t(int t)
{

int i,j,n,m,k;

  if (file_not_loaded) return;

  selection = SMOOTH_T;
  action = SELECT;
  refresh_action();


/* Compute the mean about each pixel */
	for (i=1; i<IMAGE_WIDTH-1-1; i++)
	   for (j=1; j<IMAGE_HEIGHT-1-1; j++) {
		k = 0;
		for (n=0; n<3; n++)
		   for (m=0; m<3; m++)
		      k += XGetPixel(theXImage_1,i+n-1,j+m-1);
		k = (int)( (double)k/9.0 );

/* The difference between the pixel and the mean is K */
		m = abs(XGetPixel(theXImage_1,i,j)-k);

/* Test the difference against the threshold */
		if (m<t) XPutPixel(theXImage_2,i,j, (unsigned long)k);
	   }

	XPutImage(XtDisplay(draw_2), thePixmap_2, image_gc_2,theXImage_2, 0, 0, 0, 0,theXImage_2->width,theXImage_2->height);
	XClearArea(XtDisplay(draw_2), XtWindow(draw_2), 0, 0, 0, 0, True);

}

/*      Perform a 5 or 13 point median filter, according to the parameter
	SIZE, to the image X: replace each pixel by the median of a small
	region centered at that pixel.                                  */

void median_filter_callback(Widget w, XtPointer client_data,
			 XmAnyCallbackStruct *call_data)
{

int dat[14], i,j,nn,mm,k1;
int size = 13; /* PUEDE SER 5 */

  if (file_not_loaded) return;

  selection = MEDIAN_FILTER;
  action = SELECT;
  refresh_action();

/* SIZE should be 5 or 13; if not, 5 will be assumed. */
	if (size != 5 && size != 13) size = 5;


	nn = IMAGE_WIDTH-1;
	mm = IMAGE_HEIGHT-1;

/* Examine all pixels in the source image */
	for (i=1; i<IMAGE_WIDTH-1; i++)
	   for (j=1; j<IMAGE_HEIGHT-1; j++) {
		k1=0;

/* Assemble the pixels in the region into the array DAT */
		dat[0] = XGetPixel(theXImage_1,i,j); k1 = 1;
		if ((j-1) >= 0) { dat[k1] = XGetPixel(theXImage_1,i,j-1); k1++; }
		if ((j+1) < mm) { dat[k1] = XGetPixel(theXImage_1,i,j+1); k1++; }
		if ((i-1) >= 0) { dat[k1] = XGetPixel(theXImage_1,i-1,j); k1++; }
		if ((i+1) < nn) { dat[k1] = XGetPixel(theXImage_1,i+1,j); k1++; }
		if (size == 13) {
		 if(i-2>=0) { dat[k1] = XGetPixel(theXImage_1,i-2,j); k1++; }
		 if(i+2<nn) { dat[k1] = XGetPixel(theXImage_1,i+2,j); k1++; }
		 if (j-2 >= 0) { dat[k1] = XGetPixel(theXImage_1,i,j-2); k1++; }
		 if (j+2 < mm) { dat[k1] = XGetPixel(theXImage_1,i,j+2); k1++; }
		 if ((i-1>=0) && (j-1>=0)) {dat[k1]=XGetPixel(theXImage_1,i-1,j-1); k1++; }
		 if ((i+1<nn) && (j-1>=0)) {dat[k1]=XGetPixel(theXImage_1,i+1,j-1); k1++; }
		 if ((i-1>=0) && (j+1<mm)) {dat[k1]=XGetPixel(theXImage_1,i-1,j+1); k1++; }
		 if ((i+1<nn) && (j+1<mm)) {dat[k1]=XGetPixel(theXImage_1,i+1,j+1); k1++; }
		}

/* Sort DAT */
		medsort (dat, k1);

/* Select the 'middle' value from DAT */
		k1 = (k1-1)/2;
		if (k1 < 0) k1 = 0;

		XPutPixel(theXImage_2,i,j, (unsigned long)(dat[k1]));
/* aqui en el codigo ponia unsigned char ???? */
	}
	XPutImage(XtDisplay(draw_2), thePixmap_2, image_gc_2,theXImage_2, 0, 0, 0, 0,theXImage_2->width,theXImage_2->height);
	XClearArea(XtDisplay(w), XtWindow(draw_2), 0, 0, 0, 0, True);

}

void median_vf_callback(Widget w, XtPointer client_data,
			 XmAnyCallbackStruct *call_data)
{

int dat[14], i,j,nn,mm,k1, k;
int size = 5;

  if (file_not_loaded) return;

  selection = MEDIAN_VF;
  action = SELECT;
  refresh_action();

/* SIZE should be 5 or 13; if not, 5 will be assumed. */
	if (size != 5 && size != 13) size = 5;

/* Create the temporary image */

	nn = IMAGE_WIDTH-1;
	mm = IMAGE_HEIGHT-1;

/* Examine all pixels in the source image */
	for (i=1; i<nn; i++)
	   for (j=1; j<mm; j++) {
		k1=0;
		for (k=i-size/2; k<=i+size/2; k++)
		   if (range(k,j)) 
			   dat[k1++] = XGetPixel(theXImage_1,k,j);

/* Sort DAT */
		medsort (dat, k1);

/* Select the 'middle' value from DAT */
		k1 = (k1-1)/2;
		if (k1 < 0) k1 = 0;
		XPutPixel(theXImage_2,i,j, (unsigned long)(dat[k1]));
	}

	XPutImage(XtDisplay(draw_2), thePixmap_2, image_gc_2,theXImage_2, 0, 0, 0, 0,theXImage_2->width,theXImage_2->height);
	XClearArea(XtDisplay(w), XtWindow(draw_2), 0, 0, 0, 0, True);

}

/*      Sort the integer array ARR having N elements    */

void medsort(int *arr, int n)

{
 
int i,j,a;

 

	for (j=1; j<n; j++) {
	   a = arr[j];
	   i = j - 1;
	   while (i>=0 && arr[i]>a) {
		arr[i+1] = arr[i];
		i--;
	   }
	   arr[i+1] = a;
	}
}



void create_hough_dialog(Widget parent) {

  XmString message;
  Widget temp_widget = parent;

  /* Ensure the parent of the dialog is a shell widget */
  while ( !XtIsShell(temp_widget) ) {
    temp_widget = XtParent(temp_widget);
  }

  message = XmStringLtoRCreate(THRESHOLD_MESSAGE, XmSTRING_DEFAULT_CHARSET);

  argcount = 0;
  XtSetArg(args[argcount], XmNselectionLabelString, message); argcount++;
  hough_dialog = XmCreatePromptDialog(temp_widget, "threshold dialog",
					  args, argcount);

  /* Remove the help button from the dialog */
  temp_widget = XmSelectionBoxGetChild(hough_dialog, XmDIALOG_HELP_BUTTON);
  XtUnmanageChild(temp_widget);

  /* Add the actions to the buttons */

  XtAddCallback(hough_dialog, XmNokCallback,
		(XtCallbackProc) ok_hough_callback, (XtPointer) NULL);
  XtAddCallback(hough_dialog, XmNcancelCallback,
		(XtCallbackProc) cancel_hough_callback, (XtPointer) NULL);

  XmStringFree(message);
}

void activate_hough_dialog(Widget w, XtPointer client_data,
			       XmAnyCallbackStruct *call_data) {
  if (file_not_loaded) return;
  selection = HOUGH;
  action = SELECT;
  refresh_action();
  XtManageChild(hough_dialog);
}

void deactivate_hough_dialog(void) {
  /* null - no actions at present dialog is auto unmanaged *
   * whenever any of its buttons are pressed.              */
}

static void ok_hough_callback(Widget w, XtPointer client_data,
			       XmSelectionBoxCallbackStruct *call_data) {
  int t;
  char *thresh;

  /* Get threshold value from user's selection */
  XmStringGetLtoR(call_data->value, XmSTRING_DEFAULT_CHARSET, &thresh);

  t = atoi(thresh);

  if ( t<0 || t>255 )
    XBell(XtDisplay(w),100);
  else
    hough(t);
}

static void cancel_hough_callback(Widget w, XtPointer client_data,
				   XmSelectionBoxCallbackStruct *call_data) {
  deactivate_hough_dialog();
}


/*      Compute the Hough transformation on pixels in image X having a
	value less than the threshold T. Writes the resulting image
	to the Alpha format file named 'hough.alp'.                     */

void hough(int t)
{

	int center_x, center_y, r, omega, i, j, rmax;
	double conv, rr;
	static double sarr[180], carr[180];


	conv = 3.1415926535/180.0;
	center_x = (IMAGE_HEIGHT-1)/2;       center_y = (IMAGE_WIDTH-1)/2;
	rmax = (int)(sqrt((double)((IMAGE_HEIGHT-1)*(IMAGE_HEIGHT-1) +
			  (IMAGE_WIDTH-1)*(IMAGE_WIDTH-1)))/2.0);
;

/* Allocate the Hough image: 180 degrees by twice the
   maximum possible distance found in the image X. */
	/*z = newimage (180, 2*rmax+1, error_code);*/

/* Initialize a table of sine and cosine values */

	for (omega=0; omega<180; omega++) {
		sarr[omega] = 0.5 /*sin((double)(omega*conv))*/;
		carr[omega] = 0.5 /*cos((double)(omega*conv))*/;
	}

/* Clear the Hough image */

	for (r = 0; r < 2 * rmax+1; r++)
	   for (omega = 0; omega < 180; omega++)
		XPutPixel(theXImage_2,omega,r, 0);


/* Transform each pixel in X into Hough coordinate (r,omega) if
   it has a value <= T. Increment all Hough pixels that correspond */

	for (i = 0; i < IMAGE_WIDTH-1; i++)
	   for (j = 0; j < IMAGE_HEIGHT-1; j++)
		if (XGetPixel(theXImage_1,i,j) <= threshold)
		   for (omega = 0; omega < 180; ++omega) {
			rr = (j - center_y) * sarr[omega]
			   - (i - center_x) * carr[omega];
			if (rr < 0.0) r = (int)rr;
			 else r = (int)rr + 1;
			if (XGetPixel(theXImage_2,omega,rmax+r)<255)
			XPutPixel(theXImage_2,omega,rmax+r, XGetPixel(theXImage_2,omega,rmax+r)+ 1);
		   }

	XPutImage(XtDisplay(draw_2), thePixmap_2, image_gc_2,theXImage_2, 0, 0, 0, 0,theXImage_2->width,theXImage_2->height);
	XClearArea(XtDisplay(draw_2), XtWindow(draw_2), 0, 0, 0, 0, True);

}


/* Function definition */


void histogram_callback(Widget w, XtPointer client_data,
			XmAnyCallbackStruct *call_data)
{
  int x,y,i,j;
  double offset;
  long min, max;
  long hist[256], k;
  XPoint points[3];

  if (file_not_loaded) return;

  selection = HISTOGRAM;
  action = SELECT;
  refresh_action();

  /* Fill in the image with background color */
  for(i=0; i<IMAGE_WIDTH; i++)
    for(j=0; j<IMAGE_HEIGHT; j++)
	 XPutPixel(theXImage_2,i,j,bg);

  /* Copy image into pixmap */
  XPutImage(XtDisplay(w), thePixmap_2, image_gc_2, theXImage_2,
	    0, 0, 0, 0, theXImage_2->width, theXImage_2->height);

  /* Construct a grey-level histogram of the image theXImage_1 */
  for (i=0; i<256; i++)
    hist[i] = 0L;

  for (i=0; i<IMAGE_WIDTH; i++)
    for (j=0; j<IMAGE_HEIGHT; j++) {
      k = XGetPixel(theXImage_1,i,j);
      hist[k] += 1;
    }

  /* Calculate minimun and maximun values of the histogram */
  min = 262144L;
  max = 0L;
  for (i=0; i<256; i++) {
    if (hist[i] > max) max = hist[i];
    if (hist[i] < min) min = hist[i];
  }

  /* Draw frame into pixmap */
  XDrawRectangle(XtDisplay(w),thePixmap_2,image_gc_2,29,29,451,451);

  /* Draw axis into pixmap */
  points[0].x = 129;     points[0].y = 63;
  points[1].x = 129;     points[1].y = 63+400;
  points[2].x = 129+255; points[2].y = 63+400;
  XDrawLines(XtDisplay(w),thePixmap_2,image_gc_2,points,3,CoordModeOrigin);

  /* Draw histogram values into pixmap */
  for (i=0; i<256; i++) {
    x = 129+i;
    offset = (double) ((0-400)/(double)(max-min))*(double)(hist[i]-min)+400;
    y = 63 + (int) offset;
    XDrawLine(XtDisplay(w),thePixmap_2,image_gc_2,x,y,x,63+399);
  }

  /* Copy pixmap into image */
  theXImage_2 = XGetImage(XtDisplay(w), thePixmap_2, 0, 0,
			  theXImage_2->width, theXImage_2->height,
			  AllPlanes, ZPixmap);

  /* Clear the drawing window so the image is displayed again */
  XClearArea(XtDisplay(w),XtWindow(draw_2),0,0,0,0,True);
}

/* Histogram equalize the image X. */
void histo_eq_callback(Widget w, XtPointer client_data,
		       XmAnyCallbackStruct *call_data)
{

  /* Histogram equalization */
  int i,j,k;
  unsigned long histo[256]; /* Histogram of the image */
  unsigned long cum[256];   /* Cumulative sum of h    */
  unsigned long ideal[256]; /* Ideal cumulative sum   */
  unsigned long map[256];   /* Old to new level map   */

  if (file_not_loaded) return;

  selection = HISTO_EQ;
  action = SELECT;
  refresh_action();

  /* Construct a grey-level histogram of theXImage_1 */
  for (i=0; i<256; i++)
    histo[i] = 0L;

  for (i=0; i<IMAGE_WIDTH; i++)
    for (j=0; j<IMAGE_HEIGHT; j++) {
      k = XGetPixel(theXImage_1,i,j);
      histo[k] += 1;
    }

  /* Generate the cumulative sum */
  cum[0] = histo[0];
  for (i=1; i<256; i++)
    cum[i] = cum[i-1] + histo[i];

  for (i=0; i<256; i++) map[i] = i;
  ideal[0] = (IMAGE_WIDTH*IMAGE_HEIGHT)/256;
  for (i=1; i<256; i++) ideal[i] = ideal[i-1] + ideal[0];

  j=0; i=0;
  while (j<256) {
    while (cum[i] < ideal[j]) {
      i++;
      map[i] = j;
    }
    j++;
  }

  for (i=0; i<IMAGE_WIDTH; i++)
    for (j=0; j<IMAGE_HEIGHT; j++) {
       k = XGetPixel(theXImage_1,i,j);
       XPutPixel(theXImage_2,i,j,map[k]);
    }

  /* Copy image into pixmap */
  XPutImage(XtDisplay(w), thePixmap_2, image_gc_2, theXImage_2,
	    0, 0, 0, 0, theXImage_2->width, theXImage_2->height);

  /* Clear the drawing window so the image is displayed again */
  XClearArea(XtDisplay(w),XtWindow(draw_2),0,0,0,0,True);
}

void create_threshold_dialog(Widget parent) {

  XmString message;
  Widget temp_widget = parent;

  /* Ensure the parent of the dialog is a shell widget */
  while ( !XtIsShell(temp_widget) ) {
    temp_widget = XtParent(temp_widget);
  }

  message = XmStringLtoRCreate(THRESHOLD_MESSAGE, XmSTRING_DEFAULT_CHARSET);

  argcount = 0;
  XtSetArg(args[argcount], XmNselectionLabelString, message); argcount++;
  threshold_dialog = XmCreatePromptDialog(temp_widget, "threshold dialog",
					  args, argcount);

  /* Remove the help button from the dialog */
  temp_widget = XmSelectionBoxGetChild(threshold_dialog, XmDIALOG_HELP_BUTTON);
  XtUnmanageChild(temp_widget);

  /* Add the actions to the buttons */
  XtAddCallback(threshold_dialog, XmNokCallback,
		(XtCallbackProc) ok_threshbutton_callback, (XtPointer) NULL);
  XtAddCallback(threshold_dialog, XmNcancelCallback,
		(XtCallbackProc) cancel_threshbutton_callback, (XtPointer) NULL);

  XmStringFree(message);
}

void activate_threshold_dialog(Widget w, XtPointer client_data,
			       XmAnyCallbackStruct *call_data) {
  if (file_not_loaded) return;
  selection = THRESHOLD;
  action = SELECT;
  refresh_action();
  XtManageChild(threshold_dialog);
}

void deactivate_threshold_dialog(void) {
  /* null - no actions at present dialog is auto unmanaged *
   * whenever any of its buttons are pressed.              */
}

static void ok_threshbutton_callback(Widget w, XtPointer client_data,
			       XmSelectionBoxCallbackStruct *call_data) {
  int t;
  char *thresh;

  /* Get threshold value from user's selection */
  XmStringGetLtoR(call_data->value, XmSTRING_DEFAULT_CHARSET, &thresh);

  t = atoi(thresh);

  if ( t<0 || t>255 )
    XBell(XtDisplay(w),100);
  else
    thresholding(t);
}

static void cancel_threshbutton_callback(Widget w, XtPointer client_data,
				   XmSelectionBoxCallbackStruct *call_data) {
  deactivate_threshold_dialog();
}

/* Threshold the image 1. Any pixels with a level less than T will be set *
 * to background color; others will be set to foreground color.           */
static void thresholding(int t)
{
  int i,j;

  for (i=0; i<IMAGE_WIDTH; i++)
    for (j=0; j<IMAGE_HEIGHT; j++)
      if (XGetPixel(theXImage_1,i,j) < (unsigned long) t)
	XPutPixel(theXImage_2,i,j,bg);
      else
	XPutPixel(theXImage_2,i,j,fg);

  /* Copy image into pixmap */
  XPutImage(XtDisplay(draw_2), thePixmap_2, image_gc_2, theXImage_2,
	    0, 0, 0, 0, theXImage_2->width, theXImage_2->height);

  /* Clear the drawing window so the image is displayed again */
  XClearArea(XtDisplay(draw_2),XtWindow(draw_2),0,0,0,0,True);
}

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us