about summary refs log tree commit diff
path: root/analyzer/pgmtexture.c
blob: 938f9ef8383eafe60ce029b05e392ba31b9ff454 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
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
/* pgmtexture.c - calculate textural features of a PGM image
**


*/

#include <assert.h>
#include <math.h>

#include "pm_c_util.h"
#include "mallocvar.h"
#include "shhopt.h"
#include "pgm.h"


struct CmdlineInfo {
    /* All the information the user supplied in the command line,
     * in a form easy for the program to use.
     */
    const char * inputFileName;  /* Filespec of input file */
    unsigned int d;
};



static void
parseCommandLine(int argc, const char ** const argv,
                 struct CmdlineInfo * const cmdlineP) {
/*----------------------------------------------------------------------------
   Note that the file spec array we return is stored in the storage that
   was passed to us as the argv array.
-----------------------------------------------------------------------------*/
    optEntry * option_def;
    optStruct3 opt;
    unsigned int option_def_index;

    unsigned int dSpec;

    MALLOCARRAY_NOFAIL(option_def, 100);

    option_def_index = 0;   /* incremented by OPTENT3 */
    OPTENT3(0,   "d",          OPT_UINT,   &cmdlineP->d,  &dSpec,      0);

    opt.opt_table = option_def;
    opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
    opt.allowNegNum = TRUE;  /* We may have parms that are negative numbers */

    pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0);
        /* Uses and sets argc, argv, and some of *cmdlineP and others. */

    if (!dSpec)
        cmdlineP->d = 1;

    if (argc-1 < 1)
        cmdlineP->inputFileName = "-";
    else if (argc-1 == 1)
        cmdlineP->inputFileName = argv[1];
    else
        pm_error("Program takes at most 1 parameter: the file name.  "
                 "You specified %u", argc-1);
}



#define RADIX 2.0
#define EPSILON 0.000000001
#define BL  "Angle                 "

#define SWAP(a,b) do {float const y=(a);(a)=(b);(b)=y;} while (0)



static float
sign(float const x,
     float const y) {

    return y < 0 ? -fabs(x) : fabs(x);
}



static float *
vector(unsigned int const nl,
       unsigned int const nh) {
/*----------------------------------------------------------------------------
  Allocate a float vector with range [nl..nh]

  We do some seedy C here, subtracting an arbitrary integer from a pointer and
  calling the result a pointer.  It normally works because the only way we'll
  use that pointer is by adding that same integer or something greater to it.

  The point of this is not to allocate memory for vector elements that will
  never be referenced (component < nl).
-----------------------------------------------------------------------------*/
    float * v;
    unsigned int i;

    assert(nh >= nl); assert(nh <= UINT_MAX-1);

    MALLOCARRAY(v, (unsigned) (nh - nl + 1));

    if (v == NULL)
        pm_error("Unable to allocate memory for a vector.");

    for (i = 0; i < nh - nl +1; ++i)
        v[i] = 0;
    return v - nl;
}



static float **
matrix (unsigned int const nrl,
        unsigned int const nrh,
        unsigned int const ncl,
        unsigned int const nch) {
/*----------------------------------------------------------------------------
  Allocate a float matrix with range [nrl..nrh][ncl..nch]

  Return value is a pointer to array of pointers to rows
-----------------------------------------------------------------------------*/
    /* We do some seedy C here, subtracting an arbitrary integer from a
       pointer and calling the result a pointer.  It normally works because
       the only way we'll use that pointer is by adding that same integer or
       something greater to it.

       The point of this is not to allocate memory for matrix elements that
       will never be referenced (row < nrl or column < ncl).
    */

    unsigned int i;
    float ** matrix;  /* What we are creating */

    assert(nrh >= nrl); assert(nrh <= UINT_MAX-1);

    /* allocate pointers to rows */
    MALLOCARRAY(matrix, (unsigned) (nrh - nrl + 1));
    if (matrix == NULL)
        pm_error("Unable to allocate memory for a matrix.");

    matrix -= ncl;

    assert (nch >= ncl); assert(nch <= UINT_MAX-1);

    /* allocate rows and set pointers to them */
    for (i = nrl; i <= nrh; ++i) {
        MALLOCARRAY(matrix[i], (unsigned) (nch - ncl + 1));
        if (!matrix[i])
            pm_error("Unable to allocate memory for a matrix row.");
        matrix[i] -= ncl;
    }

    return matrix;
}



static void
printHeader() {

    fprintf(stdout,
            "%-22.22s %10.10s %10.10s %10.10s %10.10s %10.10s\n",
            "Angle", "0", "45", "90", "135", "Avg");
}



static void
printResults(const char *  const name,
             const float * const a) {

    unsigned int i;

    fprintf(stdout, "%-22.22s ", name);

    for (i = 0; i < 4; ++i)
        fprintf(stdout, "% 1.3e ", a[i]);

    fprintf(stdout, "% 1.3e\n", (a[0] + a[1] + a[2] + a[3]) / 4);
}



static void
makeGrayToneSpatialDependenceMatrix(gray **        const grays,
                                    unsigned int   const rows,
                                    unsigned int   const cols,
                                    unsigned int   const d,
                                    unsigned int * const tone,
                                    unsigned int   const toneCt,
                                    float *** const pmatrix0P,
                                    float *** const pmatrix45P,
                                    float *** const pmatrix90P,
                                    float *** const pmatrix135P) {

    float ** pmatrix0, ** pmatrix45, ** pmatrix90, ** pmatrix135;
    unsigned int row;

    pm_message("Computing spatial dependence matrix...");

    /* Allocate memory */
    pmatrix0   = matrix(0, toneCt, 0, toneCt);
    pmatrix45  = matrix(0, toneCt, 0, toneCt);
    pmatrix90  = matrix(0, toneCt, 0, toneCt);
    pmatrix135 = matrix(0, toneCt, 0, toneCt);

    for (row = 0; row < toneCt; ++row) {
        unsigned int col;
        for (col = 0; col < toneCt; ++col) {
            pmatrix0 [row][col] = pmatrix45 [row][col] = 0;
            pmatrix90[row][col] = pmatrix135[row][col] = 0;
        }
    }
    for (row = 0; row < rows; ++row) {
        unsigned int col;
        for (col = 0; col < cols; ++col) {
            unsigned int angle;
            unsigned int x;
            for (angle = 0, x = 0; angle <= 135; angle += 45) {
                while (tone[x] != grays[row][col])
                    ++x;
                if (angle == 0 && col + d < cols) {
                    unsigned int y;
                    y = 0;
                    while (tone[y] != grays[row][col + d])
                        ++y;
                    ++pmatrix0[x][y];
                    ++pmatrix0[y][x];
                }
                if (angle == 90 && row + d < rows) {
                    unsigned int y;
                    y = 0;
                    while (tone[y] != grays[row + d][col])
                        ++y;
                    ++pmatrix90[x][y];
                    ++pmatrix90[y][x];
                }
                if (angle == 45 && row + d < rows && col >= d) {
                    unsigned int y;
                    y = 0;
                    while (tone[y] != grays[row + d][col - d])
                        ++y;
                    ++pmatrix45[x][y];
                    ++pmatrix45[y][x];
                }
                if (angle == 135 && row + d < rows && col + d < cols) {
                    unsigned int y;
                    y = 0;
                    while (tone[y] != grays[row + d][col + d])
                        ++y;
                    ++pmatrix135[x][y];
                    ++pmatrix135[y][x];
                }
            }
        }
    }
    /* Gray-tone spatial dependence matrices are complete */

    {
    /* Find normalizing constants */
    unsigned int const r0  = 2 * rows * (cols - d);
    unsigned int const r45 = 2 * (rows - d) * (cols - d);
    unsigned int const r90 = 2 * (rows - d) * cols;

    unsigned int i;

    /* Normalize gray-tone spatial dependence matrix */
    for (i = 0; i < toneCt; ++i) {
        unsigned int j;
        for (j = 0; j < toneCt; ++j) {
            pmatrix0  [i][j] /= r0;
            pmatrix45 [i][j] /= r45;
            pmatrix90 [i][j] /= r90;
            pmatrix135[i][j] /= r45;
        }
    }
    }
    pm_message(" ...done.");

    *pmatrix0P   = pmatrix0;
    *pmatrix45P  = pmatrix45;
    *pmatrix90P  = pmatrix90;
    *pmatrix135P = pmatrix135;
}



static void
mkbalanced (float **     const a,
            unsigned int const n) {

    float const sqrdx = SQR(RADIX);

    unsigned int last, i;
    float s, r, g, f, c;

    last = 0;
    while (last == 0) {
        last = 1;
        for (i = 1; i <= n; ++i) {
            unsigned int j;
            r = c = 0.0;
            for (j = 1; j <= n; ++j) {
                if (j != i) {
                    c += fabs (a[j][i]);
                    r += fabs (a[i][j]);
                }
            }
            if (c && r) {
                g = r / RADIX;
                f = 1.0;
                s = c + r;
                while (c < g) {
                    f *= RADIX;
                    c *= sqrdx;
                }
                g = r * RADIX;
                while (c > g) {
                    f /= RADIX;
                    c /= sqrdx;
                }
                if ((c + r) / f < 0.95 * s) {
                    unsigned int j;
                    last = 0;
                    g = 1.0 / f;
                    for (j = 1; j <= n; ++j)
                        a[i][j] *= g;
                    for (j = 1; j <= n; ++j)
                        a[j][i] *= f;
                }
            }
        }
    }
}



static void
reduction(float **     const a,
          unsigned int const n) {

    unsigned int m;

    for (m = 2; m < n; ++m) {
        unsigned int j;
        unsigned int i;
        float x;
        x = 0.0;
        i = m;
        for (j = m; j <= n; ++j) {
            if (fabs(a[j][m - 1]) > fabs(x)) {
                x = a[j][m - 1];
                i = j;
            }
        }
        if (i != m) {
            for (j = m - 1; j <= n; ++j)
                SWAP(a[i][j], a[m][j]);
            for (j = 1; j <= n; j++)
                SWAP(a[j][i], a[j][m]);
            a[j][i] = a[j][i];
        }
        if (x != 0.0) {
            unsigned int i;
            for (i = m + 1; i <= n; ++i) {
                float y;
                y = a[i][m - 1];
                if (y) {
                    y /= x;
                    a[i][m - 1] = y;
                    for (j = m; j <= n; ++j)
                        a[i][j] -= y * a[m][j];
                    for (j = 1; j <= n; ++j)
                        a[j][m] += y * a[j][i];
                }
            }
        }
    }
}



