File: PapyDataSetWrite3.c

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

/********************************************************************************/
/*                                                                              */
/*	Project  : P A P Y R U S  Toolkit                                           */
/*	File     : PapyDataSetWrite3.c                                              */
/*	Function : contains the functions that will manage the Data Sets and        */
/*		       the modules (writing).                                           */
/*	Authors  : Christian Girard                                                 */
/*             Marianne Logean                                                  */
/*                                                                              */
/*	History  : 06.1994	version 3.0                                             */
/*             06.1995	version 3.1                                             */
/*             02.1996	version 3.3                                             */
/*             02.1999	version 3.6                                             */
/*             04.2001	version 3.7                                             */
/*             09.2001  version 3.7  on CVS                                     */
/*             10.2001  version 3.71 MAJ Dicom par CHG                          */
/*                                                                              */
/********************************************************************************/

#ifdef Mac
#pragma segment papy3
#endif

/* ------------------------- includes -----------------------------------------*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#ifdef Mac
#ifndef __LOWMEM__
#include <LowMem.h>
#endif
#ifndef __FILES__
#include <Files.h>
#endif
#include <Script.h>
#endif


#ifndef Papyrus3H 
#include "Papyrus3.h"
#endif



/********************************************************************************/
/*									 	*/
/*	CreateFileMetaInformation3 : Creates the file meta information for the 	*/
/*	given file. It creates group2 and fill some elements.			*/
/*	return : noError if no problem						*/
/*		 standard error message otherwise				*/
/*										*/
/********************************************************************************/

PapyShort
CreateFileMetaInformation3 (PapyShort inFileNb, enum EPap_Compression inCompression,
			    enum ETransf_Syntax inSyntax, enum EModality inModality)
{
  SElement	*theGr2P;
  Object	*theObjectP;
  Item		*theItemP;
  PapyUShort	*theUsP;
  char		*theCharP, theChar [32];
  
  
  /* creation of the file meta information group */
  theGr2P = Papy3GroupCreate (Group2);
  
  /* provide the group structure with the needed information */
  /* set the last bit of the second byte in the file (after translation) to 1 */
  /* in order to identify the file meta information version (Version 2) */
  theUsP  = (PapyUShort *) emalloc3 ((PapyULong) sizeof (PapyUShort));
  *theUsP = 1;
  Papy3PutImage (inFileNb, theGr2P, papFileMetaInformationVersionGr, theUsP, 0, 0, 8, 2L);
  
  theCharP = (char*) &theChar[0];
  /* DICOMDIR defined SOP Class UID */
  if (gIsPapyFile [inFileNb] == DICOMDIR)
  {
    strcpy (theCharP, "1.2.840.10008.1.3.10");
    Papy3PutElement (theGr2P, papMediaStorageSOPClassUIDGr, &theCharP);
  }
  else
    /* media storage SOP class UID, i.e the UID of the imaging modality */
    Papy3PutElement (theGr2P, papMediaStorageSOPClassUIDGr, &(gArrUIDs [inModality]));
  
  /* the transfert syntax that will be used in the rest of the file */
  /* actually only the default DICOM syntax is supported, */
  /* i.e implicit VR little-endian with or without compression */  
  /* DISCUSS */
  if (inSyntax == LITTLE_ENDIAN_IMPL && inCompression == NONE)
    strcpy (theCharP, "1.2.840.10008.1.2");
  else if (inSyntax == LITTLE_ENDIAN_EXPL && inCompression == NONE)
    strcpy (theCharP, "1.2.840.10008.1.2.1");
  else if (inSyntax == LITTLE_ENDIAN_EXPL && inCompression == JPEG_LOSSLESS)
    strcpy (theCharP, "1.2.840.10008.1.2.4.70");
  else if (inSyntax == LITTLE_ENDIAN_EXPL && inCompression == JPEG_LOSSY)
    strcpy (theCharP, "1.2.840.10008.1.2.4.51");
#ifdef MAYO_WAVE
  else if (inSyntax == LITTLE_ENDIAN_EXPL && inCompression == MAYO_WAVELET)
    strcpy (theCharP, "1.2.840.10008.1.2.4.80"); 
    /* WARNING this is NOW defined in DICOM as JPEG-LS lossless image compression */
    /* whereas 1.2.840.10008.1.2.4.81 is JPEG-LS Lossy (near-Lossless) image compression */
    /* Warning: this SHOULD NOT BE a proprietary syntax transfer!!
     * the wavelet compression is still not standardized... 
     */
#endif /* MAYO_WAVE */
  else if (gIsPapyFile [inFileNb] == DICOMDIR)  /* DICOMDIR: LITTLE_ENDIAN_EXPL */
    strcpy (theCharP, "1.2.840.10008.1.2.1");
  Papy3PutElement (theGr2P, papTransferSyntaxUIDGr, &theCharP);
  
  /* we have to discuss what we will put here DISCUSS */
  strcpy (theCharP, "1.2.756.777.000");
  Papy3PutElement (theGr2P, papImplementationClassUIDGr, &theCharP);
  
  /* create an Object and put the group 2 in it */
  theObjectP = (Object *) emalloc3 ((PapyULong) sizeof (Object));
  theObjectP->group  = theGr2P;
  theObjectP->item   = NULL;
  theObjectP->module = NULL;
  theObjectP->record = NULL;
  theObjectP->tmpFileLength = 0L;
  theObjectP->whoAmI = papGroup;
  theObjectP->objID  = Group2;
  
  /* initialize the memory representation of the file by creating the first */
  /* cell that will contain the file meta information */
  theItemP = InsertFirstInList (&(gArrMemFile [inFileNb]), theObjectP);
    
  return papNoError;
  
} /* endof CreateFileMetaInformation3 */


/********************************************************************************/
/*									 	*/
/*	CreateDicomFileMetaInformation3 : Creates the file meta information for	*/
/*	the given file. It creates group2 and fill some elements. Then writes 	*/
/*	the group to the given file.						*/
/*	return : standard error message						*/
/*										*/
/********************************************************************************/

PapyShort
CreateDicomFileMetaInformation3 (PAPY_FILE inFp, PapyShort inFileNb, enum EPap_Compression inCompression,
			    	 enum ETransf_Syntax inSyntax, enum EModality inModality, 
			    	 PapyULong *OutMetaInfoSizeP)
{
  SElement	*theGr2P;
  PapyUShort	*theUsP;
  char		*theCharP, theChar [32];
  unsigned char	*theBuffP;
  PapyShort	theErr = 0;
  int		theGroupNb;
  PapyULong	theBufSize, thePos;
  
  
  /* creation of the file meta information group */
  theGr2P = Papy3GroupCreate (Group2);
  
  /* provide the group structure with the needed information */
  /* set the last bit of the second byte in the file (after translation) to 1 */
  /* in order to identify the file meta information version (Version 2) */
  theUsP  = (PapyUShort *) emalloc3 ((PapyULong) sizeof (PapyUShort));
  *theUsP = 1;
  Papy3PutImage (inFileNb, theGr2P, papFileMetaInformationVersionGr, theUsP, 0, 0, 8, 2L);
  
  /* media storage SOP class UID, i.e the UID of the imaging modality */
  Papy3PutElement (theGr2P, papMediaStorageSOPClassUIDGr, &(gArrUIDs [inModality]));
  
  /* the transfert syntax that will be used in the rest of the file */
  /* actually only the default DICOM syntax is supported, */
  /* i.e implicit VR little-endian with or without compression */  
  /* DISCUSS */
  theCharP = (char*) &theChar[0];
  if (inSyntax == LITTLE_ENDIAN_IMPL && inCompression == NONE)
    strcpy (theCharP, "1.2.840.10008.1.2");
  else if (inSyntax == LITTLE_ENDIAN_EXPL && inCompression == NONE)
    strcpy (theCharP, "1.2.840.10008.1.2.1");
  else if (inSyntax == LITTLE_ENDIAN_EXPL && inCompression == JPEG_LOSSLESS)
    strcpy (theCharP, "1.2.840.10008.1.2.4.70");
  else if (inSyntax == LITTLE_ENDIAN_EXPL && inCompression == JPEG_LOSSY)
    strcpy (theCharP, "1.2.840.10008.1.2.4.51");
#ifdef MAYO_WAVE /* WARNING this is defined in DICOM as JPEG-LS lossless image compression */
		 /* whereas 1.2.840.10008.1.2.4.81 is JPEG-LS Lossy (near-Lossless) image compression */
  else if (inSyntax == LITTLE_ENDIAN_EXPL && inCompression == MAYO_WAVELET)
    strcpy (theCharP, "1.2.840.10008.1.2.4.80"); 
    /* Warning: this is a proprietary syntax transfer!!
     * the wavelet compression is still not standardized... 
     */
#endif /* MAYO_WAVE */
  Papy3PutElement (theGr2P, papTransferSyntaxUIDGr, &theCharP);
  
  /* SOP instance UID of this data set */
  /* convert an int into a string and add a number at the end to ensure uniqueness */
  Papy3FPrint (theCharP, "64.572.218.916.%d", gCurrTmpFilename [inFileNb]);
  Papy3PutElement (theGr2P, papMediaStorageSOPInstanceUIDGr, &theCharP);
  
  /* who is the creator of this wonderfull file ? */
  strcpy (theCharP, "PAPYRUS 3.0");
  Papy3PutElement (theGr2P, papSourceApplicationEntityTitleGr, &theCharP);
  
  /* we have to discuss what we will put here DISCUSS */
  strcpy (theCharP, "1.2.756.777.001");
  Papy3PutElement (theGr2P, papImplementationClassUIDGr, &theCharP);
  
  /* now write the group to the file */
  if ((theGroupNb = Papy3ToEnumGroup (theGr2P->group)) < 0)
    RETURN (papGroupNumber);

  /* compute the size of the current group */
  ComputeGroupLength3 ((PapyShort) theGroupNb, theGr2P, NULL, inSyntax);
  theBufSize = theGr2P->value->ul + kLength_length;
    
  /* alloc the buffer that will contain the ready to write group */
  theBuffP = (unsigned char *) emalloc3 ((PapyULong) theBufSize);
    
  thePos = 0L;    
  /* put the elements of the group to the write buffer */
  if ((theErr = PutGroupInBuffer (inFileNb, 1, theGroupNb, theGr2P, theBuffP, &thePos, FALSE)) < 0) 
    RETURN (theErr);
    
  /* write the buffer to the temporary file */
  if ((theErr = WriteGroup3 (inFp, theBuffP, theBufSize)) < 0)
    RETURN (theErr);  
    
  /* frees the allocated buffer */
  efree3 ((void **) &theBuffP);
  
  /* compute the size of the file meta information */
  *OutMetaInfoSizeP = theBufSize + 132L;
    
  return theErr;
  
} /* endof CreateDicomFileMetaInformation3 */


