File: deivr.cxx

package info (click to toggle)
conquest-dicom-server 1.4.17d-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 16,332 kB
  • ctags: 11,315
  • sloc: cpp: 56,176; ansic: 52,870; sh: 14,403; asm: 284; makefile: 282
file content (1322 lines) | stat: -rw-r--r-- 32,422 bytes parent folder | download
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
/*
19990318    ljz    Added 'DICOMObject::DeleteVR'
20001106    mvh    Used delete [] operator for vr->Data
20010426    ljz    Fixed small leaks in 'DICOMObject::DeleteVR'
20010730    ljz    Added 'DICOMObject::ReplaceVR'. Is able to replace VRs inside
                   sequences.
20010802    ljz    Added 'VR::GetSpecialValueCodeOfVR'
20011121    ljz    Added VR_SPECIAL_RGB_TO_MONO. Used by a conversion-tool to change
                   ultrasound Kretz RGB images to monochrome that VariSeed (brachytherapy)
                   understands.
20011121    ljz    Added VR_SPECIAL_RGB_TO_MONO_PLUS. Same as above, but tries to preserve
                   colored drawings.
20051217    mvh    Added 'VR::ReAlloc'
20051217    mvh    Use malloc/free instead of new char[] and delete []
20051229    mvh    Removed unnecessary reset() in DeleteVR
20071031    mvh    Temp printf memory error messages
20071102    mvh    Send allocation errors to DicomError
20080313    ljz    Removed some warnings
20080910    bcb    Added VR::VR(UINT16 g, UINT16 e, UINT32 l, LE_UINT16	*d) &
                   VR::VR(UINT16 g, UINT16 e, UINT32 l, LE_UINT16 *d, BOOL mFlag)
                   for big endian.
20081016    mvh    Fix for WC: new of 0 gives NULL (check Length in error message)
20090620    jf     Added <stdlib.h>
20091230    mvh    Merged bcb bigendian change
20100110    mvh    And const change
20100309    bcb    Added double parentheses (gcc4.2 Warnings)
20100619    bcb    Fix gcc4 warnings and improve speed and added questions.
20100717    mvh    Merged except keep default ReleaseMemory to TRUE and check data when adjusitn length
20100728    bcb    Removed unused AutoMakeDO, added Get's to VR and DICOMObject.
                   Added ChangeVR's.  Fixed LastVR & LastVRG?
20100815    mvh    Merged; fix VR constructors for failed malloc; added max parameter to GetCstring function
                   Moved defaults from implementation to header files
20100826    mvh    bcb moved another default, size_t
20100901    mvh    bcb changed alloc of US to 2
20110231    mvh    DicomError returns FALSE when handler not installed - to allow some errors to pass
*/


/****************************************************************************
          Copyright (C) 1995, University of California, Davis

          THIS SOFTWARE IS MADE AVAILABLE, AS IS, AND THE UNIVERSITY
          OF CALIFORNIA DOES NOT MAKE ANY WARRANTY ABOUT THE SOFTWARE, ITS
          PERFORMANCE, ITS MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR
          USE, FREEDOM FROM ANY COMPUTER DISEASES OR ITS CONFORMITY TO ANY
          SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND PERFORMANCE OF
          THE SOFTWARE IS WITH THE USER.

          Copyright of the software and supporting documentation is
          owned by the University of California, and free access
          is hereby granted as a license to use this software, copy this
          software and prepare derivative works based upon this software.
          However, any distribution of this software source code or
          supporting documentation or derivative works (source code and
          supporting documentation) must include this copyright notice.
****************************************************************************/

/***************************************************************************
 *
 * University of California, Davis
 * UCDMC DICOM Network Transport Libraries
 * Version 0.1 Beta
 *
 * Technical Contact: mhoskin@ucdavis.edu
 *
 ***************************************************************************/

#	include	"dicom.hpp"
#       include <stdlib.h> //DUCKHEAD92

/********************************
 *
 * Error handler
 *
 ********************************/

DeepErrorCall DicomErrorHandler = NULL;

void SetDicomErrorHandler(DeepErrorCall handler)
	{ 
	DicomErrorHandler = handler;
	}

BOOL DicomError(int error, const char *message, int info)
	{ 
	if (DicomErrorHandler)
		{
		DicomErrorHandler(error, message, info);
		return TRUE;
		}
	else
		{
		printf("DICOM ERROR ");
		printf(message, info);
		return FALSE;
		}
	}

/********************************
 *
 * Base VR Type
 *
 ********************************/
VR	::	VR(UINT16 g, UINT16 e, UINT32 l, BOOL	Alloc)
#ifdef __GNUC__ //Faster with member initialization.
:Group(g), Element(e), Length(l), Data(NULL), ReleaseMemory(TRUE),
Valid(FALSE), SQObjectArray(NULL), ReferentialIntegrityDatabase(), TypeCode(0)
{
	BOOL	cLast = FALSE;

	if ( Length & 0x01 )
	{
		++Length;
		cLast = TRUE;
	}
	
	if(Alloc && Length)
	{
		Data = malloc(Length);
		if (!Data) DicomError(DCM_ERROR_MEMORY, "VR: out of memory allocating %d bytes\n", Length);
		if ( Data && cLast ) ((BYTE*)Data)[Length-1] = '\0';
	}
}
#else
{
	BYTE	*bptr = NULL;
	BOOL	cLast = FALSE;

	if ( l & 0x01 )
		{
		++l;
		cLast = TRUE;
		}


	Group = g;
	Element = e;
	Length = l;
	if(Alloc && Length)
		{
		Data = malloc(Length);
		if (!Data) DicomError(DCM_ERROR_MEMORY, "VR: out of memory allocating %d bytes\n", Length);
		if ( Data && cLast ) ((BYTE*)Data)[Length-1] = '\0';
		}
	else
		Data = NULL;

	ReleaseMemory = TRUE;
	SQObjectArray = NULL;
	TypeCode = 0;
	Valid = FALSE;
	}
