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

package javax.media.j3d;

import java.awt.image.DataBufferByte;
import java.awt.image.RenderedImage;
import java.util.ArrayList;
import java.util.HashMap;

import javax.vecmath.Color4f;
import javax.vecmath.Point2f;
import javax.vecmath.Point3f;
import javax.vecmath.Tuple3f;

/**
 * The Texture object is a component object of an Appearance object
 * that defines the texture properties used when texture mapping is
 * enabled. Texture object is an abstract class and all texture
 * objects must be created as either a Texture2D object or a
 * Texture3D object.
 */
abstract class TextureRetained extends NodeComponentRetained {
    // A list of pre-defined bits to indicate which component
    // in this Texture object changed.
    static final int ENABLE_CHANGED      = 0x001;
    static final int COLOR_CHANGED       = 0x002;
    static final int IMAGE_CHANGED       = 0x004;
    static final int STATE_CHANGED       = 0x008;
    static final int UPDATE_IMAGE        = 0x010;
    static final int IMAGES_CHANGED      = 0x020;
    static final int BASE_LEVEL_CHANGED  = 0x040;
    static final int MAX_LEVEL_CHANGED   = 0x080;
    static final int MIN_LOD_CHANGED     = 0x100;
    static final int MAX_LOD_CHANGED     = 0x200;
    static final int LOD_OFFSET_CHANGED  = 0x400;

    // constants for min and mag filter
    static final int MIN_FILTER = 0;
    static final int MAG_FILTER = 1;

    // Boundary width
    int		boundaryWidth = 0;

    // Boundary modes (wrap, clamp, clamp_to_edge, clamp_to_boundary)
    int		boundaryModeS = Texture.WRAP;
    int		boundaryModeT = Texture.WRAP;

    // Filter modes
    int		minFilter = Texture.BASE_LEVEL_POINT;
    int		magFilter = Texture.BASE_LEVEL_POINT;

    // Integer flag that contains bitset to indicate
    // which field changed.
    int isDirty = 0xffff;

    // Texture boundary color
    Color4f	boundaryColor = new Color4f(0.0f, 0.0f, 0.0f, 0.0f);

    // Texture Object Id used by native code.
    int 	objectId = -1;

    int		mipmapMode = Texture.BASE_LEVEL; // Type of mip-mapping
    int		format = Texture.RGB;		// Texture format
    int		width = 1;			// Width in pixels (2**n)
    int		height = 1;			// Height in pixels (2**m)

    // true if width or height is non power of two
    private boolean widthOrHeightIsNPOT = false;
    // Array of images (one for each mipmap level)
    ImageComponentRetained images[][];
    // maximum number of levels needed for the mipmapMode of this texture
    int	     maxLevels = 0;
    // maximum number of mipmap levels that can be defined for this texture
    private int	     maxMipMapLevels = 0;

    int 	numFaces = 1;		// For CubeMap, it is 6
    int		baseLevel = 0;
    int		maximumLevel = 0;
    float	minimumLod = -1000.0f;
    float	maximumLod = 1000.0f;
    Point3f	lodOffset = null;

    private boolean useAsRaster = false; // true if used by Raster or Background.

    // Texture mapping enable switch
    // This enable is derived from the user specified enable
    // and whether the buf image in the imagecomp is null
    boolean	enable = true;

    // User specified enable
    boolean userSpecifiedEnable = true;


    // true if alpha channel need update during rendering
    boolean isAlphaNeedUpdate = false;

    // sharpen texture info
    int numSharpenTextureFuncPts = 0;
    float sharpenTextureFuncPts[] = null;  // array of pairs of floats
					   // first value for LOD
					   // second value for the fcn value

    // filter4 info
    float filter4FuncPts[] = null;

    // anisotropic filter info
    int anisotropicFilterMode = Texture.ANISOTROPIC_NONE;
    float anisotropicFilterDegree = 1.0f;


    // Each bit corresponds to a unique renderer if shared context
    // or a unique canvas otherwise.
    // This mask specifies which renderer/canvas has loaded the
    // texture images. 0 means no renderer/canvas has loaded the texture.
    // 1 at the particular bit means that renderer/canvas has loaded the
    // texture. 0 means otherwise.
    int resourceCreationMask = 0x0;

    // Each bit corresponds to a unique renderer if shared context
    // or a unique canvas otherwise
    // This mask specifies if texture images are up-to-date.
    // 0 at a particular bit means texture images are not up-to-date.
    // 1 means otherwise. If it specifies 0, then it needs to go
    // through the imageUpdateInfo to update the images accordingly.
    //
    int resourceUpdatedMask = 0x0;

    // Each bit corresponds to a unique renderer if shared context
    // or a unique canvas otherwise
    // This mask specifies if texture lod info is up-to-date.
    // 0 at a particular bit means texture lod info is not up-to-date.
    // 1 means otherwise.
    //
    int resourceLodUpdatedMask = 0x0;

    // Each bit corresponds to a unique renderer if shared context
    // or a unique canvas otherwise
    // This mask specifies if texture is in the resource reload list
    // 0 at a particular bit means texture is not in reload list
    // 1 means otherwise.
    //
    int resourceInReloadList = 0x0;

    // image update info
    ArrayList imageUpdateInfo[][];


    int imageUpdatePruneMask[];

    // Issue 357 - we need a separate reference counter per RenderBin, since
    // each RenderBin keeps an independent list of Texture objects to be freed.
    // Since this is accessed infrequently, we will use a HashMap for the
    // textureBin reference counter
    private HashMap<RenderBin,Integer> textureBinRefCount =
            new HashMap<RenderBin,Integer>();

    // need to synchronize access from multiple rendering threads
    Object resourceLock = new Object();

    private static boolean isPowerOfTwo(int val) {
        return ((val & (val - 1)) == 0);
    }

    void initialize(int	format, int width, int widLevels,
			int height, int heiLevels, int mipmapMode,
			int boundaryWidth) {

	this.mipmapMode = mipmapMode;
	this.format = format;
	this.width = width;
	this.height = height;
	this.boundaryWidth = boundaryWidth;

        if(!isPowerOfTwo(width) || !isPowerOfTwo(height)) {
            this.widthOrHeightIsNPOT = true;
        }

	// determine the maximum number of mipmap levels that can be
	// defined from the specified dimension

        if (widLevels > heiLevels) {
            maxMipMapLevels = widLevels + 1;
        } else {
            maxMipMapLevels = heiLevels + 1;
	}


	// determine the maximum number of mipmap levels that will be
	// needed with the current mipmapMode

        if (mipmapMode != Texture.BASE_LEVEL) {
	    baseLevel = 0;
	    maximumLevel = maxMipMapLevels - 1;
	    maxLevels = maxMipMapLevels;
        } else {
	    baseLevel = 0;
	    maximumLevel = 0;
	    maxLevels = 1;
	}

        images = new ImageComponentRetained[numFaces][maxLevels];

	for (int j = 0; j < numFaces; j++) {
	    for (int i = 0; i < maxLevels; i++) {
	       images[j][i] = null;
	    }
	}
    }

    final int getFormat() {
	return this.format;
    }

    final int getWidth() {
   	return this.width;
    }

    final int getHeight() {
	return this.height;
    }

    final int numMipMapLevels() {
	return (maximumLevel - baseLevel + 1);
    }

    /**
     * Sets the boundary mode for the S coordinate in this texture object.
     * @param boundaryModeS the boundary mode for the S coordinate,
     * one of: CLAMP or WRAP.
     * @exception RestrictedAccessException if the method is called
     * when this object is part of live or compiled scene graph.
     */
    final void initBoundaryModeS(int boundaryModeS) {
	this.boundaryModeS = boundaryModeS;
    }

    /**
     * Retrieves the boundary mode for the S coordinate.
     * @return the current boundary mode for the S coordinate.
     * @exception RestrictedAccessException if the method is called
     * when this object is part of live or compiled scene graph.
     */
    final int getBoundaryModeS() {
	return  boundaryModeS;
    }

    /**
     * Sets the boundary mode for the T coordinate in this texture object.
     * @param boundaryModeT the boundary mode for the T coordinate,
     * one of: CLAMP or WRAP.
     * @exception RestrictedAccessException if the method is called
     * when this object is part of live or compiled scene graph.
     */
    final void initBoundaryModeT(int boundaryModeT) {
	this.boundaryModeT = boundaryModeT;
    }

    /**
     * Retrieves the boundary mode for the T coordinate.
     * @return the current boundary mode for the T coordinate.
     * @exception RestrictedAccessException if the method is called
     * when this object is part of live or compiled scene graph.
     */
    final int getBoundaryModeT() {
	return  boundaryModeT;
    }

    /**
     * Retrieves the boundary width.
     * @return the boundary width of this texture.
     */
    final int getBoundaryWidth() {
	return  boundaryWidth;
    }

    /**
     * Sets the minification filter function.  This
     * function is used when the pixel being rendered maps to an area
     * greater than one texel.
     * @param minFilter the minification filter, one of:
     * FASTEST, NICEST, BASE_LEVEL_POINT, BASE_LEVEL_LINEAR,
     * MULTI_LEVEL_POINT, MULTI_LEVEL_LINEAR.
     * @exception RestrictedAccessException if the method is called
     * when this object is part of live or compiled scene graph.
     */
    final void initMinFilter(int minFilter) {
	this.minFilter = minFilter;
    }

