input_array_utility.cpp 11.3 KB
Newer Older
wester committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include "precomp.hpp"

wester committed
45
using namespace std;
wester committed
46
using namespace cv;
wester committed
47
using namespace cv::gpu;
wester committed
48 49 50 51 52

Mat cv::superres::arrGetMat(InputArray arr, Mat& buf)
{
    switch (arr.kind())
    {
wester committed
53
    case _InputArray::GPU_MAT:
wester committed
54 55 56 57 58 59 60
        arr.getGpuMat().download(buf);
        return buf;

    case _InputArray::OPENGL_BUFFER:
        arr.getOGlBuffer().copyTo(buf);
        return buf;

wester committed
61 62
    case _InputArray::OPENGL_TEXTURE:
        arr.getOGlTexture2D().copyTo(buf);
wester committed
63 64 65
        return buf;

    default:
wester committed
66
        return arr.getMat();
wester committed
67 68 69 70 71 72 73
    }
}

GpuMat cv::superres::arrGetGpuMat(InputArray arr, GpuMat& buf)
{
    switch (arr.kind())
    {
wester committed
74
    case _InputArray::GPU_MAT:
wester committed
75 76 77 78 79 80
        return arr.getGpuMat();

    case _InputArray::OPENGL_BUFFER:
        arr.getOGlBuffer().copyTo(buf);
        return buf;

wester committed
81 82 83 84
    case _InputArray::OPENGL_TEXTURE:
        arr.getOGlTexture2D().copyTo(buf);
        return buf;

wester committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    default:
        buf.upload(arr.getMat());
        return buf;
    }
}

namespace
{
    void mat2mat(InputArray src, OutputArray dst)
    {
        src.getMat().copyTo(dst);
    }
    void arr2buf(InputArray src, OutputArray dst)
    {
        dst.getOGlBufferRef().copyFrom(src);
    }
wester committed
101 102 103 104
    void arr2tex(InputArray src, OutputArray dst)
    {
        dst.getOGlTexture2D().copyFrom(src);
    }
wester committed
105 106 107 108 109 110 111 112
    void mat2gpu(InputArray src, OutputArray dst)
    {
        dst.getGpuMatRef().upload(src.getMat());
    }
    void buf2arr(InputArray src, OutputArray dst)
    {
        src.getOGlBuffer().copyTo(dst);
    }
wester committed
113 114 115 116
    void tex2arr(InputArray src, OutputArray dst)
    {
        src.getOGlTexture2D().copyTo(dst);
    }
wester committed
117 118 119 120 121 122 123 124 125 126 127
    void gpu2mat(InputArray src, OutputArray dst)
    {
        GpuMat d = src.getGpuMat();
        dst.create(d.size(), d.type());
        Mat m = dst.getMat();
        d.download(m);
    }
    void gpu2gpu(InputArray src, OutputArray dst)
    {
        src.getGpuMat().copyTo(dst.getGpuMatRef());
    }
wester committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
#ifdef HAVE_OPENCV_OCL
    void ocl2mat(InputArray src, OutputArray dst)
    {
        dst.getMatRef() = (Mat)ocl::getOclMatRef(src);
    }
    void mat2ocl(InputArray src, OutputArray dst)
    {
        Mat m = src.getMat();
        ocl::getOclMatRef(dst) = (ocl::oclMat)m;
    }
    void ocl2ocl(InputArray src, OutputArray dst)
    {
        ocl::getOclMatRef(src).copyTo(ocl::getOclMatRef(dst));
    }
#else
    void ocl2mat(InputArray, OutputArray)
    {
        CV_Error(CV_StsNotImplemented, "The called functionality is disabled for current build or platform");;
    }
    void mat2ocl(InputArray, OutputArray)
    {
        CV_Error(CV_StsNotImplemented, "The called functionality is disabled for current build or platform");;
    }
    void ocl2ocl(InputArray, OutputArray)
    {
        CV_Error(CV_StsNotImplemented, "The called functionality is disabled for current build or platform");
    }
#endif
wester committed
156 157 158 159 160
}

