Program: gdcm
Module: $RCSfile: gdcmSegmentedPalette.h,v $
Language: C++
- Date: $Date: 2007/10/23 14:48:26 $
- Version: $Revision: 1.10 $
+ Date: $Date: 2007/11/13 09:37:22 $
+ Version: $Revision: 1.20 $
Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
l'Image). All rights reserved. See Doc/License.txt or
#include "gdcmTagKey.h"
#include "gdcmDataEntry.h"
-// Ref:
+// Notepad from Satomi Takeo:
// http://blog.goo.ne.jp/satomi_takeo/e/3643e5249b2a9650f9e10ef1c830e8b8
// I bet the code was compiled on VS6. Make it compile on other platform:
// * typedef are not inherited
// And both were coming from a ALOKA SSD-4000 station
//
// JPRx :
-// It compiles fine under gcc 4.1.2 and msvc 7
+// It compiles fine under gcc 4.1.2, msvc 7 and DarwinG5-g++
// It doesn't compile on msvc 6, borland and SunOS
// Any fix welcome !
// Hack for VS6
#if defined(_MSC_VER) && (_MSC_VER < 1310)
-#define GDCM_TYPENAME class
+#define GDCM_TYPENAME
#else
#define GDCM_TYPENAME typename
#endif
+// Hack for Borland
+#if (defined(_MSC_VER) && (_MSC_VER < 1310)) || defined(__BORLANDC__)
+#define GDCM_TYPENAME2
+#else
+#define GDCM_TYPENAME2 typename
+#endif
+
+
namespace GDCM_NAME_SPACE
{
+// Long stody short: Sun compiler only provide the second interface of
+// std::distance, since the implementation is so trivial, I'd rather redo it myself.
+// Ref:
+// http://www.sgi.com/tech/stl/distance.html#2
+// The second version of distance was the one defined in the original STL, and
+// the first version is the one defined in the draft C++ standard; the definition
+// was changed because the older interface was clumsy and error-prone.
+// The older interface required the use of a temporary variable, and it has semantics
+// that are somewhat nonintuitive: it increments n by the distance from first to last,
+// rather than storing that distance in n
+ template<typename InputIterator>
+ inline int32_t
+ mydistance(InputIterator first, InputIterator last)
+ {
+ int32_t n = 0;
+ while (first != last)
+ {
+ ++first;
+ ++n;
+ }
+ return n;
+ }
+
// abstract class for segment.
- template <GDCM_TYPENAME EntryType>
+ template <typename EntryType>
class Segment {
public:
- typedef std::map<const EntryType*, const Segment*> SegmentMap;
+ typedef std::map<const EntryType* const, const Segment*> SegmentMap;
virtual bool Expand(const SegmentMap& instances,
std::vector<EntryType>& expanded) const = 0;
const EntryType* First() const { return _first; }
- const EntryType* Last() const { return _last; }
+ const EntryType* Last() const { return _last; }
struct ToMap {
- std::pair<
- typename SegmentMap::key_type,
- typename SegmentMap::mapped_type
- >
+ typename SegmentMap::value_type
operator()(const Segment* segment) const
{ return std::make_pair(segment->First(), segment); }
};
};
// discrete segment (opcode = 0)
- template <GDCM_TYPENAME EntryType>
+ template <typename EntryType>
class DiscreteSegment : public Segment<EntryType> {
public:
+#if !defined(__sun__)
typedef typename Segment<EntryType>::SegmentMap SegmentMap;
+#endif
DiscreteSegment(const EntryType* first)
: Segment<EntryType>(first, first+2+*(first+1)) {}
virtual bool Expand(const SegmentMap&,
};
// linear segment (opcode = 1)
- template <GDCM_TYPENAME EntryType>
+ template <typename EntryType>
class LinearSegment : public Segment<EntryType> {
public:
+#if !defined(__sun__)
typedef typename Segment<EntryType>::SegmentMap SegmentMap;
+#endif
LinearSegment(const EntryType* first)
: Segment<EntryType>(first, first+3) {}
virtual bool Expand(const SegmentMap&,
};
// indirect segment (opcode = 2)
- template <GDCM_TYPENAME EntryType>
+ template <typename EntryType>
class IndirectSegment : public Segment<EntryType> {
public:
+#if !defined(__sun__)
typedef typename Segment<EntryType>::SegmentMap SegmentMap;
+#endif
IndirectSegment(const EntryType* first)
: Segment<EntryType>(first, first+2+4/sizeof(EntryType)) {}
virtual bool Expand(const SegmentMap& instances,
= (*pOffset) | (static_cast<unsigned long>(*(pOffset + 1)) << 16);
const EntryType* copied_part_head
= first_segment + offsetBytes / sizeof(EntryType);
- typename SegmentMap::const_iterator ppHeadSeg = instances.find(copied_part_head);
+ GDCM_TYPENAME SegmentMap::const_iterator ppHeadSeg = instances.find(copied_part_head);
if ( ppHeadSeg == instances.end() ) {
// referred segment not found
return false;
}
EntryType nNumCopies = *(this->_first + 1);
- typename SegmentMap::const_iterator ppSeg = ppHeadSeg;
- while ( std::distance(ppHeadSeg, ppSeg) < nNumCopies ) {
+ GDCM_TYPENAME SegmentMap::const_iterator ppSeg = ppHeadSeg;
+ while ( mydistance(ppHeadSeg, ppSeg) < nNumCopies ) {
+ // assert( mydistance(ppHeadSeg, ppSeg) == std::distance(ppHeadSeg, ppSeg) );
assert( ppSeg != instances.end() );
ppSeg->second->Expand(instances, expanded);
++ppSeg;
}
};
- template <GDCM_TYPENAME EntryType>
+ template <typename EntryType>
void ExpandPalette(const EntryType* raw_values, uint32_t length,
std::vector<EntryType>& palette)
{
typedef std::deque<Segment<EntryType>*> SegmentList;
SegmentList segments;
const EntryType* raw_seg = raw_values;
- while ( (std::distance(raw_values, raw_seg) * sizeof(EntryType)) <length ) {
+ while ( (mydistance(raw_values, raw_seg) * sizeof(EntryType)) < length ) {
+ // assert( mydistance(raw_values, raw_seg) == std::distance(raw_values, raw_seg) );
Segment<EntryType>* segment = NULL;
if ( *raw_seg == 0 ) {
segment = new DiscreteSegment<EntryType>(raw_seg);
// invalid opcode
break;
}
+
}
- typename Segment<EntryType>::SegmentMap instances;
+ GDCM_TYPENAME Segment<EntryType>::SegmentMap instances;
std::transform(segments.begin(), segments.end(),
- std::inserter(instances, instances.end()), typename Segment<EntryType>::ToMap());
- typename SegmentList::iterator ppSeg = segments.begin();
- typename SegmentList::iterator endOfSegments = segments.end();
+ std::inserter(instances, instances.end()), GDCM_TYPENAME2 Segment<EntryType>::ToMap());
+ GDCM_TYPENAME SegmentList::iterator ppSeg = segments.begin();
+ GDCM_TYPENAME SegmentList::iterator endOfSegments = segments.end();
for ( ; ppSeg != endOfSegments; ++ppSeg ) {
(*ppSeg)->Expand(instances, palette);
}