openexr 0.11.0

High-level bindings to OpenEXR 3.0.5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
# Reading and Writing OpenEXR Image Files.

-   [Document Purpose and Audience](#document-purpose-and-audience)
-   [Scan Line Based and Tiled OpenEXR
    files](#scan-line-based-and-tiled-openexr-files)
    -   [Multi-Part and Deep Data](#multi-part-and-deep-data)
-   [Using the RGBA-only Interface for Scan Line Based
    Files](#using-the-rgba-only-interface-for-scan-line-based-files)
    -   [Writing an RGBA Image File](#writing-an-rgba-image-file)
    -   [Writing a Cropped RGBA Image](#writing-a-cropped-rgba-image)
    -   [Storing Custom Attributes](#storing-custom-attributes)
    -   [Reading an RGBA Image File](#reading-an-rgba-image-file)
    -   [Reading an RGBA Image File in
        Chunks](#reading-an-rgba-image-file-in-chunks)
    -   [Reading Custom Attributes](#reading-custom-attributes)
    -   [Luminance/Chroma and Gray-Scale
        Images](#luminancechroma-and-gray-scale-images)
-   [Using the General Interface for Scan Line Based
    Files](#using-the-general-interface-for-scan-line-based-files)
    -   [Writing an Image File](#writing-an-image-file)
    -   [Writing a Cropped Image](#writing-a-cropped-image)
    -   [Reading an Image File](#reading-an-image-file)
    -   [Interleaving Image Channels in the Frame
        Buffer](#interleaving-image-channels-in-the-frame-buffer)
    -   [Which Channels are in a File?](#which-channels-are-in-a-file)
    -   [Layers](#layers)
-   [Tiles, Levels and Level Modes](#tiles-levels-and-level-modes)
-   [Using the RGBA-only Interface for Tiled
    Files](#using-the-rgba-only-interface-for-tiled-files)
    -   [Writing a Tiled RGBA Image File with One Resolution
        Level](#writing-a-tiled-rgba-image-file-with-one-resolution-level)
    -   [Writing a Tiled RGBA Image File with Mipmap
        Levels](#writing-a-tiled-rgba-image-file-with-mipmap-levels)
    -   [Writing a Tiled RGBA Image File with Ripmap
        Levels](#writing-a-tiled-rgba-image-file-with-ripmap-levels)
    -   [Reading a Tiled RGBA Image
        File](#reading-a-tiled-rgba-image-file)
-   [Using the General Interface for Tiled
    Files](#using-the-general-interface-for-tiled-files)
    -   [Writing a Tiled Image File](#writing-a-tiled-image-file)
    -   [Reading a Tiled Image File](#reading-a-tiled-image-file)
-   [Deep Data Files (New in 2.0)](#deep-data-files-new-in-20)
    -   [Writing a Deep Scan Line File](#writing-a-deep-scan-line-file)
    -   [Reading a Deep Scan Line File](#reading-a-deep-scan-line-file)
    -   [Writing a Deep Tiled File](#writing-a-deep-tiled-file)
    -   [Reading a Deep Tiled File](#reading-a-deep-tiled-file)
-   [Threads](#threads)
    -   [Library Thread-Safety](#library-thread-safety)
    -   [Multithreaded I/O](#multithreaded-io)
    -   [Multithreaded I/O, Multithreaded Application
        Program](#multithreaded-io-multithreaded-application-program)
<!---
-   [Low-Level I/O](#low-level-io)
    -   [Custom Low-Level File I/O](#custom-low-level-file-io)
    -   [Memory-Mapped I/O](#memory-mapped-io)
-->
-   [Miscellaneous](#miscellaneous)
    -   [Is this an OpenEXR File?](#is-this-an-openexr-file)
    -   [Is this File Complete?](#is-this-file-complete)
    -   [Preview Images](#preview-images)
    -   [Environment Maps](#environment-maps)

# Document Purpose and Audience

This document shows how to write Rust code that reads and writes OpenEXR
3.0 image files.

The text assumes that the reader is familiar with OpenEXR terms like
"channel", "attribute", "data window" or "deep data". For an explanation
of those terms see the [Technical Introduction to OpenEXR](crate::doc::technical_introduction) document.

Most of the code examples below are standalone examples in the `examples` directory and can be run as usual.

A description of the file structure and format is provided in *OpenEXR
File Layout*.

# Scan Line Based and Tiled OpenEXR files

In an OpenEXR file, pixel data can be stored either as scan lines or as
tiles. Files that store pixels as tiles can also store multi-resolution
images. For each of the two storage formats (scan line or tile-based),
the IlmImf library supports two reading and writing interfaces:

1.  The first, fully general, interface allows access to arbitrary
    channels, and supports many different in-memory pixel data layouts.
2.  The second interface is easier to use, but limits access to f16 RGBA channels, and provides fewer
    options for laying out pixels in memory.

The interfaces for reading and writing OpenEXR files are implemented in
the following eight structs:

|                    | Tiles                                                                | Scan lines                                           | Scan lines and tiles                               |
|--------------------|----------------------------------------------------------------------|------------------------------------------------------|----------------------------------------------------|
| Arbitrary channels | [`TiledInputFile`](crate::tiled::tiled_input_file::TiledInputFile)          |                                                      | [`InputFile`](crate::core::input_file::InputFile)        |
|                    | [`TiledOutputFile`](crate::tiled::tiled_output_file::TiledOutputFile)       | [`OutputFile`](crate::core::output_file::OutputFile)       |                                                    |
| RGBA only          | [`TiledRgbaInputFile`](crate::tiled::tiled_rgba_file::TiledRgbaInputFile)   |                                                      | [`RgbaInputFile`](crate::rgba::rgba_file::RgbaInputFile) |
|                    | [`TiledRgbaOutputFile`](crate::tiled::tiled_rgba_file::TiledRgbaOutputFile) | [`RgbaOutputFile`](crate::rgba::rgba_file::RgbaOutputFile) |                                                    |

The classes for reading scan line based images ([`InputFile`](crate::core::input_file::InputFile) and
[`RgbaInputFile`](crate::core::rgba_file::RgbaInputFile)) can also be used to read tiled image files. This way,
programs that do not need support for tiled or multi-resolution images
can always use the rather straightforward scan line interfaces, without
worrying about complications related to tiling and multiple resolutions.
When a multi-resolution file is read via a scan line interface, only the
highest-resolution version of the image is accessible.

## Multi-Part and Deep Data 

The procedure for writing multi-part and deep data files is similar to
writing scan line and tile. Though there is no simplified interface,
such as the RGBA-only interface.

This table describes the significant differences between writing
single-part scan line and tile files and writing multi-part and deep
data files.

<table>
<colgroup>
<col style="width: 33%" />
<col style="width: 33%" />
<col style="width: 33%" />
</colgroup>
<tbody>
<tr class="odd odd">
<td>Feature</td>
<td>scan line and tile</td>
<td>Multi-part and deep data</td>
</tr>
<tr class="even even">
<td>Channel names may be reserved</td>
<td>Some channel names are reserved in practice, but were never formally defined.</td>
<td><p>Channel name “sample count” is reserved for a pixel sample count slice in frame buffer.</p>
<p><strong>Note:</strong> The name “sample count” (all lowercase) is subject to change.</p></td>
</tr>
<tr class="odd odd">
<td>Multiple parts</td>
<td>Single-part format is intended for storing a single multichannel image</td>
<td><p>Multi-part files support multiple independent parts. This allows storing multiple views in the same file for stereo images, storing multiple resolutions in different parts. It is possible to include one or more scan line, tile, deep scan line or deep tile format images within a multi-part file.</p>
<p>Custom data formats can also be used to store additional parts, but this is outside the scope of this document.</p></td>
</tr>
<tr class="even even">
<td>Backwards-compatible low-level io available</td>
<td>The new formats share the same abstract low-level IO as OpenEXR 1.7. It is therefore possible to use the same libraries to implement low level IO to read both formats.</td>
<td></td>
</tr>
</tbody>
</table>

# Using the RGBA-only Interface for Scan Line Based Files

## Writing an RGBA Image File

Writing a simple RGBA image file is fairly straightforward:

```no_run
use openexr::prelude::*;

fn write_rgba1(filename: &str, pixels: &[Rgba], width: i32, height: i32) 
-> Result<(), Box<dyn std::error::Error>> {
    let header = Header::from_dimensions(width, height);
    let mut file = RgbaOutputFile::new(
        filename,
        &header,
        RgbaChannels::WriteRgba,
        1,
    )?;

    file.set_frame_buffer(&pixels, 1, width as usize)?;
    file.write_pixels(height)?;

    Ok(())
}
```

[`RgbaOutputFile`]: crate::rgba::rgba_file::RgbaOutputFile
[`Rgba`]: crate::rgba::rgba::Rgba

Construction of an [`RgbaOutputFile`] object, in line 1, creates an OpenEXR
header, sets the header\'s attributes, opens the file with the specified
name, and stores the header in the file. The header\'s display window
and data window are both set to `(0, 0) - (width-1, height-1)`. The
channel list contains four channels, R, G, B, and A, of type HALF (`f16`).

Line 2 specifies how the pixel data are laid out in memory. In our
example, `pixels` is a slice of [`Rgba`] of length `width * height`.

The elements of our slice are arranged so that the pixels of each scan
line are contiguous in memory. The  [`set_frame_buffer()`](crate::rgba::rgba_file::RgbaOutputFile::set_frame_buffer) method takes
three arguments, `pixels`, `x_stride`, and `y_stride`. To find the address
of pixel `(x, y)`, the [`RgbaOutputFile`] object computes

```c
pixels[x * x_stride + y * y_sride]
```

In this case, `x_stride` and `y_stride` are set to `1` and `width`, respectively, indicating that pixel `(x, y)` can be found at index

```c
pixels[1 * x + width * y]
```

The call to [`write_pixels()`](crate::rgba::rgba_file::RgbaOutputFile::write_pixels) in line 3, copies the image\'s pixels from
memory to the file. The argument to [`write_pixels()`](crate::rgba::rgba_file::RgbaOutputFile::write_pixels), `height`, specifies
how many scan lines worth of data are copied.

Finally, returning from function `write_rgba1()` drops the local [`RgbaOutputFile`] object, thereby closing the file.

Why do we have to tell the [`write_pixels()`](crate::rgba::rgba_file::RgbaOutputFile::write_pixels) function how many scan lines
we want to write? Shouldn\'t the [`RgbaOutputFile`] object be able to
derive the number of scan lines from the data window? OpenEXR
doesn\'t require writing all scan lines with a single [`write_pixels()`](crate::rgba::rgba_file::RgbaOutputFile::write_pixels)
call. Many programs want to write scan lines individually, or in small
blocks. For example, rendering computer-generated images can take a
significant amount of time, and many rendering programs want to store
each scan line in the image file as soon as all of the pixels for that
scan line are available. This way, users can look at a partial image
before rendering is finished. The OpenEXR crate allows writing the scan
lines in top-to-bottom or bottom-to-top direction. The direction is
defined by the file header\'s line order attribute ([`LineOrder::IncreasingY`](crate::core::LineOrder) or
[`LineOrder::DecreasingY`](crate::core::LineOrder)). By default, scan lines are written top to bottom
([`LineOrder::IncreasingY`](crate::core::LineOrder)).

## Writing a Cropped RGBA Image

Now we are going to store a cropped image in a file. For this example,
we assume that we have a frame buffer that is large enough to hold an
image with `width` by `height` pixels, but only part of the frame buffer
contains valid data. In the file\'s header, the size of the whole image
is indicated by the display window, `(0, 0) - (width-1, height-1)`, and
the data window specifies the region for which valid pixel data exist.
Only the pixels in the data window are stored in the file.

```no_run
use openexr::prelude::*;

fn write_rgba2(
    filename: &str, 
    pixels: &[Rgba], 
    data_window: &[i32; 4],
    display_window: &[i32; 4],
) -> Result<(), Box<dyn std::error::Error>> {
    let mut header = Header::from_windows(
        *data_window,    
        *display_window,
    );

    let mut file = RgbaOutputFile::new(
        filename,
        &header,
        RgbaChannels::WriteRgba,
        1,
    )?;

    file.set_frame_buffer(&pixels, 1, (data_window.width() + 1) as usize)?;
    file.write_pixels(data_window.height() + 1)?;

    Ok(())
}
```

The code above is similar to that in [Writing an RGBA Image File](#writing-an-rgba-image-file), where the whole image was stored in the file. Two things are different, however: When the [`RgbaOutputFile`] object is created, the data window and the display window are explicitly specified rather than being derived from the image's width and height. The number of scan lines stored in the file by [`write_pixels()`](crate::rgba::rgba_file::RgbaOutputFile::write_pixels) is equal to the height of the data window instead of the height of the whole image. Since we are using the default [`LineOrder::IncreasingY`](crate::core::LineOrder) direction for storing the scan lines in the file, [`write_pixels()`](crate::rgba::rgba_file::RgbaOutputFile::write_pixels) starts at the top of the data window, at y coordinate `data_window.min.y`, and proceeds toward the bottom, at y coordinate `data_window.max.y`.

Even though we are storing only part of the image in the file, the frame buffer is still large enough to hold the whole image. In order to save memory, a smaller frame buffer could have been allocated, just big enough to hold the contents of the data window. Assuming that the pixels were still stored in contiguous scan lines, with the *pixels* pointer pointing to the pixel at the upper left corner of the data window, at coordinates *(dataWindow.min.x, dataWindow.min.y)*, the arguments to the *setFrameBuffer()* call would have to be to be changed as follows:

int dwWidth = dataWindow.max.x - dataWindow.min.x + 1;

file.setFrameBuffer

(pixels - dataWindow.min.x - dataWindow.min.y \* dwWidth, 1, dwWidth);

With these settings, evaluation of

base + x \* xStride + y \* yStride

for pixel *(dataWindow.min.x, dataWindow.min.y)* produces

pixels - dataWindow.min.x - dataWindow.min.y \* dwWidth

\+ dataWindow.min.x \* 1

\+ dataWindow.min.y \* dwWidth

= pixels -

\- dataWindow.min.x

\- dataWindow.min.y \* (dataWindow.max.x - dataWindow.min.x + 1)

\+ dataWindow.min.x

\+ dataWindow.min.y \* (dataWindow.max.x - dataWindow.min.x + 1)

= pixels,

which is exactly what we want. Similarly, calculating the addresses for
pixels *(dataWindow.min.x+1, dataWindow.min.y)* and *(dataWindow.min.x,
dataWindow.min.y+1)* yields *pixels+1* and *pixels+dwWidth*,
respectively.

## Storing Custom Attributes

We will now to store an image in a file, and we will add two extra
attributes to the image file header: a string, called `"comments"`, and
a 4×4 matrix, called `"cameraTransform"`.

```no_run
use openexr::prelude::*;
use openexr::core::attribute::{CppStringAttribute, M44fAttribute};

fn write_rgba3(
    pixels: &[Rgba], 
    width: i32, 
    height: i32, 
    comments: &str, 
    xform: &[f32; 16]
) -> Result<(), Box<dyn std::error::Error>> {
    let mut header = Header::from_dimensions(width, height);
    header.insert("comments", &CppStringAttribute::from_value(comments))?;
    header.insert("cameraTransform", &M44fAttribute::from_value(xform))?;

    let mut file = RgbaOutputFile::new(
        "write_rgba1.exr",
        &header,
        RgbaChannels::WriteRgba,
        1,
    )?;

    file.set_frame_buffer(&pixels, 1, width as usize)?;
    file.write_pixels(height)?;

    Ok(())
}
```

In order to make it easier to exchange data between programs written by
different people, the IlmImf library defines a set of standard
attributes for commonly used data, such as colorimetric information,
time and place where an image was recorded, or the owner of an image
file\'s content. For the current list of standard attributes, see the
[`standard_attributes`](crate::core::standard_attributes) module. The list is expected to grow over
time as OpenEXR users identify new types of data they would like to
represent in a standard format. If you need to store some piece of
information in an OpenEXR file header, it is probably a good idea to
check if a suitable standard attribute exists, before you define a new
attribute.

## Reading an RGBA Image File

Reading an RGBA image is almost as easy as writing one:

```no_run
use openexr::prelude::*;

fn read_rgba1(path: &str) -> Result<(), Box<dyn std::error::Error>> {
    use imath_traits::Zero;

    let mut file = RgbaInputFile::new(path, 1).unwrap();
    let data_window = file.header().data_window::<[i32; 4]>().clone();
    let width = data_window[2] - data_window[0] + 1;
    let height = data_window[3] - data_window[1] + 1;

    let mut pixels = vec![Rgba::zero(); (width * height) as usize];
    file.set_frame_buffer(&mut pixels, 1, width as usize)?;
    file.read_pixels(0, height - 1)?;

    Ok(())
}
```
[`RgbaInputFile`]: crate::rgba::rgba_file::RgbaInputFile

Constructing an [`RgbaInputFile`] object, passing the name of the file to
the constructor, opens the file and reads the file\'s header.

After asking the [`RgbaInputFile`] object for the file\'s data window, we
allocate a buffer for the pixels. The number of scan lines in the buffer is equal
to the height of the data window, and the number of pixels per scan line
is equal to the width of the data window. The pixels are represented as
[`Rgba`] structs.

Note that we ignore the display window in this example; in a program
that wanted to place the pixels in the data window correctly in an
overall image, the display window would have to be taken into account.

Just as for writing a file, calling [`RgbaInputFile`](crate::rgba::rgba_file::RgbaInputFile::set_frame_buffer) tells the
[`RgbaInputFile`] object how to access individual pixels in the buffer.
(See also [Writing a Cropped RGBA
Image](#writing-a-cropped-rgba-image).

Calling [`read_pixels()`](crate::rgba::rgba_file::RgbaInputFile::read_pixels) copies the pixel data from the file into the
buffer. If one or more of the R, G, B, and A channels are missing in the
file, the corresponding field in the pixels is filled with an
appropriate default value. The default value for R, G and B is 0.0, or
black; the default value for A is 1.0, or opaque.

Finally, returning from function `read_rgba1()` drops the local
[`RgbaInputFile`] object, thereby closing the file.

Unlike the [`RgbaOutputFile`]'s [`write_pixels()`](crate::rgba::rgba_file::RgbaOutputFile::write_pixels) method, [`read_pixels()`](crate::rgba::rgba_file::RgbaInputFile::read_pixels)
has two arguments. Calling `read_pixels(y1, y2)` copies the pixels for all
scan lines with y coordinates from `y1` to `y2` into the frame buffer.
This allows access to the the scan lines in any order. The image can be
read all at once, one scan line at a time, or in small blocks of a few
scan lines. It is also possible to skip parts of the image.

Note that even though random access is possible, reading the scan lines
in the same order as they were written, is more efficient. Random access
to the file requires seek operations, which tend to be slow. Calling the
[`RgbaInputFile`]'s [`line_order`](crate::rgba::rgba_file::RgbaInputFile::line_order) method returns the order in which the
scan lines in the file were written ([`LineOrder::IncreasingY`](crate::core::LineOrder) or [`LineOrder::DecreasingY`](crate::core::LineOrder)).
If successive calls to [`read_pixels()`](crate::rgba::rgba_file::RgbaInputFile::read_pixels) access the scan lines in the right
order, the OpenEXR reads the file as fast as possible, without seek operations.

<!---
## Reading an RGBA Image File in Chunks

The following shows how to read an RGBA image in blocks of a few scan
lines. This is useful for programs that want to process high-resolution
images without allocating enough memory to hold the complete image.
These programs typically read a few scan lines worth of pixels into a
memory buffer, process the pixels, and store them in another file. The
buffer is then re-used for the next set of scan lines. Image operations
like color-correction or compositing (\"A over B\") are very easy to do
incrementally this way. With clever buffering of a few extra scan lines,
incremental versions of operations that require access to neighboring
pixels, like blurring or sharpening, are also possible.

void

readRgba2 (const char fileName\[\])

{

RgbaInputFile file (fileName);

Box2i dw = file.dataWindow();

int width = dw.max.x - dw.min.x + 1;

int height = dw.max.y - dw.min.y + 1;

Array2D\<Rgba> pixels (10, width);

while (dw.min.y \<= dw.max.y)

{

file.setFrameBuffer (&pixels\[0\]\[0\] - dw.min.x - dw.min.y \* width,

1, width);

file.readPixels (dw.min.y, min (dw.min.y + 9, dw.max.y));

// processPixels (pixels)

dw.min.y += 10;

}

}

Again, we open the file and read the file header by constructing an
*RgbaInputFile* object. Then we allocate a memory buffer that is just
large enough to hold ten complete scan lines. We call *readPixels()* to
copy the pixels from the file into our buffer, ten scan lines at a time.
Since we want to re-use the buffer for every block of ten scan lines, we
have to call *setFramebuffer()* before each *readPixels()* call, in
order to associate memory address *&pixels\[0\]\[0\]* first with pixel
coordinates *(dw.min.x, dw.min.y)*, then with *(dw.min.x, dw.min.y+10)*,
*(dw.min.x, dw.min.y+20)* and so on.

-->

## Reading Custom Attributes

In [Storing Custom Attributes](#storing-custom-attributes), we showed how to store custom
attributes in the image file header. Here we show how to test whether a
given file\'s header contains particular attributes, and how to read
those attributes\' values.

```no_run
use openexr::prelude::*;

fn read_header(filename: &str) -> Result<(), Box<dyn std::error::Error>> {
    let file = RgbaInputFile::new(filename, 1)?;

    if let Some(attr) = file.header().find_typed_attribute_string("comments") {
        println!("comments: {}", attr.value())
    }

    if let Some(attr) = file.header().find_typed_attribute_m44f("cameraTransform") {
        println!("cameraTransform: {:?}", attr.value::<[f32; 16]>())
    }

    Ok(())
}
```

As usual, we open the file by constructing an [`RgbaInputFile`] object.
Calling [`find_typed_attribute_X(n)`](crate::core::header::Header::find_typed_attribute_string) searches the header for an attribute
with type `X` and name `n`. If a matching attribute is found,
[`find_typed_attribute_X(n)`](crate::core::header::Header::find_typed_attribute_string) returns [`Some`] containing a reference to the attribute. If the header
contains no attribute with name `n`, or if the header contains an
attribute with name `n`, but the attribute\'s type is not `X`,
[`find_typed_attribute_X(n)`](crate::core::header::Header::find_typed_attribute_string) returns [`None`]. Once we have references to the attributes
we were looking for, we can access their values by calling the
attributes\' [`value()`](crate::core::attribute::CppStringAttribute::value) methods.


## Luminance/Chroma and Gray-Scale Images

Writing an RGBA image file usually preserves the pixels without losing
any data; saving an image file and reading it back does not alter the
pixels\' R, G, B and A values. Most of the time, lossless data storage
is exactly what we want, but sometimes file space or transmission
bandwidth are limited, and we would like to reduce the size of our image
files. It is often acceptable if the numbers in the pixels change
slightly as long as the image still looks just like the original.

The RGBA interface supports storing RGB data in
luminance/chroma format. The R, G, and B channels are converted into a
luminance channel, Y, and two chroma channels, RY and BY. The Y channel
represents a pixel\'s brightness, and the two chroma channels represent
its color. The human visual system\'s spatial resolution for color is
much lower than the spatial resolution for brightness. This allows us to
reduce the horizontal and vertical resolution of the RY and BY channels
by a factor of two. The visual appearance of the image doesn\'t change,
but the image occupies only half as much space, even before data
compression is applied. (For every four pixels, we store four Y values,
one RY value, and one BY value, instead of four R, four G, and four B
values.)

When opening a file for writing, a program can select how it wants the
pixels to be stored. The constructors for [`RgbaOutputFile`] have an
`rgba_channels` argument, which determines the set of channels in the
file:

|    Value                    | Channels written          |
|-----------------------------|---------------------------|
| [`RgbaChannels::WriteRgba`] |  red, green, blue, alpha  |
| [`RgbaChannels::WriteYc`]   |  luminance, chroma        |
| [`RgbaChannels::WriteYca`]  |  luminance, chroma, alpha |
| [`RgbaChannels::WriteY`]    |  luminance only           |
| [`RgbaChannels::WriteYa`]   |  luminance, alpha         |

[`RgbaChannels::WriteRgba`]: crate::rgba::rgba::RgbaChannels::WriteRgba
[`RgbaChannels::WriteYc`]: crate::rgba::rgba::RgbaChannels::Yc
[`RgbaChannels::WriteYca`]: crate::rgba::rgba::RgbaChannels::Yca
[`RgbaChannels::WriteY`]: crate::rgba::rgba::RgbaChannels::Y
[`RgbaChannels::WriteYa`]: crate::rgba::rgba::RgbaChannels::Ya

[`RgbaChannels::WriteY`] and [`RgbaChannels::WriteYa`] provide an efficient way to store gray-scale
images. The chroma channels for a gray-scale image contain only zeroes,
so they can be omitted from the file.

When an image file is opened for reading, class [`RgbaInputFile`]
automatically detects luminance/chroma images and converts the pixels
back to RGB format.

# Using the General Interface for Scan Line Based Files

## Writing an Image File

This example demonstrates how to write an OpenEXR image file with two
channels: one channel, of type [`PixelType::Half`], is called G, and the other, of
type [`PixelType::Float`], is called Z. The size of the image is `width` by `height`
pixels. The data for the two channels are supplied in two separate
buffers, `g` and `z`. Within each buffer, the pixels of each
scan line are contiguous in memory.

[`PixelType`]: crate::core::PixelType
[`PixelType::Half`]: crate::core::PixelType::Half
[`PixelType::Float`]: crate::core::PixelType::Float

```no_run
use openexr::prelude::*;
use half::f16;

fn write_gz1(
    filename: &str,
    g: &[f16],
    z: &[f32],
    width: i32,
    height: i32,
) -> Result<(), Box<dyn std::error::Error>> {

    let mut header = Header::from_dimensions(width, height);
    header.channels_mut().insert("G", &CHANNEL_HALF);
    header.channels_mut().insert("Z", &CHANNEL_FLOAT);

    let mut frame_buffer = FrameBuffer::new();

    frame_buffer.insert(
        "G",
        &Slice::builder(
            PixelType::Half,
            g.as_ptr() as *const u8,
            width as i64,
            height as i64,
        )
        .x_stride(std::mem::size_of::<f16>())
        .build()?
    )?;

    frame_buffer.insert(
        "Z",
        &Slice::builder(
            PixelType::Float,
            z.as_ptr() as *const u8,
            width as i64,
            height as i64,
        )
        .x_stride(std::mem::size_of::<f32>())
        .build()?
    )?;

    let mut file = OutputFile::new("write_gz1.exr", &header, 1).unwrap();
    file.set_frame_buffer(&frame_buffer).unwrap();
    unsafe { file.write_pixels(height).unwrap() };

    Ok(())
}
```

First, an OpenEXR header is created, and the header\'s display
window and data window are both set to `(0, 0) - (width-1, height-1)` by the [`with_dimensions`](crate::core::header::Header::with_dimensions) constructor.

Next the names and types of the channels that are to be stored are specified on the header.

[`FrameBuffer`]: crate::core::frame_buffer::FrameBuffer
[`Slice`]: crate::core::frame_buffer::Slice
[`SliceBuilder`]: crate::core::frame_buffer::SliceBuilder
[`OutputFile`]: crate::core::output_file::OutputFile

Then a [`FrameBuffer`] is constructed and two [`Slice`]s are added to it. [`Slice`] describes the memory layout of a single channel. The constructor takes as arguments the [`PixelType`] of the [`Slice`], a pointer to the start of the [`Slice`]'s data window, and the width and height of the [`Slice`]. No further arguments are required in this example so the [`Slice`] is built immediately.

Note that unlike the C++ API, the strides of the pixels are automatically computed from the pixel type. You only need to specify the [`x_stride`](crate::core::frame_buffer::SliceBuilder::x_stride) and/or [`y_stride`](crate::core::frame_buffer::SliceBuilder::y_stride) if your pixels are not densely packed elements of the specified type (e.g. you are passing an RGBA slice and ony want to write out the R channel).

Next, constructing an [`OutputFile`] object opens the file with the
specified name, and stores the header in the file.

The [`write_pixels()`](crate::core::output_file::OutputFile::write_pixels) call copies the image's pixels from
memory into the file. As in the RGBA-only interface, the argument to
[`write_pixels()`](crate::core::output_file::OutputFile::write_pixels) specifies how many scan lines are copied into the file.
(See [Writing an RGBA Image File](#writing-and-rgba-image-file))

If the image file contains a channel for which the [`FrameBuffer`] object
has no corresponding [`Slice`], then the pixels for that channel in the
file are filled with zeroes. If the [`FrameBuffer`] object contains a
[`Slice`] for which the file has no channel, then the [`Slice`] is ignored.

Returning from function `write_gz1()` destroys the local [`OutputFile`]
object and closes the file.

## Writing a Cropped Image

Writing a cropped image using the general interface is analogous to
writing a cropped image using the RGBA-only interface, as shown in
[Writing a Cropped RGBA Image](#writing-a-cropped-rgba-image). In the file's header the data
window is set explicitly instead of being generated automatically from
the image's width and height. The number of scan lines that are stored
in the file is equal to the height of the data window, instead of the
height of the entire image. As in [Writing a Cropped RGBA Image](#writing-a-cropped-rgba-image), the example code below assumes
that the memory buffers for the pixels are large enough to hold `width`
by `height` pixels, but only the region that corresponds to the data
window will be stored in the file. For smaller memory buffers with room
only for the pixels in the data window, the `base`, `x_stride()` and
`y_stride()` arguments for the [`FrameBuffer`] object's [`Slice`]s would have to
be adjusted accordingly. (Again, see [Writing a Cropped RGBA Image](#writing-a-cropped-rgba-image).

```no_run
use openexr::prelude::*;
use half::f16;

fn write_gz2(
    filename: &str,
    g: &[f16],
    z: &[f32],
    width: i32,
    height: i32,
    data_window: &[i32; 4],
) -> Result<(), Box<dyn std::error::Error>> {

    let mut header = Header::from_dimensions(width, height);
    header.channels_mut().insert("G", &CHANNEL_HALF);
    header.channels_mut().insert("Z", &CHANNEL_FLOAT);
    *header.data_window_mut() = *data_window;

    let mut frame_buffer = FrameBuffer::new();

    frame_buffer.insert(
        "G",
        &Slice::builder(
            PixelType::Half,
            g.as_ptr() as *const u8,
            width as i64,
            height as i64,
        )
        .x_stride(std::mem::size_of::<f16>())
        .build()?
    )?;

    frame_buffer.insert(
        "Z",
        &Slice::builder(
            PixelType::Float,
            z.as_ptr() as *const u8,
            width as i64,
            height as i64,
        )
        .x_stride(std::mem::size_of::<f32>())
        .build()?
    )?;

    let mut file = OutputFile::new("write_gz1.exr", &header, 1).unwrap();
    file.set_frame_buffer(&frame_buffer).unwrap();
    unsafe { file.write_pixels(data_window[3] - data_window[1] + 1).unwrap() };

    Ok(())
}

```

## Reading an Image File

[`InputFile`]: crate::core::input_file::InputFile

In this example, we read an OpenEXR image file using the [`InputFile`] general interface. We assume that the file contains two
channels, R, and G, of type [`PixelType::Half`], and one channel, Z, of type [`PixelType::Float`].
If one of those channels is not present in the image file, the
corresponding memory buffer for the pixels will be filled with an
appropriate default value.

void

readGZ1 (const char fileName\[\],

Array2D\<half> &rPixels,

Array2D\<half> &gPixels,

Array2D\<float> &zPixels,

int &width, int &height)

{

InputFile file (fileName);

Box2i dw = file.header().dataWindow();

width = dw.max.x - dw.min.x + 1;

height = dw.max.y - dw.min.y + 1;

rPixels.resizeErase (height, width);

gPixels.resizeErase (height, width);

zPixels.resizeErase (height, width);

FrameBuffer frameBuffer;

frameBuffer.insert (\"R\", // name

Slice (HALF, // type

(char \*) (&rPixels\[0\]\[0\] - // base

dw.min.x -

dw.min.y \* width),

sizeof (rPixels\[0\]\[0\]) \* 1, // xStride

sizeof (rPixels\[0\]\[0\]) \* width,// yStride

1, 1, // x/y sampling

0.0)); // fillValue

frameBuffer.insert (\"G\", // name

Slice (HALF, // type

(char \*) (&gPixels\[0\]\[0\] - // base

dw.min.x -

dw.min.y \* width),

sizeof (gPixels\[0\]\[0\]) \* 1, // xStride

sizeof (gPixels\[0\]\[0\]) \* width,// yStride

1, 1, // x/y sampling

0.0)); // fillValue

frameBuffer.insert (\"Z\", // name

Slice (FLOAT, // type

(char \*) (&zPixels\[0\]\[0\] - // base

dw.min.x -

dw.min.y \* width),

sizeof (zPixels\[0\]\[0\]) \* 1, // xStride

sizeof (zPixels\[0\]\[0\]) \* width,// yStride

1, 1, // x/y sampling

FLT_MAX)); // fillValue

file.setFrameBuffer (frameBuffer);

file.readPixels (dw.min.y, dw.max.y);

}

First, we open the file with the specified name, by constructing an
*InputFile* object.

Using the *Array2D* class template, we allocate memory buffers for the
image\'s R, G and Z channels. The buffers are big enough to hold all
pixels in the file\'s data window.

Next, we create a *FrameBuffer* object, which describes our buffers to
the IlmImf library. For each image channel, we add a slice to the
*FrameBuffer*.

As usual, the slice\'s *type*, *xStride*, and *yStride* describe the
corresponding buffer\'s layout. For the R channel, pixel
*(dw.min.x, dw.min.y)* is at address *&rPixels\[0\]\[0\]*. By setting
the *type*, *xStride* and *yStride* of the corresponding *Slice* object
as shown above, evaluating

base + x \* xStride + y \* yStride

for pixel *(dw.min.x, dw.min.y)* produces

(char\*)(&rPixels\[0\]\[0\] - dw.min.x - dw.min.y \* width)

\+ dw.min.x \* sizeof (rPixels\[0\]\[0\]) \* 1

\+ dw.min.y \* sizeof (rPixels\[0\]\[0\]) \* width

= (char\*)&rPixels\[0\]\[0\]

\- dw.min.x \* sizeof (rPixels\[0\]\[0\])

\- dw.min.y \* sizeof (rPixels\[0\]\[0\]) \* width

\+ dw.min.x \* sizeof (rPixels\[0\]\[0\])

\+ dw.min.y \* sizeof (rPixels\[0\]\[0\]) \* width

= &rPixels\[0\]\[0\]*.*

The address calculations for pixels *(dw.min.x+1, dw.min.y)* and
*(dw.min.x, dw.min.y+1)* produce *&rPixels\[0\]\[0\]+1* and
*&rPixels\[0\]\[0\]+width*, which is equivalent to *&rPixels\[0\]\[1\]*
and *&rPixels\[1\]\[0\]*.

Each *Slice* has a *fillValue*. If the image file does not contain an
image channel for the *Slice*, then the corresponding memory buffer will
be filled with the *fillValue*.

The *Slice\'s* remaining two parameters, *xSampling* and *ySampling* are
used for images where some of the channels are subsampled, for instance,
the RY and BY channels in luminance/chroma images. (See
*[Luminance/Chroma and Gray-Scale
Images](#Luminance/Chroma%20and%20Gray-Scale%20Images)*, on page
[9](#Luminance/Chroma%20and%20Gray-Scale%20Images).) Unless an image
contains subsampled channels, *xSampling* and *ySampling* should always
be set to 1. For details see header files *ImfFrameBuffer.h* and
*ImfChannelList.h*.

After describing our memory buffers\' layout, we call *readPixels()* to
copy the pixel data from the file into the buffers. Just as with the
RGBA-only interface, *readPixels()* allows random-access to the scan
lines in the file. (See *[Reading an RGBA Image File in
Chunks](#Reading%20an%20RGBA%20Image%20File%20in%20Chunks)*, on page
[8](#Reading%20an%20RGBA%20Image%20File%20in%20Chunks).)

## Interleaving Image Channels in the Frame Buffer

Here is a variation of the previous example. We are reading an image
file, but instead of storing each image channel in a separate memory
buffer, we interleave the channels in a single buffer. The buffer is an
array of structs, which are defined like this:

typedef struct GZ

{

half g;

float z;

};

The code to read the file is almost the same as before; aside from
reading only two instead of three channels, the only difference is how
*base*, *xStride* and *yStride* for the *Slice*s in the *FrameBuffer*
object are computed:

void

readGZ2 (const char fileName\[\],

Array2D\<GZ> &pixels,

int &width, int &height)

{

InputFile file (fileName);

Box2i dw = file.header().dataWindow();

width = dw.max.x - dw.min.x + 1;

height = dw.max.y - dw.min.y + 1;

int dx = dw.min.x;

int dy = dw.min.y;

pixels.resizeErase (height, width);

FrameBuffer frameBuffer;

frameBuffer.insert (\"G\",

Slice (HALF,

(char \*) &pixels\[-dy\]\[-dx\].g,

sizeof (pixels\[0\]\[0\]) \* 1,

sizeof (pixels\[0\]\[0\]) \* width));

frameBuffer.insert (\"Z\",

Slice (FLOAT,

(char \*) &pixels\[-dy\]\[-dx\].z,

sizeof (pixels\[0\]\[0\]) \* 1,

sizeof (pixels\[0\]\[0\]) \* width));

file.setFrameBuffer (frameBuffer);

file.readPixels (dw.min.y, dw.max.y);

}

## Which Channels are in a File?

In functions `read_gz1()` and `read_gz2()`, above, we simply assumed that
the files we were trying to read contained a certain set of channels. We
relied on the IlmImf library to do \"something reasonable\" in case our
assumption was not true. Sometimes we want to know exactly what channels
are in an image file before reading any pixels, so that we can do what
we think is appropriate.

The file\'s header contains the file\'s channel list. Using iterators
similar to those in the C++ Standard Template Library, we can iterate
over the channels:

```no_run
use std::path::PathBuf;
use openexr::prelude::*;

// Open the `InputFile` and read the header
let file = InputFile::new("image.exr", 4).unwrap();

for (name, channel) in file.header().channels().iter() {
    println!("{}", name);
}

```

Channels can also be accessed by name with the [`ChannelList::get()`](crate::core::channel_list::ChannelList::get) method, which returns an [`Option`] indiciating if the channel is present in the header or not.

```no_run
use std::path::PathBuf;
use openexr::prelude::*;

// Open the `InputFile` and read the header
let file = InputFile::new("image.exr", 4).unwrap();

if let Some(channel) = file.header().channels().get("R") {
    println!("R channel type is {:?}", channel.type_);
}

```

## Layers

In an image file with many channels it is sometimes useful to group the
channels into *layers*, that is, into sets of channels that logically
belong together. Grouping channels into layers is done using a naming
convention: channel `C` in layer `L` is called `L.C`.

For example, a computer-generated picture of a 3D scene may contain a
separate set of R, G and B channels for the light that originated at
each one of the light sources in the scene. Every set of R, G, and B
channels is in its own layer. If the layers are called `light1`, `light2`,
`light3`, etc., then the full names of the channels in this image are
`light1.R`, `light1.G`, `light1.B`, `light2.R`, `light2.G`, `light2.B`, `light3.R`,
and so on.

Layers can be nested; for instance, `light1.specular.R` refers to the R
channel in the specular sub-layer of layer `light1`.

Channel names that do not contain a `.`, or that contain a `.` only
at the beginning or at the end are not considered to be part of any
layer.

[`ChannelList`]: crate::core::channel_list::ChannelList

Class [`ChannelList`] has two member functions that support per-layer
access to channels: [`layers()`](crate::core::channel_list::ChannelList::layers) returns the names of all layers in a
[`ChannelList`], and [`channels_in_layer()`](crate::core::channel_list::ChannelList::channels_in_layer) returns an iterator over the channels in the
corresponding layer.

The following code shows how to iterate over all the channels in a particular layer:

```no_run
use openexr::prelude::*;

let mut list = ChannelList::new();
let channel = Channel {
    type_: PixelType::Half.into(),
    x_sampling: 1,
    y_sampling: 1,
    p_linear: true,
};

list.insert("diffuse.R", &channel);
list.insert("diffuse.G", &channel);
list.insert("diffuse.B", &channel);

list.insert("specular.R", &channel);
list.insert("specular.G", &channel);
list.insert("specular.B", &channel);

assert_eq!(
    list.channels_in_layer("diffuse")
        .map(|(name, _)| name )
        .collect::<Vec<&str>>(),
    ["diffuse.B", "diffuse.G", "diffuse.R"]
)
```

# Tiles, Levels and Level Modes

A single tiled OpenEXR file can hold multiple versions of an image, each
with a different resolution. Each version is called a *level*. A tiled
file\'s *level mode* defines how many levels are stored in the file.
There are three different level modes:

| Mode | Explanation |
| --- | --- |
| [`LevelMode::OneLevel`](crate::core::LevelMode::OneLevel) | The file contains a single level. |
| [`LevelMode::MipmapLevels`](crate::core::LevelMode::MipmapLevels) |   The file contains multiple levels. The first level holds the image at full resolution. Each successive level is half the resolution of the previous level in x and y direction. The last level contains only a single pixel. `MipmapLevels` files are used for texture-mapping and similar applications. |
| [`LevelMode::RipmapLevels`](crate::core::LevelMode::RipmapLevels) |  Like `MipmapLevels`, but with more levels. The levels include all combinations of reducing the resolution of the image by powers of two independently in x and y direction. Used for texture mapping, like `MipmapLevels`. The additional levels in a `RipmapLevels` file can help to accelerate anisotropic filtering during texture lookups. |

In `MipmapLevels` and `RipmapLevels` mode, the size (width or height)
of each level is computed by halving the size of the level with the next
higher resolution. If the size of the higher-resolution level is odd,
then the size of the lower-resolution level must be rounded up or down
in order to avoid arriving at a non-integer width or height. The
rounding direction is determined by the file\'s *level size rounding
mode*.

Within each level, the pixels of the image are stored in a
two-dimensional array of tiles. The tiles in an OpenEXR file can be any
rectangular shape, but all tiles in a file have the same size. This
means that lower-resolution levels contain fewer, rather than smaller,
tiles.

An OpenEXR file\'s level mode and rounding mode, and the size of the
tiles are stored in an attribute in the file header. The value of this
attribute is a [`TileDescription`](crate::core::TileDescription) object:

```
pub struct TileDescription {
    pub x_size: u32,
    pub y_size: u32,
    pub mode: LevelMode,
    pub rounding_mode: LevelRoundingMode,
}

pub enum LevelMode {
    OneLevel = 0,
    MipmapLevels = 1,
    RipmapLevels = 2,
    NumLevelmodes = 3,
}

pub enum LevelRoundingMode {
    RoundDown = 0,
    RoundUp = 1,
    NumRoundingmodes = 2,
}
```

# Using the RGBA-only Interface for Tiled Files

## Writing a Tiled RGBA Image File with One Resolution Level

Writing a tiled RGBA image with a single level is easy:

```no_run
use openexr::prelude::*;

fn write_tiled_rgba1(
    filename: &str,
    pixels: &[Rgba],
    width: i32,
    height: i32,
    tile_width: i32,
    tile_height: i32,
) -> Result<(), Box<dyn std::error::Error>> {
    let header = Header::from_dimensions(width, height);

    let mut file = TiledRgbaOutputFile::new(
        filename,
        &header,
        RgbaChannels::WriteRgba,
        tile_width,
        tile_height,
        LevelMode::OneLevel,
        LevelRoundingMode::RoundDown,
        1,
    )?;

    file.set_frame_buffer(&pixels, 1, width as usize)?;
    file.write_tiles(
        0,
        file.num_x_tiles(0)? - 1,
        0,
        file.num_y_tiles(0)? - 1,
        0,
        0,
    )?;
    
    Ok(())
}
```
[`TiledRgbaOutputFile`]: crate::tiled::tiled_rgba_file::TiledRgbaOutputFile;

Opening the file and defining the pixel data layout in memory are done
in almost the same way as for scan line based files:

Construction of the [`TiledRgbaOutputFile`] object, with `TiledRgbaOutputFile::new()` creates an OpenEXR header, sets the header's attributes, opens the file with the
specified name, and stores the header in the file. The header's display
window and data window are both set to `(0, 0) - (width-1, height-1)`.
The size of each tile in the file will be `tile_width` by `tile_height`
pixels. The channel list contains four channels, R, G, B, and A, of type
[`PixelType::Half`].

[`set_frame_buffer()`](crate::tiled::tiled_rgba_file::TiledRgbaOutputFile::set_frame_buffer) sets the `pixels` slice as the source for the image data, specifying the `x_stride` as 1 and `y_stride` as the width of the image, since the slice is denssely packed.

[`write_tiles()`](crate::tiled::tiled_rgba_file::TiledRgbaOutputFile::write_tiles) copies the pixels into the file. The first four arguments specify the x and y ranges of tiles to write, and we can use [`TiledRgbaOutputFile`]'s [`num_x_tiles()`](crate::tiled::tiled_rgba_file::TiledRgbaOutputFile::num_x_tiles) and [`num_y_tiles()`](crate::tiled::tiled_rgba_file::TiledRgbaOutputFile::num_y_tiles) to specify the full set of tiles in the image.

<!---
This simple method works well when enough memory is available to
allocate a frame buffer for the entire image. When allocating a frame
buffer for the whole image is not desirable, for example because the
image is very large, a smaller frame buffer can be used. Even a frame
buffer that can hold only a single tile is sufficient, as demonstrated
in the following example:

void

writeTiledRgbaONE2 (const char fileName\[\],

int width, int height,

int tileWidth, int tileHeight)

{

TiledRgbaOutputFile out (fileName,

width, height, // image size

tileWidth, tileHeight, // tile size

ONE_LEVEL, // level mode

ROUND_DOWN, // rounding mode

WRITE_RGBA); // channels in file // 1

Array2D\<Rgba> pixels (tileHeight, tileWidth); // 2

for (int tileY = 0; tileY \< out.numYTiles (); ++tileY) // 3

{

for (int tileX = 0; tileX \< out.numXTiles (); ++tileX) // 4

{

Box2i range = out.dataWindowForTile (tileX, tileY); // 5

generatePixels (pixels, width, height, range); // 6

out.setFrameBuffer (&pixels\[-range.min.y\]\[-range.min.x\],

1, // xStride

tileWidth); // yStride // 7

out.writeTile (tileX, tileY); // 8

}

}

}

In line 2 we allocate a *pixels* array with *tileWidth\*tileHeight*
elements, which is just enough for one tile. Line 5 computes the data
window range for each tile, that is, the set of pixel coordinates
covered by the tile. The *generatePixels()* function, in line 6, fills
the *pixels* array with one tile\'s worth of image data. The same
*pixels* array is reused for all tiles. We must call *setFrameBuffer()*,
in line 7, before writing each tile so that the pixels in the array are
accessed properly in the *writeTile()* call in line 8. Again, the
address arithmetic to access the pixels is the same as for scan line
based files. The values for the *base*, *xStride*, and *yStride*
arguments to the *setFrameBuffer()* call must be chosen so that
evaluating the expression

base + x \* xStride + y \* yStride

produces the address of the pixel with coordinates *(x,y)*.

## Writing a Tiled RGBA Image File with Mipmap Levels

In order to store a multi-resolution image in a file, we can allocate a
frame buffer large enough for the highest-resolution level, *(0,0)*, and
reuse it for all levels:

void

writeTiledRgbaMIP1 (const char fileName\[\],

int width, int height,

int tileWidth, int tileHeight)

{

TiledRgbaOutputFile out (fileName,

width, height,

tileWidth, tileHeight,

MIPMAP_LEVELS,

ROUND_DOWN,

WRITE_RGBA); // 1

Array2D\<Rgba> pixels (height, width); // 2

out.setFrameBuffer (&pixels\[0\]\[0\], 1, width); // 3

for (int level = 0; level \< out.numLevels (); ++level) // 4

{

generatePixels (pixels, width, height, level); // 5

out.writeTiles (0, out.numXTiles (level) -- 1, // 6

0, out.numYTiles (level) -- 1,

level);

}

}

The main difference here is the use of *MIPMAP_LEVELS* in line 1 for the
*TiledRgbaOutputFile* constructor. This signifies that the file will
contain multiple levels, each level being a factor of 2 smaller in both
dimensions than the previous level. Mipmap images contain *n* levels,
with level numbers

(0,0), (1,1), \... (n-1,n-1),

where

n = floor (log (max (width, height)) / log (2)) + 1

if the level size rounding mode is *ROUND_DOWN*, or

n = ceil (log (max (width, height)) / log (2)) + 1

if the level size rounding mode is *ROUND_UP*. Note that even though
level numbers are pairs of integers, *(lx,ly)*, only levels where *lx*
equals *ly* are used in *MIPMAP_LEVELS* files.

Line 2 allocates a *pixels* array with *width* by *height* pixels, big
enough to hold the highest-resolution level.

In order to store all tiles in the file, we must loop over all levels in
the image (line 4). *numLevels()* returns the number of levels, *n*, in
our mipmapped image. Since the tile sizes remain the same in all levels,
the number of tiles in both dimensions varies between levels.
*numXTiles()* and *numYTiles()* take a level number as an optional
argument, and return the number of tiles in the x or y direction for the
corresponding level. Line 5 fills the *pixels* array with appropriate
data for each level, and line 6 stores the pixel data in the file.

As with *ONE_LEVEL* images, we can choose to only allocate a frame
buffer for a single tile and reuse it for all tiles in the image:

void

writeTiledRgbaMIP2 (const char fileName\[\],

int width, int height,

int tileWidth, int tileHeight)

{

TiledRgbaOutputFile out (fileName,

width, height,

tileWidth, tileHeight,

MIPMAP_LEVELS,

ROUND_DOWN,

WRITE_RGBA);

Array2D\<Rgba> pixels (tileHeight, tileWidth);

for (int level = 0; level \< out.numLevels (); ++level)

{

for (int tileY = 0; tileY \< out.numYTiles (level); ++tileY)

{

for (int tileX = 0; tileX \< out.numXTiles (level); ++tileX)

{

Box2i range = out.dataWindowForTile (tileX, tileY, level);

generatePixels (pixels, width, height, range, level);

out.setFrameBuffer (&pixels\[-range.min.y\]\[-range.min.x\],

1, // xStride

tileWidth); // yStride

out.writeTile (tileX, tileY, level);

}

}

}

}

The structure of this code is the same as for writing a *ONE_LEVEL*
image using a tile-sized frame buffer, but we have to loop over more
tiles. Also, *dataWindowForTile()* takes an additional level argument to
determine the pixel range for the tile at the specified level.

## Writing a Tiled RGBA Image File with Ripmap Levels

The ripmap level mode allows for storing all combinations of reducing
the resolution of the image by powers of two independently in both
dimensions. Ripmap files contains *nx\*ny* levels, with level numbers:

(0, 0), (1, 0), \... (nx-1, 0),

(0, 1), (1, 1), \... (nx-1, 1),

\...

(0,ny-1), (1,ny-1), \... (nx-1,ny-1)

where

nx = floor (log (width) / log (2)) + 1

ny = floor (log (height) / log (2)) + 1

if the level size rounding mode is *ROUND_DOWN*, or

nx = ceil (log (width) / log (2)) + 1

ny = ceil (log (height) / log (2)) + 1

if the level size rounding mode is *ROUND_UP*.

With a frame buffer that is large enough to hold level *(0,0)*, we can
write a ripmap file like this:

void

writeTiledRgbaRIP1 (const char fileName\[\],

int width, int height,

int tileWidth, int tileHeight)

{

TiledRgbaOutputFile out (fileName,

width, height,

tileWidth, tileHeight,

RIPMAP_LEVELS,

ROUND_DOWN,

WRITE_RGBA);

Array2D\<Rgba> pixels (height, width);

out.setFrameBuffer (&pixels\[0\]\[0\], 1, width);

for (int yLevel = 0; yLevel \< out.numYLevels (); ++yLevel)

{

for (int xLevel = 0; xLevel \< out.numXLevels (); ++xLevel)

{

generatePixels (pixels, width, height, xLevel, yLevel);

out.writeTiles (0, out.numXTiles (xLevel) - 1,

0, out.numYTiles (yLevel) -- 1,

xLevel,

yLevel);

}

}

}

As for *ONE_LEVEL* and *MIPMAP_LEVELS* files, the frame buffer doesn\'t
have to be large enough to hold a whole level. Any frame buffer big
enough to hold at least a single tile will work.

-->

## Reading a Tiled RGBA Image File

Reading a tiled RGBA image file is done similarly to writing one:

```no_run
use imath_traits::Zero;
use openexr::prelude::*;

fn read_tiled_rgba1(
    filename: &str,
) -> Result<Vec<Rgba>, Box<dyn std::error::Error>> {
    let mut file = TiledRgbaInputFile::new(filename, 1)?;
    let data_window = file.header().data_window::<[i32; 4]>().clone();
    let width = data_window[2] - data_window[0] + 1;
    let height = data_window[3] - data_window[1] + 1;

    let mut pixels = vec![Rgba::zero(); (width * height) as usize];
    file.set_frame_buffer(&mut pixels, 1, width as usize)?;

    file.read_tiles(
        0,
        file.num_x_tiles(0)? - 1,
        0,
        file.num_y_tiles(0)? - 1,
        0,
        0,
    )?;

    Ok(pixels)
}
```

[`TiledRgbaInputFile`]: crate::tiled::tiled_rgba_file::TiledRgbaInputFile

First we need to create a [`TiledRgbaInputFile`] object for the given file
name. We then retrieve information about the data window in order to
create an appropriately sized frame buffer, in this case large enough to
hold the whole image at level `(0,0)`. After we set the frame buffer, we
read the tiles from the file.

This example only reads the highest-resolution level of the image. It
can be extended to read all levels, for multi-resolution images, by also
iterating over all levels within the image, analogous to the examples in
[Writing a Tiled RGBA Image File with Mipmap
Levels](#writing-a-tiled-rgba-image-file-with-mipmap-levels)
and [Writing a Tiled RGBA Image File with Ripmap
Levels](#writing-a-tiled-rgba-image-file-with-ripmap-levels).

# Using the General Interface for Tiled Files

## Writing a Tiled Image File

This example is a variation of the one in *[Writing an Image
File](#Writing%20an%20Image%20File)*, on page
[10](#Writing%20an%20Image%20File). We are writing a *ONE_LEVEL* image
file with two channels, G, and Z, of type *HALF*, and *FLOAT*
respectively, but here the file is tiled instead of scan line based:

void

writeTiled1 (const char fileName\[\],

Array2D\<GZ> &pixels,

int width, int height,

int tileWidth, int tileHeight)

{

Header header (width, height); // 1

header.channels().insert (\"G\", Channel (HALF)); // 2

header.channels().insert (\"Z\", Channel (FLOAT)); // 3

header.setTileDescription

(TileDescription (tileWidth, tileHeight, ONE_LEVEL)); // 4

TiledOutputFile out (fileName, header); // 5

FrameBuffer frameBuffer; // 6

frameBuffer.insert (\"G\", // name // 7

Slice (HALF, // type // 8

(char \*) &pixels\[0\]\[0\].g, // base // 9

sizeof (pixels\[0\]\[0\]) \* 1, // xStride // 10

sizeof (pixels\[0\]\[0\]) \* width)); // yStride // 11

frameBuffer.insert (\"Z\", // name // 12

Slice (FLOAT, // type // 13

(char \*) &pixels\[0\]\[0\].z, // base // 14

sizeof (pixels\[0\]\[0\]) \* 1, // xStride // 15

sizeof (pixels\[0\]\[0\]) \* width)); // yStride // 16

out.setFrameBuffer (frameBuffer); // 17

out.writeTiles (0, out.numXTiles() - 1, 0, out.numYTiles() - 1); // 18

}

As one would expect, the code here is very similar to the code in
*[Writing an Image File](#Writing%20an%20Image%20File)* on page
[10](#Writing%20an%20Image%20File). The file\'s header is created in
line 1, while lines 2 and 3 specify the names and types of the image
channels that will be stored in the file. An important addition is line
4, where we define the size of the tiles and the level mode. In this
example we use *ONE_LEVEL* for simplicity. Line 5 opens the file and
writes the header. Lines 6 through 17 tell the *TiledOutputFile* object
the location and layout of the pixel data for each channel. Finally,
line 18 stores the tiles in the file.

## Reading a Tiled Image File

Reading a tiled file with the general interface is virtually identical
to reading a scan line based file, as shown in *[Interleaving Image
Channels in the Frame
Buffer](#Interleaving%20Image%20Channels%20in%20the%20Frame%20Buffer)*,
on page
[14](#Interleaving%20Image%20Channels%20in%20the%20Frame%20Buffer); only
the last three lines are different. Instead of reading all scan lines at
once with a single function call, here we must iterate over all tiles we
want to read.

void

readTiled1 (const char fileName\[\],

Array2D\<GZ> &pixels,

int &width, int &height)

{

TiledInputFile in (fileName);

Box2i dw = in.header().dataWindow();

width = dw.max.x - dw.min.x + 1;

height = dw.max.y - dw.min.y + 1;

int dx = dw.min.x;

int dy = dw.min.y;

pixels.resizeErase (height, width);

FrameBuffer frameBuffer;

frameBuffer.insert (\"G\",

Slice (HALF,

(char \*) &pixels\[-dy\]\[-dx\].g,

sizeof (pixels\[0\]\[0\]) \* 1,

sizeof (pixels\[0\]\[0\]) \* width));

frameBuffer.insert (\"Z\",

Slice (FLOAT,

(char \*) &pixels\[-dy\]\[-dx\].z,

sizeof (pixels\[0\]\[0\]) \* 1,

sizeof (pixels\[0\]\[0\]) \* width));

in.setFrameBuffer (frameBuffer);

in.readTiles (0, in.numXTiles() - 1, 0, in.numYTiles() - 1);

}

In this example we assume that the file we want to read contains two
channels, G and Z, of type *HALF* and *FLOAT* respectively. If the file
contains other channels, we ignore them. We only read the
highest-resolution level of the image. If the input file contains more
levels (*MIPMAP_LEVELS* or *MIPMAP_LEVELS*), we can access the extra
levels by calling a four-argument version of the *readTile()* function:

in.readTile (tileX, tileY, levelX, levelY);

or by calling a six-argument version of *readTiles()*:

in.readTiles (tileXMin, tileXMax, tileYMin, tileYMax, levelX, levelY);

# Deep Data Files (New in 2.0) {#deep-data-files-new-in-20}

## Writing a Deep Scan Line File

This example creates an deep scan line file with two channels. It
demonstrates how to write a deep scan line file with two channels:

1.  type *FLOAT*, is called Z, and is used for storing sample depth, and
2.  type *HALF*, is called A and is used for storing sample opacity.

The size of the image is *width* by *height* pixels.

void writeDeepScanlineFile(const char filename\[\],

Box2i displayWindow,

Box2i dataWindow,

Array2D\< float\* \> &dataZ,

Array2D\< half\* \> &dataA,

Array2D\< unsigned int \> sampleCount)

{

int height = dataWindow.max.y - dataWindow.min.y + 1;

int width = dataWindow.max.x - dataWindow.min.x + 1;

Header header(displayWindow, dataWindow);

header.channels().insert(\"Z\", Channel(FLOAT));

header.channels().insert(\"A\", Channel(HALF));

header.setType(DEEPSCANLINE);

header.compression() = ZIP_COMPRESSION;

DeepScanLineOutputFile file(filename, header);

DeepFrameBuffer frameBuffer;

frameBuffer.insertSampleCountSlice (Slice (UINT,

(char \*) (&sampleCount\[0\]\[0\]

\- dataWindow.min.x

\- dataWindow.min.y \* width),

sizeof (unsigned int) \* 1, // xStride

sizeof (unsigned int) \* width)); // yStride

frameBuffer.insert (\"Z\",

DeepSlice (FLOAT,

(char \*) (&dataZ\[0\]\[0\]

\- dataWindow.min.x

\- dataWindow.min.y \* width),

sizeof (float \*) \* 1, // xStride for pointer array

sizeof (float \*) \* width, // yStride for pointer array

sizeof (float) \* 1)); // stride for Z data sample

frameBuffer.insert (\"A\",

DeepSlice (HALF,

(char \*) (&dataA\[0\]\[0\]

\- dataWindow.min.x

\- dataWindow.min.y \* width),

sizeof (half \*) \* 1, // xStride for pointer array

sizeof (half \*) \* width, // yStride for pointer array

sizeof (half) \* 1)); // stride for A data sample

file.setFrameBuffer(frameBuffer);

file.readPixelSampleCounts (height);

for (int i = 0; i \< height; i++)

{

for (int j = 0; j \< width; j++)

{

sampleCount\[i\]\[j\] = getPixelSampleCount(i,j);

dataZ\[i\]\[j\] = new float\[sampleCount\[i\]\[j\]\];

dataA\[i\]\[j\] = new half\[sampleCount\[i\]\[j\]\];

// Generate data for dataZ and dataA.

}

file.writePixels(1);

}

for (int i = 0; i \< height; i++)

for (int j = 0; j \< width; j++)

{

delete\[\] dataZ\[i\]\[j\];

delete\[\] dataA\[i\]\[j\];

}

}

The interface for deep scan line files is similar to scan line files. We
added two new classes to deal with deep data: *DeepFrameBuffer* and
*DeepSlice*. *DeepFrameBuffer* only accepts *DeepSlice* as its input,
except that it accepts *Slice* for sample count slice. The first
difference we see from the previous version is:

header.setType(DEEPSCANLINE);

where we set the type of the header to a predefine string
*DEEPSCANLINE*, then we insert a sample count slice using
*insertSampleCountSlice()*. After that, we insert a *DeepSlice* with
deep z data. Notice that deep slices have three strides, one more than
non-deep slices. The first two strides are used for the pointers in the
array. Because the memory space for *Array2D* is contiguous, we can get
the strides easily. The third stride is used for pixel samples. Because
the data type is float (and we are not interleaving), the stride should
be sizeof(float). If we name the stride for deep data samples
*sampleStride*, then the memory address of the i-th sample of this
channel in pixel (x, y) is

base +

x \* xStride +

y \* yStride +

i \* sampleStride

Because we may not know the data until we are going to write it, the
deep data file must support postponed initialization, as shown in the
example code. Another approach would be to prepare all the data first,
and then write it all out at once.

Once the slices have been inserted, we get the sample count for each
pixel, via a user-supplied *getPixelSampleCount()* function, and
dynamically allocate memory for the Z and A channels. We then write to
file in a line-by-line fashion and finally free the the intermediate
data structures.

## Reading a Deep Scan Line File

An example of reading a deep scan line file created by previous code.

void readDeepScanlineFile(const char filename\[\],

Box2i& displayWindow,

Box2i& dataWindow,

Array2D\< float\* \>& dataZ,

Array2D\< half\* \>& dataA,

Array2D\< unsigned int \>& sampleCount)

{

DeepScanLineInputFile file(filename);

const Header& header = file.header();

dataWindow = header.dataWindow();

displayWindow = header.displayWindow();

int width = dataWindow.max.x - dataWindow.min.x + 1;

int height = dataWindow.max.y - dataWindow.min.y + 1;

sampleCount.resizeErase(height, width);

dataZ.resizeErase(height, width);

dataA.resizeErase(height, width);

DeepFrameBuffer frameBuffer;

frameBuffer.insertSampleCountSlice (Slice (UINT,

(char \*) (&sampleCount\[0\]\[0\]

\- dataWindow.min.x

\- dataWindow.min.y \* width),

sizeof (unsigned int) \* 1, // xStride

sizeof (unsigned int) \* width)); // yStride

frameBuffer.insert (\"dataZ\",

DeepSlice (FLOAT,

(char \*) (&dataZ\[0\]\[0\]

\- dataWindow.min.x

\- dataWindow.min.y \* width),

sizeof (float \*) \* 1, // xStride for pointer array

sizeof (float \*) \* width, // yStride for pointer array

sizeof (float) \* 1)); // stride for Z data sample

frameBuffer.insert (\"dataA\",

DeepSlice (HALF,

(char \*) (&dataA\[0\]\[0\]

\- dataWindow.min.x

\- dataWindow.min.y \* width),

sizeof (half \*) \* 1, // xStride for pointer array

sizeof (half \*) \* width, // yStride for pointer array

sizeof (half) \* 1)); // stride for O data sample

file.setFrameBuffer(frameBuffer);

file.readPixelSampleCounts(dataWindow.min.y, dataWindow.max.y);

for (int i = 0; i \< height; i++)

for (int j = 0; j \< width; j++)

{

dataZ\[i\]\[j\] = new float\[sampleCount\[i\]\[j\]\];

dataA\[i\]\[j\] = new half\[sampleCount\[i\]\[j\]\];

}

file.readPixels(dataWindow.min.y, dataWindow.max.y);

for (int i = 0; i \< height; i++)

for (int j = 0; j \< width; j++)

{

delete\[\] dataZ\[i\]\[j\];

delete\[\] dataA\[i\]\[j\];

}

}

The interface for deep scan line files is similar to scan line files.
The main the difference is we use the sample count slice and deep data
slices. To do this, we added a new method to read the sample count table
from the file:

file.readPixelSampleCounts(dataWindow.min.y, dataWindow.max.y);

This method reads all pixel sample counts in the range
*\[dataWindow.min.y, dataWindow.max.y\]*, and stores the data to sample
count slice in framebuffer.

*ReadPixels()* supports for postponed memory allocation.

## Writing a Deep Tiled File

This example creates an deep tiled file with two channels. It
demonstrates how to write a deep tiled file with two channels:

1.  type *FLOAT*, is called Z, and is used for storing sample depth, and
2.  type *HALF*, is called A and is used for storing sample opacity.

The size of the image is *width* by *height* pixels.

void writeDeepTiledFile(const char filename\[\],

Box2i displayWindow,

Box2i dataWindow,

int tileSizeX, int tileSizeY)

{

int height = dataWindow.max.y - dataWindow.min.y + 1;

int width = dataWindow.max.x - dataWindow.min.x + 1;

Header header(displayWindow, dataWindow);

header.channels().insert(\"Z\", Channel(FLOAT));

header.channels().insert(\"A\", Channel(HALF));

header.setType(DEEPTILE);

header.setTileDescription(

TileDescription(tileSizeX, tileSizeY, MIPMAP_LEVELS));

Array2D\< unsigned int\* \> dataZ;

dataZ.resizeErase(height, width);

Array2D\< unsigned int\* \> dataA;

dataA.resizeErase(height, width);

Array2D\<unsigned int> sampleCount;

sampleCount.resizeErase(height, width);

DeepTiledOutputFile file(filename, header);

DeepFrameBuffer frameBuffer;

frameBuffer.insertSampleCountSlice (Slice (UINT,

(char \*) (&sampleCount\[0\]\[0\]

\- dataWindow.min.x

\- dataWindow.min.y \* width),

sizeof (unsigned int) \* 1, // xStride

sizeof (unsigned int) \* width)); // yStride

frameBuffer.insert (\"Z\",

DeepSlice (FLOAT,

(char \*) (&dataZ\[0\]\[0\]

\- dataWindow.min.x

\- dataWindow.min.y \* width),

sizeof (float \*) \* 1, // xStride for pointer array

sizeof (float \*) \* width, // yStride for pointer array

sizeof (float) \* 1)); // stride for samples

frameBuffer.insert (\"A\",

DeepSlice (HALF,

(char \*) (&dataA\[0\]\[0\]

\- dataWindow.min.x

\- dataWindow.min.y \* width),

sizeof (half \*) \* 1, // xStride for pointer array

sizeof (half \*) \* width, // yStride for pointer array

sizeof (half) \* 1)); // stride for samples

file.setFrameBuffer(frameBuffer);

for (int l = 0; l \< file.numLevels(); l++)

{

for (int j = 0; j \< file.numYTiles(l); j++)

{

for (int i = 0; i \< file.numXTiles(l); i++)

{

getSampleCountForTile(i, j, sampleCount);

getSampleDataForTile(i, j, dataZ, dataA);

file.writeTile(i, j, l);

}

}

}

}

Here, getSampleCountForTile is a user-supplied function that sets each
item in sampleCount array to the correct sampleCount for each pixel in
the tile, and getSampleDataForTile is a user-supplied function that set
the pointers in dataZ and dataA arrays to point to the correct data

The interface for deep tiled files is similar to tiled files. The
differences are:

-   we set the type of the header to *DEEPTILE*
-   we use *insertSampleCountSlice()* to set sample count slice, and
-   we use *DeepSlice* instead of *Slice* to provide three strides
    needed by the library.

Also, we support postponed initialization.

## Reading a Deep Tiled File

An example of reading a deep tiled file created by code explained in the
*[Writing a Deep Tiled File](#Writing%20a%20Deep%20Tiled%20File)*
section, on page [26](#Writing%20a%20Deep%20Tiled%20File).

void readDeepTiledFile(const char filename\[\],

Box2i& displayWindow,

Box2i& dataWindow,

Array2D\< float\* \>& dataZ,

Array2D\< half\* \>& dataA,

Array2D\< unsigned int \>& sampleCount)

{

DeepTiledInputFile file(filename);

int width = dataWindow.max.x - dataWindow.min.x + 1;

int height = dataWindow.max.y - dataWindow.min.y + 1;

sampleCount.resizeErase(height, width);

dataZ.resizeErase(height, width);

dataA.resizeErase(height, width);

DeepFrameBuffer frameBuffer;

frameBuffer.insertSampleCountSlice (Slice (UINT,

(char \*) (&sampleCount\[0\]\[0\]

\- dataWindow.min.x

\- dataWindow.min.y \* width),

sizeof (unsigned int) \* 1, // xStride

sizeof (unsigned int) \* width)); // yStride

frameBuffer.insert (\"Z\",

DeepSlice (FLOAT,

(char \*) (&dataZ\[0\]\[0\]

\- dataWindow.min.x

\- dataWindow.min.y \* width),

sizeof (float \*) \* 1, // xStride for pointer array

sizeof (float \*) \* width, // yStride for pointer array

sizeof (float) \* 1)); // stride for samples

frameBuffer.insert (\"A\",

DeepSlice (HALF,

(char \*) (&dataA\[0\]\[0\]

\- dataWindow.min.x

\- dataWindow.min.y \* width),

sizeof (half \*) \* 1, // xStride for pointer array

sizeof (half \*) \* width, // yStride for pointer array

sizeof (half) \* 1)); // stride for samples

file.setFrameBuffer(frameBuffer);

int numXTiles = file.numXTiles(0);

int numYTiles = file.numYTiles(0);

file.readPixelSampleCounts(0, numXTiles - 1, 0, numYTiles - 1);

for (int i = 0; i \< height; i++)

for (int j = 0; j \< width; j++)

{

dataZ\[i\]\[j\] = new float\[sampleCount\[i\]\[j\]\];

dataA\[i\]\[j\] = new half\[sampleCount\[i\]\[j\]\];

}

file.readTiles(0, numXTiles - 1, 0, numYTiles -- 1);

// (after read data is processed, data must be freed:)

for (int i = 0; i \< height; i++)

for (int j = 0; j \< width; j++)

{

delete\[\] dataZ\[i\]\[j\];

delete\[\] dataA\[i\]\[j\];

}

}

This code demonstrates how to read the first level of a deep tiled file
created by code explained in the *[Writing a Deep Tiled
File](#Writing%20a%20Deep%20Tiled%20File)* section, on page
[26](#Writing%20a%20Deep%20Tiled%20File). The interface for deep tiled
files is similar to tiled files. The differences are:

-   we use *insertSampleCountSlice()* to set sample count slice
-   we use *DeepSlice* instead of *Slice* to provide three strides
    needed by the library, and
-   we use *readPixelSampleCounts()* to read in pixel sample counts into
    array.

Also we support postponed memory allocation.

In this example, entries in dataZ and dataA have been allocated by the
\'new\' calls must be deleted after use.

<!---
# Threads

## Library Thread-Safety

The IlmImf library is thread-safe. In a multithreaded application
program, multiple threads can concurrently read and write distinct
OpenEXR files. In addition, accesses to a single shared file by multiple
application threads are automatically serialized. In other words, each
thread can independently create, use and destroy its own input and
output file objects. Multiple threads can also share a single input or
output file. In the latter case the IlmImf library uses mutual exclusion
to ensure that only one thread at a time can access the shared file.

## Multithreaded I/O

The IlmImf library supports multithreaded file input and output where
the library creates its own worker threads that are independent of the
application program\'s threads. When an application thread calls
*readPixels()*, *readTiles()*, *writePixels()* or *writeTiles()* to read
or write multiple scan lines or tiles at once, the library\'s worker
threads process the tiles or scanlines in parallel.

During startup, the application program must enable multithreading by
calling function *setGlobalThreadCount()*. This tells the IlmImf library
how many worker threads it should create. (As a special case, setting
the number of worker threads to zero reverts to single-threaded
operation; reading and writing image files happens entirely in the
application thread that calls the IlmImf library.)

The application program should read or write as many scan lines or tiles
as possible in each call to *readPixels()*, *readTiles()*,
*writePixels()* or *writeTiles()*. This allows the library to break up
the work into chunks that can be processed in parallel. Ideally the
application reads or writes the entire image using a single read or
write call. If the application reads or writes the image one scan line
or tile at a time, the library reverts to single-threaded file I/O.

The following function writes an RGBA file using four concurrent worker
threads:

void

writeRgbaMT (const char fileName\[\],

const Rgba \*pixels,

int width,

int height)

{

setGlobalThreadCount (4);

RgbaOutputFile file (fileName, width, height, WRITE_RGBA);

file.setFrameBuffer (pixels, 1, width);

file.writePixels (height);

}

Except for the call to *setGlobalThreadCount()*, function
*writeRgbaMT()* is identical to function *writeRgba1()* in *[Writing an
RGBA Image File](#Writing%20an%20RGBA%20Image%20File)*, on page
[4](#Writing%20an%20RGBA%20Image%20File), but on a computer with
multiple processors *writeRgbaMT()* writes files significantly faster
than *writeRgba1()*.

## Multithreaded I/O, Multithreaded Application Program

Function *setGlobalThreadCount()* creates a global pool of worker
threads inside the IlmImf library. If an application program has
multiple threads, and those threads read or write several OpenEXR files
at the same time, then the worker threads must be shared among the
application threads. By default each file will attempt to use the entire
worker thread pool for itself. If two files are read or written
simultaneously by two application threads, then it is possible that all
worker threads perform I/O on behalf of one of the files, while I/O for
the other file is stalled.

In order to avoid this situation, the constructors for input and output
file objects take an optional *numThreads* argument. This gives the
application program more control over how many threads will be kept busy
reading or writing a particular file.

For example, we may have an application program that runs on a
four-processor computer. The program has one thread that reads files and
another one that writes files. We want to keep all four processors busy,
and we want to split the processors evenly between input and output.
Before creating the input and output threads, the application instructs
the IlmImf library to create four worker threads:

// main, before application threads are created:

setGlobalThreadCount (4);

In the input and output threads, input and output files are opened with
*numThreads* set to 2:

// application\'s input thread

InputFile in (fileName, 2);

\...

// application\'s output thread

OutputFile out (fileName, header, 2);

\...

This ensures that file input and output in the application\'s two
threads can proceed concurrently, without one thread stalling the
other\'s I/O.

An alternative approach for thread management of multithreaded
applications is provided for deep scanline input files. Rather than
calling *setFrameBuffer()*, the host application may call
*rawPixelData()* to load a chunk of scanlines into a host-application
managed memory store, then pass a DeepFrameBuffer object and the raw
data to *readPixelSampleCounts()* and *readPixels()*. Only the call to
rawPixelData blocks; decompressing the underlying data and copying it to
the framebuffer will happen on the host application\'s threads
independently. This strategy is generally **less efficient** than
reading multiple scanlines at the same time and allowing OpenEXR\'s
thread management to decode the file, but may prove effective when the
host application has many threads available, cannot avoid accessing
scanlines in a random order and wishes to avoid caching an entire
uncompressed image. For more details, refer to the inline comments in
ImfDeepScanLineInputFile.h

# Low-Level I/O

## Custom Low-Level File I/O

In all of the previous file reading and writing examples, we were given
a file name, and we relied on the constructors for our input file or
output file objects to open the file. In some contexts, for example, in
a plugin for an existing application program, we may have to read from
or write to a file that has already been opened. The representation of
the open file as a C or C++ data type depends on the application program
and on the operating system.

At its lowest level, the IlmImf library performs file I/O via objects of
type *IStream* and *OStream*. *IStream* and *OStream* are abstract base
classes. The IlmImf library contains two derived classes, *StdIFStream*
and *StdOFStream*, that implement reading from *std::ifstream* and
writing to *std::ofstream* objects. An application program can implement
alternative file I/O mechanisms by deriving its own classes from
*Istream* and *Ostream*. This way, OpenEXR images can be stored in
arbitrary file-like objects, as long as it is possible to support read,
write, seek and tell operations with semantics similar to the
corresponding *std::ifstream* and *std::ofstream* methods.

For example, assume that we want to read an OpenEXR image from a C stdio
file (of type *FILE \**) that has already been opened. To do this, we
derive a new class, *C_IStream*, from *IStream*. The declaration of
class *IStream* looks like this:

class IStream

{

public:

virtual \~IStream ();

virtual bool read (char c\[\], int n) = 0;

virtual Int64 tellg () = 0;

virtual void seekg (Int64 pos) = 0;

virtual void clear ();

const char \* fileName () const;

virtual bool isMemoryMapped () const;

virtual char \* readMemoryMapped (int n);

protected:

IStream (const char fileName\[\]);

private:

\...

};

Our derived class needs a public constructor, and it must override four
methods:

class C_IStream: public IStream

{

public:

C_IStream (FILE \*file, const char fileName\[\]):

IStream (fileName), \_file (file) {}

virtual bool read (char c\[\], int n);

virtual Int64 tellg ();

virtual void seekg (Int64 pos);

virtual void clear ();

private:

FILE \* \_file;

};

*read(c,n)* reads *n* bytes from the file, and stores them in array *c*.
If reading hits the end of the file before *n* bytes have been read, or
if an I/O error occurs, *read(c,n)* throws an exception. If *read(c,n)*
hits the end of the file after reading *n* bytes, it returns *false*,
otherwise it returns *true*:

bool

C_IStream::read (char c\[\], int n)

{

if (n != fread (c, 1, n, \_file))

{

// fread() failed, but the return value does not distinguish

// between I/O errors and end of file, so we call ferror() to

// determine what happened.

if (ferror (\_file))

Iex::throwErrnoExc();

else

throw Iex::InputExc (\"Unexpected end of file.\");

}

return feof (\_file);

}

*tellg()* returns the current reading position, in bytes, from the
beginning of the file. The next *read()* call will begin reading at the
indicated position:

Int64

C_IStream::tellg ()

{

return ftell (\_file);

}

*seekg(pos)* sets the current reading position to *pos* bytes from the
beginning of the file:

void

C_IStream::seekg (Int64 pos)

{

clearerr (\_file);

fseek (\_file, pos, SEEK_SET);

}

*clear()* clears any error flags that may be set on the file after a
*read()* or *seekg()* operation has failed:

void

C_IStream::clear ()

{

clearerr (\_file);

}

In order to read an RGBA image from an open C stdio file, we first make
a *C_IStream* object. Then we create an *RgbaInputFile*, passing the
*C_IStream* instead of a file name to the constructor. After that, we
read the image as usual (see *[Reading an RGBA Image
File](#Reading%20an%20RGBA%20Image%20File)*, on page
[7](#Reading%20an%20RGBA%20Image%20File)):

void

readRgbaFILE (FILE \*cfile,

const char fileName\[\],

Array2D\<Rgba> &pixels,

int &width,

int &height)

{

C_IStream istr (cfile, fileName);

RgbaInputFile file (istr);

Box2i dw = file.dataWindow();

width = dw.max.x - dw.min.x + 1;

height = dw.max.y - dw.min.y + 1;

pixels.resizeErase (height, width);

file.setFrameBuffer (&pixels\[0\]\[0\] - dw.min.x - dw.min.y \* width,
1, width);

file.readPixels (dw.min.y, dw.max.y);

}

## Memory-Mapped I/O

When the IlmImf library reads an image file, pixel data are copied
several times on their way from the file to the application\'s frame
buffer. For compressed files, the time spent copying is usually not
significant when compared to how long it takes to uncompress the data.
However, when uncompressed image files are being read from a fast file
system, it may be advantageous to eliminate one or two copy operations
by using memory-mapped I/O.

Memory-mapping establishes a relationship between a file and a
program\'s virtual address space, such that from the program\'s point of
view the file looks like an array of type *char*. The contents of the
array match the data in the file. This allows the program to access the
data in the file directly, bypassing any copy operations associated with
reading the file via a C++ *std::ifstream* or a C *FILE \**.

Classes derived from *IStream* can optionally support memory-mapped
input. In order to do this, a derived class must override two virtual
functions, *isMemoryMapped()* and *readMemoryMapped()*, in addition to
the functions needed for regular, non-memory-mapped input:

class MemoryMappedIStream: public IStream

{

public:

MemoryMappedIStream (const char fileName\[\]);

virtual \~MemoryMappedIStream ();

virtual bool isMemoryMapped () const;

virtual char \* readMemoryMapped (int n);

virtual bool read (char c\[\], int n);

virtual Int64 tellg ();

virtual void seekg (Int64 pos);

private:

char \* \_buffer;

Int64 \_fileLength;

Int64 \_readPosition;

};

The constructor for class *MemoryMappedIStream* maps the contents of the
input file into the program\'s address space. Memory mapping is not
portable across operating systems. The example shown here uses the POSIX
*mmap()* system call. On Windows files can be memory-mapped by calling
*CreateFileMapping()* and *MapViewOfFile()*:

MemoryMappedIStream::MemoryMappedIStream (const char fileName\[\]):

IStream (fileName),

\_buffer (0),

\_fileLength (0),

\_readPosition (0)

{

int file = open (fileName, O_RDONLY);

if (file \< 0)

THROW_ERRNO (\"Cannot open file \\\"\" \<\< fileName \<\< \"\\\".\");

struct stat stat;

fstat (file, &stat);

\_fileLength = stat.st_size;

\_buffer = (char \*) mmap (0, \_fileLength, PROT_READ, MAP_PRIVATE,
file, 0);

close (file);

if (\_buffer == MAP_FAILED)

THROW_ERRNO (\"Cannot memory-map file \\\"\" \<\< fileName \<\<
\"\\\".\");

}

The destructor frees the address range associated with the file by
un-mapping the file. The POSIX version shown here uses *munmap()*. A
Windows version would call *UnmapViewOfFile()* and *CloseHandle()*:

MemoryMappedIStream::\~MemoryMappedIStream ()

{

munmap (\_buffer, \_fileLength);

}

Function *isMemoryMapped()* returns *true* to indicate that
memory-mapped input is supported. This allows the IlmImf library to call
*readMemoryMapped()* instead of *read()*:

bool

MemoryMappedIStream::isMemoryMapped () const

{

return true;

}

*readMemoryMapped()* is analogous to *read()*, but instead of copying
data into a buffer supplied by the caller, *readMemoryMapped()* returns
a pointer into the memory-mapped file, thus avoiding the copy operation:

char \*

MemoryMappedIStream::readMemoryMapped (int n)

{

if (\_readPosition \>= \_fileLength)

throw Iex::InputExc (\"Unexpected end of file.\");

if (\_readPosition + n \> \_fileLength)

throw Iex::InputExc (\"Reading past end of file.\");

char \*data = \_buffer + \_readPosition;

\_readPosition += n;

return data;

}

The *MemoryMappedIStream* class must also implement the regular *read()*
function, as well as *tellg()* and *seekg()*:

bool

MemoryMappedIStream::read (char c\[\], int n)

{

if (\_readPosition \>= \_fileLength)

throw Iex::InputExc (\"Unexpected end of file.\");

if (\_readPosition + n \> \_fileLength)

throw Iex::InputExc (\"Reading past end of file.\");

memcpy (c, \_buffer + \_readPosition, n);

\_readPosition += n;

return \_readPosition \< \_fileLength;

}

Int64

MemoryMappedIStream::tellg ()

{

return \_readPosition;

}

void

MemoryMappedIStream::seekg (Int64 pos)

{

\_readPosition = pos;

}

Class *MemoryMappedIStream* does not need a *clear()* function. Since
the memory-mapped file has no error flags that need to be cleared, the
*clear()* method provided by class *IStream*, which does nothing, can be
re-used.

Memory-mapping a file can be faster than reading the file via a C++
*std::istream* or a C *FILE \**, but the extra speed comes at a cost. A
large memory-mapped file can occupy a significant portion of a
program\'s virtual address space. In addition, mapping and un-mapping
many files of varying sizes can severely fragment the address space.
After a while, the program may be unable to map any new files because
there is no contiguous range of free addresses that would be large
enough hold a file, even though the total amount of free space would be
sufficient. An application program that uses memory-mapped I/O should
manage its virtual address space in order to avoid fragmentation. For
example, the program can reserve several address ranges, each one large
enough to hold the largest file that the program expects to read. The
program can then explicitly map each new file into one of the reserved
ranges, keeping track of which ranges are currently in use.

-->

# Miscellaneous

## Is this an OpenEXR File?

Sometimes we want to test quickly if a given file is an OpenEXR file.
This can be done by looking at the beginning of the file: The first four
bytes of every OpenEXR file contain the 32-bit integer \"magic number\"
20000630 in little-endian byte order. After reading a file\'s first four
bytes via any of the operating system\'s standard file I/O mechanisms,
we can compare them with the magic number by explicitly testing if the
bytes contain the values *0x76*, *0x2f*, *0x31*, and *0x01*.

Given a file name, the following function returns *true* if the
corresponding file exists, is readable, and contains an OpenEXR image:

bool

isThisAnOpenExrFile (const char fileName\[\])

{

std::ifstream f (fileName, std::ios_base::binary);

char b\[4\];

f.read (b, sizeof (b));

return !!f && b\[0\] == 0x76 && b\[1\] == 0x2f && b\[2\] == 0x31 &&
b\[3\] == 0x01;

}

Using this function does not require linking with the IlmImf library.

Programs that are linked with the IlmImf library can determine if a
given file is an OpenEXR file by calling one of the following functions,
which are part of the library:

bool isOpenExrFile (const char fileName\[\], bool &isTiled);

bool isOpenExrFile (const char fileName\[\]);

bool isTiledOpenExrFile (const char fileName\[\]);

bool isOpenExrFile (IStream &is, bool &isTiled);

bool isOpenExrFile (IStream &is);

bool isTiledOpenExrFile (IStream &is);

## Is this File Complete?

Sometimes we want to test if an OpenEXR file is complete. The file may
be missing pixels, either because writing the file is still in progress
or because writing was aborted before the last scan line or tile was
stored in the file. Of course, we could test if a given file is complete
by attempting to read the entire file, but the input file classes in the
IlmImf library have an *isComplete()* method that is faster and more
convenient.

The following function returns *true* or *false*, depending on whether a
given OpenEXR file is complete or not:

bool

isComplete (const char fileName\[\])

{

InputFile in (fileName);

return in.isComplete();

}

## Preview Images

Graphical user interfaces for selecting image files often represent
files as small *preview* or *thumbnail* images. In order to make loading
and displaying the preview images fast, OpenEXR files support storing
preview images in the file headers.

A preview image is an attribute whose value is of type *PreviewImage*. A
*PreviewImage* object is an array of pixels of type *PreviewRgba*. A
pixel has four components, *r*, *g*, *b* and *a*, of type *unsigned
char*, where *r*, *g* and *b* are the pixel\'s red, green and blue
components, encoded with a gamma of 2.2. *a* is the pixel\'s alpha
channel; *r*, *g* and *b* should be premultiplied by *a*. On a typical
display with 8-bits per component, the preview image can be shown by
simply loading the *r*, *g* and *b* components into the display\'s frame
buffer. (No gamma correction or tone mapping is required.)

The code fragment below shows how to test if an OpenEXR file has a
preview image, and how to access a preview image\'s pixels:

RgbaInputFile file (fileName);

if (file.header().hasPreviewImage())

{

const PreviewImage &preview = file.header().previewImage();

for (int y = 0; y \< preview.height(); ++y)

for (int x = 0; x \< preview.width(); ++x)

{

const PreviewRgba &pixel = preview.pixel (x, y);

\...

}

}

Writing an OpenEXR file with a preview image is shown in the following
example. Since the preview image is an attribute in the file\'s header,
it is entirely separate from the main image. Here the preview image is a
smaller version of the main image, but this is not required; in some
cases storing an easily recognizable icon may be more appropriate. This
example uses the RGBA-only interface to write a scan line based file,
but preview images are also supported for files that are written using
the general interface, and for tiled files.

void

writeRgbaWithPreview1 (const char fileName\[\],

const Array2D\<Rgba> &pixels,

int width,

int height)

{

Array2D \<PreviewRgba> previewPixels; // 1

int previewWidth; // 2

int previewHeight; // 3

makePreviewImage (pixels, width, height, // 4

previewPixels, previewWidth, previewHeight);

Header header (width, height); // 5

header.setPreviewImage // 6

(PreviewImage (previewWidth, previewHeight, &previewPixels\[0\]\[0\]));

RgbaOutputFile file (fileName, header, WRITE_RGBA); // 7

file.setFrameBuffer (&pixels\[0\]\[0\], 1, width); // 8

file.writePixels (height); // 9

}

Lines 1 through 4 generate the preview image. Line 5 creates a header
for the image file. Line 6 converts the preview image into a
*PreviewImage* attribute, and adds the attribute to the header. Lines 7
through 9 store the header (with the preview image) and the main image
in a file.

Function *makePreviewImage()*, called in line 4, generates the preview
image by scaling the main image down to one eighth of its original width
and height:

void

makePreviewImage (const Array2D\<Rgba> &pixels,

int width,

int height,

Array2D\<PreviewRgba> &previewPixels,

int &previewWidth,

int &previewHeight)

{

const int N = 8;

previewWidth = width / N;

previewHeight = height / N;

previewPixels.resizeErase (previewHeight, previewWidth);

for (int y = 0; y \< previewHeight; ++y)

{

for (int x = 0; x \< previewWidth; ++x)

{

const Rgba &inPixel = pixels\[y \* N\]\[x \* N\];

PreviewRgba &outPixel = previewPixels\[y\]\[x\];

outPixel.r = gamma (inPixel.r);

outPixel.g = gamma (inPixel.g);

outPixel.b = gamma (inPixel.b);

outPixel.a = int (clamp (inPixel.a \* 255.f, 0.f, 255.f) + 0.5f);

}

}

}

To make this example easier to read, scaling the image is done by just
sampling every eighth pixel of every eighth scan line. This can lead to
aliasing artifacts in the preview image; for a higher-quality preview
image, the main image should be lowpass-filtered before it is
subsampled.

Function *makePreviewImage()* calls *gamma()* to convert the
floating-point red, green, and blue components of the sampled main image
pixels to *unsigned char* values. *gamma()* is a simplified version of
what the exrdisplay program does in order to show an OpenEXR image\'s
floating-point pixels on the screen (for details, see exrdisplay\'s
source code):

unsigned char

gamma (float x)

{

x = pow (5.5555f \* max (0.f, x), 0.4545f) \* 84.66f;

return (unsigned char) clamp (x, 0.f, 255.f);

}

*makePreviewImage()* converts the pixels\' alpha component to unsigned
char by by linearly mapping the range \[0.0, 1.0\] to \[0, 255\].

Some programs write image files one scan line or tile at a time, while
the image is being generated. Since the image does not yet exist when
the file is opened for writing, it is not possible to store a preview
image in the file\'s header at this time (unless the preview image is an
icon that has nothing to do with the main image). However, it is
possible to store a blank preview image in the header when the file is
opened. The preview image can then be updated as the pixels become
available. This is demonstrated in the following example:

void

writeRgbaWithPreview2 (const char fileName\[\],

int width,

int height)

{

Array \<Rgba> pixels (width);

const int N = 8;

int previewWidth = width / N;

int previewHeight = height / N;

Array2D \<PreviewRgba> previewPixels (previewHeight, previewWidth);

Header header (width, height);

header.setPreviewImage (PreviewImage (previewWidth, previewHeight));

RgbaOutputFile file (fileName, header, WRITE_RGBA);

file.setFrameBuffer (pixels, 1, 0);

for (int y = 0; y \< height; ++y)

{

generatePixels (pixels, width, height, y);

file.writePixels (1);

if (y % N == 0)

{

for (int x = 0; x \< width; x += N)

{

const Rgba &inPixel = pixels\[x\];

PreviewRgba &outPixel = previewPixels\[y / N\]\[x / N\];

outPixel.r = gamma (inPixel.r);

outPixel.g = gamma (inPixel.g);

outPixel.b = gamma (inPixel.b);

outPixel.a = int (clamp (inPixel.a \* 255.f, 0.f, 255.f) + 0.5f);

}

}

}

file.updatePreviewImage (&previewPixels\[0\]\[0\]);

}

## Environment Maps

An environment map is an image that represents an omnidirectional view
of a three-dimensional scene as seen from a particular 3D location.
Every pixel in the image corresponds to a 3D direction, and the data
stored in the pixel represent the amount of light arriving from this
direction. In 3D rendering applications, environment maps are often used
for image-based lighting techniques that appoximate how objects are
illuminated by their surroundings. Environment maps with enough dynamic
range to represent even the brightest light sources in the environment
are sometimes called \"light probe images.\"

[`Envmap`]: crate::core::EnvMap

In an OpenEXR file, an environment map is stored as a rectangular pixel
array, just like any other image, but an attribute in the file header
indicates that the image is an environment map. The attribute\'s value,
which is of type [`Envmap`], specifies the relation between 2D pixel
locations and 3D directions. [`Envmap`] is an enumeration type. Two values
are possible:

| | |
|---|---|
| [`Envmap::Latlong`](crate::core::EnvMap::Latlong) | <p>The environment is projected onto the image using polar coordinates (latitude and longitude). A pixel's x coordinate corresponds to its longitude, and the y coordinate corresponds to its latitude. The pixel in the upper left corner of the data window has latitude +π/2 and longitude +π; the pixel in the lower right corner has latitude -π/2 and longitude -π.</p><p> In 3D space, latitudes -π/2 and +π/2 correspond to the negative and positive y direction. Latitude 0, longitude 0 points in the positive z direction; latitude 0, longitude π/2 points in the positive x direction.</p><p>  ![Latlong map][env_latlong] </p> <p> For a latitude-longitude map, the size of the data window should be 2×N by N pixels (width by height), where N can be any integer greater than 0.</p>|
| [`Envmap::Cube`](crate::core::EnvMap::Cube) | <p>The environment is projected onto the six faces of an axis-aligned cube. The cube's faces are then arranged in a 2D image as shown below.</p><p> ![Cube map][env_cubemap] </p><p> For a cube map, the size of the data window should be N by 6×N pixels (width by height), where N can be any integer greater than 0. </p> | 

**Note:** Both kinds of environment maps contain redundant pixels: In a
latitude-longitude map, the top row and the bottom row of pixels
correspond to the map\'s north pole and south pole (latitudes +π/2 and
-π/2). In each of those two rows all pixels are the same. The leftmost
column and the rightmost column of pixels both correspond to the
meridian with longitude +π (or, equivalently, -π). The pixels in the
leftmost column are repeated in the rightmost column. In a cube-face
map, the pixels along each edge of a face are repeated along the
corresponding edge of the adjacent face. The pixel in each corner of a
face is repeated in the corresponding corners of the two adjacent faces.

The following code fragment tests if an OpenEXR file contains an
environment map, and if it does, which kind:

```no_run
use openexr::prelude::*;

pub fn test_envmap(filename: &str) -> Result<(), Box<dyn std::error::Error>> {
    let file = RgbaInputFile::new(filename, 1)?;

    if let Some(e) = file.header().find_typed_attribute_envmap("envmap") {
        match e.value() {
            Envmap::Latlong => println!("latlong"),
            Envmap::Cube => println!("cubemap"),
            _ => unreachable!(),
        }
    }

    Ok(())
}
```

For each kind of environment map, openexr provides a set of
routines that convert from 3D directions to 2D floating-point pixel
locations and back. Those routines are useful in application programs
that create environment maps and in programs that perform map lookups.
For details, see the documentation for the [`envmap`](crate::core::envmap) module.