File: iprocess.c

package info (click to toggle)
xpaint 2.9.1.4-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 5,552 kB
  • sloc: ansic: 73,017; sh: 492; yacc: 247; lex: 126; sed: 43; makefile: 11
file content (2481 lines) | stat: -rw-r--r-- 61,414 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
/* +-------------------------------------------------------------------+ */
/* | Copyright 1993, David Koblas (koblas@netcom.com)		       | */
/* | Copyright 1995, 1996 Torsten Martinsen (bullestock@dk-online.dk)  | */
/* |								       | */
/* | Permission to use, copy, modify, and to distribute this software  | */
/* | and its documentation for any purpose is hereby granted without   | */
/* | fee, provided that the above copyright notice appear in all       | */
/* | copies and that both that copyright notice and this permission    | */
/* | notice appear in supporting documentation.	 There is no	       | */
/* | representations about the suitability of this software for	       | */
/* | any purpose.  this software is provided "as is" without express   | */
/* | or implied warranty.					       | */
/* |								       | */
/* +-------------------------------------------------------------------+ */

/* $Id: iprocess.c,v 1.18 2005/03/20 20:15:32 demailly Exp $ */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <dlfcn.h>

#include <X11/Intrinsic.h>
#include <X11/Shell.h>
#include <X11/StringDefs.h>
#include <X11/Xatom.h>

#include "xaw_incdir/AsciiText.h"
#include "xaw_incdir/Command.h"
#include "xaw_incdir/Form.h"
#include "xaw_incdir/Paned.h"

#include "Paint.h"
#include "PaintP.h"
#include "xpaint.h"
#include "image.h"
#include "menu.h"
#include "messages.h"
#include "misc.h"
#include "protocol.h"
#include "graphic.h"

#ifdef XAW3D
#define BORDERWIDTH 1
#else
#define BORDERWIDTH 0
#endif

typedef struct {
    Widget shell, pane, name, program, cclog;
    int mode;
    char *scriptfile;
} LocalInfo;

#define BUFSIZE 65536

void *dl_filter = NULL;
void *dl_proc = NULL;
void *dl_image = NULL;

/*
**  Some real image processing
 */

extern int file_isSpecialImage;
extern int file_transparent;

extern struct imageprocessinfo ImgProcessInfo;
extern FILE * openTempFile(char **np);
extern void removeTempFile(void);

#define CLAMP(low, value, high) \
		if (value < low) value = low; else if (value > high) value = high
#define DEG2RAD	(M_PI/180.0)

#define ConvMatrixSize	3
typedef float ConvMatrix[ConvMatrixSize * ConvMatrixSize];

static int *histogram(Image * input);
static LocalInfo *head;

static PaintMenuItem fileMenu[] =
{
#define FILE_LOAD 0
    MI_SIMPLE("load"),
#define FILE_SAVE 1
    MI_SIMPLE("save"),
#define FILE_SAVEAS 2
    MI_SIMPLE("saveas"),
#define FILE_EDIT 3
    MI_SIMPLE("editor"),
#define FILE_CLOSE 4
    MI_SIMPLE("close"),
};
static PaintMenuItem predefMenu[] =
{
#define PREDEF_FILTERS 0
    MI_SIMPLE("filters"),
#define PREDEF_IMAGES 1
    MI_SIMPLE("images"),
#define PREDEF_3D_CURVES 2
    MI_SIMPLE("3d_curves"),
#define PREDEF_3D_SURFACES 3
    MI_SIMPLE("3d_surfaces"),
#define PREDEF_LAYERS 4
    MI_SIMPLE("layers"),
#define PREDEF_PROCEDURES 5
    MI_SIMPLE("procedures"),
#define PREDEF_BATCH 6
    MI_SIMPLE("batch"),
#define PREDEF_SEPARATOR 7
    MI_SEPARATOR(),
#define PREDEF_HELP 8
    MI_SIMPLECB("help", HelpDialog, "script_editor"),
};

static PaintMenuBar printMenuBar[] =
{
    {None, "script_files", XtNumber(fileMenu), fileMenu},
    {None, "script_predef", XtNumber(predefMenu), predefMenu},
};

static Image *
convolve(Image * input, float *mat, int n,
	 unsigned char *basePixel, Boolean absFlag)
{
    int x, y, xx, yy, xv, yv;
    float sum;
    unsigned char *p;
    unsigned char *op;
    float r, g, b;
    int ir, ig, ib;
    Image *output;

    const int xy = -n/2;
    const int ye = input->height + xy;
    const int xe = input->width + xy;
    const int hh = input->height + input->height;
    const int ww = input->width + input->width;

    output = ImageNew(input->width, input->height);
    op = output->data;

    sum = 0;
    for (yy = 0; yy < n; yy++) {
	for (xx = 0; xx < n; xx++) {
	    sum += mat[xx + n * yy];
	}
    }
    if (sum <= 0)
	sum = 1;

    for (y = xy; y < ye; y++) {
	for (x = xy; x < xe; x++) {
	    r = g = b = 0;
	    for (yy = 0; yy < n; yy++) {
		for (xx = 0; xx < n; xx++) {
		    xv = x + xx;
		    yv = y + yy;
		    if (xv < 0)
			xv = -xv;
		    if (yv < 0) 
			yv = -yv;
		    if (xv >= input->width) {
			xv = xv % ww;
			if (xv >= input->width)
			    xv = ww - xv - 1;
		    }
		    if (yv >= input->height) {
			yv = yv % hh;
			if (yv >= input->height)
			    yv = hh - yv - 1;
		    }
		    p = ImagePixel(input, xv, yv);
		    r += (float) *p++ * mat[xx + n * yy];
		    g += (float) *p++ * mat[xx + n * yy];
		    b += (float) *p * mat[xx + n * yy];
		}
	    }
	    if (absFlag) {
		if (r < 0)
		    r = -r;
		if (g < 0)
		    g = -g;
		if (b < 0)
		    b = -b;
	    }
	    ir = r / sum;
	    ig = g / sum;
	    ib = b / sum;
	    if (basePixel) {
		ir += basePixel[0];
		ig += basePixel[1];
		ib += basePixel[2];
	    }
	    CLAMP(0, ir, 255);
	    CLAMP(0, ig, 255);
	    CLAMP(0, ib, 255);

	    *op++ = ir;
	    *op++ = ig;
	    *op++ = ib;
	}

	if (y % 16 == 0)
	    StateTimeStep();
    }

    return output;
}

/*
**  rescale values from 0..255
 */
static void 
normalize(Image * image)
{
    int i, count;
    unsigned char *sp;
    char *ip;
    int maxval = 0;

    if (image->cmapSize != 0) {
	sp = image->cmapData;
	count = image->cmapSize * 3;
    } else {
	sp = image->data;
	count = image->width * image->height * 3;
    }

    for (ip = (char*) sp, i = 0; i < count; i++, ip++)
	if (*ip > maxval)
	    maxval = *ip;
    if (maxval == 0)
	return;
    for (ip = (char*) sp, i = 0; i < count; i++, ip++)
	*ip = ((int) *ip * 255) / maxval;
}

/*
**  Convert image into a monochrome image
 */
static void 
monochrome(Image * image)
{
    int i, count;
    unsigned char *sp;
    unsigned char *ip;
    int v;

    if (image->cmapSize != 0) {
	sp = image->cmapData;
	count = image->cmapSize;
    } else {
	sp = image->data;
	count = image->width * image->height;
    }

    for (ip = sp, i = 0; i < count; i++) {
	v = (ip[0] * 11 + ip[1] * 16 + ip[2] * 5) >> 5;		/* pp = .33R+.5G+.17B */
	*ip++ = v;
	*ip++ = v;
	*ip++ = v;
    }
}


Image *
ImageSmooth(Image * input)
{
    float *mat;
    int n, i;
    Image *output;

    /*
     * Build n x n convolution matrix (all 1's)
     */
    n = ImgProcessInfo.smoothMaskSize;
    mat = xmalloc(n * n * sizeof(float));
    for (i = 0; i < n * n; ++i)
	mat[i] = 1.0;

    output = convolve(input, mat, n, NULL, False);
    free(mat);
    return output;
}

Image *
ImageSharpen(Image * input)
{
    static ConvMatrix mat =
    {
	-1, -2, -1,
	-2, 20, -2,
	-1, -2, -1
    };
    return convolve(input, mat, ConvMatrixSize, NULL, False);
}

Image *
ImageEdge(Image * input)
{
    static ConvMatrix mat =
    {
	-1, -2, 0,
	-2, 0, 2,
	0, 2, 1
    };
    Image *image = convolve(input, mat, ConvMatrixSize, NULL, True);

    normalize(image);

    return image;
}

Image *
ImageEmboss(Image * input)
{
    static ConvMatrix mat =
    {
	-1, -2, 0,
	-2, 0, 2,
	0, 2, 1
    };
    static unsigned char base[3] =
    {128, 128, 128};
    Image *image = convolve(input, mat, ConvMatrixSize, base, False);

    monochrome(image);
    normalize(image);

    return image;
}

Image *
ImageInvert(Image * input)
{
    Image *output;
    unsigned char *op;
    int x,y;
    unsigned char *p;

    output = ImageNew(input->width, input->height);
    op = output->data;

    for (y = 0; y < input->height; y++)
    {
	for (x = 0; x < input->width; x++)
	{
	    p = ImagePixel(input, x, y);
	    *op++ = 255 - *p++;
	    *op++ = 255 - *p++;
	    *op++ = 255 - *p;
	}
    }

#if 0
    /*
    **	 If the input has a colormap, just invert that.
     */
    if (input->scale == 1 && input->cmapSize != 0) {
	output = ImageNewCmap(input->width, input->height, input->cmapSize);
	ip = input->cmapData;
	op = output->cmapData;
	count = input->cmapSize;

	memcpy(output->data, input->data,
	     sizeof(char) * input->scale * input->width * input->height);
    } else {
	output = ImageNew(input->width, input->height);
	ip = input->data;
	op = output->data;
	count = input->width * input->height;
    }

    for (i = 0; i < count; i++) {
	*op++ = 255 - *ip++;
	*op++ = 255 - *ip++;
	*op++ = 255 - *ip++;
    }
#endif

    return output;
}