/********************************************************************************/
/*									      	*/
/*	Papy3GetGroup2 : Returns a pointer to the group 2 to allow the user to	*/
/*	put the needed elements in it.						*/
/*	return : a pointer to the group 2					*/
/*										*/
/********************************************************************************/

SElement * CALLINGCONV
Papy3GetGroup2 (PapyShort inFileNb)
{
  return (gArrMemFile [inFileNb])->object->group;
} /* endof Papy3GetGroup2 */



/********************************************************************************/
/*									 	*/
/*	Papy3GetRecordType : Function only used when creating Dicomdir file	*/
/*	return : an enumerated value which identify the kind of record.		*/
/*										*/
/********************************************************************************/

int CALLINGCONV
Papy3GetRecordType (SElement *inGroup)
{
  int		theElemType;
  PapyULong	thePos;
  UValue_T	*theValP;
  char 		theDirType [257]; /* VR_CS_LENGTH = 256 */
  int		theRecordID = -1;

  
  /* Read the directory type  (0004,1430) */
  theValP = Papy3GetElement (inGroup, papDirectoryRecordTypeGr, &thePos, &theElemType);
  if (theValP != NULL) 
    strcpy (theDirType , theValP->a);
  
  switch (theDirType [0])
  {
    case 'P' :
      switch (theDirType [1])
      {
        case 'A' : 
          theRecordID = PatientR; 
          break;
        case 'R' : 
          theRecordID = PrintQueue; 
          break;
      }/* switch */
      break;
    case 'S' :
      switch (theDirType [4])
      {
        case 'Y' : 
          if (theDirType [5] == 'C') 
            theRecordID = StudyComponentR;
          else 
            theRecordID = StudyR;
          break;
        case 'E' : 
          theRecordID = SeriesR; 
          break;
      }/* switch ...theDirType [4] */
      break;
    case 'I' :
      switch (theDirType [1])
      {
        case 'M' :
          theRecordID = ImageR;
          break;
        case 'N' :
          theRecordID = Interpretation;
          break;
      }/* switch */
      break;
    case 'O' :	
      theRecordID = OverlayR;
      break;
    case 'M' :	
      theRecordID = ModalityLUTR;
      break;
    case 'V' :
      switch (theDirType [1])
      {
        case 'O' :
          theRecordID = VOILUTR;
          break;
        case 'I' :
          theRecordID = Visit;
          break;
      }/* switch */
      break;
    case 'C' :
      theRecordID = CurveR;
      break;
    case 'T' :
      theRecordID = Topic;
      break;
    case 'R' :
      theRecordID = Result;
      break;
    case 'F' :
      theRecordID = FilmSession;
      break;
    case 'B' :
      switch (theDirType [5])
      {
        case 'F' :
          theRecordID = BasicFilmBox;
          break;
        case 'I' :
          theRecordID = BasicImageBox;
          break;
      }/* switch */
      break;
  
  } /* switch ...theDirType [0] */
  
  return theRecordID;

} /* endof Papy3GetRecordType */


/********************************************************************************/
/*									      	*/
/*	Papy3CreateDataSet : Create a new data set item and add it to 		*/
/*	the list of Data Set of the given file.					*/
/*	return : a pointer to the created Data Set				*/
/*		 NULL otherwise							*/
/*										*/
/********************************************************************************/

Item * CALLINGCONV
Papy3CreateDataSet (PapyShort inFileNb)
{
  Object	*theObjectP;
  Item		*theItemP, *theWrkP;
  SElement	*theGr41P;
  int		first = FALSE;
  
  
  /* if it is the first data set ... */
  if (gImageSequenceItem [inFileNb] == NULL) first = TRUE;
  
  /* creates an empty object that will point to the list of modules */
  theObjectP = (Object *) emalloc3 ((PapyULong) sizeof (Object));
  theObjectP->whoAmI 		= papItem;
  theObjectP->item   		= NULL;
  theObjectP->module 		= NULL;
  theObjectP->record 		= NULL;
  theObjectP->group 		= NULL;
  theObjectP->tmpFileLength 	= 0L;
  
  theItemP = InsertLastInList (&(gImageSequenceItem [inFileNb]), theObjectP);
  
  /* if it is the first data set, store the pointer to it in group 41 */
  if (first)
  {
    theWrkP  = gArrMemFile [inFileNb];
    theWrkP  = theWrkP->next->next;			/* locate group41 */
    theGr41P = theWrkP->object->group;
    Papy3PutElement (theGr41P, papImageSequenceGr, &(gImageSequenceItem [inFileNb]));
  } /* if */
    
  return theItemP;
  
} /* endof Papy3CreateDataSet */



/********************************************************************************/
/*									      	*/
/*	Papy3InsertItemToSequence : Create a new Object that will point to the 	*/
/*	given Item (a group or a module or whatever you liked).	Then link the 	*/
/*	Object to the given sequence.						*/
/*	return : standard error message						*/
/*										*/
/********************************************************************************/

PapyShort CALLINGCONV
Papy3InsertItemToSequence (Module *inModuleP, int inElemNb, enum EKind_Obj inItemType, 
			   void *inItem, int inItemId)
{
  Module	*theElementP;
  Object	*theObjectP;
  Item		*theNewItemP, *theObjectListP;
  int		first = FALSE;
  
  
  /* go to the element to add the item to */
  theElementP = inModuleP + inElemNb;
  
  /* if there were no value, add one */
  if (theElementP->nb_val == 0L) 
  {
    theElementP->nb_val = 1L;
    /* allocate room for the value to be inserted */
    theElementP->value = (UValue_T *) emalloc3 ((PapyULong) sizeof (UValue_T));
    /* and initializes it to NULL */
    theElementP->value->sq = NULL;
    
    /* creates the first part of the link */
    theObjectP = (Object *) emalloc3 ((PapyULong) sizeof (Object));
    theObjectP->whoAmI 	      = papItem;
    theObjectP->item          = NULL;
    theObjectP->module 	      = NULL;
    theObjectP->record 	      = NULL;
    theObjectP->group         = NULL;
    theObjectP->tmpFileLength = 0L;
    /* and link it to the sequence */
    theObjectListP = InsertLastInList (&(theElementP->value->sq), theObjectP);
    
    first = TRUE;
    
  } /* if ...no value */
    
  /* get the pointer to the list of objects */
  theObjectListP = theElementP->value->sq->object->item;
  
  /* creates an empty object that will point to the item to insert */
  theObjectP = (Object *) emalloc3 ((PapyULong) sizeof (Object));
  theObjectP->whoAmI 	    = inItemType;
  theObjectP->objID  	    = inItemId;
  theObjectP->item   	    = NULL;
  theObjectP->module 	    = NULL;
  theObjectP->record 	    = NULL;
  theObjectP->group  	    = NULL;
  theObjectP->tmpFileLength = 0L;
  
  /* assign the item to the Object */
  switch (inItemType)
  {
    case papItem   :
      theObjectP->item   = (Item *)     inItem;
      break;
    case papGroup  :
      theObjectP->group  = (SElement *) inItem;
      break;
    case papModule :
      theObjectP->module = (Module *)   inItem;
      break;
    case papRecord :
      theObjectP->record = (Record *)   inItem;
      break;
    case papTmpFile:
    default :
      break;
  } /* switch */
  
  /* insert the Object in the sequence */
  theNewItemP = InsertLastInList (&(theObjectListP), theObjectP);
  
  if (first)
    theElementP->value->sq->object->item = theNewItemP;
    
  return 0;
  
} /* endof Papy3InsertItemToSequence */



/********************************************************************************/
/*									 	*/
/*	CreateIcon3 : Create the icon pixel data for the Icon Image module of 	*/
/*	the module currently being closed.					*/
/* 	Each pixel of the icon is computed as a subsampling of the original 	*/
/* 	image.		 							*/
/*	return : a pointer to the pixel data of the icon.			*/
/*										*/
/********************************************************************************/

