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