    /**
     * Retrieves the minification filter.
     * @return the current minification filter function.
     * @exception RestrictedAccessException if the method is called
     * when this object is part of live or compiled scene graph.
     */
    final int getMinFilter() {
	return  minFilter;
    }

    /**
     * Sets the magnification filter function.  This
     * function is used when the pixel being rendered maps to an area
     * less than or equal to one texel.
     * @param magFilter the magnification filter, one of:
     * FASTEST, NICEST, BASE_LEVEL_POINT, or BASE_LEVEL_LINEAR.
     * @exception RestrictedAccessException if the method is called
     * when this object is part of live or compiled scene graph.
     */
    final void initMagFilter(int magFilter) {
	this.magFilter = magFilter;
    }

    /**
     * Retrieves the magnification filter.
     * @return the current magnification filter function.
     * @exception RestrictedAccessException if the method is called
     * when this object is part of live or compiled scene graph.
     */
    final int getMagFilter() {
	return  magFilter;
    }

    /**
     * Sets a specified mipmap level.
     * @param level mipmap level to set: 0 is the base level
     * @param image pixel array object containing the texture image
     * @exception RestrictedAccessException if the method is called
     * when this object is part of live or compiled scene graph.
     * @exception IllegalArgumentException if an ImageComponent3D
     * is used in a Texture2D or ImageComponent2D in Texture3D
     * power of 2 OR invalid format/mipmapMode is specified.
     */
    void initImage(int level, ImageComponent image) {

        // Issue 172 : call checkImageSize even for non-live setImage calls
        checkImageSize(level, image);

	if (this.images == null) {
           throw new IllegalArgumentException(J3dI18N.getString("TextureRetained0"));
	}

        if (this.source instanceof Texture2D) {
            if (image instanceof ImageComponent3D)
               throw new IllegalArgumentException(J3dI18N.getString("Texture8"))
;
        } else {

            if (image instanceof ImageComponent2D)
               throw new IllegalArgumentException(J3dI18N.getString("Texture14")
);
        }


	if (this.source.isLive()) {

	    if (this.images[0][level] != null) {
		this.images[0][level].clearLive(refCount);
	    }


	    if (image != null) {
		((ImageComponentRetained)image.retained).setLive(inBackgroundGroup, refCount);
	    }
	}

	if (image != null) {
	    this.images[0][level] = (ImageComponentRetained)image.retained;

	} else {
	    this.images[0][level] = null;
	}
    }

    final void checkImageSize(int level, ImageComponent image) {
        if (image != null) {
	    int imgWidth  = ((ImageComponentRetained)image.retained).width;
            int imgHeight = ((ImageComponentRetained)image.retained).height;

            int wdh = width;
	    int hgt = height;
	    for (int i = 0; i < level; i++) {
                wdh >>= 1;
                hgt >>= 1;
            }

	    if (wdh < 1) wdh = 1;
	    if (hgt < 1) hgt = 1;

	    if ((wdh != (imgWidth - 2*boundaryWidth)) ||
                    (hgt != (imgHeight - 2*boundaryWidth))) {
	       throw new IllegalArgumentException(
				J3dI18N.getString("TextureRetained1"));
	    }
        }
    }

    final void checkSizes(ImageComponentRetained images[]) {
        // Issue 172 : this method is now redundant

        // Assertion check that the image at each level is the correct size
        // This shouldn't be needed since we already should have checked the
        // size at each level, and verified that all levels are set.
        if (images != null) {
            int hgt = height;
            int wdh = width;
            for (int level = 0; level < images.length; level++) {
                int imgWidth  = images[level].width;
                int imgHeight = images[level].height;

                assert (wdh == (imgWidth - 2*boundaryWidth)) &&
                       (hgt == (imgHeight - 2*boundaryWidth));

                wdh /= 2;
                hgt /= 2;
                if (wdh < 1) wdh = 1;
                if (hgt < 1) hgt = 1;
            }
        }
    }

    final void setImage(int level, ImageComponent image) {
        initImage(level, image);

        Object arg[] = new Object[3];
	arg[0] = new Integer(level);
	arg[1] = image;
        arg[2] = new Integer(0);
	sendMessage(IMAGE_CHANGED, arg);

	// If the user has set enable to true, then if the image is null
	// turn off texture enable

	if (userSpecifiedEnable) {
	    enable = userSpecifiedEnable;
            if (image != null && level >= baseLevel && level <= maximumLevel) {
		ImageComponentRetained img= (ImageComponentRetained)image.retained;
		if (img.isByReference()) {
		    if (img.getRefImage(0) == null) {
			enable = false;
		    }
		}
		else {
		    if (img.getImageData(isUseAsRaster()).get() == null) {
			enable = false;
		    }
		}
		if (!enable)
		    sendMessage(ENABLE_CHANGED, Boolean.FALSE);
	    }
	}
    }

    void initImages(ImageComponent[] images) {

	if (images.length != maxLevels)
            throw new IllegalArgumentException(J3dI18N.getString("Texture20"));

	for (int i = 0; i < images.length; i++) {
	     initImage(i, images[i]);
	}
    }

    final void setImages(ImageComponent[] images) {

        int i;

        initImages(images);

	ImageComponent [] imgs = new ImageComponent[images.length];
	for (i = 0; i < images.length; i++) {
	     imgs[i] = images[i];
	}

        Object arg[] = new Object[2];
	arg[0] = imgs;
	arg[1] = new Integer(0);

	sendMessage(IMAGES_CHANGED, arg);

	// If the user has set enable to true, then if the image is null
	// turn off texture enable

	if (userSpecifiedEnable) {
	    enable = userSpecifiedEnable;
	    for (i = baseLevel; i <= maximumLevel && enable; i++) {
		if (images[i] != null) {
		    ImageComponentRetained img=
			(ImageComponentRetained)images[i].retained;
		    if (img.isByReference()) {
			if (img.getRefImage(0) == null) {
			    enable = false;
			}
		    }
		    else {
			if (img.getImageData(isUseAsRaster()).get() == null) {
			    enable = false;
			}
		    }
		}
	    }
	    if (!enable) {
		sendMessage(ENABLE_CHANGED, Boolean.FALSE);
	    }
	}
    }




    /**
     * Gets a specified mipmap level.
     * @param level mipmap level to get: 0 is the base level
     * @return the pixel array object containing the texture image
     * @exception RestrictedAccessException if the method is called
     * when this object is part of live or compiled scene graph.
     */
    final ImageComponent getImage(int level) {
	return  (((images != null) && (images[0][level] != null)) ?
		 (ImageComponent)images[0][level].source : null);
    }

    final ImageComponent[] getImages() {
	if (images == null)
	    return null;

	ImageComponent [] rImages = new ImageComponent[images[0].length];
	for (int i = 0; i < images[0].length; i++) {
	    if (images[0][i] != null)
	        rImages[i] = (ImageComponent)images[0][i].source;
	    else
		rImages[i] = null;
	}
	return rImages;
    }

    /**
     * Sets mipmap mode for texture mapping for this texture object.
     * @param mipMapMode the new mipmap mode for this object.  One of:
     * BASE_LEVEL or MULTI_LEVEL_MIPMAP.
     * @exception RestrictedAccessException if the method is called
     */
    final void initMipMapMode(int mipmapMode) {

	if (this.mipmapMode == mipmapMode)
	    return;


	int prevMaxLevels = maxLevels;		// previous maxLevels

	this.mipmapMode = mipmapMode;

        if (mipmapMode != Texture.BASE_LEVEL) {
	    maxLevels = maxMipMapLevels;
        } else {
            baseLevel = 0;
            maximumLevel = 0;
	    maxLevels = 1;
        }


	ImageComponentRetained[][] newImages =
			new ImageComponentRetained[numFaces][maxLevels];

	if (prevMaxLevels < maxLevels) {
	    for (int f = 0; f < numFaces; f++) {
	        for (int i = 0; i < prevMaxLevels; i++) {
		     newImages[f][i] = images[f][i];
		}

	        for (int j = prevMaxLevels; j < maxLevels; j++) {
		     newImages[f][j] = null;
		}
	    }
	} else {
	    for (int f = 0; f < numFaces; f++) {
	    	for (int i = 0; i < maxLevels; i++)
		     newImages[f][i] = images[f][i];
	    }
	}
	images = newImages;
    }

    /**
     * Retrieves current mipmap mode.
     * @return current mipmap mode of this texture object.
     * @exception RestrictedAccessException if the method is called
     */
    final int getMipMapMode() {
	return this.mipmapMode;
    }

    /**
     * Enables or disables texture mapping for this
     * appearance component object.
     * @param state true or false to enable or disable texture mapping
     */
    final void initEnable(boolean state) {
	userSpecifiedEnable = state;
    }

    /**
     * Enables or disables texture mapping for this
     * appearance component object and sends a
     * message notifying the interested structures of the change.
     * @param state true or false to enable or disable texture mapping
     */
    final void setEnable(boolean state) {

	initEnable(state);

	if (state == enable) {
	    // if enable flag is same as user specified one
            // this is only possible when enable is false
	    // because one of the images is null and user specifies false
	    return;
	}

	enable = state;

	for (int j = 0; j < numFaces && enable; j++) {
	     for (int i = baseLevel; i <= maximumLevel && enable; i++) {
	    	  if (images[j][i].isByReference()) {
		      if (images[j][i].getRefImage(0) == null) {
		          enable = false;
		      }
                  } else {
		      if (images[j][i].getImageData(isUseAsRaster()).get() == null) {
		          enable = false;
		      }
	          }
	     }
	}
	sendMessage(ENABLE_CHANGED, (enable ? Boolean.TRUE: Boolean.FALSE));
    }