PapyUShort *
CreateIcon3 ()
{
  /* PapyUShort		gRefColumns; 		 width of original image */
  /* PapyUShort		gRefRows;		 height of original image */
  /* PapyUShort		gRefIsSigned;		 signed datas or no */
  /* PapyUShort		gRefBitsAllocated; 	 depth of original image (8 or 16 bits) */
  /* PapyUShort*	gRefPixelData;		 original pixmap, char* for 8 
                                                 bits images
						 or short* for 16 bits images */

  /* PapyUShort		gIconSize;		 width  of icon */
  /* PapyUShort		gIconSize;		 height of icon */

  PapyUShort		*theUSIconP;		/* destination pixmap (16 bits)*/
  PapyUChar		*theUCIconP;		/* ptr on the dest. pixmap (8 bits) */

  PapyULong		theLine, theRatio;
  PapyUShort		i, j;
  
  int 			theMin, theMax;


  /* if there were no WW or WL saved in the file then assume default values */
  if (gRefWW == -1 && gRefWW == gRefWL) 
  {
    gRefWW = (int) (gRefPixMax - gRefPixMin + 1);
    gRefWL = (gRefWW / 2) + (int) gRefPixMin;
  } /* if */
  
  /* avoids dividing by zero */
  if (gRefWW == 0) gRefWW = 1;
  
  /* allocate the memory for the icon */
  theUSIconP = (PapyUShort *) emalloc3 ((PapyULong) (gIconSize * gIconSize));
  theUCIconP = (PapyUChar *) theUSIconP;
    
  /* computes the ratios between the image and the icon */
  theRatio = (PapyULong) gRefColumns * (PapyULong) gRefRows / (PapyULong) gIconSize; 
  
  /* 8 bits image */
  if (gRefBitsAllocated == 8) 
  {
    PapyUChar	*thePixmapP;

    thePixmapP = (PapyUChar *) gRefPixelData;
    
    for (i = 0; i < gIconSize; i++)			/* lines */
    {
      theLine = (PapyULong) ((PapyULong)i * theRatio);
      theLine = (theLine / gRefColumns) * gRefColumns;		/* must be an integer number of lines */

      for (j = 0; j < gIconSize; j++, theUCIconP++)		/* columns */
        *theUCIconP = (PapyUChar) *(thePixmapP + (theLine + ((PapyULong) j * (PapyULong) gRefColumns / (PapyULong) gIconSize)));
      
    } /* for ...i */
	
  } /* if ...8 bits image */
  
  /* 12 or 16 bits image */
  else 
  {
    if (gRefIsSigned)
    {
      PapyShort	*thePixmapP;
      PapyShort	theValue;

      /* computes the min and max pixel values */
      theMin = gRefWL - (gRefWW / 2);
      theMax = theMin + gRefWW;

      thePixmapP = (PapyShort *) gRefPixelData;

      for (i = 0; i < gIconSize; i++)				/* lines */
      {
        theLine = (PapyULong) ((PapyULong) i * theRatio);
        theLine = (theLine / gRefColumns) * gRefColumns;	/* must be an integer number of lines */

        for (j = 0; j < gIconSize; j++, theUCIconP++)		/* columns */
        {
          /* first get the subsampled pixel value */
          theValue = (PapyShort) *(thePixmapP + (theLine + ((PapyULong) j * (PapyULong) gRefColumns / (PapyULong) gIconSize)));
          if (theValue < (PapyShort) theMin) theValue = (PapyShort) theMin;
          if (theValue > (PapyShort) theMax) theValue = (PapyShort) theMax;

          /* then convert it to an 8 bit value */
          *theUCIconP = (PapyUChar) (((PapyShort) theValue - theMin) * 255 / (PapyShort) gRefWW);
        } /* for ...j */

      } /* for ...i */

    } /* if ...signed values */
    else /* unsigned values */
    {
      PapyUShort		*thePixmapP;
      PapyUShort		theValue;

      /* computes the min and max pixel values */
      theMin = gRefWL - (gRefWW / 2);
      theMax = theMin + gRefWW; 
      if (theMin < 0) theMin = 0;
    
      thePixmapP = (PapyUShort *) gRefPixelData;
    
      for (i = 0; i < gIconSize; i++)			/* lines */
      {
        theLine = (PapyULong) ((PapyULong) i * theRatio);
        theLine = (theLine / gRefColumns) * gRefColumns;	/* must be an integer number of lines */

        for (j = 0; j < gIconSize; j++, theUCIconP++)	/* columns */
        {
          /* first get the subsampled pixel value */
          theValue = (PapyUShort) *(thePixmapP + (theLine + ((PapyULong) j * (PapyULong) gRefColumns / (PapyULong) gIconSize)));
        
          if (theValue < (PapyUShort) theMin) theValue = (PapyUShort) theMin;
          if (theValue > (PapyUShort) theMax) theValue = (PapyUShort) theMax;
        
          /* then convert it to an 8 bit value */
          *theUCIconP = (PapyUChar) (((PapyUShort) theValue - theMin) * 255 / (PapyUShort) gRefWW);
        } /* for ...j */
      
      } /* for ...i */

    } /* else ...unsigned values */ 
	
  } /* else ...12 or 16 bits image */
  
  /* free the original image as we do not need it any more */
  gRefPixelData = NULL;
  
  return theUSIconP;

} /* endof CreateIcon3 */



/********************************************************************************/
/*									 	*/
/*	CreatePointerSequence3 : Creates the pointer sequence for the given data*/
/*	set of the given file. It will fill the Image Identification Module, 	*/
/*	the Icon Image Module, the Image Pointer Module and the Pixel Offset	*/
/*	Module.									*/
/*	return : standard error message						*/
/*										*/
/********************************************************************************/

PapyShort
CreatePointerSequence3 (PapyShort inFileNb, Item *inDataSetP)
{
  PapyShort	thePosOfInsert, first;
  int		theCreateIconImage = TRUE;
  PapyULong	theULong;
  Item		*theWrkItemP, *theSeqItemP;
  Object	*theObjectP;
  Module	*theModuleP;
  PapyUShort	theUShort, *theIconP;
  char		*theCharP, theChar [16];
  
  
  /* -------- insert the item in the pointer sequence -------- */
  
  /* Look for the position of the data set in the list in order   */
  /* to know where to insert the new item in the pointer sequence */
  thePosOfInsert = 0;
  theWrkItemP = gImageSequenceItem [inFileNb];
  /* loop until data set found */
  while (theWrkItemP != inDataSetP)
  {
    thePosOfInsert++;
    theWrkItemP = theWrkItemP->next;
  } /* while */
  
  /* if it is the first item of the Pointer Sequence ... */
  if (gPtrSequenceItem [inFileNb] == NULL) first = TRUE;
  else first = FALSE;
  
  /* creates an empty object that will point to the list of modules */
  theObjectP = (Object *) emalloc3 ((PapyULong) sizeof (Object));
  theObjectP->whoAmI 	    = papItem;
  theObjectP->item   	    = NULL;
  theObjectP->module 	    = NULL;
  theObjectP->record 	    = NULL;
  theObjectP->group  	    = NULL;
  theObjectP->tmpFileLength = 0L;
  
  /* insert the item in the pointer sequence at the specified position */
  theSeqItemP = InsertInListAt ((Item **) &gPtrSequenceItem [inFileNb], theObjectP, thePosOfInsert);
  
  /* if it is the first item of the pointer sequence, store the pointer to it in group 41 */
  if (first)
  {
    theWrkItemP = gArrMemFile [inFileNb];
    theWrkItemP = theWrkItemP->next; 
    theWrkItemP = theWrkItemP->next;	/* locate group41 */
    Papy3PutElement (theWrkItemP->object->group, papPointerSequenceGr, &theSeqItemP);
  } /* if */
  
  
  /* -------- create the Image Identification module -------- */
  theModuleP = Papy3CreateModule (theSeqItemP, ImageIdentification);
  
  /* fill the elements of the Image Identification module */
  Papy3PutElement (theModuleP, papReferencedImageSOPClassUIDII, &gRefSOPClassUID [inFileNb]);
  Papy3PutElement (theModuleP, papReferencedImageSOPInstanceUID, &gRefSOPInstanceUID);
  Papy3PutElement (theModuleP, papImageNumberII, &gRefImageNb);
  
  efree3 ((void **) &gRefImageNb);
  efree3 ((void **) &gRefSOPInstanceUID);
  
  /* -------- create the Icon Image module -------- */
  /* in case of a Papyrus compressed image look if there is an icon to put */
  if (gArrTransfSyntax [inFileNb] == LITTLE_ENDIAN_EXPL && 
       (gArrCompression [inFileNb] == JPEG_LOSSLESS || gArrCompression [inFileNb] == JPEG_LOSSY
#ifdef MAYO_WAVE
	  || gArrCompression [inFileNb] == MAYO_WAVELET
#endif
          ) &&
      gArrIcons [inFileNb] [thePosOfInsert] == NULL) theCreateIconImage = FALSE;
  
  if (theCreateIconImage)
  {
    theModuleP = Papy3CreateModule (theSeqItemP, IconImage);
  
    /* fill the elements of the Icon Image module */
    theUShort = 1;
    Papy3PutElement (theModuleP, papSamplesperPixelII, &theUShort);
  
    theCharP = (char *) &theChar [0];
    strcpy (theCharP, "MONOCHROME2");	/* monochrome2 : 0 = black */
    Papy3PutElement (theModuleP, papPhotometricInterpretationII, &theCharP);
    Papy3PutElement (theModuleP, papRowsII,    &gIconSize);
    Papy3PutElement (theModuleP, papColumnsII, &gIconSize);
    theUShort = 8;		/* the icon should be coded on 8 bits */
    Papy3PutElement (theModuleP, papBitsAllocatedII, &theUShort);
    Papy3PutElement (theModuleP, papBitsStoredII,    &theUShort);
    theUShort--;		/* high bit should be one less than bits allocated */
    Papy3PutElement (theModuleP, papHighBitII, &theUShort);
    theUShort = 0;	      /* this means pixel representation = PapyUShort */
    Papy3PutElement (theModuleP, papPixelRepresentationII, &theUShort);
  
    /* take the icon image from the list */
    if (gArrTransfSyntax [inFileNb] == LITTLE_ENDIAN_EXPL && (gArrCompression [inFileNb] == JPEG_LOSSLESS
							   || gArrCompression [inFileNb] == JPEG_LOSSY 
#ifdef MAYO_WAVE
                					   || gArrCompression [inFileNb] == MAYO_WAVELET
#endif
       ))
    {
      theIconP = (PapyUShort *) (gArrIcons [inFileNb] [thePosOfInsert]);
      Papy3PutImage (inFileNb, theModuleP, papPixelDataII, theIconP, gIconSize, gIconSize, 8, 0L);
      efree3 ((void **) &(gArrIcons [inFileNb] [thePosOfInsert]));
    } /* if ...take the icon from the list */
    /* create the icon pixels from the image */
    else
    {
      theIconP = CreateIcon3 ();
      Papy3PutImage (inFileNb, theModuleP, papPixelDataII, theIconP, gIconSize, gIconSize, 8, 0L);
    } /* else ...compute the icon from the original image */
  
  } /* if ...creation of the icon image module */
  
  /* -------- create the Image Pointer module -------- */
  theModuleP = Papy3CreateModule (theSeqItemP, ImagePointer);

  /* the offset to the data set is zero relatively to the temp. file */
  theULong = 0L;		
  Papy3PutElement (theModuleP, papImagePointer, &theULong);
  
  /* -------- create the Pixel Offset module -------- */
  theModuleP = Papy3CreateModule (theSeqItemP, PixelOffset);

  /* the offset to the pixel data in the tmp file */
  Papy3PutElement (theModuleP, papPixelOffset, &theULong);
  
  return 0;
  
} /* endof CreatePointerSequence3 */ 



/********************************************************************************/
/*										*/
/*	Papy3LinkModuleToDS : Link the given module to the Data Set.		*/
/*										*/
/********************************************************************************/

void CALLINGCONV
Papy3LinkModuleToDS (Item *inDataSetP, Module *inModuleP, int inModuleName)
{
  Item		*theItemP;
  Object	*theObjectP;
  
    
  /* ------- link the created module to the list of modules of the data set ---- */
  
  /* creation of the Object pointing to the module */
  theObjectP = (Object *) emalloc3 ((PapyULong) sizeof (Object));
  theObjectP->whoAmI = papModule;
  theObjectP->objID  = inModuleName;
  /* link the module to the object */
  theObjectP->module = inModuleP;
  theObjectP->record = NULL;
  theObjectP->item   = NULL;
  theObjectP->group  = NULL;
  theObjectP->file	 = NULL;
  theObjectP->tmpFileLength = 0L;

  /* insert the item at the end of the list of modules of the data set */
  theItemP = InsertLastInList ((Item **) &(inDataSetP->object->item), theObjectP);

} /* endof Papy3LinkModuleToDS */



/********************************************************************************/
/*										*/
/*	Papy3LinkGroupToDS : Link the given group to the Data Set.		*/
/*										*/
/********************************************************************************/

