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
45TestCompact::TestCompact(std::string testName_, NCVTestSourceProvider<Ncv32u> &src_,
46                                             Ncv32u length_, Ncv32u badElem_, Ncv32u badElemPercentage_)
47    :
48    NCVTestProvider(testName_),
49    src(src_),
50    length(length_),
51    badElem(badElem_),
52    badElemPercentage(badElemPercentage_ > 100 ? 100 : badElemPercentage_)
53{
54}
55
56
57bool TestCompact::toString(std::ofstream &strOut)
58{
59    strOut << "length=" << length << std::endl;
60    strOut << "badElem=" << badElem << std::endl;
61    strOut << "badElemPercentage=" << badElemPercentage << std::endl;
62    return true;
63}
64
65
66bool TestCompact::init()
67{
68    return true;
69}
70
71
72bool TestCompact::process()
73{
74    NCVStatus ncvStat;
75    bool rcode = false;
76
77    NCVVectorAlloc<Ncv32u> h_vecSrc(*this->allocatorCPU.get(), this->length);
78    ncvAssertReturn(h_vecSrc.isMemAllocated(), false);
79    NCVVectorAlloc<Ncv32u> d_vecSrc(*this->allocatorGPU.get(), this->length);
80    ncvAssertReturn(d_vecSrc.isMemAllocated(), false);
81
82    NCVVectorAlloc<Ncv32u> h_vecDst(*this->allocatorCPU.get(), this->length);
83    ncvAssertReturn(h_vecDst.isMemAllocated(), false);
84    NCVVectorAlloc<Ncv32u> d_vecDst(*this->allocatorGPU.get(), this->length);
85    ncvAssertReturn(d_vecDst.isMemAllocated(), false);
86    NCVVectorAlloc<Ncv32u> h_vecDst_d(*this->allocatorCPU.get(), this->length);
87    ncvAssertReturn(h_vecDst_d.isMemAllocated(), false);
88
89    NCV_SET_SKIP_COND(this->allocatorGPU.get()->isCounting());
90    NCV_SKIP_COND_BEGIN
91    ncvAssertReturn(this->src.fill(h_vecSrc), false);
92    for (Ncv32u i=0; i<this->length; i++)
93    {
94        Ncv32u tmp = (h_vecSrc.ptr()[i]) & 0xFF;
95        tmp = tmp * 99 / 255;
96        if (tmp < this->badElemPercentage)
97        {
98            h_vecSrc.ptr()[i] = this->badElem;
99        }
100    }
101    NCV_SKIP_COND_END
102
103    NCVVectorAlloc<Ncv32u> h_dstLen(*this->allocatorCPU.get(), 1);
104    ncvAssertReturn(h_dstLen.isMemAllocated(), false);
105    Ncv32u bufSize;
106    ncvStat = nppsStCompactGetSize_32u(this->length, &bufSize, this->devProp);
107    ncvAssertReturn(NPPST_SUCCESS == ncvStat, false);
108    NCVVectorAlloc<Ncv8u> d_tmpBuf(*this->allocatorGPU.get(), bufSize);
109    ncvAssertReturn(d_tmpBuf.isMemAllocated(), false);
110
111    Ncv32u h_outElemNum_h = 0;
112
113    NCV_SKIP_COND_BEGIN
114    ncvStat = h_vecSrc.copySolid(d_vecSrc, 0);
115    ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);
116    ncvStat = nppsStCompact_32u(d_vecSrc.ptr(), this->length,
117                                d_vecDst.ptr(), h_dstLen.ptr(), this->badElem,
118                                d_tmpBuf.ptr(), bufSize, this->devProp);
119    ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);
120    ncvStat = d_vecDst.copySolid(h_vecDst_d, 0);
121    ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);
122
123    ncvStat = nppsStCompact_32u_host(h_vecSrc.ptr(), this->length, h_vecDst.ptr(), &h_outElemNum_h, this->badElem);
124    ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);
125    NCV_SKIP_COND_END
126
127    //bit-to-bit check
128    bool bLoopVirgin = true;
129
130    NCV_SKIP_COND_BEGIN
131    if (h_dstLen.ptr()[0] != h_outElemNum_h)
132    {
133        bLoopVirgin = false;
134    }
135    else
136    {
137        for (Ncv32u i=0; bLoopVirgin && i < h_outElemNum_h; i++)
138        {
139            if (h_vecDst.ptr()[i] != h_vecDst_d.ptr()[i])
140            {
141                bLoopVirgin = false;
142            }
143        }
144    }
145    NCV_SKIP_COND_END
146
147    if (bLoopVirgin)
148    {
149        rcode = true;
150    }
151
152    return rcode;
153}
154
155
156bool TestCompact::deinit()
157{
158    return true;
159}
160