    /**
     * Retrieves the state of the texture enable flag.
     * @return true if texture mapping is enabled,
     * false if texture mapping is disabled
     */
    final boolean getEnable() {
	return userSpecifiedEnable;
    }


    final void initBaseLevel(int level) {
	if ((level < 0) || (level > maximumLevel)) {
	    throw new IllegalArgumentException(
			J3dI18N.getString("Texture36"));
	}
	baseLevel = level;
    }


    final void setBaseLevel(int level) {

	if (level == baseLevel)
	    return;

	initBaseLevel(level);
	sendMessage(BASE_LEVEL_CHANGED, new Integer(level));
    }

    final int getBaseLevel() {
	return baseLevel;
    }


    final void initMaximumLevel(int level) {
        if ((level < baseLevel) || (level >=  maxMipMapLevels)) {
            throw new IllegalArgumentException(
                    J3dI18N.getString("Texture37"));
        }
        if((mipmapMode == Texture.BASE_LEVEL) && (level != 0)) {
            throw new IllegalArgumentException(
                    J3dI18N.getString("Texture48"));
        }

        maximumLevel = level;
    }

    final void setMaximumLevel(int level) {

	if (level == maximumLevel)
	    return;

	initMaximumLevel(level);
	sendMessage(MAX_LEVEL_CHANGED, new Integer(level));
    }

    final int getMaximumLevel() {
   	return maximumLevel;
    }

    final void initMinimumLOD(float lod) {
	if (lod > maximumLod) {
	    throw new IllegalArgumentException(
			J3dI18N.getString("Texture42"));
	}
	minimumLod = lod;
    }

    final void setMinimumLOD(float lod) {
	initMinimumLOD(lod);
	sendMessage(MIN_LOD_CHANGED, new Float(lod));
    }

    final float getMinimumLOD() {
  	return minimumLod;
    }


    final void initMaximumLOD(float lod) {
	if (lod < minimumLod) {
	    throw new IllegalArgumentException(
			J3dI18N.getString("Texture42"));
	}
	maximumLod = lod;
    }

    final void setMaximumLOD(float lod) {
	initMaximumLOD(lod);
	sendMessage(MAX_LOD_CHANGED, new Float(lod));
    }

    final float getMaximumLOD() {
  	return maximumLod;
    }


    final void initLodOffset(float s, float t, float r) {
	if (lodOffset == null) {
	    lodOffset = new Point3f(s, t, r);
	} else {
	    lodOffset.set(s, t, r);
	}
    }

    final void setLodOffset(float s, float t, float r) {
	initLodOffset(s, t, r);
	sendMessage(LOD_OFFSET_CHANGED, new Point3f(s, t, r));
    }

    final void getLodOffset(Tuple3f offset) {
	if (lodOffset == null) {
	    offset.set(0.0f, 0.0f, 0.0f);
	} else {
	    offset.set(lodOffset);
	}
    }


    /**
     * Sets the texture boundary color for this texture object.  The
     * texture boundary color is used when boundaryModeS or boundaryModeT
     * is set to CLAMP.
     * @param boundaryColor the new texture boundary color.
     */
    final void initBoundaryColor(Color4f boundaryColor) {
	this.boundaryColor.set(boundaryColor);
    }

    /**
     * Sets the texture boundary color for this texture object.  The
     * texture boundary color is used when boundaryModeS or boundaryModeT
     * is set to CLAMP.
     * @param r the red component of the color.
     * @param g the green component of the color.
     * @param b the blue component of the color.
     * @param a the alpha component of the color.
     */
    final void initBoundaryColor(float r, float g, float b, float a) {
	this.boundaryColor.set(r, g, b, a);
    }

    /**
     * Retrieves the texture boundary color for this texture object.
     * @param boundaryColor the vector that will receive the
     * current texture boundary color.
     */
    final void getBoundaryColor(Color4f boundaryColor) {
	boundaryColor.set(this.boundaryColor);
    }


    /**
     * Set Anisotropic Filter
     */
    final void initAnisotropicFilterMode(int mode) {
	anisotropicFilterMode = mode;
    }

    final int getAnisotropicFilterMode() {
	return anisotropicFilterMode;
    }

    final void initAnisotropicFilterDegree(float degree) {
	anisotropicFilterDegree = degree;
    }

    final float getAnisotropicFilterDegree() {
	return anisotropicFilterDegree;
    }

    /**
     * Set Sharpen Texture function
     */
    final void initSharpenTextureFunc(float[] lod, float[] pts) {
	if (lod == null) {  // pts will be null too.
	    sharpenTextureFuncPts = null;
	    numSharpenTextureFuncPts = 0;
	} else {
	    numSharpenTextureFuncPts = lod.length;
	    if ((sharpenTextureFuncPts == null) ||
		    (sharpenTextureFuncPts.length != lod.length * 2)) {
		sharpenTextureFuncPts = new float[lod.length * 2];
	    }
	    for (int i = 0, j = 0; i < lod.length; i++) {
		sharpenTextureFuncPts[j++] = lod[i];
		sharpenTextureFuncPts[j++] = pts[i];
	    }
	}
    }

    final void initSharpenTextureFunc(Point2f[] pts) {
	if (pts == null) {
	    sharpenTextureFuncPts = null;
	    numSharpenTextureFuncPts = 0;
	} else {
	    numSharpenTextureFuncPts = pts.length;
	    if ((sharpenTextureFuncPts == null) ||
		    (sharpenTextureFuncPts.length != pts.length * 2)) {
		sharpenTextureFuncPts = new float[pts.length * 2];
	    }
	    for (int i = 0, j = 0; i < pts.length; i++) {
		sharpenTextureFuncPts[j++] = pts[i].x;
		sharpenTextureFuncPts[j++] = pts[i].y;
	    }
	}
    }

    final void initSharpenTextureFunc(float[] pts) {
	if (pts == null) {
	    sharpenTextureFuncPts = null;
	    numSharpenTextureFuncPts = 0;
	} else {
	    numSharpenTextureFuncPts = pts.length / 2;
	    if ((sharpenTextureFuncPts == null) ||
		    (sharpenTextureFuncPts.length != pts.length)) {
		sharpenTextureFuncPts = new float[pts.length];
	    }
	    for (int i = 0; i < pts.length; i++) {
		sharpenTextureFuncPts[i] = pts[i];
	    }
	}
    }

    /**
     * Get number of points in the sharpen texture LOD function
     */
    final int getSharpenTextureFuncPointsCount() {
	return numSharpenTextureFuncPts;
    }


    /**
     * Copies the array of sharpen texture LOD function points into the
     * specified arrays
     */
    final void getSharpenTextureFunc(float[] lod, float[] pts) {
	if (sharpenTextureFuncPts != null) {
	    for (int i = 0, j = 0; i < numSharpenTextureFuncPts; i++) {
		lod[i] = sharpenTextureFuncPts[j++];
		pts[i] = sharpenTextureFuncPts[j++];
	    }
	}
    }

    final void getSharpenTextureFunc(Point2f[] pts) {
	if (sharpenTextureFuncPts != null) {
	    for (int i = 0, j = 0; i < numSharpenTextureFuncPts; i++) {
		pts[i].x = sharpenTextureFuncPts[j++];
		pts[i].y = sharpenTextureFuncPts[j++]; } }
    }


    final void initFilter4Func(float[] weights) {
        if (weights == null) {
            filter4FuncPts = null;
        } else {
            if ((filter4FuncPts == null) ||
                    (filter4FuncPts.length != weights.length)) {
                filter4FuncPts = new float[weights.length];
            }
            for (int i = 0; i < weights.length; i++) {
                filter4FuncPts[i] = weights[i];
            }
        }
    }


    final int getFilter4FuncPointsCount() {
	if (filter4FuncPts == null) {
	    return 0;
	} else {
	    return filter4FuncPts.length;
	}
    }

    final void getFilter4Func(float[] weights) {
	if (filter4FuncPts != null) {
	    for (int i = 0; i < filter4FuncPts.length; i++) {
		weights[i] = filter4FuncPts[i];
	    }
	}
    }


    /**
     * internal method only -- returns internal function points
     */
    final float[] getSharpenTextureFunc() {
	return sharpenTextureFuncPts;
    }

    final float[] getFilter4Func(){
	return filter4FuncPts;
    }