void CALLINGCONV
Papy3LinkGroupToDS (Item *inDataSetP, SElement *inGroupP, int inGroupName)
{
  Item		*theItemP;
  Object	*theObjectP;
  

  /* ------- link the created group to the list of modules/groups of the data set ---- */
  
  /* creation of the Object pointing to the group */
  theObjectP = (Object *) emalloc3 ((PapyULong) sizeof (Object));
  theObjectP->whoAmI 	    = papGroup;
  theObjectP->objID  	    = inGroupName;
  /* link the group to the object */
  theObjectP->group  	    = inGroupP;
  theObjectP->item   	    = NULL;
  theObjectP->module 	    = NULL;
  theObjectP->file   	    = NULL;
  theObjectP->tmpFileLength = 0L;

  /* insert the item at the end of the list of modules/groups of the data set */
  theItemP = InsertLastInList ((Item **) &(inDataSetP->object->item), theObjectP);

} /* endof Papy3LinkGroupToDS */



/********************************************************************************/
/*										*/
/*	Papy3CreateModule : Create a new module and add it to the list of 	*/
/*	modules of the Data Set.						*/
/*	return : a pointer to the created module				*/
/*		 NULL otherwise							*/
/*										*/
/********************************************************************************/

Module * CALLINGCONV 
Papy3CreateModule (Item *inDataSetP, int inModuleName)
{
  Module	*theModuleP;
  
  
  /* create the module a la Papy3GroupCreate */
  theModuleP = CreateModule3 (inModuleName);
  
  /* link the module to the list of the data set */
  Papy3LinkModuleToDS (inDataSetP, theModuleP, inModuleName);
    
  return theModuleP;

} /* endof Papy3CreateModule */



/********************************************************************************/
/*										*/
/*	Papy3FindModule : Finds the given module in the given data set of the  	*/
/*	given file (write).							*/
/*	return : A pointer to the module or NULL if the module does not exist.	*/
/*										*/
/********************************************************************************/

Module * CALLINGCONV
Papy3FindModule (Item *inDataSetP, int inModuleID)
{
  Item	*theWrkItemP;
  int	found = FALSE;
  
  
  /* points to the first module of the list */
  theWrkItemP = inDataSetP;
  if (theWrkItemP == NULL) return NULL;
  
  /* loop on the modules until it finds the right one */
  while (!found && theWrkItemP->next != NULL)
  {
    if (theWrkItemP->object->objID == inModuleID) found = TRUE;
    else theWrkItemP = theWrkItemP->next;
  } /* while */
  
  if (found) return theWrkItemP->object->module;
  else return NULL;
  
} /* endof Papy3FindModule */



/********************************************************************************/
/*										*/
/*	CheckDataSetModules3 : check the given data set to see if all the needed*/
/*	modules have been filled.						*/
/*	return : standard error message.					*/
/*										*/
/********************************************************************************/

PapyShort
CheckDataSetModules3 (PapyShort inFileNb, Item *inDataSetP)
{
  PapyShort	i, theMaxLoop, found;
  Item		*theWrkItemP;
  Data_Set	*theWrkDSP;
  
  
  /* how many elements do we have to check ? */
  switch (gFileModality [inFileNb])
  {
    case CR_IM :
      theMaxLoop = gArrModuleNb [CR_IM];
      break;
    case CT_IM :
      theMaxLoop = gArrModuleNb [CT_IM];
      break;
    case MR_IM :
      theMaxLoop = gArrModuleNb [MR_IM];
      break;
    case NM_IM :
      theMaxLoop = gArrModuleNb [NM_IM];
      break;
    case US_IM :
      theMaxLoop = gArrModuleNb [US_IM];
      break;
    case US_MF_IM :  
      theMaxLoop = gArrModuleNb [US_MF_IM];
      break;
    case SEC_CAPT_IM :
      theMaxLoop = gArrModuleNb [SEC_CAPT_IM];
      break;
    case PX_IM :
    case DX_IM :
      theMaxLoop = gArrModuleNb [DX_IM];
      break;
    case MG_IM :
      theMaxLoop = gArrModuleNb [MG_IM];
      break;
    case IO_IM :
      theMaxLoop = gArrModuleNb [IO_IM];
      break;
    case RF_IM :
      theMaxLoop = gArrModuleNb [RF_IM];
      break;
    case PET_IM :
      theMaxLoop = gArrModuleNb [PET_IM];
      break;
    case VLE_IM :
      theMaxLoop = gArrModuleNb [VLE_IM];
      break;
    case VLM_IM :
      theMaxLoop = gArrModuleNb [VLM_IM];
      break;
    case VLS_IM :
      theMaxLoop = gArrModuleNb [VLS_IM];
      break;
    case VLP_IM :
      theMaxLoop = gArrModuleNb [VLP_IM];
      break;
    case MFSBSC_IM :
      theMaxLoop = gArrModuleNb [MFSBSC_IM];
      break;
    case MFGBSC_IM :
      theMaxLoop = gArrModuleNb [MFGBSC_IM];
      break;
    case MFGWSC_IM :
      theMaxLoop = gArrModuleNb [MFGWSC_IM];
      break;
    case MFTCSC_IM :
      theMaxLoop = gArrModuleNb [MFTCSC_IM];
      break;
  } /* switch */
  
  /* check the data set */
  theWrkDSP = gArrModalities [gFileModality [inFileNb]];
  for (i = 0; i < theMaxLoop; i++)
  {
    /* is the checked module mandatory ? */
    if (theWrkDSP->usage == M)
    {
      /* look if this module is in the data set */
      theWrkItemP = (Item *) inDataSetP->object->item;
      found = FALSE;
      while (theWrkItemP != NULL && !found)
      {
        if ((theWrkItemP->object != NULL) && 
            (theWrkItemP->object->objID  == theWrkDSP->moduleName) &&
            (theWrkItemP->object->whoAmI == papModule)) found = TRUE;
        theWrkItemP = theWrkItemP->next;
      } /* while */
      
      if (!found) RETURN (papMissingModule);
    
    } /* if ...we only check the mandatory modules */
    
    /* incrementation */
    theWrkDSP++;
  } /* for ...checking the data set */
  
  return 0;
  
} /* endof CheckDataSetModules3 */



/********************************************************************************/
/*										*/
/*	CreatePatientSummary3 : Creates the general patient summary module for 	*/
/*	the given file. It takes the datas out of the Patient and the Patient 	*/
/*	Study modules.								*/
/*	return : papNoError if no problem, standard error message otherwise.	*/
/*										*/
/********************************************************************************/

PapyShort
CreatePatientSummary3 (PapyShort inFileNb)
{
  int		theElemType;
  Item		*theFirstDataSetP;
  Module	*theFromModuleP, *thePatSumModuleP;
  UValue_T	*theValP;
  PapyULong	theNbVal;
  
  
  /* search the first data set */
  theFirstDataSetP = (Item *) gImageSequenceItem [inFileNb]->object->item;
  
  /* search for the Patient module */
  theFromModuleP = Papy3FindModule (theFirstDataSetP, Patient);
  if (theFromModuleP == NULL) RETURN (papMissingModule);
  
  /* create the general patient summary module and add it to the list of summaries */
  thePatSumModuleP = Papy3CreateModule (gPatientSummaryItem [inFileNb], GeneralPatientSummary);
  
  /* copy the elements from the Patient module to the Patient Summary module */
  theValP = Papy3GetElement (theFromModuleP, papPatientsNameP, &theNbVal, &theElemType);
  if (theValP != NULL)
    Papy3PutElement (thePatSumModuleP, papPatientsNameGPS, &(theValP->a));
  
  theValP = Papy3GetElement (theFromModuleP, papPatientIDP, &theNbVal, &theElemType);
  if (theValP != NULL)
    Papy3PutElement (thePatSumModuleP, papPatientsID, &(theValP->a));
  
  theValP = Papy3GetElement (theFromModuleP, papPatientsBirthDateP, &theNbVal, &theElemType);
  if (theValP != NULL)
    Papy3PutElement (thePatSumModuleP, papPatientsBirthDateGPS, &(theValP->a));
  
  theValP = Papy3GetElement (theFromModuleP, papPatientsSexP, &theNbVal, &theElemType);
  if (theValP != NULL)
    Papy3PutElement (thePatSumModuleP, papPatientsSexGPS, &(theValP->a));
  
  /* search for the Patient Study module */
  theFromModuleP = Papy3FindModule (theFirstDataSetP, PatientStudy);
  if (theFromModuleP == NULL) RETURN (papNoError);
  
  /* copy the elements from the Patient Study module to the Patient Summary module */
  theValP = Papy3GetElement (theFromModuleP, papPatientsSizePS, &theNbVal, &theElemType);
  if (theValP != NULL)
    Papy3PutElement (thePatSumModuleP, papPatientsHeight, &(theValP->a));
  
  theValP = Papy3GetElement (theFromModuleP, papPatientsWeightPS, &theNbVal, &theElemType);
  if (theValP != NULL)
    Papy3PutElement (thePatSumModuleP, papPatientsWeightGPS, &(theValP->a));
    
  return 0;
  
} /* endof CreatePatientSummary3 */



/********************************************************************************/
/*										*/
/*	CreateStudySummary3 : Creates the general study summary module for 	*/
/*	the given file. It takes the datas out of the General Study module.	*/
/*	return : papNoError if no problem, standard error message otherwise.	*/
/*										*/
/********************************************************************************/

PapyShort
CreateStudySummary3 (PapyShort inFileNb)
{
  int		theElemType;
  Item		*theFirstDataSetP;
  Module	*theFromModuleP, *thePatStudyModuleP;
  UValue_T	*theValP;
  PapyULong	theNbVal;
  
  
  /* search the first data set */
  theFirstDataSetP = (Item *) gImageSequenceItem [inFileNb]->object->item;
  
  /* search for the General Study module */
  theFromModuleP = Papy3FindModule (theFirstDataSetP, GeneralStudy);
  if (theFromModuleP == NULL) RETURN (papMissingModule);
  
  /* create the general study summary module and add it to the list of summaries */
  thePatStudyModuleP = Papy3CreateModule (gPatientSummaryItem [inFileNb], GeneralStudySummary);
  
  /* copy the elements to the General Patient Summary module */
  theValP = Papy3GetElement (theFromModuleP, papStudyDateGS, &theNbVal, &theElemType);
  if (theValP != NULL)
    Papy3PutElement (thePatStudyModuleP, papStudyDateGSS, &(theValP->a));

  theValP = Papy3GetElement (theFromModuleP, papStudyTimeGS, &theNbVal, &theElemType);
  if (theValP != NULL)
    Papy3PutElement (thePatStudyModuleP, papStudyTimeGSS, &(theValP->a));

  theValP = Papy3GetElement (theFromModuleP, papStudyInstanceUIDGS, &theNbVal, &theElemType);
  if (theValP != NULL)
    Papy3PutElement (thePatStudyModuleP, papStudyUID, &(theValP->a));

  theValP = Papy3GetElement (theFromModuleP, papStudyIDGS, &theNbVal, &theElemType);
  if (theValP != NULL)
    Papy3PutElement (thePatStudyModuleP, papStudyIDGSS, &(theValP->a));

  theValP = Papy3GetElement (theFromModuleP, papAccessionNumberGS, &theNbVal, &theElemType);
  if (theValP != NULL)
    Papy3PutElement (thePatStudyModuleP, papAccessionnumberGSS, &(theValP->a));

  theValP = Papy3GetElement (theFromModuleP, papReferringPhysiciansNameGS, &theNbVal, &theElemType);
  if (theValP != NULL)
    Papy3PutElement (thePatStudyModuleP, papReferringPhysiciansNameGSS, &(theValP->a));
  
  return 0;
  
} /* endof CreateStudySummary3 */