static float
norm(float **     const a,
     unsigned int const n) {

    float anorm;
    unsigned int i;

    for (i = 2, anorm = fabs(a[1][1]); i <= n; ++i) {
        unsigned int j;
        for (j = (i - 1); j <= n; ++j)
            anorm += fabs(a[i][j]);
    }
    return anorm;
}



static void
hessenberg(float **     const a,
           unsigned int const n,
           float *      const wr,
           float *      const wi) {

    float const anorm = norm(a, n);

    int nn;
    float t;

    assert(n >= 1);

    for (nn = n, t = 0.0; nn >= 1; ) {
        unsigned int its;
        int l;
        its = 0;
        do {
            float x;
            for (l = nn; l >= 2; --l) {
                float s;
                s = fabs (a[l - 1][l - 1]) + fabs (a[l][l]);
                if (s == 0.0)
                    s = anorm;
                if ((float) (fabs (a[l][l - 1]) + s) == s)
                    break;
            }
            assert(nn >= 1);
            x = a[nn][nn];
            if (l == nn) {
                wr[nn] = x + t;
                wi[nn--] = 0.0;
            } else {
                float w, y;
                y = a[nn - 1][nn - 1];  /* initial value */
                w = a[nn][nn - 1] * a[nn - 1][nn];  /* initial value */
                if (l == (nn - 1)) {
                    float const p = 0.5 * (y - x);
                    float const q = p * p + w;
                    float const z = sqrt(fabs(q));
                    x += t;
                    if (q >= 0.0) {
                        float const z2 = p + sign(z, p);
                        wr[nn - 1] = wr[nn] = x + z2;
                        if (z2)
                            wr[nn] = x - w / z2;
                        wi[nn - 1] = wi[nn] = 0.0;
                    } else {
                        wr[nn - 1] = wr[nn] = x + p;
                        wi[nn - 1] = -(wi[nn] = z);
                    }
                    nn -= 2;
                } else {
                    int i, k, m;
                    float p, q, r;
                    if (its == 30)
                        pm_error("Too many iterations to required "
                                 "to find max correlation coefficient");
                    if (its == 10 || its == 20) {
                        int i;
                        float s;
                        t += x;
                        for (i = 1; i <= nn; ++i)
                            a[i][i] -= x;
                        s = fabs(a[nn][nn - 1]) + fabs(a[nn - 1][nn - 2]);
                        y = x = 0.75 * s;
                        w = -0.4375 * s * s;
                    }
                    ++its;
                    for (m = (nn - 2); m >= l; --m) {
                        float const z = a[m][m];
                        float s, u, v;
                        r = x - z;
                        s = y - z;
                        p = (r * s - w) / a[m + 1][m] + a[m][m + 1];
                        q = a[m + 1][m + 1] - z - r - s;
                        r = a[m + 2][m + 1];
                        s = fabs(p) + fabs(q) + fabs(r);
                        p /= s;
                        q /= s;
                        r /= s;
                        if (m == l)
                            break;
                        u = fabs(a[m][m - 1]) * (fabs(q) + fabs(r));
                        v = fabs(p) * (fabs(a[m - 1][m - 1]) + fabs(z) +
                                       fabs(a[m + 1][m + 1]));
                        if (u + v == v)
                            break;
                    }
                    for (i = m + 2; i <= nn; ++i) {
                        a[i][i - 2] = 0.0;
                        if (i != (m + 2))
                            a[i][i - 3] = 0.0;
                    }
                    for (k = m; k <= nn - 1; ++k) {
                        float s;
                        if (k != m) {
                            p = a[k][k - 1];
                            q = a[k + 1][k - 1];
                            r = 0.0;
                            if (k != (nn - 1))
                                r = a[k + 2][k - 1];
                            if ((x = fabs(p) + fabs(q) + fabs(r))) {
                                p /= x;
                                q /= x;
                                r /= x;
                            }
                        }
                        s = sign(sqrt(SQR(p) + SQR(q) + SQR(r)), p);
                        if (s) {
                            int const mmin = nn < k + 3 ? nn : k + 3;
                            float z;
                            int j;
                            if (k == m) {
                                if (l != m)
                                    a[k][k - 1] = -a[k][k - 1];
                            } else
                                a[k][k - 1] = -s * x;
                            p += s;
                            x = p / s;
                            y = q / s;
                            z = r / s;
                            q /= p;
                            r /= p;
                            for (j = k; j <= nn; ++j) {
                                p = a[k][j] + q * a[k + 1][j];
                                if (k != (nn - 1)) {
                                    p += r * a[k + 2][j];
                                    a[k + 2][j] -= p * z;
                                }
                                a[k + 1][j] -= p * y;
                                a[k][j] -= p * x;
                            }
                            for (i = l; i <= mmin; ++i) {
                                p = x * a[i][k] + y * a[i][k + 1];
                                if (k != (nn - 1)) {
                                    p += z * a[i][k + 2];
                                    a[i][k + 2] -= p * r;
                                }
                                a[i][k + 1] -= p * q;
                                a[i][k] -= p;
                            }
                        }
                    }
                }
            }
        } while (l < nn - 1);
    }
}