    @Override
    void setLive(boolean backgroundGroup, int refCount) {

	// This line should be assigned before calling doSetLive, so that
	// the mirror object's enable is assigned correctly!
	enable = userSpecifiedEnable;

        super.doSetLive(backgroundGroup, refCount);

	// XXXX: for now, do setLive for all the defined images.
	// But in theory, we only need to setLive those within the
	// baseLevel and maximumLevel range. But then we'll need
	// setLive and clearLive image when the range changes.

	if (images != null) {

	    for (int j = 0; j < numFaces; j++) {
	  	 for (int i = 0; i < maxLevels; i++){
	    	      if (images[j][i] == null) {
              		  throw new IllegalArgumentException(
				J3dI18N.getString("TextureRetained3") + i);
		      }
	    	      images[j][i].setLive(backgroundGroup, refCount);
		 }
	    }
	}

        // Issue 172 : assertion check the sizes of the images after we have
        // checked for all mipmap levels being set
	if (images != null) {
	    for (int j = 0; j < numFaces; j++) {
                checkSizes(images[j]);
	    }
	}

        // Send a message to Rendering Attr stucture to update the resourceMask
        J3dMessage createMessage = new J3dMessage();
        createMessage.threads = J3dThread.UPDATE_RENDERING_ATTRIBUTES;
        createMessage.type = J3dMessage.TEXTURE_CHANGED;
        createMessage.args[0] = this;
        createMessage.args[1]= new Integer(UPDATE_IMAGE);
	createMessage.args[2] = null;
	createMessage.args[3] = new Integer(changedFrequent);
        VirtualUniverse.mc.processMessage(createMessage);

	// If the user has set enable to true, then if the image is null
	// turn off texture enable
	if (userSpecifiedEnable) {
	    if (images != null) {
	        for (int j = 0; j < numFaces && enable; j++) {
	  	    for (int i = baseLevel; i <= maximumLevel && enable; i++){
		        if (images[j][i].isByReference()) {
		            if (images[j][i].getRefImage(0) == null) {
			        enable = false;
 			    }
		        } else {
		            if (images[j][i].getImageData(isUseAsRaster()).get() == null) {
			        enable = false;
		            }
		        }
		    }
		}
	    } else {
		enable = false;
	    }
	    if (!enable)
		sendMessage(ENABLE_CHANGED, Boolean.FALSE);
	}

	super.markAsLive();
    }

    @Override
    void clearLive(int refCount) {
	super.clearLive(refCount);

	if (images != null) {
	    for (int j = 0; j < numFaces; j++) {
	         for (int i = 0; i < maxLevels; i++) {
	              images[j][i].clearLive(refCount);
	              images[j][i].removeUser(mirror);
		 }
	    }
	}
    }

    /*
     * The following methods update the native context.
     * The implementation for Texture2D happens here.
     * Texture3D and TextureCubeMap implement their own versions.
     */

    void bindTexture(Context ctx, int objectId, boolean enable) {
        Pipeline.getPipeline().bindTexture2D(ctx, objectId, enable);
    }

    void updateTextureBoundary(Context ctx,
            int boundaryModeS, int boundaryModeT,
            float boundaryRed, float boundaryGreen,
            float boundaryBlue, float boundaryAlpha) {

        Pipeline.getPipeline().updateTexture2DBoundary(ctx,
                boundaryModeS, boundaryModeT,
                boundaryRed, boundaryGreen,
                boundaryBlue, boundaryAlpha);
    }

    void updateTextureFilterModes(Context ctx,
            int minFilter, int magFilter) {

        Pipeline.getPipeline().updateTexture2DFilterModes(ctx,
                minFilter, magFilter);
    }

    void updateTextureSharpenFunc(Context ctx,
            int numSharpenTextureFuncPts,
            float[] sharpenTextureFuncPts) {

        Pipeline.getPipeline().updateTexture2DSharpenFunc(ctx,
            numSharpenTextureFuncPts, sharpenTextureFuncPts);
    }

    void updateTextureFilter4Func(Context ctx,
            int numFilter4FuncPts,
            float[] filter4FuncPts) {

        Pipeline.getPipeline().updateTexture2DFilter4Func(ctx,
                numFilter4FuncPts, filter4FuncPts);
    }

    void updateTextureAnisotropicFilter(Context ctx, float degree) {
        Pipeline.getPipeline().updateTexture2DAnisotropicFilter(ctx, degree);
    }

    void updateTextureLodRange(Context ctx,
            int baseLevel, int maximumLevel,
            float minimumLod, float maximumLod) {

        Pipeline.getPipeline().updateTexture2DLodRange(ctx, baseLevel, maximumLevel,
                minimumLod, maximumLod);
    }

    void updateTextureLodOffset(Context ctx,
            float lodOffsetX, float lodOffsetY,
            float lodOffsetZ) {

        Pipeline.getPipeline().updateTexture2DLodOffset(ctx,
                lodOffsetX, lodOffsetY, lodOffsetZ);
    }

    // free a Texture2D id
    void freeTextureId(int id) {
	synchronized (resourceLock) {
		if (objectId == id)
			objectId = -1;
	}
    }

    private boolean isEnabled(Canvas3D cv) {
        if(widthOrHeightIsNPOT && !isUseAsRaster() &&
                ((cv.textureExtendedFeatures & Canvas3D.TEXTURE_NON_POWER_OF_TWO ) == 0)) {
            return false;
        }
        return enable;
    }


    // bind a named texture to a texturing target
    void bindTexture(Canvas3D cv) {

        synchronized(resourceLock) {
		if (objectId == -1)
			objectId = Canvas3D.generateTexID(cv.ctx);
	    cv.addTextureResource(objectId, this);
 	}
	bindTexture(cv.ctx, objectId, isEnabled(cv));
    }


    /**
     * load level 0 explicitly with null pointer to enable
     * mipmapping when level 0 is not the base level
     */
    void updateTextureDimensions(Canvas3D cv) {
        if(images[0][0] != null) {
            updateTextureImage(cv, 0, maxLevels, 0,
                    format, images[0][0].getImageFormatTypeIntValue(false),
                    width, height, boundaryWidth,
                    images[0][0].getImageDataTypeIntValue(), null);
        }
    }


    void updateTextureLOD(Canvas3D cv) {

	if ((cv.textureExtendedFeatures & Canvas3D.TEXTURE_LOD_RANGE) != 0 ) {

            int max = 0;
            if( mipmapMode == Texture.BASE_LEVEL ) {
                max = maxMipMapLevels;
            }
            else {
                max = maximumLevel;
            }

            updateTextureLodRange(cv.ctx, baseLevel, max,
                    minimumLod, maximumLod);
	}

        if ((lodOffset != null) &&
                ((cv.textureExtendedFeatures & Canvas3D.TEXTURE_LOD_OFFSET) != 0)) {
            updateTextureLodOffset(cv.ctx,
                    lodOffset.x, lodOffset.y, lodOffset.z);
	}
    }


    void updateTextureBoundary(Canvas3D cv) {
	updateTextureBoundary(cv.ctx, boundaryModeS, boundaryModeT,
				boundaryColor.x, boundaryColor.y,
				boundaryColor.z, boundaryColor.w);
    }


    void updateTextureFields(Canvas3D cv) {

	int magnificationFilter = magFilter;
	int minificationFilter = minFilter;

	// update sharpen texture function if applicable

	if ((magnificationFilter >= Texture.LINEAR_SHARPEN) &&
		(magnificationFilter <= Texture.LINEAR_SHARPEN_ALPHA)) {

	    if ((cv.textureExtendedFeatures & Canvas3D.TEXTURE_SHARPEN) != 0 ) {

		// send down sharpen texture LOD function
		//
	        updateTextureSharpenFunc(cv.ctx,
			numSharpenTextureFuncPts, sharpenTextureFuncPts);

	    } else {

		// sharpen texture is not supported by the underlying
		// library, fallback to BASE_LEVEL_LINEAR

		magnificationFilter = Texture.BASE_LEVEL_LINEAR;
	    }
	} else if ((magnificationFilter >= Texture2D.LINEAR_DETAIL) &&
		(magnificationFilter <= Texture2D.LINEAR_DETAIL_ALPHA)) {
	    if ((cv.textureExtendedFeatures & Canvas3D.TEXTURE_DETAIL) == 0) {

		// detail texture is not supported by the underlying
		// library, fallback to BASE_LEVEL_LINEAR

		magnificationFilter = Texture.BASE_LEVEL_LINEAR;
	    }
	}

	if (minificationFilter == Texture.FILTER4 || magnificationFilter == Texture.FILTER4) {

	    boolean noFilter4 = false;

	    if ((cv.textureExtendedFeatures & Canvas3D.TEXTURE_FILTER4) != 0) {

		if (filter4FuncPts == null) {

		    // filter4 function is not defined,
		    // fallback to BASE_LEVEL_LINEAR

		    noFilter4 = true;
		} else {
	            updateTextureFilter4Func(cv.ctx, filter4FuncPts.length,
					filter4FuncPts);
		}
	    } else {

		// filter4 is not supported by the underlying
		// library, fallback to BASE_LEVEL_LINEAR

		noFilter4 = true;
	    }

	    if (noFilter4) {
		if (minificationFilter == Texture.FILTER4) {
		    minificationFilter = Texture.BASE_LEVEL_LINEAR;
		}
		if (magnificationFilter == Texture.FILTER4) {
		    magnificationFilter = Texture.BASE_LEVEL_LINEAR;
		}
	    }
	}

        // Fallback to BASE mode if hardware mipmap generation is not supported.
        if ((mipmapMode == Texture.BASE_LEVEL) && ((cv.textureExtendedFeatures &
                Canvas3D.TEXTURE_AUTO_MIPMAP_GENERATION) == 0)) {

            if (minificationFilter == Texture.NICEST ||
                    minificationFilter == Texture.MULTI_LEVEL_LINEAR) {
                minificationFilter = Texture.BASE_LEVEL_LINEAR;
            } else if (minificationFilter == Texture.MULTI_LEVEL_POINT) {
                minificationFilter = Texture.BASE_LEVEL_POINT;
            }
        }

	// update texture filtering modes
	updateTextureFilterModes(cv.ctx, minificationFilter,
						magnificationFilter);

	if ((cv.textureExtendedFeatures & Canvas3D.TEXTURE_ANISOTROPIC_FILTER)
			!= 0) {
	    if (anisotropicFilterMode == Texture.ANISOTROPIC_NONE) {
	        updateTextureAnisotropicFilter(cv.ctx, 1.0f);
	    } else {
	        updateTextureAnisotropicFilter(cv.ctx, anisotropicFilterDegree);
	    }
	}

	// update texture boundary modes, boundary color

	updateTextureBoundary(cv);

    }


