-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreaddcmfile.cpp
executable file
·688 lines (620 loc) · 21.6 KB
/
creaddcmfile.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
#include "creaddcmfile.h"
#include "dcdeftag.h"
#include "dcdatset.h"
#include "dcelem.h"
#include "dcfilefo.h"
#include "dcuid.h"
#include "dcrledrg.h"
#include "dcmetinf.h"
#include "djdecode.h"
#include "dcjson.h"
#include <QtXml>
CReadDcmFile::CReadDcmFile()
: m_nMultiFrameCount(1)
, m_pQPixmap(NULL)
, m_pDcmFile(NULL)
, m_pAnnotationInfo(NULL)
{
}
CReadDcmFile::~CReadDcmFile()
{
if (m_pQPixmap!=NULL)
{
delete [] m_pQPixmap;
m_pQPixmap = NULL;
}
if(m_pDcmFile!=NULL)
{
delete m_pDcmFile;
m_pDcmFile = NULL;
}
if (m_pAnnotationInfo!=NULL)
{
delete m_pAnnotationInfo;
m_pAnnotationInfo = NULL;
}
}
bool CReadDcmFile::ReadFile(const char* strFileName)
{
if (m_pDcmFile!=NULL)
{
delete m_pDcmFile;
}
if (m_pAnnotationInfo!=NULL)
{
delete m_pAnnotationInfo;
m_pAnnotationInfo = NULL;
}
m_pDcmFile = new DcmFileFormat();
OFCondition result = m_pDcmFile->loadFile(strFileName);
if (result.bad())
{
delete m_pDcmFile;
m_pDcmFile = NULL;
return false;
}
DcmDataset* dataset = m_pDcmFile->getDataset();
if (dataset == NULL)
{
delete m_pDcmFile;
m_pDcmFile = NULL;
return false;
}
DcmMetaInfo* meta = m_pDcmFile->getMetaInfo();
DcmElement *element = NULL;
OFString transferSyntaxUID;
result = meta->findAndGetElement(DCM_TransferSyntaxUID, element);
if (result.bad() || element == NULL)
{
// assert(false);
// return false;
}
else
{
element->getOFString(transferSyntaxUID, 0);
}
if (transferSyntaxUID.compare(UID_RLELosslessTransferSyntax)==0)
{
DcmRLEDecoderRegistration::registerCodecs();
result = dataset->chooseRepresentation(EXS_LittleEndianExplicit, NULL);
DcmRLEDecoderRegistration::cleanup();
if (result.bad())
{
delete m_pDcmFile;
m_pDcmFile = NULL;
return false;
}
}
else if ( transferSyntaxUID.compare(UID_JPEGProcess14SV1TransferSyntax)==0
|| transferSyntaxUID.compare(UID_JPEGProcess1TransferSyntax)==0 )
{
DJDecoderRegistration::registerCodecs(EDC_lossyOnly);
result = dataset->chooseRepresentation(EXS_LittleEndianExplicit, NULL);
DJDecoderRegistration::cleanup();
if (result.bad())
{
delete m_pDcmFile;
m_pDcmFile = NULL;
return false;
}
}
//sample per pixel
result = dataset->findAndGetElement(DCM_SamplesPerPixel, element);
if (result.bad() || element == NULL)
{
assert(false);
delete m_pDcmFile;
m_pDcmFile = NULL;
return false;
}
Uint16 samplesPerPixel(0);
element->getUint16(samplesPerPixel);
result = dataset->findAndGetElement(DCM_PhotometricInterpretation, element);
if (result.bad() || element==NULL)
{
assert(false);
delete m_pDcmFile;
m_pDcmFile = NULL;
return false;
}
OFString photometricInterpretation;
element->getOFString(photometricInterpretation,0);
result = dataset->findAndGetElement(DCM_Columns, element);
if (result.bad() || element == NULL)
{
assert(false);
delete m_pDcmFile;
m_pDcmFile = NULL;
return false;
}
Uint16 nColumns(0);
element->getUint16(nColumns);
result = dataset->findAndGetElement(DCM_Rows, element);
if (result.bad() || element == NULL)
{
assert(false);
delete m_pDcmFile;
m_pDcmFile = NULL;
return false;
}
Uint16 nRows(0);
element->getUint16(nRows);
result = dataset->findAndGetElement(DCM_BitsAllocated, element);
if (result.bad() || element == NULL)
{
assert(false);
delete m_pDcmFile;
m_pDcmFile = NULL;
return false;
}
Uint16 nBitsAllocated(0);
element->getUint16(nBitsAllocated);
result = dataset->findAndGetElement(DCM_BitsStored, element);
if (result.bad() || element == NULL)
{
assert(false);
delete m_pDcmFile;
m_pDcmFile = NULL;
return false;
}
Uint16 nBitsStored(0);
element->getUint16(nBitsStored);
result = dataset->findAndGetElement(DCM_HighBit, element);
if (result.bad() || element == NULL)
{
assert(false);
delete m_pDcmFile;
m_pDcmFile = NULL;
return false;
}
Uint16 nHighBit(0);
element->getUint16(nHighBit);
result = dataset->findAndGetElement(DCM_PixelRepresentation, element);
if (result.good() && element != NULL)
{
Uint16 nPixelRepresentation(0);
element->getUint16(nPixelRepresentation);
}
Uint16 planarConfiguration(0);
result = dataset->findAndGetElement(DCM_PlanarConfiguration, element);
if (result.good() && element != NULL)
{
element->getUint16(planarConfiguration);
}
result = dataset->findAndGetElement(DCM_PixelAspectRatio, element);
if (result.good() && element != NULL)
{
Uint16 nPixelAspectRatio(0);
element->getUint16(nPixelAspectRatio);
}
Sint16 smallestImagePixelValue(0x7FFF);
result = dataset->findAndGetElement(DCM_SmallestImagePixelValue, element);
if (result.good() && element != NULL)
{
element->getSint16(smallestImagePixelValue);
}
Sint16 largestImagePixelValue(0);
result = dataset->findAndGetElement(DCM_LargestImagePixelValue, element);
if (result.good() && element != NULL)
{
element->getSint16(largestImagePixelValue);
}
Uint16 pixelRepresentation(0);
result = dataset->findAndGetElement(DCM_PixelRepresentation, element);
if (result.good() && element != NULL)
{
element->getUint16(pixelRepresentation);
}
unsigned short* pPaletteColorLUTDescrip = NULL;
result = dataset->findAndGetElement(DCM_RedPaletteColorLookupTableDescriptor, element);
if (result.good() && element != NULL)
{
element->getUint16Array(pPaletteColorLUTDescrip);
}
//Green and Blue equal to red;
unsigned short *pPaletteColorLUT_Red(NULL), *pPaletteColorLUT_Green(NULL), *pPaletteColorLUT_Blue(NULL);
result = dataset->findAndGetElement(DCM_RedPaletteColorLookupTableData, element);
if (result.good() && element != NULL)
{
element->getUint16Array(pPaletteColorLUT_Red);
}
result = dataset->findAndGetElement(DCM_GreenPaletteColorLookupTableData, element);
if (result.good() && element != NULL)
{
element->getUint16Array(pPaletteColorLUT_Green);
}
result = dataset->findAndGetElement(DCM_BluePaletteColorLookupTableData, element);
if (result.good() && element != NULL)
{
element->getUint16Array(pPaletteColorLUT_Blue);
}
//Modality LUT
bool isRescaleIntercept(true);
DcmSequenceOfItems *sequence = NULL;
result = dataset->findAndGetSequence(DCM_ModalityLUTSequence, sequence);
if (result.good() && element != NULL)
{
isRescaleIntercept = false;
}
float rescaleIntercept(0.0);
result = dataset->findAndGetElement(DCM_RescaleIntercept, element);
if (result.good() && element != NULL)
{
OFString strValue("");
element->getOFString(strValue, 0);
rescaleIntercept = atof(strValue.c_str());
}
float rescaleSlope(1.0);
result = dataset->findAndGetElement(DCM_RescaleSlope, element);
if (result.good() && element != NULL)
{
OFString strValue("");
element->getOFString(strValue, 0);
rescaleSlope = atof(strValue.c_str());
}
OFString rescaleType("");
result = dataset->findAndGetElement(DCM_RescaleType, element);
if (result.good() && element != NULL)
{
element->getOFString(rescaleType, 0);
}
//End Modality LUT
//VOI LUT
bool isVOILUTSeq(false);
result = dataset->findAndGetSequence(DCM_VOILUTSequence, sequence);
if (result.good() && sequence != NULL)
{
isVOILUTSeq = true;
}
float windowCenter(0.0);
result = dataset->findAndGetElement(DCM_WindowCenter, element);
if (result.good() && element != NULL)
{
isVOILUTSeq = false;
OFString strValue("");
element->getOFString(strValue, 0);
windowCenter = atof(strValue.c_str());
}
float windowWidth(0.0);
result = dataset->findAndGetElement(DCM_WindowWidth, element);
if (result.good() && element != NULL)
{
OFString strValue("");
element->getOFString(strValue, 0);
windowWidth = atof(strValue.c_str());
}
//End VOI LUT
//Multi-frame Module. PS3.3 2017e, C.7.6.6, P470.
result = dataset->findAndGetElement(DCM_NumberOfFrames, element);
if (result.good() && element != NULL)
{
OFString strValue("");
element->getOFString(strValue, 0);
m_nMultiFrameCount = atoi(strValue.c_str());
assert(m_nMultiFrameCount>0);
}
DcmTagKey frameIncrementPointer;
result = dataset->findAndGetElement(DCM_FrameIncrementPointer, element);
if (result.good() && element != NULL)
{
Uint16* tag = NULL;
element->getUint16Array(tag);
frameIncrementPointer.setGroup(tag[0]);
frameIncrementPointer.setElement(tag[1]);
}
//End Multi-frame Module.
result = dataset->findAndGetElement(DCM_PixelData, element);
if (result.bad() || element == NULL)
{
assert(false);
delete m_pDcmFile;
m_pDcmFile = NULL;
return false;
}
Uint8* pPixSrcBuf;
int pixLen(0);
if (nBitsAllocated==8)
{
result = element->getUint8Array(pPixSrcBuf);
pixLen = element->getLength();//num of bit
}
else if (nBitsAllocated==16)
{
Uint16* pSrcTemp(0);
result = element->getUint16Array(pSrcTemp);
pixLen = element->getLength();//num of byte
pPixSrcBuf = (Uint8*)pSrcTemp;
}
else
{
assert(false);
delete m_pDcmFile;
m_pDcmFile = NULL;
return false;
}
if (pixLen != m_nMultiFrameCount * nColumns * nRows * samplesPerPixel * nBitsAllocated/8)
{
assert(false);
delete m_pDcmFile;
m_pDcmFile = NULL;
return false;
}
if( m_pQPixmap!=NULL )
{
delete [] m_pQPixmap;
m_pQPixmap = NULL;
}
m_pQPixmap = new QPixmap[m_nMultiFrameCount];
for (int frame=0; frame<m_nMultiFrameCount; frame++)
{
int* pSrcIntValue = new int[nRows*nColumns];//Save int value of src
memset(pSrcIntValue, 0, nRows*nColumns*sizeof(int));
//get int value for per pixel; and Find min and max from all samples.
for (int r=0; r<nRows; r++)
{
for (int c=0; c<nColumns; c++)
{
assert(nBitsAllocated<32 && nBitsStored<32);
int& srcValue = pSrcIntValue[r*nColumns+c];
if (samplesPerPixel==1)
{
Uint8* valueAddr = pPixSrcBuf + frame*nColumns*nRows*samplesPerPixel*nBitsAllocated/8
+ (r*nColumns+c)*samplesPerPixel*nBitsAllocated/8;
//
if (pixelRepresentation==0)
{
memcpy(&srcValue, valueAddr, samplesPerPixel*nBitsAllocated/8);
}
else if (pixelRepresentation==1)
{
if (samplesPerPixel==1)
{
if (nBitsAllocated==8)
{
Sint8 sValue = (*(Sint8*)valueAddr) & (~(0xFF<<nBitsStored));
srcValue = sValue;
}
else if (nBitsAllocated == 16)
{
short sValue = (*((short*)valueAddr)) & (~(0xFFFF<<nBitsStored));
srcValue = sValue;
}
else
{
memcpy(&srcValue, valueAddr, samplesPerPixel*nBitsAllocated/8);
srcValue = srcValue & (~(0xFFFFFFFFFFFFFFFF<<nBitsStored));
}
}//end if (samplesPerPixel==1)
}
if (!isVOILUTSeq && windowWidth<0.1 && (smallestImagePixelValue>srcValue||largestImagePixelValue<srcValue))
{
smallestImagePixelValue = srcValue<smallestImagePixelValue?srcValue:smallestImagePixelValue;
largestImagePixelValue = srcValue>largestImagePixelValue?srcValue:largestImagePixelValue;
}
}
else if (samplesPerPixel==3)
{
for (int sample=0; sample<samplesPerPixel; sample++)
{
if (planarConfiguration==0)//RGBRGBRGB...RGB
{
Uint8* rAddr = pPixSrcBuf + frame*nColumns*nRows*samplesPerPixel*nBitsAllocated/8
+ (r*nColumns+c)*samplesPerPixel*nBitsAllocated/8;
srcValue |= (*rAddr)<<16;
Uint8* gAddr = rAddr + nBitsAllocated/8;
srcValue |= (*gAddr)<<8;
Uint8* bAddr = gAddr + nBitsAllocated/8;
srcValue |= (*bAddr);
srcValue |= (0xFF<<24);
}
else if (planarConfiguration==1)//RRR...RGGG...GBBB...B
{
srcValue = 0xFF<<24;
Uint8* rAddr = pPixSrcBuf + frame*nColumns*nRows*nBitsAllocated/8
+ (r*nColumns+c)*nBitsAllocated/8;
srcValue |= (*rAddr)<<16;
Uint8* gAddr = rAddr + nColumns*nRows*nBitsAllocated/8;
srcValue |= (*gAddr)<<8;
Uint8* bAddr = gAddr + nColumns*nRows*nBitsAllocated/8;
srcValue |= (*bAddr);
}
}
}//else if (samplesPerPixel == 3)
}
}
uchar* pPixDstData = new uchar[nRows*nColumns];
memset(pPixDstData, 0, nRows*nColumns*sizeof(uchar));
QImage* img(NULL);
if ( (strcmp(photometricInterpretation.c_str(), "MONOCHROME1")==0)
|| (strcmp(photometricInterpretation.c_str(), "MONOCHROME2")==0) )
{
for (int r=0; r<nRows; r++)
{
for (int c=0; c<nColumns; c++)
{
int &srcValue = pSrcIntValue[r*nColumns+c];
//Modality LUT only use for grayscale
int modalityLUTValue(0);
if (isRescaleIntercept)
{
modalityLUTValue = rescaleSlope * srcValue + rescaleIntercept;
}
else
{
}
//VOI LUT only use for MONO1 or MONO2. ps3.3 2017e, C11.2.1.2.2 P1198.
Sint16 yMin(0), yMax(255);
Uint8 VOILUTValue(0);
if (isVOILUTSeq)
{
}
else
{
if (windowWidth<1 && (smallestImagePixelValue<yMin || largestImagePixelValue>yMax))
{
//if not def windowWidth/windowCenter, and pixel value not in range of yMin and yMax.
windowCenter = (smallestImagePixelValue+largestImagePixelValue)/2.0;
windowWidth = largestImagePixelValue-smallestImagePixelValue;
}
if (windowWidth>1)
{
if ( modalityLUTValue <= (windowCenter-0.5 - (windowWidth-1)/2.0) )
{
VOILUTValue = yMin;
}
else if( modalityLUTValue>(windowCenter-0.5 + (windowWidth-1)/2.0))
{
VOILUTValue = yMax;
}
else
{
VOILUTValue = ((modalityLUTValue-(windowCenter-0.5))/(windowWidth-1)+0.5)*(yMax-yMin) + yMin;
}
}
else
{
VOILUTValue = modalityLUTValue;
}
}//not VOI LUT Sequence
if (strcmp(photometricInterpretation.c_str(), "MONOCHROME1")==0)
{
VOILUTValue = yMax - VOILUTValue;
}
pPixDstData[r*nColumns+c] = VOILUTValue;
}
}
img = new QImage(pPixDstData, (int)nColumns, (int)nRows, QImage::Format_Grayscale8);
}
else if (strcmp(photometricInterpretation.c_str(), "RGB")==0)
{
img = new QImage((Uint8*)pSrcIntValue, (int)nColumns, (int)nRows, QImage::Format_ARGB32);
}
else if (strcmp(photometricInterpretation.c_str(), "PALETTE COLOR")==0)
{
for (int r=0; r<nRows; r++)
{
for (int c=0; c<nColumns; c++)
{
int &srcValue = pSrcIntValue[r*nColumns+c];
if ((pPaletteColorLUTDescrip[0]==0) || (srcValue < pPaletteColorLUTDescrip[0]))
{
if (srcValue<pPaletteColorLUTDescrip[1])
{
srcValue = pPaletteColorLUTDescrip[1];
}
unsigned short red = pPaletteColorLUT_Red[srcValue];
red = (float)red/65535.0f*255.0f;
unsigned short green = pPaletteColorLUT_Green[srcValue];
green = (float)green/65535.0f*255.0f;
unsigned short blue = pPaletteColorLUT_Blue[srcValue];
blue = (float)blue/65535.0f*255.0f;
srcValue = (0xFF<<24) | ((red&0xFF) << 16) | ((green&0xFF) << 8) | (blue&0xFF);
}
}
}
img = new QImage((Uint8*)pSrcIntValue, (int)nColumns, (int)nRows, QImage::Format_ARGB32);
}
QPixmap file = QPixmap::fromImage(*img);
m_pQPixmap[frame] = file;
delete pSrcIntValue;
pSrcIntValue = NULL;
delete pPixDstData;
pPixDstData = NULL;
}//frame loop
ReadAnnotationInfo();
m_pAnnotationInfo->nWindowCenter = windowCenter<1? 128 : windowCenter;
m_pAnnotationInfo->nWindowWidth = windowWidth<1? 256 : windowWidth;
return true;
}
bool CReadDcmFile::ReadAnnotationInfo()
{
if (m_pAnnotationInfo==NULL)
{
m_pAnnotationInfo=new stAnnotationInfo();
}
DcmElement* element = NULL;
DcmDataset* dataset = m_pDcmFile->getDataset();
OFCondition result = dataset->findAndGetElement(DCM_PatientName, element);
if (result.good() && element != NULL)
{
element->getOFString(m_pAnnotationInfo->strPatientName, 0);
}
result = dataset->findAndGetElement(DCM_PatientID, element);
if (result.good() && element!=NULL)
{
element->getOFString(m_pAnnotationInfo->strPatientID, 0);
}
result = dataset->findAndGetElement(DCM_PatientSex, element);
if (result.good() && element!=NULL)
{
element->getOFString(m_pAnnotationInfo->strPatientSex, 0);
}
result = dataset->findAndGetElement(DCM_StudyID, element);
if (result.good() && element!=NULL)
{
element->getOFString(m_pAnnotationInfo->strStudyID, 0);
}
result = dataset->findAndGetElement(DCM_SeriesNumber, element);
if (result.good() && element!=NULL)
{
element->getOFString(m_pAnnotationInfo->strSeriesNum, 0);
}
result = dataset->findAndGetElement(DCM_StudyDate, element);
if (result.good() && element!=NULL)
{
element->getOFString(m_pAnnotationInfo->strStudyDate, 0);
}
result = dataset->findAndGetElement(DCM_StudyTime, element);
if (result.good() && element!=NULL)
{
element->getOFString(m_pAnnotationInfo->strStudyTime, 0);
}
result = dataset->findAndGetElement(DCM_NumberOfFrames, element);
if (result.good() && element!=NULL)
{
string strFrameCount;
element->getOFString(strFrameCount, 0);
m_pAnnotationInfo->nFrameCount = atoi(strFrameCount.c_str());
}
result = dataset->findAndGetElement(DCM_WindowCenter, element);
if (result.good() && element!=NULL)
{
Sint32 nWC;
element->getSint32(nWC);
m_pAnnotationInfo->nWindowCenter = nWC;
}
result = dataset->findAndGetElement(DCM_WindowWidth, element);
if (result.good() && element!=NULL)
{
Sint32 nWW;
element->getSint32(nWW);
m_pAnnotationInfo->nWindowWidth = nWW;
}
return true;
}
ushort CReadDcmFile::GetFrameCount()
{
return m_nMultiFrameCount;
}
QPixmap* CReadDcmFile::GetPixmap(ushort nFrame)
{
if (m_pQPixmap == NULL)
{
return NULL;
}
return &(m_pQPixmap[nFrame]);
}
bool CReadDcmFile::WriteXML(const char* strFileName)
{
if (m_pDcmFile==NULL)
{
return false;
}
std::ofstream fstream(strFileName);
if (fstream.good())
{
OFCondition result = m_pDcmFile->writeXML(fstream);
return result.good();
}
return false;
}