static float
f1_a2m(float **     const p,
       unsigned int const ng) {
/*----------------------------------------------------------------------------
  Angular Second Moment

  The angular second-moment feature (ASM) f1 is a measure of homogeneity of
  the image. In a homogeneous image, there are very few dominant gray-tone
  transitions. Hence the P matrix for such an image will have fewer entries of
  large magnitude.
-----------------------------------------------------------------------------*/
    unsigned int i;
    float sum;

    for (i = 0, sum = 0.0; i < ng; ++i) {
        unsigned int j;
        for (j = 0; j < ng; ++j)
            sum += p[i][j] * p[i][j];
    }
    return sum;
}



static float
f2_contrast(float **     const p,
            unsigned int const ng) {
/*----------------------------------------------------------------------------
   Contrast

   The contrast feature is a difference moment of the P matrix and is a
   measure of the contrast or the amount of local variations present in an
   image.
-----------------------------------------------------------------------------*/
    unsigned int n;
    float bigsum;

    for (n = 0, bigsum = 0.0; n < ng; ++n) {
        unsigned int i;
        float sum;
        for (i = 0, sum = 0.0; i < ng; ++i) {
            unsigned int j;
            for (j = 0; j < ng; ++j) {
                if ((i - j) == n || (j - i) == n)
                    sum += p[i][j];
            }
        }
        bigsum += SQR(n) * sum;
    }
    return bigsum;
}



static float
f3_corr(float **     const p,
        unsigned int const ng) {
/*----------------------------------------------------------------------------
   Correlation

   This correlation feature is a measure of gray-tone linear-dependencies in
   the image.
-----------------------------------------------------------------------------*/
    unsigned int i;
    float sumSqrx;
    float tmp;
    float * px;
    float meanx, meany, stddevx, stddevy;

    sumSqrx = 0.0;
    meanx = 0.0; meany = 0.0;

    px = vector(0, ng);
    for (i = 0; i < ng; ++i)
        px[i] = 0;

    /* px[i] is the (i-1)th entry in the marginal probability matrix obtained
       by summing the rows of p[i][j]
    */
    for (i = 0; i < ng; ++i) {
        unsigned int j;
        for (j = 0; j < ng; ++j)
            px[i] += p[i][j];
    }

    /* Now calculate the means and standard deviations of px and py */
    for (i = 0; i < ng; ++i) {
        meanx += px[i] * i;
        sumSqrx += px[i] * SQR(i);
    }

    meany = meanx;
    stddevx = sqrt(sumSqrx - (SQR(meanx)));
    stddevy = stddevx;

    /* Finally, the correlation ... */
    for (i = 0, tmp = 0; i < ng; ++i) {
        unsigned int j;
        for (j = 0; j < ng; ++j)
            tmp += i * j * p[i][j];
    }
    return (tmp - meanx * meany) / (stddevx * stddevy);
}



static float
f4_var (float **     const p,
        unsigned int const ng) {
/*----------------------------------------------------------------------------
  Sum of Squares: Variance
-----------------------------------------------------------------------------*/
    unsigned int i;
    float mean, var;

    for (i = 0, mean = 0.0; i < ng; ++i) {
        unsigned int j;
        for (j = 0; j < ng; ++j)
            mean += i * p[i][j];
    }
    for (i = 0, var = 0.0; i < ng; ++i) {
        unsigned int j;
        for (j = 0; j < ng; ++j)
            var += (i + 1 - mean) * (i + 1 - mean) * p[i][j];
    }
    return var;
}



static float
f5_idm (float **     const p,
        unsigned int const ng) {
/*----------------------------------------------------------------------------
  Inverse Difference Moment
-----------------------------------------------------------------------------*/
    unsigned int i;
    float idm;

    for (i = 0, idm = 0.0; i < ng; ++i) {
        unsigned int j;
        for (j = 0; j < ng; ++j)
            idm += p[i][j] / (1 + (i - j) * (i - j));
    }
    return idm;
}


static double *
newPxpy2(unsigned int const ng) {

    double * pxpy;

    if (ng > UINT_MAX-1)
        pm_error("Too many gray levels (%u) to do computations", ng);

    MALLOCARRAY(pxpy, ng+1);

    if (!pxpy)
        pm_error("Unable to allocate %u entries for the pxpy table",
                 ng+1);

    return pxpy;
}



static float *
newPxpy(unsigned int const ng) {

    float * pxpy;

    if (ng > (UINT_MAX-1)/2 -1)
        pm_error("Too many gray levels (%u) to do computations", ng);

    MALLOCARRAY(pxpy, 2 * (ng+1) + 1);

    if (!pxpy)
        pm_error("Unable to allocate %u entries for the pxpy table",
                 2* (ng+1) + 1);

    return pxpy;
}



static float
f6_savg (float **     const p,
         unsigned int const ng) {
/*----------------------------------------------------------------------------
   Sum Average
-----------------------------------------------------------------------------*/
    float * const pxpy = newPxpy(ng);

    unsigned int i;
    float savg;

    for (i = 0; i <= 2 * ng; ++i)
        pxpy[i] = 0.0;

    for (i = 0; i < ng; ++i) {
        unsigned int j;
        for (j = 0; j < ng; ++j)
            pxpy[i + j + 2] += p[i][j];
    }
    for (i = 2, savg = 0.0; i <= 2 * ng; ++i)
        savg += i * pxpy[i];

    free(pxpy);

    return savg;
}



