-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathvtkDICOMReader.h
546 lines (459 loc) · 17.9 KB
/
vtkDICOMReader.h
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
/*=========================================================================
Program: DICOM for VTK
Copyright (c) 2012-2024 David Gobbi
All rights reserved.
See Copyright.txt or http://dgobbi.github.io/bsd3.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/**
* \class vtkDICOMReader
* \brief Read DICOM image files.
*
* This class reads a series of DICOM files into a vtkImageData object,
* and also provides access to the DICOM meta data for each file.
*/
#ifndef vtkDICOMReader_h
#define vtkDICOMReader_h
#include "vtkImageReader2.h"
#include "vtkDICOMModule.h" // For export macro
#include "vtkDICOMConfig.h" // For configuration details
#include "vtkDICOMCharacterSet.h" // For character sets
// Declare VTK classes within VTK's optional namespace
#if defined(VTK_ABI_NAMESPACE_BEGIN)
VTK_ABI_NAMESPACE_BEGIN
#endif
class vtkIntArray;
class vtkTypeInt64Array;
class vtkStringArray;
class vtkMatrix4x4;
class vtkMedicalImageProperties;
#if defined(VTK_ABI_NAMESPACE_BEGIN)
VTK_ABI_NAMESPACE_END
#endif
class vtkDICOMMetaData;
class vtkDICOMParser;
class vtkDICOMSliceSorter;
// For compatibility with VTK 7.0 and earlier
#ifndef VTK_TYPE_BOOL_TYPEDEFED
#define VTK_TYPE_BOOL_TYPEDEFED
typedef int vtkTypeBool;
#endif
//----------------------------------------------------------------------------
class VTKDICOM_EXPORT vtkDICOMReader : public vtkImageReader2
{
public:
vtkTypeMacro(vtkDICOMReader, vtkImageReader2);
//! Static method for construction.
static vtkDICOMReader *New();
//! Print information about this object.
void PrintSelf(ostream& os, vtkIndent indent) VTK_DICOM_OVERRIDE;
//@{
//! Valid extensions for this file type.
const char* GetFileExtensions() VTK_DICOM_OVERRIDE {
return ".dcm .dc"; }
//! Return a descriptive name that might be useful in a GUI.
const char* GetDescriptiveName() VTK_DICOM_OVERRIDE {
return "DICOM"; }
//! Return true if this reader can read the given file.
int CanReadFile(const char* filename) VTK_DICOM_OVERRIDE;
//@}
//@{
//! Set the Stack ID of the stack to load, for named stacks.
/*!
* If the series has multiple stacks, then by default the reader
* will only load the first stack. This method allows you to select
* a different stack, if you know the DICOM StackID for the stack.
*/
void SetDesiredStackID(const char *stackId);
const char *GetDesiredStackID() { return this->DesiredStackID; }
//! Get a list of the stacks that are present in the input files.
/*!
* A stack is a contiguous array of slices that form a volume.
*/
vtkStringArray *GetStackIDs() { return this->StackIDs; }
//@}
//@{
//! Get an array that converts slice index to input file index.
/*!
* If the reader has generated scalar components, then this will
* be a two-dimensional array and calling array->GetComponent(i,j)
* will return the file index for slice i and scalar component j
* for monochrome images, or for slice i and scalar component 3*j
* for RGB images (or more precisely, at scalar component N*j where
* N is the SamplesPerPixel value from the DICOM metadata). If the
* data has just one component, then use j=0. If you used SetFileNames()
* to provide a list of files to the reader, then use this array to
* find out which file provided which slice, or to index into the
* MetaData object to get the metadata for a particular slice.
*/
vtkIntArray *GetFileIndexArray() { return this->FileIndexArray; }
//! Get an array that converts slice index to frame index.
/*!
* The purpose of this array is to identify individual frames in
* multi-frame DICOM files. The dimensions of this array are identical
* to the FileIndexArray. Use FileIndexArray to identify the file,
* then use FrameIndexArray to identify the frame within that file.
*/
vtkIntArray *GetFrameIndexArray() { return this->FrameIndexArray; }
//@}
//@{
//! Get the meta data for the DICOM files.
/*!
* The Get() method of vtkDICOMMataData takes optional file and frame
* indices, which specify the file and the frame within that file to
* get the attribute from. If you have a slice index rather than a
* file index and frame index, then use the FileIndexArray and
* FrameIndexArray to convert the slice index into file and frame indices.
*/
vtkDICOMMetaData *GetMetaData() { return this->MetaData; }
//@}
//@{
//! Set the character set to use if SpecificCharacterSet is missing.
/*!
* Some DICOM files do not list a SpecificCharacterSet attribute, but
* nevertheless use a non-ASCII character encoding. This method can be
* used to specify the character set in absence of SpecificCharacterSet.
* This method will not take effect unless is is called before the image
* is read. If SpecificCharacterSet is present, the default will not
* override it unless OverrideCharacterSet is true.
*/
void SetDefaultCharacterSet(vtkDICOMCharacterSet cs);
vtkDICOMCharacterSet GetDefaultCharacterSet() {
return this->DefaultCharacterSet; }
//! Override the value stored in SpecificCharacterSet.
/*!
* This method can be used if the SpecificCharacterSet attribute of a
* file is incorrect. It overrides the SpecificCharacterSet with the
* DefaultCharacterSet.
*/
vtkSetMacro(OverrideCharacterSet, bool);
vtkBooleanMacro(OverrideCharacterSet, bool);
bool GetOverrideCharacterSet() {
return this->OverrideCharacterSet; }
//@}
//@{
//! If the files have been pre-sorted, the sorting can be disabled.
vtkGetMacro(Sorting, int);
vtkSetMacro(Sorting, int);
vtkBooleanMacro(Sorting, int);
//@}
//@{
//! Set a custom sorter to be used to sort files and frames into slices.
/*!
* The default sorter uses the attributes "ImagePositionPatient" and
* "ImageOrientationPatient" to spatially arrange the slices.
*/
void SetSorter(vtkDICOMSliceSorter *sorter);
vtkDICOMSliceSorter *GetSorter() { return this->Sorter; }
//@}
//@{
//! Read the time dimension as scalar components (default: Off).
/*!
* If this is on, then each time point will be stored as a scalar
* component in the image data. If the data has both a time dimension
* and a vector dimension, then the number of components will be the
* product of these two dimensions, i.e. the components will store
* a sequence of vectors, one vector for each time point.
*/
vtkGetMacro(TimeAsVector, int);
vtkSetMacro(TimeAsVector, int);
vtkBooleanMacro(TimeAsVector, int);
//@}
//@{
//! Get the time dimension if the DICOM series has one.
int GetTimeDimension() { return this->TimeDimension; }
double GetTimeSpacing() { return this->TimeSpacing; }
//@}
//@{
//! Set the desired time index (set to -1 for all).
vtkSetMacro(DesiredTimeIndex, int);
vtkGetMacro(DesiredTimeIndex, int);
//@}
//@{
//! Turn off automatic conversion of YBR images to RGB.
/*!
* By default, YBR images are always converted to RGB (though the
* photometric interpretation in the metadata will remain the same).
*/
vtkGetMacro(AutoYBRToRGB, int);
vtkSetMacro(AutoYBRToRGB, int);
vtkBooleanMacro(AutoYBRToRGB, int);
//@}
//@{
//! Turn off automatic rescaling of stored pixel values.
/*!
* By default, if the RescaleSlope and RescaleIntercept attributes
* are present in the DICOM data set, they will be used to rescale
* stored pixel values according to the DICOM modality LUT operation.
* The resulting values will be stored as 16-bit integers if possible,
* but single-precision floats will be used if the potential output
* range is greater than can be stored in 16 bits, or if either the
* RescaleSlope or the RescaleIntercept is not an integer.
* If AutoRescale is off, then the data is not rescaled.
*/
vtkGetMacro(AutoRescale, int);
vtkSetMacro(AutoRescale, int);
vtkBooleanMacro(AutoRescale, int);
//@}
//@{
//! Get the slope and intercept for rescaling the scalar values.
/*!
* If AutoRescale in On (which is the default), then GetRescaleSlope()
* will return 1 and GetRescaleIntercept() will return zero. Otherwise,
* they will return the RescaleSlope and RescaleIntercept for the data
* set. Use the equation v = u*RescaleSlope + RescaleIntercept.
*
* Note that the returned values might not be valid if these attributes
* are not the same for all slices in the series. Use the meta data to
* retrieve the per-slice slope and intercept.
*/
double GetRescaleSlope() { return this->RescaleSlope; }
double GetRescaleIntercept() { return this->RescaleIntercept; }
//@}
//@{
//! Get a matrix to place the image within DICOM patient coords.
/*!
* This matrix is constructed from the ImageOrientationPatient
* and ImagePositionPatient meta data attributes. See the
* SetMemoryRowOrder method for additional information.
*/
vtkMatrix4x4 *GetPatientMatrix() { return this->PatientMatrix; }
//@}
//@{
//! Get the overlay.
/*!
* The extent of the overlay will be the same as the main image.
* For multiple overlays, each overlay will be stored in a different
* bit. An 8-bit image will be used if there are eight or fewer
* overlays, and an unsigned 16-bit image will be used if there are
* more than eight overlays.
*/
vtkImageData *GetOverlayOutput();
vtkAlgorithmOutput *GetOverlayOutputPort();
void SetOverlayOutput(vtkImageData *data);
//! Returns true if any overlays are present.
bool HasOverlay() { return (this->OverlayBitfield != 0); }
//! Returns a bitfield that indicates which overlays are present.
unsigned short GetOverlayBitfield() { return this->OverlayBitfield; }
//@}
//@{
//! Get a MedicalImageProperties object for this file.
vtkMedicalImageProperties *GetMedicalImageProperties();
//@}
//! Enumeration for top-down vs. bottom-up ordering.
enum RowOrder { FileNative, TopDown, BottomUp };
//@{
//! Set the ordering of the image rows in memory.
/*!
* If the order is BottomUp (which is the default) then
* the images will be flipped when they are read from disk.
* The native orientation of DICOM images is top-to-bottom.
*/
void SetMemoryRowOrder(int order);
void SetMemoryRowOrderToFileNative() {
this->SetMemoryRowOrder(FileNative); }
void SetMemoryRowOrderToTopDown() {
this->SetMemoryRowOrder(TopDown); }
void SetMemoryRowOrderToBottomUp() {
this->SetMemoryRowOrder(BottomUp); }
int GetMemoryRowOrder() { return this->MemoryRowOrder; }
const char *GetMemoryRowOrderAsString();
//@}
//@{
//! Set the scalar type of the output.
/*!
* The default value is -1, which means that the input scalar type will
* be used to set the output scalar type (accounting for any type
* changes required by AutoRescale). Allowed output scalar types are
* VTK_SIGNED_CHAR, VTK_UNSIGNED_CHAR, VTK_SHORT, VTK_UNSIGNED_SHORT,
* VTK_INT, VTK_UNSIGNED_INT, VTK_FLOAT, or VTK_DOUBLE. The output
* data values will be clamped to the limits of the output type.
*/
vtkSetMacro(OutputScalarType, int);
vtkGetMacro(OutputScalarType, int);
//@}
#ifndef __WRAP__
//@{
using Superclass::Update;
//! Update both the image and, if present, the overlay
void Update() VTK_DICOM_OVERRIDE;
//@}
#endif
//@{
//! Control registration of image codecs (DCMTK-specific).
/*!
* If the reader has been compiled with DCMTK support, RegisterCodecs()
* allows manual registration of the DCMTK codecs by the application.
* If you do not call this method, the reader will automatically register
* the codecs when it is instantiated. The registration is internally
* guarded by a refererence count, and every RegisterCodecs() call must
* eventually be matched by an UnRegisterCodecs() call.
*/
static void RegisterCodecs();
static void UnRegisterCodecs();
//@}
protected:
vtkDICOMReader();
~vtkDICOMReader() VTK_DICOM_OVERRIDE;
//@{
//! Entry point for all pipeline requests.
vtkTypeBool ProcessRequest(
vtkInformation* request, vtkInformationVector** inputVector,
vtkInformationVector* outputVector) VTK_DICOM_OVERRIDE;
//! Read the header information.
int RequestInformation(
vtkInformation* request, vtkInformationVector** inputVector,
vtkInformationVector* outputVector) VTK_DICOM_OVERRIDE;
//! Read the voxel data.
int RequestData(
vtkInformation* request, vtkInformationVector** inputVector,
vtkInformationVector* outputVector) VTK_DICOM_OVERRIDE;
//@}
//@{
//! Read the overlays into an allocated vtkImageData object.
virtual bool ReadOverlays(vtkImageData *data);
//! Unpack overlay bits to build the overlay image.
void UnpackOverlay(
const void *filePtr, vtkIdType bitskip, vtkIdType count,
void *buffer, vtkIdType incr, int bit);
//@}
//@{
//! Read one file. Specify the offset to the PixelData.
virtual bool ReadOneFile(
const char *filename, int idx,
unsigned char *buffer, vtkIdType bufferSize);
//! Clear or sign-extend any bits beyond BitsStored.
void MaskBits(void *buffer, vtkIdType bufferSize, int scalarSize,
int bitsStored, int pixelRepresentation);
//! Unpack 1 bit to 8 bits or 12 bits to 16 bits.
void UnpackBits(
const void *source, void *buffer, vtkIdType bufferSize, int bits);
//! Unpack 4:2:2 color data.
void UnpackYBR422(
const void *source, void *buffer, vtkIdType bufferSize, vtkIdType rowlen);
//! Read an DICOM file directly.
virtual bool ReadFileNative(
const char *filename, int idx,
unsigned char *buffer, vtkIdType bufferSize);
//! Read a DICOM file via DCMTK or GDCM.
virtual bool ReadFileDelegated(
const char *filename, int idx,
unsigned char *buffer, vtkIdType bufferSize);
//@}
//@{
//! Check if rescaling will change scalar type.
virtual int ComputeRescaledScalarType(
int scalarType, int bitsStored, int pixelRepresentation);
//! Rescale the data in the buffer.
virtual void RescaleBuffer(
int fileIdx, int frameIdx, int fileType, int outputType,
int numFileComponents, int numComponents,
void *fileBuffer, void *outputBuffer, vtkIdType bufferSize);
//! Convert buffer from YUV to RGB.
virtual void YBRToRGB(
int fileIdx, int frameIdx, void *buffer, vtkIdType bufferSize);
//@}
//@{
//! Convert parser errors into reader errors.
void RelayError(vtkObject *o, unsigned long e, void *data);
//@}
//@{
//! Verify that the files can be composed into a volume.
/*!
* This is called after SortFiles has been called. It should invoke an
* error event and return zero upon failure.
*/
virtual bool ValidateStructure(
vtkIntArray *fileArray, vtkIntArray *frameArray);
//! Sort the input files, put the sort in the supplied arrays.
virtual void SortFiles(vtkIntArray *fileArray, vtkIntArray *frameArray);
//! Do not sort the files, just build the arrays.
void NoSortFiles(vtkIntArray *fileArray, vtkIntArray *frameArray);
//@}
//@{
//! Update the medical image properties;
virtual void UpdateMedicalImageProperties();
//@}
//! Select whether to sort the files.
int Sorting;
//! Information for rescaling data to quantitative units.
double RescaleIntercept;
double RescaleSlope;
//! The orientation matrix for the DICOM file.
vtkMatrix4x4 *PatientMatrix;
//! The meta data for the image.
vtkDICOMMetaData *MetaData;
//! The MedicalImageProperties, for compatibility with other readers.
vtkMedicalImageProperties *MedicalImageProperties;
//! The default character set to use while parsing the file.
vtkDICOMCharacterSet DefaultCharacterSet;
//! Whether the default should override SpecificCharacterSet.
bool OverrideCharacterSet;
//! The parser that is used to read the file.
vtkDICOMParser *Parser;
//! The sorter that orders the slices within the volume.
vtkDICOMSliceSorter *Sorter;
//! The offsets to the pixel data in each file.
vtkTypeInt64Array *FileOffsetArray;
//! An array to convert slice indices to input files
vtkIntArray *FileIndexArray;
//! An array to convert slice indices to input frames
vtkIntArray *FrameIndexArray;
//! An array that holds the stack IDs.
vtkStringArray *StackIDs;
//! The row order to use when storing the data in memory.
int MemoryRowOrder;
//! This indicates that the data must be rescaled.
int NeedsRescale;
int AutoRescale;
int FileScalarType;
int OutputScalarType;
//! This indicates that the data must be converted to RGB.
int NeedsYBRToRGB;
int AutoYBRToRGB;
//! The number of packed pixel components in the input file.
/*!
* This is for packed, rather than planar, components.
*/
int NumberOfPackedComponents;
//! The number of color planes in the file.
int NumberOfPlanarComponents;
//! Time dimension variables.
int TimeAsVector;
int TimeDimension;
int DesiredTimeIndex;
double TimeSpacing;
//! The stack to load.
char DesiredStackID[20];
//! Bitfield that says what overlays are present.
unsigned short OverlayBitfield;
bool UpdateOverlayFlag;
private:
#ifdef VTK_DICOM_DELETE
vtkDICOMReader(const vtkDICOMReader&) VTK_DICOM_DELETE;
void operator=(const vtkDICOMReader&) VTK_DICOM_DELETE;
#else
vtkDICOMReader(const vtkDICOMReader&) = delete;
void operator=(const vtkDICOMReader&) = delete;
#endif
};
//! @cond
//! Initializer (Schwarz counter).
/*!
* This ensures that the vtkDICOMReader module is initialized before
* any other module that includes this header file.
*/
class VTKDICOM_EXPORT vtkDICOMReaderInitializer
{
public:
vtkDICOMReaderInitializer();
~vtkDICOMReaderInitializer();
private:
vtkDICOMReaderInitializer(const vtkDICOMReaderInitializer&);
vtkDICOMReaderInitializer& operator=(const vtkDICOMReaderInitializer&);
};
static vtkDICOMReaderInitializer vtkDICOMReaderInitializerInstance;
//! @endcond
#endif // vtkDICOMReader_h