7 #include "fwDcmtkIO/reader/rgblookup/ImageRGBLookupLazyStream.hpp" 9 #include "fwDcmtkIO/reader/rgblookup/ImageRGBLookupLazyReader.hpp" 11 #include <boost/filesystem/operations.hpp> 12 #include <boost/filesystem/path.hpp> 13 #include <boost/foreach.hpp> 14 #include <boost/make_shared.hpp> 16 #include <dcmtk/dcmdata/dcistrmb.h> 30 SLM_ASSERT(
"ImageRGBLookupLazySource needs at least one dicom item to read an image.",
31 !dcmInfo->m_dicomSeries->getDicomContainer().empty());
33 m_frameSize = m_dcmInfo->m_rows * m_dcmInfo->m_columns * m_dcmInfo->m_imageType.sizeOf() * 3;
35 m_currentPosition = 0;
36 m_currentDicom = m_dcmInfo->m_dicomSeries->getDicomContainer().begin();
39 DcmFileFormat fileFormat;
44 const auto firstItem = m_dcmInfo->m_dicomSeries->getDicomContainer().begin();
45 const ::fwMemory::BufferObject::sptr bufferObj = firstItem->second;
46 const size_t buffSize = bufferObj->getSize();
47 const std::string dicomPath = bufferObj->getStreamInfo().fsFile.string();
49 char* buffer =
static_cast< char*
>( lock.
getBuffer() );
51 DcmInputBufferStream is;
52 is.setBuffer(buffer, offile_off_t(buffSize));
55 fileFormat.transferInit();
56 if (!fileFormat.read(is).good())
58 FW_RAISE(
"Unable to read Dicom file '"<< dicomPath <<
"' "<<
59 "(slice: '" << firstItem->first <<
"')");
62 fileFormat.loadAllDataIntoMemory();
63 fileFormat.transferEnd();
65 dataset = fileFormat.getDataset();
67 if(m_dcmInfo->m_bitsAllocated == 16)
69 const Uint16* redLookup;
70 const Uint16* greenLookup;
71 const Uint16* blueLookup;
73 dataset->findAndGetUint16Array(DCM_RedPaletteColorLookupTableData, redLookup);
74 dataset->findAndGetUint16Array(DCM_GreenPaletteColorLookupTableData, greenLookup);
75 dataset->findAndGetUint16Array(DCM_BluePaletteColorLookupTableData, blueLookup);
76 m_redLookup = (
void*) redLookup;
77 m_greenLookup = (
void*) greenLookup;
78 m_blueLookup = (
void*) blueLookup;
82 const Uint8* redLookup;
83 const Uint8* greenLookup;
84 const Uint8* blueLookup;
86 dataset->findAndGetUint8Array(DCM_RedPaletteColorLookupTableData, redLookup);
87 dataset->findAndGetUint8Array(DCM_GreenPaletteColorLookupTableData, greenLookup);
88 dataset->findAndGetUint8Array(DCM_BluePaletteColorLookupTableData, blueLookup);
89 m_redLookup = (
void*) redLookup;
90 m_greenLookup = (
void*) greenLookup;
91 m_blueLookup = (
void*) blueLookup;
94 if(m_dcmInfo->m_bitsAllocated == 16)
97 m_dcmInfo->m_rows, m_dcmInfo->m_columns,
98 m_currentDicom->second,
100 (Uint16*)m_greenLookup, (Uint16*)m_blueLookup,
101 m_dcmInfo->m_pixelValueBitsAllocated));
106 m_dcmInfo->m_rows, m_dcmInfo->m_columns,
107 m_currentDicom->second,
109 (Uint8*)m_greenLookup, (Uint8*)m_blueLookup,
110 m_dcmInfo->m_pixelValueBitsAllocated));
118 std::streamsize result = -1;
121 if(m_currentPosition + size > m_frameSize)
124 const size_t remainingBytes = m_frameSize - m_currentPosition;
125 const size_t extraBytes = size - remainingBytes;
127 if(remainingBytes > 0)
129 memcpy(stream, m_frame + m_currentPosition, remainingBytes);
130 result = remainingBytes;
137 m_currentPosition = 0;
140 if(m_currentDicom != m_dcmInfo->m_dicomSeries->getDicomContainer().end())
145 if(m_dcmInfo->m_bitsAllocated == 16)
150 m_dcmInfo->m_rows, m_dcmInfo->m_columns,
151 m_currentDicom->second,
152 (Uint16*)m_redLookup,
153 (Uint16*)m_greenLookup,
154 (Uint16*)m_blueLookup,
155 m_dcmInfo->m_pixelValueBitsAllocated));
162 m_dcmInfo->m_rows, m_dcmInfo->m_columns,
163 m_currentDicom->second,
165 (Uint8*)m_greenLookup,
166 (Uint8*)m_blueLookup,
167 m_dcmInfo->m_pixelValueBitsAllocated));
170 memcpy(stream, m_frame + m_currentPosition, extraBytes);
171 m_currentPosition += extraBytes;
172 result = remainingBytes + extraBytes;
183 memcpy(stream, m_frame + m_currentPosition, size);
184 m_currentPosition += size;
187 if(m_currentDicom->second == m_dcmInfo->m_dicomSeries->getDicomContainer().rbegin()->second &&
188 m_currentPosition == m_frameSize)
208 SPTR(::boost::iostreams::stream<ImageRGBLookupLazySource>) is
209 = std::make_shared< ::boost::iostreams::stream<ImageRGBLookupLazySource> >(
m_dcmInfo );
ImageRGBLookupLazyStream(ImageRGBLookupLazyInformation::sptr dcmInfo)
Constructor. Builds the fwMemory::stream::in::IFactory with few dicom information.
std::shared_ptr< std::istream > get()
Returns the istream on dicom image buffer.
base class for BufferObject Lock
LockBase< T >::BufferType getBuffer() const
Returns BufferObject's buffer pointer.
std::streamsize read(char *stream, std::streamsize size)
Method to read n bytes in dicom buffer and write it in s.
Class used to perform a lazy reading of dicom images with fw4spl system.
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
fwDcmtkIO contains classes used to pull Dicom images from a pacs using dcmtk library.
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 SLM_TRACE(message)
ImageRGBLookupLazyInformation::sptr m_dcmInfo
To conserve dicom information.
ImageRGBLookupLazySource(ImageRGBLookupLazyInformation::sptr dcmInfo)
Constructor.