static float
f7_svar (float **     const p,
         unsigned int const ng,
         float        const s) {
/*----------------------------------------------------------------------------
   Sum Variance
-----------------------------------------------------------------------------*/
    float * const pxpy = newPxpy(ng);

    unsigned int i;
    float var;

    for (i = 0; i <= 2 * ng; ++i)
        pxpy[i] = 0;

    for (i = 0; i < ng; ++i) {
        unsigned int j;
        for (j = 0; j < ng; ++j)
            pxpy[i + j + 2] += p[i][j];
    }
    for (i = 2, var = 0.0; i <= 2 * ng; ++i)
        var += (i - s) * (i - s) * pxpy[i];

    free(pxpy);

    return var;
}



static float
f8_sentropy (float **     const p,
             unsigned int const ng) {
/*----------------------------------------------------------------------------
   Sum Entropy
-----------------------------------------------------------------------------*/
    float * const pxpy = newPxpy(ng);

    unsigned int i;
    float sentropy;

    for (i = 0; i <= 2 * ng; ++i)
        pxpy[i] = 0;

    for (i = 0; i < ng; ++i) {
        unsigned int j;
        for (j = 0; j < ng; ++j)
            pxpy[i + j + 2] += p[i][j];
    }
    for (i = 2, sentropy = 0.0; i <= 2 * ng; ++i)
        sentropy -= pxpy[i] * log10(pxpy[i] + EPSILON);

    free(pxpy);

    return sentropy;
}



static float
f9_entropy (float **     const p,
            unsigned int const ng) {
/*----------------------------------------------------------------------------
   Entropy
-----------------------------------------------------------------------------*/
    unsigned int i;
    float entropy;

    for (i = 0, entropy = 0.0; i < ng; ++i) {
        unsigned int j;
        for (j = 0; j < ng; ++j)
            entropy += p[i][j] * log10(p[i][j] + EPSILON);
    }
    return -entropy;
}



static float
f10_dvar(float **     const p,
         unsigned int const ng) {
/*----------------------------------------------------------------------------
   Difference Variance
-----------------------------------------------------------------------------*/
    double * const pxpy = newPxpy2(ng);

    unsigned int i;
    double sqrNg;  /* Square of 'ng' */
    double sum;
    double sumSqr;
    double var;

    for (i = 0; i < ng; ++i)
        pxpy[i] = 0;

    for (i = 0; i < ng; ++i) {
        unsigned int j;
        for (j = 0; j < ng; ++j)
            pxpy[abs((int)i - (int)j)] += p[i][j];
    }
    /* Now calculate the variance of Pxpy (Px-y) */
    for (i = 0, sum = 0.0, sumSqr = 0.0; i < ng; ++i) {
        sum += pxpy[i];
        sumSqr += SQR(pxpy[i]);
    }
    sqrNg = SQR(ng);
    var = (sqrNg * sumSqr - SQR(sum)) / SQR(sqrNg);

    free(pxpy);

    return var;
}



static float
f11_dentropy (float **     const p,
              unsigned int const ng) {
/*----------------------------------------------------------------------------
   Difference Entropy
-----------------------------------------------------------------------------*/
    float * const pxpy = newPxpy(ng);

    unsigned int i;
    float sum;

    for (i = 0; i <= 2 * ng; ++i)
        pxpy[i] = 0;

    for (i = 0; i < ng; ++i) {
        unsigned int j;
        for (j = 0; j < ng; ++j)
            pxpy[abs((int)i - (int)j)] += p[i][j];
    }
    for (i = 0, sum = 0.0; i < ng; ++i)
        sum += pxpy[i] * log10(pxpy[i] + EPSILON);

    free(pxpy);

    return -sum;
}



static float
f12_icorr (float **     const p,
           unsigned int const ng) {
/*----------------------------------------------------------------------------
  Information Measures of Correlation
-----------------------------------------------------------------------------*/
    unsigned int i;
    float * px;
    float * py;
    float hx, hy, hxy, hxy1, hxy2;

    px = vector(0, ng);
    py = vector(0, ng);

    /*
     * px[i] is the (i-1)th entry in the marginal probability matrix obtained
     * by summing the rows of p[i][j]
     */
    for (i = 0; i < ng; ++i) {
        unsigned int j;
        for (j = 0; j < ng; ++j) {
            px[i] += p[i][j];
            py[j] += p[i][j];
        }
    }

    hx = 0.0; hy = 0.0; hxy = 0.0; hxy1 = 0.0; hxy2 = 0.0;

    for (i = 0; i < ng; ++i) {
        unsigned int j;
        for (j = 0; j < ng; ++j) {
            hxy1 -= p[i][j] * log10(px[i] * py[j] + EPSILON);
            hxy2 -= px[i] * py[j] * log10(px[i] * py[j] + EPSILON);
            hxy  -= p[i][j] * log10 (p[i][j] + EPSILON);
        }
    }
    /* Calculate entropies of px and py - is this right? */
    for (i = 0; i < ng; ++i) {
        hx -= px[i] * log10(px[i] + EPSILON);
        hy -= py[i] * log10(py[i] + EPSILON);
    }
    return (hxy - hxy1) / (hx > hy ? hx : hy);
}