    // Wrapper around the native call for 2D textures; overridden for
    // TextureCureMap
    void updateTextureImage(Canvas3D cv,
            int face, int numLevels, int level,
            int textureFormat, int imageFormat,
            int width, int height,
            int boundaryWidth,
            int imageDataType, Object data) {

        Pipeline.getPipeline().updateTexture2DImage(cv.ctx,
                numLevels, level,
                textureFormat, imageFormat,
                width, height, boundaryWidth,
                imageDataType, data, useAutoMipMapGeneration(cv));
    }

    // Wrapper around the native call for 2D textures; overridden for
    // TextureCureMap
    void updateTextureSubImage(Canvas3D cv,
            int face, int level,
            int xoffset, int yoffset,
            int textureFormat, int imageFormat,
            int imgXOffset, int imgYOffset,
            int tilew, int width, int height,
            int imageDataType, Object data) {

        Pipeline.getPipeline().updateTexture2DSubImage(cv.ctx,
                level, xoffset, yoffset,
                textureFormat, imageFormat,
                imgXOffset, imgYOffset,
                tilew, width, height,
                imageDataType, data, useAutoMipMapGeneration(cv));
    }


    /**
     * reloadTextureImage is used to load a particular level of image
     * This method needs to take care of RenderedImage as well as
     * BufferedImage
     */
    void reloadTextureImage(Canvas3D cv, int face, int level,
            ImageComponentRetained image, int numLevels) {

        boolean useAsRaster = isUseAsRaster();
        ImageComponentRetained.ImageData imageData = image.getImageData(useAsRaster);

        assert imageData != null;

        updateTextureImage(cv,
                face, numLevels, level,
                format, image.getImageFormatTypeIntValue(useAsRaster),
                imageData.getWidth(), imageData.getHeight(),
                boundaryWidth, image.getImageDataTypeIntValue(),
                imageData.get());


        // TODO : Dead code - need to clean up for 1.6
        // Now take care of the RenderedImage (byRef and yUp) case. Note, if image
        // is a RenderedImage ( byRef and yUp), then imageData will be null

        if (imageData == null) {
            //		    System.err.println("==========. subImage");
	    // Download all the tiles for this texture
	    int xoffset = 0, yoffset = 0;
	    int tmpw = image.width;
	    int tmph = image.height;
	    int endXTile = image.tilew;
	    int endYTile = image.tileh;
	    int curw = endXTile;
	    int curh = endYTile;

	    if (tmpw < curw) {
	        curw = tmpw;
	    }

	    if (tmph < curh) {
	        curh = tmph;
	    }

	    int startw = curw;
	    int imageXOffset = image.tilew - curw;
	    int imageYOffset = image.tileh - curh;
	    for (int m = 0; m < image.numYTiles; m++) {
	        xoffset = 0;
	        tmpw = width;
	        curw = startw;
	        imageXOffset = image.tilew - curw;
	        for (int n = 0; n < image.numXTiles; n++) {
		    java.awt.image.Raster ras;
		    ras = ((RenderedImage)image.getRefImage(0)).getTile(n,m);
		    byte[] data =  ((DataBufferByte)ras.getDataBuffer()).getData();
                    updateTextureSubImage(cv, face,
                            level, xoffset, yoffset, format,
                            image.getImageFormatTypeIntValue(false),
                            imageXOffset, imageYOffset,
                            image.tilew,
                            curw, curh,
                            ImageComponentRetained.IMAGE_DATA_TYPE_BYTE_ARRAY,
                            (Object) data);
	  	    xoffset += curw;
	  	    imageXOffset = 0;
		    tmpw -= curw;
		    if (tmpw < image.tilew)
		        curw = tmpw;
		    else
		        curw = image.tilew;
	        }
	        yoffset += curh;
	        imageYOffset = 0;
	        tmph -= curh;
	        if (tmph < image.tileh)
		    curh = tmph;
	        else
		    curh = image.tileh;
	    }
        }
    }


    /**
     * update a subregion of the texture image
     * This method needs to take care of RenderedImage as well as
     * BufferedImage
     */
    void reloadTextureSubImage(Canvas3D cv, int face, int level,
				ImageComponentUpdateInfo info,
				ImageComponentRetained image) {

	int x = info.x,
	    y = info.y,
	    width = info.width,
	    height = info.height;

        //The x and y here specifies the subregion of the imageData of
        //the associated RenderedImage.

	//System.err.println("\nupdateTextureSubImage: x= " + x + " y= " + y +
	//			" width= " + width + " height= " + height +
	//			" format= " + format);

        ImageComponentRetained.ImageData imageData = image.getImageData(isUseAsRaster());
        if(imageData != null) {
            int xoffset = x;
            int yoffset = y;

            // TODO Check this logic : If !yUp adjust yoffset
            if (!image.yUp) {
                yoffset = image.height - yoffset - height;
            }

            updateTextureSubImage(cv, face, level,
                    xoffset, yoffset,
                    format, image.getImageFormatTypeIntValue(false),
                    xoffset, yoffset,
                    image.width, width, height,
                    image.getImageDataTypeIntValue(),
                    imageData.get());

        } else {

            assert false;

            // TODO : Dead code - need to clean up for 1.6
	    // System.err.println("RenderedImage subImage update");
	    // determine the first tile of the image

            float mt;
	    int minTileX, minTileY;

	    int rx = x;
	    int ry = y;

            mt = (float)(rx) / (float)image.tilew;
            if (mt < 0) {
                minTileX = (int)(mt - 1);
            } else {
                minTileX = (int)mt;
            }

            mt = (float)(ry) / (float)image.tileh;
            if (mt < 0) {
                minTileY = (int)(mt - 1);
            } else {
                minTileY = (int)mt;
            }

	    // determine the pixel offset of the upper-left corner of the
	    // first tile
	    int startXTile = minTileX * image.tilew;
	    int startYTile = minTileY * image.tilew;


            // image dimension in the first tile

            int curw = (startXTile + image.tilew - rx);
            int curh = (startYTile + image.tileh - ry);

            // check if the to-be-copied region is less than the tile image
            // if so, update the to-be-copied dimension of this tile

            if (curw > width) {
                curw = width;
            }

            if (curh > height) {
                curh = height;
            }

            // save the to-be-copied width of the left most tile

            int startw = curw;


            // temporary variable for dimension of the to-be-copied region

            int tmpw = width;
            int tmph = height;


            // offset of the first pixel of the tile to be copied; offset is
            // relative to the upper left corner of the title

            int imgX = rx - startXTile;
            int imgY = ry - startYTile;


            // determine the number of tiles in each direction that the
            // image spans

            int numXTiles = (width + imgX) / image.tilew;
            int numYTiles = (height + imgY) / image.tileh;

            if (((float)(width + imgX ) % (float)image.tilew) > 0) {
                numXTiles += 1;
            }

            if (((float)(height + imgY ) % (float)image.tileh) > 0) {
                numYTiles += 1;
            }

	    java.awt.image.Raster ras;

	    int textureX = x; 	// x offset in the texture
	    int textureY = y; 	// y offset in the texture

	    for (int yTile = minTileY; yTile < minTileY + numYTiles;
			yTile++) {

		tmpw = width;
		curw = startw;
		imgX = rx - startXTile;

		for (int xTile = minTileX; xTile < minTileX + numXTiles;
			xTile++) {
		    ras = ((RenderedImage)image.getRefImage(0)).getTile(xTile, yTile);
		    byte[] data = ((DataBufferByte)ras.getDataBuffer()).getData();

                    updateTextureSubImage(cv, face, level,
                            textureX, textureY,
                            format, image.getImageFormatTypeIntValue(false),
                            imgX, imgY,
                            image.tilew, curw, curh,
                            ImageComponentRetained.IMAGE_DATA_TYPE_BYTE_ARRAY,
                            (Object)data);

                    // move to the next tile in x direction

		    textureX += curw;
		    imgX = 0;

                    // determine the width of copy region of the next tile

		    tmpw -= curw;
		    if (tmpw < image.tilew) {
			curw = tmpw;
		    } else {
			curw = image.tilew;
		    }
	   	}

                // move to the next set of tiles in y direction
		textureY += curh;
                imgY = 0;

                // determine the height of copy region for the next set
                // of tiles
                tmph -= curh;
                if (tmph < image.tileh) {
                    curh = tmph;
                } else {
                    curh = image.tileh;
                }
	    }
	}
    }


    // reload texture mipmap