Image *
ImageOilPaint(Image * input)
{
    Image *output = ImageNew(input->width, input->height);
    unsigned char *op = output->data;
    int x, y, xx, yy, i;
    int rVal, gVal, bVal;
    int rCnt, gCnt, bCnt;
    int rHist[256], gHist[256], bHist[256];
    int oilArea_2;


    oilArea_2 = ImgProcessInfo.oilArea / 2;

    for (y = 0; y < input->height; y++) {
	for (x = 0; x < input->width; x++) {
	    /*
	    **	compute histogram of (on-screen hunk of) n*n 
	    **	  region centered plane
	     */

	    rCnt = gCnt = bCnt = 0;
	    rVal = gVal = bVal = 0;
	    for (i = 0; i < XtNumber(rHist); i++)
		rHist[i] = gHist[i] = bHist[i] = 0;

	    for (yy = y - oilArea_2; yy < y + oilArea_2; yy++) {
		if (yy < 0 || yy >= input->height)
		    continue;
		for (xx = x - oilArea_2; xx < x + oilArea_2; xx++) {
		    int c, p;
		    unsigned char *rgb;

		    if (xx < 0 || xx >= input->width)
			continue;

		    rgb = ImagePixel(input, xx, yy);

		    if ((c = ++rHist[(p = rgb[0]) / 4]) > rCnt) {
			rVal = p;
			rCnt = c;
		    }
		    if ((c = ++gHist[(p = rgb[1]) / 4]) > gCnt) {
			gVal = p;
			gCnt = c;
		    }
		    if ((c = ++bHist[(p = rgb[2]) / 4]) > bCnt) {
			bVal = p;
			bCnt = c;
		    }
		}
	    }

	    *op++ = rVal;
	    *op++ = gVal;
	    *op++ = bVal;
	}

	if (y % 16 == 0)
	    StateTimeStep();
    }

    return output;
}


/*
 * Add random noise to an image.
 */
Image *
ImageAddNoise(Image * input)
{
    unsigned char *p;
    int x, y, r, g, b, ddelta;
    Image *output = ImageNew(input->width, input->height);
    unsigned char *op = output->data;


    ddelta = ImgProcessInfo.noiseDelta * 2;

    for (y = 0; y < input->height; y++) {
	for (x = 0; x < input->width; x++) {
	    p = ImagePixel(input, x, y);
	    r = p[0] + ddelta * gauss();
	    CLAMP(0, r, 255);
	    g = p[1] + ddelta * gauss();
	    CLAMP(0, g, 255);
	    b = p[2] + ddelta * gauss();
	    CLAMP(0, b, 255);
	    *op++ = r;
	    *op++ = g;
	    *op++ = b;
	}
    }
    return output;
}

/*
 * This function works in-place.
 * Because it swaps pixels in the input image, which may be colour mapped
 * or not, is has knowledge about the Image format that should probably
 * have stayed in image.h. Too bad.
 */
Image *
ImageSpread(Image * input)
{
    int w = input->width, h = input->height;
    unsigned char *p, *np;
    int x, y, minx, miny, maxx, maxy, xn, yn, dist;


    dist = ImgProcessInfo.spreadDistance;
    for (y = 0; y < h; y++) {
	for (x = 0; x < w; x++) {
	    p = ImagePixel(input, x, y);
	    /* find random neighbour pixel within +- dist */
	    minx = x - dist;
	    if (minx < 0)
		minx = 0;
	    maxx = x + dist;
	    if (maxx >= w)
		maxx = w - 1;
	    xn = RANDOMI2(minx, maxx);

	    miny = y - dist;
	    if (miny < 0)
		miny = 0;
	    maxy = y + dist;
	    if (maxy >= h)
		maxy = h - 1;
	    yn = RANDOMI2(miny, maxy);
	    /* swap pixel with neighbour */
	    np = ImagePixel(input, xn, yn);
	    if (input->cmapSize == 0) {
		unsigned char rn, gn, bn;

		rn = np[0];
		gn = np[1];
		bn = np[2];
		np[0] = p[0];
		np[1] = p[1];
		np[2] = p[2];
		p[0] = rn;
		p[1] = gn;
		p[2] = bn;
	    } else if (input->cmapSize > 256) {
		unsigned short i, *ps, *nps;

		ps = &((unsigned short *) input->data)[y * w + x];
		nps = &((unsigned short *) input->data)[yn * w + xn];
		i = *nps;
		*nps = *ps;
		*ps = i;
	    } else {		/* colour map of 256 or less */
		unsigned char i;

		p = &input->data[y * w + x];
		np = &input->data[yn * w + xn];
		i = *np;
		*np = *p;
		*p = i;
	    }
	}
    }

    return input;
}

Image *
ImageBlend(Image * input)
{
    int w = input->width, h = input->height;
    Image *output = ImageNew(w, h);
    unsigned char *op = output->data;
    unsigned char *rgb;
    int x, y, n, ox, oy, px, py, dx, dy;
    int rSum, gSum, bSum, r, g, b;
    float diagonal, gradient, ap;


    ox = w / 2;
    oy = h / 2;
    diagonal = ((double) h) / w;

    /*
     * Compute average of pixels on edge of region. While we are
     * at it, we copy the edge to the output as well.
     */
    rSum = gSum = bSum = 0;
    for (x = 0; x < w; ++x) {
	rgb = ImagePixel(input, x, 0);
	r = rgb[0];
	g = rgb[1];
	b = rgb[2];
	rSum += r;
	gSum += g;
	bSum += b;
	*op++ = r;
	*op++ = g;
	*op++ = b;
    }
    op = ImagePixel(output, 0, h - 1);
    for (x = 0; x < w; ++x) {
	rgb = ImagePixel(input, x, h - 1);
	r = rgb[0];
	g = rgb[1];
	b = rgb[2];
	rSum += r;
	gSum += g;
	bSum += b;
	*op++ = r;
	*op++ = g;
	*op++ = b;
    }
    for (y = 1; y < h - 1; ++y) {
	rgb = ImagePixel(input, 0, y);
	r = rgb[0];
	g = rgb[1];
	b = rgb[2];
	rSum += r;
	gSum += g;
	bSum += b;
	op = ImagePixel(output, 0, y);
	*op++ = r;
	*op++ = g;
	*op++ = b;

	rgb = ImagePixel(input, w - 1, y);
	r = rgb[0];
	g = rgb[1];
	b = rgb[2];
	rSum += r;
	gSum += g;
	bSum += b;
	op = ImagePixel(output, w - 1, y);
	*op++ = r;
	*op++ = g;
	*op++ = b;
    }

    n = 2 * (w + h - 2);	/* total # of pixels on edge */
    rSum /= n;
    gSum /= n;
    bSum /= n;
    op = ImagePixel(output, 1, 1);

    /* compute colour of each point in the interior */
    for (y = 1; y < h - 1; y++) {
	for (x = 1; x < w - 1; x++) {
	    dx = x - ox;
	    dy = y - oy;
	    if (dx == 0 && dy == 0) {	/* this is the centre point */
		r = rSum;
		g = gSum;
		b = bSum;
	    } else {
		if (dx == 0) {	/* special case 1 */
		    px = ox;
		    py = (dy > 0) ? h - 1 : 0;
		} else if (dy == 0) {	/* special case 2 */
		    py = oy;
		    px = (dx > 0) ? w - 1 : 0;
		} else {	/* general case */
		    gradient = ((double) dy) / dx;
		    if (fabs(gradient) < fabs(diagonal)) {
			px = (dx > 0) ? w - 1 : 0;
			py = oy + ((px - ox) * dy) / dx;
		    } else {
			py = (dy > 0) ? h - 1 : 0;
			px = ox + ((py - oy) * dx) / dy;
		    }
		}

		/*
		 * given O(ox,oy), P(px,py), and A(x,y), compute
		 *   |AO|/|PO|
		 */
		ap = sqrt((double) (dx * dx) + (double) (dy * dy)) /
		    sqrt((double) ((px - ox) * (px - ox)) +
			 (double) ((py - oy) * (py - oy)));

		rgb = ImagePixel(input, px, py);
		r = (1 - ap) * rSum + ap * rgb[0];
		g = (1 - ap) * gSum + ap * rgb[1];
		b = (1 - ap) * bSum + ap * rgb[2];
	    }
	    *op++ = r;
	    *op++ = g;
	    *op++ = b;
	}
	op += 6;		/* skip last on this line and first on next */
    }

    return output;
}