static float
f13_icorr (float **     const p,
           unsigned int const ng) {
/*----------------------------------------------------------------------------
  Information Measures of Correlation
-----------------------------------------------------------------------------*/
    unsigned int i;
    float * px;
    float * py;
    float hx, hy, hxy, hxy1, hxy2;

    px = vector(0, ng);
    py = vector(0, ng);

    /*
     * px[i] is the (i-1)th entry in the marginal probability matrix obtained
     * by summing the rows of p[i][j]
     */
    for (i = 0; i < ng; ++i) {
        unsigned int j;
        for (j = 0; j < ng; ++j) {
            px[i] += p[i][j];
            py[j] += p[i][j];
        }
    }

    hx = 0.0; hy = 0.0; hxy = 0.0; hxy1 = 0.0; hxy2 = 0.0;

    for (i = 0; i < ng; ++i) {
        unsigned int j;
        for (j = 0; j < ng; ++j) {
            hxy1 -= p[i][j] * log10(px[i] * py[j] + EPSILON);
            hxy2 -= px[i] * py[j] * log10(px[i] * py[j] + EPSILON);
            hxy  -= p[i][j] * log10(p[i][j] + EPSILON);
        }
    }
    /* Calculate entropies of px and py */
    for (i = 0; i < ng; ++i) {
        hx -= px[i] * log10 (px[i] + EPSILON);
        hy -= py[i] * log10 (py[i] + EPSILON);
    }
    return sqrt(fabs(1 - exp (-2.0 * (hxy2 - hxy))));
}



static float
f14_maxcorr (float **     const p,
             unsigned int const ng) {
/*----------------------------------------------------------------------------
  The Maximal Correlation Coefficient
-----------------------------------------------------------------------------*/
    unsigned int i;
    float *px, *py;
    float ** q;
    float * x;
    float * iy;
    float tmp;

    px = vector(0, ng);
    py = vector(0, ng);
    q = matrix(1, ng + 1, 1, ng + 1);
    x = vector(1, ng);
    iy = vector(1, ng);

    /*
     * px[i] is the (i-1)th entry in the marginal probability matrix obtained
     * by summing the rows of p[i][j]
     */
    for (i = 0; i < ng; ++i) {
        unsigned int j;
        for (j = 0; j < ng; ++j) {
            px[i] += p[i][j];
            py[j] += p[i][j];
        }
    }

    /* Compute the Q matrix */
    for (i = 0; i < ng; ++i) {
        unsigned int j;
        for (j = 0; j < ng; ++j) {
            unsigned int k;
            q[i + 1][j + 1] = 0;
            for (k = 0; k < ng; ++k)
                q[i + 1][j + 1] += p[i][k] * p[j][k] / px[i] / py[k];
        }
    }

    /* Balance the matrix */
    mkbalanced(q, ng);
    /* Reduction to Hessenberg Form */
    reduction(q, ng);
    /* Finding eigenvalue for nonsymetric matrix using QR algorithm */
    hessenberg(q, ng, x, iy);

    /* Return the sqrt of the second largest eigenvalue of q */
    for (i = 2, tmp = x[1]; i <= ng; ++i)
        tmp = (tmp > x[i]) ? tmp : x[i];

    return sqrt(x[ng - 1]);
}



static void
printAngularSecondMom(float **     const pmatrix0,
                      float **     const pmatrix45,
                      float **     const pmatrix90,
                      float **     const pmatrix135,
                      unsigned int const toneCt) {

    float res[4];

    res[0] = f1_a2m(pmatrix0,   toneCt);
    res[1] = f1_a2m(pmatrix45,  toneCt);
    res[2] = f1_a2m(pmatrix90,  toneCt);
    res[3] = f1_a2m(pmatrix135, toneCt);

    printResults("Angular Second Moment", res);
}



static void
printContrast(float **     const pmatrix0,
              float **     const pmatrix45,
              float **     const pmatrix90,
              float **     const pmatrix135,
              unsigned int const toneCt) {

    float res[4];

    res[0] = f2_contrast(pmatrix0,   toneCt);
    res[1] = f2_contrast(pmatrix45,  toneCt);
    res[2] = f2_contrast(pmatrix90,  toneCt);
    res[3] = f2_contrast(pmatrix135, toneCt);

    printResults("Contrast", res);
}



static void
printCorrelation(float **     const pmatrix0,
                 float **     const pmatrix45,
                 float **     const pmatrix90,
                 float **     const pmatrix135,
                 unsigned int const toneCt) {

    float res[4];

    res[0] = f3_corr(pmatrix0,   toneCt);
    res[1] = f3_corr(pmatrix45,  toneCt);
    res[2] = f3_corr(pmatrix90,  toneCt);
    res[3] = f3_corr(pmatrix135, toneCt);

    printResults("Correlation", res);
}



static void
printVariance(float **     const pmatrix0,
              float **     const pmatrix45,
              float **     const pmatrix90,
              float **     const pmatrix135,
              unsigned int const toneCt) {

    float res[4];

    res[0] = f4_var(pmatrix0,   toneCt);
    res[1] = f4_var(pmatrix45,  toneCt);
    res[2] = f4_var(pmatrix90,  toneCt);
    res[3] = f4_var(pmatrix135, toneCt);

    printResults("Variance", res);
}



static void
printInverseDiffMoment(float **     const pmatrix0,
                       float **     const pmatrix45,
                       float **     const pmatrix90,
                       float **     const pmatrix135,
                       unsigned int const toneCt) {

    float res[4];

    res[0] = f5_idm(pmatrix0,   toneCt);
    res[1] = f5_idm(pmatrix45,  toneCt);
    res[2] = f5_idm(pmatrix90,  toneCt);
    res[3] = f5_idm(pmatrix135, toneCt);

    printResults("Inverse Diff Moment", res);
}



