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