Image *
ImagePixelize(Image * input)
{
    int width = input->width, height = input->height;
    Image *output = ImageNew(width, height);
    unsigned char *op, *rgb;
    int x, y, xx, yy, n;
    int rSum, gSum, bSum;
    int xsize, ysize;


    xsize = ImgProcessInfo.pixelizeXSize;
    ysize = ImgProcessInfo.pixelizeYSize;

    if (xsize > width)
	xsize = width;
    if (ysize > height)
	ysize = height;
    n = xsize * ysize;

    for (y = 0; y < height; y += ysize) {
	for (x = 0; x < width; x += xsize) {
	    /*
	     *	compute average of pixels inside megapixel
	     */
	    rSum = gSum = bSum = 0;
	    for (yy = y; yy < y + ysize; yy++) {
		if (yy >= height)
		    continue;
		for (xx = x; xx < x + xsize; xx++) {
		    if (xx >= width)
			continue;
		    rgb = ImagePixel(input, xx, yy);
		    rSum += rgb[0];
		    gSum += rgb[1];
		    bSum += rgb[2];
		}
	    }
	    rSum /= n;
	    gSum /= n;
	    bSum /= n;
	    /*
	     * replace each pixel in megapixel with average
	     */
	    for (yy = y; yy < y + ysize; yy++) {
		if (yy >= height)
		    continue;
		for (xx = x; xx < x + xsize; xx++) {
		    if (xx >= width)
			continue;
		    op = ImagePixel(output, xx, yy);
		    *op++ = rSum;
		    *op++ = gSum;
		    *op = bSum;
		}
	    }
	}

	if (y % 16 == 0)
	    StateTimeStep();
    }

    /* if any incomplete megapixels are left, copy them to the output */
    for (x = (width / xsize) * xsize; x < width; ++x)
	for (y = 0; y < height; ++y) {
	    rgb = ImagePixel(input, x, y);
	    op = ImagePixel(output, x, y);
	    *op++ = *rgb++;
	    *op++ = *rgb++;
	    *op = *rgb;
	}
    for (y = (height / ysize) * ysize; y < height; ++y)
	for (x = 0; x < width; ++x) {
	    rgb = ImagePixel(input, x, y);
	    op = ImagePixel(output, x, y);
	    *op++ = *rgb++;
	    *op++ = *rgb++;
	    *op = *rgb;
	}

    return output;
}


#define SWAP(a, b)	{ int t = (a); (a) = (b); (b) = t; }

Image *
ImageDespeckle(Image * input)
{
    int width = input->width, height = input->height;
    Image *output = ImageNew(width, height);
    unsigned char *op, *rgb;
    int x, y, xx, yy, mask, mask2, i, j, k, l;
    int *ra, *ga, *ba;

    mask = ImgProcessInfo.despeckleMask;
    if (mask > width)
	mask = width;
    if (mask > height)
	mask = height;
    mask2 = mask / 2;

    /* arrays for storing pixels inside mask */
    ra = xmalloc(mask * mask * sizeof(int));
    ga = xmalloc(mask * mask * sizeof(int));
    ba = xmalloc(mask * mask * sizeof(int));

    op = output->data;
    for (y = 0; y < height; ++y) {
	for (x = 0; x < width; ++x) {
	    i = 0;
	    for (yy = MAX(0, y - mask2); yy < MIN(height, y + mask2); ++yy)
		for (xx = MAX(0, x - mask2); xx < MIN(width, x + mask2); ++xx) {
		    rgb = ImagePixel(input, xx, yy);
		    ra[i] = *rgb++;
		    ga[i] = *rgb++;
		    ba[i] = *rgb;
		    ++i;
		}
	    /*
	     * now find median by (shell-)sorting the arrays and
	     * picking the center value
	     */
	    for (j = i / 2; j > 0; j = j / 2)
		for (k = j; k < i; k++) {
		    for (l = k - j; l >= 0 && ra[l] > ra[l + j]; l -= j)
			SWAP(ra[l], ra[l + j]);
		    for (l = k - j; l >= 0 && ga[l] > ga[l + j]; l -= j)
			SWAP(ga[l], ga[l + j]);
		    for (l = k - j; l >= 0 && ba[l] > ba[l + j]; l -= j)
			SWAP(ba[l], ba[l + j]);
		}
	    if (i & 1) {	/* uneven number of data points */
		*op++ = ra[i / 2];
		*op++ = ga[i / 2];
		*op++ = ba[i / 2];
	    } else {		/* even, take average */
		*op++ = (ra[i / 2 - 1] + ra[i / 2]) / 2;
		*op++ = (ga[i / 2 - 1] + ga[i / 2]) / 2;
		*op++ = (ba[i / 2 - 1] + ba[i / 2]) / 2;
	    }
	}
	if (y % 16 == 0)
	    StateTimeStep();
    }

    free(ra);
    free(ga);
    free(ba);

    return output;
}


/*
 * Normalize the contrast of an image.
 */
Image *
ImageNormContrast(Image * input)
{
    unsigned char *p;
    int *hist;
    int x, y, w, h, i, max = 0, cumsum, limit;
    Image *output = ImageNew(input->width, input->height);
    unsigned char *op = output->data;
    int blackpcnt, blackval, whitepcnt, whiteval;


    blackpcnt = ImgProcessInfo.contrastB;
    whitepcnt = 100 - ImgProcessInfo.contrastW;

    hist = histogram(input);

    w = input->width;
    h = input->height;

    /* Find lower knee point in histogram to determine 'black' threshold. */
    cumsum = 0;
    limit = w * h * blackpcnt / 100;
    for (i = 0; i < 256; ++i)
	if ((cumsum += hist[i]) > limit)
	    break;
    blackval = i;

    /* Likewise for 'white' threshold. */
    cumsum = 0;
    limit = w * h * whitepcnt / 100;
    for (i = 256; i > 0; --i) {
	if ((max == 0) && hist[i])
	    max = i + 1;	/* max is index of highest non-zero entry */
	if ((cumsum += hist[i]) > limit)
	    break;
    }
    whiteval = i;

    if (whiteval == blackval) {
        for (y = 0; y < h; y++) {
	    for (x = 0; x < w; x++) {
	        p = ImagePixel(input, x, y);
	        *op++ = *p++;
	        *op++ = *p++;
	        *op++ = *p;
	    }
	}
        free(hist);
        return output;
    }

    /*
     * Now create a histogram with a linear ramp
     * from (blackval, 0) to (whiteval, max)
     */
    for (i = 0; i < blackval; ++i)
	hist[i] = 0;
    for (; i <= whiteval; ++i)
	hist[i] = max * (i - blackval) / (whiteval - blackval);
    for (; i < 256; ++i)
	hist[i] = max;

    for (y = 0; y < h; y++) {
	for (x = 0; x < w; x++) {
	    p = ImagePixel(input, x, y);
	    *op++ = hist[*p++];
	    *op++ = hist[*p++];
	    *op++ = hist[*p];
	}
    }
    free(hist);

    return output;
}

/*
 * Build histogram data for the image.
 */
static int *
histogram(Image * input)
{
    unsigned char *p;
    int x, y, w, h, i, r, g, b, *hist;

    w = input->width;
    h = input->height;

    /* Make a table of 256 zeros plus one sentinel */
    hist = xmalloc(257 * sizeof(int));
    for (i = 0; i <= 256; ++i)
	hist[i] = 0;

    /*
     * Build histogram of intensity values.
     */
    for (y = 0; y < h; y++) {
	for (x = 0; x < w; x++) {
	    p = ImagePixel(input, x, y);
	    r = *p++;
	    g = *p++;
	    b = *p;
	    i = (r * 169 + g * 256 + b * 87) / 512;	/* i = .33R+.5G+.17B */
	    ++hist[i];
	}
    }
    return hist;
}

#if 0

#define HIST_H	  120		/* Height of histogram */
#define HIST_W	  256		/* Width of histogram */
#define HIST_B	    2		/* Horizontal border width for histogram */
#define HIST_R	   12		/* Height of ruler below histogram */
#define HIST_T	    9		/* Height of tick marks on ruler */
#define LIGHTGREY 200		/* Greyscale level used for bars */
#define MIDGREY	  255		/* Greyscale level used for ruler */
#define DARKGREY  120		/* Greyscale level used for background */

/*
 * Create an image (width 256, height HIST_H) depicting the histogram data.
 */
Image *
HistogramImage(char *hist)
{
    unsigned char *p;
    int x, y, i;
    int max, e:
    unsigned char *ihist;
    Image *output = ImageNewGrey(HIST_W + 2 * HIST_B, HIST_H + HIST_R);;

    max = 0;
    ihist = xmalloc(128 * sizeof(int));
    for (i = 0; i < 128; ++i) {
	e = ihist[i] = hist[i * 2] + hist[i * 2 + 1];
	if (e > max)
	    max = e;
    }
    /*
     * Now create histogram image.
     * Only display 128 bins to make the histogram easier to read.
     * Leave a border of a few pixels in each side.
     */

    memset(output->data, DARKGREY, (HIST_W + 2 * HIST_B) * (HIST_H + HIST_R));
    for (x = 0; x < 128; ++x) {
	i = ihist[x] * (HIST_H - 1) / max;
	for (y = HIST_H - 1 - i; y < HIST_H - 1; y++) {
	    p = output->data + y * (HIST_W + 2 * HIST_B) + x * 2 + HIST_B;
	    *p++ = LIGHTGREY;
	    *p = LIGHTGREY;
	}
    }
    /* Ruler */
    memset(output->data + HIST_H * (HIST_W + 2 * HIST_B) + HIST_B,
	   MIDGREY, HIST_W);
    for (x = 0; x < 5; ++x) {
	int tick[] =
	{0, HIST_W / 4, HIST_W / 2, HIST_W * 3 / 4, HIST_W - 1};

	for (y = 0; y < HIST_T; y++) {
	    p = output->data + (y + HIST_H + 1) * (HIST_W + 2 * HIST_B)
		+ tick[x] + HIST_B;
	    *p = MIDGREY;
	}
    }
    free(ihist);

    return output;
}
#endif

/*
 * Tilt an image.
 */

/*
 * Compute the interpolated RGB values of a 1 x 1 pixel centered
 * at (x,y) and store these values in op[0] trough op[2].
 * The interpolation is based on weighting the RGB values proportionally
 * to the overlapping areas.
 */