static void
printSumAverage(float **     const pmatrix0,
                float **     const pmatrix45,
                float **     const pmatrix90,
                float **     const pmatrix135,
                unsigned int const toneCt) {

    float res[4];

    res[0] = f6_savg(pmatrix0,  toneCt);
    res[1] = f6_savg(pmatrix45,  toneCt);
    res[2] = f6_savg(pmatrix90,  toneCt);
    res[3] = f6_savg(pmatrix135, toneCt);

    printResults("Sum Average", res);
}



static void
printSumVariance(float **     const pmatrix0,
                 float **     const pmatrix45,
                 float **     const pmatrix90,
                 float **     const pmatrix135,
                 unsigned int const toneCt) {

    float res[4];
    float savg[4];

    savg[0] = f6_savg(pmatrix0,   toneCt);
    savg[1] = f6_savg(pmatrix45,  toneCt);
    savg[2] = f6_savg(pmatrix90,  toneCt);
    savg[3] = f6_savg(pmatrix135, toneCt);

    res[0] = f7_svar(pmatrix0,   toneCt, savg[0]);
    res[1] = f7_svar(pmatrix45,  toneCt, savg[1]);
    res[2] = f7_svar(pmatrix90,  toneCt, savg[2]);
    res[3] = f7_svar(pmatrix135, toneCt, savg[3]);

    printResults("Sum Variance", res);
}



static void
printSumVarianceEnt(float **     const pmatrix0,
                    float **     const pmatrix45,
                    float **     const pmatrix90,
                    float **     const pmatrix135,
                    unsigned int const toneCt) {

    float res[4];

    res[0] = f8_sentropy(pmatrix0,   toneCt);
    res[1] = f8_sentropy(pmatrix45,  toneCt);
    res[2] = f8_sentropy(pmatrix90,  toneCt);
    res[3] = f8_sentropy(pmatrix135, toneCt);

    printResults("Sum Entropy", res);
}



static void
printEntropy(float **     const pmatrix0,
             float **     const pmatrix45,
             float **     const pmatrix90,
             float **     const pmatrix135,
             unsigned int const toneCt) {

    float res[4];


    res[0] = f9_entropy(pmatrix0,   toneCt);
    res[1] = f9_entropy(pmatrix45,  toneCt);
    res[2] = f9_entropy(pmatrix90,  toneCt);
    res[3] = f9_entropy(pmatrix135, toneCt);

    printResults("Entropy", res);
}



static void
printDiffVariance(float **     const pmatrix0,
                  float **     const pmatrix45,
                  float **     const pmatrix90,
                  float **     const pmatrix135,
                  unsigned int const toneCt) {

    float res[4];

    res[0] = f10_dvar(pmatrix0,   toneCt);
    res[1] = f10_dvar(pmatrix45,  toneCt);
    res[2] = f10_dvar(pmatrix90,  toneCt);
    res[3] = f10_dvar(pmatrix135, toneCt);

    printResults("Difference Variance", res);
}



static void
printDiffEntropy(float **     const pmatrix0,
                 float **     const pmatrix45,
                 float **     const pmatrix90,
                 float **     const pmatrix135,
                 unsigned int const toneCt) {

    float res[4];

    res[0] = f11_dentropy(pmatrix0,   toneCt);
    res[1] = f11_dentropy(pmatrix45,  toneCt);
    res[2] = f11_dentropy(pmatrix90,  toneCt);
    res[3] = f11_dentropy(pmatrix135, toneCt);

    printResults ("Difference Entropy", res);
}



static void
printCorrelation1(float **     const pmatrix0,
                  float **     const pmatrix45,
                  float **     const pmatrix90,
                  float **     const pmatrix135,
                  unsigned int const toneCt) {

    float res[4];

    res[0] = f12_icorr(pmatrix0,   toneCt);
    res[1] = f12_icorr(pmatrix45,  toneCt);
    res[2] = f12_icorr(pmatrix90,  toneCt);
    res[3] = f12_icorr(pmatrix135, toneCt);

    printResults("Meas of Correlation-1", res);
}



static void
printCorrelation2(float **     const pmatrix0,
                  float **     const pmatrix45,
                  float **     const pmatrix90,
                  float **     const pmatrix135,
                  unsigned int const toneCt) {

    float res[4];

    res[0] = f13_icorr(pmatrix0,   toneCt);
    res[1] = f13_icorr(pmatrix45,  toneCt);
    res[2] = f13_icorr(pmatrix90,  toneCt);
    res[3] = f13_icorr(pmatrix135, toneCt);

    printResults("Meas of Correlation2", res);
}



static void
printCorrelationMax(float **     const pmatrix0,
                    float **     const pmatrix45,
                    float **     const pmatrix90,
                    float **     const pmatrix135,
                    unsigned int const toneCt) {

    float res[4];

    res[0] = f14_maxcorr(pmatrix0,   toneCt);
    res[1] = f14_maxcorr(pmatrix45,  toneCt);
    res[2] = f14_maxcorr(pmatrix90,  toneCt);
    res[3] = f14_maxcorr(pmatrix135, toneCt);

    printResults("Max Correlation Coeff", res);
}



