1 /*=========================================================================
4 Module: $RCSfile: gdcmSegmentedPalette.h,v $
6 Date: $Date: 2007/10/03 12:01:27 $
7 Version: $Revision: 1.6 $
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
44 // abstract class for segment.
45 template <typename EntryType>
48 typedef std::map<const EntryType*, const Segment*> SegmentMap;
49 virtual bool Expand(const SegmentMap& instances,
50 std::vector<EntryType>& expanded) const = 0;
51 const EntryType* First() const { return _first; }
52 const EntryType* Last() const { return _last; }
55 typename SegmentMap::key_type,
56 typename SegmentMap::mapped_type
58 operator()(const Segment* segment) const
59 { return std::make_pair(segment->First(), segment); }
62 Segment(const EntryType* first, const EntryType* last) {
63 _first = first; _last = last;
65 const EntryType* _first;
66 const EntryType* _last;
69 // discrete segment (opcode = 0)
70 template <typename EntryType>
71 class DiscreteSegment : public Segment<EntryType> {
73 typedef typename Segment<EntryType>::SegmentMap SegmentMap;
74 DiscreteSegment(const EntryType* first)
75 : Segment<EntryType>(first, first+2+*(first+1)) {}
76 virtual bool Expand(const SegmentMap&,
77 std::vector<EntryType>& expanded) const
79 std::copy(this->_first + 2, this->_last, std::back_inserter(expanded));
84 // linear segment (opcode = 1)
85 template <typename EntryType>
86 class LinearSegment : public Segment<EntryType> {
88 typedef typename Segment<EntryType>::SegmentMap SegmentMap;
89 LinearSegment(const EntryType* first)
90 : Segment<EntryType>(first, first+3) {}
91 virtual bool Expand(const SegmentMap&,
92 std::vector<EntryType>& expanded) const
94 if ( expanded.empty() ) {
95 // linear segment can't be the first segment.
98 EntryType length = *(this->_first + 1);
99 EntryType y0 = expanded.back();
100 EntryType y1 = *(this->_first + 2);
101 double y01 = y1 - y0;
102 for ( EntryType i = 0; i <length; ++i ) {
104 = static_cast<double>(y0)
105 + (static_cast<double>(i)/static_cast<double>(length)) * y01;
106 EntryType value_int = static_cast<EntryType>(value_float + 0.5);
107 expanded.push_back(value_int);
113 // indirect segment (opcode = 2)
114 template <typename EntryType>
115 class IndirectSegment : public Segment<EntryType> {
117 typedef typename Segment<EntryType>::SegmentMap SegmentMap;
118 IndirectSegment(const EntryType* first)
119 : Segment<EntryType>(first, first+2+4/sizeof(EntryType)) {}
120 virtual bool Expand(const SegmentMap& instances,
121 std::vector<EntryType>& expanded) const
123 if ( instances.empty() ) {
124 // some other segments are required as references.
127 const EntryType* first_segment = instances.begin()->first;
128 const unsigned short* pOffset
129 = reinterpret_cast<const unsigned short*>(this->_first + 2);
130 unsigned long offsetBytes
131 = (*pOffset) | (static_cast<unsigned long>(*(pOffset + 1)) << 16);
132 const EntryType* copied_part_head
133 = first_segment + offsetBytes / sizeof(EntryType);
134 typename SegmentMap::const_iterator ppHeadSeg = instances.find(copied_part_head);
135 if ( ppHeadSeg == instances.end() ) {
136 // referred segment not found
139 EntryType nNumCopies = *(this->_first + 1);
140 typename SegmentMap::const_iterator ppSeg = ppHeadSeg;
141 while ( std::distance(ppHeadSeg, ppSeg) < nNumCopies ) {
142 assert( ppSeg != instances.end() );
143 ppSeg->second->Expand(instances, expanded);
150 template <typename EntryType>
151 void ExpandPalette(const EntryType* raw_values, uint32_t length,
152 std::vector<EntryType>& palette)
154 typedef std::deque<Segment<EntryType>*> SegmentList;
155 SegmentList segments;
156 const EntryType* raw_seg = raw_values;
157 while ( (std::distance(raw_values, raw_seg) * sizeof(EntryType)) <length ) {
158 Segment<EntryType>* segment = NULL;
159 if ( *raw_seg == 0 ) {
160 segment = new DiscreteSegment<EntryType>(raw_seg);
161 } else if ( *raw_seg == 1 ) {
162 segment = new LinearSegment<EntryType>(raw_seg);
163 } else if ( *raw_seg == 2 ) {
164 segment = new IndirectSegment<EntryType>(raw_seg);
167 segments.push_back(segment);
168 raw_seg = segment->Last();
174 typename Segment<EntryType>::SegmentMap instances;
175 std::transform(segments.begin(), segments.end(),
176 std::inserter(instances, instances.end()), typename Segment<EntryType>::ToMap());
177 typename SegmentList::iterator ppSeg = segments.begin();
178 typename SegmentList::iterator endOfSegments = segments.end();
179 for ( ; ppSeg != endOfSegments; ++ppSeg ) {
180 (*ppSeg)->Expand(instances, palette);
182 ppSeg = segments.begin();
183 for ( ; ppSeg != endOfSegments; ++ppSeg ) {
188 void ReadPaletteInto(GDCM_NAME_SPACE::File* pds, const GDCM_NAME_SPACE::TagKey& descriptor,
189 const GDCM_NAME_SPACE::TagKey& segment, uint8_t* lut)
191 unsigned int desc_values[3] = {};
192 unsigned long count = 0;
193 //if ( pds->findAndGetUint16Array(descriptor, desc_values, &count).good() )
194 std::string desc_values_str = pds->GetEntryString(descriptor.GetGroup(), descriptor.GetElement() );
195 count = sscanf( desc_values_str.c_str(), "%u\\%u\\%u", desc_values, desc_values+1, desc_values+2 );
197 assert( count == 3 );
198 unsigned int num_entries = desc_values[0];
199 if ( num_entries == 0 ) {
200 num_entries = 0x10000;
202 unsigned int min_pixel_value = desc_values[1];
203 assert( min_pixel_value == 0 ); // FIXME
204 unsigned int entry_size = desc_values[2];
205 assert( entry_size == 8 || entry_size == 16 );
206 //DcmElement* pe = NULL;
207 GDCM_NAME_SPACE::DataEntry* pe = NULL;
209 pe = pds->GetDataEntry(segment.GetGroup(), segment.GetElement() ); {
210 //if ( pds->findAndGetElement(segment, pe).good() )
211 unsigned long length = pe->GetLength();
212 if ( entry_size == 8 ) {
213 uint8_t* segment_values = NULL;
214 //if ( pe->getUint8Array(segment_values).good() )
215 segment_values = (uint8_t*)pe->GetBinArea(); {
216 std::vector<uint8_t> palette;
217 palette.reserve(num_entries);
218 ExpandPalette(segment_values, length, palette);
219 memcpy(lut, &palette[0], palette.size() );
221 } else if ( entry_size == 16 ) {
222 uint16_t* segment_values = NULL;
223 segment_values = (uint16_t*)pe->GetBinArea(); {
224 //if ( pe->getUint16Array(segment_values).good() )
225 std::vector<uint16_t> palette;
226 palette.reserve(num_entries);
227 ExpandPalette(segment_values, length, palette);
228 memcpy(lut, &palette[0], palette.size()*2 );
229 // std::copy(palette.begin(), palette.end(),
230 // std::ostream_iterator<uint16_t>(std::cout, "\n"));
237 } // end namespace gdcm