forked from ImpulseAdventure/JPEGsnoop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DecodeDicom.cpp
878 lines (757 loc) · 22.9 KB
/
DecodeDicom.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
// JPEGsnoop - JPEG Image Decoder & Analysis Utility
// Copyright (C) 2017 - Calvin Hass
// http://www.impulseadventure.com/photo/jpeg-snoop.html
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#include "stdafx.h"
#include "DecodeDicom.h"
#include "snoop.h"
#include "JPEGsnoop.h" // for m_pAppConfig get
#include "WindowBuf.h"
// Do we want to display extended DICOM tag info?
//#ifdef DICOM_TAG_EXTENDED
// Forward declarations
struct tsDicomTag asDicomTags[];
struct tsDicomTagSpec asTagSpecial[];
CDecodeDicom::CDecodeDicom(CwindowBuf* pWBuf,CDocLog* pLog)
{
// Save copies of class pointers
m_pWBuf = pWBuf;
m_pLog = pLog;
// Ideally this would be passed by constructor, but simply access
// directly for now.
CJPEGsnoopApp* pApp;
pApp = (CJPEGsnoopApp*)AfxGetApp();
m_pAppConfig = pApp->m_pAppConfig;
Reset();
}
void CDecodeDicom::Reset()
{
m_bDicom = false;
m_bJpegEncap = false;
m_bJpegEncapOffsetNext = false;
}
CDecodeDicom::~CDecodeDicom(void)
{
}
// Provide a short-hand alias for the m_pWBuf buffer
// INPUT:
// - offset File offset to read from
// - bClean Forcibly disables any redirection to Fake DHT table
//
// POST:
// - m_pLog
//
// RETURN:
// - Byte from file
//
BYTE CDecodeDicom::Buf(unsigned long offset,bool bClean=false)
{
return m_pWBuf->Buf(offset,bClean);
}
bool CDecodeDicom::GetTagHeader(unsigned long nPos,tsTagDetail &sTagDetail)
{
unsigned nVR = 0;
CString strError;
unsigned nTagGroup;
unsigned nTagElement;
CString strTagName = _T("");
unsigned nLen = 0;
unsigned nOffset = 0;
CString strVR = _T("");
bool bTagOk = false;
CString strTag = _T("");
bool bVrExplicit;
bool bLen4B;
bool bTagIsJpeg;
unsigned long nPosJpeg;
bool bTagIsOffsetHdr;
// Find the tag
nTagGroup = m_pWBuf->BufX(nPos+0,2,true);
nTagElement = m_pWBuf->BufX(nPos+2,2,true);
bool bFoundTag;
unsigned nFldInd = 0;
bFoundTag = FindTag(nTagGroup,nTagElement,nFldInd);
if (!bFoundTag) {
// Check for group length entry
// FIXME:
// Reference: Part 5, Section 7.2
if (nTagElement == 0x0000) {
strTagName.Format(_T("Group Length (Group=%04X)"),nTagGroup);
bTagOk = true;
} else {
/*
strError.Format(_T("ERROR: Unknown Tag ID (%04X,%04X) @ 0x%08X"),nTagGroup,nTagElement,nPos);
m_pLog->AddLineErr(strError);
*/
strTagName.Format(_T("??? (%04X,%04X)"),nTagGroup,nTagElement);
bTagOk = false;
}
} else {
strTagName = asDicomTags[nFldInd].strTagName;
bTagOk = true;
}
// Check for zeros in place of VR. This might hint at implicit VR
// FIXME
if (m_pWBuf->BufX(nPos+4,2,true) == 0) {
//unsigned nBad=1;
}
strVR = m_pWBuf->BufReadStrn(nPos+4,2);
nVR = m_pWBuf->BufX(nPos+4,2,false);
// Value Representation (VR)
// Reference: Part 5, Section 6.2
bVrExplicit = false;
bLen4B = false;
switch (nVR) {
// Ref: Part 5, Section 7.1.2 "Data Element Structure with Explicit VR"
case 'AE':
case 'AS':
case 'AT':
case 'CS':
case 'DA':
case 'DS':
case 'DT':
case 'FL':
case 'FD':
case 'IS':
case 'LO':
case 'LT':
//case 'OB':
//case 'OF':
//case 'OW':
case 'PN':
case 'SH':
case 'SL':
//case 'SQ':
case 'SS':
case 'ST':
case 'TM':
case 'UI':
case 'UL':
//case 'UN':
case 'US':
//case 'UT':
bVrExplicit = true;
bLen4B = false;
break;
// Definition of VRs that have 4B length
// Reference (informal): http://cran.r-project.org/web/packages/oro.dicom/vignettes/dicom.pdf
// Ref: Part 5, Section 7.1.2 "Data Element Structure with Explicit VR"
case 'OB':
case 'OF':
case 'OW':
case 'SQ':
case 'UN':
bVrExplicit = true;
bLen4B = true;
break;
// FIXME:
// Ref: Part 5, Section 7.1.2 "Data Element Structure with Explicit VR"
case 'UT':
bVrExplicit = true;
bLen4B = true;
break;
default:
// FIXME:
// I have seen some documentation from others that say if VR is not
// known then it is implicit VR. However, this doesn't seem
// accurate to me since some implicit VRs could have length values
// that just happen to alias with a known VR code above!
bVrExplicit = false;
bLen4B = true; //FIXME
break;
}
// Force some known implicit VRs to be handled this way
if ((nTagGroup == 0xFFFE) && (nTagElement == 0xE000)) { bVrExplicit = false; bLen4B = true; }
if ((nTagGroup == 0xFFFE) && (nTagElement == 0xE00D)) { bVrExplicit = false; bLen4B = true; }
if ((nTagGroup == 0xFFFE) && (nTagElement == 0xE0DD)) { bVrExplicit = false; bLen4B = true; }
if (bVrExplicit) {
if (!bLen4B) {
// 2B length
nLen = m_pWBuf->BufX(nPos+6,2,true);
nOffset = 8;
} else {
// 4B length
// - Skip over 2B field (should be 0)
nLen = m_pWBuf->BufX(nPos+6,2,true);
ASSERT(nLen == 0);
nLen = m_pWBuf->BufX(nPos+8,4,true);
nOffset = 12;
}
} else {
// Implicit VR
strVR = _T("??");
// 4B length
nLen = m_pWBuf->BufX(nPos+4,4,true);
nOffset = 8;
}
// Handle Items & Sequences (Image Fragment?)
bTagIsJpeg = false;
bTagIsOffsetHdr = false;
if (!m_bJpegEncap) {
// Not in a delimited sequence
// FIXME: Handle special case of sequence of fragments with offset table (7FE0,0010)
// Ref: Part 7, Section A.4, Table A.4-1
if ((nTagGroup == 0x7FE0) && (nTagElement == 0x0010)) {
// Start of Pixel Data
if (nLen == 0xFFFFFFFF) {
// Undefined length. So expect encapsulated data with delimiter
// Expect we aren't already in encapsulated mode
ASSERT(m_bJpegEncap == false);
// Indicate we are now starting an encapsulation
m_bJpegEncap = true;
// Next tag should be offset header
m_bJpegEncapOffsetNext = true;
} else {
// FIXME: handle native type
ASSERT(false);
}
}
} else {
// We are in a sequence
if ((nTagGroup == 0xFFFE) && (nTagElement == 0xE000)) {
// Is this an offset header?
if (m_bJpegEncapOffsetNext) {
ASSERT(nTagGroup == 0xFFFE);
ASSERT(nTagElement == 0xE000);
// Clear next flag since we've handled it
m_bJpegEncapOffsetNext = false;
// This is an offset header
bTagIsOffsetHdr = true;
} else {
// Not an offset header, so process it as embedded JPEG
bTagIsJpeg = true;
}
} else if ((nTagGroup == 0xFFFE) && (nTagElement == 0xE0DD)) {
// Sequence delimiter
m_bJpegEncap = false;
}
}
if (bTagIsJpeg) {
nPosJpeg = nPos+8;
} else {
// Indicate not a JPEG tag
nPosJpeg = 0;
}
#ifdef DICOM_TAG_EXTENDED
// Extended tag info
if (bTagIsOffsetHdr) {
strTag.Format(_T("@ 0x%08X (%04X,%04X) VR=[%2s] Len=0x%08X [OffsetHdr] %s"),nPos,nTagGroup,nTagElement,strVR,nLen,strTagName);
} else if (bTagIsJpeg) {
strTag.Format(_T("@ 0x%08X (%04X,%04X) VR=[%2s] Len=0x%08X [JPEG] %s"),nPos,nTagGroup,nTagElement,strVR,nLen,strTagName);
} else {
strTag.Format(_T("@ 0x%08X (%04X,%04X) VR=[%2s] Len=0x%08X %s"),nPos,nTagGroup,nTagElement,strVR,nLen,strTagName);
}
#else
// Simple tag info
strTag.Format(_T("%s"),(LPCTSTR)strTagName);
#endif
// Assign struct
sTagDetail.nTagGroup = nTagGroup;
sTagDetail.nTagElement = nTagElement;
sTagDetail.strVR = strVR;
sTagDetail.nLen = nLen;
sTagDetail.nOffset = nOffset;
sTagDetail.bVrExplicit = bVrExplicit;
sTagDetail.bLen4B = bLen4B;
sTagDetail.bTagOk = bTagOk;
sTagDetail.strTag = strTag;
//sTagDetail.bValOk = bValOk;
//sTagDetail.strVal = strDesc;
sTagDetail.bTagIsJpeg = bTagIsJpeg;
sTagDetail.nPosJpeg = nPosJpeg;
//sTagDetail.bTagIsOffsetHdr = bTagIsOffsetHdr;
return true;
}
#if 0
bool CDecodeDicom::DecodeTagHeader(unsigned long nPos,CString &strTag,CString &strVR,unsigned &nLen,unsigned &nOffset,unsigned long &nPosJpeg)
{
unsigned nVR = 0;
CString strError;
unsigned nTagGroup;
unsigned nTagElement;
CString strTagName = _T("");
// Assign defaults
strTag = _T("");
strVR = _T("");
nLen = 0;
nOffset = 0;
// Find the tag
nTagGroup = m_pWBuf->BufX(nPos+0,2,true);
nTagElement = m_pWBuf->BufX(nPos+2,2,true);
bool bFoundTag;
unsigned nFldInd = 0;
bFoundTag = FindTag(nTagGroup,nTagElement,nFldInd);
if (!bFoundTag) {
// Check for group length entry
// FIXME:
// Reference: Part 5, Section 7.2
if (nTagElement == 0x0000) {
strTagName.Format(_T("Group Length (Group=%04X)"),nTagGroup);
} else {
strError.Format(_T("ERROR: Unknown Tag ID (%04X,%04X) @ 0x%08X"),nTagGroup,nTagElement,nPos);
m_pLog->AddLineErr(strError);
strTagName.Format(_T("??? (%04X,%04X)"),nTagGroup,nTagElement);
}
} else {
strTagName = asDicomTags[nFldInd].strTagName;
}
// Check for zeros in place of VR. This might hint at implicit VR
// FIXME
if (m_pWBuf->BufX(nPos+4,2,true) == 0) {
unsigned nBad=1;
}
strVR = m_pWBuf->BufReadStrn(nPos+4,2);
nVR = m_pWBuf->BufX(nPos+4,2,false);
// Value Representation (VR)
// Reference: Part 5, Section 6.2
bool bVrExplicit = false;
bool bVrLen4B = false;
switch (nVR) {
// Ref: Part 5, Section 7.1.2 "Data Element Structure with Explicit VR"
case 'AE':
case 'AS':
case 'AT':
case 'CS':
case 'DA':
case 'DS':
case 'DT':
case 'FL':
case 'FD':
case 'IS':
case 'LO':
case 'LT':
//case 'OB':
//case 'OF':
//case 'OW':
case 'PN':
case 'SH':
case 'SL':
//case 'SQ':
case 'SS':
case 'ST':
case 'TM':
case 'UI':
case 'UL':
//case 'UN':
case 'US':
//case 'UT':
bVrExplicit = true;
bVrLen4B = false;
break;
// Definition of VRs that have 4B length
// Reference (informal): http://cran.r-project.org/web/packages/oro.dicom/vignettes/dicom.pdf
// Ref: Part 5, Section 7.1.2 "Data Element Structure with Explicit VR"
case 'OB':
case 'OF':
case 'OW':
case 'SQ':
case 'UN':
bVrExplicit = true;
bVrLen4B = true;
break;
// FIXME:
// Ref: Part 5, Section 7.1.2 "Data Element Structure with Explicit VR"
case 'UT':
bVrExplicit = true;
bVrLen4B = true;
break;
default:
// FIXME:
// I have seen some documentation from others that say if VR is not
// known then it is implicit VR. However, this doesn't seem
// accurate to me since some implicit VRs could have length values
// that just happen to alias with a known VR code above!
bVrExplicit = false;
bVrLen4B = true; //FIXME
break;
}
// Force some known implicit VRs to be handled this way
if ((nTagGroup == 0xFFFE) && (nTagElement == 0xE000)) { bVrExplicit = false; bVrLen4B = true; }
if ((nTagGroup == 0xFFFE) && (nTagElement == 0xE00D)) { bVrExplicit = false; bVrLen4B = true; }
if ((nTagGroup == 0xFFFE) && (nTagElement == 0xE0DD)) { bVrExplicit = false; bVrLen4B = true; }
if (bVrExplicit) {
if (!bVrLen4B) {
// 2B length
nLen = m_pWBuf->BufX(nPos+6,2,true);
nOffset = 8;
} else {
// 4B length
// - Skip over 2B field (should be 0)
nLen = m_pWBuf->BufX(nPos+6,2,true);
ASSERT(nLen == 0);
nLen = m_pWBuf->BufX(nPos+8,4,true);
nOffset = 12;
}
} else {
// Implicit VR
strVR = _T("??");
// 4B length
nLen = m_pWBuf->BufX(nPos+4,4,true);
nOffset = 8;
}
// Handle Items & Sequences (Image Fragment?)
bool bTagIsJpeg = false;
bool bTagIsOffsetHdr = false;
if (!m_bJpegEncap) {
// Not in a delimited sequence
// FIXME: Handle special case of sequence of fragments with offset table (7FE0,0010)
// Ref: Part 7, Section A.4, Table A.4-1
if ((nTagGroup == 0x7FE0) && (nTagElement == 0x0010)) {
// Start of Pixel Data
if (nLen == 0xFFFFFFFF) {
// Undefined length. So expect encapsulated data with delimiter
// Expect we aren't already in encapsulated mode
ASSERT(m_bJpegEncap == false);
// Indicate we are now starting an encapsulation
m_bJpegEncap = true;
// Next tag should be offset header
m_bJpegEncapOffsetNext = true;
} else {
// FIXME: handle native type
ASSERT(false);
}
}
} else {
// We are in a sequence
if ((nTagGroup == 0xFFFE) && (nTagElement == 0xE000)) {
// Is this an offset header?
if (m_bJpegEncapOffsetNext) {
ASSERT(nTagGroup == 0xFFFE);
ASSERT(nTagElement == 0xE000);
// Clear next flag since we've handled it
m_bJpegEncapOffsetNext = false;
// This is an offset header
bTagIsOffsetHdr = true;
} else {
// Not an offset header, so process it as embedded JPEG
bTagIsJpeg = true;
}
} else if ((nTagGroup == 0xFFFE) && (nTagElement == 0xE0DD)) {
// Sequence delimiter
m_bJpegEncap = false;
}
}
#ifdef DICOM_TAG_EXTENDED
// Extended tag info
if (bTagIsOffsetHdr) {
strTag.Format(_T("@ 0x%08X (%04X,%04X) VR=[%2s] Len=0x%08X [OffsetHdr] %s"),nPos,nTagGroup,nTagElement,strVR,nLen,strTagName);
} else if (bTagIsJpeg) {
strTag.Format(_T("@ 0x%08X (%04X,%04X) VR=[%2s] Len=0x%08X [JPEG] %s"),nPos,nTagGroup,nTagElement,strVR,nLen,strTagName);
} else {
strTag.Format(_T("@ 0x%08X (%04X,%04X) VR=[%2s] Len=0x%08X %s"),nPos,nTagGroup,nTagElement,strVR,nLen,strTagName);
}
#else
// Simple tag info
strTag.Format(_T("%s"),strTagName);
#endif
if (bTagIsJpeg) {
nPosJpeg = nPos+8;
} else {
// Indicate not a JPEG tag
nPosJpeg = 0;
}
return true;
}
#endif
// Determine if the file is a DICOM
// If so, parse the headers. Generally want to start at start of file (nPos=0).
bool CDecodeDicom::DecodeDicom(unsigned long nPos,unsigned long nPosFileEnd,unsigned long &nPosJpeg)
{
unsigned nLen;
unsigned nOffset;
CString strTag;
CString strSig;
CString strVR;
m_bDicom = false;
// Skip File Preamble
nPos += 128;
// DICOM Prefix
strSig = m_pWBuf->BufReadStrn(nPos,4);
if (strSig == _T("DICM")) {
m_bDicom = true;
m_pLog->AddLine(_T(""));
m_pLog->AddLineHdr(_T("*** DICOM File Decoding ***"));
m_pLog->AddLine(_T("Decoding DICOM format..."));
m_pLog->AddLine(_T(""));
} else {
return false;
}
nPos+=4;
bool bDone = false;
// If we get here, then we have found the header signature
// FIXME: Handle proper termination!
if (nPos >= nPosFileEnd) {
bDone = true;
}
//bool bFoundJpeg = false;
unsigned long nPosJpegFound = 0;
tsTagDetail sTagDetail;
while (!bDone) {
sTagDetail.Reset();
GetTagHeader(nPos,sTagDetail);
// Fetch results
nLen = sTagDetail.nLen;
nPosJpegFound = sTagDetail.nPosJpeg;
strTag = sTagDetail.strTag;
strVR = sTagDetail.strVR;
nOffset = sTagDetail.nOffset;
// Advance the file position
nPos += sTagDetail.nOffset;
// Process the field
if (nLen == 0xFFFFFFFF) {
// Detect "invalid length"
// In these cases we are probably dependent upon delimiters
// so we don't have any real content in this tag.
//
// Example: E:\JPEG\JPEGsnoop Testcases\DICOM_samples\24-bit JPEG Lossy Color.dcm
// Offset: 0x508
nLen = 0;
}
if (nPosJpegFound != 0) {
// Only capture first image offset
if (nPosJpeg == 0) {
nPosJpeg = nPosJpegFound;
}
}
// See if this is an enumerated value
// If not, just report as hex for now
// Now determine enum value
CString strValTrunc = _T("");
CString strDesc = _T("");
bool bIsEnum = false;
strValTrunc = m_pWBuf->BufReadStrn(nPos,200);
bIsEnum = LookupEnum(sTagDetail.nTagGroup,sTagDetail.nTagElement,strValTrunc,strDesc);
if (bIsEnum) {
ReportFldStrEnc(1,sTagDetail.strTag,strDesc,strValTrunc);
} else {
ReportFldHex(1,strTag,nPos,nLen);
}
nPos += nLen;
if (nPos >= nPosFileEnd) {
bDone = true;
}
} // bDone
return true;
}
// Create indent string
// - Returns a sequence of spaces according to the indent level
CString CDecodeDicom::ParseIndent(unsigned nIndent)
{
CString strIndent = _T("");
for (unsigned nInd=0;nInd<nIndent;nInd++) {
strIndent += _T(" ");
}
return strIndent;
}
// Display a formatted string field
// - Report the value with the field name (strField) and current indent level (nIndent)
void CDecodeDicom::ReportFldStr(unsigned nIndent,CString strField,CString strVal)
{
CString strIndent;
CString strLine;
strIndent = ParseIndent(nIndent);
strLine.Format(_T("%s%-50s = \"%s\""),(LPCTSTR)strIndent,(LPCTSTR)strField,(LPCTSTR)strVal);
m_pLog->AddLine(strLine);
}
// Display a formatted string field with encoded value
// - Report the value with the field name (strField) and current indent level (nIndent)
void CDecodeDicom::ReportFldStrEnc(unsigned nIndent,CString strField,CString strVal,CString strEncVal)
{
CString strIndent;
CString strLine;
strIndent = ParseIndent(nIndent);
strLine.Format(_T("%s%-50s = %s \"%s\""),(LPCTSTR)strIndent,(LPCTSTR)strField,(LPCTSTR)strEncVal,(LPCTSTR)strVal);
m_pLog->AddLine(strLine);
}
// Locate a field ID in the constant Meta Tag array
//
// INPUT:
// - nTagGroup = Meta Tag (High)
// - nTagElement = Meta Tag (Low)
// OUTPUT:
// - nFldInd = Index into asDicomTags[] array
// RETURN:
// - Returns true if ID was found in array
// NOTE:
// - The constant struct array depends on DICOM_T_END as a terminator for the list
//
bool CDecodeDicom::FindTag(unsigned nTagGroup,unsigned nTagElement,unsigned &nFldInd)
{
unsigned nInd=0;
bool bDone=false;
bool bFound=false;
while (!bDone) {
if (asDicomTags[nInd].eTagType == DICOM_T_END) {
bDone = true;
} else {
unsigned nFoundTagH = asDicomTags[nInd].nTagGroup;
unsigned nFoundTagL = asDicomTags[nInd].nTagElement;
if ((nTagGroup == nFoundTagH) && (nTagElement == nFoundTagL)) {
// Matched
bDone = true;
bFound = true;
} else {
nInd++;
}
}
}
if (bFound) {
nFldInd = nInd;
}
return bFound;
}
//
// Display a formatted hexadecimal field
// - Report the value with the field name (strField) and current indent level (nIndent)
// - Depending on the length of the buffer, either report the hex value in-line with
// the field name or else start a new row and begin a hex dump
// - Also report the ASCII representation alongside the hex dump
//
// INPUT:
// - nIndent = Current indent level for reporting
// - strField = Field name
// - nPosStart = Field byte array file position start
// - nLen = Field byte array length
//
void CDecodeDicom::ReportFldHex(unsigned nIndent,CString strField,unsigned long nPosStart,unsigned nLen)
{
CString strIndent;
unsigned nByte;
CString strPrefix;
CString strByteHex;
CString strByteAsc;
CString strValHex;
CString strValAsc;
CString strLine;
strIndent = ParseIndent(nIndent);
if (nLen == 0) {
// Print out the header row, but no data will be shown
strLine.Format(_T("%s%-50s = "),(LPCTSTR)strIndent,(LPCTSTR)strField);
m_pLog->AddLine(strLine);
// Nothing to report, exit now
return;
} else if (nLen <= DC_HEX_MAX_INLINE) {
// Define prefix for row
strPrefix.Format(_T("%s%-50s = "),(LPCTSTR)strIndent,(LPCTSTR)strField);
} else {
#if 0
// Print out header row
strLine.Format(_T("%s%-50s ="),(LPCTSTR)strIndent,(LPCTSTR)strField);
m_pLog->AddLine(strLine);
// Define prefix for next row
//strPrefix.Format(_T("%s"),(LPCTSTR)strIndent);
strPrefix.Format(_T("%s%-50s "),(LPCTSTR)strIndent,_T(""));
#else
// Define prefix for row
//FIXME: Only report field on first row
strPrefix.Format(_T("%s%-50s = "),(LPCTSTR)strIndent,(LPCTSTR)strField);
#endif
}
// Build up the hex string
// Limit to DC_HEX_TOTAL bytes
unsigned nRowOffset = 0;
bool bDone = false;
unsigned nLenClip;
nLenClip = min(nLen,DC_HEX_TOTAL);
strValHex = _T("");
strValAsc = _T("");
while (!bDone) {
// Have we reached the end of the data we wish to display?
if (nRowOffset>=nLenClip) {
bDone = true;
} else {
// Reset the cumulative hex/ascii value strings
strValHex = _T("");
strValAsc = _T("");
// Build the hex/ascii value strings
for (unsigned nInd=0;nInd<DC_HEX_MAX_ROW;nInd++) {
if ((nRowOffset+nInd)<nLenClip) {
nByte = m_pWBuf->Buf(nPosStart+nRowOffset+nInd);
strByteHex.Format(_T("%02X "),nByte);
// Only display printable characters
if (isprint(nByte)) {
strByteAsc.Format(_T("%c"),nByte);
} else {
strByteAsc = _T(".");
}
} else {
// Pad out to end of row
strByteHex.Format(_T(" "));
strByteAsc = _T(" ");
}
// Build up the strings
strValHex += strByteHex;
strValAsc += strByteAsc;
}
// Generate the line with Hex and ASCII representations
strLine.Format(_T("%s | 0x%s | %s"),(LPCTSTR)strPrefix,(LPCTSTR)strValHex,(LPCTSTR)strValAsc);
m_pLog->AddLine(strLine);
// Now increment file offset
nRowOffset += DC_HEX_MAX_ROW;
}
}
// If we had to clip the display length, then show ellipsis now
if (nLenClip < nLen) {
strLine.Format(_T("%s | ..."),(LPCTSTR)strPrefix);
m_pLog->AddLine(strLine);
}
}
// Display a formatted enumerated type field
// - Look up enumerated constant (nVal) for the given field (eEnumField)
// - Report the value with the field name (strField) and current indent level (nIndent)
void CDecodeDicom::ReportFldEnum(unsigned nIndent,CString strField,unsigned nTagGroup,unsigned nTagElement,CString strVal)
{
CString strIndent;
CString strDesc;
CString strLine;
bool bFound;
strIndent = ParseIndent(nIndent);
bFound = LookupEnum(nTagGroup,nTagElement,strVal,strDesc);
if (bFound) {
strLine.Format(_T("%s%-50s = %s"),(LPCTSTR)strIndent,(LPCTSTR)strField,(LPCTSTR)strDesc);
m_pLog->AddLine(strLine);
} else {
ASSERT(false);
}
}
// Look up the enumerated constant (nVal) for the specified field (eEnumField)
// - Returns "" if the field wasn't found
bool CDecodeDicom::LookupEnum(unsigned nTagGroup,unsigned nTagElement,CString strVal,CString &strDesc)
{
// Find the enum value
bool bFound = false;
bool bDone = false;
unsigned nEnumInd=0;
while (!bDone) {
if ( (asTagSpecial[nEnumInd].nTagGroup == nTagGroup) && (asTagSpecial[nEnumInd].nTagElement == nTagElement) ) {
if (asTagSpecial[nEnumInd].strVal == strVal) {
bDone = true;
bFound = true;
strDesc = asTagSpecial[nEnumInd].strDefinition;
}
}
if (asTagSpecial[nEnumInd].strVal == _T("")) {
bDone = true;
}
if (!bDone) {
nEnumInd++;
}
}
return bFound;
}
// ===============================================================================
// CONSTANTS
// ===============================================================================