#endif

VR	::	VR(UINT16 g, UINT16 e, UINT32 l, LE_UINT16	*d)
#ifdef __GNUC__ //Faster with member initialization.
:Group(g), Element(e), Length(l), Data(d), ReleaseMemory(TRUE),
Valid(FALSE), SQObjectArray(NULL), ReferentialIntegrityDatabase(), TypeCode(0)
{
#if NATIVE_ENDIAN == BIG_ENDIAN //Big Endian like Apple power pc
	*d = SwitchEndian((UINT16)*d);
#endif //BigEndian
}
#else
{
	Group = g;
	Element = e;
	Length = l;
	Data = d;
#if NATIVE_ENDIAN == BIG_ENDIAN //Big Endian like Apple power pc
	*d = SwitchEndian((UINT16)*d);
#endif //BigEndian
	ReleaseMemory = TRUE;
	SQObjectArray = NULL;
	TypeCode = 0;
	Valid = FALSE;
}
#endif

VR	::	VR(UINT16 g, UINT16 e, UINT32 l, LE_UINT16	*d, BOOL	mFlag)
#ifdef __GNUC__ //Faster with member initialization.
:Group(g), Element(e), Length(l), Data(d), ReleaseMemory(mFlag),
Valid(FALSE), SQObjectArray(NULL), ReferentialIntegrityDatabase(), TypeCode(0)
{
#if NATIVE_ENDIAN == BIG_ENDIAN //Big Endian like Apple power pc
	*d = SwitchEndian((UINT16)*d);
#endif //BigEndian
}
#else
{
	Group = g;
	Element = e;
	Length = l;
	Data = d;
#if NATIVE_ENDIAN == BIG_ENDIAN //Big Endian like Apple power pc
	*d = SwitchEndian((UINT16)*d);
#endif //BigEndian
	ReleaseMemory = mFlag;
	SQObjectArray = NULL;
	TypeCode = 0;
	Valid = FALSE;
}
#endif

VR	::	VR(UINT16 g, UINT16 e, UINT32 l, void	*d)
#ifdef __GNUC__ //Faster with member initialization.
:Group(g), Element(e), Length(l), Data(d), ReleaseMemory(TRUE),
Valid(FALSE), SQObjectArray(NULL), ReferentialIntegrityDatabase(), TypeCode(0)
{
}
#else
{

	BYTE	*bptr = NULL;
	BOOL	cLast = FALSE;

/*	if ( l & 0x01 )
		{
		++l;
		cLast = TRUE;
		}*/

	Group = g;
	Element = e;
	Length = l;
//	if(Length&1)
//		++Length;
	Data = d;
	if ( cLast )
		{
		bptr = (BYTE*)Data;
		bptr[Length-1] = '\0';
		}
	ReleaseMemory = TRUE;
	SQObjectArray = NULL;
	TypeCode = 0;
	Valid = FALSE;
}
#endif

VR	::	VR(UINT16 g, UINT16 e, UINT32 l, void	*d, BOOL	mFlag)
#ifdef __GNUC__ //Faster with member initialization.
:Group(g), Element(e), Length(l), Data(d), ReleaseMemory(mFlag),
Valid(FALSE), SQObjectArray(NULL), ReferentialIntegrityDatabase(), TypeCode(0)
{
}
#else
{
	Group = g;
	Element = e;
	Length = l;
	Data = d;
	ReleaseMemory = mFlag;
	SQObjectArray = NULL;
	TypeCode = 0;
	Valid = FALSE;
}
#endif
VR	::	~VR()
{
	Array<DICOMObject	*>	*ArrayDO;
/*
	printf("~VR()\n\tGroup = %4.4x Element = %4.4x Data = %x Length = %d\n",
		Group, Element, Data, Length);
	printf("\tReleasememory = %d, SQObjectArray = %x, RID = %x Valid = %d\n",
		ReleaseMemory, SQObjectArray, ReferentialIntegrityDatabase, Valid);
	fflush(stdout);
*/
	if(ReleaseMemory)
		if(Data)
//			delete [] Data;
			free(Data);
	if(SQObjectArray)
	{
		ArrayDO = (Array<DICOMObject *> *) SQObjectArray;
		while(ArrayDO->GetSize())
		{
			delete ArrayDO->Get(0);
			ArrayDO->RemoveAt(0);
		}
		delete ArrayDO;
		SQObjectArray = NULL;
	}		
}

UINT	VR	::	operator	>	(VR	&vr)
{
	if(Group > vr.Group)
		return ( 1 );
	if(Group == vr.Group)
	{
		if(Element > vr.Element)
			return ( 1 );
	}
	return ( 0 );
}

UINT	VR	::	operator	<	(VR	&vr)
{
	if(Group < vr.Group)
		return ( 1 );
	if(Group == vr.Group)
	{
		if(Element < vr.Element)
			return ( 1 );
	}
	return ( 0 );
}

UINT	VR	::	operator	==	(VR	&vr)
{
	if(Group == vr.Group)
	if(Element == vr.Element)
			return ( 1 );
	return ( 0 );
}

/*****************************************************
 *
 * VR Group P-Queues
 *
 *****************************************************/

BOOL	VRGroupPQueue	::	Push(VR	*vr)
{
//	printf("[VRGroupPQueue : Push(%x)]\n", vr);
	if(vr->Group!=Group)
		return ( FALSE );
	Length += vr->Length + sizeof(UINT16) + sizeof(UINT16) + sizeof(UINT32);
	if(vr->Length&0x01)
		++Length;	// Transfer Syntax Encodeder will +1 here
	PQueueOfPtr<VR *> :: Push ( vr );
	return ( TRUE );
}

VR	*	VRGroupPQueue	::	Pop()
{
	VR	*tv;

//	printf("[VRGroupPQueue : Pop()]\n");
	if ( !PQueueOfPtr<VR *> :: GetSize())
		return ( (VR *) NULL );
	tv = PQueueOfPtr<VR *> :: Pop ();
	if(!tv)
		return ( tv );
	Length -= (tv->Length + sizeof(UINT16) + sizeof(UINT16) + sizeof(UINT32));
	if(tv->Length&0x01)
		--Length;	// See Push
	return ( tv );
}

VR	*	VRGroupPQueue	::	GroupLength()
{
	LE_UINT32	*puint32;

	Element0 = new VR ( Group, 0x0000, 4, TRUE);
	if(!Element0)
		return ( NULL );
	puint32 = (LE_UINT32 *) Element0->Data;
#if NATIVE_ENDIAN == LITTLE_ENDIAN //Little Endian
	(*puint32) = (LE_UINT32) Length;// + sizeof(UINT16) + sizeof(UINT16) + sizeof(UINT32) + sizeof(UINT32);
#else //Big Endian like Apple power pc
	(*puint32) = (LE_UINT32)SwitchEndian((UINT32) Length);// + sizeof(UINT16) + sizeof(UINT16) + sizeof(UINT32) + sizeof(UINT32);
#endif
	Element0->TypeCode = 'UL';
	return ( Element0 );
}

UINT	VRGroupPQueue	::	operator	>	(VRGroupPQueue	&vrgroup)
{
	if(Group > vrgroup.Group)
		return ( 1 );
	return ( 0 );
}

UINT	VRGroupPQueue	::	operator	<	(VRGroupPQueue	&vrgroup)
{
	if(Group < vrgroup.Group)
		return ( 1 );
	return ( 0 );
}

UINT	VRGroupPQueue	::	operator	==	(VRGroupPQueue	&vrgroup)
{
	if(Group == vrgroup.Group)
		return ( 1 );
	return ( 0 );
}

/*****************************************************
 *
 * DICOM Objects
 *
 *****************************************************/

DICOMObject	::	DICOMObject()
#ifdef __GNUC__ //Faster with member initialization.
:CurrentGroup(), LastVRG(NULL), LastVR(NULL), LastGroup(0), LastElement(0),
Length(0), Packaged(FALSE), VRGroupPQueues(),SQObjectArray(NULL),
FreeSQObjectSize(0), FixedSize(0), UseFixedSize(FALSE)
{
	VRGroupPQueues.ClearType = FALSE;
}
#else
{
	Packaged = FALSE;
	Length = 0;
	VRGroupPQueues.ClearType = FALSE;
	SQObjectArray = NULL;
	LastVR = NULL;
	LastVRG = NULL;
	FreeSQObjectSize = 0;
	FixedSize = 0;
	UseFixedSize = FALSE;
}
#endif
	
DICOMObject	::	~DICOMObject()
{
	VR	*VRPtr;

	if ( !Packaged)
		if ( !Package() )
			return;

	if ( Packaged )
	{
		while (( VRPtr = this->Pop()))
			delete VRPtr;
		return;
	}
	while ( VRGroupPQueues.GetSize() )
		VRGroupPQueues.RemoveAt(0);
}

//Only used in ParseRaw and unused Morph functions, LastVR & LastVRG should be valid at this point.
BOOL	DICOMObject	::	Push(DICOMObject	*SQDO)
{
	Array<DICOMObject *>	*ArrayPtr;

	if ( Packaged )
		return ( FALSE );

	if ( ! LastVR || !LastVRG )//bcb Both used, check both.
	{
		return ( FALSE );	// not-supported yet
		if (!SQObjectArray)
		{
			SQObjectArray = new Array<DICOMObject *>;
			if ( !SQObjectArray )
				return ( FALSE );
		}
		SQObjectArray->Add ( SQDO );
		FreeSQObjectSize += SQDO->Length;
		return ( TRUE );
	}
	if (!LastVR->SQObjectArray)
	{
		ArrayPtr = new Array<DICOMObject *>;
		if (!ArrayPtr)
			return ( FALSE );
		LastVR->SQObjectArray = (void *) ArrayPtr;
		// "new sequence"
		LastVRG->Length += (2 * (sizeof(UINT16) + sizeof(UINT16) +
						sizeof(UINT32) ));
		Length += (2 * (sizeof(UINT16) + sizeof(UINT16) +
						sizeof(UINT32) ));
	}
	ArrayPtr = (Array<DICOMObject *> *) LastVR->SQObjectArray;
	ArrayPtr->Add ( SQDO );	
	LastVRG->Length += SQDO->Length + 
			(2 * (sizeof(UINT16) + sizeof(UINT16) + sizeof(UINT32)));
	Length += SQDO->Length + 
			(2 * (sizeof(UINT16) + sizeof(UINT16) + sizeof(UINT32)));
	return ( TRUE );
}

