TestDrawRects.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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>
  44. TestDrawRects<T>::TestDrawRects(std::string testName_, NCVTestSourceProvider<T> &src_,
  45. NCVTestSourceProvider<Ncv32u> &src32u_,
  46. Ncv32u width_, Ncv32u height_, Ncv32u numRects_, T color_)
  47. :
  48. NCVTestProvider(testName_),
  49. src(src_),
  50. src32u(src32u_),
  51. width(width_),
  52. height(height_),
  53. numRects(numRects_),
  54. color(color_)
  55. {
  56. }
  57. template <class T>
  58. bool TestDrawRects<T>::toString(std::ofstream &strOut)
  59. {
  60. strOut << "sizeof(T)=" << sizeof(T) << std::endl;
  61. strOut << "width=" << width << std::endl;
  62. strOut << "height=" << height << std::endl;
  63. strOut << "numRects=" << numRects << std::endl;
  64. strOut << "color=" << color << std::endl;
  65. return true;
  66. }
  67. template <class T>
  68. bool TestDrawRects<T>::init()
  69. {
  70. return true;
  71. }
  72. template <class T>
  73. bool TestDrawRects<T>::process()
  74. {
  75. NCVStatus ncvStat;
  76. bool rcode = false;
  77. NCVMatrixAlloc<T> d_img(*this->allocatorGPU.get(), this->width, this->height);
  78. ncvAssertReturn(d_img.isMemAllocated(), false);
  79. NCVMatrixAlloc<T> h_img(*this->allocatorCPU.get(), this->width, this->height);
  80. ncvAssertReturn(h_img.isMemAllocated(), false);
  81. NCVMatrixAlloc<T> h_img_d(*this->allocatorCPU.get(), this->width, this->height);
  82. ncvAssertReturn(h_img_d.isMemAllocated(), false);
  83. NCVVectorAlloc<NcvRect32u> d_rects(*this->allocatorGPU.get(), this->numRects);
  84. ncvAssertReturn(d_rects.isMemAllocated(), false);
  85. NCVVectorAlloc<NcvRect32u> h_rects(*this->allocatorCPU.get(), this->numRects);
  86. ncvAssertReturn(h_rects.isMemAllocated(), false);
  87. NCV_SET_SKIP_COND(this->allocatorGPU.get()->isCounting());
  88. NCV_SKIP_COND_BEGIN
  89. ncvAssertReturn(this->src.fill(h_img), false);
  90. ncvStat = h_img.copySolid(d_img, 0);
  91. ncvAssertReturn(ncvStat == NCV_SUCCESS, false);
  92. ncvAssertCUDAReturn(cudaStreamSynchronize(0), false);
  93. //fill vector of rectangles with random rects covering the input
  94. NCVVectorReuse<Ncv32u> h_rects_as32u(h_rects.getSegment());
  95. ncvAssertReturn(h_rects_as32u.isMemReused(), false);
  96. ncvAssertReturn(this->src32u.fill(h_rects_as32u), false);
  97. for (Ncv32u i=0; i<this->numRects; i++)
  98. {
  99. h_rects.ptr()[i].x = (Ncv32u)(((1.0 * h_rects.ptr()[i].x) / RAND_MAX) * (this->width-2));
  100. h_rects.ptr()[i].y = (Ncv32u)(((1.0 * h_rects.ptr()[i].y) / RAND_MAX) * (this->height-2));
  101. h_rects.ptr()[i].width = (Ncv32u)(((1.0 * h_rects.ptr()[i].width) / RAND_MAX) * (this->width+10 - h_rects.ptr()[i].x));
  102. h_rects.ptr()[i].height = (Ncv32u)(((1.0 * h_rects.ptr()[i].height) / RAND_MAX) * (this->height+10 - h_rects.ptr()[i].y));
  103. }
  104. ncvStat = h_rects.copySolid(d_rects, 0);
  105. ncvAssertReturn(ncvStat == NCV_SUCCESS, false);
  106. ncvAssertCUDAReturn(cudaStreamSynchronize(0), false);
  107. if (sizeof(T) == sizeof(Ncv32u))
  108. {
  109. ncvStat = ncvDrawRects_32u_device((Ncv32u *)d_img.ptr(), d_img.stride(), this->width, this->height,
  110. (NcvRect32u *)d_rects.ptr(), this->numRects, this->color, 0);
  111. }
  112. else if (sizeof(T) == sizeof(Ncv8u))
  113. {
  114. ncvStat = ncvDrawRects_8u_device((Ncv8u *)d_img.ptr(), d_img.stride(), this->width, this->height,
  115. (NcvRect32u *)d_rects.ptr(), this->numRects, (Ncv8u)this->color, 0);
  116. }
  117. else
  118. {
  119. ncvAssertPrintReturn(false, "Incorrect drawrects test instance", false);
  120. }
  121. ncvAssertReturn(ncvStat == NCV_SUCCESS, false);
  122. NCV_SKIP_COND_END
  123. ncvStat = d_img.copySolid(h_img_d, 0);
  124. ncvAssertReturn(ncvStat == NCV_SUCCESS, false);
  125. ncvAssertCUDAReturn(cudaStreamSynchronize(0), false);
  126. NCV_SKIP_COND_BEGIN
  127. if (sizeof(T) == sizeof(Ncv32u))
  128. {
  129. ncvStat = ncvDrawRects_32u_host((Ncv32u *)h_img.ptr(), h_img.stride(), this->width, this->height,
  130. (NcvRect32u *)h_rects.ptr(), this->numRects, this->color);
  131. }
  132. else if (sizeof(T) == sizeof(Ncv8u))
  133. {
  134. ncvStat = ncvDrawRects_8u_host((Ncv8u *)h_img.ptr(), h_img.stride(), this->width, this->height,
  135. (NcvRect32u *)h_rects.ptr(), this->numRects, (Ncv8u)this->color);
  136. }
  137. else
  138. {
  139. ncvAssertPrintReturn(false, "Incorrect drawrects test instance", false);
  140. }
  141. ncvAssertReturn(ncvStat == NCV_SUCCESS, false);
  142. NCV_SKIP_COND_END
  143. //bit-to-bit check
  144. bool bLoopVirgin = true;
  145. NCV_SKIP_COND_BEGIN
  146. //const Ncv64f relEPS = 0.005;
  147. for (Ncv32u i=0; bLoopVirgin && i < h_img.height(); i++)
  148. {
  149. for (Ncv32u j=0; bLoopVirgin && j < h_img.width(); j++)
  150. {
  151. if (h_img.ptr()[h_img.stride()*i+j] != h_img_d.ptr()[h_img_d.stride()*i+j])
  152. {
  153. bLoopVirgin = false;
  154. }
  155. }
  156. }
  157. NCV_SKIP_COND_END
  158. if (bLoopVirgin)
  159. {
  160. rcode = true;
  161. }
  162. return rcode;
  163. }
  164. template <class T>
  165. bool TestDrawRects<T>::deinit()
  166. {
  167. return true;
  168. }
  169. template class TestDrawRects<Ncv8u>;
  170. template class TestDrawRects<Ncv32u>;