static void 
interpol(float x, float y, Image * input, unsigned char *op)
{
    int xl, xh, yl, yh;
    float a, b, c, d, A, B, C, D;
    unsigned char *plh, *phh, *pll, *phl;

    xl = floor(x);
    xh = ceil(x);
    yl = floor(y);
    yh = ceil(y);

    pll = ImagePixel(input, xl, yl);

    if (xh == xl) {
	if (yh == yl) {
	    /* pixel coincides with one pixel */
	    *op++ = *pll++;
	    *op++ = *pll++;
	    *op++ = *pll++;
	} else {
	    /* pixel overlaps with two pixels on top of each other */
	    plh = ImagePixel(input, xl, yh);
	    A = y - yl;
	    B = yh - y;
	    *op++ = A * *plh++ + B * *pll++;
	    *op++ = A * *plh++ + B * *pll++;
	    *op++ = A * *plh++ + B * *pll++;
	}
    } else if (yh == yl) {
	/* pixel overlaps with two side-by-side pixels */
	phl = ImagePixel(input, xh, yl);
	A = x - xl;
	B = xh - x;
	*op++ = A * *phl++ + B * *pll++;
	*op++ = A * *phl++ + B * *pll++;
	*op++ = A * *phl++ + B * *pll++;
    } else {
	/* general case: pixel overlaps all four neighbour pixels */
	a = xh - x;
	b = y - yl;
	c = x - xl;
	d = yh - y;
	A = a * b;
	B = b * c;
	C = a * d;
	D = c * d;
	plh = ImagePixel(input, xl, yh);
	phl = ImagePixel(input, xh, yl);
	phh = ImagePixel(input, xh, yh);
	*op++ = A * *plh++ + B * *phh++ + C * *pll++ + D * *phl++;
	*op++ = A * *plh++ + B * *phh++ + C * *pll++ + D * *phl++;
	*op++ = A * *plh++ + B * *phh++ + C * *pll++ + D * *phl++;
    }
}

Image *
ImageTilt(Image * input)
{
    int x, y, br, bg, bb;
    int width = input->width, height = input->height;
    Image *output;
    unsigned char *op;
    float xt, yt;
    float X1, Y1, X2, Y2;


    X1 = width * ImgProcessInfo.tiltX1 / 100;
    X2 = width * ImgProcessInfo.tiltX2 / 100;

    Y1 = height * ImgProcessInfo.tiltY1 / 100;
    Y2 = height * ImgProcessInfo.tiltY2 / 100;

    if (((Y1 >= 0) && (Y1 < height)) || ((X2 >= 0) && (X2 < width)))
	return input;

    output = ImageNew(width, height);

    /* Get RGB values of background colour */
    br = ImgProcessInfo.background->red / 256;
    bg = ImgProcessInfo.background->green / 256;
    bb = ImgProcessInfo.background->blue / 256;

    op = output->data;
    for (x = 0; x < width * height; ++x) {
	*op++ = br;
	*op++ = bg;
	*op++ = bb;
    }

    for (y = 0; y < height; y++) {
	for (x = 0; x < width; x++) {
	    op = ImagePixel(output, x, y);
	    /* find the input coords corresponding to output coords (x,y) */
	    xt = (X1 * y - Y1 * x) / (y - Y1);
	    yt = (-X2 * y + x * Y2) / (x - X2);
	    if ((xt >= 0) && (xt < width) && (yt >= 0) && (yt < height)) {
#if 0
		unsigned char *p;
		/* rounding */
		p = ImagePixel(input, (int) (xt + 0.5), (int) (yt + 0.5));
		*op++ = *p++;
		*op++ = *p++;
		*op = *p;
#else
		interpol(xt, yt, input, op);
#endif
	    }
	}
    }

    return output;
}

/*
 * Produce the 'solarization' effect seen when exposing a 
 * photographic film to light during the development process.
 * Done here by inverting all pixels above threshold level.
 */
Image *
ImageSolarize(Image * input)
{
    Image *output = ImageNewCmap(input->width, input->height, input->cmapSize);
    int i;
    unsigned char *ip, *op;
    int count, limit;

    limit = ImgProcessInfo.solarizeThreshold * 255 / 100;

    /*
    **	 If the input has a colormap, just tweak that.
     */
    if (input->cmapSize != 0) {
	ip = input->cmapData;
	op = output->cmapData;
	count = input->cmapSize;

	memcpy(output->data, input->data,
	     sizeof(char) * input->scale * input->width * input->height);
    } else {
	ip = input->data;
	op = output->data;
	count = input->width * input->height;
    }

    for (i = 0; i < count; i++) {
	*op++ = (*ip > limit) ? 255 - *ip++ : *ip++;
	*op++ = (*ip > limit) ? 255 - *ip++ : *ip++;
	*op++ = (*ip > limit) ? 255 - *ip++ : *ip++;
    }

    return output;
}

/*
 * Colourmap quantization. Hacked from ppmqvga.c, which is part of Netpbm and
 * was originally written by Lyle Rains (lrains@netcom.com) and Bill Davidsen
 * (davidsen@crd.ge.com).
 * You are probably not supposed to understand this.
 */

#define RED_BITS   5
#define GREEN_BITS 6
#define BLUE_BITS  5

#define MAX_RED	   (1 << RED_BITS)
#define MAX_GREEN  (1 << GREEN_BITS)
#define MAX_BLUE   (1 << BLUE_BITS)

#define MAXWEIGHT  128
#define STDWEIGHT_DIV  (2 << 8)
#define STDWEIGHT_MUL  (2 << 10)
#define GAIN	   4

static int r, g, b, clutx, rep_threshold, rep_weight, dr, dg, db;
static int *color_cube, ncolors;
static unsigned char *clut;

#define CUBEINDEX(r,g,b)  (r)*(MAX_GREEN*MAX_BLUE) + (g)*MAX_BLUE + (b)

static void 
diffuse(void)
{
    int _7_32nds, _3_32nds, _1_16th;

    if (clutx < ncolors) {
	if (color_cube[CUBEINDEX(r, g, b)] > rep_threshold) {
	    clut[clutx * 4 + 0] = ((2 * r + 1) * 256) / (2 * MAX_RED);
	    clut[clutx * 4 + 1] = ((2 * g + 1) * 256) / (2 * MAX_GREEN);
	    clut[clutx * 4 + 2] = ((2 * b + 1) * 256) / (2 * MAX_BLUE);
	    ++clutx;
	    color_cube[CUBEINDEX(r, g, b)] -= rep_weight;
	}
	_7_32nds = (7 * color_cube[CUBEINDEX(r, g, b)]) / 32;
	_3_32nds = (3 * color_cube[CUBEINDEX(r, g, b)]) / 32;
	_1_16th = color_cube[CUBEINDEX(r, g, b)] - 3 * (_7_32nds + _3_32nds);
	color_cube[CUBEINDEX(r, g, b)] = 0;
	/* spread error evenly in color space. */
	color_cube[CUBEINDEX(r, g, b + db)] += _7_32nds;
	color_cube[CUBEINDEX(r, g + dg, b)] += _7_32nds;
	color_cube[CUBEINDEX(r + dr, g, b)] += _7_32nds;
	color_cube[CUBEINDEX(r, g + dg, b + db)] += _3_32nds;
	color_cube[CUBEINDEX(r + dr, g, b + db)] += _3_32nds;
	color_cube[CUBEINDEX(r + dr, g + dg, b)] += _3_32nds;
	color_cube[CUBEINDEX(r + dr, g + dg, b + db)] += _1_16th;

	/*
	 * Conserve the error at edges if possible
	 * (which it is, except the last pixel)
	 */
	if (color_cube[CUBEINDEX(r, g, b)] != 0) {
	    if (dg != 0)
		color_cube[CUBEINDEX(r, g + dg, b)] +=
		    color_cube[CUBEINDEX(r, g, b)];
	    else if (dr != 0)
		color_cube[CUBEINDEX(r + dr, g, b)] +=
		    color_cube[CUBEINDEX(r, g, b)];
	    else if (db != 0)
		color_cube[CUBEINDEX(r, g, b) + db] +=
		    color_cube[CUBEINDEX(r, g, b)];
	    else
		fprintf(stderr, "%s\n", msgText[LOST_ERROR_TERM]);
	}
    }
    color_cube[CUBEINDEX(r, g, b)] = -1;
}

/*
** Find representative color nearest to requested color.  Check color cube
** for a cached color index.  If not cached, compute nearest and cache result.
 */
static int 
nearest_color(unsigned char *p)
{
    register unsigned char *test;
    register unsigned i;
    unsigned long min_dist_sqd, dist_sqd;
    int nearest = 0;
    int *cache;
    int r, g, b;

    r = *p++;
    g = *p++;
    b = *p;
    cache = &(color_cube[CUBEINDEX((r << RED_BITS) / 256,
				   (g << GREEN_BITS) / 256,
				   (b << BLUE_BITS) / 256)]);
    if (*cache >= 0)
	return *cache;
    min_dist_sqd = ~0;
    for (i = 0; i < ncolors; ++i) {
	test = &clut[i * 4];
	dist_sqd =
	    3 * (r - test[0]) * (r - test[0]) +
	    4 * (g - test[1]) * (g - test[1]) +
	    2 * (b - test[2]) * (b - test[2]);
	if (dist_sqd < min_dist_sqd) {
	    nearest = i;
	    min_dist_sqd = dist_sqd;
	}
    }
    return (*cache = nearest);
}

