Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ImageBaseInline.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 // #include <bias_config.h>
26 // #ifdef BIAS_HAVE_OPENEXR
27 // # ifdef WIN32
28 // # pragma warning( push, 1)
29 // # define PLATFORM_WINDOWS
30 // # endif // WIN32
31 // # include <half.h>
32 // # ifdef WIN32
33 // # pragma warning( pop)
34 // # endif // WIN32
35 // #endif
36 
37 
38 #if !defined(_IMAGE_BASE_H_INCLUDED_)
39 # error do not include ImageBaseInline here.
40 #endif
41 
42 //
43 // Check for valid x and y coordinates (if BIAS_DEBUG is defined)
44 // In case of error display the functionname
45 //
46 #ifdef BIAS_DEBUG
47 #define CheckCoordinates(x,y,functionname) \
48  if (x < 0 || x > (int)GetWidth()-1) { \
49  BIASERR("x-coordinate out of range in "<<functionname<<" "<<x); \
50  } \
51  if (y < 0 || y > (int)GetHeight()-1) { \
52  BIASERR("y-coordinate out of range in "<<functionname<<" "<<y); \
53  }
54 #else
55 #define CheckCoordinates(x,y,functionname)
56 #endif
57 
58 
59 // Check for valid channel nr. in Debug mode. Do nothing in Release
60 // In case of error display the functionname (JW 09/2003)
61 #ifdef BIAS_DEBUG
62 #define CheckChannel(channel,functionname) \
63  if (channel >= GetChannelCount()) { \
64  BIASERR("channel"<<channel<<" out of range in "<<functionname); \
65  }
66 #else
67 #define CheckChannel(channel,functionname)
68 #endif
69 
70 
71 
72 inline bool ImageBase::SamePixelAndChannelCount(const ImageBase& Image) const {
73  return ( (GetPixelCount() == Image.GetPixelCount()) &&
74  (GetChannelCount() == Image.GetChannelCount()) );
75 }
76 
77 inline bool ImageBase::NotBiggerPixelAndSameChannelCount(const ImageBase& Image) const {
78  return ( (GetPixelCount() <= Image.GetPixelCount()) &&
79  (GetChannelCount() == Image.GetChannelCount()) );
80 }
81 
82 inline void ImageBase::SetZero()
83 {
84  if (!IsEmpty()){
85  memset(GetImageData(), 0, GetWidthStep()*GetHeight());
86  }
87 }
88 
89 inline void ImageBase::ReleaseImageDataPointer()
90 {
91  delete[] (char**)ImageDataArray_;
92  delete[] (char*)ImageData_;
93  ImageDataArray_=NULL;
94  ImageData_=NULL;
95 }
96 
97 inline unsigned int
98 ImageBase::GetPixelPosition(unsigned int x, unsigned int y,
99  unsigned short int channel) const
100 {
101  CheckCoordinates((int)x, (int)y, "GetPixelPosition");
102 
103  if (IsPlanar())
104  return y*Width_+x+channel*GetPixelCount();
105  else
106  return y*Width_*ChannelCount_ +x*(ChannelCount_)+channel;
107 }
108 
109 bool ImageBase::IsPositionInImage(const int& x,const int& y) const{
110  return (x>=0 && x<(int)Width_ && y>=0 && y<(int)Height_);
111 }
112 
113 int ImageBase::MoveToClosestPositionInImage(int& x, int& y) const {
114 #ifdef BIAS_DEBUG
115  if (!((Width_>0)&&(Height_>0))) {
116  BIASERR("Image is not initialiozed correctly: "<<Width_<<"x"<<Height_);
117  BIASABORT;
118  }
119 #endif
120  int ret = 0;
121  if (x<0) {
122  x = 0;
123  ret = 1;
124  } else if (x>=(int)Width_) {
125  x = Width_-1;
126  ret = 2;
127  }
128  if (y<0) {
129  y = 0;
130  ret = 3;
131  } else if (y>=(int)Height_) {
132  y = Height_-1;
133  ret = 4;
134  }
135  return ret;
136 }
137 
138 
139 
140 inline void ImageBase::BIASToTextureCoordinates(const double& biasx,
141  const double& biasy,
142  double& gl_x,
143  double& gl_y) const {
144  // to compute relative coordinates, need image initialized:
145  BIASASSERT(Width_>0 && Height_>0);
146  gl_x = (biasx + 0.5) / double(Width_);
147  //gl_y = (double(Height_) - biasy - 0.5) / double(Height_);
148  gl_y = (biasy + 0.5) / double(Height_);
149 
150 //#ifdef BIAS_DEBUG
151  //static bool recursion = true;
152  //recursion = !recursion;
153  //if (recursion) {
154  // return;
155  //}
156  //// check consistency
157  //double biasx2, biasy2;
158  //TextureToBIASCoordinates(gl_x, gl_y, biasx2, biasy2);
159  /**
160  BIASASSERT(Equal(biasx, biasx2));
161  BIASASSERT(Equal(biasy, biasy2));
162  **/
163 //#endif
164 
165 }
166 
167 inline void ImageBase::TextureToBIASCoordinates(const double& gl_x,
168  const double& gl_y,
169  double& biasx,
170  double& biasy) const {
171  // to compute relative coordinates, need image initialized:
172  BIASASSERT(Width_>0 && Height_>0);
173  biasx = gl_x * double(Width_) - 0.5;
174  biasy = double(Height_) - (gl_y * double(Height_) + 0.5);
175 
176 #ifdef BIAS_DEBUG
177  static bool recursion = true;
178  recursion = !recursion;
179  if (recursion) {
180  return;
181  }
182  // check consitency
183  double gl_x2, gl_y2;
184  BIASToTextureCoordinates(biasx, biasy, gl_x2, gl_y2);
185  /**
186  BIASASSERT(Equal(gl_x2, gl_x));
187  BIASASSERT(Equal(gl_y2, gl_y));
188  **/
189 #endif
190 }
191 
192 template<class castType>
193 castType ImageBase::GetValue(const ImageBase & im,
194  const unsigned int x,
195  const unsigned int y,
196  const unsigned int channel)
197 {
198  BIASASSERT(im.IsInterleaved());
199  unsigned int C = im.GetChannelCount();
200  // determine storage type
201  switch ( im.GetStorageType())
202  {
203  case ImageBase::ST_unsignedchar: {
204  unsigned char** p = (unsigned char**)im.GetImageDataArray();
205  return static_cast<castType>(p[y][x*C+channel]);
206  break; }
207 
208  case ImageBase::ST_float: {
209  float** p = (float**)im.GetImageDataArray();
210  return static_cast<castType>(p[y][x*C+channel]);
211  break; }
212 
213 #ifdef BUILD_IMAGE_DOUBLE
214  case ImageBase::ST_double: {
215  double** p = (double**)im.GetImageDataArray();
216  return static_cast<castType>(p[y][x*C+channel]);
217  break; }
218 #endif
219 
220 
221 #ifdef BUILD_IMAGE_INT
222  case ImageBase::ST_int: {
223  int** p = (int**)im.GetImageDataArray();
224  return static_cast<castType>(p[y][x*C+channel]);
225  break; }
226 #endif
227 
228 #ifdef BUILD_IMAGE_UINT
229  case ImageBase::ST_unsignedint: {
230  unsigned int** p = (unsigned int**)im.GetImageDataArray();
231  return static_cast<castType>(p[y][x*C+channel]);
232  break; }
233 #endif
234 
235 #ifdef BUILD_IMAGE_SHORT
236  case ImageBase::ST_shortint: {
237  short** p = (short**)im.GetImageDataArray();
238  return static_cast<castType>(p[y][x*C+channel]);
239  break; }
240 #endif
241 
242 #ifdef BUILD_IMAGE_USHORT
243  case ImageBase::ST_unsignedshortint: {
244  unsigned short** p = (unsigned short**)im.GetImageDataArray();
245  return static_cast<castType>(p[y][x*C+channel]);
246  break; }
247 #endif
248 
249 #ifdef BUILD_IMAGE_CHAR // signed!
250  case ImageBase::ST_char: {
251  char** p = (char**)im.GetImageDataArray();
252  return static_cast<castType>(p[y][x*C+channel]);
253  break; }
254 #endif
255 
256  default:
257  {
258  BIASERR("unknown StorageType "<<im.GetStorageType()<<"detected");
259  }
260  };
261  return 0;
262 }
263 
264 template<class inputType>
265 void ImageBase::SetValue(const ImageBase & im,
266  const unsigned int x,
267  const unsigned int y,
268  const unsigned int channel,
269  const inputType val)
270 {
271  BIASASSERT(im.IsInterleaved());
272  unsigned int C = im.GetChannelCount();
273  // determine storage type
274  switch ( im.GetStorageType())
275  {
276  case ImageBase::ST_unsignedchar: {
277  unsigned char** p = (unsigned char**)im.GetImageDataArray();
278  p[y][x*C+channel] = static_cast<unsigned char>(val);
279  break; }
280 
281  case ImageBase::ST_float: {
282  float** p = (float**)im.GetImageDataArray();
283  p[y][x*C+channel] = static_cast<float>(val);
284  break; }
285 
286 #ifdef BUILD_IMAGE_DOUBLE
287  case ImageBase::ST_double: {
288  double** p = (double**)im.GetImageDataArray();
289  p[y][x*C+channel] = static_cast<double>(val);
290  break; }
291 #endif
292 
293 
294 #ifdef BUILD_IMAGE_INT
295  case ImageBase::ST_int: {
296  int** p = (int**)im.GetImageDataArray();
297  p[y][x*C+channel] = static_cast<int>(val);
298  break; }
299 #endif
300 
301 #ifdef BUILD_IMAGE_UINT
302  case ImageBase::ST_unsignedint: {
303  unsigned int** p = (unsigned int**)im.GetImageDataArray();
304  p[y][x*C+channel] = static_cast<unsigned int>(val);
305  break; }
306 #endif
307 
308 #ifdef BUILD_IMAGE_SHORT
309  case ImageBase::ST_shortint: {
310  short** p = (short**)im.GetImageDataArray();
311  p[y][x*C+channel] = static_cast<short>(val);
312  break; }
313 #endif
314 
315 #ifdef BUILD_IMAGE_USHORT
316  case ImageBase::ST_unsignedshortint: {
317  unsigned short** p = (unsigned short**)im.GetImageDataArray();
318  p[y][x*C+channel] = static_cast<unsigned short>(val);
319  break; }
320 #endif
321 
322 #ifdef BUILD_IMAGE_CHAR // signed!
323  case ImageBase::ST_char: {
324  char** p = (char**)im.GetImageDataArray();
325  p[y][x*C+channel] = static_cast<char>(val);
326  break; }
327 #endif
328 
329  default:
330  {
331  BIASERR("unknown StorageType "<<im.GetStorageType()<<"detected");
332  }
333  };
334 
335 }
336 
337 
338 template<class inputType>
339 void ImageBase::SetChannel(const BIAS::ImageBase & im,
340  const unsigned int channelId,
341  const inputType* channelIn)
342 {
343 
344  BIASASSERT(im.IsInterleaved());
345  unsigned int C = im.GetChannelCount();
346  unsigned int width = im.GetWidth();
347  unsigned int height = im.GetHeight();
348  // determine storage type
349  switch ( im.GetStorageType())
350  {
352  unsigned char** p = (unsigned char**)im.GetImageDataArray();
353  for(unsigned int y=0; y<height; y++) {
354  for(unsigned int x=0; x<width; x++) {
355  p[y][x*C+channelId] = static_cast<unsigned char>(channelIn[y*width+x]);
356  }
357  }
358  break; }
359 
361  float** p = (float**)im.GetImageDataArray();
362  for(unsigned int y=0; y<height; y++) {
363  for(unsigned int x=0; x<width; x++) {
364  p[y][x*C+channelId] = static_cast<float>(channelIn[y*width+x]);
365  }
366  }
367  break; }
368 
369 #ifdef BUILD_IMAGE_DOUBLE
371  double** p = (double**)im.GetImageDataArray();
372  for(unsigned int y=0; y<height; y++) {
373  for(unsigned int x=0; x<width; x++) {
374  p[y][x*C+channelId] = static_cast<double>(channelIn[y*width+x]);
375  }
376  }
377  break; }
378 #endif
379 
380 
381 #ifdef BUILD_IMAGE_INT
383  int** p = (int**)im.GetImageDataArray();
384  for(unsigned int y=0; y<height; y++) {
385  for(unsigned int x=0; x<width; x++) {
386  p[y][x*C+channelId] = static_cast<int>(channelIn[y*width+x]);
387  }
388  }
389  break; }
390 #endif
391 
392 #ifdef BUILD_IMAGE_UINT
394  unsigned int** p = (unsigned int**)im.GetImageDataArray();
395  for(unsigned int y=0; y<height; y++) {
396  for(unsigned int x=0; x<width; x++) {
397  p[y][x*C+channelId] = static_cast<unsigned int>(channelIn[y*width+x]);
398  }
399  }
400 
401  break; }
402 #endif
403 
404 #ifdef BUILD_IMAGE_SHORT
406  short** p = (short**)im.GetImageDataArray();
407  for(unsigned int y=0; y<height; y++) {
408  for(unsigned int x=0; x<width; x++) {
409  p[y][x*C+channelId] = static_cast<short>(channelIn[y*width+x]);
410  }
411  }
412  break; }
413 #endif
414 
415 #ifdef BUILD_IMAGE_USHORT
417  unsigned short** p = (unsigned short**)im.GetImageDataArray();
418  for(unsigned int y=0; y<height; y++) {
419  for(unsigned int x=0; x<width; x++) {
420  p[y][x*C+channelId] = static_cast<unsigned short>(channelIn[y*width+x]);
421  }
422  }
423  break; }
424 #endif
425 
426 #ifdef BUILD_IMAGE_CHAR // signed!
428  char** p = (char**)im.GetImageDataArray();
429  for(unsigned int y=0; y<height; y++) {
430  for(unsigned int x=0; x<width; x++) {
431  p[y][x*C+channelId] = static_cast<char>(channelIn[y*width+x]);
432  }
433  }
434 
435  break; }
436 #endif
437 
438  default:
439  {
440  BIASERR("unknown StorageType "<<im.GetStorageType()<<"detected");
441  }
442  };
443 
444 }
445 
446 
447 template<class outputType>
448 void ImageBase::GetChannel(const BIAS::ImageBase & im,
449  const unsigned int channelId,
450  outputType* channelOut)
451 {
452  // BIASASSERT(im.IsInterleaved());
453  unsigned int C = im.GetChannelCount();
454  unsigned int width = im.GetWidth();
455  unsigned int height = im.GetHeight();
456  switch ( im.GetStorageType())
457  {
459  if (im.IsInterleaved()) {
460  unsigned char** p = (unsigned char**)im.GetImageDataArray();
461  for(unsigned int y=0; y<height; y++) {
462  for(unsigned int x=0; x<width; x++) {
463  channelOut[y*width+x] = static_cast<float>(p[y][x*C+channelId]);
464  }
465  }
466  } else {
467  unsigned char const *data = reinterpret_cast<unsigned char const *>(im.GetImageData());
468  unsigned char const *start = &data[channelId * width * height];
469  unsigned char const *end = &data[(channelId + 1) * width * height];
470  std::copy(start, end, channelOut);
471  }
472  break; }
473 
475  if (im.IsInterleaved()) {
476  float** p = (float**)im.GetImageDataArray();
477  for(unsigned int y=0; y<height; y++) {
478  for(unsigned int x=0; x<width; x++) {
479  channelOut[y*width+x] = p[y][x*C+channelId];
480  }
481  }
482  } else {
483  float const *data = reinterpret_cast<float const *>(im.GetImageData());
484  float const *start = &data[channelId * width * height];
485  float const *end = &data[(channelId + 1) * width * height];
486  std::copy(start, end, channelOut);
487  }
488  break; }
489 
490 #ifdef BUILD_IMAGE_DOUBLE
492  if (im.IsInterleaved()) {
493  double** p = (double**)im.GetImageDataArray();
494  for(unsigned int y=0; y<height; y++) {
495  for(unsigned int x=0; x<width; x++) {
496  channelOut[y*width+x] = static_cast<float>(p[y][x*C+channelId]);
497  }
498  }
499  } else {
500  double const *data = reinterpret_cast<double const *>(im.GetImageData());
501  double const *start = &data[channelId * width * height];
502  double const *end = &data[(channelId + 1) * width * height];
503  std::copy(start, end, channelOut);
504  }
505  break; }
506 #endif
507 
508 
509 #ifdef BUILD_IMAGE_INT
511  if (im.IsInterleaved()) {
512  int** p = (int**)im.GetImageDataArray();
513  for(unsigned int y=0; y<height; y++) {
514  for(unsigned int x=0; x<width; x++) {
515  channelOut[y*width+x] = static_cast<float>(p[y][x*C+channelId]);
516  }
517  }
518  } else {
519  int const *data = reinterpret_cast<int const *>(im.GetImageData());
520  int const *start = &data[channelId * width * height];
521  int const *end = &data[(channelId + 1) * width * height];
522  std::copy(start, end, channelOut);
523  }
524  break; }
525 #endif
526 
527 #ifdef BUILD_IMAGE_UINT
529  if (im.IsInterleaved()) {
530  unsigned int** p = (unsigned int**)im.GetImageDataArray();
531  for(unsigned int y=0; y<height; y++) {
532  for(unsigned int x=0; x<width; x++) {
533  channelOut[y*width+x] = static_cast<float>(p[y][x*C+channelId]);
534  }
535  }
536  } else {
537  unsigned int const *data = reinterpret_cast<unsigned int const *>(im.GetImageData());
538  unsigned int const *start = &data[channelId * width * height];
539  unsigned int const *end = &data[(channelId + 1) * width * height];
540  std::copy(start, end, channelOut);
541  }
542 
543  break; }
544 #endif
545 
546 #ifdef BUILD_IMAGE_SHORT
548  if (im.IsInterleaved()) {
549  short** p = (short**)im.GetImageDataArray();
550  for(unsigned int y=0; y<height; y++) {
551  for(unsigned int x=0; x<width; x++) {
552  channelOut[y*width+x] = static_cast<float>(p[y][x*C+channelId]);
553  }
554  }
555  } else {
556  short const *data = reinterpret_cast<short const *>(im.GetImageData());
557  short const *start = &data[channelId * width * height];
558  short const *end = &data[(channelId + 1) * width * height];
559  std::copy(start, end, channelOut);
560  }
561  break; }
562 #endif
563 
564 #ifdef BUILD_IMAGE_USHORT
566  if (im.IsInterleaved()) {
567  unsigned short** p = (unsigned short**)im.GetImageDataArray();
568  for(unsigned int y=0; y<height; y++) {
569  for(unsigned int x=0; x<width; x++) {
570  channelOut[y*width+x] = static_cast<float>(p[y][x*C+channelId]);
571  }
572  }
573  } else {
574  unsigned short const *data = reinterpret_cast<unsigned short const *>(im.GetImageData());
575  unsigned short const *start = &data[channelId * width * height];
576  unsigned short const *end = &data[(channelId + 1) * width * height];
577  std::copy(start, end, channelOut);
578  }
579  break; }
580 #endif
581 
582 #ifdef BUILD_IMAGE_CHAR // signed!
584  if (im.IsInterleaved()) {
585  char** p = (char**)im.GetImageDataArray();
586  for(unsigned int y=0; y<height; y++) {
587  for(unsigned int x=0; x<width; x++) {
588  channelOut[y*width+x] = static_cast<float>(p[y][x*C+channelId]);
589  }
590  }
591  } else {
592  char const *data = reinterpret_cast<char const *>(im.GetImageData());
593  char const *start = &data[channelId * width * height];
594  char const *end = &data[(channelId + 1) * width * height];
595  std::copy(start, end, channelOut);
596  }
597 
598  break; }
599 #endif
600 
601  default:
602  {
603  BIASERR("unknown StorageType "<<im.GetStorageType()<<"detected");
604  }
605  };
606 
607 }
(16bit) unsigned integer image storage type
Definition: ImageBase.hh:114
bool IsInterleaved() const
Definition: ImageBase.hh:491
(8bit) signed char image storage type
Definition: ImageBase.hh:113
float image storage type
Definition: ImageBase.hh:118
double image storage type
Definition: ImageBase.hh:119
unsigned int GetWidth() const
Definition: ImageBase.hh:312
(16bit) signed integer image storage type
Definition: ImageBase.hh:115
const void * GetImageData() const
Definition: ImageBase.hh:280
void ** GetImageDataArray() const
Get an array of pointers to image data.
Definition: ImageBase.hh:305
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
unsigned int GetHeight() const
Definition: ImageBase.hh:319
(32bit) signed integer image storage type
Definition: ImageBase.hh:117
enum EStorageType GetStorageType() const
Definition: ImageBase.hh:414
(8bit) unsigned char image storage type
Definition: ImageBase.hh:112
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
(32bit) unsigned integer image storage type
Definition: ImageBase.hh:116