1 /*=========================================================================
4 Module: $RCSfile: gdcmSegmentedPalette.h,v $
6 Date: $Date: 2007/10/03 13:18:28 $
7 Version: $Revision: 1.8 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
19 #ifndef _GDCMSEGMENTEDPALETTE_H_
20 #define _GDCMSEGMENTEDPALETTE_H_
23 #include "gdcmTagKey.h"
24 #include "gdcmDataEntry.h"
27 // http://blog.goo.ne.jp/satomi_takeo/e/3643e5249b2a9650f9e10ef1c830e8b8
28 // I bet the code was compiled on VS6. Make it compile on other platform:
29 // * typedef are not inherited
30 // * need to explicitely add typename keyword
31 // * Uint8 / Uint16 are neither C nor C++
32 // * replace all dcmtk code with equivalent gdcm code
33 // * Add extra parameter to ReadPaletteInto to save extracted info
35 // MM: Over 50000 DICOM files I only found two that had a Segmented Palette LUT
36 // And both were coming from a ALOKA SSD-4000 station
44 namespace GDCM_NAME_SPACE
46 // abstract class for segment.
47 template <typename EntryType>
50 typedef std::map<const EntryType*, const Segment*> SegmentMap;
51 virtual bool Expand(const SegmentMap& instances,
52 std::vector<EntryType>& expanded) const = 0;
53 const EntryType* First() const { return _first; }
54 const EntryType* Last() const { return _last; }
57 typename SegmentMap::key_type,
58 typename SegmentMap::mapped_type
60 operator()(const Segment* segment) const
61 { return std::make_pair(segment->First(), segment); }
64 Segment(const EntryType* first, const EntryType* last) {
65 _first = first; _last = last;
67 const EntryType* _first;
68 const EntryType* _last;
71 // discrete segment (opcode = 0)
72 template <typename EntryType>
73 class DiscreteSegment : public Segment<EntryType> {
75 typedef typename Segment<EntryType>::SegmentMap SegmentMap;
76 DiscreteSegment(const EntryType* first)
77 : Segment<EntryType>(first, first+2+*(first+1)) {}
78 virtual bool Expand(const SegmentMap&,
79 std::vector<EntryType>& expanded) const
81 std::copy(this->_first + 2, this->_last, std::back_inserter(expanded));
86 // linear segment (opcode = 1)
87 template <typename EntryType>
88 class LinearSegment : public Segment<EntryType> {
90 typedef typename Segment<EntryType>::SegmentMap SegmentMap;
91 LinearSegment(const EntryType* first)
92 : Segment<EntryType>(first, first+3) {}
93 virtual bool Expand(const SegmentMap&,
94 std::vector<EntryType>& expanded) const
96 if ( expanded.empty() ) {
97 // linear segment can't be the first segment.
100 EntryType length = *(this->_first + 1);
101 EntryType y0 = expanded.back();
102 EntryType y1 = *(this->_first + 2);
103 double y01 = y1 - y0;
104 for ( EntryType i = 0; i <length; ++i ) {
106 = static_cast<double>(y0)
107 + (static_cast<double>(i)/static_cast<double>(length)) * y01;
108 EntryType value_int = static_cast<EntryType>(value_float + 0.5);
109 expanded.push_back(value_int);
115 // indirect segment (opcode = 2)
116 template <typename EntryType>
117 class IndirectSegment : public Segment<EntryType> {
119 typedef typename Segment<EntryType>::SegmentMap SegmentMap;
120 IndirectSegment(const EntryType* first)
121 : Segment<EntryType>(first, first+2+4/sizeof(EntryType)) {}
122 virtual bool Expand(const SegmentMap& instances,
123 std::vector<EntryType>& expanded) const
125 if ( instances.empty() ) {
126 // some other segments are required as references.
129 const EntryType* first_segment = instances.begin()->first;
130 const unsigned short* pOffset
131 = reinterpret_cast<const unsigned short*>(this->_first + 2);
132 unsigned long offsetBytes
133 = (*pOffset) | (static_cast<unsigned long>(*(pOffset + 1)) << 16);
134 const EntryType* copied_part_head
135 = first_segment + offsetBytes / sizeof(EntryType);
136 typename SegmentMap::const_iterator ppHeadSeg = instances.find(copied_part_head);
137 if ( ppHeadSeg == instances.end() ) {
138 // referred segment not found
141 EntryType nNumCopies = *(this->_first + 1);
142 typename SegmentMap::const_iterator ppSeg = ppHeadSeg;
143 while ( std::distance(ppHeadSeg, ppSeg) < nNumCopies ) {
144 assert( ppSeg != instances.end() );
145 ppSeg->second->Expand(instances, expanded);
152 template <typename EntryType>
153 void ExpandPalette(const EntryType* raw_values, uint32_t length,
154 std::vector<EntryType>& palette)
156 typedef std::deque<Segment<EntryType>*> SegmentList;
157 SegmentList segments;
158 const EntryType* raw_seg = raw_values;
159 while ( (std::distance(raw_values, raw_seg) * sizeof(EntryType)) <length ) {
160 Segment<EntryType>* segment = NULL;
161 if ( *raw_seg == 0 ) {
162 segment = new DiscreteSegment<EntryType>(raw_seg);
163 } else if ( *raw_seg == 1 ) {
164 segment = new LinearSegment<EntryType>(raw_seg);
165 } else if ( *raw_seg == 2 ) {
166 segment = new IndirectSegment<EntryType>(raw_seg);
169 segments.push_back(segment);
170 raw_seg = segment->Last();
176 typename Segment<EntryType>::SegmentMap instances;
177 std::transform(segments.begin(), segments.end(),
178 std::inserter(instances, instances.end()), typename Segment<EntryType>::ToMap());
179 typename SegmentList::iterator ppSeg = segments.begin();
180 typename SegmentList::iterator endOfSegments = segments.end();
181 for ( ; ppSeg != endOfSegments; ++ppSeg ) {
182 (*ppSeg)->Expand(instances, palette);
184 ppSeg = segments.begin();
185 for ( ; ppSeg != endOfSegments; ++ppSeg ) {
190 void ReadPaletteInto(GDCM_NAME_SPACE::File* pds, const GDCM_NAME_SPACE::TagKey& descriptor,
191 const GDCM_NAME_SPACE::TagKey& segment, uint8_t* lut)
193 unsigned int desc_values[3] = {};
194 unsigned long count = 0;
195 //if ( pds->findAndGetUint16Array(descriptor, desc_values, &count).good() )
196 std::string desc_values_str = pds->GetEntryString(descriptor.GetGroup(), descriptor.GetElement() );
197 count = sscanf( desc_values_str.c_str(), "%u\\%u\\%u", desc_values, desc_values+1, desc_values+2 );
199 assert( count == 3 );
200 unsigned int num_entries = desc_values[0];
201 if ( num_entries == 0 ) {
202 num_entries = 0x10000;
204 unsigned int min_pixel_value = desc_values[1];
205 assert( min_pixel_value == 0 ); // FIXME
206 unsigned int entry_size = desc_values[2];
207 assert( entry_size == 8 || entry_size == 16 );
208 //DcmElement* pe = NULL;
209 GDCM_NAME_SPACE::DataEntry* pe = NULL;
211 pe = pds->GetDataEntry(segment.GetGroup(), segment.GetElement() );
213 //if ( pds->findAndGetElement(segment, pe).good() )
214 unsigned long length = pe->GetLength();
215 if ( entry_size == 8 )
217 uint8_t* segment_values = NULL;
218 //if ( pe->getUint8Array(segment_values).good() )
219 segment_values = (uint8_t*)pe->GetBinArea();
221 std::vector<uint8_t> palette;
222 palette.reserve(num_entries);
223 ExpandPalette(segment_values, length, palette);
224 memcpy(lut, &palette[0], palette.size() );
227 else if ( entry_size == 16 )
229 uint16_t* segment_values = NULL;
230 segment_values = (uint16_t*)pe->GetBinArea();
232 //if ( pe->getUint16Array(segment_values).good() )
233 std::vector<uint16_t> palette;
234 palette.reserve(num_entries);
235 ExpandPalette(segment_values, length, palette);
236 memcpy(lut, &palette[0], palette.size()*2 );
243 } // end namespace gdcm