Image *
ImageQuantize(Image * input)
{
    int w = input->width, h = input->height;
    Image *output = ImageNew(w, h);
    unsigned char *op = output->data;
    int *weight_convert;
    int k, x, y, i, j, nearest;
    int total_weight, cum_weight[MAX_GREEN];
    int *erropt;
    int *errP;
    unsigned char *p, *clutP;


    ncolors = ImgProcessInfo.quantizeColors;

    /* Make a color cube, initialized to zeros */
    color_cube = calloc(MAX_RED * MAX_GREEN * MAX_BLUE, sizeof(int));
    clut = calloc(ncolors * 4, sizeof(int));
    erropt = calloc(ncolors * 4, sizeof(int));

    clutx = 0;

    /* Count all occurrences of each color */
    for (y = 0; y < h; ++y)
	for (x = 0; x < w; ++x) {
	    p = ImagePixel(input, x, y);
	    r = p[0] / (256 / MAX_RED);
	    g = p[1] / (256 / MAX_GREEN);
	    b = p[2] / (256 / MAX_BLUE);
	    ++color_cube[CUBEINDEX(r, g, b)];
	}

    /* Initialize logarithmic weighting table */
    weight_convert = xmalloc(MAXWEIGHT * sizeof(int));
    weight_convert[0] = 0;
    for (i = 1; i < MAXWEIGHT; ++i) {
	weight_convert[i] = (int) (100.0 * log((double) i));
    }

    k = w * h;
    if ((k /= STDWEIGHT_DIV) == 0)
	k = 1;
    total_weight = i = 0;
    for (g = 0; g < MAX_GREEN; ++g) {
	for (r = 0; r < MAX_RED; ++r) {
	    for (b = 0; b < MAX_BLUE; ++b) {
		register int weight;
		/* Normalize the weights, independent of picture size. */
		weight = color_cube[CUBEINDEX(r, g, b)] * STDWEIGHT_MUL;
		weight /= k;
		if (weight)
		    ++i;
		if (weight >= MAXWEIGHT)
		    weight = MAXWEIGHT - 1;
		total_weight += (color_cube[CUBEINDEX(r, g, b)]
				 = weight_convert[weight]);
	    }
	}
	cum_weight[g] = total_weight;
    }
    rep_weight = total_weight / ncolors;

    /* Magic foo-foo dust here.	 What IS the correct way to select threshold? */
    rep_threshold = total_weight * (28 + 110000 / i) / 95000;

    /*
     * Do a 3-D error diffusion dither on the data in the color cube
     * to select the representative colors.  Do the dither back and forth in
     * such a manner that all the error is conserved (none lost at the edges).
     */

    dg = 1;
    for (g = 0; g < MAX_GREEN; ++g) {
	dr = 1;
	for (r = 0; r < MAX_RED; ++r) {
	    db = 1;
	    for (b = 0; b < MAX_BLUE - 1; ++b)
		diffuse();
	    db = 0;
	    diffuse();
	    ++b;
	    if (++r == MAX_RED - 1)
		dr = 0;
	    db = -1;
	    while (--b > 0)
		diffuse();
	    db = 0;
	    diffuse();
	}
	/* Modify threshold to keep rep points proportionally distributed */
	if ((j = clutx - (ncolors * cum_weight[g]) / total_weight) != 0)
	    rep_threshold += j * GAIN;

	if (++g == MAX_GREEN - 1)
	    dg = 0;
	dr = -1;
	while (r-- > 0) {
	    db = 1;
	    for (b = 0; b < MAX_BLUE - 1; ++b)
		diffuse();
	    db = 0;
	    diffuse();
	    ++b;
	    if (--r == 0)
		dr = 0;
	    db = -1;
	    while (--b > 0)
		diffuse();
	    db = 0;
	    diffuse();
	}
	/* Modify threshold to keep rep points proportionally distributed */
	if ((j = clutx - (ncolors * cum_weight[g]) / total_weight) != 0)
	    rep_threshold += j * GAIN;
    }

    /*
     * Check the error associated with the use of each color, and
     * change the value of the color to minimize the error.
     */
    for (y = 0; y < h; ++y) {
	for (x = 0; x < w; ++x) {
	    p = ImagePixel(input, x, y);
	    nearest = nearest_color(p);
	    errP = &erropt[nearest * 4];
	    clutP = &clut[nearest * 4];
	    errP[0] += *p++ - clutP[0];
	    errP[1] += *p++ - clutP[1];
	    errP[2] += *p - clutP[2];
	    ++errP[3];
	}
    }

    for (i = 0; i < ncolors; ++i) {
	clutP = &clut[i * 4];
	errP = &erropt[i * 4];
	j = errP[3];
	if (j > 0)
	    j *= 4;
	else if (j == 0)
	    j = 1;
	clutP[0] += (errP[0] / j) * 4;
	clutP[1] += (errP[1] / j) * 4;
	clutP[2] += (errP[2] / j) * 4;
    }

    /* Reset the color cache. */
    for (i = 0; i < MAX_RED * MAX_GREEN * MAX_BLUE; ++i)
	color_cube[i] = -1;

    /*
     * Map the colors in the image to their closest match in the new colormap.
     */
    for (y = 0; y < h; ++y) {
	for (x = 0; x < w; ++x) {
	    p = ImagePixel(input, x, y);
	    nearest = nearest_color(p);
	    clutP = &clut[nearest * 4];
	    *op++ = *clutP++;
	    *op++ = *clutP++;
	    *op++ = *clutP;
	}
    }

    free(clut);
    free(erropt);
    free(weight_convert);

    return output;
}

/*
 * Convert an image to grey scale.
 */
Image *
ImageGrey(Image * input)
{
    Image *output = ImageNew(input->width, input->height);
    unsigned char *p, *op = output->data;
    int x, y, v;


    for (y = 0; y < input->height; y++) {
	for (x = 0; x < input->width; x++) {
	    p = ImagePixel(input, x, y);
	    v = (32*p[0]+50*p[1]+18*p[2])/100;	/* 0.32R + 0.5G + 0.18B */
	    *op++ = v;
	    *op++ = v;
	    *op++ = v;
	}
    }
    return output;
}

Image *
ImageBWMask(Image * input)
{
    Image *output = ImageNew(input->width, input->height);
    unsigned char *p, *op = output->data;
    int x, y, v;

    for (y = 0; y < input->height; y++) {
	for (x = 0; x < input->width; x++) {
	    p = ImagePixel(input, x, y);
            if (memcmp(p, Global.bg, 3)) v = 0xff; else v = 0;
	    *op++ = v;
	    *op++ = v;
	    *op++ = v;
	}
    }
    return output;
}

/*
 * Modify RGB values of an image.
 */
Image *
ImageModifyRGB(Image * input)
{
    Image *output;
    unsigned char *p, *op;
    int x, y;
    double fr, fg, fb, frp, fgp, fbp;

    if (!ImgProcessInfo.redshift &&
        !ImgProcessInfo.greenshift &&
        !ImgProcessInfo.blueshift) return input;
  
    output = ImageNew(input->width, input->height);
    op = output->data;

    fr = 2.0*ImgProcessInfo.redshift / (100.01-ImgProcessInfo.redshift);
    fg = 2.0*ImgProcessInfo.greenshift / (100.01-ImgProcessInfo.greenshift);
    fb = 2.0*ImgProcessInfo.blueshift / (100.01-ImgProcessInfo.blueshift);
    frp = fr / 255.0;
    fgp = fg / 255.0;
    fbp = fb / 255.0;
    fr = fr + 1.0;
    fg = fg + 1.0;
    fb = fb + 1.0;

    for (y = 0; y < input->height; y++) {
	for (x = 0; x < input->width; x++) {
	    p = ImagePixel(input, x, y);
	    *op++ = (char) ( 0.25 + p[0] * fr / (1.0 + p[0] * frp) );
	    *op++ = (char) ( 0.25 + p[1] * fg / (1.0 + p[1] * fgp) );
	    *op++ = (char) ( 0.25 + p[2] * fb / (1.0 + p[2] * fbp) );
	}
    }

    return output;
}

/*
 * Pipe command routines
 */

void pipe_command(command, input, output, error)
    char *command;
    int input(char*, int);
    void output(const char*, int);
    void error(const char*, int);
{
    int in_pipe[2];
    int out_pipe[2];
    int err_pipe[2];
    int pid, bytes;
    char buffer[BUFSIZE];
    
    if (pipe(in_pipe)==-1)
    {
        return;
    }
    if (pipe(out_pipe)==-1)
    {
        return;
    }
    if (pipe(err_pipe)==-1)
    {
        return;
    }
    
    pid = fork();
    if (pid==-1)
    {
        close(in_pipe[0]);
        close(in_pipe[1]);
        close(out_pipe[0]);
        close(out_pipe[1]);
        close(err_pipe[0]);
        close(err_pipe[1]);
        return;
    }
    
    if (!pid)
    {    /* child's thread */
        close(in_pipe[1]); /* there's no writing on input pipe! */
        close(out_pipe[0]); /* there's no reading of output pipe! */
        close(err_pipe[0]); /* there's no reading of error pipe! */        
        /* redirect stdin */
        if (input)
        {            
            if (dup2(in_pipe[0], fileno(stdin))==-1)
            {
                exit(1);
            }
        }
        /* redirect stdout */
        if (output)
        {            
            if (dup2(out_pipe[1], fileno(stdout))==-1)
            {
                exit(1);
            }
        }
        /* redirect stderr */
        if (error)
        {
            if (dup2(err_pipe[1], fileno(stderr))==-1)
            {
                exit(1);
            }
        }
                  system(command);
        exit(1);        
    }
    else
    {    /* parent's thread */
        close(in_pipe[0]); /* there's no reading of input pipe! */
        close(out_pipe[1]); /* there's no writing on output pipe! */
        close(err_pipe[1]); /* there's no writing on error pipe! */
        /* start writing to pipe */
        if (input)
        {            
            while((bytes=input(buffer, BUFSIZE-1))>0)
            {  
                bytes = write(in_pipe[1], buffer, bytes);
                if (bytes==-1)
                {                  
                    break;
                }            
            }
            close(in_pipe[1]);
        }
        /* start reading pipe output */
        if (output)
        {
            while((bytes=read(out_pipe[0], buffer, BUFSIZE-1))>0)
                output(buffer, bytes);
            close(out_pipe[0]);
        }
        /* start reading pipe error*/
        if (error)
        {
            while((bytes=read(err_pipe[0], buffer, BUFSIZE-1))>0)
                error(buffer, bytes);
            close(err_pipe[0]);
        }
    }        
}