BOOL	DICOMObject	::	Push(VR	*vr)
{
	UINT			Index;
 	VRGroupPQueue		*VRGroupPQueuePtr;

//	printf("[Push]\n");
	if ( Packaged )
		return ( FALSE );	// already Pop() from this object

	if(vr->Length == 0xffffffff)
	{
//		fprintf(stdout, "Attemping to push -1 length\n");
		vr->Length = 0;
	}

	Index = 0; 
	if(vr->Element == 0x0000)
	{
//		fprintf(stderr, "Attemping to push GLC: %d\n",
//			(*((UINT32 *) vr->Data)));
		delete vr;
		return ( TRUE );	// ignore Group Length Codes (auto calculated)
	}
	LastVR = vr;

	if(LastVRG)
		if(LastVRG->Group == vr->Group)	// Faster, no need to search again
		{
			Length += vr->Length + sizeof(UINT16) + sizeof(UINT16) +
						sizeof(UINT32);
			if(vr->Length&0x01)
				++Length;
			return ( LastVRG->Push ( vr ));
		}
	while ( Index < VRGroupPQueues.GetSize())
	{
		VRGroupPQueuePtr = VRGroupPQueues.Get(Index);
		if(!VRGroupPQueuePtr)
			return ( FALSE );
		if(VRGroupPQueuePtr->Group == vr->Group)
			{
			LastVRG = VRGroupPQueuePtr;	// If we do the same group again, faster.
			Length += vr->Length + sizeof(UINT16) + sizeof(UINT16) +
						sizeof(UINT32);
			if(vr->Length&0x01)
				++Length;
//			printf("[VRGroupPQueuePtr->Group : %d]\n", VRGroupPQueuePtr->Group);
			return ( VRGroupPQueuePtr->Push ( vr ));
		}
		++Index;
	}

	VRGroupPQueuePtr = new VRGroupPQueue(vr->Group);
	if(!VRGroupPQueuePtr)
		return ( FALSE );

	Length += sizeof(UINT16) + sizeof(UINT16) + sizeof(UINT32) +
			sizeof(UINT32);
	Length += vr->Length + sizeof(UINT16) + sizeof(UINT16) +
			sizeof(UINT32);
	if(vr->Length&0x01)
		++Length;
	LastVRG = VRGroupPQueuePtr;	
//	printf("[VRGroupPQueues.Push]\n");
	VRGroupPQueues.Add(VRGroupPQueuePtr);
	return ( VRGroupPQueuePtr->Push(vr) );
}

VR	*	DICOMObject	::	Pop()
{
	VR	*VRPtr;

	//return ( (VR *) NULL );

	if(!Packaged)
		if(!Package())
			return ( (VR *) NULL );
	if(!Packaged)
		return ( (VR *) NULL );

	if ( ! CurrentGroup )
	{
//		printf("[Pop : CurrentGroup = NULL]\n");
		if ( !PQueueOfPtr<VRGroupPQueue *>::GetSize())
		{
			Packaged = FALSE;
			LastVRG = NULL;
			LastVR = NULL;
			return ( (VR *) NULL );
		}
		CurrentGroup = PQueueOfPtr<VRGroupPQueue *>::Pop();
		if ( ! CurrentGroup )
			return ( (VR *) NULL );
		Length -= ( sizeof(UINT16) + sizeof(UINT16) + sizeof(UINT32) +
				sizeof(UINT32) );
	
		return ( CurrentGroup->GroupLength() );
	}
	if ( ! ( VRPtr = CurrentGroup->Pop() ) )
	{
		delete CurrentGroup;
		CurrentGroup = NULL;
		return ( this->Pop() );
	}
	Length -= (VRPtr->Length + sizeof(UINT16) + sizeof(UINT16) +
			sizeof(UINT32) );
	if(VRPtr->Length&0x01)
		--Length;	// See Push
	return ( VRPtr );
}

// this needs to be replaced with a more efficient algorithm in the
// future, but it will do for now.
VR		*DICOMObject	::	GetVR(UINT16	g, UINT16	e)
{
	VR				*vr;
	UINT			Index,Index2;
	VRGroupPQueue	*VRGroupPQueuePtr;

	Index = 0;

	while ( Index < VRGroupPQueues.GetSize())
	{
		VRGroupPQueuePtr = VRGroupPQueues.Get(Index);
		if(!VRGroupPQueuePtr)
			return ( FALSE );
		if(VRGroupPQueuePtr->Group == g)
		{
			Index2 = 0;
			while ( Index2 < VRGroupPQueuePtr->GetSize() )
			{
				vr = VRGroupPQueuePtr->Get(Index2);
				if(!vr)
					return ( 0 );
				if(vr->Element == e)
				{
					return ( vr );
				}
				++Index2;
			}
			return ( NULL );
		}
		++Index;
	}
	return ( NULL );
}