void cv::superres::arrCopy(InputArray src, OutputArray dst)
{
    typedef void (*func_t)(InputArray src, OutputArray dst);
wester committed
161
    static const func_t funcs[11][11] =
wester committed
162
    {
wester committed
163 164 165 166 167 168 169 170 171 172 173
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, arr2tex, mat2gpu, mat2ocl},
        {0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, arr2tex, mat2gpu, mat2ocl},
        {0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, arr2tex, mat2gpu, mat2ocl},
        {0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, arr2tex, mat2gpu, mat2ocl},
        {0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, arr2tex, mat2gpu, mat2ocl},
        {0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, arr2tex, mat2gpu, mat2ocl},
        {0, buf2arr, buf2arr, buf2arr, buf2arr, buf2arr, buf2arr, buf2arr, buf2arr, buf2arr, 0      },
        {0, tex2arr, tex2arr, tex2arr, tex2arr, tex2arr, tex2arr, tex2arr, tex2arr, tex2arr, 0      },
        {0, gpu2mat, gpu2mat, gpu2mat, gpu2mat, gpu2mat, gpu2mat, arr2buf, arr2tex, gpu2gpu, 0      },
        {0, ocl2mat, ocl2mat, ocl2mat, ocl2mat, ocl2mat, ocl2mat, 0,       0,       0,       ocl2ocl}
wester committed
174 175 176 177 178
    };

    const int src_kind = src.kind() >> _InputArray::KIND_SHIFT;
    const int dst_kind = dst.kind() >> _InputArray::KIND_SHIFT;

wester committed
179 180
    CV_DbgAssert( src_kind >= 0 && src_kind < 11 );
    CV_DbgAssert( dst_kind >= 0 && dst_kind < 11 );
wester committed
181 182

    const func_t func = funcs[src_kind][dst_kind];
wester committed
183
    CV_DbgAssert( func != 0 );
wester committed
184 185 186 187 188 189 190 191

    func(src, dst);
}

namespace
{
    void convertToCn(InputArray src, OutputArray dst, int cn)
    {
wester committed
192
        CV_Assert( src.channels() == 1 || src.channels() == 3 || src.channels() == 4 );
wester committed
193 194 195 196
        CV_Assert( cn == 1 || cn == 3 || cn == 4 );

        static const int codes[5][5] =
        {
wester committed
197 198 199 200 201
            {-1, -1, -1, -1, -1},
            {-1, -1, -1, COLOR_GRAY2BGR, COLOR_GRAY2BGRA},
            {-1, -1, -1, -1, -1},
            {-1, COLOR_BGR2GRAY, -1, -1, COLOR_BGR2BGRA},
            {-1, COLOR_BGRA2GRAY, -1, COLOR_BGRA2BGR, -1},
wester committed
202 203
        };

wester committed
204 205
        const int code = codes[src.channels()][cn];
        CV_DbgAssert( code >= 0 );
wester committed
206 207 208

        switch (src.kind())
        {
wester committed
209 210 211
        case _InputArray::GPU_MAT:
            #if defined(HAVE_OPENCV_GPU) && !defined(DYNAMIC_CUDA_SUPPORT)
                gpu::cvtColor(src.getGpuMat(), dst.getGpuMatRef(), code, cn);
wester committed
212
            #else
wester committed
213
                CV_Error(CV_StsNotImplemented, "The called functionality is disabled for current build or platform");
wester committed
214 215 216 217
            #endif
            break;

        default:
wester committed
218
            cvtColor(src, dst, code, cn);
wester committed
219 220 221 222 223
            break;
        }
    }
    void convertToDepth(InputArray src, OutputArray dst, int depth)
    {
a  
Kai Westerkamp committed
224
        CV_Assert( src.depth() <= CV_64F );
wester committed
225 226
        CV_Assert( depth == CV_8U || depth == CV_32F );

a  
Kai Westerkamp committed
227
        static const double maxVals[] =
wester committed
228
        {
wester committed
229 230 231 232 233
            (double)numeric_limits<uchar>::max(),
            (double)numeric_limits<schar>::max(),
            (double)numeric_limits<ushort>::max(),
            (double)numeric_limits<short>::max(),
            (double)numeric_limits<int>::max(),
wester committed
234 235 236 237
            1.0,
            1.0,
        };

a  
Kai Westerkamp committed
238
        const double scale = maxVals[depth] / maxVals[src.depth()];
wester committed
239 240 241

        switch (src.kind())
        {
wester committed
242
        case _InputArray::GPU_MAT:
wester committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
            src.getGpuMat().convertTo(dst.getGpuMatRef(), depth, scale);
            break;

        default:
            src.getMat().convertTo(dst, depth, scale);
            break;
        }
    }
}

