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 pathPapyDataSetWrite3.c
2310 lines (1857 loc) · 83.5 KB
/
PapyDataSetWrite3.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 : 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];