static void append_status(char *string)
{
char *ptr, *catptr;

    XtVaGetValues(head->cclog, XtNstring, &ptr, NULL);
    catptr = xmalloc(strlen(ptr)+strlen(string)+10);
    strcpy(catptr, ptr);
    strcat(catptr, "\n");
    strcat(catptr, string);
    XtVaSetValues(head->cclog, XtNstring, catptr, NULL);
}

static void CollectOutput(const char* data, int i)
{
    static int FlagNewLine = 1;
    int pos=0;
    char superstring[2000];

    while(i--)
    {
    if(data[pos] == '\n')
    {
        char tmp[2000];
        strncpy(tmp, data, pos);
        tmp[pos] = '\0';
        tmp[pos+1] = '\0';
        if (!FlagNewLine)
        {
        strcat(superstring, tmp);
        }
        else
        {
            superstring[0] = '\0';
        strcpy(superstring, tmp);
        }
        data = data + pos + 1;
        pos = 0;
        FlagNewLine = 1;
            append_status(superstring);
    }
    else
        pos++;
    }
    if (pos)
    {
    if (!FlagNewLine)
        strcat(superstring, data);
    else
        superstring[0] = '\0';
    FlagNewLine = 0;
    }
}


/*
 * User defined script procedures
 */

static void
closeCallback(Widget w, XtPointer wlArg, XtPointer junk)
{
   LocalInfo *info = (LocalInfo *) wlArg;
   PopdownMenusGlobal();
   XtDestroyWidget(info->shell);
}

static void
compileCallback(Widget w, XtPointer wlArg, XtPointer junk)
{
   LocalInfo *info = (LocalInfo *) wlArg;
   FILE *fd;
   void (* proc)(Widget wid);
   Image * (* iproc)();
   Image * image;
   Screen *screen;
   Visual *visual;
   Pixmap pix;
   Colormap cmap;
   Widget paint;
   char *tmp, *ptr, *text;
   char cmd[512];
   char header[30];
   char * error;
   void *dl_handle = NULL;
   unsigned char *alpha = NULL;
   int i;

   /* create temporary name */
   fd= openTempFile(&tmp);
   fclose(fd);

   /* copy buffer to temporary C file */
   XtVaGetValues(info->program, XtNstring, &text, NULL);
   if (!text || !*text) {
      return;
   }
   sprintf(cmd, "%s.c", tmp);
   fd = fopen(cmd, "w");
   if (!fd) return;

   ptr = text;
   i = 0;
   while (*ptr) {
      fputc(*ptr, fd);
      if (i <= 25) {
         header[i] = *ptr;
         if (*ptr == '\n') header[i] = '\0';
	 i++;
      }
      ++ptr;
   }
   fclose(fd);
   header[i] = '\0';

   info->mode = -1;
   if (strncasecmp(header, "/* Xpaint-image */", 18) == 0)
      info->mode = PREDEF_IMAGES;
   if (strncasecmp(header, "/* Xpaint-filter */", 19) == 0)
      info->mode = PREDEF_FILTERS;
   if (strncasecmp(header, "/* Xpaint-procedure */", 22) == 0)
      info->mode = PREDEF_PROCEDURES;

   /* compile C script */
   sprintf(cmd, "cd /tmp\ngcc -fPIC -I%s/include -I/usr/include/X11 "
                "-c %s.c -o %s.o\n"
                "gcc -fPIC -shared -Wl,-soname,%s.so %s.o -o %s.so\n",
	        GetShareDir(),
                tmp, tmp, strstr(tmp, "XPaint"), tmp, tmp);
   XtVaSetValues(info->cclog, XtNstring, cmd, NULL);
   pipe_command(cmd, NULL, CollectOutput, CollectOutput);

   /* clean *.c and *.o file */
   sprintf(cmd, "%s.c", tmp);
   unlink(cmd);
   sprintf(cmd, "%s.o", tmp);
   unlink(cmd);

   /* load dll library */
   sprintf(cmd, "%s.so", tmp);

   if (info->mode >= 0)
      dl_handle = dlopen(cmd, RTLD_LAZY);

   /* clean dll .so file */
   unlink(cmd);
   removeTempFile();

   if (dl_handle)
      sprintf(cmd, "echo \"%s\"",
	      msgText[C_SCRIPT_SUCCESSFULLY_COMPILED_AND_LINKED]);
   else
      sprintf(cmd, "echo \"\"; echo \"%s\"; echo \"%s\"",
              msgText[C_SCRIPT_COULD_NOT_BE_COMPILED_OR_LINKED], dlerror());

   pipe_command(cmd, NULL, CollectOutput, CollectOutput);
   if (!dl_handle) return;

   if (info->mode == PREDEF_FILTERS) {
      if (dl_filter) dlclose(dl_filter);
      dl_filter = dl_handle;
   }

   if (info->mode == PREDEF_IMAGES) {
      if (dl_image) dlclose(dl_image);
      dl_image = dl_handle;   
      iproc = dlsym(dl_image, "ImageCreate");
      if ((error = dlerror()) != NULL || !iproc) {
	 if (!error) error = msgText[UNABLE_TO_CREATE_IMAGE];
         Notice(Global.toplevel, error);
         return;
      }
      pix = None;
      image = iproc();
      if (image) {
	 screen = XtScreen(Global.toplevel);
	 visual = DefaultVisualOfScreen(screen);
	 cmap = XCreateColormap(XtDisplay(Global.toplevel), 
		    RootWindowOfScreen(screen), visual, AllocNone);
         alpha = image->alpha;
	 ImageToPixmap(image, Global.toplevel, &pix, &cmap);
      }
      if (!pix) {
         Notice(Global.toplevel, msgText[UNABLE_TO_CREATE_IMAGE]);
	 return;
      }
      if (alpha) {
         file_isSpecialImage = 1;
         file_transparent = 1;
      }
      paint = graphicCreate(makeGraphicShell(Global.toplevel), 0, 0, -1, 
                            pix, cmap, alpha);
      SetAlphaMode((Widget)paint, -1);
      free(alpha);
      return;
   }

   if (info->mode == PREDEF_PROCEDURES) {
      if (dl_proc) dlclose(dl_proc);
      dl_proc = dl_handle;
      proc = dlsym(dl_proc, "PaintProcedure");
      if ((error = dlerror()) != NULL) {
         Notice(Global.toplevel, error);
         return;
      }
      if (proc) proc(Global.toplevel);
      return;
   }
}

static void
externCallback(Widget w, XtPointer wlArg, XtPointer junk)
{
   FILE *fd;
   LocalInfo *info = (LocalInfo *) wlArg;
   int l;
   char *tmp, *ptr, *text;
   char name[256];
   char buf[2048];

   /* write buffer to file */
   PopdownMenusGlobal();
   fd = openTempFile(&tmp);
   fclose(fd);
   sprintf(name, "%s.c", tmp);
   removeTempFile();
   fd = fopen(name, "w");
   if (!fd) {
      Notice(w, msgText[UNABLE_TO_OPEN_FILE], name);
      return;
   }
   XtVaGetValues(info->program, XtNstring, &text, NULL);
   if (text && *text) {
      ptr = text;
      while (*ptr) {
         fputc(*ptr, fd);
         ++ptr;
      }
   }
   fclose(fd);

   /* call external editor */
   sprintf(buf, "%s %s", EDITOR, name);
   system(buf);

   /* Now copy edited file into buffer */
   fd = fopen(name, "r");
   if (!fd) {
      Notice(w, msgText[UNABLE_TO_OPEN_FILE], name);
      return;
   }

   text = xmalloc(2);
   *text = '\0';
   l = 0;
   while (fgets(buf, 2040, fd)) {
      l += strlen(buf);
      text = realloc(text, l+2);
      strcat(text, buf);
   }
   fclose(fd);
   unlink(name);
   XtVaSetValues(info->program, XtNstring, text, NULL);
}

static void 
loadFileCallbackOK(Widget w, XtPointer wlArg, char *file)
{
   FILE *fd;
   LocalInfo *info = (LocalInfo *) wlArg;
   static char *text;
   char buf[2048];
   int l;

   if (!file || !*file) return;
   fd = fopen(file, "r");
   if (!fd) {
      Notice(w, msgText[UNABLE_TO_OPEN_FILE], file);
      return;
   }
   info->scriptfile = file;
   XtVaSetValues(info->name, XtNlabel, file, NULL);
   text = xmalloc(2);
   *text = '\0';
   l = 0;
   while (fgets(buf, 2040, fd)) {
      l += strlen(buf);
      text = realloc(text, l+2);
      strcat(text, buf);
   }
   fclose(fd);
   XtVaSetValues(info->program, XtNstring, text, NULL);
}

static void
loadFileCallback(Widget w, XtPointer wlArg, XtPointer junk)
{
   char buf[256];
   *buf = '\0';
   if (getcwd(buf, 256)) strcat(buf, "/");
   Global.explore = True;
   GetFileName(GetShell(w), BROWSER_SIMPLEREAD, 
               buf, (XtCallbackProc) loadFileCallbackOK, wlArg);
   Global.explore = False;
}