/********************************************************************************/
/*										*/
/*	CreateSeriesSummary3 : Creates the general series summary module for the*/
/*	given file. It takes the datas out of the General series module and the */
/*	group 41.								*/
/*	return : papNoError if no problem, standard error message otherwise.	*/
/*										*/
/********************************************************************************/

PapyShort
CreateSeriesSummary3 (PapyShort inFileNb)
{
  int		theElemType;
  Item		*theFirstDataSetP;
  Module	*theFromModuleP, *thePatSeriesModuleP;
  UValue_T	*theValP;
  PapyULong	theNbVal;
  
  
  /* search the first data set */
  theFirstDataSetP = (Item *) gImageSequenceItem [inFileNb]->object->item;
  
  /* search for the General Series module */
  theFromModuleP = Papy3FindModule (theFirstDataSetP, GeneralSeries);
  if (theFromModuleP == NULL) RETURN (papMissingModule);
  
  /* create the general series summary module and add it to the list of summaries */
  thePatSeriesModuleP = Papy3CreateModule (gPatientSummaryItem [inFileNb], GeneralSeriesSummary);
  
  /* copy the elements to the General Series Summary module */
  theValP = Papy3GetElement (theFromModuleP, papModalityGS, &theNbVal, &theElemType);
  if (theValP != NULL)
    Papy3PutElement (thePatSeriesModuleP, papModalityGSS, &(theValP->a));

  theValP = Papy3GetElement (theFromModuleP, papSeriesInstanceUIDGS, &theNbVal, &theElemType);
  if (theValP != NULL)
    Papy3PutElement (thePatSeriesModuleP, papSeriesInstanceUIDGSS, &(theValP->a));

  theValP = Papy3GetElement (theFromModuleP, papSeriesNumberGS, &theNbVal, &theElemType);
  if (theValP != NULL)
    Papy3PutElement (thePatSeriesModuleP, papSeriesNumberGSS, &(theValP->a));
  
  /* !! dont take number of images from group 41 as it will remain there when written */
  
  return 0;
  
} /* endof CreateSeriesSummary3 */


/********************************************************************************/
/*										*/
/*	CreateSummaries3 : Creates the summaries modules for the given file.	*/
/*	return : papNoError if no problem, standard error message otherwise.	*/
/*										*/
/********************************************************************************/

PapyShort
CreateSummaries3 (PapyShort inFileNb)
{
  PapyShort	theErr;
  
  
  /* make the link to the memory representation of the file */
  gPatientSummaryItem [inFileNb] = gArrMemFile [inFileNb]->next;
  
  /* creation of the summaries */
  if ((theErr = CreatePatientSummary3 (inFileNb)) < 0) RETURN (theErr);
  if ((theErr = CreateStudySummary3   (inFileNb)) < 0) RETURN (theErr);
  if ((theErr = CreateSeriesSummary3  (inFileNb)) < 0) RETURN (theErr);
  
  RETURN (0);
  
} /* endof CreateSummaries3 */



/********************************************************************************/
/*										*/
/*	LookForGroupsInModule3 : Scan a module for its list of groups. Compares	*/
/* 	the found groups with the list of existing groups (if any) and build 	*/
/*	the list of groups to create or read (list that is returned).		*/
/*										*/
/********************************************************************************/

void
LookForGroupsInModule3 (Module *inModuleP, int inModuleID, 
		        int *inDSGroupsTotP, int *inGrToCreateP)
{
  int		*theTmpCrP, *theTmpTotP, i;
  int		theEnumGrNb;
  Module	*theElemP;
  
  
  theElemP = inModuleP;
  theTmpCrP = inGrToCreateP;
  
  /* initialize the array of groups to create to empty */
  for (i = 0; i < END_GROUP; i++) theTmpCrP [i] = 0;
  
  /* test to see wether there is a list of existing groups or not */
  if (inDSGroupsTotP != NULL)
  {
    theTmpTotP = inDSGroupsTotP;
    for (i = 0; i < gArrModule [inModuleID]; i++)
    {
      theEnumGrNb = Papy3ToEnumGroup (theElemP->group);
    
      /* if this group is not already in the array put it in the to create list */
      if (*(theTmpTotP + theEnumGrNb) == 0) 
      {
        *(theTmpCrP  + theEnumGrNb) = 1;
        *(theTmpTotP + theEnumGrNb) = 1;
      } /* if */
    
      /* next element of the module */
      theElemP++;
    } /* for ...loop on the elements of the module */
  
  } /* if ...existing list of read groups */
  else /* no list just add the groups found to the list */
  {
    for (i = 0; i < gArrModule [inModuleID]; i++)
    {
      theEnumGrNb = Papy3ToEnumGroup (theElemP->group);
    
      /* put the group in the to read list */
      if (theTmpCrP [theEnumGrNb] == 0)
        theTmpCrP [theEnumGrNb] = 1;
    
      /* next element of the module */
      theElemP++;
    } /* for ...loop on the elements of the module */
  } /* else */
  
} /* endof LookForGroupsInModule3 */



/********************************************************************************/
/*										*/
/*	SequencesToGroups3 : Convert the list of items of the sequence to a list*/
/*	of a list of groups. If the toDel param is set to TRUE, deletes the 	*/
/*	modules in the list too.						*/
/*	return : papNoError if no problem, standard error message otherwise.	*/
/*										*/
/********************************************************************************/

PapyShort
SequencesToGroups3 (PapyShort inFileNb, Item *inSequenceP, int inToDel)
{
  Item		*theWrkItemListP;
  PapyShort	theErr;
  

  /* loop on the items of the sequence */
  theWrkItemListP = inSequenceP;
  while (theWrkItemListP != NULL)
  {
    switch (theWrkItemListP->object->whoAmI)
    {
      case papModule:
        /* translate the content of the data set to a list of groups */
        if ((theErr = ItemModulesToGroups3 (inFileNb, theWrkItemListP, inToDel)) < 0)
          RETURN (theErr);
        break;

      case papRecord:
        /* translate the content of the data set to a list of groups */
        if ((theErr = ItemRecordsToGroups3 (inFileNb, theWrkItemListP, inToDel)) < 0)
        break;

      case papItem:
      /*  if (theWrkItemListP->object->item != NULL)
        {*/
          switch (theWrkItemListP->object->item->object->whoAmI)
          {
            case papRecord:
              /* translate the content of the data set to a list of groups */
              if ((theErr = ItemRecordsToGroups3 (inFileNb, theWrkItemListP, inToDel)) < 0)
              break;

            default :
              SequencesToGroups3 (inFileNb, theWrkItemListP->object->item, inToDel);
              break;
          } /* switch ...theWrkItemListP->object->item->object->whoAmI */
   /*     }*/
        break;
        
      default:
        break;
    } /* switch (theWrkItemListP->object->whoAmI) */

    /* get next item of the sequence */
    theWrkItemListP = theWrkItemListP->next;
    
  } /* while ...loop on the items of the sequence */

  return 0;
   
} /* endof SequencesToGroups3 */



/********************************************************************************/
/*										*/
/*	KeepReferences3 : Keep some references to elements that will be needed  */
/* 	by the modules of the pointer sequence.					*/
/*										*/
/********************************************************************************/

void
KeepReferences3 (PapyShort inFileNb, int inModuleID, int inElementID, UValue_T *inValP)
{
  
  
  switch (inModuleID)
  {
    case SOPCommon :
      if (inElementID == papSOPClassUID) 
        /* it is the same SOP class UID for the whole file */
        if (gRefSOPClassUID [inFileNb] == NULL)
          gRefSOPClassUID [inFileNb] = PapyStrDup (inValP->a);
          
      if (inElementID == papSOPInstanceUID)
        gRefSOPInstanceUID = PapyStrDup (inValP->a);
            
      break;
     case GeneralImage : /* papInstanceNumber was papImageNumberGI before */
       if (inElementID == papInstanceNumberGI) gRefImageNb = PapyStrDup (inValP->a);
       break;
     case ImagePixel :
       switch (inElementID)
       {
          case papRows :
            gRefRows = inValP->us;
            break;
          case papColumns :
            gRefColumns = inValP->us;
            break;
          case papBitsAllocatedIP :
            gRefBitsAllocated = inValP->us;
            break;
          case papBitsStoredIP :
            gRefBitsStored = inValP->us;
            break;
          case papHighBitIP :
            gRefHighBit = inValP->us;
            break;
          case papPixelRepresentationIP :
            gRefIsSigned = inValP->us;
            break;
          case papSmallestImagePixelValue :
            gRefPixMin = inValP->us;
            break;
          case papLargestImagePixelValue :
            gRefPixMax = inValP->us;
            break;
          case papPixelData :   
            gRefPixelData = inValP->ow;
            break;
        } /* switch ...elems of image pixel module */
        break;
     case VOILUT :
       switch (inElementID)
       {
         case papWindowWidth :
           gRefWW = atoi (inValP->a);
           break;
         case papWindowCenter :
           gRefWL = atoi (inValP->a);
           break;
       } /* switch ...elems of VOILUT module */
       break;
            
  } /* switch ...module ID */
  
} /* endof KeepReferences3 */


/********************************************************************************/
/*										*/
/*	Papy3CheckDirectoryInformation : 					*/
/*	return : 								*/
/*										*/
/********************************************************************************/

