#ifndef TIMAGE_HPP
#define TIMAGE_HPP
#include <cstring>
#include <stdlib.h>
#include "Image.h"
NAMESPACE_LERC_START
class CntZ
{
public:
float cnt, z;
bool operator == (const CntZ& cz) const { return cnt == cz.cnt && z == cz.z; }
bool operator != (const CntZ& cz) const { return cnt != cz.cnt || z != cz.z; }
void operator += (const CntZ& cz) { cnt += cz.cnt; z += cz.z; }
};
template< class Element >
class TImage : public Image
{
public:
TImage() : data_(0) {}
TImage(const TImage& tImg) : data_(0) { type_ = tImg.type_; *this = tImg; }
virtual ~TImage() { clear(); }
virtual TImage& operator=(const TImage& tImg);
bool resize(int width, int height);
virtual void clear();
Element getPixel(int row, int col) const { return data_[row * width_ + col]; }
const Element& operator() (int row, int col) const { return data_[row * width_ + col]; }
const Element* getData() const { return data_; }
void setPixel(int row, int col, Element element) { data_[row * width_ + col] = element; }
Element& operator() (int row, int col) { return data_[row * width_ + col]; }
Element* getData() { return data_; }
bool operator == (const Image& img) const;
bool operator != (const Image& img) const { return !operator==(img); };
protected:
Element* data_;
};
template< class Element >
bool TImage< Element >::resize(int width, int height)
{
if (width <= 0 || height <= 0)
return false;
if (width == width_ && height == height_ && data_)
return true;
free(data_);
width_ = 0;
height_ = 0;
data_ = (Element*)malloc(width * height * sizeof(Element));
if (!data_)
return false;
width_ = width;
height_ = height;
return true;
}
template< class Element >
void TImage< Element >::clear()
{
free(data_);
data_ = 0;
width_ = 0;
height_ = 0;
}
template< class Element >
TImage< Element >& TImage< Element >::operator = (const TImage& tImg)
{
if (this == &tImg) return *this;
if (!resize(tImg.getWidth(), tImg.getHeight()))
return *this;
if (data_ && tImg.data_)
{
memcpy(data_, tImg.data_, getSize() * sizeof(Element));
Image::operator=(tImg);
}
return *this;
}
template< class Element >
bool TImage< Element >::operator == (const Image& img) const
{
if (!Image::operator == (img)) return false;
const Element* ptr0 = getData();
const Element* ptr1 = ((const TImage&)img).getData();
int cnt = getSize();
while (cnt--)
if (*ptr0++ != *ptr1++)
return false;
return true;
}
NAMESPACE_LERC_END
#endif