/* bcb replaced by byte order independent versions
LE_UINT16	DICOMObject	::	GetUINT16(UINT16	g, UINT16	e)
{
	VR		*vr;
	LE_UINT16	Val;

	vr = this->GetVR ( g, e );
	if(!vr)
		return(0);
		
	if ( vr->Data )
		if(vr->Length == sizeof(UINT16))
		{
			memcpy(&Val, vr->Data, 2);
#if NATIVE_ENDIAN == LITTLE_ENDIAN //Little Endian
			return ( Val );
#else //Big Endian like Apple power pc
			return (SwitchEndian((UINT16) Val ));
#endif //Big Endian
		}
	return ( 0 );
}*/

// Will return the first byte from LE data, independent of vr->Length
UINT8 DICOMObject	::	GetBYTE(UINT16	g, UINT16	e)
{
	VR		*vr;
	UINT8		value = 0;
	
	vr = this->GetVR ( g, e );
	if(!vr || !vr->Length) return(0);
	
	if(vr->Data) value = ((unsigned char *)vr->Data)[0];
	return(value);
}

// Will return a short from LE data, independent of vr->Length
UINT16 DICOMObject	::	GetUINT16(UINT16	g, UINT16	e)
{
	VR		*vr;
	UINT16	value = 0;
	
	vr = this->GetVR ( g, e );
	if(!vr || !vr->Length) return(0);
	
	if(vr->Data)
	{
		if (vr->Length > 1) value = ((unsigned char *)vr->Data)[1] << 8;
		value += ((unsigned char *)vr->Data)[0];
	}
	return(value);
}

// Will return a 32 bit integer from LE data, independent of vr->Length
UINT DICOMObject	::	GetUINT(UINT16	g, UINT16	e)
{
	VR		*vr;
	INT	cnt;
	UINT	value = 0;
	
	vr = this->GetVR ( g, e );
	if(!vr || !vr->Length) return(0);

	cnt = vr->Length - 1;
	
	if(vr->Data)
	{
		if (cnt > 3) cnt = 3;
		for(;cnt >= 0;--cnt)
		{
			value <<= 8;
			value += ((unsigned char *)vr->Data)[cnt];
		}
	}
	return(value);
}

// Will return a 64 bit integer from LE data, independent of vr->Length
unsigned long long DICOMObject	::	GetULongLong(UINT16	g, UINT16	e)
{
	VR		*vr;
	unsigned long long	value = 0;
	INT			cnt;
	
	vr = this->GetVR ( g, e );
	if(!vr || !vr->Length) return(0);

	cnt = vr->Length - 1;
	
	if(vr->Data)
	{
		if (cnt > 7) cnt = 7;
		for(;cnt >= 0;--cnt)
		{
			value <<= 8;
			value += ((unsigned char *)vr->Data)[cnt];
		}
	}
	return(value);
}

// Will return a 32 bit integer from Ascii, with integer vr->Length the first 10 chars (max for 32 bits anyway).
INT DICOMObject	::	Getatoi(UINT16	g, UINT16	e)
{
	VR		*vr;
	INT	cnt, value = 0;
	char	s[11];
	
	vr = this->GetVR ( g, e );
	if(!vr || !vr->Length) return(0);

	cnt = vr->Length;
	if(cnt > 10) cnt = 10;
	
	if(vr->Data)
	{
		strncpy(s, (const char*)vr->Data, cnt);
		s[cnt] = 0;
		value = atoi(s);
	}
	return(value);
}


// This will allocate this string, do not forget to free it.
char * DICOMObject	::	GetCString(UINT16 g, UINT16 e, size_t max)
{
	VR	*vr;
	size_t	cnt;
	char	*s;
	
	vr = this->GetVR ( g, e );
	if(!vr || !vr->Length || !vr->Data) return(NULL);

	cnt = vr->Length - 1;
	if(((char*)vr->Data)[cnt] != 0 || ((char*)vr->Data)[cnt] != ' ') cnt++;

	if(cnt > max) cnt = max;// Long enough
    
	if(!(s = (char*)malloc(cnt + 1))) return(NULL);// Maybe return -1?
	strncpy(s, (const char*)vr->Data, cnt);
	s[cnt] = 0;// End the string

	return(s);
}

//Fixed LastVR & LastVRG, still worry about CurrentGroup.
BOOL	DICOMObject::DeleteVR(VR* pVR)
{
  VR			*vr; 
  UINT			Index,Index2;
  VRGroupPQueue		*VRGroupPQueuePtr;

  if ( Packaged )
    return ( FALSE );	// already Pop() from this object

  /* Search the pVR */
  Index = 0;
  while ( Index < VRGroupPQueues.GetSize())
  { VRGroupPQueuePtr = VRGroupPQueues.Get(Index);
    if(!VRGroupPQueuePtr)
      return FALSE;

    if(VRGroupPQueuePtr->Group == pVR->Group)
    { Index2 = 0;
      while ( Index2 < VRGroupPQueuePtr->GetSize() )
      { vr = VRGroupPQueuePtr->Get(Index2);
        if(!vr)
        return FALSE;

        if(vr->Element == pVR->Element)
        { /* OK, the pVR has been found.
          Remove from Array of VrPointers */
          VRGroupPQueuePtr->RemoveAt(Index2);

          VRGroupPQueuePtr->Length -= (pVR->Length + sizeof(UINT16) + sizeof(UINT16) +
          				sizeof(UINT32) );
          if (VRGroupPQueuePtr->Length & 0x01)
            VRGroupPQueuePtr->Length--;

          Length -= (pVR->Length + sizeof(UINT16) + sizeof(UINT16) +
          		sizeof(UINT32) );
          if (Length & 0x01)
            Length--;

          // Maybe remove from Array of GroupPointers.
          if (!VRGroupPQueuePtr->Length)
          { VRGroupPQueues.RemoveAt(Index);
            delete VRGroupPQueuePtr;		// LJ: leak
            Length -= (sizeof(UINT16) + sizeof(UINT16) + sizeof(UINT32));
            LastVRG = NULL;			// bcb: Fix invalid pointer
          }

          // Get rid of the data.
          delete vr;				// LJ: leak
          LastVR = NULL;			// bcb: Crash fix.
          return TRUE;
        }
        ++Index2;
      }
      return FALSE;
    }
    ++Index;
  }
  return FALSE;
}

