16 class Image :
public std::vector<T>
19 typedef typename std::vector<T>::iterator
iterator;
25 Image(
int sx,
int sy,
const T &value)
32 void setSize(
int nx,
int ny,
const T &value);
39 const T *
getData()
const {
return &(*std::vector<T>::begin());}
40 T *
getData() {
return &(*std::vector<T>::begin());}
44 int getSize()
const {
return std::vector<T>::size();}
60 return std::vector<T>::at(
getIndex(x,y));
63 std::vector<T>::at(
getIndex(x,y)) = value;
83 void addLine(
int x0,
int y0,
int x1,
int y1,
const T& value);
96 for(
int i=
getSize();i>0;i--,dd++,sd++)
108 int si = horiz ?
getSizeX()-i-1 : i;
121 int endX =
sizeX-ksx;
122 int endY =
sizeY-ksy;
127 for(
int i=ksy;i<endY;i++)
129 for(
int j=ksx;j<endX;j++,dstdat++,srcdat++)
132 const S *kdat = kernel.
getData();
133 const T *sdat = srcdat;
134 for(
int l=0;l<kernel.
getSizeY();l++)
136 for(
int k=0;k<kernel.
getSizeX();k++, kdat++, sdat++)
153 if(!ncomp) ncomp = img.size();
154 const int comp = img.size()>ncomp ? ncomp : img.size();
155 const int size = img[0].size();
158 for(
int a=0; a<comp; a++) bi[a] = img[a].begin();
160 const int cvinc = ncomp-comp;
161 for(
int i=0; i<size; i++) {
162 for(
int a=0; a<comp; a++) {
175 int stepx = (int)round(1/sx);
if(stepx<=0) stepx=1;
176 int stepy = (int)round(1/sy);
if(stepy<=0) stepy=1;
180 for(
int j=0; j<nimg.
sizeY; j++) {
182 for(
int i=0; i<nimg.
sizeX; i++, dst++, src+=stepx) {
185 src = linebegin+(stepy*
sizeX);
193 friend std::ostream& operator<<(std::ostream& os, const Image<T>& rhs) {
194 os << rhs.
sizeX <<
" " << rhs.
sizeY << std::endl;
196 for(const_iterator pnt = rhs.begin(); pnt != rhs.end(); pnt++) {
199 std::cout << std::endl;
201 }
else std::cout <<
" ";
207 std::fstream ofile(fname.c_str(), std::ios::binary|std::ios::out);
212 if(!os)
return false;
214 os.write((
const char*)&std::vector<T>::front(),
sizeX*
sizeY);
219 std::fstream ifile(fname.c_str(), std::ios::binary|std::ios::in);
224 if(!is)
return false;
227 std::vector<T>::resize(sizeX*sizeY);
228 is.read((
char*)&std::vector<T>::front(), sizeX*sizeY);
236 for(iterator pnt = rhs.begin(); is && pnt != rhs.end(); pnt++)
241 void insert(
const Image<T> &ii,
const int x = 0,
const int y = 0);
270 if(!std::vector<T>::empty() && nx==
sizeX && ny==
sizeY)
return;
273 std::vector<T>::clear();
274 if(nx*ny) std::vector<T>::resize(nx*ny);
282 std::vector<T>::clear();
283 if(nx*ny) resize(nx*ny, value);
289 if(&rhs ==
this)
return *
this;
290 std::vector<T>::operator=(rhs);
299 for(
iterator pnt = std::vector<T>::begin(); pnt != std::vector<T>::end(); pnt++, rhs++)
313 if(!std::vector<T>::empty() &&
sizeX>0 &&
sizeY>0)
315 for(
iterator pnt = std::vector<T>::begin(); pnt != std::vector<T>::end(); pnt++)
324 if(std::vector<T>::empty())
return 0;
327 for(
int i=
getSize(); i>0; i--, pnt++)
328 if(*pnt>max) max = *pnt;
335 if(std::vector<T>::empty())
return 0;
338 for(
int i=
getSize(); i>0; i--, pnt++)
339 if(*pnt<min) min = *pnt;
348 for(
int i=
getSize(); i>0; i--, pnt++)
359 for(
int i=
getSize(); i>0; i--, pnt++)
368 for(
int i=
getSize(); i>0; i--, pnt++)
377 for(
int i=
getSize(); i>0; i--, pnt++)
386 for(
int i=
getSize(); i>0; i--, pnt++)
395 for(
int i=
getSize(); i>0; i--, pnt++)
405 for(
int i=
getSize(); i>0; i--, pnt++, rval++)
415 for(
int i=
getSize(); i>0; i--, pnt++)
425 for(
int i=
getSize(); i>0; i--, pnt++)
435 for(
int i=
getSize(); i>0; i--, pnt++)
443 if(std::vector<T>::empty())
return;
446 x0 += x1; x1 = x0-x1; x0 -= x1;
447 y0 += y1; y1 = y0-y1; y0 -= y1;
459 int dir = dx>
absint(dy) ? 1 : -1;
468 int diagInc = 2*(nx+ny*yinc);
474 int d = 2*nx + ny*yinc;
488 int diagInc = 2*(nx+ny*yinc);
489 int axisInc = 2*ny*yinc;
493 int d = 2*ny*yinc + nx;
514 bool bin = (binarize>0);
516 for(
int i=
getSize(); i>0; i--, pnt++)
517 if(*pnt<th) *pnt = 0;
518 else if (bin) *pnt = T(binarize);
526 for(
int i=
getSize(); i>0; i--, pnt++)
527 if(isnan(*pnt)) *pnt = val;
537 int endX =
sizeX-ksx;
538 int endY =
sizeY-ksy;
539 int offset = ksy*
sizeX+ksx;
540 int winskip =
sizeX-env;
543 for(
int i=ksy;i<endY;i++)
545 for(
int j=ksx;j<endX;j++,srcdat++, maxpnt++)
548 const T *sdat = srcdat;
549 maxpnt = srcdat+offset;
550 for(
int l=0;l<env && ismax; l++)
552 for(
int k=0;k<env && ismax; k++, sdat++)
555 maxpnt != sdat) ismax=
false;
559 if(!ismax) *maxpnt = 0;
572 if(iwidth>0 && iheight>0)
574 for(
int j=0; j<iheight; j++) {
577 for(
int i=0; i<iwidth; i++, src++, dst++)
591 ni.
insert(*
this, brd, brd);
596 ni.
insert(*
this, brd, brd);
bool writePPM(const std::string &fname) const
int getBoundedIndex(int x, int y) const
bool readPPM(const std::string &fname)
bool readPPMstream(std::istream &is)
const Image< T > mirror(bool horiz=true, bool vert=true) const
std::vector< T >::iterator iterator
std::vector< T >::const_iterator const_iterator
Image< T > & operator-=(const T &v)
Image< T > & interleave(const std::vector< Image< T > > &img, dword ncomp=0)
Image< T > & operator=(const Image< T > &rhs)
const T findMax() const
calculate maximum
Image< T > & operator*=(const T &v)
const T findMin() const
calculate minimum
const T getPixel(int x, int y) const
Image< T > & zeroPad(int brd, const T &col=0, bool smoothbrd=false)
Image< T > & convertFrom(const Image< S > &rhs)
Image< T > & operator/=(const T &v)
Image< T > & operator+=(const T &v)
Image(int sx, int sy, const T &value)
friend std::istream & operator>>(std::istream &is, Image< T > &rhs)
bool writePPMstream(std::ostream &os) const
Image< T > scaleBy(float s)
Image< T > & threshold(const T &th, double binarize=0)
void setSize(int nx, int ny)
void insert(const Image< T > &ii, const int x=0, const int y=0)
const T * getData() const
Image< T > & findMaxima(int env=3)
int getIndex(int x, int y) const
bool sameSize(const Image< S > &rhs)
Image< T > scaleBy(float sx, float sy)
void setPixel(int x, int y, const T &value)
void addLine(int x0, int y0, int x1, int y1, const T &value)
Image< T > & copy(const Image< T > &rhs)
const Image< T > convolve(const Image< S > &kernel) const
Image< T > & unsetNAN(double val=0)
DMatrix< T > & sqrt(DMatrix< T > &mat)