1 /*=========================================================================
4 Module: $RCSfile: gdcmSegmentedPalette.h,v $
6 Date: $Date: 2007/10/03 09:46:09 $
7 Version: $Revision: 1.4 $
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 =========================================================================*/
21 // http://blog.goo.ne.jp/satomi_takeo/e/3643e5249b2a9650f9e10ef1c830e8b8
22 // I bet the code was compiled on VS6. Make it compile on other platform:
23 // * typedef are not inherited
24 // * need to explicitely add typename keyword
25 // * Uint8 / Uint16 are neither C nor C++
26 // * replace all dcmtk code with equivalent gdcm code
27 // * Add extra parameter to ReadPaletteInto to save extracted info
37 // abstract class for segment.
38 template <typename EntryType>
41 typedef std::map<const EntryType*, const Segment*> SegmentMap;
42 virtual bool Expand(const SegmentMap& instances,
43 std::vector<EntryType>& expanded) const = 0;
44 const EntryType* First() const { return _first; }
45 const EntryType* Last() const { return _last; }
48 typename SegmentMap::key_type,
49 typename SegmentMap::mapped_type
51 operator()(const Segment* segment) const
52 { return std::make_pair(segment->First(), segment); }
55 Segment(const EntryType* first, const EntryType* last) {
56 _first = first; _last = last;
58 const EntryType* _first;
59 const EntryType* _last;
62 // discrete segment (opcode = 0)
63 template <typename EntryType>
64 class DiscreteSegment : public Segment<EntryType> {
66 typedef typename Segment<EntryType>::SegmentMap SegmentMap;
67 DiscreteSegment(const EntryType* first)
68 : Segment<EntryType>(first, first+2+*(first+1)) {}
69 virtual bool Expand(const SegmentMap&,
70 std::vector<EntryType>& expanded) const
72 std::copy(this->_first + 2, this->_last, std::back_inserter(expanded));
77 // linear segment (opcode = 1)
78 template <typename EntryType>
79 class LinearSegment : public Segment<EntryType> {
81 typedef typename Segment<EntryType>::SegmentMap SegmentMap;
82 LinearSegment(const EntryType* first)
83 : Segment<EntryType>(first, first+3) {}
84 virtual bool Expand(const SegmentMap&,
85 std::vector<EntryType>& expanded) const
87 if ( expanded.empty() ) {
88 // linear segment can't be the first segment.
91 EntryType length = *(this->_first + 1);
92 EntryType y0 = expanded.back();
93 EntryType y1 = *(this->_first + 2);
95 for ( EntryType i = 0; i <length; ++i ) {
97 = static_cast<double>(y0)
98 + (static_cast<double>(i)/static_cast<double>(length)) * y01;
99 EntryType value_int = static_cast<EntryType>(value_float + 0.5);
100 expanded.push_back(value_int);
106 // indirect segment (opcode = 2)
107 template <typename EntryType>
108 class IndirectSegment : public Segment<EntryType> {
110 typedef typename Segment<EntryType>::SegmentMap SegmentMap;
111 IndirectSegment(const EntryType* first)
112 : Segment<EntryType>(first, first+2+4/sizeof(EntryType)) {}
113 virtual bool Expand(const SegmentMap& instances,
114 std::vector<EntryType>& expanded) const
116 if ( instances.empty() ) {
117 // some other segments are required as references.
120 const EntryType* first_segment = instances.begin()->first;
121 const unsigned short* pOffset
122 = reinterpret_cast<const unsigned short*>(this->_first + 2);
123 unsigned long offsetBytes
124 = (*pOffset) | (static_cast<unsigned long>(*(pOffset + 1)) << 16);
125 const EntryType* copied_part_head
126 = first_segment + offsetBytes / sizeof(EntryType);
127 typename SegmentMap::const_iterator ppHeadSeg = instances.find(copied_part_head);
128 if ( ppHeadSeg == instances.end() ) {
129 // referred segment not found
132 EntryType nNumCopies = *(this->_first + 1);
133 typename SegmentMap::const_iterator ppSeg = ppHeadSeg;
134 while ( std::distance(ppHeadSeg, ppSeg) <nNumCopies ) {
135 assert( ppSeg != instances.end() );
136 ppSeg->second->Expand(instances, expanded);
143 template <typename EntryType>
144 void ExpandPalette(const EntryType* raw_values, uint32_t length,
145 std::vector<EntryType>& palette)
147 typedef std::deque<Segment<EntryType>*> SegmentList;
148 SegmentList segments;
149 const EntryType* raw_seg = raw_values;
150 while ( (std::distance(raw_values, raw_seg) * sizeof(EntryType)) <length ) {
151 Segment<EntryType>* segment = NULL;
152 if ( *raw_seg == 0 ) {
153 segment = new DiscreteSegment<EntryType>(raw_seg);
154 } else if ( *raw_seg == 1 ) {
155 segment = new LinearSegment<EntryType>(raw_seg);
156 } else if ( *raw_seg == 2 ) {
157 segment = new IndirectSegment<EntryType>(raw_seg);
160 segments.push_back(segment);
161 raw_seg = segment->Last();
167 typename Segment<EntryType>::SegmentMap instances;
168 std::transform(segments.begin(), segments.end(),
169 std::inserter(instances, instances.end()), typename Segment<EntryType>::ToMap());
170 typename SegmentList::iterator ppSeg = segments.begin();
171 typename SegmentList::iterator endOfSegments = segments.end();
172 for ( ; ppSeg != endOfSegments; ++ppSeg ) {
173 (*ppSeg)->Expand(instances, palette);
175 ppSeg = segments.begin();
176 for ( ; ppSeg != endOfSegments; ++ppSeg ) {
181 void ReadPaletteInto(GDCM_NAME_SPACE::File* pds, const GDCM_NAME_SPACE::TagKey& descriptor,
182 const GDCM_NAME_SPACE::TagKey& segment, uint8_t* lut)
184 int desc_values[3] = {};
185 unsigned long count = 0;
186 //if ( pds->findAndGetUint16Array(descriptor, desc_values, &count).good() )
187 std::string desc_values_str = pds->GetEntryString(descriptor.GetGroup(), descriptor.GetElement() );
188 count = sscanf( desc_values_str.c_str(), "%d\\%d\\%d", desc_values, desc_values+1, desc_values+2 );
190 assert( count == 3 );
191 unsigned int num_entries = desc_values[0];
192 if ( num_entries == 0 ) {
193 num_entries = 0x10000;
195 int min_pixel_value = desc_values[1];
196 unsigned int entry_size = desc_values[2];
197 assert( entry_size == 8 || entry_size == 16 );
198 //DcmElement* pe = NULL;
199 GDCM_NAME_SPACE::DataEntry* pe = NULL;
201 pe = pds->GetDataEntry(segment.GetGroup(), segment.GetElement() ); {
202 //if ( pds->findAndGetElement(segment, pe).good() )
203 unsigned long length = pe->GetLength();
204 if ( entry_size == 8 ) {
205 uint8_t* segment_values = NULL;
206 //if ( pe->getUint8Array(segment_values).good() )
207 segment_values = (uint8_t*)pe->GetBinArea(); {
208 std::vector<uint8_t> palette;
209 palette.reserve(num_entries);
210 ExpandPalette(segment_values, length, palette);
211 memcpy(lut, &palette[0], palette.size() );
213 } else if ( entry_size == 16 ) {
214 uint16_t* segment_values = NULL;
215 segment_values = (uint16_t*)pe->GetBinArea(); {
216 //if ( pe->getUint16Array(segment_values).good() )
217 std::vector<uint16_t> palette;
218 palette.reserve(num_entries);
219 ExpandPalette(segment_values, length, palette);
220 memcpy(lut, &palette[0], palette.size()*2 );
221 // std::copy(palette.begin(), palette.end(),
222 // std::ostream_iterator<uint16_t>(std::cout, "\n"));