1 /*=========================================================================
4 Module: $RCSfile: gdcmSegmentedPalette.h,v $
6 Date: $Date: 2007/11/13 09:37:22 $
7 Version: $Revision: 1.20 $
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"
26 // Notepad from Satomi Takeo:
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
39 // It compiles fine under gcc 4.1.2, msvc 7 and DarwinG5-g++
40 // It doesn't compile on msvc 6, borland and SunOS
51 #if defined(_MSC_VER) && (_MSC_VER < 1310)
54 #define GDCM_TYPENAME typename
58 #if (defined(_MSC_VER) && (_MSC_VER < 1310)) || defined(__BORLANDC__)
59 #define GDCM_TYPENAME2
61 #define GDCM_TYPENAME2 typename
65 namespace GDCM_NAME_SPACE
67 // Long stody short: Sun compiler only provide the second interface of
68 // std::distance, since the implementation is so trivial, I'd rather redo it myself.
70 // http://www.sgi.com/tech/stl/distance.html#2
71 // The second version of distance was the one defined in the original STL, and
72 // the first version is the one defined in the draft C++ standard; the definition
73 // was changed because the older interface was clumsy and error-prone.
74 // The older interface required the use of a temporary variable, and it has semantics
75 // that are somewhat nonintuitive: it increments n by the distance from first to last,
76 // rather than storing that distance in n
77 template<typename InputIterator>
79 mydistance(InputIterator first, InputIterator last)
90 // abstract class for segment.
91 template <typename EntryType>
94 typedef std::map<const EntryType* const, const Segment*> SegmentMap;
95 virtual bool Expand(const SegmentMap& instances,
96 std::vector<EntryType>& expanded) const = 0;
97 const EntryType* First() const { return _first; }
98 const EntryType* Last() const { return _last; }
100 typename SegmentMap::value_type
101 operator()(const Segment* segment) const
102 { return std::make_pair(segment->First(), segment); }
105 Segment(const EntryType* first, const EntryType* last) {
106 _first = first; _last = last;
108 const EntryType* _first;
109 const EntryType* _last;
112 // discrete segment (opcode = 0)
113 template <typename EntryType>
114 class DiscreteSegment : public Segment<EntryType> {
116 #if !defined(__sun__)
117 typedef typename Segment<EntryType>::SegmentMap SegmentMap;
119 DiscreteSegment(const EntryType* first)
120 : Segment<EntryType>(first, first+2+*(first+1)) {}
121 virtual bool Expand(const SegmentMap&,
122 std::vector<EntryType>& expanded) const
124 std::copy(this->_first + 2, this->_last, std::back_inserter(expanded));
129 // linear segment (opcode = 1)
130 template <typename EntryType>
131 class LinearSegment : public Segment<EntryType> {
133 #if !defined(__sun__)
134 typedef typename Segment<EntryType>::SegmentMap SegmentMap;
136 LinearSegment(const EntryType* first)
137 : Segment<EntryType>(first, first+3) {}
138 virtual bool Expand(const SegmentMap&,
139 std::vector<EntryType>& expanded) const
141 if ( expanded.empty() ) {
142 // linear segment can't be the first segment.
145 EntryType length = *(this->_first + 1);
146 EntryType y0 = expanded.back();
147 EntryType y1 = *(this->_first + 2);
148 double y01 = y1 - y0;
149 for ( EntryType i = 0; i <length; ++i ) {
151 = static_cast<double>(y0)
152 + (static_cast<double>(i)/static_cast<double>(length)) * y01;
153 EntryType value_int = static_cast<EntryType>(value_float + 0.5);
154 expanded.push_back(value_int);
160 // indirect segment (opcode = 2)
161 template <typename EntryType>
162 class IndirectSegment : public Segment<EntryType> {
164 #if !defined(__sun__)
165 typedef typename Segment<EntryType>::SegmentMap SegmentMap;
167 IndirectSegment(const EntryType* first)
168 : Segment<EntryType>(first, first+2+4/sizeof(EntryType)) {}
169 virtual bool Expand(const SegmentMap& instances,
170 std::vector<EntryType>& expanded) const
172 if ( instances.empty() ) {
173 // some other segments are required as references.
176 const EntryType* first_segment = instances.begin()->first;
177 const unsigned short* pOffset
178 = reinterpret_cast<const unsigned short*>(this->_first + 2);
179 unsigned long offsetBytes
180 = (*pOffset) | (static_cast<unsigned long>(*(pOffset + 1)) << 16);
181 const EntryType* copied_part_head
182 = first_segment + offsetBytes / sizeof(EntryType);
183 GDCM_TYPENAME SegmentMap::const_iterator ppHeadSeg = instances.find(copied_part_head);
184 if ( ppHeadSeg == instances.end() ) {
185 // referred segment not found
188 EntryType nNumCopies = *(this->_first + 1);
189 GDCM_TYPENAME SegmentMap::const_iterator ppSeg = ppHeadSeg;
190 while ( mydistance(ppHeadSeg, ppSeg) < nNumCopies ) {
191 // assert( mydistance(ppHeadSeg, ppSeg) == std::distance(ppHeadSeg, ppSeg) );
192 assert( ppSeg != instances.end() );
193 ppSeg->second->Expand(instances, expanded);
200 template <typename EntryType>
201 void ExpandPalette(const EntryType* raw_values, uint32_t length,
202 std::vector<EntryType>& palette)
204 typedef std::deque<Segment<EntryType>*> SegmentList;
205 SegmentList segments;
206 const EntryType* raw_seg = raw_values;
207 while ( (mydistance(raw_values, raw_seg) * sizeof(EntryType)) < length ) {
208 // assert( mydistance(raw_values, raw_seg) == std::distance(raw_values, raw_seg) );
209 Segment<EntryType>* segment = NULL;
210 if ( *raw_seg == 0 ) {
211 segment = new DiscreteSegment<EntryType>(raw_seg);
212 } else if ( *raw_seg == 1 ) {
213 segment = new LinearSegment<EntryType>(raw_seg);
214 } else if ( *raw_seg == 2 ) {
215 segment = new IndirectSegment<EntryType>(raw_seg);
218 segments.push_back(segment);
219 raw_seg = segment->Last();
226 GDCM_TYPENAME Segment<EntryType>::SegmentMap instances;
227 std::transform(segments.begin(), segments.end(),
228 std::inserter(instances, instances.end()), GDCM_TYPENAME2 Segment<EntryType>::ToMap());
229 GDCM_TYPENAME SegmentList::iterator ppSeg = segments.begin();
230 GDCM_TYPENAME SegmentList::iterator endOfSegments = segments.end();
231 for ( ; ppSeg != endOfSegments; ++ppSeg ) {
232 (*ppSeg)->Expand(instances, palette);
234 ppSeg = segments.begin();
235 for ( ; ppSeg != endOfSegments; ++ppSeg ) {
240 void ReadPaletteInto(GDCM_NAME_SPACE::File* pds, const GDCM_NAME_SPACE::TagKey& descriptor,
241 const GDCM_NAME_SPACE::TagKey& segment, uint8_t* lut)
243 unsigned int desc_values[3] = {0,0,0};
244 unsigned long count = 0;
245 //if ( pds->findAndGetUint16Array(descriptor, desc_values, &count).good() )
246 std::string desc_values_str = pds->GetEntryString(descriptor.GetGroup(), descriptor.GetElement() );
247 count = sscanf( desc_values_str.c_str(), "%u\\%u\\%u", desc_values, desc_values+1, desc_values+2 );
249 assert( count == 3 );
250 unsigned int num_entries = desc_values[0];
251 if ( num_entries == 0 ) {
252 num_entries = 0x10000;
254 unsigned int min_pixel_value = desc_values[1];
255 assert( min_pixel_value == 0 ); // FIXME
256 unsigned int entry_size = desc_values[2];
257 assert( entry_size == 8 || entry_size == 16 );
258 //DcmElement* pe = NULL;
259 GDCM_NAME_SPACE::DataEntry* pe = NULL;
261 pe = pds->GetDataEntry(segment.GetGroup(), segment.GetElement() );
263 //if ( pds->findAndGetElement(segment, pe).good() )
264 unsigned long length = pe->GetLength();
265 if ( entry_size == 8 )
267 uint8_t* segment_values = NULL;
268 //if ( pe->getUint8Array(segment_values).good() )
269 segment_values = (uint8_t*)pe->GetBinArea();
271 std::vector<uint8_t> palette;
272 palette.reserve(num_entries);
273 ExpandPalette(segment_values, length, palette);
274 memcpy(lut, &palette[0], palette.size() );
277 else if ( entry_size == 16 )
279 uint16_t* segment_values = NULL;
280 segment_values = (uint16_t*)pe->GetBinArea();
282 //if ( pe->getUint16Array(segment_values).good() )
283 std::vector<uint16_t> palette;
284 palette.reserve(num_entries);
285 ExpandPalette(segment_values, length, palette);
286 memcpy(lut, &palette[0], palette.size()*2 );
293 } // end namespace gdcm
296 // do not pollute namespace: