image.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. Copyright (C) 2006 Pedro Felzenszwalb
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. /* a simple image class */
  16. #ifndef IMAGE_H
  17. #define IMAGE_H
  18. #include <cstring>
  19. namespace felzenszwalb{
  20. template <class T>
  21. class image {
  22. public:
  23. /* create an image */
  24. image(const int width, const int height, const bool init = true);
  25. /* delete an image */
  26. ~image();
  27. /* init an image */
  28. void init(const T &val);
  29. /* copy an image */
  30. image<T> *copy() const;
  31. /* get the width of an image. */
  32. int width() const { return w; }
  33. /* get the height of an image. */
  34. int height() const { return h; }
  35. /* image data. */
  36. T *data;
  37. /* row pointers. */
  38. T **access;
  39. private:
  40. int w, h;
  41. };
  42. /* use imRef to access image data. */
  43. #define imRef(im, x, y) (im->access[y][x])
  44. /* use imPtr to get pointer to image data. */
  45. #define imPtr(im, x, y) &(im->access[y][x])
  46. template <class T>
  47. image<T>::image(const int width, const int height, const bool init) {
  48. w = width;
  49. h = height;
  50. data = new T[w * h]; // allocate space for image data
  51. access = new T*[h]; // allocate space for row pointers
  52. // initialize row pointers
  53. for (int i = 0; i < h; i++)
  54. access[i] = data + (i * w);
  55. if (init)
  56. memset(data, 0, w * h * sizeof(T));
  57. }
  58. template <class T>
  59. image<T>::~image() {
  60. delete [] data;
  61. delete [] access;
  62. }
  63. template <class T>
  64. void image<T>::init(const T &val) {
  65. T *ptr = imPtr(this, 0, 0);
  66. T *end = imPtr(this, w-1, h-1);
  67. while (ptr <= end)
  68. *ptr++ = val;
  69. }
  70. template <class T>
  71. image<T> *image<T>::copy() const {
  72. image<T> *im = new image<T>(w, h, false);
  73. memcpy(im->data, data, w * h * sizeof(T));
  74. return im;
  75. }
  76. }//namespace
  77. #endif