BOOL DICOMObject::ReplaceVR(VR* pVR)
{
  VR*			vr;
  UINT			Index, Index1, Index2;
  VRGroupPQueue*	VRGroupPQueuePtr;
  Array<DICOMDataObject*>*	pADDO;

  if ( Packaged )
    return ( FALSE );	// already Pop() from this object
  /* Search the pVR */
  Index = 0;
  while ( Index < VRGroupPQueues.GetSize())
  { VRGroupPQueuePtr = VRGroupPQueues.Get(Index);
    if(!VRGroupPQueuePtr)
      return FALSE;
    Index1 = 0;
    while ( Index1< VRGroupPQueuePtr->GetSize() )
    { vr = VRGroupPQueuePtr->Get(Index1);
      if(!vr)
	    return FALSE;
      if (vr->SetIf(pVR))
      { /* OK, the pVR has been found */
      }
      else if (vr->SQObjectArray)
      { /* Loop over all DICOMDataObjects in this sequence */
	    pADDO = (Array<DICOMDataObject*>*)vr->SQObjectArray;
	    Index2 = 0;
	    while (Index2< pADDO->GetSize())
	    { pADDO->Get(Index2)->ReplaceVR(pVR);
	      ++Index2;
	    }
      }
      ++Index1;
    }
    ++Index;
  }
  return FALSE;
}
// The ChangeVRs are cleaner than the ReplaceVR and do not require delete vr,
// they return a 0 if failed,
// a 1 if replaced,
// a 2 if created.
// For a string, it must end in a 0 ( standard c string ). 
INT     DICOMObject	::	ChangeVR(UINT16 g, UINT16 e, const char * str, UINT16 type, BOOL space)
{
    VR      *vr;
    UINT    slen;
    INT     rc = 1;
    
    slen = strlen(str);// Remove the end 
    vr = this->GetVR( g, e );
    if(!vr)
    {
        vr = new VR( g, e, slen, TRUE);
        if(!vr)return(0);
        rc = 2;
        vr->TypeCode = type;
        this->Push(vr);
    }
    else
    {
        if(!vr->ReAlloc(slen))return(0);
    }
    memcpy(vr->Data, str, slen);
    if(slen & 1)
    {
        if(space) ((char *)vr->Data)[slen] = ' ';
        else ((char *)vr->Data)[slen] = 0;
    }
    return(rc);
}

INT     DICOMObject	::	ChangeVR(UINT16 g, UINT16 e, UINT8 val, UINT16 type = 0)
{
    VR      *vr;
    INT     rc = 1;
    
    vr = this->GetVR( g, e );
    if(!vr)
    {
        vr = new VR( g, e, 2, TRUE);
        if(!vr)return(0);
        rc = 2;
        vr->TypeCode = type;
        this->Push(vr);
    }
    else
    {
        if(vr->Length != 2 || !vr->ReAlloc(2))return(0);
    }

    *(UINT8 *)vr->Data = val;
    ((UINT8 *)vr->Data)[1] = 0;
    return(rc);
}

INT     DICOMObject	::	ChangeVR(UINT16 g, UINT16 e, UINT16 val, UINT16 type = 0)
{
    VR      *vr;
    INT     rc = 1;
    
    vr = this->GetVR( g, e );
    if(!vr)
    {
        vr = new VR( g, e, 2, TRUE);
        if(!vr)return(0);
        rc = 2;
        vr->TypeCode = type;
        this->Push(vr);
    }
    else
    {
        if(vr->Length != 2 || !vr->ReAlloc(2))return(0);
    }
#if NATIVE_ENDIAN == LITTLE_ENDIAN //Little Endian
	*(UINT16 *)vr->Data = val;
#else //Big Endian like Apple power pc
	*(UINT16 *)vr->Data = SwitchEndian(val);
#endif
    return(rc);
}

INT     DICOMObject	::	ChangeVR(UINT16 g, UINT16 e, UINT val, UINT16 type = 0)
{
    VR      *vr;
    INT     rc = 1;
    
    vr = this->GetVR( g, e );
    if(!vr)
    {
        vr = new VR( g, e, 4, TRUE);
        if(!vr)return(0);
        vr->TypeCode = type;
        rc = 2;
        this->Push(vr);
    }
    else
    {
        if(vr->Length != 4 || !vr->ReAlloc(4))return(0);
    }
#if NATIVE_ENDIAN == LITTLE_ENDIAN //Little Endian
	*(UINT *)vr->Data = val;
#else //Big Endian like Apple power pc
	*(UINT *)vr->Data = SwitchEndian(val);
#endif
    return(rc);
}