    void reloadTexture(Canvas3D cv) {


	int blevel, mlevel;

	//System.err.println("reloadTexture: baseLevel= " + baseLevel +
	//			" maximumLevel= " + maximumLevel);

	if ((cv.textureExtendedFeatures & Canvas3D.TEXTURE_LOD_RANGE) == 0 ) {
	    blevel = 0;
	    mlevel = maxLevels - 1;
        } else {
	    blevel = baseLevel;
	    mlevel = maximumLevel;
	}

	if (blevel != 0) {
	    // level 0 is not the base level, hence, need
            // to load level 0 explicitly with a null pointer in order
	    // for mipmapping to be active.

	    updateTextureDimensions(cv);
	}

	for (int j = 0; j < numFaces; j++) {
	    for (int i = blevel; i <= mlevel; i++) {

		// it is possible to have null pointer if only a subset
                // of mipmap levels is defined but the canvas does not
 		// support lod_range extension

                ImageComponentRetained image = images[j][i];
		if (image != null) {
                    // Issue 366: call evaluateExtensions, since it may not
                    // have been called yet in all cases
                    image.evaluateExtensions(cv);
		    reloadTextureImage(cv, j, i, image, maxLevels);
		}
	    }
        }
    }


    // update texture mipmap based on the imageUpdateInfo

    void updateTexture(Canvas3D cv, int resourceBit) {

	//System.err.println("updateTexture\n");

	ImageComponentUpdateInfo info;

	for (int k = 0; k < numFaces; k++) {
	    for (int i = baseLevel; i <= maximumLevel; i++) {
		if (imageUpdateInfo[k][i] != null) {
		    for (int j = 0; j < imageUpdateInfo[k][i].size(); j++) {

			info = (ImageComponentUpdateInfo)
					imageUpdateInfo[k][i].get(j);


	    	        synchronized(resourceLock) {

			    // if this info is updated, move on to the next one

			    if ((info.updateMask & resourceBit) == 0)
			        continue;


			    // check if all canvases have processed this update
			    info.updateMask &= ~resourceBit;

			    // all the current resources have updated this
			    // info, so this info can be removed from the
			    // update list
			    if ((info.updateMask & resourceCreationMask)
					== 0) {
				info.updateMask = 0; // mark this update as
						     // done

			 	// mark the prune flag so as to prune the
				// update list next time when the update
				// list is to be modified.
				// Don't want to clean up the list at
				// rendering time because (1) MT issue,
				// other renderer could be processing
				// the update list now;
				// (2) takes up rendering time.
				if (imageUpdatePruneMask == null) {
				    imageUpdatePruneMask = new int[numFaces];
				}
				imageUpdatePruneMask[k] = 1 << i;
			    }
			}

			if (info.entireImage == true) {
			    reloadTextureImage(cv, k, i,
						images[k][i], maxLevels);
			} else {
			    reloadTextureSubImage(cv, k, i, info, images[k][i]);
			}

		    }
		}
	    }
	}
    }


    /**
     * reloadTextureSharedContext is called to reload texture
     * on a shared context. It is invoked from the Renderer
     * before traversing the RenderBin. The idea is to reload
     * all necessary textures up front for all shared contexts
     * in order to minimize the context switching overhead.
     */
    void reloadTextureSharedContext(Canvas3D cv) {

	// if texture is not enabled, don't bother downloading the
	// the texture state

	if (!isEnabled(cv)) {
	    return;
	}

	bindTexture(cv);

	// reload all levels of texture image

	// update texture parameters such as boundary modes, filtering

	updateTextureFields(cv);


	// update texture Lod parameters

	updateTextureLOD(cv);


	// update all texture images

	reloadTexture(cv);

        synchronized(resourceLock) {
	    resourceCreationMask |= cv.screen.renderer.rendererBit;
	    resourceUpdatedMask |= cv.screen.renderer.rendererBit;
	    resourceLodUpdatedMask |= cv.screen.renderer.rendererBit;
	    resourceInReloadList &= ~cv.screen.renderer.rendererBit;
	}
    }


    /**
     * updateNative is called while traversing the RenderBin to
     * update the texture state
     */
    void updateNative(Canvas3D cv) {
	boolean reloadTexture = false; // true - reload all levels of texture
	boolean updateTexture = false; // true - update a portion of texture
	boolean updateTextureLod = false; // true - update texture Lod info

        //System.err.println("Texture/updateNative: " + this + "object= " + objectId + " enable= " + enable);

	bindTexture(cv);

	// if texture is not enabled, don't bother downloading the
	// the texture state

	if (!isEnabled(cv)) {
	    return;
	}

        if (cv.useSharedCtx && cv.screen.renderer.sharedCtx != null) {

            if ((resourceCreationMask & cv.screen.renderer.rendererBit) == 0) {
		reloadTexture = true;
	    } else {
	        if (((resourceUpdatedMask &
		      cv.screen.renderer.rendererBit) == 0) &&
		    (imageUpdateInfo != null)) {
		    updateTexture = true;
		}

		if ((resourceLodUpdatedMask &
				cv.screen.renderer.rendererBit) == 0) {
		    updateTextureLod = true;
		}
	    }
	    if (reloadTexture || updateTexture || updateTextureLod) {
		cv.makeCtxCurrent(cv.screen.renderer.sharedCtx);
	        bindTexture(cv);
	    }
	} else {
            if ((resourceCreationMask & cv.canvasBit) == 0) {
		reloadTexture = true;
	    } else {
	  	if (((resourceUpdatedMask & cv.canvasBit) == 0) &&
			(imageUpdateInfo != null)) {
		    updateTexture = true;
		}

		if ((resourceLodUpdatedMask & cv.canvasBit) == 0) {
		    updateTextureLod = true;
		}
	    }
	}

//System.err.println("......... reloadTexture= " + reloadTexture +
//		 " updateTexture= " + updateTexture +
//		 " updateTextureLod= " + updateTextureLod);

//System.err.println("......... resourceCreationMask= " + resourceCreationMask +
//		   " resourceUpdatedMask= " + resourceUpdatedMask);

	if (reloadTexture) {

	    // reload all levels of texture image

	    // update texture parameters such as boundary modes, filtering

	    updateTextureFields(cv);


	    // update texture Lod parameters

	    updateTextureLOD(cv);


	    // update all texture images

	    reloadTexture(cv);


	    if (cv.useSharedCtx) {
		cv.makeCtxCurrent(cv.ctx);
                synchronized(resourceLock) {
		    resourceCreationMask |= cv.screen.renderer.rendererBit;
		    resourceUpdatedMask |= cv.screen.renderer.rendererBit;
		    resourceLodUpdatedMask |= cv.screen.renderer.rendererBit;
	 	}
	    }
	    else {
                synchronized(resourceLock) {
                    resourceCreationMask |= cv.canvasBit;
                    resourceUpdatedMask |= cv.canvasBit;
                    resourceLodUpdatedMask |= cv.canvasBit;
	 	}
	    }
	} else if (updateTextureLod || updateTexture) {

	    if (updateTextureLod) {
		updateTextureLOD(cv);
	    }

	    if (updateTexture) {

	        // update texture image

	        int resourceBit = 0;

	        if (cv.useSharedCtx) {
		    resourceBit = cv.screen.renderer.rendererBit;
	        } else {
		    resourceBit = cv.canvasBit;
	        }

	        // update texture based on the imageComponent update info

	        updateTexture(cv, resourceBit);
	    }

	    // set the appropriate bit in the resource update masks showing
            // that the resource is up-to-date

	    if (cv.useSharedCtx) {
		cv.makeCtxCurrent(cv.ctx);
                synchronized(resourceLock) {
		    resourceUpdatedMask |= cv.screen.renderer.rendererBit;
		    resourceLodUpdatedMask |= cv.screen.renderer.rendererBit;
	 	}
	    } else {
                synchronized(resourceLock) {
		    resourceUpdatedMask |= cv.canvasBit;
		    resourceLodUpdatedMask |= cv.canvasBit;
	 	}
	    }
	}
    }

    @Override
    synchronized void createMirrorObject() {
       if (mirror == null) {
	   if (this instanceof Texture3DRetained) {
		Texture3DRetained t3d = (Texture3DRetained)this;
		Texture3D tex = new Texture3D(t3d.mipmapMode,
						 t3d.format,
						 t3d.width,
						 t3d.height,
						 t3d.depth,
						 t3d.boundaryWidth);
		mirror = (Texture3DRetained)tex.retained;;

	   } else if (this instanceof TextureCubeMapRetained) {
		TextureCubeMap tex = new TextureCubeMap(mipmapMode,
						format, width,
						boundaryWidth);
		mirror = (TextureCubeMapRetained)tex.retained;

	   } else {
		Texture2D tex = new Texture2D(mipmapMode,
						 format,
						 width,
						 height,
						 boundaryWidth);
		mirror = (Texture2DRetained)tex.retained;
	   }

	   ((TextureRetained)mirror).objectId = -1;
       }
       initMirrorObject();
    }