void 
Papy3CheckDirectoryInformation (Module *inModuleP)
{
  PapyULong   theLongValue;
  PapyUShort  theShortValue;

  
  /* initialize */
  theLongValue  = 0L;
  theShortValue = 0;

  /* 0004:1200 */
  Papy3PutElement (inModuleP, papOffsetofTheFirstDirectoryRecord, &theLongValue);
  /* 0004:1202 */
  Papy3PutElement (inModuleP, papOffsetofTheLastDirectoryRecord, &theLongValue);
  /* 0004:1212 */
  Papy3PutElement (inModuleP, papFilesetConsistencyFlag, &theShortValue);
  /* 0004:1220 */
  /*Papy3PutElement (inModuleP, papDirectoryRecordSequence, &theLongValue);*/

} /* endof Papy3CheckDirectoryInformation */


/********************************************************************************/
/*										*/
/*	ItemModulesToGroups3 : Convert the list of modules of the Item to 	*/
/*	a list of groups. If the toDel param is set to TRUE, deletes the 	*/
/*	modules too.								*/
/*	return : papNoError if no problem, standard error message otherwise.	*/
/*										*/
/********************************************************************************/

PapyShort
ItemModulesToGroups3 (PapyShort inFileNb, Item *ioDataSetP, int inToDel)
{
  int		*theDSGroupsTotP, *theGrToCreateP, *theTmpP;
  int		i, theEnumGr, theEnumTag, found, theElemType;
  UValue_T	*theValP, *theTmpValP;
  Module	*theWrkModP;
  SElement	*theGroupP;
  Object	*theObjectP;
  Item		*theWrkItemMP, *theGroupListP, *theWrkItemGP;
  PapyShort	theErr, theNbOfModuleElem;
  PapyUShort	theElemTag;
  PapyULong	theNbVal, theLoop;
  
  
  /* initialize the lists of groups contained in the data set to empty */
  theDSGroupsTotP = (int *) ecalloc3 ((PapyULong) END_GROUP, (PapyULong) sizeof (int));
  theGrToCreateP  = (int *) ecalloc3 ((PapyULong) END_GROUP, (PapyULong) sizeof (int));
  for (i = 0, theTmpP = theDSGroupsTotP; i < END_GROUP; i++, theTmpP++) *theTmpP = 0;
  
  /* perform some pointer initialization */
  theWrkItemMP = (Item *) ioDataSetP->object->item;	/* points on the first module */
  theGroupListP = NULL;					/* make sure it is blank */
  
  /* loop on the modules of the data set */
  while (theWrkItemMP != NULL)
  {
    /* if it is a module */
    if (theWrkItemMP->object->whoAmI == papModule)
    {
      /* Fill Directory Information module */
      if (theWrkItemMP->object->objID == DirectoryInformation)
          Papy3CheckDirectoryInformation (theWrkItemMP->object->module);

      /* find the groups to create from the description of the module */
      LookForGroupsInModule3 (theWrkItemMP->object->module, theWrkItemMP->object->objID,
      			      theDSGroupsTotP, theGrToCreateP);
    
      /* creation of the needed groups */
      for (i = 0; i < END_GROUP; i++)
      {
        /* do we have to add the group to the list of groups of the data set */
        if (*(theGrToCreateP + i) == 1)
        {
          /* creation of the group */
          theGroupP = Papy3GroupCreate (i);
        
          /* creation of the object to point to the group */
          theObjectP = (Object *) emalloc3 ((PapyULong) sizeof (Object));
          theObjectP->whoAmI        = papGroup;
          theObjectP->objID         = i;
          theObjectP->group         = theGroupP;	/* link the group to the object */
  	      theObjectP->item          = NULL;
  	      theObjectP->module        = NULL;
  	      theObjectP->tmpFileLength = 0L;
        
          /* insert the object at its place in the list of groups of the data set */
          theWrkItemGP = InsertGroupInList ((Item **) &theGroupListP, theObjectP);
         
        } /* if ...add the group to the list of groups of the data set */
      } /* for ...loop on the array of groups */
    
      
      /* copy the elements of the module to the groups */
      theNbOfModuleElem = gArrModule [theWrkItemMP->object->objID];
      for (i = 0; i < theNbOfModuleElem; i++)
      {
        /* get the element from the module */
        theValP = Papy3GetElement (theWrkItemMP->object->module, i, &theNbVal, &theElemType);
      
        if (theValP != NULL)
        {
          /* Keep references to some elements to create the pointer sequence */
          KeepReferences3 (inFileNb, theWrkItemMP->object->objID, i, theValP);
        
          /* extract the group number to put the element to */
          theWrkModP = (Module *) (theWrkItemMP->object->module + i);
          theEnumGr  = Papy3ToEnumGroup (theWrkModP->group);
          theElemTag = theWrkModP->element;

          /* locate the group to which to put the element in the group list */
          found = FALSE;
          theWrkItemGP = theGroupListP;
          while (!found && theWrkItemGP != NULL)
            if (theWrkItemGP->object->objID == theEnumGr) found = TRUE;
            else theWrkItemGP = theWrkItemGP->next;
      
          /* locate the enum place of the element given its tag */
          theGroupP = theWrkItemGP->object->group;
          found = FALSE;
          theEnumTag = 0;
          while (!found)
            if (theGroupP->element == theElemTag) found = TRUE;
            else
            {
              theEnumTag++;
              theGroupP++;
            } /* else */
          
          /* put the value(s) to the group */
          theTmpValP = theValP;
          for (theLoop = 0L; theLoop < theNbVal; theLoop++)
          {
          
            /* depending on the VR of the element put it to the group */
            switch (theWrkModP->vr)
            {
              case SS :
                Papy3PutElement (theWrkItemGP->object->group, theEnumTag, &(theTmpValP->ss)); 
                break;
              case USS :
              case AT :
                Papy3PutElement (theWrkItemGP->object->group, theEnumTag, &(theTmpValP->us)); 
                break;
              case OB :
                Papy3PutImage (inFileNb, theWrkItemGP->object->group, theEnumTag, 
            		       (PapyUShort *) theTmpValP->a, 0, 0, 8, theWrkModP->length);
                break;
              case OW :
                Papy3PutImage (inFileNb, theWrkItemGP->object->group, theEnumTag, theTmpValP->ow,
            		       0, 0, 16, theWrkModP->length);
                break;
              case SL :
                Papy3PutElement (theWrkItemGP->object->group, theEnumTag, &(theTmpValP->sl)); 
                break;
              case UL :
                Papy3PutElement (theWrkItemGP->object->group, theEnumTag, &(theTmpValP->ul)); 
                break;
              case FL :
                Papy3PutElement (theWrkItemGP->object->group, theEnumTag, &(theTmpValP->fl)); 
                break;
              case FD :
                Papy3PutElement (theWrkItemGP->object->group, theEnumTag, &(theTmpValP->fd)); 
                break;
              case AE :
              case AS :
              case CS :
              case DA :
              case DS :
              case DT :
              case IS :
              case LO :
              case LT :
              case PN :
              case SH :
              case ST :
              case TM :
              case UI :
              case UT :
                Papy3PutElement (theWrkItemGP->object->group, theEnumTag, &(theTmpValP->a)); 
                break;
              case UN :
		            Papy3PutUnknown (theWrkItemGP->object->group, theEnumTag, theTmpValP->a, theWrkModP->length);
		            break;
              case SQ :
                /* convert the list of items of the sequence to a list of groups */
                if ((theErr = SequencesToGroups3 (inFileNb, theTmpValP->sq, inToDel)) < 0) RETURN (theErr);
            
                Papy3PutElement (theWrkItemGP->object->group, theEnumTag, &(theTmpValP->sq)); 
                break;
            } /* switch */
            
            theTmpValP++;
          
          } /* for ...loop on the value(s) of an element */
      
        } /* if ...theValP != NULL */
 
      } /* for ...loop on the elements of the module */
      
  
    } /* if ...it is a module */
    
    
    /* if it is a group insert it at its place in the list */
    /* nota : this is done to allow for UIN Overlays to be in the data set */
    else if (theWrkItemMP->object->whoAmI == papGroup)
    {
      /* creation of the object to point to the group */
      theObjectP = (Object *) emalloc3 ((PapyULong) sizeof (Object));
      theObjectP->whoAmI = papGroup;
      theObjectP->objID  = theWrkItemMP->object->objID;
      theObjectP->group  = theWrkItemMP->object->group;	/* link the group to the object */
      theObjectP->item   = NULL;
      theObjectP->module = NULL;
      theObjectP->tmpFileLength = 0L;
        
       
      /* insert the object at its place in the list of groups of the data set */
      theWrkItemGP = InsertGroupInList ((Item **) &theGroupListP, theObjectP);
    
    } /* else ...it is a group */
    
    /* examine next module (or group) */
    theWrkItemMP = theWrkItemMP->next;
    
  } /* while ...loop on the modules of the data set */
  

  /* delete the lists of groups number of the data set */
  efree3 ((void **) &theDSGroupsTotP);
  efree3 ((void **) &theGrToCreateP);
  
  /* deletes the list of modules and put the list of groups instead */
  /* deletes the groups but not the sequences */
  if ((theErr = DeleteList (inFileNb, (Papy_List **) &(ioDataSetP->object->item), inToDel, FALSE, FALSE)) < 0)
    RETURN (theErr);
  ioDataSetP->object->item = theGroupListP;

  return 0;
  
} /* endof ItemModulesToGroups3 */


/********************************************************************************/
/*										*/
/*	ItemRecordsToGroups3 : Convert the list of records of the Item to 	*/
/*	a list of groups. If the toDel param is set to TRUE, deletes the 	*/
/*	record too.								*/
/*	return : papNoError if no problem, standard error message otherwise.	*/
/*										*/
/********************************************************************************/