Mat cv::superres::convertToType(const Mat& src, int type, Mat& buf0, Mat& buf1)
{
    if (src.type() == type)
        return src;

    const int depth = CV_MAT_DEPTH(type);
    const int cn = CV_MAT_CN(type);

    if (src.depth() == depth)
    {
        convertToCn(src, buf0, cn);
        return buf0;
    }

    if (src.channels() == cn)
    {
        convertToDepth(src, buf1, depth);
        return buf1;
    }

    convertToCn(src, buf0, cn);
    convertToDepth(buf0, buf1, depth);
    return buf1;
}

wester committed
278
GpuMat cv::superres::convertToType(const GpuMat& src, int type, GpuMat& buf0, GpuMat& buf1)
wester committed
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
{
    if (src.type() == type)
        return src;

    const int depth = CV_MAT_DEPTH(type);
    const int cn = CV_MAT_CN(type);

    if (src.depth() == depth)
    {
        convertToCn(src, buf0, cn);
        return buf0;
    }

    if (src.channels() == cn)
    {
        convertToDepth(src, buf1, depth);
        return buf1;
    }

    convertToCn(src, buf0, cn);
    convertToDepth(buf0, buf1, depth);
    return buf1;
}
wester committed
302 303 304 305 306 307 308 309
#ifdef HAVE_OPENCV_OCL
namespace
{
    // TODO(pengx17): remove these overloaded functions until IntputArray fully supports oclMat
    void convertToCn(const ocl::oclMat& src, ocl::oclMat& dst, int cn)
    {
        CV_Assert( src.channels() == 1 || src.channels() == 3 || src.channels() == 4 );
        CV_Assert( cn == 1 || cn == 3 || cn == 4 );
wester committed
310

wester committed
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
        static const int codes[5][5] =
        {
            {-1, -1, -1, -1, -1},
            {-1, -1, -1, COLOR_GRAY2BGR, COLOR_GRAY2BGRA},
            {-1, -1, -1, -1, -1},
            {-1, COLOR_BGR2GRAY, -1, -1, COLOR_BGR2BGRA},
            {-1, COLOR_BGRA2GRAY, -1, COLOR_BGRA2BGR, -1},
        };

        const int code = codes[src.channels()][cn];
        CV_DbgAssert( code >= 0 );

        ocl::cvtColor(src, dst, code, cn);
    }
    void convertToDepth(const ocl::oclMat& src, ocl::oclMat& dst, int depth)
    {
        CV_Assert( src.depth() <= CV_64F );
        CV_Assert( depth == CV_8U || depth == CV_32F );

        static const double maxVals[] =
        {
            (double)std::numeric_limits<uchar>::max(),
            (double)std::numeric_limits<schar>::max(),
            (double)std::numeric_limits<ushort>::max(),
            (double)std::numeric_limits<short>::max(),
            (double)std::numeric_limits<int>::max(),
            1.0,
            1.0,
        };
        const double scale = maxVals[depth] / maxVals[src.depth()];
        src.convertTo(dst, depth, scale);
    }
}
ocl::oclMat cv::superres::convertToType(const ocl::oclMat& src, int type, ocl::oclMat& buf0, ocl::oclMat& buf1)
wester committed
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
{
    if (src.type() == type)
        return src;

    const int depth = CV_MAT_DEPTH(type);
    const int cn = CV_MAT_CN(type);

    if (src.depth() == depth)
    {
        convertToCn(src, buf0, cn);
        return buf0;
    }

    if (src.channels() == cn)
    {
        convertToDepth(src, buf1, depth);
        return buf1;
    }

    convertToCn(src, buf0, cn);
    convertToDepth(buf0, buf1, depth);
    return buf1;
}
wester committed
368
#endif