00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00020 #ifndef SHIMAGEIMPL_HPP
00021 #define SHIMAGEIMPL_HPP
00022
00023 #include "ShImage.hpp"
00024 #include "ShDebug.hpp"
00025
00026 namespace SH {
00027
00028 template<typename T>
00029 ShTypedImage<T>::ShTypedImage(int width, int height, int elements)
00030 {
00031 set_size(width, height, elements);
00032 }
00033
00034 template<typename T>
00035 ShTypedImage<T>::ShTypedImage(const ShTypedImage<T>& other)
00036 : m_width(other.m_width), m_height(other.m_height), m_elements(other.m_elements),
00037 m_memory(other.m_memory ? new ShHostMemory(sizeof(T) * m_width * m_height * m_elements, ShStorageTypeInfo<T>::value_type) : 0)
00038 {
00039 if (m_memory) {
00040 std::memcpy(m_memory->hostStorage()->data(),
00041 other.m_memory->hostStorage()->data(),
00042 m_width * m_height * m_elements * sizeof(T));
00043 }
00044 }
00045
00046 template<typename T>
00047 ShTypedImage<T>& ShTypedImage<T>::operator=(const ShTypedImage<T>& other)
00048 {
00049 m_width = other.m_width;
00050 m_height = other.m_height;
00051 m_elements = other.m_elements;
00052 m_memory = (other.m_memory ? new ShHostMemory(sizeof(T) * m_width * m_height * m_elements, ShStorageTypeInfo<T>::value_type) : 0);
00053 std::memcpy(m_memory->hostStorage()->data(),
00054 other.m_memory->hostStorage()->data(),
00055 m_width * m_height * m_elements * sizeof(T));
00056
00057 return *this;
00058 }
00059
00060 template<typename T>
00061 ShTypedImage<T> ShTypedImage<T>::getNormalImage()
00062 {
00063 int w = width();
00064 int h = height();
00065 ShTypedImage<T> output_image(w,h,3);
00066 for (int j = 0; j < h; j++) {
00067 int jp1 = j + 1;
00068 if (jp1 >= h) jp1 = 0;
00069 int jm1 = (j - 1);
00070 if (jm1 < 0) jm1 = h - 1;
00071 for (int i = 0; i < w; i++) {
00072 int ip1 = i + 1;
00073 if (ip1 >= w) ip1 = 0;
00074 int im1 = (i - 1);
00075 if (im1 < 0) im1 = w - 1;
00076 T x, y, z;
00077 x = ((*this)(ip1,j,0) - (*this)(im1,j,0))/2.0f;
00078 output_image(i,j,0) = x/2.0f + 0.5f;
00079 y = ((*this)(i,jp1,0) - (*this)(i,jm1,0))/2.0f;
00080 output_image(i,j,1) = y/2.0f + 0.5f;
00081 z = x*x + y*y;
00082 if (z < 1.0f) {
00083 z = std::sqrt(1 - z);
00084 } else {
00085 z = 0.0f;
00086 }
00087 output_image(i,j,2) = z;
00088 }
00089 }
00090 return output_image;
00091 }
00092
00093 template<typename T>
00094 const T* ShTypedImage<T>::data() const
00095 {
00096 if (!m_memory) return 0;
00097 return reinterpret_cast<const T*>(m_memory->hostStorage()->data());
00098 }
00099
00100 template<typename T>
00101 T* ShTypedImage<T>::data()
00102 {
00103 if (!m_memory) return 0;
00104 return reinterpret_cast<T*>(m_memory->hostStorage()->data());
00105 }
00106
00107 template<typename T>
00108 T ShTypedImage<T>::operator()(int x, int y, int i) const
00109 {
00110 SH_DEBUG_ASSERT(m_memory);
00111 return data()[m_elements * (m_width * y + x) + i];
00112 }
00113
00114 template<typename T>
00115 T& ShTypedImage<T>::operator()(int x, int y, int i)
00116 {
00117 return data()[m_elements * (m_width * y + x) + i];
00118 }
00119
00120 template<typename T>
00121 ShTypedImage<T>::ShTypedImage()
00122 : m_width(0), m_height(0), m_elements(0), m_memory(0)
00123 {
00124 }
00125
00126 template<typename T>
00127 ShTypedImage<T>::~ShTypedImage()
00128 {
00129 }
00130
00131 template<typename T>
00132 int ShTypedImage<T>::width() const
00133 {
00134 return m_width;
00135 }
00136
00137 template<typename T>
00138 int ShTypedImage<T>::height() const
00139 {
00140 return m_height;
00141 }
00142
00143 template<typename T>
00144 void ShTypedImage<T>::set_size(int width, int height, int elements)
00145 {
00146 m_width = width;
00147 m_height = height;
00148 m_elements = elements;
00149 m_memory = new ShHostMemory(sizeof(T) * m_width * m_height * m_elements, ShStorageTypeInfo<T>::value_type);
00150 }
00151
00152 template<typename T>
00153 int ShTypedImage<T>::elements() const
00154 {
00155 return m_elements;
00156 }
00157
00158 template<typename T>
00159 void ShTypedImage<T>::dirty()
00160 {
00161 if (!m_memory) return;
00162 m_memory->hostStorage()->dirty();
00163 }
00164
00165 template<typename T>
00166 ShMemoryPtr ShTypedImage<T>::memory()
00167 {
00168 return m_memory;
00169 }
00170
00171 template<typename T>
00172 ShPointer<const ShMemory> ShTypedImage<T>::memory() const
00173 {
00174 return m_memory;
00175 }
00176
00177 }
00178
00179 #endif