    /**
     * Initializes a mirror object, point the mirror object to the retained
     * object if the object is not editable
     */
    @Override
    synchronized void initMirrorObject() {
	mirror.source = source;
	if (this instanceof Texture3DRetained) {
	    Texture3DRetained t3d = (Texture3DRetained)this;

	    ((Texture3DRetained)mirror).boundaryModeR = t3d.boundaryModeR;
	    ((Texture3DRetained)mirror).depth = t3d.depth;
	}
	TextureRetained mirrorTexture = (TextureRetained)mirror;

	mirrorTexture.boundaryModeS = boundaryModeS;
	mirrorTexture.boundaryModeT = boundaryModeT;
	mirrorTexture.minFilter = minFilter;
	mirrorTexture.magFilter = magFilter;
	mirrorTexture.boundaryColor.set(boundaryColor);
	mirrorTexture.enable = enable;
	mirrorTexture.userSpecifiedEnable = enable;
	mirrorTexture.enable = enable;
	mirrorTexture.numFaces = numFaces;
	mirrorTexture.resourceCreationMask = 0x0;
	mirrorTexture.resourceUpdatedMask = 0x0;
	mirrorTexture.resourceLodUpdatedMask = 0x0;
	mirrorTexture.resourceInReloadList = 0x0;

	// LOD information
	mirrorTexture.baseLevel = baseLevel;
	mirrorTexture.maximumLevel = maximumLevel;
	mirrorTexture.minimumLod = minimumLod;
	mirrorTexture.maximumLod = maximumLod;
	mirrorTexture.lodOffset = lodOffset;

	// sharpen texture LOD function

	mirrorTexture.numSharpenTextureFuncPts = numSharpenTextureFuncPts;
	if (sharpenTextureFuncPts == null) {
	    mirrorTexture.sharpenTextureFuncPts = null;
	} else {
	    if ((mirrorTexture.sharpenTextureFuncPts == null) ||
		    (mirrorTexture.sharpenTextureFuncPts.length !=
			sharpenTextureFuncPts.length)) {
		mirrorTexture.sharpenTextureFuncPts =
			new float[sharpenTextureFuncPts.length];
	    }
	    for (int i = 0; i < sharpenTextureFuncPts.length; i++) {
		mirrorTexture.sharpenTextureFuncPts[i] =
			sharpenTextureFuncPts[i];
	    }
	}

        // filter4 function
        if (filter4FuncPts == null) {
	    mirrorTexture.filter4FuncPts = null;
        } else {
	    if ((mirrorTexture.filter4FuncPts == null) ||
		    (mirrorTexture.filter4FuncPts.length !=
			filter4FuncPts.length)) {
		mirrorTexture.filter4FuncPts =
			new float[filter4FuncPts.length];
	    }
	    for (int i = 0; i < filter4FuncPts.length; i++) {
		mirrorTexture.filter4FuncPts[i] =
		  	filter4FuncPts[i];
	    }
	}

	// Anisotropic Filter
	mirrorTexture.anisotropicFilterMode = anisotropicFilterMode;
	mirrorTexture.anisotropicFilterDegree = anisotropicFilterDegree;

        mirrorTexture.maxLevels = maxLevels;
        if (images != null) {

            for (int j = 0; j < numFaces; j++) {
                for (int i = 0; i < maxLevels; i++) {
                    mirrorTexture.images[j][i] = images[j][i];

                    // add texture to the userList of the images
                    if (images[j][i] != null) {
                        images[j][i].addUser(mirrorTexture);
                    }
                }
            }
        }
    }

    boolean useAutoMipMapGeneration(Canvas3D cv) {
	if (mipmapMode == Texture.BASE_LEVEL &&
                (minFilter == Texture.NICEST ||
                minFilter == Texture.MULTI_LEVEL_POINT ||
                minFilter == Texture.MULTI_LEVEL_LINEAR) &&
                ((cv.textureExtendedFeatures &
                Canvas3D.TEXTURE_AUTO_MIPMAP_GENERATION) != 0)) {
            return true;
        }

        return false;
    }

    /**
     * Go through the image update info list
     * and remove those that are already done
     * by all the resources
     */
    void pruneImageUpdateInfo() {
	ImageComponentUpdateInfo info;

	//System.err.println("Texture.pruneImageUpdateInfo");

	for (int k = 0; k < numFaces; k++) {
	    for (int i = baseLevel; i <= maximumLevel; i++) {
	        if ((imageUpdatePruneMask[k] & (1<<i)) != 0) {
		    if (imageUpdateInfo[k][i] != null) {
		        for (int j = 0; j < imageUpdateInfo[k][i].size(); j++) {
			    info = (ImageComponentUpdateInfo)
					imageUpdateInfo[k][i].get(j);
			    if (info.updateMask == 0) {
			        // this update info is done, remove it
			        // from the update list
			        imageUpdateInfo[k][i].remove(j);
			    }
		        }
		    }
		    imageUpdatePruneMask[k] &= ~(1<<i);
		}
	    }
	}
    }

    /**
     * addImageUpdateInfo(int level) is to update a particular level.
     * In this case, it supercedes all the subImage update for this level,
     * and all those update info can be removed from the update list.
     *
     * Note: this method is called from mirror only
     */
    void addImageUpdateInfo(int level, int face, ImageComponentUpdateInfo arg) {

	ImageComponentUpdateInfo info;

	if (imageUpdateInfo == null) {
	    imageUpdateInfo = new ArrayList[numFaces][maxLevels];
	}

	if (imageUpdateInfo[face][level] == null) {
	    imageUpdateInfo[face][level] = new ArrayList();
	}

	info = new ImageComponentUpdateInfo();


	if (arg == null) {
	    // no subimage info, so the entire image is to be updated
	    info.entireImage = true;
        // Fix issue 117 using ogl subimage always
//	} else if ((arg.width >= width/2) && (arg.height >= height/2)) {
//
//	    // if the subimage dimension is close to the complete dimension,
//            // use the full update (it's more efficient)
//	    info.entireImage = true;
	} else {
	    info.entireImage = false;
	}

	if (info.entireImage) {
	    // the entire image update supercedes all the subimage update;
            // hence, remove all the existing updates from the list
	    imageUpdateInfo[face][level].clear();

	    // reset the update prune mask for this level
	    if (imageUpdatePruneMask != null) {
	        imageUpdatePruneMask[face] &= ~(1 << level);
	    }

	} else {
	    // subimage update, needs to save the subimage info
	    info.x = arg.x;
	    info.y = arg.y;
	    info.z = arg.z;
	    info.width = arg.width;
	    info.height = arg.height;
	}

	// save the mask which shows the canvases that have created resources
	// for this image, aka, these are the resources that need to be
	// updated.
	info.updateMask = resourceCreationMask;

	// add the image update to the list
	imageUpdateInfo[face][level].add(info);

	// check if the update list stills need to be pruned
	if (imageUpdatePruneMask != null) {
	    pruneImageUpdateInfo();
	}
    }

    void validate() {
	enable = true;
	for (int j = 0; j < numFaces && enable; j++) {
	    for (int i = baseLevel; i <= maximumLevel && enable; i++) {
		if (images[j][i] == null) {
		    enable = false;
		}
	    }
	}
    }