static void 
saveFileCallbackOK(Widget w, XtPointer wlArg, char *file)
{
   FILE *fd;
   LocalInfo *info = (LocalInfo *) wlArg;
   char *ptr, *text;
 
   if (!file || !*file) return;
   fd = fopen(file, "w");
   if (!fd) {
      Notice(w, msgText[UNABLE_TO_OPEN_FILE], file);
      return;
   }
   info->scriptfile = file;
   XtVaSetValues(info->name, XtNlabel, file, NULL);
   XtVaGetValues(info->program, XtNstring, &text, NULL);
   if (!text || !*text) {
      fclose(fd);
      return;
   }
   ptr = text;
   while (*ptr) {
      fputc(*ptr, fd);
      ++ptr;
   }
   fclose(fd);
}

static void
saveFileCallback(Widget w, XtPointer wlArg, XtPointer junk)
{
   LocalInfo *info = (LocalInfo *) wlArg;
   char buf[256];
   *buf = '\0';
   if (getcwd(buf, 256)) strcat(buf, "/");
   Global.explore = True;
   if (!info->scriptfile || !info->scriptfile[0])
      GetFileName(GetShell(w), BROWSER_SIMPLESAVE, 
               buf, (XtCallbackProc) saveFileCallbackOK, wlArg);
   else
      saveFileCallbackOK(w, wlArg, info->scriptfile);
   Global.explore = False;
}

static void
saveasFileCallback(Widget w, XtPointer wlArg, XtPointer junk)
{
   char buf[256];
   *buf = '\0';
   if (getcwd(buf, 256)) strcat(buf, "/");
   Global.explore = True;
   GetFileName(GetShell(w), BROWSER_SIMPLESAVE, 
               buf, (XtCallbackProc) saveFileCallbackOK, wlArg);
   Global.explore = False;
}

static void
loadScriptCallback(Widget w, XtPointer wlArg, XtPointer junk)
{
   LocalInfo *info = (LocalInfo *) wlArg;
   static char directory[256];
   int i;

   info->mode = PREDEF_IMAGES;
   for (i=0; i<PREDEF_SEPARATOR; i++)
      if (w == predefMenu[i].widget) {
	 info->mode = i;
	 break;
      }

   Global.explore = True;
   sprintf(directory, "%s/c_scripts/%s/", GetShareDir(),
	              predefMenu[info->mode].name);

   GetFileName(GetShell(w), BROWSER_SIMPLEREAD, directory, 
      (XtCallbackProc) loadFileCallbackOK, wlArg);
   Global.explore = False;
}

void
editorResized(Widget w, LocalInfo * l, XConfigureEvent * event, Boolean * flg)
{
    Dimension width, height;
    Dimension width1, height1, height2;

    XtVaGetValues(l->shell, XtNwidth, &width, XtNheight, &height, NULL);
    XtResizeWidget(XtParent(l->name), width, 38, BORDERWIDTH);
    XtResizeWidget(l->pane, width, height, BORDERWIDTH);
    XtVaSetValues(l->pane, XtNwidth, width, XtNheight, height, NULL);
    XtVaGetValues(XtParent(l->program), XtNheight, &height1, NULL);

#ifdef XAW3DG   
    width1 = (width>12)? width-10 : 2;
    height1 = (height1>10)? height1-8 : 2;
    height2 = (height>height1+63)? height - height1 - 53 : 10;
#else
    width1 = (width>10)? width-8 : 2;
    height1 = (height1>10)? height1-8 : 2;
    height2 = (height>height1+60)? height - height1 - 50 : 10;
#endif   
    XtResizeWidget(XtParent(l->program), width1+8, height1+8, BORDERWIDTH);
    XtResizeWidget(l->program, width1, height1, BORDERWIDTH);
    XtVaSetValues(XtParent(l->program), 
       XtNwidth, width1+8, XtNheight, height1+8, NULL);
    XtVaSetValues(l->program, 
       XtNwidth, width1, XtNheight, height1, NULL);
    XtMoveWidget(XtParent(l->program), 0, 39);
    XtMoveWidget(l->program, 4, 0);

    XtResizeWidget(XtParent(l->cclog), width1+8, height2+8, BORDERWIDTH);
    XtResizeWidget(l->cclog, width1, height2, BORDERWIDTH);
    XtVaSetValues(XtParent(l->cclog), 
       XtNwidth, width1+8, XtNheight, height2+8, NULL);
    XtVaSetValues(l->cclog, 
       XtNwidth, width1, XtNheight, height2, NULL);
    XtMoveWidget(XtParent(l->cclog), 0, 46+height1);
    XtMoveWidget(l->cclog, 4, 0);
}

static void
popupHandler(Widget w, LocalInfo * info, XEvent * event, Boolean * flag)
{
    if (Global.popped_up) {
        if (event->type == ButtonRelease)
            PopdownMenusGlobal();
        event->type = None;       
    }
}

void *
ScriptEditor(Widget w, Widget paint)
{
    Display *dpy;
    Window root;
    Widget topform, midform, botform;
    Widget bar, cancel, compile;
    LocalInfo *info = XtNew(LocalInfo);
    Arg args[4];
    int nargs = 0;
    int i;

    PopdownMenusGlobal();

    head = info;
    info->mode = 0;
    info->scriptfile = NULL;

    dpy = XtDisplay(GetShell(w));
    root = RootWindowOfScreen(XtScreen(GetShell(w)));

    info->shell = XtVisCreatePopupShell("XPaint / C scripts", 
                                  topLevelShellWidgetClass,
				  Global.toplevel, args, nargs);
    info->pane = XtVaCreateManagedWidget("pane", panedWidgetClass, info->shell,
                                   NULL);
    topform = XtVaCreateManagedWidget("topform", formWidgetClass, info->pane, 
                                      NULL);

    bar = MenuBarCreate(topform, XtNumber(printMenuBar), printMenuBar);
    XtVaSetValues(bar, XtNvertDistance, 2, NULL);
    
    compile = XtVaCreateManagedWidget("compile",
		 		    commandWidgetClass, topform,
				    XtNfromHoriz, bar,
				    XtNhorizDistance, 32,
				    XtNvertDistance, 5,
				    NULL);
    cancel = XtVaCreateManagedWidget("close",
		 		    commandWidgetClass, topform,
				    XtNfromHoriz, compile,
				    XtNhorizDistance, 4,
				    XtNvertDistance, 5,
				    NULL);

    info->name = XtVaCreateManagedWidget("name",
				    labelWidgetClass, topform,
				    XtNlabel, "",
				    XtNwidth, 350,
				    XtNfromHoriz, cancel,
                                    XtNhorizDistance, 24,
                                    XtNvertDistance, 6,
				    XtNborderWidth, 0,
                                    NULL);

    midform = XtVaCreateManagedWidget("midform", 
                                    formWidgetClass, info->pane, NULL);
    botform = XtVaCreateManagedWidget("botform", 
                                    formWidgetClass, info->pane, NULL);

    info->program = 
          XtVaCreateManagedWidget("program", asciiTextWidgetClass, midform,
                                  XtNeditType, XawtextEdit,
                                  XtNresizable, True,
                                  XtNwidth, 620,
                                  XtNheight, 300,
				  XtNstring, NULL,
				  XtNscrollHorizontal, XawtextScrollWhenNeeded,
				  XtNscrollVertical, XawtextScrollWhenNeeded,
				  NULL);
    XtInsertRawEventHandler(info->program, 
			    ButtonPressMask | ButtonReleaseMask,
                            False, (XtEventHandler) popupHandler, info, 
                            XtListHead);


    info->cclog = 
          XtVaCreateManagedWidget("cclog", asciiTextWidgetClass, botform,
                                  XtNeditType, XawtextRead,
                                  XtNresizable, True,
                                  XtNwidth, 620,
                                  XtNheight, 100,
				  XtNstring, NULL,
				  XtNscrollHorizontal, XawtextScrollWhenNeeded,
				  XtNscrollVertical, XawtextScrollWhenNeeded,
				  NULL);
    XtInsertRawEventHandler(info->cclog, 
			    ButtonPressMask | ButtonReleaseMask,
                            False, (XtEventHandler) popupHandler, info, 
                            XtListHead);


    AddDestroyCallback(info->shell,
                       (DestroyCallbackFunc) closeCallback, (XtPointer) info);

    XtAddCallback(fileMenu[FILE_LOAD].widget, XtNcallback, 
		     (XtCallbackProc) loadFileCallback, (XtPointer) info);
    XtAddCallback(fileMenu[FILE_SAVE].widget, XtNcallback, 
		     (XtCallbackProc) saveFileCallback, (XtPointer) info);
    XtAddCallback(fileMenu[FILE_SAVEAS].widget, XtNcallback, 
		     (XtCallbackProc) saveasFileCallback, (XtPointer) info);
    XtAddCallback(fileMenu[FILE_EDIT].widget, XtNcallback, 
		     (XtCallbackProc) externCallback, (XtPointer) info);
    XtAddCallback(fileMenu[FILE_CLOSE].widget, XtNcallback, 
		     (XtCallbackProc) closeCallback, (XtPointer) info);
    XtAddCallback(compile, XtNcallback,
                  (XtCallbackProc)compileCallback, (XtPointer) info);
    XtAddCallback(cancel, XtNcallback,
                  (XtCallbackProc)closeCallback, (XtPointer) info);

    for (i=0; i<PREDEF_SEPARATOR; i++)
       XtAddCallback(predefMenu[i].widget, XtNcallback, 
		     (XtCallbackProc) loadScriptCallback, (XtPointer) info);

    /* XtRealizeWidget(info->shell); */
    XtPopup(info->shell, XtGrabNone);
    XtVaSetValues(topform, XtNshowGrip, False, NULL);
    XtVaSetValues(midform, XtNshowGrip, True, NULL);
    XtVaSetValues(botform, XtNshowGrip, True, NULL);

    XtUnmanageChild(info->name);
    XMapWindow(dpy, XtWindow(info->name));
    XtUnmanageChild(cancel);
    XMapWindow(dpy, XtWindow(cancel));
    XtUnmanageChild(compile);
    XMapWindow(dpy, XtWindow(compile));
    XtUnmanageChild(info->program);
    XMapWindow(dpy, XtWindow(info->program));
    XtUnmanageChild(info->cclog);
    XMapWindow(dpy, XtWindow(info->cclog));      
   
    XtAddEventHandler(midform, StructureNotifyMask, False,
		      (XtEventHandler) editorResized, (XtPointer) info);
    XtAddEventHandler(info->shell, StructureNotifyMask, False,
		      (XtEventHandler) editorResized, (XtPointer) info);
    XtAddEventHandler(info->program, ButtonPressMask, False,
		      (XtEventHandler) mousewheelScroll, (XtPointer) NULL);
    XtAddEventHandler(info->cclog, ButtonPressMask, False,
		      (XtEventHandler) mousewheelScroll, (XtPointer) NULL);
    XtSetMinSizeHints(info->shell, 300, 160);
    return info;
}

