TestIntegralImage.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #include "test_precomp.hpp"
  43. template <class T_in, class T_out>
  44. TestIntegralImage<T_in, T_out>::TestIntegralImage(std::string testName_, NCVTestSourceProvider<T_in> &src_,
  45. Ncv32u width_, Ncv32u height_)
  46. :
  47. NCVTestProvider(testName_),
  48. src(src_),
  49. width(width_),
  50. height(height_)
  51. {
  52. }
  53. template <class T_in, class T_out>
  54. bool TestIntegralImage<T_in, T_out>::toString(std::ofstream &strOut)
  55. {
  56. strOut << "sizeof(T_in)=" << sizeof(T_in) << std::endl;
  57. strOut << "sizeof(T_out)=" << sizeof(T_out) << std::endl;
  58. strOut << "width=" << width << std::endl;
  59. strOut << "height=" << height << std::endl;
  60. return true;
  61. }
  62. template <class T_in, class T_out>
  63. bool TestIntegralImage<T_in, T_out>::init()
  64. {
  65. return true;
  66. }
  67. template <class T_in, class T_out>
  68. bool TestIntegralImage<T_in, T_out>::process()
  69. {
  70. NCVStatus ncvStat;
  71. bool rcode = false;
  72. Ncv32u widthII = this->width + 1;
  73. Ncv32u heightII = this->height + 1;
  74. NCVMatrixAlloc<T_in> d_img(*this->allocatorGPU.get(), this->width, this->height);
  75. ncvAssertReturn(d_img.isMemAllocated(), false);
  76. NCVMatrixAlloc<T_in> h_img(*this->allocatorCPU.get(), this->width, this->height);
  77. ncvAssertReturn(h_img.isMemAllocated(), false);
  78. NCVMatrixAlloc<T_out> d_imgII(*this->allocatorGPU.get(), widthII, heightII);
  79. ncvAssertReturn(d_imgII.isMemAllocated(), false);
  80. NCVMatrixAlloc<T_out> h_imgII(*this->allocatorCPU.get(), widthII, heightII);
  81. ncvAssertReturn(h_imgII.isMemAllocated(), false);
  82. NCVMatrixAlloc<T_out> h_imgII_d(*this->allocatorCPU.get(), widthII, heightII);
  83. ncvAssertReturn(h_imgII_d.isMemAllocated(), false);
  84. Ncv32u bufSize;
  85. if (sizeof(T_in) == sizeof(Ncv8u))
  86. {
  87. ncvStat = nppiStIntegralGetSize_8u32u(NcvSize32u(this->width, this->height), &bufSize, this->devProp);
  88. ncvAssertReturn(NPPST_SUCCESS == ncvStat, false);
  89. }
  90. else if (sizeof(T_in) == sizeof(Ncv32f))
  91. {
  92. ncvStat = nppiStIntegralGetSize_32f32f(NcvSize32u(this->width, this->height), &bufSize, this->devProp);
  93. ncvAssertReturn(NPPST_SUCCESS == ncvStat, false);
  94. }
  95. else
  96. {
  97. ncvAssertPrintReturn(false, "Incorrect integral image test instance", false);
  98. }
  99. NCVVectorAlloc<Ncv8u> d_tmpBuf(*this->allocatorGPU.get(), bufSize);
  100. ncvAssertReturn(d_tmpBuf.isMemAllocated(), false);
  101. NCV_SET_SKIP_COND(this->allocatorGPU.get()->isCounting());
  102. NCV_SKIP_COND_BEGIN
  103. ncvAssertReturn(this->src.fill(h_img), false);
  104. ncvStat = h_img.copySolid(d_img, 0);
  105. ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);
  106. if (sizeof(T_in) == sizeof(Ncv8u))
  107. {
  108. ncvStat = nppiStIntegral_8u32u_C1R((Ncv8u *)d_img.ptr(), d_img.pitch(),
  109. (Ncv32u *)d_imgII.ptr(), d_imgII.pitch(),
  110. NcvSize32u(this->width, this->height),
  111. d_tmpBuf.ptr(), bufSize, this->devProp);
  112. ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);
  113. }
  114. else if (sizeof(T_in) == sizeof(Ncv32f))
  115. {
  116. ncvStat = nppiStIntegral_32f32f_C1R((Ncv32f *)d_img.ptr(), d_img.pitch(),
  117. (Ncv32f *)d_imgII.ptr(), d_imgII.pitch(),
  118. NcvSize32u(this->width, this->height),
  119. d_tmpBuf.ptr(), bufSize, this->devProp);
  120. ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);
  121. }
  122. else
  123. {
  124. ncvAssertPrintReturn(false, "Incorrect integral image test instance", false);
  125. }
  126. ncvStat = d_imgII.copySolid(h_imgII_d, 0);
  127. ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);
  128. if (sizeof(T_in) == sizeof(Ncv8u))
  129. {
  130. ncvStat = nppiStIntegral_8u32u_C1R_host((Ncv8u *)h_img.ptr(), h_img.pitch(),
  131. (Ncv32u *)h_imgII.ptr(), h_imgII.pitch(),
  132. NcvSize32u(this->width, this->height));
  133. ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);
  134. }
  135. else if (sizeof(T_in) == sizeof(Ncv32f))
  136. {
  137. ncvStat = nppiStIntegral_32f32f_C1R_host((Ncv32f *)h_img.ptr(), h_img.pitch(),
  138. (Ncv32f *)h_imgII.ptr(), h_imgII.pitch(),
  139. NcvSize32u(this->width, this->height));
  140. ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);
  141. }
  142. else
  143. {
  144. ncvAssertPrintReturn(false, "Incorrect integral image test instance", false);
  145. }
  146. NCV_SKIP_COND_END
  147. //bit-to-bit check
  148. bool bLoopVirgin = true;
  149. NCV_SKIP_COND_BEGIN
  150. for (Ncv32u i=0; bLoopVirgin && i < h_img.height() + 1; i++)
  151. {
  152. for (Ncv32u j=0; bLoopVirgin && j < h_img.width() + 1; j++)
  153. {
  154. if (sizeof(T_in) == sizeof(Ncv8u))
  155. {
  156. if (h_imgII.ptr()[h_imgII.stride()*i+j] != h_imgII_d.ptr()[h_imgII_d.stride()*i+j])
  157. {
  158. bLoopVirgin = false;
  159. }
  160. }
  161. else if (sizeof(T_in) == sizeof(Ncv32f))
  162. {
  163. if (fabsf((float)h_imgII.ptr()[h_imgII.stride()*i+j] - (float)h_imgII_d.ptr()[h_imgII_d.stride()*i+j]) > 0.01f)
  164. {
  165. bLoopVirgin = false;
  166. }
  167. }
  168. else
  169. {
  170. ncvAssertPrintReturn(false, "Incorrect integral image test instance", false);
  171. }
  172. }
  173. }
  174. NCV_SKIP_COND_END
  175. if (bLoopVirgin)
  176. {
  177. rcode = true;
  178. }
  179. return rcode;
  180. }
  181. template <class T_in, class T_out>
  182. bool TestIntegralImage<T_in, T_out>::deinit()
  183. {
  184. return true;
  185. }
  186. template class TestIntegralImage<Ncv8u, Ncv32u>;
  187. template class TestIntegralImage<Ncv32f, Ncv32f>;