fw4spl
ImageRGBLookupLazyReader.hpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #pragma once
8 
9 #include "fwDcmtkIO/config.hpp"
10 #include "fwDcmtkIO/helper/Codec.hpp"
11 
12 #include <fwMedData/DicomSeries.hpp>
13 
14 #include <fwTools/Type.hpp>
15 
16 #include <dcmtk/config/osconfig.h>
17 #include <dcmtk/dcmdata/dcdeftag.h>
18 #include <dcmtk/dcmdata/dcfilefo.h>
19 #include <dcmtk/dcmdata/dcistrmb.h>
20 #include <dcmtk/dcmimgle/dcmimage.h>
21 #include <dcmtk/dcmnet/diutil.h>
22 
23 namespace fwDcmtkIO
24 {
25 namespace reader
26 {
27 namespace rgblookup
28 {
29 
33 class FWDCMTKIO_CLASS_API ImageRGBLookupLazyReader
34 {
35 public:
36 
48  template< typename T>
49  static void* createInstanceBuffer(unsigned int rows, unsigned int columns,
50  const ::fwMemory::BufferObject::sptr& instance,
51  const T* redLookup,
52  const T* greenLookup,
53  const T* blueLookup, unsigned short pixelValueBitsAllocated)
54  {
55  if(pixelValueBitsAllocated == 16)
56  {
57  return ::fwDcmtkIO::reader::rgblookup::ImageRGBLookupLazyReader::createInstanceBuffer<T, Uint16>(
58  rows, columns, instance, redLookup, greenLookup, blueLookup);
59  }
60  else
61  {
62  return ::fwDcmtkIO::reader::rgblookup::ImageRGBLookupLazyReader::createInstanceBuffer<T, Uint8>(
63  rows, columns, instance, redLookup, greenLookup, blueLookup);
64  }
65  }
66 
78  template< typename T, typename U >
79  static T* createInstanceBuffer(unsigned int rows, unsigned int columns,
80  const ::fwMemory::BufferObject::sptr& instance,
81  const T* redLookup,
82  const T* greenLookup,
83  const T* blueLookup)
84  {
85  DcmFileFormat fileFormat;
86  OFCondition status;
87  DcmDataset* dataset;
88 
89  // Register codecs
91 
92  // Create temporary buffer
93  T* tempoBuffer = new T[rows * columns * 3]; // 3 colors (RGB)
94 
95  // Read instance
96  const size_t buffSize = instance->getSize();
97  const std::string dicomPath = instance->getStreamInfo().fsFile.string();
98  ::fwMemory::BufferObject::Lock lock(instance);
99  char* buffer = static_cast< char* >( lock.getBuffer() );
100 
101  DcmInputBufferStream is;
102  is.setBuffer(buffer, offile_off_t(buffSize));
103  is.setEos();
104 
105  fileFormat.transferInit();
106  if (!fileFormat.read(is).good())
107  {
108  FW_RAISE("Unable to read Dicom file '"<< dicomPath <<"'");
109  }
110 
111  fileFormat.loadAllDataIntoMemory();
112  fileFormat.transferEnd();
113 
114  dataset = fileFormat.getDataset();
115 
116  // Decompress data set if compressed
117  dataset->chooseRepresentation(EXS_LittleEndianExplicit, nullptr);
118 
119  // Read pixels
120  const U* pixelData;
121  if(sizeof(U) == 1)
122  {
123  const Uint8* pdata;
124  dataset->findAndGetUint8Array(DCM_PixelData, pdata);
125  pixelData = (U*)pdata;
126  }
127  else
128  {
129  const Uint16* pdata;
130  dataset->findAndGetUint16Array(DCM_PixelData, pdata);
131  pixelData = (U*)pdata;
132  }
133 
134  OSLM_WARN_IF("Unable to read pixel data.", !pixelData);
135 
136  for (unsigned int x = 0; x < columns; ++x)
137  {
138  for (unsigned int y = 0; y < rows; ++y)
139  {
140  unsigned int position = y + (x * rows);
141  U value = pixelData[position];
142  tempoBuffer[position*3] = static_cast< T >((redLookup[value]/(double)0xffff)*256);
143  tempoBuffer[position*3+1] = static_cast< T >((greenLookup[value]/(double)0xffff)*256);
144  tempoBuffer[position*3+2] = static_cast< T >((blueLookup[value]/(double)0xffff)*256);
145  }
146  }
147 
148  // Clean up codecs
150 
151  return tempoBuffer;
152  }
153 };
154 
155 } //rgblookup
156 } //reader
157 } //fwDcmtkIO
static FWDCMTKIO_API void registerCodecs()
Load DICOM codec.
Definition: Codec.cpp:18
base class for BufferObject Lock
LockBase< T >::BufferType getBuffer() const
Returns BufferObject&#39;s buffer pointer.
static T * createInstanceBuffer(unsigned int rows, unsigned int columns, const ::fwMemory::BufferObject::sptr &instance, const T *redLookup, const T *greenLookup, const T *blueLookup)
Create an instance buffer according to the image type. The template T is used to determine color form...
static FWDCMTKIO_API void cleanup()
Clean up codec register.
Definition: Codec.cpp:29
fwDcmtkIO contains classes used to pull Dicom images from a pacs using dcmtk library.
Definition: Codec.hpp:12
static void * createInstanceBuffer(unsigned int rows, unsigned int columns, const ::fwMemory::BufferObject::sptr &instance, const T *redLookup, const T *greenLookup, const T *blueLookup, unsigned short pixelValueBitsAllocated)
Create an instance buffer according to the image type. The template T is used to determine color form...
#define OSLM_WARN_IF(message, cond)
Definition: spyLog.hpp:267
This class is used to read the buffer of a DICOM image in LAZY mode when a pixel lookup must be perfo...