/* convenience routines */
int
RegionX()
{
    PaintWidget pw = (PaintWidget)Global.curpaint;
    return pw->paint.region.rect.x;
}
int
RegionY()
{
    PaintWidget pw = (PaintWidget)Global.curpaint;
    return pw->paint.region.rect.y;
}
int
RegionWidth()
{
    PaintWidget pw = (PaintWidget)Global.curpaint;
    return pw->paint.region.rect.width;
}
int
RegionHeight()
{
    PaintWidget pw = (PaintWidget)Global.curpaint;
    return pw->paint.region.rect.height;
}

void ImageToRegion(Image * input, XRectangle rect, Pixmap mask)
{
    Pixmap pix;
    Colormap cmap;
    ImageToPixmap(input, Global.curpaint, &pix, &cmap);
    PwRegionSet(Global.curpaint, &rect, pix, mask);
}

void ImageToCanvas(Image * input, Pixmap mask)
{
    Pixmap pix;
    Colormap cmap;
    XRectangle rect;
    if (!input || !Global.curpaint) return;
    rect.width = input->width;
    rect.height = input->height;
    rect.x = 0;
    rect.y = 0;
    ImageToPixmap(input, Global.curpaint, &pix, &cmap);
    PwRegionSet(Global.curpaint, &rect, pix, mask);
    PwRegionSet(Global.curpaint, None, None, None);
}

Image *
CanvasToImage()
{
    PaintWidget pw = (PaintWidget)Global.curpaint;
    Image * image;

    image = PixmapToImage((Widget)pw, GET_PIXMAP(pw), Global.clipboard.cmap);
    return image;
}

Boolean
isFilterDefined()
{
    return (dl_filter)? True : False;
}

Image *
ImageUserDefined(Image * input)
{
    Image *output;
    char * error;
    void (* proc)(Image *, Image *);
    
    if (!dl_filter) return NULL;

    proc = dlsym(dl_filter, "FilterProcess");
    if ((error = dlerror()) != NULL) {
       Notice(Global.toplevel, error);
       return input;
    }

    output = ImageNew(input->width, input->height);

    proc(input, output);

    return output;
}


#define GRAY(p)		(((p)[0]*169 + (p)[1]*256 + (p)[2]*87)/512)
#define SQ(x)		((x)*(x))

/*
 * Directional filter, according to the algorithm described on p. 60 of:
 * _Algorithms_for_Graphics_and_Image_Processing_. Theo Pavlidis.
 * Computer Science Press, 1982.
 * For each pixel, detect the most prominent edge and apply a filter that
 * does not degrade that edge.
 */
Image *
ImageDirectionalFilter(Image * input)
{
    unsigned char *p00, *p10, *p_0, *p11, *p__, *p01, *p0_, *p_1, *p1_;
    int g00, g10, g_0, g11, g__, g01, g0_, g_1, g1_;
    int v0, v45, v90, v135, vmin, theta;
    int x, y;
    Image *output = ImageNew(input->width, input->height);
    unsigned char *op = output->data;

    /*
     * We don't process the border of the image tto avoid the hassle
     * of having do deal with boundary conditions. Hopefully no one
     * will notice.
     */
    /* copy first row unchanged */
    for (x = 0; x < input->width; x++) {
	p00 = ImagePixel(input, x, 0);
	*op++ = *p00++;
	*op++ = *p00++;
	*op++ = *p00++;
    }

    for (y = 1; y < input->height - 1; y++) {
	/* copy first column unchanged */
	p00 = ImagePixel(input, 0, y);
	*op++ = *p00++;
	*op++ = *p00++;
	*op++ = *p00++;
	for (x = 1; x < input->width - 1; x++) {
	    /* find values of pixel and all neighbours */
	    p00 = ImagePixel(input, x, y);
	    p10 = ImagePixel(input, x + 1, y);
	    p_0 = ImagePixel(input, x - 1, y);
	    p11 = ImagePixel(input, x + 1, y + 1);
	    p__ = ImagePixel(input, x - 1, y - 1);
	    p01 = ImagePixel(input, x, y + 1);
	    p0_ = ImagePixel(input, x, y - 1);
	    p_1 = ImagePixel(input, x - 1, y + 1);
	    p1_ = ImagePixel(input, x + 1, y - 1);

	    /* get grayscale values */
	    g00 = GRAY(p00);
	    g01 = GRAY(p01);
	    g10 = GRAY(p10);
	    g11 = GRAY(p11);
	    g0_ = GRAY(p0_);
	    g_0 = GRAY(p_0);
	    g__ = GRAY(p__);
	    g_1 = GRAY(p_1);
	    g1_ = GRAY(p1_);

	    /* estimate direction of edge, if any */
	    v0 = SQ(g00 - g10) + SQ(g00 - g_0);
	    v45 = SQ(g00 - g11) + SQ(g00 - g__);
	    v90 = SQ(g00 - g01) + SQ(g00 - g0_);
	    v135 = SQ(g00 - g_1) + SQ(g00 - g1_);

	    vmin = MIN(MIN(v0, v45), MIN(v90, v135));
	    theta = 0;
	    if (vmin == v45)
		theta = 1;
	    else if (vmin == v90)
		theta = 2;
	    else if (vmin == v135)
		theta = 3;

	    /* apply filtering according to direction of edge */
	    switch (theta) {
	    case 0:		/* 0 degrees */
		*op++ = (*p_0++ + *p00++ + *p10++) / 3;
		*op++ = (*p_0++ + *p00++ + *p10++) / 3;
		*op++ = (*p_0++ + *p00++ + *p10++) / 3;
		break;
	    case 1:		/* 45 degrees */
		*op++ = (*p__++ + *p00++ + *p11++) / 3;
		*op++ = (*p__++ + *p00++ + *p11++) / 3;
		*op++ = (*p__++ + *p00++ + *p11++) / 3;
		break;
	    case 2:		/* 90 degrees */
		*op++ = (*p0_++ + *p00++ + *p01++) / 3;
		*op++ = (*p0_++ + *p00++ + *p01++) / 3;
		*op++ = (*p0_++ + *p00++ + *p01++) / 3;
		break;
	    case 3:		/* 135 degrees */
		*op++ = (*p1_++ + *p00++ + *p_1++) / 3;
		*op++ = (*p1_++ + *p00++ + *p_1++) / 3;
		*op++ = (*p1_++ + *p00++ + *p_1++) / 3;
		break;
	    }
	}
	/* copy last column unchanged */
	p00 = ImagePixel(input, x, y);
	*op++ = *p00++;
	*op++ = *p00++;
	*op++ = *p00++;
    }

    /* copy last row unchanged */
    for (x = 0; x < input->width; x++) {
	p00 = ImagePixel(input, x, y);
	*op++ = *p00++;
	*op++ = *p00++;
	*op++ = *p00++;
    }
    return output;
}

#if 0
#define ISFOREGND(p) ((p) ? \
		      (deltaR-deltaRV <= p[0]) && (p[0] <= deltaR+deltaRV) && \
		      (deltaG-deltaGV <= p[1]) && (p[1] <= deltaG+deltaGV) && \
		      (deltaB-deltaBV <= p[2]) && (p[2] <= deltaB+deltaBV) : 1)

/*
 * Thicken an image.
 */
Image *
ImageThicken(Image * input)
{
    unsigned char *p00, *p10, *p_0, *p01, *p0_;
    int x, y, width, height, br, bg, bb;
    int deltaR, deltaRV, deltaG, deltaGV, deltaB, deltaBV;
    Image *output;
    unsigned char *op;

    width = input->width;
    height = input->height;
    output = ImageNew(width, height);
    op = output->data;

    /* Get RGB values of background colour */
    br = ImgProcessInfo.background->red / 256;
    bg = ImgProcessInfo.background->green / 256;
    bb = ImgProcessInfo.background->blue / 256;

    for (y = 0; y < height; y++)
	for (x = 0; x < width; x++) {
	    /* find values of pixel and all d-neighbours */
	    p00 = ImagePixel(input, x, y);
	    p10 = x < width - 1 ? ImagePixel(input, x + 1, y) : NULL;
	    p_0 = x > 0 ? ImagePixel(input, x - 1, y) : NULL;
	    p01 = y < height - 1 ? ImagePixel(input, x, y + 1) : NULL;
	    p0_ = y > 0 ? ImagePixel(input, x, y - 1) : NULL;

	    if (ISFOREGND(p00)) {
		*op++ = *p00++;
		*op++ = *p00++;
		*op++ = *p00++;
	    } else {
		*op++ = br;
		*op++ = bg;
		*op++ = bb;
	    }
	}

    return output;
}
#endif