11 #include "edge/BgImage.h"
20 BgImage::BgImage(
int x,
int y,
bool colorIm)
23 if (colorIm_ ==
false)
24 im_ =
new unsigned char[x*y];
26 im_ =
new unsigned char[x*y*3];
37 void BgImage::CleanData()
48 void BgImage::SetImage(
unsigned char* im,
int x,
int y,
bool colorIm)
52 if (colorIm_ ==
false)
53 im_ =
new unsigned char[x*y];
55 im_ =
new unsigned char[x*y*3];
61 unsigned char *its, *itd;
62 if (colorIm_ ==
false)
64 for (i=0, its=im, itd=im_; i<x*y; i++)
71 for (i=0, its=im, itd=im_; i<x*y*3; i++)
79 void BgImage::SetImageFromRGB(
unsigned char* im,
int x,
int y,
bool colorIm)
81 PrivateResize(x, y, colorIm);
84 unsigned char *its, *itd;
85 if (colorIm_ ==
false)
87 for (i=0, its=im, itd=im_; i<x*y; i++, itd++, its+=3)
89 *itd = (int) (its[0]*RED_WEIGHT + its[1]*GREEN_WEIGHT + its[2]*BLUE_WEIGHT);
94 for (i=0, its=im, itd=im_; i<x*y*3; i++)
99 void BgImage::SetSameImageFromRGB(
unsigned char* im)
102 unsigned char *its, *itd;
103 if (colorIm_ ==
false)
105 for (i=0, its=im, itd=im_; i<x_*y_; i++, itd++, its+=3)
106 *itd = (
int) (its[0]*RED_WEIGHT + its[1]*GREEN_WEIGHT + its[2]*BLUE_WEIGHT);
110 for (i=0, its=im, itd=im_; i<x_*y_*3; i++)
115 void BgImage::SetImage(
short* im,
int x,
int y,
bool colorIm)
119 if (colorIm_ ==
false)
120 im_ =
new unsigned char[x*y];
122 im_ =
new unsigned char[x*y*3];
130 if (colorIm_ ==
false)
132 for (i=0, its=im, itd=im_; i<x*y; i++)
134 *(itd++) = (
unsigned char) *(its++);
139 for (i=0, its=im, itd=im_; i<x*y*3; i++)
141 *(itd++) = (
unsigned char) *(its++);
146 void BgImage::GetImage(
unsigned char* im)
148 unsigned char *its, *itd;
150 if (colorIm_ ==
false)
152 for(i=0, its=im_, itd=im; i<x_*y_; i++)
157 for(i=0, its=im_, itd=im; i<x_*y_*3; i++)
162 void BgImage::GetImageColor(
unsigned char* im)
164 unsigned char *its, *itd;
166 if (colorIm_ ==
false)
168 for(i=0, its=im_, itd=im; i<x_*y_; i++)
177 for(i=0, its=im_, itd=im; i<(x_*y_*3); i++)
182 void BgImage::GetImageBW(
unsigned char* im)
184 unsigned char *its, *itd;
186 if (colorIm_ ==
false)
188 for(i=0, its=im_, itd=im; i<x_*y_; i++)
193 for (i=0, its=im_, itd=im; i<x_*y_; i++, itd++, its+=3)
194 *itd = (
int) (its[0]*RED_WEIGHT + its[1]*GREEN_WEIGHT + its[2]*BLUE_WEIGHT);
198 void BgImage::GetImageR(
unsigned char* im)
200 unsigned char *its, *itd;
202 if (colorIm_ ==
false)
204 for(i=0, its=im_, itd=im; i<x_*y_; i++)
209 for(i=0, its=im_, itd=im; i<x_*y_; i++)
217 void BgImage::GetImageG(
unsigned char* im)
219 unsigned char *its, *itd;
221 if (colorIm_ ==
false)
223 for(i=0, its=im_, itd=im; i<x_*y_; i++)
228 for(i=0, its=im_+1, itd=im; i<x_*y_; i++)
236 void BgImage::GetImageB(
unsigned char* im)
238 unsigned char *its, *itd;
240 if (colorIm_ ==
false)
242 for(i=0, its=im_, itd=im; i<x_*y_; i++)
247 for(i=0, its=im_+2, itd=im; i<x_*y_; i++)
255 inline unsigned char BgImage::operator()(
int r,
int c)
const
257 assert(hasIm_ && (r >= 0) && (r < y_) && (c >= 0) && (c < x_));
261 inline unsigned char& BgImage::operator()(
int r,
int c)
263 assert(hasIm_ && (r >= 0) && (r < y_) && (c >= 0) && (c < x_));
267 unsigned char BgImage::PixelValue(
int r,
int c)
269 assert(hasIm_ && (r >= 0) && (r < y_) && (c >= 0) && (c < x_));
273 inline unsigned char gBgImPt(BgImage* in_im,
int in_r,
int in_c)
275 assert(in_im->hasIm_ && (in_r >= 0) && (in_r < in_im->y_) && (in_c >= 0) && (in_c < in_im->x_));
276 return in_im->im_[in_c+in_r*in_im->x_];
279 bool BgImage::ValidCoord(
int in_x,
int in_y)
281 return ((in_x>=0) && (in_x<x_) && (in_y>=0) && (in_y<y_));
284 int BgImage::ValidReturnBW(
int in_x,
int in_y,
int& cval)
286 if ((in_x>=0) && (in_x<x_) && (in_y>=0) && (in_y<y_))
288 cval = im_[in_x+in_y*x_];
296 int BgImage::ValidReturnCol(
int in_x,
int in_y,
int& rval,
int& gval,
int& bval)
298 if ((in_x>=0) && (in_x<x_) && (in_y>=0) && (in_y<y_))
300 rval = im_[in_x*3+0+in_y*(x_*3)];
301 gval = im_[in_x*3+1+in_y*(x_*3)];
302 bval = im_[in_x*3+2+in_y*(x_*3)];
307 rval = gval = bval = -1;
312 int BgImage::ReturnCol(
int in_x,
int in_y,
int& rval,
int& gval,
int& bval)
314 rval = im_[in_x*3+0+in_y*(x_*3)];
315 gval = im_[in_x*3+1+in_y*(x_*3)];
316 bval = im_[in_x*3+2+in_y*(x_*3)];
320 bool BgImage::IsAllocated(
void)
const
325 const BgImage& BgImage::operator=(
const BgImage& im)
330 if (!im.IsAllocated())
336 PrivateCopyToThis(im);
340 void BgImage::PrivateCopyToThis(
const BgImage& im)
342 PrivateResize(im.x_, im.y_, im.colorIm_);
345 if (colorIm_ ==
true)
350 for (i=0; i<ncopy; i++)
354 void BgImage::PrivateResize(
int width,
int height,
bool color)
356 if ((hasIm_ ==
false) || (width != x_) || (height != y_) || (color != colorIm_))
363 im_ =
new unsigned char[x_*y_];
365 im_ =
new unsigned char[x_*y_*3];
370 void BgImage::Resize(
int width,
int height,
bool color)
372 PrivateResize(width, height, color);