PapyShort
ItemRecordsToGroups3 (PapyShort inFileNb, Item *ioDataSetP, int inToDel)
{
  int		*theDSGroupsTotP, *theGrToCreateP, *theTmpP;
  int		i, j, theEnumGr, theEnumTag, found, theElemType;
  UValue_T	*theValP, *theTmpValP;
  SElement	*theWrkModP;
  SElement	*theGroupP;
  Object	*theObjectP;
  Item		*theWrkItemMP, *theGroupListP, *theWrkItemGP;
  PapyShort	theErr, theNbOfRecordElem;
  PapyUShort	theElemTag;
  PapyULong	theNbVal, theLoop, theNbRecordGroups, theNbTotGroups;
		
  
  /* initialize the lists of groups contained in the data set to empty */
  theDSGroupsTotP = (int *) ecalloc3 ((PapyULong) END_GROUP, (PapyULong) sizeof (int));
  theGrToCreateP  = (int *) ecalloc3 ((PapyULong) END_GROUP, (PapyULong) sizeof (int));
  for (i = 0, theTmpP = theDSGroupsTotP; i < END_GROUP; i++, theTmpP++) *theTmpP = 0;
  
  /* perform some pointer initialization */
  theWrkItemMP  = (Item *) ioDataSetP->object->item;	/* points on the first record */
  theGroupListP = NULL;			/* make sure it is blank */

  /* count the number of groups for each records */
  theNbRecordGroups = 0L;
  theNbTotGroups    = 0L;

  /* loop on the records or groups of the data set */
  while (theWrkItemMP != NULL)
  {
    /* get item which contain the record
    /* if it is a record */
    if (theWrkItemMP->object->whoAmI == papRecord)
    {
      theNbRecordGroups = 0;
        
      /* find the groups to create from the description of the record */
      LookForGroupsInRecord3 (theWrkItemMP->object->record, theWrkItemMP->object->objID, theGrToCreateP);
      
      /* creation of the needed groups */
      for (i = 0; i < END_GROUP; i++)
      {
        /* do we have to add the group to the list of groups of the data set */
        if (*(theGrToCreateP + i) == 1)
        {	  
          /* creation of the group */
          theGroupP = Papy3GroupCreate (i);

          /* creation of the object to point to the group */
          theObjectP = (Object *) emalloc3 ((PapyULong) sizeof (Object));
          theObjectP->whoAmI        = papGroup;
          theObjectP->objID         = i;
          theObjectP->group         = theGroupP;	/* link the group to the object */
          theObjectP->item          = NULL;
          theObjectP->record        = NULL;
          theObjectP->module        = NULL;
          theObjectP->tmpFileLength = 0L;
        
          /* insert the object at its place in the list of groups of the data set */
          theWrkItemGP = InsertLastInList ((Item **) &theGroupListP, theObjectP);
          
          /* only useful for records sequence */
          theNbRecordGroups++;

        } /* if ...add the group to the list of groups of the data set */
      } /* for ...loop on the array of groups */
      
      /* copy the elements of the record to the groups */
      theNbOfRecordElem = gArrRecord [theWrkItemMP->object->objID];
      for (i = 0; i < theNbOfRecordElem; i++)
      {
        /* get the element from the record */
        theValP = Papy3GetElement (theWrkItemMP->object->record, i, &theNbVal, &theElemType);
      
        if (theValP != NULL)
        {
          /* Keep references to some elements to create the pointer sequence */
          KeepReferences3 (inFileNb, theWrkItemMP->object->objID, i, theValP);
        
          /* extract the group number to put the element to */
          theWrkModP = (SElement *) (theWrkItemMP->object->record + i);
          theEnumGr  = Papy3ToEnumGroup (theWrkModP->group);
          theElemTag = theWrkModP->element;

          /* locate the group to which to put the element in the group list */
          found        = FALSE;
          theWrkItemGP = theGroupListP;
          
          /* found the just created groups */
          for (j = 0; j < (int) theNbTotGroups; j++) 
              theWrkItemGP = theWrkItemGP->next;
      
          while (!found && theWrkItemGP != NULL)
            if (theWrkItemGP->object->objID == theEnumGr) 
              found = TRUE;
            else 
              theWrkItemGP = theWrkItemGP->next;
      
          /* locate the enum place of the element given its tag */
          theGroupP  = theWrkItemGP->object->group;
          found      = FALSE;
          theEnumTag = 0;
          while (!found)
            if (theGroupP->element == theElemTag) 
              found = TRUE;
            else
            {
              theEnumTag++;
              theGroupP++;
            } /* else */
          
          /* put the value(s) to the group */
          theTmpValP = theValP;
          for (theLoop = 0L; theLoop < theNbVal; theLoop++)
          {
            /* depending on the VR of the element put it to the group */
            switch (theWrkModP->vr)
            {
              case SS :
                Papy3PutElement (theWrkItemGP->object->group, theEnumTag, &(theTmpValP->ss)); 
                break;
              case USS :
              case AT :
                Papy3PutElement (theWrkItemGP->object->group, theEnumTag, &(theTmpValP->us)); 
                break;
	            case OB :
                Papy3PutImage (inFileNb, theWrkItemGP->object->group, theEnumTag, 
            		       (PapyUShort *) theTmpValP->a, 0, 0, 8, theWrkModP->length);
                break;
              case OW :
                Papy3PutImage (inFileNb, theWrkItemGP->object->group, theEnumTag, theTmpValP->ow,
            		       0, 0, 16, theWrkModP->length);
                break;
              case SL :
                Papy3PutElement (theWrkItemGP->object->group, theEnumTag, &(theTmpValP->sl)); 
                break;
              case UL :
                Papy3PutElement (theWrkItemGP->object->group, theEnumTag, &(theTmpValP->ul)); 
                break;
              case FL :
                Papy3PutElement (theWrkItemGP->object->group, theEnumTag, &(theTmpValP->fl)); 
                break;
              case FD :
                Papy3PutElement (theWrkItemGP->object->group, theEnumTag, &(theTmpValP->fd)); 
                break;
              case AE :
              case AS :
              case CS :
              case DA :
              case DS :
              case DT :
              case IS :
              case LO :
              case LT :
              case PN :
              case SH :
              case ST :
              case TM :
              case UI :
                Papy3PutElement (theWrkItemGP->object->group, theEnumTag, &(theTmpValP->a)); 
                break;
              case SQ :
                /* convert the list of items of the sequence to a list of groups */
                if ((theErr = SequencesToGroups3 (inFileNb, theTmpValP->sq, inToDel)) < 0)
                  RETURN (theErr);
                Papy3PutElement (theWrkItemGP->object->group, theEnumTag, &(theTmpValP->sq));                	
                break;
            } /* switch */
            
            theTmpValP++;
          
          } /* for ...loop on the value(s) of an element */
      
        } /* if ...theValP != NULL */
 
      } /* for ...loop on the elements of the module */
      
  
    } /* if ...it is a record */
    
    
    /* if it is a group insert it at its place in the list */
    else    
      
    if (theWrkItemMP->object->whoAmI == papGroup)
    {
      /* creation of the object to point to the group */
      theObjectP = (Object *) emalloc3 ((PapyULong) sizeof (Object));
      theObjectP->whoAmI 	= papGroup;
      theObjectP->objID  	= theWrkItemMP->object->objID;
      theObjectP->group  	= theWrkItemMP->object->group;/* link the group to the object */
      theObjectP->item   	= NULL;
      theObjectP->module 	= NULL;
      theObjectP->record 	= NULL;
      theObjectP->tmpFileLength = 0L;
        
       
      /* insert the object at its place in the list of groups of the data set */
      theWrkItemGP = InsertGroupInList ((Item **) &theGroupListP, theObjectP);
    
    } /* else ...it is a group */
    
    /* only useful for record sequence */
    theNbTotGroups += theNbRecordGroups;
    
    /* examine next record */
    theWrkItemMP = theWrkItemMP->next;
    
  } /* while ...loop on the modules of the data set */
  

  /* delete the lists of groups number of the data set */
  efree3 ((void **) &theDSGroupsTotP);
  efree3 ((void **) &theGrToCreateP);
  
  /* deletes the list of records and put the list of groups instead */
  /* deletes the groups but not the sequences */
  if ((theErr = DeleteList (inFileNb, (Papy_List **) &(ioDataSetP->object->item), inToDel, FALSE, FALSE)) < 0)
    RETURN (theErr);
  ioDataSetP->object->item = theGroupListP;
  
  return 0;
  
} /* endof ItemRecordsToGroups3 */



/********************************************************************************/
/*										*/
/*	CreateTmpFile3 : Creates and open a temporary file to store the groups	*/
/*	of a data set. 								*/
/*	return : a pointer to an object containing the tmp file, NULL otherwise	*/
/*										*/
/********************************************************************************/

PapyShort
CreateTmpFile3 (PapyShort inFileNb, PAPY_FILE *ioFpP, void **ioVoidP)
{
  char		*theFilenameP, theChar [256], theOtherStr [10];
  int		theLength;
  PapyShort	theErr;
  
  
  /* create the name of the temporary file */
  /* i.e. takes the name of the file and concatenate an incremental number */
  theFilenameP = (char *) &theChar [0];
  strcpy (theFilenameP, gPapFilename [inFileNb]);
  
  /* DICOM file case & first image of the data set*/
  if ((gIsPapyFile [inFileNb] == DICOM10 || gIsPapyFile [inFileNb] == DICOM_NOT10) &&
      gCurrTmpFilename [inFileNb] == 1)
  {
    /* try to open the file */
    theErr = Papy3FOpen (theFilenameP, 'w', (PAPY_FILE) 0, ioFpP, ioVoidP);
    switch (theErr)
    {
      case -49:	/* file was already open in write mode */
        /* in this case the file pointer is simply the original one ... */
        *ioFpP = gPapyFile [inFileNb];

      case 0:	/* was able to open the file in write mode */
        /* increment this to ensure uniqueness of temporary files */
        gCurrTmpFilename [inFileNb]++;
    
        /* remove the "0001.dcm" string from the filename if present */
        theLength = (int) strlen (theFilenameP);
        if (theFilenameP [theLength - 8] == '0' &&
    	    theFilenameP [theLength - 7] == '0' &&
    	    theFilenameP [theLength - 6] == '0' &&
    	    theFilenameP [theLength - 5] == '1' &&
    	    theFilenameP [theLength - 4] == '.' &&
    	    theFilenameP [theLength - 3] == 'd' &&
    	    theFilenameP [theLength - 2] == 'c' &&
    	    theFilenameP [theLength - 1] == 'm')
        {
          theFilenameP [theLength - 8] = '\0';
          strcpy (gPapFilename [inFileNb], theFilenameP);
        } /* if ...string ends by "0001.dcm" */
        
        /* makes sure the file pointer is set correctly */
        gPapyFile [inFileNb] = *ioFpP;
        
        /* then exits successfully the function */
        return 0;
        break;

      default:	/* file does not exist */
        break;
    } /* switch */
      
  } /* if ... DICOM file */
  
  /* append the incremental number to the filename */
  Papy3FPrint (theOtherStr, "%d", gCurrTmpFilename [inFileNb]);
  strcat (theOtherStr, ".dcm");
  strcat (theOtherStr, "\0");
  if (gCurrTmpFilename [inFileNb]      < 10)
    strcat (theFilenameP, "000");
  else if (gCurrTmpFilename [inFileNb] < 100)
    strcat (theFilenameP, "00");
  else if (gCurrTmpFilename [inFileNb] < 1000)
    strcat (theFilenameP, "0");
  strcat (theFilenameP, theOtherStr);
  
  /* increment the number to ensure uniqueness of tmp filenames */
  gCurrTmpFilename [inFileNb]++;
  
  /* creation of the temporary file */
  while (Papy3FCreate (theFilenameP, (PAPY_FILE) -1, ioFpP, ioVoidP) < 0 &&
  	 gCurrTmpFilename [inFileNb] < kMax_tmp_file) 
  {
    /* free the memory if some was allocated */
    /*if (*ioVoidP != NULL)
        efree3 ((void **) ioVoidP);*/
    
    /* re-create the name of the temporary file */
    Papy3FPrint (theOtherStr, "%d", gCurrTmpFilename [inFileNb]);
    strcat (theOtherStr, ".dcm");
    strcat (theOtherStr, "\0");
    strcpy (theFilenameP, gPapFilename [inFileNb]);
    if (gCurrTmpFilename [inFileNb]      < 10)
      strcat (theFilenameP, "000");
    else if (gCurrTmpFilename [inFileNb] < 100)
      strcat (theFilenameP, "00");
    else if (gCurrTmpFilename [inFileNb] < 1000)
      strcat (theFilenameP, "0");
    strcat (theFilenameP, theOtherStr);
      
    /* re-increment the number to ensure uniqueness of tmp filenames */
    gCurrTmpFilename [inFileNb]++;
    
  } /* while ...looking for a temp file to create */

  /* the file creation failed */
  if (gCurrTmpFilename [inFileNb] == kMax_tmp_file)
  {
    /*if (*ioVoidP != NULL)
        efree3 ((void **) ioVoidP); */
    RETURN (papFileCreationFailed);
  } /* if ...file creation failed */
   
  /* open the created tmp file */
  if (Papy3FOpen (theFilenameP, 'w', (PAPY_FILE) 0, ioFpP, ioVoidP) < 0)
  {
    /*if (*ioVoidP != NULL)
        efree3 ((void **) ioVoidP);*/
    RETURN (papFileCreationFailed);
  } /* if ...opening the file failed */
  
  /* this is to mark the DICOM file as being in use for the skeleton */
  if ((gIsPapyFile [inFileNb] == DICOM10 || gIsPapyFile [inFileNb] == DICOM_NOT10) &&
      gPapyFile [inFileNb] == 0)
    gPapyFile [inFileNb] = *ioFpP;
  
  return 0;

} /* endof CreateTmpFile3 */



