1 /*=========================================================================
4 Module: $RCSfile: gdcmSegmentedPalette.h,v $
6 Date: $Date: 2007/10/26 08:14:18 $
7 Version: $Revision: 1.12 $
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 and msvc 7
40 // It doesn't compile on msvc 6, borland and SunOS
51 #if defined(_MSC_VER) && (_MSC_VER < 1310)
54 #define GDCM_TYPENAME typename
57 namespace GDCM_NAME_SPACE
59 // abstract class for segment.
60 template <typename EntryType>
63 typedef std::map<const EntryType*, const Segment*> SegmentMap;
64 virtual bool Expand(const SegmentMap& instances,
65 std::vector<EntryType>& expanded) const = 0;
66 const EntryType* First() const { return _first; }
67 const EntryType* Last() const { return _last; }
69 typename SegmentMap::value_type
70 operator()(const Segment* segment) const
71 { return std::make_pair(segment->First(), segment); }
74 Segment(const EntryType* first, const EntryType* last) {
75 _first = first; _last = last;
77 const EntryType* _first;
78 const EntryType* _last;
81 // discrete segment (opcode = 0)
82 template <typename EntryType>
83 class DiscreteSegment : public Segment<EntryType> {
85 typedef typename Segment<EntryType>::SegmentMap SegmentMap;
86 DiscreteSegment(const EntryType* first)
87 : Segment<EntryType>(first, first+2+*(first+1)) {}
88 virtual bool Expand(const SegmentMap&,
89 std::vector<EntryType>& expanded) const
91 std::copy(this->_first + 2, this->_last, std::back_inserter(expanded));
96 // linear segment (opcode = 1)
97 template <typename EntryType>
98 class LinearSegment : public Segment<EntryType> {
100 typedef typename Segment<EntryType>::SegmentMap SegmentMap;
101 LinearSegment(const EntryType* first)
102 : Segment<EntryType>(first, first+3) {}
103 virtual bool Expand(const SegmentMap&,
104 std::vector<EntryType>& expanded) const
106 if ( expanded.empty() ) {
107 // linear segment can't be the first segment.
110 EntryType length = *(this->_first + 1);
111 EntryType y0 = expanded.back();
112 EntryType y1 = *(this->_first + 2);
113 double y01 = y1 - y0;
114 for ( EntryType i = 0; i <length; ++i ) {
116 = static_cast<double>(y0)
117 + (static_cast<double>(i)/static_cast<double>(length)) * y01;
118 EntryType value_int = static_cast<EntryType>(value_float + 0.5);
119 expanded.push_back(value_int);
125 // indirect segment (opcode = 2)
126 template <typename EntryType>
127 class IndirectSegment : public Segment<EntryType> {
129 typedef typename Segment<EntryType>::SegmentMap SegmentMap;
130 IndirectSegment(const EntryType* first)
131 : Segment<EntryType>(first, first+2+4/sizeof(EntryType)) {}
132 virtual bool Expand(const SegmentMap& instances,
133 std::vector<EntryType>& expanded) const
135 if ( instances.empty() ) {
136 // some other segments are required as references.
139 const EntryType* first_segment = instances.begin()->first;
140 const unsigned short* pOffset
141 = reinterpret_cast<const unsigned short*>(this->_first + 2);
142 unsigned long offsetBytes
143 = (*pOffset) | (static_cast<unsigned long>(*(pOffset + 1)) << 16);
144 const EntryType* copied_part_head
145 = first_segment + offsetBytes / sizeof(EntryType);
146 GDCM_TYPENAME SegmentMap::const_iterator ppHeadSeg = instances.find(copied_part_head);
147 if ( ppHeadSeg == instances.end() ) {
148 // referred segment not found
151 EntryType nNumCopies = *(this->_first + 1);
152 GDCM_TYPENAME SegmentMap::const_iterator ppSeg = ppHeadSeg;
153 while ( std::distance(ppHeadSeg, ppSeg) < nNumCopies ) {
154 assert( ppSeg != instances.end() );
155 ppSeg->second->Expand(instances, expanded);
162 template <typename EntryType>
163 void ExpandPalette(const EntryType* raw_values, uint32_t length,
164 std::vector<EntryType>& palette)
166 typedef std::deque<Segment<EntryType>*> SegmentList;
167 SegmentList segments;
168 const EntryType* raw_seg = raw_values;
169 while ( (std::distance(raw_values, raw_seg) * sizeof(EntryType)) <length ) {
170 Segment<EntryType>* segment = NULL;
171 if ( *raw_seg == 0 ) {
172 segment = new DiscreteSegment<EntryType>(raw_seg);
173 } else if ( *raw_seg == 1 ) {
174 segment = new LinearSegment<EntryType>(raw_seg);
175 } else if ( *raw_seg == 2 ) {
176 segment = new IndirectSegment<EntryType>(raw_seg);
179 segments.push_back(segment);
180 raw_seg = segment->Last();
186 GDCM_TYPENAME Segment<EntryType>::SegmentMap instances;
187 std::transform(segments.begin(), segments.end(),
188 std::inserter(instances, instances.end()), GDCM_TYPENAME Segment<EntryType>::ToMap());
189 GDCM_TYPENAME SegmentList::iterator ppSeg = segments.begin();
190 GDCM_TYPENAME SegmentList::iterator endOfSegments = segments.end();
191 for ( ; ppSeg != endOfSegments; ++ppSeg ) {
192 (*ppSeg)->Expand(instances, palette);
194 ppSeg = segments.begin();
195 for ( ; ppSeg != endOfSegments; ++ppSeg ) {
200 void ReadPaletteInto(GDCM_NAME_SPACE::File* pds, const GDCM_NAME_SPACE::TagKey& descriptor,
201 const GDCM_NAME_SPACE::TagKey& segment, uint8_t* lut)
203 unsigned int desc_values[3] = {0,0,0};
204 unsigned long count = 0;
205 //if ( pds->findAndGetUint16Array(descriptor, desc_values, &count).good() )
206 std::string desc_values_str = pds->GetEntryString(descriptor.GetGroup(), descriptor.GetElement() );
207 count = sscanf( desc_values_str.c_str(), "%u\\%u\\%u", desc_values, desc_values+1, desc_values+2 );
209 assert( count == 3 );
210 unsigned int num_entries = desc_values[0];
211 if ( num_entries == 0 ) {
212 num_entries = 0x10000;
214 unsigned int min_pixel_value = desc_values[1];
215 assert( min_pixel_value == 0 ); // FIXME
216 unsigned int entry_size = desc_values[2];
217 assert( entry_size == 8 || entry_size == 16 );
218 //DcmElement* pe = NULL;
219 GDCM_NAME_SPACE::DataEntry* pe = NULL;
221 pe = pds->GetDataEntry(segment.GetGroup(), segment.GetElement() );
223 //if ( pds->findAndGetElement(segment, pe).good() )
224 unsigned long length = pe->GetLength();
225 if ( entry_size == 8 )
227 uint8_t* segment_values = NULL;
228 //if ( pe->getUint8Array(segment_values).good() )
229 segment_values = (uint8_t*)pe->GetBinArea();
231 std::vector<uint8_t> palette;
232 palette.reserve(num_entries);
233 ExpandPalette(segment_values, length, palette);
234 memcpy(lut, &palette[0], palette.size() );
237 else if ( entry_size == 16 )
239 uint16_t* segment_values = NULL;
240 segment_values = (uint16_t*)pe->GetBinArea();
242 //if ( pe->getUint16Array(segment_values).good() )
243 std::vector<uint16_t> palette;
244 palette.reserve(num_entries);
245 ExpandPalette(segment_values, length, palette);
246 memcpy(lut, &palette[0], palette.size()*2 );
253 } // end namespace gdcm
256 // do not pollute namespace: