Path: blob/master/modules/superres/src/input_array_utility.cpp
16354 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#include "precomp.hpp"4344using namespace cv;45using namespace cv::cuda;4647Mat cv::superres::arrGetMat(InputArray arr, Mat& buf)48{49switch (arr.kind())50{51case _InputArray::CUDA_GPU_MAT:52arr.getGpuMat().download(buf);53return buf;5455case _InputArray::OPENGL_BUFFER:56arr.getOGlBuffer().copyTo(buf);57return buf;5859default:60return arr.getMat();61}62}6364UMat cv::superres::arrGetUMat(InputArray arr, UMat& buf)65{66switch (arr.kind())67{68case _InputArray::CUDA_GPU_MAT:69arr.getGpuMat().download(buf);70return buf;7172case _InputArray::OPENGL_BUFFER:73arr.getOGlBuffer().copyTo(buf);74return buf;7576default:77return arr.getUMat();78}79}8081GpuMat cv::superres::arrGetGpuMat(InputArray arr, GpuMat& buf)82{83switch (arr.kind())84{85case _InputArray::CUDA_GPU_MAT:86return arr.getGpuMat();8788case _InputArray::OPENGL_BUFFER:89arr.getOGlBuffer().copyTo(buf);90return buf;9192default:93buf.upload(arr.getMat());94return buf;95}96}9798namespace99{100void mat2mat(InputArray src, OutputArray dst)101{102src.getMat().copyTo(dst);103}104void arr2buf(InputArray src, OutputArray dst)105{106dst.getOGlBufferRef().copyFrom(src);107}108void mat2gpu(InputArray src, OutputArray dst)109{110dst.getGpuMatRef().upload(src.getMat());111}112void buf2arr(InputArray src, OutputArray dst)113{114src.getOGlBuffer().copyTo(dst);115}116void gpu2mat(InputArray src, OutputArray dst)117{118GpuMat d = src.getGpuMat();119dst.create(d.size(), d.type());120Mat m = dst.getMat();121d.download(m);122}123void gpu2gpu(InputArray src, OutputArray dst)124{125src.getGpuMat().copyTo(dst.getGpuMatRef());126}127}128129void cv::superres::arrCopy(InputArray src, OutputArray dst)130{131if (dst.isUMat() || src.isUMat())132{133src.copyTo(dst);134return;135}136137typedef void (*func_t)(InputArray src, OutputArray dst);138static const func_t funcs[10][10] =139{140{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },141{ 0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, 0, mat2gpu },142{ 0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, 0, mat2gpu },143{ 0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, 0, mat2gpu },144{ 0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, 0, mat2gpu },145{ 0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, 0, mat2gpu },146{ 0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, 0, mat2gpu },147{ 0, buf2arr, buf2arr, buf2arr, buf2arr, buf2arr, buf2arr, buf2arr, 0, buf2arr },148{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },149{ 0, gpu2mat, gpu2mat, gpu2mat, gpu2mat, gpu2mat, gpu2mat, arr2buf, 0 , gpu2gpu },150};151152const int src_kind = src.kind() >> _InputArray::KIND_SHIFT;153const int dst_kind = dst.kind() >> _InputArray::KIND_SHIFT;154155CV_Assert( src_kind >= 0 && src_kind < 10 );156CV_Assert( dst_kind >= 0 && dst_kind < 10 );157158const func_t func = funcs[src_kind][dst_kind];159CV_Assert( func != 0 );160161func(src, dst);162}163164namespace165{166void convertToCn(InputArray src, OutputArray dst, int cn)167{168int scn = src.channels();169CV_Assert( scn == 1 || scn == 3 || scn == 4 );170CV_Assert( cn == 1 || cn == 3 || cn == 4 );171172static const int codes[5][5] =173{174{ -1, -1, -1, -1, -1 },175{ -1, -1, -1, COLOR_GRAY2BGR, COLOR_GRAY2BGRA },176{ -1, -1, -1, -1, -1 },177{ -1, COLOR_BGR2GRAY, -1, -1, COLOR_BGR2BGRA },178{ -1, COLOR_BGRA2GRAY, -1, COLOR_BGRA2BGR, -1 }179};180181const int code = codes[scn][cn];182CV_Assert( code >= 0 );183184switch (src.kind())185{186case _InputArray::CUDA_GPU_MAT:187#ifdef HAVE_OPENCV_CUDAIMGPROC188cuda::cvtColor(src.getGpuMat(), dst.getGpuMatRef(), code, cn);189#else190CV_Error(cv::Error::StsNotImplemented, "The called functionality is disabled for current build or platform");191#endif192break;193194default:195cv::cvtColor(src, dst, code, cn);196break;197}198}199200void convertToDepth(InputArray src, OutputArray dst, int depth)201{202const int sdepth = src.depth();203CV_Assert( sdepth <= CV_64F );204CV_Assert( depth == CV_8U || depth == CV_32F );205206static const double maxVals[CV_64F + 1] =207{208(double)std::numeric_limits<uchar>::max(),209(double)std::numeric_limits<schar>::max(),210(double)std::numeric_limits<ushort>::max(),211(double)std::numeric_limits<short>::max(),212(double)std::numeric_limits<int>::max(),2131.0,2141.0,215};216217const double scale = maxVals[depth] / maxVals[sdepth];218219switch (src.kind())220{221case _InputArray::CUDA_GPU_MAT:222src.getGpuMat().convertTo(dst.getGpuMatRef(), depth, scale);223break;224225case _InputArray::UMAT:226src.getUMat().convertTo(dst, depth, scale);227break;228229default:230src.getMat().convertTo(dst, depth, scale);231break;232}233}234}235236Mat cv::superres::convertToType(const Mat& src, int type, Mat& buf0, Mat& buf1)237{238CV_INSTRUMENT_REGION();239240if (src.type() == type)241return src;242243const int depth = CV_MAT_DEPTH(type);244const int cn = CV_MAT_CN(type);245246if (src.depth() == depth)247{248convertToCn(src, buf0, cn);249return buf0;250}251252if (src.channels() == cn)253{254convertToDepth(src, buf1, depth);255return buf1;256}257258convertToCn(src, buf0, cn);259convertToDepth(buf0, buf1, depth);260return buf1;261}262263UMat cv::superres::convertToType(const UMat& src, int type, UMat& buf0, UMat& buf1)264{265CV_INSTRUMENT_REGION();266267if (src.type() == type)268return src;269270const int depth = CV_MAT_DEPTH(type);271const int cn = CV_MAT_CN(type);272273if (src.depth() == depth)274{275convertToCn(src, buf0, cn);276return buf0;277}278279if (src.channels() == cn)280{281convertToDepth(src, buf1, depth);282return buf1;283}284285convertToCn(src, buf0, cn);286convertToDepth(buf0, buf1, depth);287return buf1;288}289290GpuMat cv::superres::convertToType(const GpuMat& src, int type, GpuMat& buf0, GpuMat& buf1)291{292if (src.type() == type)293return src;294295const int depth = CV_MAT_DEPTH(type);296const int cn = CV_MAT_CN(type);297298if (src.depth() == depth)299{300convertToCn(src, buf0, cn);301return buf0;302}303304if (src.channels() == cn)305{306convertToDepth(src, buf1, depth);307return buf1;308}309310convertToCn(src, buf0, cn);311convertToDepth(buf0, buf1, depth);312return buf1;313}314315316