forked from duckhead/Conquest-DICOM-Server
/
loadddo.cpp
963 lines (865 loc) · 24.5 KB
/
loadddo.cpp
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
/*
* Reads DICOMObject's from disk. As well as makes Windows BITMAP
* Structures.
*
* Bugs: Does not handle sequence objects
* Does not support the recent change to DICOM Chapter 10
* Only works on 32 bit little-endian architecture
* Notes:
*
* Eventually this routine will be not be needed. I will
* add Disk I/O to the PDU_Service class, which will be a much cleaner,
* and much more portable solution.
* Mark Oskin
*
*/
/*
19980409 mvh Added NKI private decompression engine VR 0x7fdf,0x0010
19980410 mvh Added run length encoding of zero differences
19980410 ljz+mvh Fix 16 bit absolute value decode
19980415 ljz+mvh Fix leak on decompress
19980625 mvh Added compression in SaveDicomDataObject(C10)
19980703 mvh+ljz Made DObjectSerialize thread save (removed statics)
19990105 ljz Fix: SaveDICOMDataObjectC10 created VR 0x0002 0x0013 twice
Added comment in SaveDICOMDataObjectC10; CleanUp
Put the nki-routines also in dgate.hpp
Added LoadImplicitLittleEndianFile
19990317 ljz Decompression removed from LoadImplicitLittleEndianFile; added a
VR-sizelimit of that reader
19990827 mvh NOTE: LoadImplicitLittleEndianFile allocates 100 MB and crashes when passing
chapter 10 file (happens when extension is wrongly .v2)
19990830 ljz Fixed problem above: 'LoadImplicitLittleEndianFile' uses
'PDU.LoadDICOMDataObject' in such cases.
19991117 ljz Added parameter FileCompressMode to in nki_private_compress call
20000629 ljz Logging of trouble now starts with '***'
20001104 mvh Renamed _LittleEndianUID to _LittleEndianUID_space
20001105 mvh Fixed where malloc and new were mixed (vr->data)
20001106 mvh Use delete [] operation for vr->Data
20001128 ljz Fix in 'Implicit length' code in LoadImplicitLittleEndian
20010429 mvh Use faster and safer decompressor
20020412 ljz Added class CBufferedIO for faster reading from DVD (stream functions
do not seem to be cacheing...)
20020415 mvh Removed diagnostic time code (did not compile on ms4.2)
Made buffer size dependent on iVrSizelimit
(i.e, use smaller for regen, slightly faster)
20030706 mvh Attach VRType to PDU's for implicit little endian support
20041130 mvh Documented crash on win2000 server in code - remain to be fixed
20050107 mvh Adapted for linux compile
20051219 mvh Use ReAlloc instead of new BYTE[] to fill VR data
20070308 bcb And little/big endian.
*/
#ifdef WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
#include <fcntl.h>
#include <sys/stat.h>
#ifndef WHEDGE
# include "dgate.hpp"
#else
# include "master.h"
#endif
typedef struct _DEIVR
{
UINT16 Group;
UINT16 Element;
UINT32 Length;
} DEIVR;
// Serializes the input of a DICOM Data object 1 byte at a time. Very usefull
// routine to use if memory constraints are present
typedef struct _DOBJECTSERSTATE
{
int state;
UINT16 stuint16;
UINT32 stuint32;
DEIVR deivr;
VR *vr;
UINT sDIndex;
} DOBJECTSERSTATE;
DICOMDataObject *
DObjectSerialize ( DICOMDataObject *DDO, unsigned char ch,
BOOL LFrame, DOBJECTSERSTATE *doss)
{
// NOTE: THIS CODE WAS NOT THREADSAVE DUE TO THE USE OF STATIC VARIABLES !
// static int state;
// static UINT16 stuint16;
// static UINT32 stuint32;
// static DEIVR deivr;
// static VR *vr;
// static UINT sDIndex;
UINT32 tuint32;
if(!DDO) // re-intialize state engine
{
doss->state = 0;
return (new DICOMDataObject);
}
tuint32 = ch;
switch ( doss->state )
{
case 0: // base state
doss->stuint16 = (UINT16) (tuint32 << 0);
doss->state = 1;
return (DDO);
case 1:
doss->stuint16 = doss->stuint16 | (UINT16) (tuint32 << 8);
doss->deivr.Group = doss->stuint16;
doss->state = 2;
return ( DDO );
case 2:
doss->stuint16 = (UINT16) (tuint32 << 0);
doss->state = 3;
return ( DDO );
case 3:
doss->stuint16 = doss->stuint16 | (UINT16) (tuint32 << 8);
doss->deivr.Element = doss->stuint16;
doss->state = 4;
return ( DDO );
case 4:
doss->stuint32 = (tuint32 << 0);
doss->state = 5;
return ( DDO );
case 5:
doss->stuint32 = doss->stuint32 | (tuint32 << 8);
doss->state = 6;
return ( DDO );
case 6:
doss->stuint32 = doss->stuint32 | (tuint32 << 16);
doss->state = 7;
return ( DDO );
case 7:
doss->stuint32 = doss->stuint32 | (tuint32 << 24);
doss->deivr.Length = doss->stuint32;
doss->sDIndex = 0;
if(doss->deivr.Group==0x7fe0 && doss->deivr.Element==0x0010)
if(LFrame)
{
return ( NULL );
}
if(doss->deivr.Group==0x7fdf && doss->deivr.Element==0x0010)
if(LFrame)
{
return ( NULL );
}
doss->vr = new VR(doss->deivr.Group, doss->deivr.Element, doss->deivr.Length, TRUE);
if(!doss->deivr.Length)
{
DDO->Push(doss->vr);
doss->state = 0;
}
else
doss->state = 8;
return ( DDO );
case 8:
if(!doss->vr->Data)
return ( NULL ); // error
((BYTE *) doss->vr->Data ) [doss->sDIndex] = ch;
++(doss->sDIndex);
if(doss->sDIndex == doss->deivr.Length)
{
if(doss->deivr.Group==0x7fdf && doss->deivr.Element==0x0010)
{
VR *v;
signed char *CompressedData = ((signed char *)(doss->vr->Data));
int UncompressedLength = get_nki_private_decompressed_length(CompressedData);
v = new VR(0x7fe0, 0x0010, UncompressedLength, TRUE);
nki_private_decompress((short *)(v->Data), CompressedData, v->Length);
DDO->Push(v);
delete doss->vr; // we stored a copy
}
else
DDO->Push(doss->vr);
doss->state = 0;
}
return ( DDO );
}
return ( NULL ); // error
}
DICOMDataObject *
LoadDICOMDataObject(char *filename)
{
DICOMDataObject *DDO;
UINT MemBlock, Block;
UINT32 TCount, Index;
unsigned char *mem;
FILE *fp;
DOBJECTSERSTATE doss;
MemBlock = 50000;
fp = fopen(filename, "rb");
if(!fp)
{
return ( FALSE );
}
mem = (unsigned char *) malloc(MemBlock);
if(!mem)
{
fclose(fp);
return ( FALSE );
}
DDO = DObjectSerialize(NULL, 0, FALSE, &doss);
if(!DDO)
{
free(mem);
return ( FALSE );
}
TCount = 0;
while( TRUE )
{
Block = fread(mem, 1, MemBlock, fp);
Index = 0;
if(!TCount)
{
if(!strncmp((char*) &mem[128], "DICM", 4))
Index = 128 + 4;
}
TCount += Block;
while(Index < Block)
{
DObjectSerialize(DDO, mem[Index], FALSE, &doss);
++Index;
}
if(Block!=MemBlock)
break;
}
free(mem);
fclose ( fp );
return ( DDO );
}
static BOOL
SaveDDO(DICOMDataObject* DDOPtr, FILE* fp, int FileCompressMode)
{
VR *TempVR;
#if NATIVE_ENDIAN == BIG_ENDIAN //Big Endian like Apple power pc
UINT16 beGroup, beElement;
UINT32 beLength;
#endif //Big Endian
while(TempVR = DDOPtr->Pop())
{
if(TempVR->Group==0x7fe0 && TempVR->Element==0x0010 && FileCompressMode)
{
int DataLength;
short CompressedGroup = 0x7fdf, CompressedElement = 0x0010;
signed char *CompressedData;
CompressedData = new signed char [(TempVR->Length/2) * 3 + 10];
DataLength = nki_private_compress(CompressedData, (short int *)(TempVR->Data), TempVR->Length/2, FileCompressMode);
if (!CompressedData)
{
OperatorConsole.printf("***Out of memory NKI-compress");
return FALSE;
}
if (DataLength&1)
CompressedData[DataLength++] = 0;
#if NATIVE_ENDIAN == LITTLE_ENDIAN //Little Endian
fwrite(&CompressedGroup, 1, 2, fp);
fwrite(&CompressedElement, 1, 2, fp);
fwrite(&DataLength, 1, 4, fp);
#else //Big Endian like Apple power pc
beGroup = SwitchEndian((UINT16) CompressedGroup);
beElement = SwitchEndian((UINT16) CompressedElement);
beLength = SwitchEndian((UINT32) DataLength);
fwrite(&beGroup, 1, 2, fp);
fwrite(&beElement, 1, 2, fp);
fwrite(&beLength, 1, 4, fp);
#endif //Big Endian
if (fwrite(CompressedData, 1, DataLength, fp) != (unsigned int)DataLength)
{
free(CompressedData);
OperatorConsole.printf("***Failed to save compressed pixel data (disk full?)");
return FALSE;
}
delete [] CompressedData;
}
else
{
#if NATIVE_ENDIAN == LITTLE_ENDIAN //Little Endian
fwrite(&TempVR->Group, 1, 2, fp);
fwrite(&TempVR->Element, 1, 2, fp);
fwrite(&TempVR->Length, 1, 4, fp);
#else //Big Endian like Apple power pc
beGroup = SwitchEndian((UINT16) TempVR->Group);
beElement = SwitchEndian((UINT16) TempVR->Element);
beLength = SwitchEndian((UINT32) TempVR->Length);
fwrite(&beGroup, 1, 2, fp);
fwrite(&beElement, 1, 2, fp);
fwrite(&beLength, 1, 4, fp);
#endif //Big Endian
if(TempVR->Length)
{
if(!TempVR->Data)
{
OperatorConsole.printf("***Missing data in VR (0x%04x,0x%04x)",
TempVR->Group, TempVR->Element);
return FALSE;
}
fwrite(TempVR->Data, 1, TempVR->Length, fp);
}
}
delete TempVR;
}
return TRUE;
}
BOOL
SaveDICOMDataObject(DICOMDataObject *DDOPtr, char *filename, int FileCompressMode)
{
FILE* fp;
BOOL rc;
fp = fopen(filename, "wb");
if(!fp) return FALSE;
rc = SaveDDO(DDOPtr, fp, FileCompressMode);
fclose(fp);
return rc;
}
# define _LittleEndianUID_space "1.2.840.10008.1.2 "
# define _ImplementationUID "none yet"
# define _ImplementationVersion "0.1AlphaUCDMC "
# define _SourceApplicationAE "none "
BOOL
SaveDICOMDataObjectC10(DICOMDataObject *DDOPtr, char *filename, int FileCompressMode)
{
VR *vr, *vr1;
UINT16 tuint16;
FILE *fp;
char s[140];
BOOL rc;
vr = DDOPtr->GetVR(0x0002, 0x0001);
if(!vr)
{
// This does not contain the C10 Header yet, so we need to
// constuct it
// FileMetaInformationVersion
vr = new VR(0x0002, 0x0001, 0x0002, TRUE);
tuint16 = 0x0001;
memcpy(vr->Data, (void*)&tuint16, 2);
DDOPtr->Push(vr);
// SOPClassUID
vr = DDOPtr->GetVR(0x0008, 0x0016);
if(vr)
{
// MediaStorageSOPClassUID
vr1 = new VR(0x0002, 0x0002, vr->Length, TRUE);
memcpy(vr1->Data, vr->Data, vr->Length);
DDOPtr->Push(vr1);
}
// SOPInstanceUID
vr = DDOPtr->GetVR(0x0008, 0x0018);
if(vr)
{
// MediaStorageSOPInstanceUID
vr1 = new VR(0x0002, 0x0003, vr->Length, TRUE);
memcpy(vr1->Data, vr->Data, vr->Length);
DDOPtr->Push(vr1);
}
// TransferSyntaxUID
vr = new VR(0x0002, 0x0010, strlen(_LittleEndianUID_space), TRUE);
memset(vr->Data, 0, vr->Length);
memcpy(vr->Data, (void*)_LittleEndianUID_space, strlen(_LittleEndianUID_space));
DDOPtr->Push(vr);
// ImplementationClassUID
vr = new VR(0x0002, 0x0012, strlen(_ImplementationUID), TRUE);
memset(vr->Data, 0, vr->Length);
memcpy(vr->Data, (void*)_ImplementationUID, strlen(_ImplementationUID));
DDOPtr->Push(vr);
// ImplementationVersionName
vr = new VR(0x0002, 0x0013, strlen(_ImplementationVersion), TRUE);
memset(vr->Data, 0, vr->Length);
memcpy(vr->Data, (void*)_ImplementationVersion, strlen(_ImplementationVersion));
DDOPtr->Push(vr);
// SourceApplicationEntityTitle
vr = new VR(0x0002, 0x0016, strlen(_SourceApplicationAE), TRUE);
memset(vr->Data, 0, vr->Length);
memcpy(vr->Data, (void*)_SourceApplicationAE, strlen(_SourceApplicationAE));
DDOPtr->Push(vr);
}
fp = fopen(filename, "wb");
if(!fp) return FALSE;
memset((void*)s, 0, 128);
strcpy(&s[128], "DICM");
fwrite(s, 128 + 4, 1, fp);
rc = SaveDDO(DDOPtr, fp, FileCompressMode);
fclose(fp);
return rc;
}
static BOOL
OrgLoadImplicitLittleEndian(DICOMDataObject* pDDO, FILE* fp, unsigned int iVrSizeLimit)
{
DICOMDataObject* pNewDDO;
VR* pVR;
char Buf[2 + 2 + 4];
while (fread(Buf, 1, sizeof(Buf), fp) == sizeof(Buf))
{
/* Group, Element and Size could be read */
pVR = new VR;
if (!pVR)
return FALSE;
#if NATIVE_ENDIAN == LITTLE_ENDIAN //Little Endian
pVR->Group = *((unsigned short*) Buf);
pVR->Element = *((unsigned short*)(Buf + 2));
pVR->Length = *((unsigned int*) (Buf + 2 + 2));
#else //Big Endian like Apple power pc
pVR->Group = SwitchEndian( *((UINT16*) Buf));
pVR->Element = SwitchEndian( *((UINT16*)(Buf + 2)));
pVR->Length = SwitchEndian( *((UINT32*) (Buf + 2 + 2)));
#endif //Big Endian
if (pVR->Group == 0xfffe)
{
/* A deliminator */
if ((pVR->Element == 0xe0dd) || (pVR->Element == 0xe00d))
{
delete pVR;
return TRUE;
}
if (pVR->Length == 0xffffffff)
{
/* Implicit length... Go until deliminator */
pVR->Length = 0;
delete pVR;
pNewDDO = new DICOMDataObject;
if (OrgLoadImplicitLittleEndian(pNewDDO, fp, iVrSizeLimit))
{
pDDO->Push(pNewDDO);
continue;
}
else
{
delete pNewDDO;
return FALSE;
}
}
if (pVR->Element == 0xe000)
{
/* Sequence begin ? */
pVR->Length = 0;
delete pVR;
pNewDDO = new DICOMDataObject;
if (OrgLoadImplicitLittleEndian(pNewDDO, fp, iVrSizeLimit))
{
pDDO->Push(pNewDDO);
continue;
}
else
{
delete pNewDDO;
return FALSE;
}
}
}
if (pVR->Length == 0xffffffff)
{
pVR->Length = 0;
pDDO->Push(pVR);
if (!OrgLoadImplicitLittleEndian(pDDO, fp, iVrSizeLimit))
return FALSE;
continue;
}
/* Check whether the current VR has to be read.
NKI DicomNodes can restrict what has to be read
Following code assumes that reading is finished when pixeldata
are encountered. (Maybe a problem here!!!)
*/
if (pVR->Length > iVrSizeLimit)
{
if (((pVR->Group == 0x7fdf) || (pVR->Group == 0x7fe0)) &&
(pVR->Element == 0x0010))
{
/* Ready !? */
pVR->Length = 0;
delete pVR;
return TRUE;
}
else
{ /* Read it, throw it away and continue */
// pVR->Data = new char [pVR->Length];
pVR->ReAlloc(pVR->Length);
if (!pVR->Data)
return FALSE;
fread(pVR->Data, 1, pVR->Length, fp);
delete pVR;
continue;
}
}
if (pVR->Length)
{
// pVR->Data = new char [pVR->Length];
pVR->ReAlloc(pVR->Length);
if (!pVR->Data)
return FALSE;
fread(pVR->Data, 1, pVR->Length, fp);
/* if ((pVR->Group == 0x7fdf) && (pVR->Element == 0x0010))
{
VR* v;
signed char *CompressedData = ((signed char *)(pVR->Data));
int UncompressedLength = get_nki_private_decompressed_length(CompressedData);
v = new VR(0x7fe0, 0x0010, UncompressedLength, TRUE);
nki_private_decompress((short *)(v->Data), CompressedData);
delete pVR;
pVR = v;
}
*/ }
else
pVR->Data = NULL;
pDDO->Push(pVR);
}
return TRUE;
}
DICOMDataObject*
OrgLoadImplicitLittleEndianFile(char* filename, unsigned int iVrSizeLimit)
{
DICOMDataObject* pDDO;
FILE* fp;
char Buf[128 + 4];
PDU_Service PDU;
PDU.AttachRTC(&VRType);
fp = fopen(filename, "rb");
if(!fp)
{
return NULL;
}
/* Find out whether marker 'DICM' exists */
if (fread(Buf, 1, 128 + 4, fp) != 128 + 4)
{
fclose(fp);
return NULL;
}
else
{
if (strncmp(Buf + 128, "DICM", 4) != 0)
{
fseek(fp, 0, SEEK_SET);
/* We are at beginning of the VRs */
pDDO = new DICOMDataObject;
OrgLoadImplicitLittleEndian(pDDO, fp, iVrSizeLimit);
fclose ( fp );
return pDDO;
}
else
{
/* The function 'LoadImplicitLittleEndianFile' is not intended for
DICM files, however don't be fuzzy about it... */
fclose(fp);
pDDO = PDU.LoadDICOMDataObject(filename);
return pDDO;
}
}
}
/*-----------------------------------------------------------------------*/
class CBufferedIO
{
private:
char* m_pBuf;
int m_iBufSize;
int m_iCurPos;
int m_iNbBytesInBuffer;
int m_iNbBlocksRead;
public:
CBufferedIO(int iBufSize = 0x4000);
~CBufferedIO();
int read (int handle, void *buffer, unsigned int count);
long lseek(int handle, long offset, int origin);
};
CBufferedIO::CBufferedIO(int iBufSize)
{
m_pBuf = NULL;
m_iBufSize = iBufSize;
m_iCurPos = 0;
m_iNbBytesInBuffer = 0;
m_iNbBlocksRead = 0;
};
CBufferedIO::~CBufferedIO()
{
if (m_pBuf)
free(m_pBuf);
};
int CBufferedIO::read (int handle, void *buffer, unsigned int count)
{ int iNbBytesCopied = 0;
if (!m_pBuf)
{ if (m_iBufSize <= 0)
return -1;
m_pBuf = (char*)malloc(m_iBufSize);
if (!m_pBuf)
return -1;
}
while (count - iNbBytesCopied > m_iNbBytesInBuffer - m_iCurPos)
{ memcpy(buffer, m_pBuf + m_iCurPos, m_iNbBytesInBuffer - m_iCurPos);
buffer = (char*)buffer + m_iNbBytesInBuffer - m_iCurPos;
iNbBytesCopied += m_iNbBytesInBuffer - m_iCurPos;
m_iCurPos = 0;
m_iNbBytesInBuffer = ::read(handle, m_pBuf, m_iBufSize);
m_iNbBlocksRead ++;
if (m_iNbBytesInBuffer <= 0)
return iNbBytesCopied;
}
/* crash probably when read failed of file after:
2004-11-30 09:46:48 [RT8769103] Sending file : y:\dv0_0001\20001314\1.2.840.113619.2.22.287.1.369.2.20000619.221935_0002_000014_96263112199.V2
0001:0001fb70 ?read@CBufferedIO@@QAEHHPAXI@Z 00420b70 f loadddo.obj
0001:0001fc40 ?lseek@CBufferedIO@@QAEJHJH@Z 00420c40 f loadddo.obj
function: <nosymbols>
00420c0d 5b pop ebx
00420c0e c20c00 ret 0xc
00420c11 8b44241c mov eax,[esp+0x1c] ss:0b6c89cf=????????
00420c15 8b7500 mov esi,[ebp+0x0] ss:02b85cb6=????????
00420c18 8bd0 mov edx,eax
00420c1a 037508 add esi,[ebp+0x8] ss:02b85cb6=????????
00420c1d 2bd3 sub edx,ebx
00420c1f 8b7c2418 mov edi,[esp+0x18] ss:0b6c89cf=????????
00420c23 8bca mov ecx,edx
00420c25 c1e902 shr ecx,0x2
FAULT ->00420c28 f3a5 rep movsd ds:023c2000=???????? es:1f838480=00000000
00420c2a 8bca mov ecx,edx
00420c2c 83e103 and ecx,0x3
00420c2f f3a4 rep movsb ds:023c2000=?? es:1f838480=00
00420c31 8bc8 mov ecx,eax
00420c33 2bcb sub ecx,ebx
00420c35 014d08 add [ebp+0x8],ecx ss:02b85cb6=????????
00420c38 5d pop ebp
00420c39 5f pop edi
00420c3a 5e pop esi
00420c3b 5b pop ebx
00420c3c c20c00 ret 0xc
*/
memcpy(buffer, m_pBuf + m_iCurPos, count - iNbBytesCopied);
m_iCurPos += count - iNbBytesCopied;
return count;
}
long CBufferedIO::lseek(int handle, long offset, int origin)
{ long rc;
/* Just handle the special case for the 'DICM' test */
if ((origin == SEEK_SET) &&
(offset < m_iNbBytesInBuffer) &&
(m_iNbBlocksRead == 1))
{ m_iCurPos = offset;
rc = offset;
}
else
{ /* Make sure that above special case does not re-appear */
m_iNbBlocksRead = 999;
if (origin == SEEK_CUR)
/* Adjust the file-position to where the caller thinks it is */
offset = offset - (m_iNbBytesInBuffer - m_iCurPos);
rc = ::lseek(handle, offset, origin);
m_iCurPos = 0;
m_iNbBytesInBuffer = 0;
}
return rc;
}
/*-----------------------------------------------------------------------*/
static BOOL
LoadImplicitLittleEndian(DICOMDataObject* pDDO, CBufferedIO* pBufferedIO,
int handle, unsigned int iVrSizeLimit)
{
DICOMDataObject* pNewDDO;
VR* pVR;
char Buf[2 + 2 + 4];
while (pBufferedIO->read(handle, Buf, sizeof(Buf)) == sizeof(Buf))
{
/* Group, Element and Size could be read */
pVR = new VR;
if (!pVR)
return FALSE;
#if NATIVE_ENDIAN == LITTLE_ENDIAN //Little Endian
pVR->Group = *((unsigned short*) Buf);
pVR->Element = *((unsigned short*)(Buf + 2));
pVR->Length = *((unsigned int*) (Buf + 2 + 2));
#else //Big Endian like Apple power pc
pVR->Group = SwitchEndian( *((UINT16*) Buf));
pVR->Element = SwitchEndian( *((UINT16*)(Buf + 2)));
pVR->Length = SwitchEndian( *((UINT32*) (Buf + 2 + 2)));
#endif //Big Endian
if (pVR->Group == 0xfffe)
{
/* A deliminator */
if ((pVR->Element == 0xe0dd) || (pVR->Element == 0xe00d))
{
delete pVR;
return TRUE;
}
if (pVR->Length == 0xffffffff)
{
/* Implicit length... Go until deliminator */
pVR->Length = 0;
delete pVR;
pNewDDO = new DICOMDataObject;
if (LoadImplicitLittleEndian(pNewDDO, pBufferedIO, handle, iVrSizeLimit))
{
pDDO->Push(pNewDDO);
continue;
}
else
{
delete pNewDDO;
return FALSE;
}
}
if (pVR->Element == 0xe000)
{
/* Sequence begin ? */
pVR->Length = 0;
delete pVR;
pNewDDO = new DICOMDataObject;
if (LoadImplicitLittleEndian(pNewDDO, pBufferedIO, handle, iVrSizeLimit))
{
pDDO->Push(pNewDDO);
continue;
}
else
{
delete pNewDDO;
return FALSE;
}
}
}
if (pVR->Length == 0xffffffff)
{
pVR->Length = 0;
pDDO->Push(pVR);
if (!LoadImplicitLittleEndian(pDDO, pBufferedIO, handle, iVrSizeLimit))
return FALSE;
continue;
}
/* Check whether the current VR has to be read.
NKI DicomNodes can restrict what has to be read
Following code assumes that reading is finished when pixeldata
are encountered. (Maybe a problem here!!!)
*/
if (pVR->Length > iVrSizeLimit)
{
if (((pVR->Group == 0x7fdf) || (pVR->Group == 0x7fe0)) &&
(pVR->Element == 0x0010))
{
/* Ready !? */
pVR->Length = 0;
delete pVR;
return TRUE;
}
else
{ /* Read it, throw it away and continue */
// pVR->Data = new char [pVR->Length];
pVR->ReAlloc(pVR->Length);
if (!pVR->Data)
return FALSE;
pBufferedIO->read(handle, pVR->Data, pVR->Length);
delete pVR;
continue;
}
}
if (pVR->Length)
{
// pVR->Data = new char [pVR->Length];
pVR->ReAlloc(pVR->Length);
if (!pVR->Data)
return FALSE;
pBufferedIO->read(handle, pVR->Data, pVR->Length);
}
else
pVR->Data = NULL;
pDDO->Push(pVR);
}
return TRUE;
}
DICOMDataObject*
LoadImplicitLittleEndianFile(char* filename, unsigned int iVrSizeLimit)
{
DICOMDataObject* pDDO;
int handle;
char Buf[128 + 4];
PDU_Service PDU;
unsigned int iLastTime, iCurTime;
CBufferedIO* pBufferedIO;
PDU.AttachRTC(&VRType);
#ifdef WIN32
handle = sopen( filename, O_RDONLY | O_BINARY, SH_DENYNO, S_IREAD);
#else
handle = open( filename, O_RDONLY);
#endif
if (handle == -1)
{
return NULL;
}
pBufferedIO = new CBufferedIO(iVrSizeLimit < 0x1000 ? 0x1000 : 0x8000);
/* Find out whether marker 'DICM' exists */
if (pBufferedIO->read(handle, Buf, 128 + 4) != 128 + 4)
{
delete pBufferedIO;
close(handle);
return NULL;
}
else
{
if (strncmp(Buf + 128, "DICM", 4) != 0)
{
pBufferedIO->lseek(handle, 0, SEEK_SET);
/* We are at beginning of the VRs */
pDDO = new DICOMDataObject;
LoadImplicitLittleEndian(pDDO, pBufferedIO, handle, iVrSizeLimit);
//iLastTime = timeGetTime();
delete pBufferedIO;
close(handle);
//iCurTime = timeGetTime();
//printf("fopen=%d\r\n", iCurTime - iLastTime);
return pDDO;
}
else
{
/* The function 'LoadImplicitLittleEndianFile' is not intended for
DICM files, however don't be fuzzy about it... */
delete pBufferedIO;
close(handle);
pDDO = PDU.LoadDICOMDataObject(filename);
return pDDO;
}
}
}
DICOMDataObject *
LoadDICOMDataObjectFrame(char *filename)
{
DICOMDataObject *DDO;
UINT MemBlock, Block;
UINT32 TCount, Index;
unsigned char *mem;
FILE *fp;
DOBJECTSERSTATE doss;
MemBlock = 50000;
fp = fopen(filename, "rb");
if(!fp)
{
return ( FALSE );
}
mem = (unsigned char *) malloc(MemBlock);
if(!mem)
{
fclose(fp);
return ( FALSE );
}
DDO = DObjectSerialize(NULL, 0, FALSE, &doss);
if(!DDO)
{
free(mem);
return ( FALSE );
}
TCount = 0;
while( TRUE )
{
Block = fread(mem, 1, MemBlock, fp);
Index = 0;
if(!TCount)
{
if(!strncmp((char*) &mem[128], "DICM", 4))
Index = 128 + 4;
}
TCount += Block;
while(Index < Block)
{
if(!DObjectSerialize(DDO, mem[Index], TRUE, &doss))
{
free(mem);
fclose(fp);
return(DDO);
}
++Index;
}
if(Block!=MemBlock)
break;
}
free(mem);
fclose ( fp );
return ( DDO );
}