33 template <
typename T >
35 class ImageRLE :
public ImageBase
40 typedef typename std::list<RepeatedValue> Line;
41 typedef typename std::vector<Line> Data;
117 for (
typename Line::const_iterator iRepValue = line.begin(); iRepValue != line.end(); ++iRepValue)
119 std::cout <<
"Value : " << iRepValue->getValue() <<
" ";
120 std::cout <<
"Repeat : " << iRepValue->getRepeat() <<
", ";
122 std::cout << std::endl;
128 for (
typename Data::const_iterator iLine = m_data.begin(); iLine != m_data.end(); ++iLine)
136 if (!((this->
dim()[0] == im.
dim()[0]) &&
137 (this->
dim()[1] == im.
dim()[1]) &&
138 (this->
dim()[2] == im.
dim()[2]) &&
139 (this->
vdim()[0] == im.
vdim()[0]) &&
140 (this->
vdim()[1] == im.
vdim()[1]) &&
143 throw std::invalid_argument(
"Incompatible images");
148 typename Data::iterator myData = m_data.begin();
149 typename Data::const_iterator imData = im.m_data.begin();
150 for(; myData != m_data.end(); ++myData, ++imData)
153 myData->assign(imData->begin(), imData->end());
176 typedef int t_repeat;
178 RepeatedValue() : m_value(), m_repeat(0) {};
179 RepeatedValue(
const T &v, t_repeat r) : m_value(v), m_repeat(r) {};
183 const t_repeat & getRepeat()
const {
return m_repeat; }
184 t_repeat & getRepeat() {
return m_repeat; }
185 const T &
getValue()
const {
return m_value; }
203 int nLine = this->
dim()[1] * this->
dim()[2];
204 m_data.resize(nLine);
209 RepeatedValue zeroLine()
212 zeros.getValue() = T();
213 zeros.getRepeat() = this->
dim()[0];
223 typename Line::iterator & iLine,
239 Line & getLine(
int y,
int z)
241 return m_data[y + z * this->
dim()[1]];
243 const Line & getLine(
int y,
int z)
const 245 return m_data[y + z * this->
dim()[1]];
249 int lineLength(
const Line & line)
252 typename Line::const_iterator iRepValue = line.begin();
253 for (; iRepValue != line.end(); ++iRepValue)
255 count += iRepValue->getRepeat();
268 template <
typename T >
314 template <
typename T >
320 template <
typename T >
340 template <
typename T >
343 this->
init(x, y, z, vx, vy, vz);
346 template <
typename T >
350 this->_init(x, y, z, vx, vy, vz);
352 this->allocateData();
358 template <
typename T >
361 this->
init(dim, vDim);
364 template <
typename T >
370 template <
typename T >
377 template <
typename T >
383 template <
typename T >
390 template <
typename T >
397 template <
typename T >
404 template <
typename T >
408 RepeatedValue zeros = this->zeroLine();
411 typename std::list<RepeatedValue> emptyLine(1, zeros);
414 int nLine = this->
dim()[1] * this->
dim()[2];
417 m_data.assign(nLine, emptyLine);
420 template <
typename T >
434 template <
typename T >
443 throw std::out_of_range(
"Position out of image range");
448 template <
typename T >
453 const Line & line = this->getLine(j,k);
455 typename std::list<RepeatedValue>::const_iterator iRepValue;
457 for (iRepValue = line.begin(); iRepValue != line.end(); ++iRepValue)
459 current_i += iRepValue->getRepeat();
462 return iRepValue->getValue();
467 std::cerr <<
"Problem in ImageRLE probably due to a bug\n";
468 std::cerr <<
"Coordinate asked: " << i <<
" on line ( " << j <<
" , " << k <<
" ) \n";
469 std::cerr <<
"Max coordinate reached: " << current_i <<
"\n";
470 std::cerr <<
"Current line: \n";
471 for (iRepValue = line.begin(); iRepValue != line.end(); ++iRepValue)
473 std::cerr <<
"Repeat: " << iRepValue->getRepeat() <<
", value: " << iRepValue->getValue() <<
"\n";
475 std::cerr << std::endl;
477 throw std::underflow_error(
"Incomplete image -- cannot return a value");
482 template <
typename T >
491 throw std::out_of_range(
"Index out of range");
508 template <
typename T >
513 typename Line::iterator & iRepValue,
518 if (value == iRepValue->getValue())
return;
522 typename Line::iterator iRepValuePrevious = iRepValue;
523 bool atBeginning = (iRepValue == line.begin());
524 if (!atBeginning) --iRepValuePrevious;
525 bool atEnd = (iRepValue == line.end());
526 typename Line::iterator iRepValueNext = iRepValue;
531 if (iRepValue->getRepeat() == 1)
541 if (!atBeginning && iRepValueNext->getValue() == value)
543 remaining += iRepValueNext->getRepeat();
545 if (!atEnd && iRepValuePrevious->getValue() == value)
548 iRepValueNext->getRepeat() += iRepValuePrevious->getRepeat()+1;
549 line.erase(iRepValuePrevious);
554 ++(iRepValueNext->getRepeat());
556 line.erase(iRepValue);
557 iRepValue = iRepValueNext;
562 else if (!atBeginning && iRepValuePrevious->getValue() == value)
565 line.erase(iRepValue);
566 ++(iRepValuePrevious->getRepeat());
568 iRepValue = iRepValuePrevious;
577 iRepValue->getValue() = value;
586 else if (remaining == 1)
592 --(iRepValue->getRepeat());
597 if (atEnd || iRepValueNext->getValue() != value)
605 line.insert(iRepValueNext, RepeatedValue(value, 1));
613 remaining = ++(iRepValueNext->getRepeat());
614 iRepValue = iRepValueNext;
619 else if (remaining == iRepValue->getRepeat())
625 --(iRepValue->getRepeat());
630 if (!atBeginning && iRepValuePrevious->getValue() == value)
633 ++(iRepValuePrevious->getRepeat());
635 iRepValue = iRepValuePrevious;
646 line.insert(iRepValue, RepeatedValue(value, 1));
655 line.insert(iRepValue, RepeatedValue(iRepValue->getValue(), iRepValue->getRepeat()-remaining));
657 line.insert(iRepValue, RepeatedValue(value, 1));
659 iRepValue->getRepeat() = remaining-1;
670 template <
typename T >
674 Line & line = this->getLine(j,k);
678 typename Line::iterator iRepValue;
679 for (iRepValue = line.begin(); iRepValue != line.end(); ++iRepValue)
681 current_i += iRepValue->getRepeat();
686 int tmp = current_i-i;
689 if (this->
dim()[0] != this->lineLength(line))
691 throw std::runtime_error(
"ImageRLE length not preserved");
699 throw std::underflow_error(
"Incomplete ImageRLE (probably a bug) -- cannot set value");
A trait class to assign iterators to image types.
ConstLinearIterator< ImageRLE< T > > ConstLinear
bool contains(const numeric_array< int, 3 > &p) const
void set_dim(const numeric_array< int, 3 > &dim)
const numeric_array< t_voxsize, 3 > & vdim() const
get voxel size
ImageParameter param(const TImage &im)
Create an ImageParameter structure out of an Image.
Collects image information to create similar images.
void setValue(T value, const numeric_array< int, 3 > &v)
Image class using run-length encoded data.
Belongs to package Box Do not include directly, include til/Box.h instead.
INLINE T operator()(int i, int j, int k)
bool isAllocated() const
Check whether data has been allocated or not.
General macros, definitions and functions.
T getUnsafeValue(const numeric_array< int, 3 > &v) const
numeric_array< t_voxsize, 3 > m_vDim
Voxel size.
INLINE const T getValue(int i, int j, int k) const
Get the value at point (i,j,k) for read-only purpose.
void debugLine(const Line &line) const
void set_vdim(const numeric_array< t_voxsize, 3 > &vdim)
Set the voxel coordinates.
T operator()(const numeric_array< int, 3 > &v)
INLINE void setUnsafeValue(T value, int i, int j, int k)
void debug() const
for debugging purposes
INLINE void setValue(T value, int i, int j, int k)
void copy(const Self &im)
const numeric_array< int, 3 > & dim() const
get image dimension
Collects common code accross all image classes.
const T getValue(const numeric_array< int, 3 > &v) const
float t_voxsize
type of voxel size
T getUnsafeValue(int i, int j, int k) const
Get the value at point (i,j,k) for fast read-only purpose.
numeric_array< int, 3 > m_dim
Image dimensions.
void reset()
Set all values to zero.
LinearIterator< ImageRLE< T > > Linear