int
main (int argc, const char ** argv) {

    struct CmdlineInfo cmdline;
    FILE * ifP;
    gray ** grays;
    unsigned int * tone;  /* malloced array */
    unsigned int toneCt;
    unsigned int row;
    int rows, cols;
    unsigned int itone;
    float ** pmatrix0, ** pmatrix45, ** pmatrix90, ** pmatrix135;
    gray maxval;
    unsigned int i;

    pm_proginit(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFileName);

    grays = pgm_readpgm(ifP, &cols, &rows, &maxval);

    MALLOCARRAY(tone, maxval+1);

    /* Determine the number of different gray scales (not maxval) */
    for (i = 0; i <= maxval; ++i)
        tone[i] = -1;
    for (row = 0; row < rows; ++row) {
        unsigned int col;
        for (col = 0; col < cols; ++col)
            tone[grays[row][col]] = grays[row][col];
    }
    for (i = 0, toneCt = 0; i <= maxval; ++i) {
        if (tone[i] != -1)
            ++toneCt;
    }
    pm_message("(Image has %u gray levels.)", toneCt);

    /* Collapse array, taking out all zero values */
    for (row = 0, itone = 0; row <= maxval; ++row)
        if (tone[row] != -1)
            tone[itone++] = tone[row];
    /* Now array contains only the gray levels present (in ascending order) */

    if (cmdline.d > cols)
        pm_error("Image is narrower (%u columns) "
                 "than specified distance (%u)", cols, cmdline.d);

    makeGrayToneSpatialDependenceMatrix(
        grays, rows, cols, cmdline.d, tone, toneCt,
        &pmatrix0, &pmatrix45, &pmatrix90, &pmatrix135);

    pm_message("Computing textural features ...");

    fprintf(stdout, "\n");

    printHeader();

    printAngularSecondMom (pmatrix0, pmatrix45, pmatrix90, pmatrix135, toneCt);

    printContrast         (pmatrix0, pmatrix45, pmatrix90, pmatrix135, toneCt);

    printCorrelation      (pmatrix0, pmatrix45, pmatrix90, pmatrix135, toneCt);

    printVariance         (pmatrix0, pmatrix45, pmatrix90, pmatrix135, toneCt);

    printInverseDiffMoment(pmatrix0, pmatrix45, pmatrix90, pmatrix135, toneCt);

    printSumAverage       (pmatrix0, pmatrix45, pmatrix90, pmatrix135, toneCt);

    printSumVariance      (pmatrix0, pmatrix45, pmatrix90, pmatrix135, toneCt);

    printSumVarianceEnt   (pmatrix0, pmatrix45, pmatrix90, pmatrix135, toneCt);

    printEntropy          (pmatrix0, pmatrix45, pmatrix90, pmatrix135, toneCt);

    printDiffVariance     (pmatrix0, pmatrix45, pmatrix90, pmatrix135, toneCt);

    printDiffEntropy      (pmatrix0, pmatrix45, pmatrix90, pmatrix135, toneCt);

    printCorrelation1     (pmatrix0, pmatrix45, pmatrix90, pmatrix135, toneCt);

    printCorrelation2     (pmatrix0, pmatrix45, pmatrix90, pmatrix135, toneCt);

    printCorrelationMax   (pmatrix0, pmatrix45, pmatrix90, pmatrix135, toneCt);

    pm_message(" ...done.");

    pm_close (ifP);

    return 0;
}


/*
** Author: James Darrell McCauley
**         Texas Agricultural Experiment Station
**         Department of Agricultural Engineering
**         Texas A&M University
**         College Station, Texas 77843-2117 USA
**
** Code written partially taken from pgmtofs.c in the PBMPLUS package
** by Jef Poskanzer.
**
** Algorithms for calculating features (and some explanatory comments) are
** taken from:
**
**   Haralick, R.M., K. Shanmugam, and I. Dinstein. 1973. Textural features
**   for image classification.  IEEE Transactions on Systems, Man, and
**   Cybertinetics, SMC-3(6):610-621.
**
** Copyright (C) 1991 Texas Agricultural Experiment Station, employer for
** hire of James Darrell McCauley
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation.  This software is provided "as is" without express or
** implied warranty.
**
** THE TEXAS AGRICULTURAL EXPERIMENT STATION (TAES) AND THE TEXAS A&M
** UNIVERSITY SYSTEM (TAMUS) MAKE NO EXPRESS OR IMPLIED WARRANTIES
** (INCLUDING BY WAY OF EXAMPLE, MERCHANTABILITY) WITH RESPECT TO ANY
** ITEM, AND SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL
** OR CONSEQUENTAL DAMAGES ARISING OUT OF THE POSSESSION OR USE OF
** ANY SUCH ITEM. LICENSEE AND/OR USER AGREES TO INDEMNIFY AND HOLD
** TAES AND TAMUS HARMLESS FROM ANY CLAIMS ARISING OUT OF THE USE OR
** POSSESSION OF SUCH ITEMS.
**
** Modification History:
** 24 Jun 91 - J. Michael Carstensen <jmc@imsor.dth.dk> supplied fix for
**             correlation function.
**
** 05 Oct 05 - Marc Breithecker <Marc.Breithecker@informatik.uni-erlangen.de>
**             Fix calculation or normalizing constants for d > 1.
** 9 Jul 11  - Francois P. S. Luus <fpsluus@gmail.com> supplied fix for sum
**             variance calculation (use F6:savg instead of F8:sentropy in
**             F7:svar equation).
*/