segmentation
All Data Structures Namespaces Files Functions Variables Modules Pages
BgImage.cpp
1 // Name: BgImage.cpp
3 // Purpose: BgImage class functions
4 // Author: Bogdan Georgescu
5 // Modified by:
6 // Created: 06/22/2000
7 // Copyright: (c) Bogdan Georgescu
8 // Version: v0.1
10 
11 #include "edge/BgImage.h"
12 
13 BgImage::BgImage()
14 {
15  hasIm_ = false;
16  colorIm_ = false;
17  im_ = 0;
18 }
19 
20 BgImage::BgImage(int x,int y, bool colorIm)
21 {
22  colorIm_ = colorIm;
23  if (colorIm_ == false)
24  im_ = new unsigned char[x*y];
25  else
26  im_ = new unsigned char[x*y*3];
27  x_ = x;
28  y_ = y;
29  hasIm_ = true;
30 }
31 
32 BgImage::~BgImage()
33 {
34  CleanData();
35 }
36 
37 void BgImage::CleanData()
38 {
39  if (hasIm_)
40  {
41  delete [] im_;
42  x_ = y_ = 0;
43  hasIm_ = false;
44  colorIm_ = false;
45  }
46 }
47 
48 void BgImage::SetImage(unsigned char* im, int x, int y, bool colorIm)
49 {
50  CleanData();
51  colorIm_ = colorIm;
52  if (colorIm_ == false)
53  im_ = new unsigned char[x*y];
54  else
55  im_ = new unsigned char[x*y*3];
56  x_ = x;
57  y_ = y;
58  hasIm_ = true;
59 
60  int i;
61  unsigned char *its, *itd;
62  if (colorIm_ == false)
63  {
64  for (i=0, its=im, itd=im_; i<x*y; i++)
65  {
66  *(itd++) = *(its++);
67  }
68  }
69  else
70  {
71  for (i=0, its=im, itd=im_; i<x*y*3; i++)
72  {
73  *(itd++) = *(its++);
74  }
75  }
76 
77 }
78 
79 void BgImage::SetImageFromRGB(unsigned char* im, int x, int y, bool colorIm)
80 {
81  PrivateResize(x, y, colorIm);
82 
83  int i;
84  unsigned char *its, *itd;
85  if (colorIm_ == false)
86  {
87  for (i=0, its=im, itd=im_; i<x*y; i++, itd++, its+=3)
88  {
89  *itd = (int) (its[0]*RED_WEIGHT + its[1]*GREEN_WEIGHT + its[2]*BLUE_WEIGHT);
90  }
91  }
92  else
93  {
94  for (i=0, its=im, itd=im_; i<x*y*3; i++)
95  *(itd++) = *(its++);
96  }
97 }
98 
99 void BgImage::SetSameImageFromRGB(unsigned char* im)
100 {
101  int i;
102  unsigned char *its, *itd;
103  if (colorIm_ == false)
104  {
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);
107  }
108  else
109  {
110  for (i=0, its=im, itd=im_; i<x_*y_*3; i++)
111  *(itd++) = *(its++);
112  }
113 }
114 
115 void BgImage::SetImage(short* im, int x, int y, bool colorIm)
116 {
117  CleanData();
118  colorIm_ = colorIm;
119  if (colorIm_ == false)
120  im_ = new unsigned char[x*y];
121  else
122  im_ = new unsigned char[x*y*3];
123  x_ = x;
124  y_ = y;
125  hasIm_ = true;
126 
127  int i;
128  unsigned char *itd;
129  short* its;
130  if (colorIm_ == false)
131  {
132  for (i=0, its=im, itd=im_; i<x*y; i++)
133  {
134  *(itd++) = (unsigned char) *(its++);
135  }
136  }
137  else
138  {
139  for (i=0, its=im, itd=im_; i<x*y*3; i++)
140  {
141  *(itd++) = (unsigned char) *(its++);
142  }
143  }
144 }
145 
146 void BgImage::GetImage(unsigned char* im)
147 {
148  unsigned char *its, *itd;
149  int i;
150  if (colorIm_ == false)
151  {
152  for(i=0, its=im_, itd=im; i<x_*y_; i++)
153  *(itd++) = *(its++);
154  }
155  else
156  {
157  for(i=0, its=im_, itd=im; i<x_*y_*3; i++)
158  *(itd++) = *(its++);
159  }
160 }
161 
162 void BgImage::GetImageColor(unsigned char* im)
163 {
164  unsigned char *its, *itd;
165  int i;
166  if (colorIm_ == false)
167  {
168  for(i=0, its=im_, itd=im; i<x_*y_; i++)
169  {
170  *(itd++) = *its;
171  *(itd++) = *its;
172  *(itd++) = *(its++);
173  }
174  }
175  else
176  {
177  for(i=0, its=im_, itd=im; i<(x_*y_*3); i++)
178  *(itd++) = *(its++);
179  }
180 }
181 
182 void BgImage::GetImageBW(unsigned char* im)
183 {
184  unsigned char *its, *itd;
185  int i;
186  if (colorIm_ == false)
187  {
188  for(i=0, its=im_, itd=im; i<x_*y_; i++)
189  *(itd++) = *(its++);
190  }
191  else
192  {
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);
195  }
196 }
197 
198 void BgImage::GetImageR(unsigned char* im)
199 {
200  unsigned char *its, *itd;
201  int i;
202  if (colorIm_ == false)
203  {
204  for(i=0, its=im_, itd=im; i<x_*y_; i++)
205  *(itd++) = *(its++);
206  }
207  else
208  {
209  for(i=0, its=im_, itd=im; i<x_*y_; i++)
210  {
211  *(itd++) = *its;
212  its += 3;
213  }
214  }
215 }
216 
217 void BgImage::GetImageG(unsigned char* im)
218 {
219  unsigned char *its, *itd;
220  int i;
221  if (colorIm_ == false)
222  {
223  for(i=0, its=im_, itd=im; i<x_*y_; i++)
224  *(itd++) = *(its++);
225  }
226  else
227  {
228  for(i=0, its=im_+1, itd=im; i<x_*y_; i++)
229  {
230  *(itd++) = *its;
231  its += 3;
232  }
233  }
234 }
235 
236 void BgImage::GetImageB(unsigned char* im)
237 {
238  unsigned char *its, *itd;
239  int i;
240  if (colorIm_ == false)
241  {
242  for(i=0, its=im_, itd=im; i<x_*y_; i++)
243  *(itd++) = *(its++);
244  }
245  else
246  {
247  for(i=0, its=im_+2, itd=im; i<x_*y_; i++)
248  {
249  *(itd++) = *its;
250  its += 3;
251  }
252  }
253 }
254 
255 inline unsigned char BgImage::operator()(int r, int c) const
256 {
257  assert(hasIm_ && (r >= 0) && (r < y_) && (c >= 0) && (c < x_));
258  return im_[c+r*x_];
259 }
260 
261 inline unsigned char& BgImage::operator()(int r, int c)
262 {
263  assert(hasIm_ && (r >= 0) && (r < y_) && (c >= 0) && (c < x_));
264  return im_[c+r*x_];
265 }
266 
267 unsigned char BgImage::PixelValue(int r, int c)
268 {
269  assert(hasIm_ && (r >= 0) && (r < y_) && (c >= 0) && (c < x_));
270  return im_[c+r*x_];
271 }
272 
273 inline unsigned char gBgImPt(BgImage* in_im, int in_r, int in_c)
274 {
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_];
277 }
278 
279 bool BgImage::ValidCoord(int in_x, int in_y)
280 {
281  return ((in_x>=0) && (in_x<x_) && (in_y>=0) && (in_y<y_));
282 }
283 
284 int BgImage::ValidReturnBW(int in_x, int in_y, int& cval)
285 {
286  if ((in_x>=0) && (in_x<x_) && (in_y>=0) && (in_y<y_))
287  {
288  cval = im_[in_x+in_y*x_];
289  return 1;
290  } else
291  {
292  cval = -1;
293  return 0;
294  }
295 }
296 int BgImage::ValidReturnCol(int in_x, int in_y, int& rval, int& gval, int& bval)
297 {
298  if ((in_x>=0) && (in_x<x_) && (in_y>=0) && (in_y<y_))
299  {
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)];
303 
304  return 1;
305  } else
306  {
307  rval = gval = bval = -1;
308  return 0;
309  }
310 }
311 
312 int BgImage::ReturnCol(int in_x, int in_y, int& rval, int& gval, int& bval)
313 {
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)];
317  return 1;
318 }
319 
320 bool BgImage::IsAllocated(void) const
321 {
322  return hasIm_;
323 }
324 
325 const BgImage& BgImage::operator=(const BgImage& im)
326 {
327  if (this == &im)
328  return *this;
329 
330  if (!im.IsAllocated())
331  {
332  CleanData();
333  return *this;
334  }
335 
336  PrivateCopyToThis(im);
337  return *this;
338 }
339 
340 void BgImage::PrivateCopyToThis(const BgImage& im)
341 {
342  PrivateResize(im.x_, im.y_, im.colorIm_);
343  int ncopy, i;
344  ncopy = x_*y_;
345  if (colorIm_ == true)
346  ncopy *= 3;
347 
348  unsigned char *src;
349  src = im.im_;
350  for (i=0; i<ncopy; i++)
351  im_[i] = src[i];
352 }
353 
354 void BgImage::PrivateResize(int width, int height, bool color)
355 {
356  if ((hasIm_ == false) || (width != x_) || (height != y_) || (color != colorIm_))
357  {
358  CleanData();
359  x_ = width;
360  y_ = height;
361  colorIm_ = color;
362  if (color == false)
363  im_ = new unsigned char[x_*y_];
364  else
365  im_ = new unsigned char[x_*y_*3];
366  hasIm_ = true;
367  }
368 }
369 
370 void BgImage::Resize(int width, int height, bool color)
371 {
372  PrivateResize(width, height, color);
373 }