    /**
     * Update the "component" field of the mirror object with the
     *  given "value"
     */
    @Override
    synchronized void updateMirrorObject(int component, Object value) {

	TextureRetained mirrorTexture = (TextureRetained)mirror;

	if ((component & ENABLE_CHANGED) != 0) {
	    mirrorTexture.enable = ((Boolean)value).booleanValue();

	} else if ((component & IMAGE_CHANGED) != 0) {

	    Object [] arg = (Object []) value;
            int level = ((Integer)arg[0]).intValue();
	    ImageComponent image = (ImageComponent)arg[1];
            int face = ((Integer)arg[2]).intValue();

	    // first remove texture from the userList of the current
            // referencing image and

	    if (mirrorTexture.images[face][level] != null) {
	        mirrorTexture.images[face][level].removeUser(mirror);
	    }

	    // assign the new image and add texture to the userList
	    if (image == null) {
	        mirrorTexture.images[face][level] = null;

	    } else {
	        mirrorTexture.images[face][level] =
			(ImageComponentRetained)image.retained;
	        mirrorTexture.images[face][level].addUser(mirror);

            }

	    // NOTE: the old image has to be removed from the
	    // renderBins' NodeComponentList and new image has to be
	    // added to the lists. This will be taken care of
	    // in the RenderBin itself in response to the
	    // IMAGE_CHANGED message


	    // mark that texture images need to be updated
	    mirrorTexture.resourceUpdatedMask = 0;

	    // add update info to the update list
	    mirrorTexture.addImageUpdateInfo(level, face, null);

	} else if ((component & IMAGES_CHANGED) != 0) {

	    Object [] arg = (Object []) value;
	    ImageComponent [] images = (ImageComponent[])arg[0];
	    int face = ((Integer)arg[1]).intValue();

	    for (int i = 0; i < images.length; i++) {

		// first remove texture from the userList of the current
		// referencing image
		if (mirrorTexture.images[face][i] != null) {
		    mirrorTexture.images[face][i].removeUser(mirror);
		}

		// assign the new image and add texture to the userList
		if (images[i] == null) {
		    mirrorTexture.images[face][i] = null;
		} else {
		    mirrorTexture.images[face][i] =
			(ImageComponentRetained)images[i].retained;
		    mirrorTexture.images[face][i].addUser(mirror);
		}
	    }
	    mirrorTexture.updateResourceCreationMask();

	    // NOTE: the old images have to be removed from the
	    // renderBins' NodeComponentList and new image have to be
	    // added to the lists. This will be taken care of
	    // in the RenderBin itself in response to the
	    // IMAGES_CHANGED message

	} else if ((component & BASE_LEVEL_CHANGED) != 0) {
	    int level = ((Integer)value).intValue();

	    if (level < mirrorTexture.baseLevel) {

		// add texture to the userList of those new levels of
		// enabling images

		for (int j = 0; j < numFaces; j++) {
		    for (int i = level; i < mirrorTexture.baseLevel; i++) {

			if (mirrorTexture.images[j][i] == null) {
			    mirrorTexture.enable = false;
			} else {
	    		    mirrorTexture.addImageUpdateInfo(i, j, null);
			}
		    }
		}

		mirrorTexture.baseLevel = level;

	        // mark that texture images need to be updated
	        mirrorTexture.resourceUpdatedMask = 0;


	    } else {

		mirrorTexture.baseLevel = level;

		if (userSpecifiedEnable && (mirrorTexture.enable == false)) {

		    // if texture is to be enabled but is currently
		    // disabled, it's probably disabled because
		    // some of the images are missing. Now that
		    // the baseLevel is modified, validate the
		    // texture images again

		    mirrorTexture.validate();
		}
	    }

	    // mark that texture lod info needs to be updated
	    mirrorTexture.resourceLodUpdatedMask = 0;

	} else if ((component & MAX_LEVEL_CHANGED) != 0) {
            int level = ((Integer)value).intValue();

            if (level > mirrorTexture.maximumLevel) {

                // add texture to the userList of those new levels of
                // enabling images

                for (int j = 0; j < numFaces; j++) {
                    for (int i = mirrorTexture.maximumLevel; i < level; i++) {

                        if (mirrorTexture.images[j][i] == null) {
                            mirrorTexture.enable = false;
			} else {
	    		    mirrorTexture.addImageUpdateInfo(i, j, null);
                        }
                    }
                }

                mirrorTexture.maximumLevel = level;

	        // mark that texture images need to be updated
	        mirrorTexture.resourceUpdatedMask = 0;

            } else {

                mirrorTexture.maximumLevel = level;

                if (userSpecifiedEnable && (mirrorTexture.enable == false)) {

                    // if texture is to be enabled but is currently
                    // disabled, it's probably disabled because
                    // some of the images are missing. Now that
                    // the baseLevel is modified, validate the
                    // texture images again

                    mirrorTexture.validate();
                }
            }

	    // mark that texture lod info needs to be updated
	    mirrorTexture.resourceLodUpdatedMask = 0;

	} else if ((component & MIN_LOD_CHANGED) != 0) {
	    mirrorTexture.minimumLod = ((Float)value).floatValue();

	    // mark that texture lod info needs to be updated
	    mirrorTexture.resourceLodUpdatedMask = 0;

	} else if ((component & MAX_LOD_CHANGED) != 0) {
	    mirrorTexture.maximumLod = ((Float)value).floatValue();

	    // mark that texture lod info needs to be updated
	    mirrorTexture.resourceLodUpdatedMask = 0;

	} else if ((component & LOD_OFFSET_CHANGED) != 0) {
	    if ((mirrorTexture.lodOffset) == null) {
		mirrorTexture.lodOffset =
					new Point3f((Point3f)value);
	    } else {
		mirrorTexture.lodOffset.set((Point3f)value);
	    }

	    // mark that texture lod info needs to be updated
	    mirrorTexture.resourceLodUpdatedMask = 0;

        } else if ((component & UPDATE_IMAGE) != 0) {
            mirrorTexture.updateResourceCreationMask();
        }

    }


    // notifies the Texture mirror object that the image data in a referenced
    // ImageComponent object is changed. Need to update the texture image
    // accordingly.
    // Note: this is called from mirror object only

    void notifyImageComponentImageChanged(ImageComponentRetained image,
					ImageComponentUpdateInfo value) {

        //System.err.println("Texture.notifyImageComponentImageChanged");


	// if this texture is to be reloaded, don't bother to keep
	// the update info

	if (resourceCreationMask == 0) {

	    if (imageUpdateInfo != null) {

                //remove all the existing updates from the list

	        for (int face = 0; face < numFaces; face++) {
		    for (int level = 0; level < maxLevels; level++) {
			if (imageUpdateInfo[face][level] != null) {
			    imageUpdateInfo[face][level].clear();
			}
		    }

                    // reset the update prune mask for this level
                    if (imageUpdatePruneMask != null) {
                        imageUpdatePruneMask[face] = 0;
		    }
		}
	    }

	    return;
	}


	// first find which texture image is being affected

	boolean done;

	for (int j = 0; j < numFaces; j++) {

	    done = false;
	    for (int i = baseLevel; i <= maximumLevel && !done; i++) {
	         if (images[j][i] == image) {

		     // reset the resourceUpdatedMask to tell the
		     // rendering method to update the resource
		     resourceUpdatedMask = 0;

	             // add update info to the update list
	             addImageUpdateInfo(i, j, value);

		     // set done to true for this face because no two levels
		     // can reference the same ImageComponent object
		     done = true;
	         }
	    }
	}
    }


    // reset the resourceCreationMask
    // Note: called from the mirror object only

    void updateResourceCreationMask() {
        resourceCreationMask = 0x0;
    }

    void incTextureBinRefCount(TextureBin tb) {

	ImageComponentRetained image;

        setTextureBinRefCount(tb, getTextureBinRefCount(tb) + 1);

	// check to see if there is any modifiable images,
	// if yes, add those images to nodeComponentList in RenderBin
	// so that RenderBin can acquire a lock before rendering
        // to prevent updating of image data while rendering

        for (int j = 0; j < numFaces; j++) {
            for (int i = 0; i < maxLevels; i++) {
		image = images[j][i];

		// it is possible that image.source == null because
		// the mipmap could have been created by the library, and
		// hence don't have source and therefore they are
		// guaranteed not modifiable

		if (image != null &&
		     (image.isByReference() ||
		      (image.source != null &&
		       image.source.getCapability(
				ImageComponent.ALLOW_IMAGE_WRITE)))) {
		    tb.renderBin.addNodeComponent(image);
		}
	    }
	}
    }

    void decTextureBinRefCount(TextureBin tb) {

	ImageComponentRetained image;

        setTextureBinRefCount(tb, getTextureBinRefCount(tb) - 1);

	// remove any modifiable images from RenderBin nodeComponentList

        for (int j = 0; j < numFaces; j++) {
            for (int i = 0; i < maxLevels; i++) {
		image = images[j][i];
		if (image != null &&
		     (image.isByReference() ||
		      (image.source != null &&
		       image.source.getCapability(
				ImageComponent.ALLOW_IMAGE_WRITE)))) {
		    tb.renderBin.removeNodeComponent(image);
		}
	    }
	}
    }


    final void sendMessage(int attrMask, Object attr) {

	ArrayList<VirtualUniverse> univList = new ArrayList<VirtualUniverse>();
	ArrayList<ArrayList<GeometryAtom>> gaList = Shape3DRetained.getGeomAtomsList(mirror.users, univList);

	// Send to rendering attribute structure, regardless of
	// whether there are users or not (alternate appearance case ..)
	J3dMessage createMessage = new J3dMessage();
	createMessage.threads = J3dThread.UPDATE_RENDERING_ATTRIBUTES;
	createMessage.type = J3dMessage.TEXTURE_CHANGED;
	createMessage.universe = null;
	createMessage.args[0] = this;
	createMessage.args[1] = new Integer(attrMask);
	createMessage.args[2] = attr;
	createMessage.args[3] = new Integer(changedFrequent);
	VirtualUniverse.mc.processMessage(createMessage);

	// System.err.println("univList.size is " + univList.size());
	for(int i=0; i<univList.size(); i++) {
	    createMessage = new J3dMessage();
	    createMessage.threads = J3dThread.UPDATE_RENDER;
	    createMessage.type = J3dMessage.TEXTURE_CHANGED;

		createMessage.universe = univList.get(i);
	    createMessage.args[0] = this;
	    createMessage.args[1]= new Integer(attrMask);
	    createMessage.args[2] = attr;

		ArrayList<GeometryAtom> gL = gaList.get(i);
	    GeometryAtom[] gaArr = new GeometryAtom[gL.size()];
	    gL.toArray(gaArr);
	    createMessage.args[3] = gaArr;

	    VirtualUniverse.mc.processMessage(createMessage);
	}

    }

    @Override
    void handleFrequencyChange(int bit) {
        switch (bit) {
        case Texture.ALLOW_ENABLE_WRITE:
        case Texture.ALLOW_IMAGE_WRITE:
        case Texture.ALLOW_LOD_RANGE_WRITE: {
            setFrequencyChangeMask(bit, bit);
        }
        default:
            break;
        }
    }

    void setUseAsRaster(boolean useAsRaster) {
        this.useAsRaster = useAsRaster;
    }

    boolean isUseAsRaster() {
        return this.useAsRaster;
    }

    // Issue 357 - {get/set}TextureBinRefCount now uses a separate reference
    // counter per RenderBin. The absence of the RenderBin key in the hash map
    // is used to indicate a value of 0. This makes initialization easier, and
    // prevents a small amount of garbage accumulating for inactive RenderBins.

    int getTextureBinRefCount(TextureBin tb) {
        Integer i = textureBinRefCount.get(tb.renderBin);
        return i == null ? 0 : i.intValue();
    }

    private void setTextureBinRefCount(TextureBin tb, int refCount) {
        if (refCount == 0) {
            textureBinRefCount.remove(tb.renderBin);
        } else {
            textureBinRefCount.put(tb.renderBin, new Integer(refCount));
        }
    }

}