Image.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // This code is in the public domain -- castanyo@yahoo.es
  2. #include "Image.h"
  3. //#include "ImageIO.h"
  4. #include "nvmath/Color.h"
  5. #include "nvcore/Debug.h"
  6. #include "nvcore/Ptr.h"
  7. #include "nvcore/Utils.h" // swap
  8. #include "nvcore/Memory.h" // realloc, free
  9. #include <string.h> // memcpy
  10. using namespace nv;
  11. Image::Image() : m_width(0), m_height(0), m_format(Format_RGB), m_data(NULL)
  12. {
  13. }
  14. Image::Image(const Image & img) : m_data(NULL)
  15. {
  16. allocate(img.m_width, img.m_height, img.m_depth);
  17. m_format = img.m_format;
  18. memcpy(m_data, img.m_data, sizeof(Color32) * m_width * m_height * m_depth);
  19. }
  20. Image::~Image()
  21. {
  22. free();
  23. }
  24. const Image & Image::operator=(const Image & img)
  25. {
  26. allocate(img.m_width, img.m_height, m_depth);
  27. m_format = img.m_format;
  28. memcpy(m_data, img.m_data, sizeof(Color32) * m_width * m_height * m_depth);
  29. return *this;
  30. }
  31. void Image::allocate(uint w, uint h, uint d/*= 1*/)
  32. {
  33. m_width = w;
  34. m_height = h;
  35. m_depth = d;
  36. m_data = realloc<Color32>(m_data, w * h * d);
  37. }
  38. void Image::acquire(Color32 * data, uint w, uint h, uint d/*= 1*/)
  39. {
  40. free();
  41. m_width = w;
  42. m_height = h;
  43. m_depth = d;
  44. m_data = data;
  45. }
  46. void Image::resize(uint w, uint h, uint d/*= 1*/) {
  47. Image img;
  48. img.allocate(w, h, d);
  49. Color32 background(0,0,0,0);
  50. // Copy image.
  51. uint x, y, z;
  52. for(z = 0; z < min(d, m_depth); z++) {
  53. for(y = 0; y < min(h, m_height); y++) {
  54. for(x = 0; x < min(w, m_width); x++) {
  55. img.pixel(x, y, z) = pixel(x, y, z);
  56. }
  57. for(; x < w; x++) {
  58. img.pixel(x, y, z) = background;
  59. }
  60. }
  61. for(; y < h; y++) {
  62. for(x = 0; x < w; x++) {
  63. img.pixel(x, y, z) = background;
  64. }
  65. }
  66. }
  67. for(; z < d; z++) {
  68. for(y = 0; y < h; y++) {
  69. for(x = 0; x < w; x++) {
  70. img.pixel(x, y, z) = background;
  71. }
  72. }
  73. }
  74. swap(m_width, img.m_width);
  75. swap(m_height, img.m_height);
  76. swap(m_depth, img.m_depth);
  77. swap(m_format, img.m_format);
  78. swap(m_data, img.m_data);
  79. }
  80. /*bool Image::load(const char * name)
  81. {
  82. free();
  83. AutoPtr<Image> img(ImageIO::load(name));
  84. if (img == NULL) {
  85. return false;
  86. }
  87. swap(m_width, img->m_width);
  88. swap(m_height, img->m_height);
  89. swap(m_depth, img->m_depth);
  90. swap(m_format, img->m_format);
  91. swap(m_data, img->m_data);
  92. return true;
  93. }*/
  94. void Image::wrap(void * data, uint w, uint h, uint d)
  95. {
  96. free();
  97. m_data = (Color32 *)data;
  98. m_width = w;
  99. m_height = h;
  100. m_depth = d;
  101. }
  102. void Image::unwrap()
  103. {
  104. m_data = NULL;
  105. m_width = 0;
  106. m_height = 0;
  107. m_depth = 0;
  108. }
  109. void Image::free()
  110. {
  111. ::free(m_data);
  112. m_data = NULL;
  113. }
  114. uint Image::width() const
  115. {
  116. return m_width;
  117. }
  118. uint Image::height() const
  119. {
  120. return m_height;
  121. }
  122. uint Image::depth() const
  123. {
  124. return m_depth;
  125. }
  126. const Color32 * Image::scanline(uint h) const
  127. {
  128. nvDebugCheck(h < m_height);
  129. return m_data + h * m_width;
  130. }
  131. Color32 * Image::scanline(uint h)
  132. {
  133. nvDebugCheck(h < m_height);
  134. return m_data + h * m_width;
  135. }
  136. const Color32 * Image::pixels() const
  137. {
  138. return m_data;
  139. }
  140. Color32 * Image::pixels()
  141. {
  142. return m_data;
  143. }
  144. const Color32 & Image::pixel(uint idx) const
  145. {
  146. nvDebugCheck(idx < m_width * m_height * m_depth);
  147. return m_data[idx];
  148. }
  149. Color32 & Image::pixel(uint idx)
  150. {
  151. nvDebugCheck(idx < m_width * m_height * m_depth);
  152. return m_data[idx];
  153. }
  154. Image::Format Image::format() const
  155. {
  156. return m_format;
  157. }
  158. void Image::setFormat(Image::Format f)
  159. {
  160. m_format = f;
  161. }
  162. void Image::fill(Color32 c)
  163. {
  164. const uint size = m_width * m_height * m_depth;
  165. for (uint i = 0; i < size; ++i)
  166. {
  167. m_data[i] = c;
  168. }
  169. }