This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPapyFiles3.c
2218 lines (1807 loc) · 81.7 KB
/
PapyFiles3.c
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
/********************************************************************************/
/* */
/* 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 [email protected] */
/* 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 : PapyFiles3.c */
/* Function : contains all the file functions */
/* Authors : Matthieu Funk */
/* Christian Girard */
/* Jean-Francois Vurlod */
/* Marianne Logean */
/* */
/* History : 12.1990 version 1.0 */
/* 04.1991 version 1.1 */
/* 12.1991 version 1.2 */
/* 06.1993 version 2.0 */
/* 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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <memory.h>
#ifndef Papyrus3H
#include "Papyrus3.h"
#endif
#ifdef _WINDOWS
#include <io.h>
#endif
enum {kPAPY_READ, kPAPY_WRITE}; /* are we in read or write mode ? */
/********************************************************************************/
/* */
/* FindFreeFile3 : find a free file number in the array of files or */
/* increment the current number of open files */
/* return : the number of the file, or standard error message */
/* */
/********************************************************************************/
PapyShort CALLINGCONV
FindFreeFile3 ()
{
PapyShort i;
for (i = 0; i < kMax_file_open; i++)
{
if (gPapyFile [i] == 0)
{
return i;
} /* if */
} /* for */
RETURN (papMaxOpenFile);
} /* endof FindFreeFile3 */
/********************************************************************************/
/* */
/* FileOpen3 : Given a filename open the file for reading */
/* return : no error if OK standard error message otherwise */
/* */
/********************************************************************************/
PapyShort
FileOpen3 (char *inNameP, PAPY_FILE inVRefNum, PAPY_FILE *outFp, void* inFSSpec)
{
PapyShort theErr;
if (inNameP == NULL || *inNameP == '\0')
RETURN (papFileName);
if (inFSSpec)
theErr = Papy3FOpen (inNameP, 'r', inVRefNum, outFp, &inFSSpec);
else
theErr = Papy3FOpen (inNameP, 'r', inVRefNum, outFp, NULL);
if ((theErr) < 0)
RETURN (papOpenFile);
return papNoError;
} /* endof FileOpen3 */
/********************************************************************************/
/* */
/* Papy3FileOpen : Given a filename open it and check if it is a Papyrus */
/* file by looking for the DICM string at offset 129. It extracts the */
/* modality and the transfert syntax used for this file. It initializes */
/* the pointers to the data sets and the pixel datas. */
/* return : a reference number to the opened file if successful */
/* standard error message otherwise */
/* */
/********************************************************************************/
PapyShort CALLINGCONV
Papy3FileOpen (char *inNameP, PAPY_FILE inVRefNum, int inToOpen, void* inFSSpec)
{
PAPY_FILE theFp;
char theFilename [256];
unsigned char theBuff [15], theVersion [8];
PapyLong theFilePos;
PapyULong theReadSize, theNbVal;
PapyShort theFileNb, theErr;
int i, theElemType;
enum EFile_Type thePapyrusFile = PAPYRUS3;
SElement *theGroupP;
UValue_T *theValP;
PapyShort iResult;
iResult = papNoError;
if (inToOpen)
{
/* open the file */
if ((theErr = FileOpen3 (inNameP, inVRefNum, &theFp, inFSSpec)) < 0)
{
iResult = papReadingOpenFile;
}
}
else
theFp = inVRefNum;
if (iResult == papNoError)
{
/* set the file pointer at the begining */
if ((theErr = (PapyShort) Papy3FSeek (theFp, (int) SEEK_SET, (PapyLong) 0L)) != 0)
{
iResult = papPositioning;
}
else
{
/* test if the "PAPYRUS 3.X" string is at the begining of the file */
theReadSize = 15L;
if ((theErr = (PapyShort) Papy3FRead (theFp, (PapyULong *) &theReadSize, 1L, theBuff)) < 0)
{
iResult = papReadFile;
}
else
{
/* compares the extracted string with the awaited string */
theBuff [14] = '\0';
/* if the PAPYRUS 3.0 string is not here it could be a basic DICOM file */
if (strncmp ((char *) theBuff, "PAPYRUS 3.", 10) != 0)
{
thePapyrusFile = DICOM10;
}
/* test the compatibility flag to ensure the file is readable by this */
/* version of the PAPYRUS toolkit */
if (thePapyrusFile == PAPYRUS3)
{
if ((char) theBuff [13] > gPapyrusCompatibility [0])
{
iResult = papReadFile;
} /* if ...incompatible version of the PAPYRUS file */
}
if (iResult == papNoError)
{
/* find a free place for the file */
theFileNb = FindFreeFile3 ();
if (theFileNb < 0)
{
iResult = theFileNb;
}
else
{
gPapyFile [theFileNb] = theFp;
gReadOrWrite [theFileNb] = kPAPY_READ;
/* set the papyrus version number */
if (thePapyrusFile == PAPYRUS3)
{
for (i = 0; i < 4; i++)
{
theVersion [i] = theBuff [i + 8];
}
theVersion [4] = '\0';
/* convert the result to a float */
gPapyrusFileVersion [theFileNb] = (float)atof ((char *) theVersion);
} /* if ...it is a PAPYRUS file */
else
{
/* put the current value */
gPapyrusFileVersion [theFileNb] = (float)atof ((char *) gPapyrusVersion);
}
/* set the transfert syntax to the default one */
gArrTransfSyntax [theFileNb] = LITTLE_ENDIAN_EXPL;
/* go to the place where the "DICM" prefix should be (position 128) */
if ((theErr = (PapyShort) Papy3FSeek (theFp, (int) SEEK_SET, (PapyLong) 128L)) != 0)
{
iResult = papPositioning;
}
else
{
theReadSize = 4L;
if ((theErr = (PapyShort) Papy3FRead (theFp, (PapyULong *) &theReadSize, 1L, theBuff)) < 0)
{
iResult = papReadFile;
}
else
{
/* compares the extracted string with the awaited string */
theBuff [4] = '\0';
if (strcmp ((char *) theBuff, "DICM") != 0)
{
/* it could still be a non-part 10 DICOM file */
/* so try to get the modality element, if everything works fine */
/* assume it is the case */
/* reset the file pointer at the begining of the file */
theErr = Papy3FSeek (theFp, (int) SEEK_SET, (PapyLong) 0L);
/* set the transfert syntax to the most banal one */
gArrTransfSyntax [theFileNb] = LITTLE_ENDIAN_IMPL;
gArrCompression [theFileNb] = NONE;
/* goto group number 8 and if found read it */
if ((theErr = Papy3GotoGroupNb (theFileNb, 0x0008)) < 0)
{
iResult = papNotPapyrusFile;
}
else
{
if ((theErr = Papy3GroupRead (theFileNb, &theGroupP)) < 0)
{
iResult = papNotPapyrusFile;
}
} /* else ...group 0x0008 found */
if (iResult == papNoError)
{
/* try to extract the modality */
theValP = Papy3GetElement (theGroupP, papModalityGr, &theNbVal, &theElemType);
if (theValP != NULL)
{
ExtractModality (theValP, theFileNb);
thePapyrusFile = DICOM_NOT10; /* non-part 10 DICOM file */
}
else
{
thePapyrusFile = DICOM10; /* neither a DICOM file nor a PAPYRUS one */
} /* theValp NULL */
/* free the group 8 */
theErr = Papy3GroupFree (&theGroupP, TRUE);
/* reset the file pointer to its previous position */
theErr = Papy3FSeek (gPapyFile [theFileNb], SEEK_SET, 0L);
/* neither a PAPYRUS nor a DICOM file */
if (thePapyrusFile == DICOM10)
{
iResult = papNotPapyrusFile;
}
} /* if ...no error til yet ... */
} /* if ...could it be a non-part 10 DICOM file ? */
else /* is it a DICOMDIR file? */
{
ExtractDicomdirFromPath (inNameP, theFilename);
if (theFilename [0] != '\0' &&
((strncmp ((char*) theFilename, "dicomdir", 8) == 0) ||
(strncmp ((char*) theFilename, "Dicomdir", 8) == 0) ||
(strncmp ((char*) theFilename, "DICOMDIR", 8) == 0)))
{/* it is a DICOMDIR file: find now if group 0004 exist */
/* set the file pointer at the begining of the Directory Information */
theErr = Papy3FSeek (theFp, (int) SEEK_SET, (PapyLong) 132L);
/* goto group number 4 */
if ((theErr = Papy3GotoGroupNb (theFileNb, 0x0004)) >= 0)
thePapyrusFile = DICOMDIR; /* DICOMDIR file */
/* reset the file pointer to its previous position */
theErr = Papy3FSeek (gPapyFile [theFileNb], SEEK_SET, 132L);
}/* if DICOMDIR filename */
} /* else is it a DICOMDIR file? */
if (iResult == papNoError)
{
/* is it a PAPYRUS or a basic DICOM file ? */
/* 0 = DICOM part 10, 1 = PAPYRUS 3.X, 2 = non-part 10 DICOM, 3 = DICOMDIR */
gIsPapyFile [theFileNb] = thePapyrusFile;
gNbShadowOwner [theFileNb] = 0;
/* shadow_group that we allow to read */
(void) Papy3AddOwner (theFileNb, "PAPYRUS 3.0");
/* read group 2 (File Meta Information) to extract the basic informations */
/* regarding the way of reading the file */
if (gIsPapyFile [theFileNb] != DICOM_NOT10)
{
if ((theErr = ExtractFileMetaInformation3 (theFileNb)) < 0)
{
iResult = theErr;
}
} /* if ...anything but a DICOM not 10 file */
if (iResult == papNoError)
{
if (gIsPapyFile [theFileNb] == DICOMDIR)
{
iResult = theFileNb;
}
else
{
/* extraction of the informations regarding groups 41 such as the number */
/* of images, the offsets to the data set and the pixel datas. */
/* !!! This is only done for the PAPYRUS 3.X files !!! */
if (gIsPapyFile [theFileNb] == PAPYRUS3)
{
if ((theErr = ExtractPapyDataSetInformation3 (theFileNb)) < 0)
{
iResult = theErr;
}
} /* if ...PAPYRUS file */
if (iResult == papNoError)
{
/* extract some information from group 28 */
/* first : keep the position in the file */
theErr = Papy3FTell (gPapyFile [theFileNb], &theFilePos);
/* then go to the data set */
if (gIsPapyFile [theFileNb] == DICOM10)
theErr = Papy3FSeek (gPapyFile [theFileNb], SEEK_SET, 132L);
else if (gIsPapyFile [theFileNb] == DICOM_NOT10)
theErr = Papy3FSeek (gPapyFile [theFileNb], SEEK_SET, 0L);
else if (gIsPapyFile [theFileNb] == PAPYRUS3)
theErr = Papy3FSeek (gPapyFile [theFileNb], SEEK_SET, *gRefImagePointer [theFileNb]);
/* extract the informations from group28 from the file */
if ((theErr = ExtractGroup28Information (theFileNb)) < 0)
{
iResult = theErr;
}
/* extract data set information for the DICOM files */
if ((gIsPapyFile [theFileNb] == DICOM10 || gIsPapyFile [theFileNb] == DICOM_NOT10) && (iResult == papNoError))
{
if ((theErr = ExtractDicomDataSetInformation3 (theFileNb)) < 0)
{
iResult = theErr;
}
} /* if */
if (iResult == papNoError)
{
/* reset the file pointer to its previous position */
theErr = Papy3FSeek (gPapyFile [theFileNb], SEEK_SET, theFilePos);
iResult=theFileNb;
}
} /* if ...no error 'til yet... */
} /* else ... not a DICOMDIR file */
} /* if ...no error 'til yet... */
} /* if ...no error 'til yet... */
} /* else ...no error reading the DICM string from the file */
} /* else ...no error positioning the file pointer to the place where the DICM string is */
} /* found a free file number for the file */
/* if error */
if (iResult < 0)
{
gPapyFile [theFileNb] = 0;
} /* if */
} /* if ...no error 'til yet... */
} /* else ...no error reading the file looking for the PAPYRUS 3.X str */
} /* else ...no error setting the file pointer at the begining */
/* if error */
if (iResult < 0)
{
/* if file was open */
if (inToOpen)
{
Papy3FClose (&theFp);
} /* if */
} /* if ...no error 'til yet... */
} /* if ...no error 'til yet... */
return iResult;
} /* endof Papy3FileOpen */
/********************************************************************************/
/* */
/* Papy3FileCreate : given a filename check if this file does not exist and*/
/* creates a new file. It has to put the file Meta Info as well as the */
/* DICM prefix to identify the file. It has to initialize the variables */
/* necessary to store the different offsets. It has to create the file */
/* structure in memory (list). */
/* return : a reference number to the opened file if successful */
/* standard error message otherwise */
/* */
/********************************************************************************/
PapyShort CALLINGCONV
Papy3FileCreate (char *inNameP, PAPY_FILE inVRefNum, PapyUShort inNbImages,
enum ETransf_Syntax inSyntax, enum EPap_Compression inCompression,
enum EModality inModality, int inToCreate, int inIsPapyrus, void* inFSSpec)
{
PapyULong theNumberOfBytes, theLengthOfFilename;
int i;
PapyShort theFileNb, theErr;
PAPY_FILE theFp;
char theBuff [134];
Object *theObjectP;
Item *theItemP;
SElement *theGr41P;
/*void *theFSSpecP;*/
/* -------- validity tests -------- */
/* we have to have an image */
if (inNbImages == 0) RETURN (papNbImagesIsZero);
/* no valid filename specified */
if (inToCreate && (inNameP == NULL || *inNameP == '\0')) RETURN (papFileName);
/* Is the choosen syntax implemented ? */
if (!((inSyntax == LITTLE_ENDIAN_IMPL && inCompression == NONE) ||
(inSyntax == LITTLE_ENDIAN_EXPL &&
(inCompression == NONE || inCompression == JPEG_LOSSLESS || inCompression == JPEG_LOSSY
#ifdef MAYO_WAVE
|| inCompression == MAYO_WAVELET
#endif
))))
RETURN (papSyntaxNotImplemented);
/* Is the modality known in DICOM ? */
if (inModality != CR_IM && inModality != CT_IM && inModality != MR_IM &&
inModality != NM_IM && inModality != US_IM && inModality != US_MF_IM &&
inModality != SEC_CAPT_IM && inModality != PX_IM && inModality != DX_IM &&
inModality != MG_IM && inModality != IO_IM && inModality != RF_IM &&
inModality != PET_IM && inModality != VLE_IM && inModality != VLM_IM &&
inModality != VLS_IM && inModality != VLP_IM && inModality != MFSBSC_IM &&
inModality != MFGBSC_IM && inModality != MFGWSC_IM && inModality != MFTCSC_IM)
RETURN (papUnknownModality);
/* -------- creating and opening the file -------- */
/* look for a valid file number */
theFileNb = FindFreeFile3 ();
/* test wether it will be a PAPYRUS file or a set of DICOM files */
if (inIsPapyrus == PAPYRUS3) /* it is a PAPYRUS file, not a basic DICOM file */
gIsPapyFile [theFileNb] = PAPYRUS3;
else /* it will be a set of DICOM files */
gIsPapyFile [theFileNb] = DICOM10;
/* too many open files */
if (theFileNb < 0) RETURN (theFileNb);
if (inToCreate && gIsPapyFile [theFileNb] == PAPYRUS3)
{
/*if (inFSSpec)
theFSSpecP = &inFSSpec;
else
theFSSpecP = NULL;*/
if ((theErr = Papy3FCreate (inNameP, inVRefNum, &theFp, &inFSSpec)) != 0)
RETURN (papFileAlreadyExist);
if ((theErr = Papy3FOpen (inNameP, 'w', inVRefNum, &theFp, &inFSSpec)) != 0)
RETURN (papFileCreationFailed);
/*if (theFSSpecP != NULL) efree3 ((void **) &theFSSpecP);*/
} /* if ...inToCreate */
/* give it the file reference number */
else theFp = inVRefNum;
/* assign the file to the array of files */
gPapyFile [theFileNb] = theFp;
/* -------- file meta information -------- */
/* if it is a PAPYRUS file */
if (gIsPapyFile [theFileNb] == PAPYRUS3)
{
/* Put the DICOM File Meta Information in the file */
/* first put a PAPYRUS 3.X string at the begining of the file */
strcpy (theBuff, "PAPYRUS ");
strcat (theBuff, gPapyrusVersion);
strcat (theBuff, " ");
/* set a different compatibility flag depending on the syntax used */
if (inSyntax == LITTLE_ENDIAN_EXPL)
strcat (theBuff, gPapyrusCompatibility);
else strcat (theBuff, "1");
/* then put 128 bytes set to 0 */
for (i = 14; i < 128; i++) theBuff [i] = 0;
theNumberOfBytes = 128L;
/* writes the bytes to the file */
if (Papy3FWrite (theFp, (PapyULong *) &theNumberOfBytes, 1, theBuff) < 0)
{
theErr = Papy3FClose (&theFp);
RETURN (papWriteFile)
} /* if */
/* then put the "DICM" string that will identify the file as a DICOM one */
strcpy (theBuff, "DICM");
theNumberOfBytes = (PapyULong) 4L;
/* writes the bytes to the file */
if (Papy3FWrite (theFp, (PapyULong *) &theNumberOfBytes, 1, theBuff) < 0)
{
theErr = Papy3FClose (&theFp);
RETURN (papWriteFile)
} /* if */
} /* if ...PAPYRUS file */
/* creation of the file meta information and initialization of the memory */
/* representation of the file structure (list) */
if ((theErr = CreateFileMetaInformation3 (theFileNb, inCompression, inSyntax, inModality)) < 0) RETURN (theErr);
/* -------- creation of the basic objects -------- */
/* add a blank object for the Patient Summary to the file representation */
theObjectP = (Object *) emalloc3 ((PapyULong) sizeof (Object));
theObjectP->whoAmI = papItem;
theObjectP->item = NULL;
theObjectP->module = NULL;
theObjectP->group = NULL;
theObjectP->tmpFileLength = 0L;
theItemP = InsertLastInList (&(gArrMemFile [theFileNb]), theObjectP);
/* create a new Object to store group 41 */
theObjectP = (Object *) emalloc3 ((PapyULong) sizeof (Object));
theObjectP->whoAmI = papGroup;
theObjectP->item = NULL;
theObjectP->module = NULL;
theObjectP->tmpFileLength = 0L;
/* creation of the group 41 (Papyrus Data Element) */
theGr41P = Papy3GroupCreate (Group41);
/* store the group41 in the Object */
theObjectP->group = theGr41P;
theObjectP->objID = Group41;
/* insert the Object at the end of the list representing the file */
theItemP = InsertLastInList (&(gArrMemFile [theFileNb]), theObjectP);
/* put the number of images in the group 41 */
Papy3PutElement (theGr41P, papNumberofimagesGr, &inNbImages);
/* -------- initializations a la Papyrus 2 -------- */
/* nb of images in this file */
gArrNbImages [theFileNb] = inNbImages;
/* stores the name of the file (will be used to create the tmp files */
theLengthOfFilename = (PapyULong) strlen (inNameP);
gPapFilename [theFileNb] = (char *) emalloc3 ((PapyULong) theLengthOfFilename + 1L);
strcpy (gPapFilename [theFileNb], inNameP);
/* allocates room for the icons only if Papyrus compressed file */
if (inSyntax == LITTLE_ENDIAN_EXPL && (inCompression == JPEG_LOSSLESS ||
inCompression == JPEG_LOSSY
#ifdef MAYO_WAVE
|| inCompression == MAYO_WAVELET
#endif
))
{
gArrIcons [theFileNb] = (PapyUChar **) ecalloc3 ((PapyULong) inNbImages,
(PapyULong) sizeof (PapyUChar *));
/* initializes the icon pointers to NULL */
for (i = 0; i < inNbImages; i++)
gArrIcons [theFileNb] [i] = NULL;
} /* if ...compressed file */
/* modality of the file */
gFileModality [theFileNb] = inModality;
/* set the transfert syntax and the compression used for the file */
gArrTransfSyntax [theFileNb] = inSyntax;
gArrCompression [theFileNb] = inCompression;
/*if (compression == JPEG_LOSSLESS)
gArrCompression [theFileNb] = JPEG_LOSSLESS;
else
gArrCompression [theFileNb] = JPEG_LOSSY;*/
/* allocate room for the offsets to the data set and pixel data of the file */
gRefImagePointer [theFileNb] = (PapyULong *) ecalloc3 ((PapyULong) inNbImages,
(PapyULong) sizeof (PapyULong));
gPosImagePointer [theFileNb] = (PapyULong *) ecalloc3 ((PapyULong) inNbImages,
(PapyULong) sizeof (PapyULong));
gRefPixelOffset [theFileNb] = (PapyULong *) ecalloc3 ((PapyULong) inNbImages,
(PapyULong) sizeof (PapyULong));
gPosPixelOffset [theFileNb] = (PapyULong *) ecalloc3 ((PapyULong) inNbImages,
(PapyULong) sizeof (PapyULong));
/* the file is in write mode */
gReadOrWrite [theFileNb] = kPAPY_WRITE;
RETURN (theFileNb);
} /* endof Papy3FileCreate */
/********************************************************************************/
/* */
/* write_pos3 : writes from the buffer to the file at the specified pos. */
/* This function is used when writting the backward references to the file */
/* return : standard error message */
/* */
/********************************************************************************/
PapyShort
write_pos3 (PAPY_FILE inFp, PapyULong inPos, unsigned char **ioBuffP, PapyShort inLength)
/*PapyShort inFp; the file pointer */
/*PapyULong inPos; the position to write from */
/*unsigned char **ioBuffP; the buffer to write from */
/*PapyShort inLength; the length of the element to write */
{
int theErr;
PapyULong theTmpULong;
if (Papy3FSeek (inFp, (int) SEEK_SET, (PapyLong) inPos))
{
theErr = Papy3FClose (&inFp);
efree3 ((void **) ioBuffP);
RETURN (papPositioning)
} /* if */
theTmpULong = inLength;
if (Papy3FWrite (inFp, (PapyULong *) &theTmpULong, 1L, *ioBuffP) < 0)
{
theErr = Papy3FClose (&inFp);
efree3 ((void **) ioBuffP);
RETURN (papWriteFile)
} /* if */
efree3 ((void **) ioBuffP);
RETURN (papNoError);
} /* endof write_pos3 */
/********************************************************************************/
/* */
/* Papy3FileClose : Free the memory used by this file and destroys the in */
/* memory structure representing the file. */
/* return : standard error message */
/* */
/********************************************************************************/
PapyShort CALLINGCONV
Papy3FileClose (PapyShort inFileNb, int inToClose)
{
PapyShort theErr;
int i;
/* close the Papyrus file */
if (inToClose)
if (Papy3FClose (&(gPapyFile [inFileNb])) != 0) RETURN (papCLOSE_FILE);
/* delete the in memory file representation */
if ((theErr = DeleteList (inFileNb, &(gArrMemFile [inFileNb]), TRUE, TRUE, TRUE)) < 0)
RETURN (theErr);
/* deletes the pointer sequence */
if ((theErr = DeleteList (inFileNb, &(gPtrSequenceItem [inFileNb]), TRUE, TRUE, TRUE)) < 0)
RETURN (theErr);
if ((theErr = DeleteList (inFileNb, &(gImageSequenceItem [inFileNb]), TRUE, TRUE, TRUE)) < 0)
RETURN (theErr);
if (gArrGroup41 [inFileNb] != NULL)
theErr = Papy3GroupFree (&(gArrGroup41 [inFileNb]), TRUE);
/* frees the allocated memory for the file */
if (gPapFilename [inFileNb] != NULL)
efree3 ((void **) &(gPapFilename [inFileNb]));
/* frees the allocated memory for the file */
if (gShadowOwner [inFileNb] != NULL)
efree3 ((void **) &(gShadowOwner [inFileNb]));
if (gArrIcons [inFileNb] != NULL)
efree3 ((void **) &(gArrIcons [inFileNb]));
if (gRefSOPClassUID [inFileNb] != NULL)
efree3 ((void **) &(gRefSOPClassUID [inFileNb]));
if (gRefImagePointer [inFileNb] != NULL)
efree3 ((void **) &(gRefImagePointer [inFileNb]));
if (gPosImagePointer [inFileNb] != NULL)
efree3 ((void **) &(gPosImagePointer [inFileNb]));
if (gRefPixelOffset [inFileNb] != NULL)
efree3 ((void **) &(gRefPixelOffset [inFileNb]));
if (gPosPixelOffset [inFileNb] != NULL)
efree3 ((void **) &(gPosPixelOffset [inFileNb]));
if (gImageSOPinstUID [inFileNb] != NULL)
{
for (i = 0; i < gArrNbImages [inFileNb]; i++)
{
if (*(gImageSOPinstUID [inFileNb] + i) != NULL)
efree3 ((void **) (gImageSOPinstUID [inFileNb] + i));
} /* for */
efree3 ((void **) &(gImageSOPinstUID [inFileNb]));
} /* if */
gx0028BitsAllocated [inFileNb] = 0;
gPatientSummaryItem [inFileNb] = NULL;
gPapyFile [inFileNb] = 0;
/* reset the incremental number for the file to zero */
gCurrTmpFilename [inFileNb] = 1;
RETURN (theErr);
} /* endof Papy3FileClose */
/********************************************************************************/
/* */
/* Papy3WriteAndCloseFile : writes the whole in memory structure of the */
/* given file to the disk. It closes any unclosed data set and saves the */
/* references to the data sets and the pixel data. Finally frees some */
/* memory that is no more needed. */
/* return : standard error message */
/* */
/********************************************************************************/
PapyShort CALLINGCONV
Papy3WriteAndCloseFile (PapyShort inFileNb, int inToClose)
{
unsigned char *theBuffP;
PapyShort theErr, i;
PapyULong theBufPos;
Item *theFileStructP, *theSeqP, *theDataSetP, *theWrkItemP;
/* Do the following tasks only if we are writing a PAPYRUS file */
if (gIsPapyFile [inFileNb] == PAPYRUS3)
{
/* write group 2 (File Meta Information) and free it */
if ((theErr = Papy3GroupWrite (inFileNb, (gArrMemFile [inFileNb])->object->group, FALSE)) < 0)
RETURN (theErr);
/* convert the Patient summaries modules to groups */
theFileStructP = gArrMemFile [inFileNb]->next;
if ((theErr = ItemModulesToGroups3 (inFileNb, theFileStructP, TRUE)) < 0)
RETURN (theErr);
/* write the groups of the patient summary to the Papyrus file */
theWrkItemP = (Item *) theFileStructP->object->item;
while (theWrkItemP != NULL)
{
/* if gr 41 do not write it as the only element will be in THE gr 41 */
if (theWrkItemP->object->group->group != 0x0041)
{
/* write the current group to the file and then frees the allocated memory */
if ((theErr = Papy3GroupWrite (inFileNb, theWrkItemP->object->group, FALSE)) < 0)
RETURN (theErr);
}
/* get next element of the list */
theWrkItemP = theWrkItemP->next;
} /* while ...loop on the groups of the patient summary */
/* makes sure that all the data sets have been converted to temporary files */
/* if not, converts them and frees the modules of the data set */
theDataSetP = gImageSequenceItem [inFileNb];
while (theDataSetP != NULL)
{
if (theDataSetP->object->whoAmI != papTmpFile)
if ((theErr = Papy3CloseDataSet (inFileNb, theDataSetP, TRUE, TRUE)) < 0)
RETURN (theErr);
/* get next data set of the list */
theDataSetP = theDataSetP->next;
} /* while ...makes sure all data sets have been converted to tmp files */
/* convert the items modules of the pointer sequence to groups */
theSeqP = gPtrSequenceItem [inFileNb];
while (theSeqP != NULL)
{
if ((theErr = ItemModulesToGroups3 (inFileNb, theSeqP, TRUE)) < 0)
RETURN (theErr);
/* get the next item of the pointer sequence */
theSeqP = theSeqP->next;
} /* while ...loop on the items of the pointer sequence */
/* points to group 41 in the in memory file structure */
theFileStructP = theFileStructP->next;
/* write the group 41 */
if ((theErr = Papy3GroupWrite (inFileNb, theFileStructP->object->group, FALSE)) < 0)
RETURN (theErr);
/* writes the saved references to the file */
/* loop on the images of the file */
for (i = 0; i < gArrNbImages [inFileNb]; i++)
{
/* put the offset to the data set in the buffer */
theBuffP = (unsigned char *) emalloc3 ((PapyULong) sizeof (PapyULong));
theBufPos = 0;
Put4Bytes (*(gRefImagePointer [inFileNb] + i), theBuffP, &theBufPos);
/* write the offset from the begining of the file until the data set */
if ((theErr = write_pos3 (gPapyFile [inFileNb], *(gPosImagePointer [inFileNb] + i), &theBuffP, 4)) < 0)
RETURN (theErr);
efree3 ((void **) &theBuffP);
/* put the offset to the pixel datas in the buffer */
theBuffP = (unsigned char *) emalloc3 ((PapyULong) sizeof (PapyULong));
theBufPos = 0L;
Put4Bytes (*(gRefPixelOffset [inFileNb] + i), theBuffP, &theBufPos);
/* write the offset from the begining of the file until the data set */
if ((theErr = write_pos3 (gPapyFile [inFileNb], *(gPosPixelOffset [inFileNb] + i), &theBuffP, 4)) < 0)
RETURN (theErr);
efree3 ((void **) &theBuffP);
} /* for ...writing the references to the data sets and the pixel datas */
} /* if ...PAPYRUS file */
/* frees the allocated memory */
/* if DICOM file, inFileNb has never existed */
if (gIsPapyFile [inFileNb] == DICOM10)
theErr = Papy3FileClose (inFileNb, FALSE);
else
theErr = Papy3FileClose (inFileNb, inToClose);
RETURN (theErr);
} /* endof Papy3WriteAndCloseFile */
/********************************************************************************/
/* */
/* ReadGroup3 : reads the next group from the file and put it in a buffer.*/
/* return : standard error message */
/* */
/********************************************************************************/
PapyShort
ReadGroup3 (PapyShort inFileNb, PapyUShort *outGroupNbP, unsigned char **outBuffP,
PapyULong *outBytesReadP, PapyULong *outGroupLengthP)
{
PapyULong theStartPos, theCurrPos, theReadLength, theBufPos;
PapyULong theGrLength, theElemLength, theFirstElemLength, theTempL, i;
PapyUShort theElemNb, theTemplGr2, theElemCreator, theElemNb2;
int theErr;
PAPY_FILE theFp;
theFp = gPapyFile [inFileNb];
theFirstElemLength = (PapyULong) kLength_length; /* 12 */
/* allocate a buffer to read until group length */
*outBuffP = (unsigned char *) emalloc3 (theFirstElemLength);
*outBytesReadP = theFirstElemLength;
*outGroupLengthP = 0L;
/* read the file until the group length value */
theTempL = theFirstElemLength;
if ((theErr = (PapyShort) Papy3FRead (theFp, &theTempL, 1L, *outBuffP)) < 0)
{
theErr = Papy3FClose (&theFp);
efree3 ((void **) outBuffP);
RETURN (papReadFile)
} /* if */
i = 0L; /* position in the read buffer */
*outGroupNbP = Extract2Bytes (*outBuffP, &i); /* group number */
theElemNb = Extract2Bytes (*outBuffP, &i); /* element number */
/* check the transfert syntax */
if (gArrTransfSyntax [inFileNb] == LITTLE_ENDIAN_EXPL || *outGroupNbP == 0x0002)
{
i += 2; /* moves 2 bytes forward */
theTemplGr2 = Extract2Bytes (*outBuffP, &i);
theTempL = (PapyULong) theTemplGr2; /* element length */
} /* if ...EXPLICIT VR */
/* IMPLICIT VR */
else theTempL = Extract4Bytes (*outBuffP, &i); /* element length */
/* length of the group element is present */
/* or DICOMDIR, so compute it */
if (theElemNb == 0) /* && *outGroupNbP != 0x0004)*/
theGrLength = Extract4Bytes (*outBuffP, &i);
/* group with no length set, so compute it */
else
{
theErr = Papy3FSeek (theFp, (int) SEEK_CUR, - (long) theFirstElemLength);
if (*outGroupNbP != 0x7FE0)
theGrLength = ComputeUndefinedGroupLength3 (inFileNb, -1L);
else
{
if (gArrTransfSyntax [inFileNb] == LITTLE_ENDIAN_EXPL) theGrLength = 12L;
else theGrLength = 8L;
} /* else ...group 0x7FE0 */