/********************************************************************************/
/*										*/
/*	WriteDicomHeader3 : Writes the DICOM header at the begining of the tmp	*/
/*	file (i.e. 128 bytes set to blank followed by the DICM string) as well 	*/
/* 	as the group 0x0002 which contains the file meta information 		*/
/*	return : standard error message						*/
/*										*/
/********************************************************************************/

PapyShort
WriteDicomHeader3 (PAPY_FILE inFp, PapyShort inFileNb, PapyULong *outMetaInfoSizeP)
{
  PapyShort	theErr = 0;
  PapyULong	theNumberOfBytes;
  char		theBuff [128];
  int		i;
  
  
  /* set 128 bytes to blank */
  for (i = 0; i < 128; i++) theBuff [i] = 0;
  theNumberOfBytes = 128L;
  
  /* write them to the file */
  if (Papy3FWrite (inFp, (PapyULong *) &theNumberOfBytes, 1, theBuff) < 0)
  {
    Papy3FClose (&inFp);
    RETURN (papWriteFile)
  } /* if */
  
  /* then put the DICM string that identifies the file as a DICOM one */
  strcpy (theBuff, "DICM");
  theNumberOfBytes = 4L;
  
  /* write the string to the file */
  if (Papy3FWrite (inFp, (PapyULong *) &theNumberOfBytes, 1, theBuff) < 0)
  {
    Papy3FClose (&inFp);
    RETURN (papWriteFile)
  } /* if */
  
  /* creation and writting of the file meta information (i.e. group 0x0002) */
  theErr = CreateDicomFileMetaInformation3 (inFp, inFileNb, gArrCompression [inFileNb], gArrTransfSyntax [inFileNb], 
  					    (enum EModality) gFileModality [inFileNb], outMetaInfoSizeP);

  return theErr;
  
} /* endof WriteDicomHeader3 */



/********************************************************************************/
/*										*/
/*	WriteTmpFile3 : Creates a temporary file to store the groups of a given	*/
/*	data set. It will save as well some relativ positions in the file.	*/
/*	return : papNoError if no problem, standard error message otherwise.	*/
/*										*/
/********************************************************************************/

PapyShort
WriteTmpFile3 (PapyShort inFileNb, Item *ioDataSetP, int inToDel)
{
  int		theGroupNb;
  PapyShort	theErr, theImNb;
  PAPY_FILE	theFp;
  PapyULong	theBufSize, theMetaInfoSize, thePos, theTmpUL;
  unsigned char *theBuffP;
  Item		*theWrkItemP;
  void		*theVoidP;
  
  
  theVoidP     	  = NULL;
  theMetaInfoSize = 0L;
  theFp	       	  = NULLFILE;

  /* create the temporary file that will contain the given data set */
  if ((theErr = CreateTmpFile3 (inFileNb, &theFp, &theVoidP)) < 0)
    RETURN (papFileCreationFailed);
  
  /* if the file is a DICOM one, then put the DICOM header to the temp file */
  /* in order to get a real DICOM file part 10 compliant */
  if (gIsPapyFile [inFileNb] == DICOM10)
    theErr = WriteDicomHeader3 (theFp, inFileNb, &theMetaInfoSize);
  
  /* find the place of the data set in the list of data sets */
  theImNb = 0;
  theWrkItemP = gImageSequenceItem [inFileNb];
  while (theWrkItemP != ioDataSetP && theWrkItemP != NULL)
  {
    theImNb++;
    theWrkItemP = theWrkItemP->next;
  } /* while ...loop on the data sets */

  /* initialisation */
  gCurrentOverlay    [inFileNb] = 0x6000;
  gCurrentUINOverlay [inFileNb] = 0x6001;
  
  /* take a pointer to the first group of the data set */
  theWrkItemP = (Item *) ioDataSetP->object->item;  
  
  /* loop on the groups of the data set */
  while (theWrkItemP != NULL)
  {
    if ((theGroupNb = Papy3ToEnumGroup (theWrkItemP->object->group->group)) < 0)
	RETURN (papGroupNumber);

    /* compute the size of the current group */
    ComputeGroupLength3 ((PapyShort) theGroupNb, theWrkItemP->object->group, NULL, 
			 gArrTransfSyntax [inFileNb]);
    theBufSize = theWrkItemP->object->group->value->ul + kLength_length;
    
    /* alloc the buffer that will contain the ready to write group */
    theBuffP = (unsigned char *) emalloc3 ((PapyULong) theBufSize);
    
    thePos = 0L;    
    /* put the elements of the group to the write buffer */
    if ((theErr = PutGroupInBuffer (inFileNb, theImNb, theGroupNb, theWrkItemP->object->group, 
    				    theBuffP, &thePos, FALSE)) < 0) RETURN (theErr);
    
    /* add the offsets to the data set to the offset to the group */
    if (theWrkItemP->object->group->group == 0x7FE0)
    {
      Papy3FTell (theFp, (PapyLong *) &theTmpUL);
      *(gRefPixelOffset  [inFileNb] + theImNb) += theTmpUL;
    } /* if */
    
    /* write the buffer to the temporary file */
    if ((theErr = WriteGroup3 (theFp, theBuffP, theBufSize)) < 0)
      RETURN (theErr);  
    
    /* frees the allocated buffer */
    efree3 ((void **) &theBuffP);
    
    /* get next item */
    theWrkItemP = theWrkItemP->next;
    
  } /* while ...loop on the groups of the data set */
  
  /* deletes the list of groups of the data set and put the tmp file instead */
  if ((theErr = DeleteList (inFileNb, (Papy_List **) &(ioDataSetP->object->item), inToDel, TRUE, TRUE)) < 0) 
    RETURN (theErr);
  
  ioDataSetP->object->whoAmI  = papTmpFile;
  ioDataSetP->object->objID   = gCurrTmpFilename [inFileNb] - 1;
  ioDataSetP->object->file    = theVoidP;
  
  /* stores the length of the tmp file */
  if (Papy3FSeek (theFp, (int) SEEK_END, (PapyLong) 0L) != 0)
    RETURN (papPositioning);
  Papy3FTell (theFp, (PapyLong *) &(ioDataSetP->object->tmpFileLength));

  
  /* close the temporary file */
  Papy3FClose (&theFp);
  
  return 0;

} /* endof WriteTmpFile3 */



/********************************************************************************/
/*										*/
/*	Papy3CloseDataSet : Close the given data set. If it is the first data 	*/
/*	set to be closed, it builds the summaries modules from the data set.It 	*/
/*	translates the modules into a group representation, frees the modules 	*/
/*	if the toDel param is set to TRUE. Then it saves the list of groups on 	*/
/*	a temporary file that is ready to be copied in the definitiv Papyrus 	*/
/*	file.									*/
/*	return : papNoError if no problem, standard error message otherwise.	*/
/*										*/
/********************************************************************************/

PapyShort CALLINGCONV
Papy3CloseDataSet (PapyShort inFileNb, Item *inDataSetP, int inToDel, int inPerformCheck)
{
  PapyShort	theErr;
  
  
  /* introduce some odd value to be tested */
  gRefPixMin =  0;
  gRefPixMax =  0;
  gRefWW     = -1;
  gRefWL     = -1;
  
  /* check if all the needed modules have been filled only if specified */
  if (inPerformCheck)
    if ((theErr = CheckDataSetModules3 (inFileNb, inDataSetP)) < 0) RETURN (theErr);

  /* perform the following task only if it is a PAPYRUS file */
  if (gIsPapyFile [inFileNb] == PAPYRUS3)
  {
    /* if it is the first data set to be saved, create the summaries modules */
    if (gPatientSummaryItem [inFileNb] == NULL) 
      if ((theErr = CreateSummaries3 (inFileNb)) < 0) RETURN (theErr);
  } /* if ...it is a PAPYRUS file */
  
  /* convert the module representation to the group representation */
  if ((theErr = ItemModulesToGroups3 (inFileNb, inDataSetP, inToDel)) < 0)
    RETURN (theErr);
  
  /* perform the following task only if it is a PAPYRUS file */
  if (gIsPapyFile [inFileNb] == PAPYRUS3)
  {  
    /* creation of the pointer sequence for this data set */
    theErr = CreatePointerSequence3 (inFileNb, inDataSetP);
  } /* if ...it is a PAPYRUS file */
  
  /* write the data set to a temporary file */
  if ((theErr = WriteTmpFile3 (inFileNb, inDataSetP, inToDel)) < 0) RETURN (theErr);  
  
  RETURN (theErr);
  
} /* endof Papy3CloseDataSet */