BOOL	DICOMObject	::	Reset()
{
	VR	*vr;

	while (( vr = Pop() ))
		delete vr;

	LastVRG = NULL;
	LastVR = NULL;

	return ( TRUE );
}


BOOL	DICOMObject	::	Package()
{
	UINT	Index;
	
	CurrentGroup = NULL;
	Index = 0;
//	printf("[start: Package()]\n");
	while ( Index < VRGroupPQueues.GetSize())
	{
		PQueueOfPtr<VRGroupPQueue *> :: Push (VRGroupPQueues.Get(Index) );
		++Index;
	}
	while ( VRGroupPQueues.GetSize() )
		VRGroupPQueues.RemoveAt(0);
		
//	printf("[Packaged : %d Groups]\n", Index);
	Packaged = TRUE;
	if ( Index )
		return ( TRUE );
	Packaged = FALSE;
	return ( FALSE );
}


#define VR_SPECIAL_NONE			0x00
#define VR_SPECIAL_FIX_TMS		0x01
#define VR_SPECIAL_RGB_TO_MONO		0x02
#define VR_SPECIAL_RGB_TO_MONO_PLUS	0x03
#define VR_SPECIAL_UNKNOWN		0xFF

int VR::GetSpecialValueCodeOfVR(VR* pVR)
{  char*	pcData;

  /* This private function is called by SetIf.
     It analyzes the data of the VR. If it contains a special code, a string between
     '[[' and ']]', it will return the code; otherwise zero.
  */
  if ((pVR->Length < 4) || (!pVR->Data))
    return VR_SPECIAL_NONE;
  pcData = (char*)pVR->Data;
  if ((pcData[0] != '[') || (pcData[1] != '['))
    return VR_SPECIAL_NONE;

  if ((pVR->Length == 12) && (strncmp(pcData, "[[FIX_TMS]]", 11) == 0))
    return VR_SPECIAL_FIX_TMS;
  if ((pVR->Length == 20) && (strncmp(pcData, "[[RGB_TO_MONO_PLUS]]", 20) == 0))
    return VR_SPECIAL_RGB_TO_MONO_PLUS;
  if ((pVR->Length == 16) && (strncmp(pcData, "[[RGB_TO_MONO]]", 15) == 0))
    return VR_SPECIAL_RGB_TO_MONO;

  return VR_SPECIAL_UNKNOWN;
}


BOOL VR::SetIf(VR	*vr)
{ Array<DICOMObject*>	*ArrayDO;
  int			i, iSecialCodeOfVR;
  BOOL			rc = FALSE;
  char*			pCopy;
  unsigned char*	pRGB;
  unsigned char*	pMono;
  int			iNewLength;
  int			iNewValue, iPad = 0;

  if (!vr)
    return rc;
  if ((vr->Group==Group) && (vr->Element==Element))
  {
    iSecialCodeOfVR = GetSpecialValueCodeOfVR(vr);
    switch (iSecialCodeOfVR)
    { case VR_SPECIAL_NONE:
      /* Just replace the VR */
      if(ReleaseMemory)
//        delete [] Data;
	  free(Data);
      if(SQObjectArray)
      { ArrayDO = (Array<DICOMObject *> *) SQObjectArray;
	while(ArrayDO->GetSize())
	{ delete ArrayDO->Get(0);
	  ArrayDO->RemoveAt(0);
	}
	delete ArrayDO;
	SQObjectArray = NULL;
      }
      Length = vr->Length;
      if (vr->Length)
      { Data = malloc(vr->Length);
        if (!Data) DicomError(DCM_ERROR_MEMORY, "VR:SetIf out of memory allocating %d bytes\n", Length);
	memcpy(Data, vr->Data, vr->Length);
	ReleaseMemory = TRUE;
      }
      else
	ReleaseMemory = FALSE;
      rc = TRUE;
      break;

      case VR_SPECIAL_FIX_TMS:
      /* Change PatientName like 'LastName^FirstName^MiddleName' into
	 'LastName,FirstName,MiddleName'.
      */
      if (Length > 0)
      { pCopy = (char*)malloc(Length+2);
        if (!pCopy) DicomError(DCM_ERROR_MEMORY, "VR:SetIf out of memory allocating %d bytes\n", Length+2);
	memcpy(pCopy, (char*)Data, Length);
	pCopy[Length] = 0;
	/* Maybe padded with a space */
	if ((pCopy[Length-1] == ' ') || (pCopy[Length-1] == '^'))
	  pCopy[Length-1] = 0;
	if ((Length > 1) && (pCopy[Length-2] == '^'))
	  pCopy[Length-2] = 0;
	for (i=0; i<(int)(strlen(pCopy)); i++)
	  if (pCopy[i] == '^')
	    pCopy[i] = ',';
	/* May pad it again */
	if (strlen(pCopy) & 0x01)
	  strcat(pCopy, " ");
	strncpy((char*)Data, pCopy, strlen(pCopy));
	Length = strlen(pCopy);
	free(pCopy);
      }
      rc = TRUE;
      break;

      case VR_SPECIAL_RGB_TO_MONO:
      /* Convert triplets of bytes to just bytes (the mean of the RGB channels) */
      if (Length >= 3)
      { iNewLength = Length / 3;
	if (iNewLength & 0x01)
	  iPad = 1;
        pMono = (unsigned char*)malloc(iNewLength + iPad);
        if (!pMono) DicomError(DCM_ERROR_MEMORY, "VR:SetIf out of memory allocating %d bytes\n", iNewLength + iPad);
	pRGB = (unsigned char*)Data;
	for (i=0; i<iNewLength; i++)
	{ iNewValue = (pRGB[i * 3] + pRGB[i * 3 + 1] + pRGB[i * 3 + 2]) / 3; 
	  pMono[i] = (unsigned char)iNewValue;
	}
	if (iPad)
	  pMono[iNewLength] = 0;
	Length = iNewLength + iPad;
	free(Data);
	Data = pMono;
      }
      rc = TRUE;
      break;

      case VR_SPECIAL_RGB_TO_MONO_PLUS:
      /* Convert triplets of bytes to just bytes only when the value of those three
	 are equal (a B&W image). However, 'real' color (contours e.g.) are converted
	 to very white (255).
      */
      if (Length >= 3)
      { iNewLength = Length / 3;
	if (iNewLength & 0x01)
	  iPad = 1;
        pMono = (unsigned char*)malloc(iNewLength + iPad);
        if (!pMono) DicomError(DCM_ERROR_MEMORY, "VR:SetIf out of memory allocating %d bytes\n", iNewLength + iPad);
	pRGB = (unsigned char*)Data;
	for (i=0; i<iNewLength; i++)
	{ if ((pRGB[i * 3] == pRGB[i * 3 + 1]) && (pRGB[i * 3] == pRGB[i * 3 + 2])) 
	    pMono[i] = pRGB[i * 3];
	  else
	    pMono[i] = 255;
	}
	if (iPad)
	  pMono[iNewLength] = 0;
	Length = iNewLength + iPad;
	free(Data);
	Data = pMono;
      }
      rc = TRUE;
      break;
    }
  }
  return rc;
}


