NCVTestSourceProvider.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. #ifndef _ncvtestsourceprovider_hpp_
  43. #define _ncvtestsourceprovider_hpp_
  44. #include <memory>
  45. #include "opencv2/highgui.hpp"
  46. #include "opencv2/cudalegacy.hpp"
  47. template <class T>
  48. class NCVTestSourceProvider
  49. {
  50. public:
  51. NCVTestSourceProvider(Ncv32u seed, T rangeLow, T rangeHigh, Ncv32u maxWidth, Ncv32u maxHeight)
  52. :
  53. bInit(false)
  54. {
  55. ncvAssertPrintReturn(rangeLow < rangeHigh, "NCVTestSourceProvider ctor:: Invalid range", );
  56. int devId;
  57. cudaDeviceProp devProp;
  58. ncvAssertPrintReturn(cudaSuccess == cudaGetDevice(&devId), "Error returned from cudaGetDevice", );
  59. ncvAssertPrintReturn(cudaSuccess == cudaGetDeviceProperties(&devProp, devId), "Error returned from cudaGetDeviceProperties", );
  60. //Ncv32u maxWpitch = alignUp(maxWidth * sizeof(T), devProp.textureAlignment);
  61. allocatorCPU.reset(new NCVMemNativeAllocator(NCVMemoryTypeHostPinned, static_cast<Ncv32u>(devProp.textureAlignment)));
  62. data.reset(new NCVMatrixAlloc<T>(*this->allocatorCPU.get(), maxWidth, maxHeight));
  63. ncvAssertPrintReturn(data.get()->isMemAllocated(), "NCVTestSourceProvider ctor:: Matrix not allocated", );
  64. this->dataWidth = maxWidth;
  65. this->dataHeight = maxHeight;
  66. srand(seed);
  67. for (Ncv32u i=0; i<maxHeight; i++)
  68. {
  69. for (Ncv32u j=0; j<data.get()->stride(); j++)
  70. {
  71. data.get()->ptr()[i * data.get()->stride() + j] =
  72. (T)(((1.0 * rand()) / RAND_MAX) * (rangeHigh - rangeLow) + rangeLow);
  73. }
  74. }
  75. this->bInit = true;
  76. }
  77. NCVTestSourceProvider(std::string pgmFilename)
  78. :
  79. bInit(false)
  80. {
  81. ncvAssertPrintReturn(sizeof(T) == 1, "NCVTestSourceProvider ctor:: PGM constructor complies only with 8bit types", );
  82. cv::Mat image = cv::imread(pgmFilename);
  83. ncvAssertPrintReturn(!image.empty(), "NCVTestSourceProvider ctor:: PGM file error", );
  84. int devId;
  85. cudaDeviceProp devProp;
  86. ncvAssertPrintReturn(cudaSuccess == cudaGetDevice(&devId), "Error returned from cudaGetDevice", );
  87. ncvAssertPrintReturn(cudaSuccess == cudaGetDeviceProperties(&devProp, devId), "Error returned from cudaGetDeviceProperties", );
  88. allocatorCPU.reset(new NCVMemNativeAllocator(NCVMemoryTypeHostPinned, static_cast<Ncv32u>(devProp.textureAlignment)));
  89. data.reset(new NCVMatrixAlloc<T>(*this->allocatorCPU.get(), image.cols, image.rows));
  90. ncvAssertPrintReturn(data.get()->isMemAllocated(), "NCVTestSourceProvider ctor:: Matrix not allocated", );
  91. this->dataWidth = image.cols;
  92. this->dataHeight = image.rows;
  93. cv::Mat hdr(image.size(), CV_8UC1, data.get()->ptr(), data.get()->pitch());
  94. image.copyTo(hdr);
  95. this->bInit = true;
  96. }
  97. NcvBool fill(NCVMatrix<T> &dst)
  98. {
  99. ncvAssertReturn(this->isInit() &&
  100. dst.memType() == allocatorCPU.get()->memType(), false);
  101. if (dst.width() == 0 || dst.height() == 0)
  102. {
  103. return true;
  104. }
  105. for (Ncv32u i=0; i<dst.height(); i++)
  106. {
  107. Ncv32u srcLine = i % this->dataHeight;
  108. Ncv32u srcFullChunks = dst.width() / this->dataWidth;
  109. for (Ncv32u j=0; j<srcFullChunks; j++)
  110. {
  111. memcpy(dst.ptr() + i * dst.stride() + j * this->dataWidth,
  112. this->data.get()->ptr() + this->data.get()->stride() * srcLine,
  113. this->dataWidth * sizeof(T));
  114. }
  115. Ncv32u srcLastChunk = dst.width() % this->dataWidth;
  116. memcpy(dst.ptr() + i * dst.stride() + srcFullChunks * this->dataWidth,
  117. this->data.get()->ptr() + this->data.get()->stride() * srcLine,
  118. srcLastChunk * sizeof(T));
  119. }
  120. return true;
  121. }
  122. NcvBool fill(NCVVector<T> &dst)
  123. {
  124. ncvAssertReturn(this->isInit() &&
  125. dst.memType() == allocatorCPU.get()->memType(), false);
  126. if (dst.length() == 0)
  127. {
  128. return true;
  129. }
  130. Ncv32u srcLen = this->dataWidth * this->dataHeight;
  131. Ncv32u srcFullChunks = (Ncv32u)dst.length() / srcLen;
  132. for (Ncv32u j=0; j<srcFullChunks; j++)
  133. {
  134. memcpy(dst.ptr() + j * srcLen, this->data.get()->ptr(), srcLen * sizeof(T));
  135. }
  136. Ncv32u srcLastChunk = dst.length() % srcLen;
  137. memcpy(dst.ptr() + srcFullChunks * srcLen, this->data.get()->ptr(), srcLastChunk * sizeof(T));
  138. return true;
  139. }
  140. ~NCVTestSourceProvider()
  141. {
  142. data.reset();
  143. allocatorCPU.reset();
  144. }
  145. private:
  146. NcvBool isInit(void)
  147. {
  148. return this->bInit;
  149. }
  150. NcvBool bInit;
  151. std::unique_ptr< INCVMemAllocator > allocatorCPU;
  152. std::unique_ptr< NCVMatrixAlloc<T> > data;
  153. Ncv32u dataWidth;
  154. Ncv32u dataHeight;
  155. };
  156. #endif // _ncvtestsourceprovider_hpp_