/* Just not used
BOOL
VR	::	Morph (
	DICOMObject	*DDO)
	{
	Array<DICOMObject	*>	*ArrayDO;

	if (!Length)
		if(!Valid)
			return ( FALSE );

	VR	*vr = new VR(Group, Element, 0, (BOOL) FALSE);
	vr->Length = Length;
	vr->Data = Data;
	vr->ReleaseMemory = ReleaseMemory;
	Length = 0;
	Data = NULL;
	ReleaseMemory = FALSE;
	DDO->Push(vr);

	if(SQObjectArray)
		{
		ArrayDO = (Array<DICOMObject *> *) SQObjectArray;
		while(ArrayDO->GetSize())
			{
			DDO->Push(ArrayDO->Get(0));
			ArrayDO->RemoveAt(0);
			}
		delete ArrayDO;
		SQObjectArray = NULL;
		}
	return ( TRUE );
	}*/

BOOL
VR	::	Reset ()
	{
	Array<DICOMObject	*>	*ArrayDO;

	if(ReleaseMemory)
		if(Data)
//			delete [] Data;
			free(Data);
	if(SQObjectArray)
		{
		ArrayDO = (Array<DICOMObject *> *) SQObjectArray;
		while(ArrayDO->GetSize())
			{
			delete ArrayDO->Get(0);
			ArrayDO->RemoveAt(0);
			}
		delete ArrayDO;
		SQObjectArray = NULL;
		}
	ReleaseMemory = FALSE;
	Data = NULL;
	Length = 0;
	return ( TRUE );
	}


BOOL
VR	::	ReAlloc (UINT32 N)
	{
        Reset();

	if (N&1) N++;
	ReleaseMemory = TRUE;
	Data = malloc(N);
        if (!Data) DicomError(DCM_ERROR_MEMORY, "VR:ReAlloc out of memory allocating %d bytes\n", N);
	Length = N;
	return ( Data!=NULL );
	}

// Will return an unsigned short from LE data, independent of vr->Length
UINT16
VR	::	GetUINT16()
{
	UINT16	value;
	
	value = 0;
	
	if(Data)
	{
		if (Length > 1)value = ((unsigned char *)Data)[1] << 8;
		value += ((unsigned char *)Data)[0];
	}
	return(value);
}

// Will return a 32 bit interger from LE data, independent of vr->Length
UINT
VR	::	GetUINT()
{
	INT	cnt;
	UINT	value = 0;
	
	cnt = Length - 1;
	
	if(Data)
	{
		if (cnt > 3) cnt = 3;
		for(;cnt >= 0;--cnt)
		{
			value <<= 8;
			value += ((unsigned char *)Data)[cnt];
		}
	}
	return(value);
}

// Will return an unsigned 64 bit interger from LE data, independent of vr->Length
unsigned long long
VR	::	GetULongLong()
{
	unsigned long long	value;
	INT			cnt;
	
	cnt = Length - 1;
	value = 0;
	
	if(Data)
	{
		if (cnt > 7) cnt = 7;
		for(;cnt >= 0;--cnt)
		{
			value <<= 8;
			value += ((unsigned char *)Data)[cnt];
		}
	}
	return(value);
}

// Will return an 32 bit interger from Ascii, with interger vr->Length or shorter.
INT
VR	::	Getatoi()
{
	INT		cnt, value = 0;
	char		s[11];
	
	cnt = Length;
    if(cnt > 10) cnt = 10;
	
	if(cnt && Data)
	{
		strncpy(s, (const char*)Data,cnt);
		s[cnt] = 0;
		value = atoi(s);
	}
	return(value);
}