mat.inl.hpp 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533
  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. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  16. // Copyright (C) 2015, Itseez Inc., all rights reserved.
  17. // Third party copyrights are property of their respective owners.
  18. //
  19. // Redistribution and use in source and binary forms, with or without modification,
  20. // are permitted provided that the following conditions are met:
  21. //
  22. // * Redistribution's of source code must retain the above copyright notice,
  23. // this list of conditions and the following disclaimer.
  24. //
  25. // * Redistribution's in binary form must reproduce the above copyright notice,
  26. // this list of conditions and the following disclaimer in the documentation
  27. // and/or other materials provided with the distribution.
  28. //
  29. // * The name of the copyright holders may not be used to endorse or promote products
  30. // derived from this software without specific prior written permission.
  31. //
  32. // This software is provided by the copyright holders and contributors "as is" and
  33. // any express or implied warranties, including, but not limited to, the implied
  34. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  35. // In no event shall the Intel Corporation or contributors be liable for any direct,
  36. // indirect, incidental, special, exemplary, or consequential damages
  37. // (including, but not limited to, procurement of substitute goods or services;
  38. // loss of use, data, or profits; or business interruption) however caused
  39. // and on any theory of liability, whether in contract, strict liability,
  40. // or tort (including negligence or otherwise) arising in any way out of
  41. // the use of this software, even if advised of the possibility of such damage.
  42. //
  43. //M*/
  44. #ifndef OPENCV_CORE_MATRIX_OPERATIONS_HPP
  45. #define OPENCV_CORE_MATRIX_OPERATIONS_HPP
  46. #ifndef __cplusplus
  47. # error mat.inl.hpp header must be compiled as C++
  48. #endif
  49. #ifdef _MSC_VER
  50. #pragma warning( push )
  51. #pragma warning( disable: 4127 )
  52. #endif
  53. #if defined(CV_SKIP_DISABLE_CLANG_ENUM_WARNINGS)
  54. // nothing
  55. #elif defined(CV_FORCE_DISABLE_CLANG_ENUM_WARNINGS)
  56. #define CV_DISABLE_CLANG_ENUM_WARNINGS
  57. #elif defined(__clang__) && defined(__has_warning)
  58. #if __has_warning("-Wdeprecated-enum-enum-conversion") && __has_warning("-Wdeprecated-anon-enum-enum-conversion")
  59. #define CV_DISABLE_CLANG_ENUM_WARNINGS
  60. #endif
  61. #endif
  62. #ifdef CV_DISABLE_CLANG_ENUM_WARNINGS
  63. #pragma clang diagnostic push
  64. #pragma clang diagnostic ignored "-Wdeprecated-enum-enum-conversion"
  65. #pragma clang diagnostic ignored "-Wdeprecated-anon-enum-enum-conversion"
  66. #endif
  67. namespace cv
  68. {
  69. CV__DEBUG_NS_BEGIN
  70. //! @cond IGNORED
  71. ////////////////////////// Custom (raw) type wrapper //////////////////////////
  72. template<typename _Tp> static inline
  73. int rawType()
  74. {
  75. CV_StaticAssert(sizeof(_Tp) <= CV_CN_MAX, "sizeof(_Tp) is too large");
  76. const int elemSize = sizeof(_Tp);
  77. return (int)CV_MAKETYPE(CV_8U, elemSize);
  78. }
  79. //////////////////////// Input/Output Arrays ////////////////////////
  80. inline void _InputArray::init(int _flags, const void* _obj)
  81. { flags = _flags; obj = (void*)_obj; }
  82. inline void _InputArray::init(int _flags, const void* _obj, Size _sz)
  83. { flags = _flags; obj = (void*)_obj; sz = _sz; }
  84. inline void* _InputArray::getObj() const { return obj; }
  85. inline int _InputArray::getFlags() const { return flags; }
  86. inline Size _InputArray::getSz() const { return sz; }
  87. inline _InputArray::_InputArray() { init(NONE, 0); }
  88. inline _InputArray::_InputArray(int _flags, void* _obj) { init(_flags, _obj); }
  89. inline _InputArray::_InputArray(const Mat& m) { init(MAT+ACCESS_READ, &m); }
  90. inline _InputArray::_InputArray(const std::vector<Mat>& vec) { init(STD_VECTOR_MAT+ACCESS_READ, &vec); }
  91. inline _InputArray::_InputArray(const UMat& m) { init(UMAT+ACCESS_READ, &m); }
  92. inline _InputArray::_InputArray(const std::vector<UMat>& vec) { init(STD_VECTOR_UMAT+ACCESS_READ, &vec); }
  93. template<typename _Tp> inline
  94. _InputArray::_InputArray(const std::vector<_Tp>& vec)
  95. { init(FIXED_TYPE + STD_VECTOR + traits::Type<_Tp>::value + ACCESS_READ, &vec); }
  96. #ifdef CV_CXX_STD_ARRAY
  97. template<typename _Tp, std::size_t _Nm> inline
  98. _InputArray::_InputArray(const std::array<_Tp, _Nm>& arr)
  99. { init(FIXED_TYPE + FIXED_SIZE + MATX + traits::Type<_Tp>::value + ACCESS_READ, arr.data(), Size(1, _Nm)); }
  100. template<std::size_t _Nm> inline
  101. _InputArray::_InputArray(const std::array<Mat, _Nm>& arr)
  102. { init(STD_ARRAY_MAT + ACCESS_READ, arr.data(), Size(1, _Nm)); }
  103. #endif
  104. inline
  105. _InputArray::_InputArray(const std::vector<bool>& vec)
  106. { init(FIXED_TYPE + STD_BOOL_VECTOR + traits::Type<bool>::value + ACCESS_READ, &vec); }
  107. template<typename _Tp> inline
  108. _InputArray::_InputArray(const std::vector<std::vector<_Tp> >& vec)
  109. { init(FIXED_TYPE + STD_VECTOR_VECTOR + traits::Type<_Tp>::value + ACCESS_READ, &vec); }
  110. inline
  111. _InputArray::_InputArray(const std::vector<std::vector<bool> >&)
  112. { CV_Error(Error::StsUnsupportedFormat, "std::vector<std::vector<bool> > is not supported!\n"); }
  113. template<typename _Tp> inline
  114. _InputArray::_InputArray(const std::vector<Mat_<_Tp> >& vec)
  115. { init(FIXED_TYPE + STD_VECTOR_MAT + traits::Type<_Tp>::value + ACCESS_READ, &vec); }
  116. template<typename _Tp, int m, int n> inline
  117. _InputArray::_InputArray(const Matx<_Tp, m, n>& mtx)
  118. { init(FIXED_TYPE + FIXED_SIZE + MATX + traits::Type<_Tp>::value + ACCESS_READ, &mtx, Size(n, m)); }
  119. template<typename _Tp> inline
  120. _InputArray::_InputArray(const _Tp* vec, int n)
  121. { init(FIXED_TYPE + FIXED_SIZE + MATX + traits::Type<_Tp>::value + ACCESS_READ, vec, Size(n, 1)); }
  122. template<typename _Tp> inline
  123. _InputArray::_InputArray(const Mat_<_Tp>& m)
  124. { init(FIXED_TYPE + MAT + traits::Type<_Tp>::value + ACCESS_READ, &m); }
  125. inline _InputArray::_InputArray(const double& val)
  126. { init(FIXED_TYPE + FIXED_SIZE + MATX + CV_64F + ACCESS_READ, &val, Size(1,1)); }
  127. inline _InputArray::_InputArray(const cuda::GpuMat& d_mat)
  128. { init(CUDA_GPU_MAT + ACCESS_READ, &d_mat); }
  129. inline _InputArray::_InputArray(const std::vector<cuda::GpuMat>& d_mat)
  130. { init(STD_VECTOR_CUDA_GPU_MAT + ACCESS_READ, &d_mat);}
  131. inline _InputArray::_InputArray(const ogl::Buffer& buf)
  132. { init(OPENGL_BUFFER + ACCESS_READ, &buf); }
  133. inline _InputArray::_InputArray(const cuda::HostMem& cuda_mem)
  134. { init(CUDA_HOST_MEM + ACCESS_READ, &cuda_mem); }
  135. template<typename _Tp> inline
  136. _InputArray _InputArray::rawIn(const std::vector<_Tp>& vec)
  137. {
  138. _InputArray v;
  139. v.flags = _InputArray::FIXED_TYPE + _InputArray::STD_VECTOR + rawType<_Tp>() + ACCESS_READ;
  140. v.obj = (void*)&vec;
  141. return v;
  142. }
  143. #ifdef CV_CXX_STD_ARRAY
  144. template<typename _Tp, std::size_t _Nm> inline
  145. _InputArray _InputArray::rawIn(const std::array<_Tp, _Nm>& arr)
  146. {
  147. _InputArray v;
  148. v.flags = FIXED_TYPE + FIXED_SIZE + MATX + traits::Type<_Tp>::value + ACCESS_READ;
  149. v.obj = (void*)arr.data();
  150. v.sz = Size(1, _Nm);
  151. return v;
  152. }
  153. #endif
  154. inline _InputArray::~_InputArray() {}
  155. inline Mat _InputArray::getMat(int i) const
  156. {
  157. if( kind() == MAT && i < 0 )
  158. return *(const Mat*)obj;
  159. return getMat_(i);
  160. }
  161. inline bool _InputArray::isMat() const { return kind() == _InputArray::MAT; }
  162. inline bool _InputArray::isUMat() const { return kind() == _InputArray::UMAT; }
  163. inline bool _InputArray::isMatVector() const { return kind() == _InputArray::STD_VECTOR_MAT; }
  164. inline bool _InputArray::isUMatVector() const { return kind() == _InputArray::STD_VECTOR_UMAT; }
  165. inline bool _InputArray::isMatx() const { return kind() == _InputArray::MATX; }
  166. inline bool _InputArray::isVector() const { return kind() == _InputArray::STD_VECTOR ||
  167. kind() == _InputArray::STD_BOOL_VECTOR ||
  168. (kind() == _InputArray::MATX && (sz.width <= 1 || sz.height <= 1)); }
  169. inline bool _InputArray::isGpuMat() const { return kind() == _InputArray::CUDA_GPU_MAT; }
  170. inline bool _InputArray::isGpuMatVector() const { return kind() == _InputArray::STD_VECTOR_CUDA_GPU_MAT; }
  171. ////////////////////////////////////////////////////////////////////////////////////////
  172. inline _OutputArray::_OutputArray() { init(ACCESS_WRITE, 0); }
  173. inline _OutputArray::_OutputArray(int _flags, void* _obj) { init(_flags|ACCESS_WRITE, _obj); }
  174. inline _OutputArray::_OutputArray(Mat& m) { init(MAT+ACCESS_WRITE, &m); }
  175. inline _OutputArray::_OutputArray(std::vector<Mat>& vec) { init(STD_VECTOR_MAT+ACCESS_WRITE, &vec); }
  176. inline _OutputArray::_OutputArray(UMat& m) { init(UMAT+ACCESS_WRITE, &m); }
  177. inline _OutputArray::_OutputArray(std::vector<UMat>& vec) { init(STD_VECTOR_UMAT+ACCESS_WRITE, &vec); }
  178. template<typename _Tp> inline
  179. _OutputArray::_OutputArray(std::vector<_Tp>& vec)
  180. { init(FIXED_TYPE + STD_VECTOR + traits::Type<_Tp>::value + ACCESS_WRITE, &vec); }
  181. #ifdef CV_CXX_STD_ARRAY
  182. template<typename _Tp, std::size_t _Nm> inline
  183. _OutputArray::_OutputArray(std::array<_Tp, _Nm>& arr)
  184. { init(FIXED_TYPE + FIXED_SIZE + MATX + traits::Type<_Tp>::value + ACCESS_WRITE, arr.data(), Size(1, _Nm)); }
  185. template<std::size_t _Nm> inline
  186. _OutputArray::_OutputArray(std::array<Mat, _Nm>& arr)
  187. { init(STD_ARRAY_MAT + ACCESS_WRITE, arr.data(), Size(1, _Nm)); }
  188. #endif
  189. inline
  190. _OutputArray::_OutputArray(std::vector<bool>&)
  191. { CV_Error(Error::StsUnsupportedFormat, "std::vector<bool> cannot be an output array\n"); }
  192. template<typename _Tp> inline
  193. _OutputArray::_OutputArray(std::vector<std::vector<_Tp> >& vec)
  194. { init(FIXED_TYPE + STD_VECTOR_VECTOR + traits::Type<_Tp>::value + ACCESS_WRITE, &vec); }
  195. inline
  196. _OutputArray::_OutputArray(std::vector<std::vector<bool> >&)
  197. { CV_Error(Error::StsUnsupportedFormat, "std::vector<std::vector<bool> > cannot be an output array\n"); }
  198. template<typename _Tp> inline
  199. _OutputArray::_OutputArray(std::vector<Mat_<_Tp> >& vec)
  200. { init(FIXED_TYPE + STD_VECTOR_MAT + traits::Type<_Tp>::value + ACCESS_WRITE, &vec); }
  201. template<typename _Tp> inline
  202. _OutputArray::_OutputArray(Mat_<_Tp>& m)
  203. { init(FIXED_TYPE + MAT + traits::Type<_Tp>::value + ACCESS_WRITE, &m); }
  204. template<typename _Tp, int m, int n> inline
  205. _OutputArray::_OutputArray(Matx<_Tp, m, n>& mtx)
  206. { init(FIXED_TYPE + FIXED_SIZE + MATX + traits::Type<_Tp>::value + ACCESS_WRITE, &mtx, Size(n, m)); }
  207. template<typename _Tp> inline
  208. _OutputArray::_OutputArray(_Tp* vec, int n)
  209. { init(FIXED_TYPE + FIXED_SIZE + MATX + traits::Type<_Tp>::value + ACCESS_WRITE, vec, Size(n, 1)); }
  210. template<typename _Tp> inline
  211. _OutputArray::_OutputArray(const std::vector<_Tp>& vec)
  212. { init(FIXED_TYPE + FIXED_SIZE + STD_VECTOR + traits::Type<_Tp>::value + ACCESS_WRITE, &vec); }
  213. #ifdef CV_CXX_STD_ARRAY
  214. template<typename _Tp, std::size_t _Nm> inline
  215. _OutputArray::_OutputArray(const std::array<_Tp, _Nm>& arr)
  216. { init(FIXED_TYPE + FIXED_SIZE + MATX + traits::Type<_Tp>::value + ACCESS_WRITE, arr.data(), Size(1, _Nm)); }
  217. template<std::size_t _Nm> inline
  218. _OutputArray::_OutputArray(const std::array<Mat, _Nm>& arr)
  219. { init(FIXED_SIZE + STD_ARRAY_MAT + ACCESS_WRITE, arr.data(), Size(1, _Nm)); }
  220. #endif
  221. template<typename _Tp> inline
  222. _OutputArray::_OutputArray(const std::vector<std::vector<_Tp> >& vec)
  223. { init(FIXED_TYPE + FIXED_SIZE + STD_VECTOR_VECTOR + traits::Type<_Tp>::value + ACCESS_WRITE, &vec); }
  224. template<typename _Tp> inline
  225. _OutputArray::_OutputArray(const std::vector<Mat_<_Tp> >& vec)
  226. { init(FIXED_TYPE + FIXED_SIZE + STD_VECTOR_MAT + traits::Type<_Tp>::value + ACCESS_WRITE, &vec); }
  227. template<typename _Tp> inline
  228. _OutputArray::_OutputArray(const Mat_<_Tp>& m)
  229. { init(FIXED_TYPE + FIXED_SIZE + MAT + traits::Type<_Tp>::value + ACCESS_WRITE, &m); }
  230. template<typename _Tp, int m, int n> inline
  231. _OutputArray::_OutputArray(const Matx<_Tp, m, n>& mtx)
  232. { init(FIXED_TYPE + FIXED_SIZE + MATX + traits::Type<_Tp>::value + ACCESS_WRITE, &mtx, Size(n, m)); }
  233. template<typename _Tp> inline
  234. _OutputArray::_OutputArray(const _Tp* vec, int n)
  235. { init(FIXED_TYPE + FIXED_SIZE + MATX + traits::Type<_Tp>::value + ACCESS_WRITE, vec, Size(n, 1)); }
  236. inline _OutputArray::_OutputArray(cuda::GpuMat& d_mat)
  237. { init(CUDA_GPU_MAT + ACCESS_WRITE, &d_mat); }
  238. inline _OutputArray::_OutputArray(std::vector<cuda::GpuMat>& d_mat)
  239. { init(STD_VECTOR_CUDA_GPU_MAT + ACCESS_WRITE, &d_mat);}
  240. inline _OutputArray::_OutputArray(ogl::Buffer& buf)
  241. { init(OPENGL_BUFFER + ACCESS_WRITE, &buf); }
  242. inline _OutputArray::_OutputArray(cuda::HostMem& cuda_mem)
  243. { init(CUDA_HOST_MEM + ACCESS_WRITE, &cuda_mem); }
  244. inline _OutputArray::_OutputArray(const Mat& m)
  245. { init(FIXED_TYPE + FIXED_SIZE + MAT + ACCESS_WRITE, &m); }
  246. inline _OutputArray::_OutputArray(const std::vector<Mat>& vec)
  247. { init(FIXED_SIZE + STD_VECTOR_MAT + ACCESS_WRITE, &vec); }
  248. inline _OutputArray::_OutputArray(const UMat& m)
  249. { init(FIXED_TYPE + FIXED_SIZE + UMAT + ACCESS_WRITE, &m); }
  250. inline _OutputArray::_OutputArray(const std::vector<UMat>& vec)
  251. { init(FIXED_SIZE + STD_VECTOR_UMAT + ACCESS_WRITE, &vec); }
  252. inline _OutputArray::_OutputArray(const cuda::GpuMat& d_mat)
  253. { init(FIXED_TYPE + FIXED_SIZE + CUDA_GPU_MAT + ACCESS_WRITE, &d_mat); }
  254. inline _OutputArray::_OutputArray(const ogl::Buffer& buf)
  255. { init(FIXED_TYPE + FIXED_SIZE + OPENGL_BUFFER + ACCESS_WRITE, &buf); }
  256. inline _OutputArray::_OutputArray(const cuda::HostMem& cuda_mem)
  257. { init(FIXED_TYPE + FIXED_SIZE + CUDA_HOST_MEM + ACCESS_WRITE, &cuda_mem); }
  258. template<typename _Tp> inline
  259. _OutputArray _OutputArray::rawOut(std::vector<_Tp>& vec)
  260. {
  261. _OutputArray v;
  262. v.flags = _InputArray::FIXED_TYPE + _InputArray::STD_VECTOR + rawType<_Tp>() + ACCESS_WRITE;
  263. v.obj = (void*)&vec;
  264. return v;
  265. }
  266. #ifdef CV_CXX_STD_ARRAY
  267. template<typename _Tp, std::size_t _Nm> inline
  268. _OutputArray _OutputArray::rawOut(std::array<_Tp, _Nm>& arr)
  269. {
  270. _OutputArray v;
  271. v.flags = FIXED_TYPE + FIXED_SIZE + MATX + traits::Type<_Tp>::value + ACCESS_WRITE;
  272. v.obj = (void*)arr.data();
  273. v.sz = Size(1, _Nm);
  274. return v;
  275. }
  276. #endif
  277. ///////////////////////////////////////////////////////////////////////////////////////////
  278. inline _InputOutputArray::_InputOutputArray() { init(ACCESS_RW, 0); }
  279. inline _InputOutputArray::_InputOutputArray(int _flags, void* _obj) { init(_flags|ACCESS_RW, _obj); }
  280. inline _InputOutputArray::_InputOutputArray(Mat& m) { init(MAT+ACCESS_RW, &m); }
  281. inline _InputOutputArray::_InputOutputArray(std::vector<Mat>& vec) { init(STD_VECTOR_MAT+ACCESS_RW, &vec); }
  282. inline _InputOutputArray::_InputOutputArray(UMat& m) { init(UMAT+ACCESS_RW, &m); }
  283. inline _InputOutputArray::_InputOutputArray(std::vector<UMat>& vec) { init(STD_VECTOR_UMAT+ACCESS_RW, &vec); }
  284. template<typename _Tp> inline
  285. _InputOutputArray::_InputOutputArray(std::vector<_Tp>& vec)
  286. { init(FIXED_TYPE + STD_VECTOR + traits::Type<_Tp>::value + ACCESS_RW, &vec); }
  287. #ifdef CV_CXX_STD_ARRAY
  288. template<typename _Tp, std::size_t _Nm> inline
  289. _InputOutputArray::_InputOutputArray(std::array<_Tp, _Nm>& arr)
  290. { init(FIXED_TYPE + FIXED_SIZE + MATX + traits::Type<_Tp>::value + ACCESS_RW, arr.data(), Size(1, _Nm)); }
  291. template<std::size_t _Nm> inline
  292. _InputOutputArray::_InputOutputArray(std::array<Mat, _Nm>& arr)
  293. { init(STD_ARRAY_MAT + ACCESS_RW, arr.data(), Size(1, _Nm)); }
  294. #endif
  295. inline _InputOutputArray::_InputOutputArray(std::vector<bool>&)
  296. { CV_Error(Error::StsUnsupportedFormat, "std::vector<bool> cannot be an input/output array\n"); }
  297. template<typename _Tp> inline
  298. _InputOutputArray::_InputOutputArray(std::vector<std::vector<_Tp> >& vec)
  299. { init(FIXED_TYPE + STD_VECTOR_VECTOR + traits::Type<_Tp>::value + ACCESS_RW, &vec); }
  300. template<typename _Tp> inline
  301. _InputOutputArray::_InputOutputArray(std::vector<Mat_<_Tp> >& vec)
  302. { init(FIXED_TYPE + STD_VECTOR_MAT + traits::Type<_Tp>::value + ACCESS_RW, &vec); }
  303. template<typename _Tp> inline
  304. _InputOutputArray::_InputOutputArray(Mat_<_Tp>& m)
  305. { init(FIXED_TYPE + MAT + traits::Type<_Tp>::value + ACCESS_RW, &m); }
  306. template<typename _Tp, int m, int n> inline
  307. _InputOutputArray::_InputOutputArray(Matx<_Tp, m, n>& mtx)
  308. { init(FIXED_TYPE + FIXED_SIZE + MATX + traits::Type<_Tp>::value + ACCESS_RW, &mtx, Size(n, m)); }
  309. template<typename _Tp> inline
  310. _InputOutputArray::_InputOutputArray(_Tp* vec, int n)
  311. { init(FIXED_TYPE + FIXED_SIZE + MATX + traits::Type<_Tp>::value + ACCESS_RW, vec, Size(n, 1)); }
  312. template<typename _Tp> inline
  313. _InputOutputArray::_InputOutputArray(const std::vector<_Tp>& vec)
  314. { init(FIXED_TYPE + FIXED_SIZE + STD_VECTOR + traits::Type<_Tp>::value + ACCESS_RW, &vec); }
  315. #ifdef CV_CXX_STD_ARRAY
  316. template<typename _Tp, std::size_t _Nm> inline
  317. _InputOutputArray::_InputOutputArray(const std::array<_Tp, _Nm>& arr)
  318. { init(FIXED_TYPE + FIXED_SIZE + MATX + traits::Type<_Tp>::value + ACCESS_RW, arr.data(), Size(1, _Nm)); }
  319. template<std::size_t _Nm> inline
  320. _InputOutputArray::_InputOutputArray(const std::array<Mat, _Nm>& arr)
  321. { init(FIXED_SIZE + STD_ARRAY_MAT + ACCESS_RW, arr.data(), Size(1, _Nm)); }
  322. #endif
  323. template<typename _Tp> inline
  324. _InputOutputArray::_InputOutputArray(const std::vector<std::vector<_Tp> >& vec)
  325. { init(FIXED_TYPE + FIXED_SIZE + STD_VECTOR_VECTOR + traits::Type<_Tp>::value + ACCESS_RW, &vec); }
  326. template<typename _Tp> inline
  327. _InputOutputArray::_InputOutputArray(const std::vector<Mat_<_Tp> >& vec)
  328. { init(FIXED_TYPE + FIXED_SIZE + STD_VECTOR_MAT + traits::Type<_Tp>::value + ACCESS_RW, &vec); }
  329. template<typename _Tp> inline
  330. _InputOutputArray::_InputOutputArray(const Mat_<_Tp>& m)
  331. { init(FIXED_TYPE + FIXED_SIZE + MAT + traits::Type<_Tp>::value + ACCESS_RW, &m); }
  332. template<typename _Tp, int m, int n> inline
  333. _InputOutputArray::_InputOutputArray(const Matx<_Tp, m, n>& mtx)
  334. { init(FIXED_TYPE + FIXED_SIZE + MATX + traits::Type<_Tp>::value + ACCESS_RW, &mtx, Size(n, m)); }
  335. template<typename _Tp> inline
  336. _InputOutputArray::_InputOutputArray(const _Tp* vec, int n)
  337. { init(FIXED_TYPE + FIXED_SIZE + MATX + traits::Type<_Tp>::value + ACCESS_RW, vec, Size(n, 1)); }
  338. inline _InputOutputArray::_InputOutputArray(cuda::GpuMat& d_mat)
  339. { init(CUDA_GPU_MAT + ACCESS_RW, &d_mat); }
  340. inline _InputOutputArray::_InputOutputArray(ogl::Buffer& buf)
  341. { init(OPENGL_BUFFER + ACCESS_RW, &buf); }
  342. inline _InputOutputArray::_InputOutputArray(cuda::HostMem& cuda_mem)
  343. { init(CUDA_HOST_MEM + ACCESS_RW, &cuda_mem); }
  344. inline _InputOutputArray::_InputOutputArray(const Mat& m)
  345. { init(FIXED_TYPE + FIXED_SIZE + MAT + ACCESS_RW, &m); }
  346. inline _InputOutputArray::_InputOutputArray(const std::vector<Mat>& vec)
  347. { init(FIXED_SIZE + STD_VECTOR_MAT + ACCESS_RW, &vec); }
  348. inline _InputOutputArray::_InputOutputArray(const UMat& m)
  349. { init(FIXED_TYPE + FIXED_SIZE + UMAT + ACCESS_RW, &m); }
  350. inline _InputOutputArray::_InputOutputArray(const std::vector<UMat>& vec)
  351. { init(FIXED_SIZE + STD_VECTOR_UMAT + ACCESS_RW, &vec); }
  352. inline _InputOutputArray::_InputOutputArray(const cuda::GpuMat& d_mat)
  353. { init(FIXED_TYPE + FIXED_SIZE + CUDA_GPU_MAT + ACCESS_RW, &d_mat); }
  354. inline _InputOutputArray::_InputOutputArray(const std::vector<cuda::GpuMat>& d_mat)
  355. { init(FIXED_TYPE + FIXED_SIZE + STD_VECTOR_CUDA_GPU_MAT + ACCESS_RW, &d_mat);}
  356. template<> inline _InputOutputArray::_InputOutputArray(std::vector<cuda::GpuMat>& d_mat)
  357. { init(FIXED_TYPE + FIXED_SIZE + STD_VECTOR_CUDA_GPU_MAT + ACCESS_RW, &d_mat);}
  358. inline _InputOutputArray::_InputOutputArray(const ogl::Buffer& buf)
  359. { init(FIXED_TYPE + FIXED_SIZE + OPENGL_BUFFER + ACCESS_RW, &buf); }
  360. inline _InputOutputArray::_InputOutputArray(const cuda::HostMem& cuda_mem)
  361. { init(FIXED_TYPE + FIXED_SIZE + CUDA_HOST_MEM + ACCESS_RW, &cuda_mem); }
  362. template<typename _Tp> inline
  363. _InputOutputArray _InputOutputArray::rawInOut(std::vector<_Tp>& vec)
  364. {
  365. _InputOutputArray v;
  366. v.flags = _InputArray::FIXED_TYPE + _InputArray::STD_VECTOR + rawType<_Tp>() + ACCESS_RW;
  367. v.obj = (void*)&vec;
  368. return v;
  369. }
  370. #ifdef CV_CXX_STD_ARRAY
  371. template<typename _Tp, std::size_t _Nm> inline
  372. _InputOutputArray _InputOutputArray::rawInOut(std::array<_Tp, _Nm>& arr)
  373. {
  374. _InputOutputArray v;
  375. v.flags = FIXED_TYPE + FIXED_SIZE + MATX + traits::Type<_Tp>::value + ACCESS_RW;
  376. v.obj = (void*)arr.data();
  377. v.sz = Size(1, _Nm);
  378. return v;
  379. }
  380. #endif
  381. template<typename _Tp> static inline _InputArray rawIn(_Tp& v) { return _InputArray::rawIn(v); }
  382. template<typename _Tp> static inline _OutputArray rawOut(_Tp& v) { return _OutputArray::rawOut(v); }
  383. template<typename _Tp> static inline _InputOutputArray rawInOut(_Tp& v) { return _InputOutputArray::rawInOut(v); }
  384. CV__DEBUG_NS_END
  385. //////////////////////////////////////////// Mat //////////////////////////////////////////
  386. template<typename _Tp> inline
  387. Mat::Mat(const std::vector<_Tp>& vec, bool copyData)
  388. : flags(MAGIC_VAL | traits::Type<_Tp>::value | CV_MAT_CONT_FLAG), dims(2), rows((int)vec.size()),
  389. cols(1), data(0), datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows), step(0)
  390. {
  391. if(vec.empty())
  392. return;
  393. if( !copyData )
  394. {
  395. step[0] = step[1] = sizeof(_Tp);
  396. datastart = data = (uchar*)&vec[0];
  397. datalimit = dataend = datastart + rows * step[0];
  398. }
  399. else
  400. Mat((int)vec.size(), 1, traits::Type<_Tp>::value, (uchar*)&vec[0]).copyTo(*this);
  401. }
  402. #ifdef CV_CXX11
  403. template<typename _Tp, typename> inline
  404. Mat::Mat(const std::initializer_list<_Tp> list)
  405. : Mat()
  406. {
  407. CV_Assert(list.size() != 0);
  408. Mat((int)list.size(), 1, traits::Type<_Tp>::value, (uchar*)list.begin()).copyTo(*this);
  409. }
  410. template<typename _Tp> inline
  411. Mat::Mat(const std::initializer_list<int> sizes, const std::initializer_list<_Tp> list)
  412. : Mat()
  413. {
  414. size_t size_total = 1;
  415. for(auto s : sizes)
  416. size_total *= s;
  417. CV_Assert(list.size() != 0);
  418. CV_Assert(size_total == list.size());
  419. Mat((int)sizes.size(), (int*)sizes.begin(), traits::Type<_Tp>::value, (uchar*)list.begin()).copyTo(*this);
  420. }
  421. #endif
  422. #ifdef CV_CXX_STD_ARRAY
  423. template<typename _Tp, std::size_t _Nm> inline
  424. Mat::Mat(const std::array<_Tp, _Nm>& arr, bool copyData)
  425. : flags(MAGIC_VAL | traits::Type<_Tp>::value | CV_MAT_CONT_FLAG), dims(2), rows((int)arr.size()),
  426. cols(1), data(0), datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows), step(0)
  427. {
  428. if(arr.empty())
  429. return;
  430. if( !copyData )
  431. {
  432. step[0] = step[1] = sizeof(_Tp);
  433. datastart = data = (uchar*)arr.data();
  434. datalimit = dataend = datastart + rows * step[0];
  435. }
  436. else
  437. Mat((int)arr.size(), 1, traits::Type<_Tp>::value, (uchar*)arr.data()).copyTo(*this);
  438. }
  439. #endif
  440. template<typename _Tp, int n> inline
  441. Mat::Mat(const Vec<_Tp, n>& vec, bool copyData)
  442. : flags(MAGIC_VAL | traits::Type<_Tp>::value | CV_MAT_CONT_FLAG), dims(2), rows(n), cols(1), data(0),
  443. datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows), step(0)
  444. {
  445. if( !copyData )
  446. {
  447. step[0] = step[1] = sizeof(_Tp);
  448. datastart = data = (uchar*)vec.val;
  449. datalimit = dataend = datastart + rows * step[0];
  450. }
  451. else
  452. Mat(n, 1, traits::Type<_Tp>::value, (void*)vec.val).copyTo(*this);
  453. }
  454. template<typename _Tp, int m, int n> inline
  455. Mat::Mat(const Matx<_Tp,m,n>& M, bool copyData)
  456. : flags(MAGIC_VAL | traits::Type<_Tp>::value | CV_MAT_CONT_FLAG), dims(2), rows(m), cols(n), data(0),
  457. datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows), step(0)
  458. {
  459. if( !copyData )
  460. {
  461. step[0] = cols * sizeof(_Tp);
  462. step[1] = sizeof(_Tp);
  463. datastart = data = (uchar*)M.val;
  464. datalimit = dataend = datastart + rows * step[0];
  465. }
  466. else
  467. Mat(m, n, traits::Type<_Tp>::value, (uchar*)M.val).copyTo(*this);
  468. }
  469. template<typename _Tp> inline
  470. Mat::Mat(const Point_<_Tp>& pt, bool copyData)
  471. : flags(MAGIC_VAL | traits::Type<_Tp>::value | CV_MAT_CONT_FLAG), dims(2), rows(2), cols(1), data(0),
  472. datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows), step(0)
  473. {
  474. if( !copyData )
  475. {
  476. step[0] = step[1] = sizeof(_Tp);
  477. datastart = data = (uchar*)&pt.x;
  478. datalimit = dataend = datastart + rows * step[0];
  479. }
  480. else
  481. {
  482. create(2, 1, traits::Type<_Tp>::value);
  483. ((_Tp*)data)[0] = pt.x;
  484. ((_Tp*)data)[1] = pt.y;
  485. }
  486. }
  487. template<typename _Tp> inline
  488. Mat::Mat(const Point3_<_Tp>& pt, bool copyData)
  489. : flags(MAGIC_VAL | traits::Type<_Tp>::value | CV_MAT_CONT_FLAG), dims(2), rows(3), cols(1), data(0),
  490. datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows), step(0)
  491. {
  492. if( !copyData )
  493. {
  494. step[0] = step[1] = sizeof(_Tp);
  495. datastart = data = (uchar*)&pt.x;
  496. datalimit = dataend = datastart + rows * step[0];
  497. }
  498. else
  499. {
  500. create(3, 1, traits::Type<_Tp>::value);
  501. ((_Tp*)data)[0] = pt.x;
  502. ((_Tp*)data)[1] = pt.y;
  503. ((_Tp*)data)[2] = pt.z;
  504. }
  505. }
  506. template<typename _Tp> inline
  507. Mat::Mat(const MatCommaInitializer_<_Tp>& commaInitializer)
  508. : flags(MAGIC_VAL | traits::Type<_Tp>::value | CV_MAT_CONT_FLAG), dims(0), rows(0), cols(0), data(0),
  509. datastart(0), dataend(0), allocator(0), u(0), size(&rows)
  510. {
  511. *this = commaInitializer.operator Mat_<_Tp>();
  512. }
  513. inline
  514. Mat Mat::row(int y) const
  515. {
  516. return Mat(*this, Range(y, y + 1), Range::all());
  517. }
  518. inline
  519. Mat Mat::col(int x) const
  520. {
  521. return Mat(*this, Range::all(), Range(x, x + 1));
  522. }
  523. inline
  524. Mat Mat::rowRange(int startrow, int endrow) const
  525. {
  526. return Mat(*this, Range(startrow, endrow), Range::all());
  527. }
  528. inline
  529. Mat Mat::rowRange(const Range& r) const
  530. {
  531. return Mat(*this, r, Range::all());
  532. }
  533. inline
  534. Mat Mat::colRange(int startcol, int endcol) const
  535. {
  536. return Mat(*this, Range::all(), Range(startcol, endcol));
  537. }
  538. inline
  539. Mat Mat::colRange(const Range& r) const
  540. {
  541. return Mat(*this, Range::all(), r);
  542. }
  543. inline
  544. Mat Mat::operator()( Range _rowRange, Range _colRange ) const
  545. {
  546. return Mat(*this, _rowRange, _colRange);
  547. }
  548. inline
  549. Mat Mat::operator()( const Rect& roi ) const
  550. {
  551. return Mat(*this, roi);
  552. }
  553. inline
  554. Mat Mat::operator()(const Range* ranges) const
  555. {
  556. return Mat(*this, ranges);
  557. }
  558. inline
  559. Mat Mat::operator()(const std::vector<Range>& ranges) const
  560. {
  561. return Mat(*this, ranges);
  562. }
  563. inline
  564. bool Mat::isContinuous() const
  565. {
  566. return (flags & CONTINUOUS_FLAG) != 0;
  567. }
  568. inline
  569. bool Mat::isSubmatrix() const
  570. {
  571. return (flags & SUBMATRIX_FLAG) != 0;
  572. }
  573. inline
  574. size_t Mat::elemSize() const
  575. {
  576. size_t res = dims > 0 ? step.p[dims - 1] : 0;
  577. CV_DbgAssert(res != 0);
  578. return res;
  579. }
  580. inline
  581. size_t Mat::elemSize1() const
  582. {
  583. return CV_ELEM_SIZE1(flags);
  584. }
  585. inline
  586. int Mat::type() const
  587. {
  588. return CV_MAT_TYPE(flags);
  589. }
  590. inline
  591. int Mat::depth() const
  592. {
  593. return CV_MAT_DEPTH(flags);
  594. }
  595. inline
  596. int Mat::channels() const
  597. {
  598. return CV_MAT_CN(flags);
  599. }
  600. inline
  601. uchar* Mat::ptr(int y)
  602. {
  603. CV_DbgAssert( y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0]) );
  604. return data + step.p[0] * y;
  605. }
  606. inline
  607. const uchar* Mat::ptr(int y) const
  608. {
  609. CV_DbgAssert( y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0]) );
  610. return data + step.p[0] * y;
  611. }
  612. template<typename _Tp> inline
  613. _Tp* Mat::ptr(int y)
  614. {
  615. CV_DbgAssert( y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0]) );
  616. return (_Tp*)(data + step.p[0] * y);
  617. }
  618. template<typename _Tp> inline
  619. const _Tp* Mat::ptr(int y) const
  620. {
  621. CV_DbgAssert( y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0]) );
  622. return (const _Tp*)(data + step.p[0] * y);
  623. }
  624. inline
  625. uchar* Mat::ptr(int i0, int i1)
  626. {
  627. CV_DbgAssert(dims >= 2);
  628. CV_DbgAssert(data);
  629. CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
  630. CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
  631. return data + i0 * step.p[0] + i1 * step.p[1];
  632. }
  633. inline
  634. const uchar* Mat::ptr(int i0, int i1) const
  635. {
  636. CV_DbgAssert(dims >= 2);
  637. CV_DbgAssert(data);
  638. CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
  639. CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
  640. return data + i0 * step.p[0] + i1 * step.p[1];
  641. }
  642. template<typename _Tp> inline
  643. _Tp* Mat::ptr(int i0, int i1)
  644. {
  645. CV_DbgAssert(dims >= 2);
  646. CV_DbgAssert(data);
  647. CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
  648. CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
  649. return (_Tp*)(data + i0 * step.p[0] + i1 * step.p[1]);
  650. }
  651. template<typename _Tp> inline
  652. const _Tp* Mat::ptr(int i0, int i1) const
  653. {
  654. CV_DbgAssert(dims >= 2);
  655. CV_DbgAssert(data);
  656. CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
  657. CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
  658. return (const _Tp*)(data + i0 * step.p[0] + i1 * step.p[1]);
  659. }
  660. inline
  661. uchar* Mat::ptr(int i0, int i1, int i2)
  662. {
  663. CV_DbgAssert(dims >= 3);
  664. CV_DbgAssert(data);
  665. CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
  666. CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
  667. CV_DbgAssert((unsigned)i2 < (unsigned)size.p[2]);
  668. return data + i0 * step.p[0] + i1 * step.p[1] + i2 * step.p[2];
  669. }
  670. inline
  671. const uchar* Mat::ptr(int i0, int i1, int i2) const
  672. {
  673. CV_DbgAssert(dims >= 3);
  674. CV_DbgAssert(data);
  675. CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
  676. CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
  677. CV_DbgAssert((unsigned)i2 < (unsigned)size.p[2]);
  678. return data + i0 * step.p[0] + i1 * step.p[1] + i2 * step.p[2];
  679. }
  680. template<typename _Tp> inline
  681. _Tp* Mat::ptr(int i0, int i1, int i2)
  682. {
  683. CV_DbgAssert(dims >= 3);
  684. CV_DbgAssert(data);
  685. CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
  686. CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
  687. CV_DbgAssert((unsigned)i2 < (unsigned)size.p[2]);
  688. return (_Tp*)(data + i0 * step.p[0] + i1 * step.p[1] + i2 * step.p[2]);
  689. }
  690. template<typename _Tp> inline
  691. const _Tp* Mat::ptr(int i0, int i1, int i2) const
  692. {
  693. CV_DbgAssert(dims >= 3);
  694. CV_DbgAssert(data);
  695. CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
  696. CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
  697. CV_DbgAssert((unsigned)i2 < (unsigned)size.p[2]);
  698. return (const _Tp*)(data + i0 * step.p[0] + i1 * step.p[1] + i2 * step.p[2]);
  699. }
  700. inline
  701. uchar* Mat::ptr(const int* idx)
  702. {
  703. int i, d = dims;
  704. uchar* p = data;
  705. CV_DbgAssert( d >= 1 && p );
  706. for( i = 0; i < d; i++ )
  707. {
  708. CV_DbgAssert( (unsigned)idx[i] < (unsigned)size.p[i] );
  709. p += idx[i] * step.p[i];
  710. }
  711. return p;
  712. }
  713. inline
  714. const uchar* Mat::ptr(const int* idx) const
  715. {
  716. int i, d = dims;
  717. uchar* p = data;
  718. CV_DbgAssert( d >= 1 && p );
  719. for( i = 0; i < d; i++ )
  720. {
  721. CV_DbgAssert( (unsigned)idx[i] < (unsigned)size.p[i] );
  722. p += idx[i] * step.p[i];
  723. }
  724. return p;
  725. }
  726. template<typename _Tp> inline
  727. _Tp* Mat::ptr(const int* idx)
  728. {
  729. int i, d = dims;
  730. uchar* p = data;
  731. CV_DbgAssert( d >= 1 && p );
  732. for( i = 0; i < d; i++ )
  733. {
  734. CV_DbgAssert( (unsigned)idx[i] < (unsigned)size.p[i] );
  735. p += idx[i] * step.p[i];
  736. }
  737. return (_Tp*)p;
  738. }
  739. template<typename _Tp> inline
  740. const _Tp* Mat::ptr(const int* idx) const
  741. {
  742. int i, d = dims;
  743. uchar* p = data;
  744. CV_DbgAssert( d >= 1 && p );
  745. for( i = 0; i < d; i++ )
  746. {
  747. CV_DbgAssert( (unsigned)idx[i] < (unsigned)size.p[i] );
  748. p += idx[i] * step.p[i];
  749. }
  750. return (const _Tp*)p;
  751. }
  752. template<int n> inline
  753. uchar* Mat::ptr(const Vec<int, n>& idx)
  754. {
  755. return Mat::ptr(idx.val);
  756. }
  757. template<int n> inline
  758. const uchar* Mat::ptr(const Vec<int, n>& idx) const
  759. {
  760. return Mat::ptr(idx.val);
  761. }
  762. template<typename _Tp, int n> inline
  763. _Tp* Mat::ptr(const Vec<int, n>& idx)
  764. {
  765. CV_DbgAssert( elemSize() == sizeof(_Tp) );
  766. return Mat::ptr<_Tp>(idx.val);
  767. }
  768. template<typename _Tp, int n> inline
  769. const _Tp* Mat::ptr(const Vec<int, n>& idx) const
  770. {
  771. CV_DbgAssert( elemSize() == sizeof(_Tp) );
  772. return Mat::ptr<_Tp>(idx.val);
  773. }
  774. template<typename _Tp> inline
  775. _Tp& Mat::at(int i0, int i1)
  776. {
  777. CV_DbgAssert(dims <= 2);
  778. CV_DbgAssert(data);
  779. CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
  780. CV_DbgAssert((unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels()));
  781. CV_DbgAssert(CV_ELEM_SIZE1(traits::Depth<_Tp>::value) == elemSize1());
  782. return ((_Tp*)(data + step.p[0] * i0))[i1];
  783. }
  784. template<typename _Tp> inline
  785. const _Tp& Mat::at(int i0, int i1) const
  786. {
  787. CV_DbgAssert(dims <= 2);
  788. CV_DbgAssert(data);
  789. CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
  790. CV_DbgAssert((unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels()));
  791. CV_DbgAssert(CV_ELEM_SIZE1(traits::Depth<_Tp>::value) == elemSize1());
  792. return ((const _Tp*)(data + step.p[0] * i0))[i1];
  793. }
  794. template<typename _Tp> inline
  795. _Tp& Mat::at(Point pt)
  796. {
  797. CV_DbgAssert(dims <= 2);
  798. CV_DbgAssert(data);
  799. CV_DbgAssert((unsigned)pt.y < (unsigned)size.p[0]);
  800. CV_DbgAssert((unsigned)(pt.x * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels()));
  801. CV_DbgAssert(CV_ELEM_SIZE1(traits::Depth<_Tp>::value) == elemSize1());
  802. return ((_Tp*)(data + step.p[0] * pt.y))[pt.x];
  803. }
  804. template<typename _Tp> inline
  805. const _Tp& Mat::at(Point pt) const
  806. {
  807. CV_DbgAssert(dims <= 2);
  808. CV_DbgAssert(data);
  809. CV_DbgAssert((unsigned)pt.y < (unsigned)size.p[0]);
  810. CV_DbgAssert((unsigned)(pt.x * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels()));
  811. CV_DbgAssert(CV_ELEM_SIZE1(traits::Depth<_Tp>::value) == elemSize1());
  812. return ((const _Tp*)(data + step.p[0] * pt.y))[pt.x];
  813. }
  814. template<typename _Tp> inline
  815. _Tp& Mat::at(int i0)
  816. {
  817. CV_DbgAssert(dims <= 2);
  818. CV_DbgAssert(data);
  819. CV_DbgAssert((unsigned)i0 < (unsigned)(size.p[0] * size.p[1]));
  820. CV_DbgAssert(elemSize() == sizeof(_Tp));
  821. if( isContinuous() || size.p[0] == 1 )
  822. return ((_Tp*)data)[i0];
  823. if( size.p[1] == 1 )
  824. return *(_Tp*)(data + step.p[0] * i0);
  825. int i = i0 / cols, j = i0 - i * cols;
  826. return ((_Tp*)(data + step.p[0] * i))[j];
  827. }
  828. template<typename _Tp> inline
  829. const _Tp& Mat::at(int i0) const
  830. {
  831. CV_DbgAssert(dims <= 2);
  832. CV_DbgAssert(data);
  833. CV_DbgAssert((unsigned)i0 < (unsigned)(size.p[0] * size.p[1]));
  834. CV_DbgAssert(elemSize() == sizeof(_Tp));
  835. if( isContinuous() || size.p[0] == 1 )
  836. return ((const _Tp*)data)[i0];
  837. if( size.p[1] == 1 )
  838. return *(const _Tp*)(data + step.p[0] * i0);
  839. int i = i0 / cols, j = i0 - i * cols;
  840. return ((const _Tp*)(data + step.p[0] * i))[j];
  841. }
  842. template<typename _Tp> inline
  843. _Tp& Mat::at(int i0, int i1, int i2)
  844. {
  845. CV_DbgAssert( elemSize() == sizeof(_Tp) );
  846. return *(_Tp*)ptr(i0, i1, i2);
  847. }
  848. template<typename _Tp> inline
  849. const _Tp& Mat::at(int i0, int i1, int i2) const
  850. {
  851. CV_DbgAssert( elemSize() == sizeof(_Tp) );
  852. return *(const _Tp*)ptr(i0, i1, i2);
  853. }
  854. template<typename _Tp> inline
  855. _Tp& Mat::at(const int* idx)
  856. {
  857. CV_DbgAssert( elemSize() == sizeof(_Tp) );
  858. return *(_Tp*)ptr(idx);
  859. }
  860. template<typename _Tp> inline
  861. const _Tp& Mat::at(const int* idx) const
  862. {
  863. CV_DbgAssert( elemSize() == sizeof(_Tp) );
  864. return *(const _Tp*)ptr(idx);
  865. }
  866. template<typename _Tp, int n> inline
  867. _Tp& Mat::at(const Vec<int, n>& idx)
  868. {
  869. CV_DbgAssert( elemSize() == sizeof(_Tp) );
  870. return *(_Tp*)ptr(idx.val);
  871. }
  872. template<typename _Tp, int n> inline
  873. const _Tp& Mat::at(const Vec<int, n>& idx) const
  874. {
  875. CV_DbgAssert( elemSize() == sizeof(_Tp) );
  876. return *(const _Tp*)ptr(idx.val);
  877. }
  878. template<typename _Tp> inline
  879. MatConstIterator_<_Tp> Mat::begin() const
  880. {
  881. if (empty())
  882. return MatConstIterator_<_Tp>();
  883. CV_DbgAssert( elemSize() == sizeof(_Tp) );
  884. return MatConstIterator_<_Tp>((const Mat_<_Tp>*)this);
  885. }
  886. template<typename _Tp> inline
  887. MatConstIterator_<_Tp> Mat::end() const
  888. {
  889. if (empty())
  890. return MatConstIterator_<_Tp>();
  891. CV_DbgAssert( elemSize() == sizeof(_Tp) );
  892. MatConstIterator_<_Tp> it((const Mat_<_Tp>*)this);
  893. it += total();
  894. return it;
  895. }
  896. template<typename _Tp> inline
  897. MatIterator_<_Tp> Mat::begin()
  898. {
  899. if (empty())
  900. return MatIterator_<_Tp>();
  901. CV_DbgAssert( elemSize() == sizeof(_Tp) );
  902. return MatIterator_<_Tp>((Mat_<_Tp>*)this);
  903. }
  904. template<typename _Tp> inline
  905. MatIterator_<_Tp> Mat::end()
  906. {
  907. if (empty())
  908. return MatIterator_<_Tp>();
  909. CV_DbgAssert( elemSize() == sizeof(_Tp) );
  910. MatIterator_<_Tp> it((Mat_<_Tp>*)this);
  911. it += total();
  912. return it;
  913. }
  914. template<typename _Tp, typename Functor> inline
  915. void Mat::forEach(const Functor& operation) {
  916. this->forEach_impl<_Tp>(operation);
  917. }
  918. template<typename _Tp, typename Functor> inline
  919. void Mat::forEach(const Functor& operation) const {
  920. // call as not const
  921. (const_cast<Mat*>(this))->forEach<_Tp>(operation);
  922. }
  923. template<typename _Tp> inline
  924. Mat::operator std::vector<_Tp>() const
  925. {
  926. std::vector<_Tp> v;
  927. copyTo(v);
  928. return v;
  929. }
  930. #ifdef CV_CXX_STD_ARRAY
  931. template<typename _Tp, std::size_t _Nm> inline
  932. Mat::operator std::array<_Tp, _Nm>() const
  933. {
  934. std::array<_Tp, _Nm> v;
  935. copyTo(v);
  936. return v;
  937. }
  938. #endif
  939. template<typename _Tp, int n> inline
  940. Mat::operator Vec<_Tp, n>() const
  941. {
  942. CV_Assert( data && dims <= 2 && (rows == 1 || cols == 1) &&
  943. rows + cols - 1 == n && channels() == 1 );
  944. if( isContinuous() && type() == traits::Type<_Tp>::value )
  945. return Vec<_Tp, n>((_Tp*)data);
  946. Vec<_Tp, n> v;
  947. Mat tmp(rows, cols, traits::Type<_Tp>::value, v.val);
  948. convertTo(tmp, tmp.type());
  949. return v;
  950. }
  951. template<typename _Tp, int m, int n> inline
  952. Mat::operator Matx<_Tp, m, n>() const
  953. {
  954. CV_Assert( data && dims <= 2 && rows == m && cols == n && channels() == 1 );
  955. if( isContinuous() && type() == traits::Type<_Tp>::value )
  956. return Matx<_Tp, m, n>((_Tp*)data);
  957. Matx<_Tp, m, n> mtx;
  958. Mat tmp(rows, cols, traits::Type<_Tp>::value, mtx.val);
  959. convertTo(tmp, tmp.type());
  960. return mtx;
  961. }
  962. template<typename _Tp> inline
  963. void Mat::push_back(const _Tp& elem)
  964. {
  965. if( !data )
  966. {
  967. *this = Mat(1, 1, traits::Type<_Tp>::value, (void*)&elem).clone();
  968. return;
  969. }
  970. CV_Assert(traits::Type<_Tp>::value == type() && cols == 1
  971. /* && dims == 2 (cols == 1 implies dims == 2) */);
  972. const uchar* tmp = dataend + step[0];
  973. if( !isSubmatrix() && isContinuous() && tmp <= datalimit )
  974. {
  975. *(_Tp*)(data + (size.p[0]++) * step.p[0]) = elem;
  976. dataend = tmp;
  977. }
  978. else
  979. push_back_(&elem);
  980. }
  981. template<typename _Tp> inline
  982. void Mat::push_back(const Mat_<_Tp>& m)
  983. {
  984. push_back((const Mat&)m);
  985. }
  986. template<> inline
  987. void Mat::push_back(const MatExpr& expr)
  988. {
  989. push_back(static_cast<Mat>(expr));
  990. }
  991. template<typename _Tp> inline
  992. void Mat::push_back(const std::vector<_Tp>& v)
  993. {
  994. push_back(Mat(v));
  995. }
  996. #ifdef CV_CXX_MOVE_SEMANTICS
  997. inline
  998. Mat::Mat(Mat&& m)
  999. : flags(m.flags), dims(m.dims), rows(m.rows), cols(m.cols), data(m.data),
  1000. datastart(m.datastart), dataend(m.dataend), datalimit(m.datalimit), allocator(m.allocator),
  1001. u(m.u), size(&rows)
  1002. {
  1003. if (m.dims <= 2) // move new step/size info
  1004. {
  1005. step[0] = m.step[0];
  1006. step[1] = m.step[1];
  1007. }
  1008. else
  1009. {
  1010. CV_DbgAssert(m.step.p != m.step.buf);
  1011. step.p = m.step.p;
  1012. size.p = m.size.p;
  1013. m.step.p = m.step.buf;
  1014. m.size.p = &m.rows;
  1015. }
  1016. m.flags = MAGIC_VAL; m.dims = m.rows = m.cols = 0;
  1017. m.data = NULL; m.datastart = NULL; m.dataend = NULL; m.datalimit = NULL;
  1018. m.allocator = NULL;
  1019. m.u = NULL;
  1020. }
  1021. inline
  1022. Mat& Mat::operator = (Mat&& m)
  1023. {
  1024. if (this == &m)
  1025. return *this;
  1026. release();
  1027. flags = m.flags; dims = m.dims; rows = m.rows; cols = m.cols; data = m.data;
  1028. datastart = m.datastart; dataend = m.dataend; datalimit = m.datalimit; allocator = m.allocator;
  1029. u = m.u;
  1030. if (step.p != step.buf) // release self step/size
  1031. {
  1032. fastFree(step.p);
  1033. step.p = step.buf;
  1034. size.p = &rows;
  1035. }
  1036. if (m.dims <= 2) // move new step/size info
  1037. {
  1038. step[0] = m.step[0];
  1039. step[1] = m.step[1];
  1040. }
  1041. else
  1042. {
  1043. CV_DbgAssert(m.step.p != m.step.buf);
  1044. step.p = m.step.p;
  1045. size.p = m.size.p;
  1046. m.step.p = m.step.buf;
  1047. m.size.p = &m.rows;
  1048. }
  1049. m.flags = MAGIC_VAL; m.dims = m.rows = m.cols = 0;
  1050. m.data = NULL; m.datastart = NULL; m.dataend = NULL; m.datalimit = NULL;
  1051. m.allocator = NULL;
  1052. m.u = NULL;
  1053. return *this;
  1054. }
  1055. #endif
  1056. ///////////////////////////// MatSize ////////////////////////////
  1057. inline
  1058. MatSize::MatSize(int* _p) CV_NOEXCEPT
  1059. : p(_p) {}
  1060. inline
  1061. int MatSize::dims() const CV_NOEXCEPT
  1062. {
  1063. return (p - 1)[0];
  1064. }
  1065. inline
  1066. Size MatSize::operator()() const
  1067. {
  1068. CV_DbgAssert(dims() <= 2);
  1069. return Size(p[1], p[0]);
  1070. }
  1071. inline
  1072. const int& MatSize::operator[](int i) const
  1073. {
  1074. CV_DbgAssert(i < dims());
  1075. #ifdef __OPENCV_BUILD
  1076. CV_DbgAssert(i >= 0);
  1077. #endif
  1078. return p[i];
  1079. }
  1080. inline
  1081. int& MatSize::operator[](int i)
  1082. {
  1083. CV_DbgAssert(i < dims());
  1084. #ifdef __OPENCV_BUILD
  1085. CV_DbgAssert(i >= 0);
  1086. #endif
  1087. return p[i];
  1088. }
  1089. inline
  1090. MatSize::operator const int*() const CV_NOEXCEPT
  1091. {
  1092. return p;
  1093. }
  1094. inline
  1095. bool MatSize::operator != (const MatSize& sz) const CV_NOEXCEPT
  1096. {
  1097. return !(*this == sz);
  1098. }
  1099. ///////////////////////////// MatStep ////////////////////////////
  1100. inline
  1101. MatStep::MatStep() CV_NOEXCEPT
  1102. {
  1103. p = buf; p[0] = p[1] = 0;
  1104. }
  1105. inline
  1106. MatStep::MatStep(size_t s) CV_NOEXCEPT
  1107. {
  1108. p = buf; p[0] = s; p[1] = 0;
  1109. }
  1110. inline
  1111. const size_t& MatStep::operator[](int i) const CV_NOEXCEPT
  1112. {
  1113. return p[i];
  1114. }
  1115. inline
  1116. size_t& MatStep::operator[](int i) CV_NOEXCEPT
  1117. {
  1118. return p[i];
  1119. }
  1120. inline MatStep::operator size_t() const
  1121. {
  1122. CV_DbgAssert( p == buf );
  1123. return buf[0];
  1124. }
  1125. inline MatStep& MatStep::operator = (size_t s)
  1126. {
  1127. CV_DbgAssert( p == buf );
  1128. buf[0] = s;
  1129. return *this;
  1130. }
  1131. ////////////////////////////// Mat_<_Tp> ////////////////////////////
  1132. template<typename _Tp> inline
  1133. Mat_<_Tp>::Mat_() CV_NOEXCEPT
  1134. : Mat()
  1135. {
  1136. flags = (flags & ~CV_MAT_TYPE_MASK) | traits::Type<_Tp>::value;
  1137. }
  1138. template<typename _Tp> inline
  1139. Mat_<_Tp>::Mat_(int _rows, int _cols)
  1140. : Mat(_rows, _cols, traits::Type<_Tp>::value)
  1141. {
  1142. }
  1143. template<typename _Tp> inline
  1144. Mat_<_Tp>::Mat_(int _rows, int _cols, const _Tp& value)
  1145. : Mat(_rows, _cols, traits::Type<_Tp>::value)
  1146. {
  1147. *this = value;
  1148. }
  1149. template<typename _Tp> inline
  1150. Mat_<_Tp>::Mat_(Size _sz)
  1151. : Mat(_sz.height, _sz.width, traits::Type<_Tp>::value)
  1152. {}
  1153. template<typename _Tp> inline
  1154. Mat_<_Tp>::Mat_(Size _sz, const _Tp& value)
  1155. : Mat(_sz.height, _sz.width, traits::Type<_Tp>::value)
  1156. {
  1157. *this = value;
  1158. }
  1159. template<typename _Tp> inline
  1160. Mat_<_Tp>::Mat_(int _dims, const int* _sz)
  1161. : Mat(_dims, _sz, traits::Type<_Tp>::value)
  1162. {}
  1163. template<typename _Tp> inline
  1164. Mat_<_Tp>::Mat_(int _dims, const int* _sz, const _Tp& _s)
  1165. : Mat(_dims, _sz, traits::Type<_Tp>::value, Scalar(_s))
  1166. {}
  1167. template<typename _Tp> inline
  1168. Mat_<_Tp>::Mat_(int _dims, const int* _sz, _Tp* _data, const size_t* _steps)
  1169. : Mat(_dims, _sz, traits::Type<_Tp>::value, _data, _steps)
  1170. {}
  1171. template<typename _Tp> inline
  1172. Mat_<_Tp>::Mat_(const Mat_<_Tp>& m, const Range* ranges)
  1173. : Mat(m, ranges)
  1174. {}
  1175. template<typename _Tp> inline
  1176. Mat_<_Tp>::Mat_(const Mat_<_Tp>& m, const std::vector<Range>& ranges)
  1177. : Mat(m, ranges)
  1178. {}
  1179. template<typename _Tp> inline
  1180. Mat_<_Tp>::Mat_(const Mat& m)
  1181. : Mat()
  1182. {
  1183. flags = (flags & ~CV_MAT_TYPE_MASK) | traits::Type<_Tp>::value;
  1184. *this = m;
  1185. }
  1186. template<typename _Tp> inline
  1187. Mat_<_Tp>::Mat_(const Mat_& m)
  1188. : Mat(m)
  1189. {}
  1190. template<typename _Tp> inline
  1191. Mat_<_Tp>::Mat_(int _rows, int _cols, _Tp* _data, size_t steps)
  1192. : Mat(_rows, _cols, traits::Type<_Tp>::value, _data, steps)
  1193. {}
  1194. template<typename _Tp> inline
  1195. Mat_<_Tp>::Mat_(const Mat_& m, const Range& _rowRange, const Range& _colRange)
  1196. : Mat(m, _rowRange, _colRange)
  1197. {}
  1198. template<typename _Tp> inline
  1199. Mat_<_Tp>::Mat_(const Mat_& m, const Rect& roi)
  1200. : Mat(m, roi)
  1201. {}
  1202. template<typename _Tp> template<int n> inline
  1203. Mat_<_Tp>::Mat_(const Vec<typename DataType<_Tp>::channel_type, n>& vec, bool copyData)
  1204. : Mat(n / DataType<_Tp>::channels, 1, traits::Type<_Tp>::value, (void*)&vec)
  1205. {
  1206. CV_Assert(n%DataType<_Tp>::channels == 0);
  1207. if( copyData )
  1208. *this = clone();
  1209. }
  1210. template<typename _Tp> template<int m, int n> inline
  1211. Mat_<_Tp>::Mat_(const Matx<typename DataType<_Tp>::channel_type, m, n>& M, bool copyData)
  1212. : Mat(m, n / DataType<_Tp>::channels, traits::Type<_Tp>::value, (void*)&M)
  1213. {
  1214. CV_Assert(n % DataType<_Tp>::channels == 0);
  1215. if( copyData )
  1216. *this = clone();
  1217. }
  1218. template<typename _Tp> inline
  1219. Mat_<_Tp>::Mat_(const Point_<typename DataType<_Tp>::channel_type>& pt, bool copyData)
  1220. : Mat(2 / DataType<_Tp>::channels, 1, traits::Type<_Tp>::value, (void*)&pt)
  1221. {
  1222. CV_Assert(2 % DataType<_Tp>::channels == 0);
  1223. if( copyData )
  1224. *this = clone();
  1225. }
  1226. template<typename _Tp> inline
  1227. Mat_<_Tp>::Mat_(const Point3_<typename DataType<_Tp>::channel_type>& pt, bool copyData)
  1228. : Mat(3 / DataType<_Tp>::channels, 1, traits::Type<_Tp>::value, (void*)&pt)
  1229. {
  1230. CV_Assert(3 % DataType<_Tp>::channels == 0);
  1231. if( copyData )
  1232. *this = clone();
  1233. }
  1234. template<typename _Tp> inline
  1235. Mat_<_Tp>::Mat_(const MatCommaInitializer_<_Tp>& commaInitializer)
  1236. : Mat(commaInitializer)
  1237. {}
  1238. template<typename _Tp> inline
  1239. Mat_<_Tp>::Mat_(const std::vector<_Tp>& vec, bool copyData)
  1240. : Mat(vec, copyData)
  1241. {}
  1242. #ifdef CV_CXX11
  1243. template<typename _Tp> inline
  1244. Mat_<_Tp>::Mat_(std::initializer_list<_Tp> list)
  1245. : Mat(list)
  1246. {}
  1247. template<typename _Tp> inline
  1248. Mat_<_Tp>::Mat_(const std::initializer_list<int> sizes, std::initializer_list<_Tp> list)
  1249. : Mat(sizes, list)
  1250. {}
  1251. #endif
  1252. #ifdef CV_CXX_STD_ARRAY
  1253. template<typename _Tp> template<std::size_t _Nm> inline
  1254. Mat_<_Tp>::Mat_(const std::array<_Tp, _Nm>& arr, bool copyData)
  1255. : Mat(arr, copyData)
  1256. {}
  1257. #endif
  1258. template<typename _Tp> inline
  1259. Mat_<_Tp>& Mat_<_Tp>::operator = (const Mat& m)
  1260. {
  1261. if (m.empty())
  1262. {
  1263. release();
  1264. return *this;
  1265. }
  1266. if( traits::Type<_Tp>::value == m.type() )
  1267. {
  1268. Mat::operator = (m);
  1269. return *this;
  1270. }
  1271. if( traits::Depth<_Tp>::value == m.depth() )
  1272. {
  1273. return (*this = m.reshape(DataType<_Tp>::channels, m.dims, 0));
  1274. }
  1275. CV_Assert(DataType<_Tp>::channels == m.channels() || m.empty());
  1276. m.convertTo(*this, type());
  1277. return *this;
  1278. }
  1279. template<typename _Tp> inline
  1280. Mat_<_Tp>& Mat_<_Tp>::operator = (const Mat_& m)
  1281. {
  1282. Mat::operator=(m);
  1283. return *this;
  1284. }
  1285. template<typename _Tp> inline
  1286. Mat_<_Tp>& Mat_<_Tp>::operator = (const _Tp& s)
  1287. {
  1288. typedef typename DataType<_Tp>::vec_type VT;
  1289. Mat::operator=(Scalar((const VT&)s));
  1290. return *this;
  1291. }
  1292. template<typename _Tp> inline
  1293. void Mat_<_Tp>::create(int _rows, int _cols)
  1294. {
  1295. Mat::create(_rows, _cols, traits::Type<_Tp>::value);
  1296. }
  1297. template<typename _Tp> inline
  1298. void Mat_<_Tp>::create(Size _sz)
  1299. {
  1300. Mat::create(_sz, traits::Type<_Tp>::value);
  1301. }
  1302. template<typename _Tp> inline
  1303. void Mat_<_Tp>::create(int _dims, const int* _sz)
  1304. {
  1305. Mat::create(_dims, _sz, traits::Type<_Tp>::value);
  1306. }
  1307. template<typename _Tp> inline
  1308. void Mat_<_Tp>::release()
  1309. {
  1310. Mat::release();
  1311. flags = (flags & ~CV_MAT_TYPE_MASK) | traits::Type<_Tp>::value;
  1312. }
  1313. template<typename _Tp> inline
  1314. Mat_<_Tp> Mat_<_Tp>::cross(const Mat_& m) const
  1315. {
  1316. return Mat_<_Tp>(Mat::cross(m));
  1317. }
  1318. template<typename _Tp> template<typename T2> inline
  1319. Mat_<_Tp>::operator Mat_<T2>() const
  1320. {
  1321. return Mat_<T2>(static_cast<const Mat&>(*this));
  1322. }
  1323. template<typename _Tp> inline
  1324. Mat_<_Tp> Mat_<_Tp>::row(int y) const
  1325. {
  1326. return Mat_(*this, Range(y, y+1), Range::all());
  1327. }
  1328. template<typename _Tp> inline
  1329. Mat_<_Tp> Mat_<_Tp>::col(int x) const
  1330. {
  1331. return Mat_(*this, Range::all(), Range(x, x+1));
  1332. }
  1333. template<typename _Tp> inline
  1334. Mat_<_Tp> Mat_<_Tp>::diag(int d) const
  1335. {
  1336. return Mat_(Mat::diag(d));
  1337. }
  1338. template<typename _Tp> inline
  1339. Mat_<_Tp> Mat_<_Tp>::clone() const
  1340. {
  1341. return Mat_(Mat::clone());
  1342. }
  1343. template<typename _Tp> inline
  1344. size_t Mat_<_Tp>::elemSize() const
  1345. {
  1346. CV_DbgAssert( Mat::elemSize() == sizeof(_Tp) );
  1347. return sizeof(_Tp);
  1348. }
  1349. template<typename _Tp> inline
  1350. size_t Mat_<_Tp>::elemSize1() const
  1351. {
  1352. CV_DbgAssert( Mat::elemSize1() == sizeof(_Tp) / DataType<_Tp>::channels );
  1353. return sizeof(_Tp) / DataType<_Tp>::channels;
  1354. }
  1355. template<typename _Tp> inline
  1356. int Mat_<_Tp>::type() const
  1357. {
  1358. CV_DbgAssert( Mat::type() == traits::Type<_Tp>::value );
  1359. return traits::Type<_Tp>::value;
  1360. }
  1361. template<typename _Tp> inline
  1362. int Mat_<_Tp>::depth() const
  1363. {
  1364. CV_DbgAssert( Mat::depth() == traits::Depth<_Tp>::value );
  1365. return traits::Depth<_Tp>::value;
  1366. }
  1367. template<typename _Tp> inline
  1368. int Mat_<_Tp>::channels() const
  1369. {
  1370. CV_DbgAssert( Mat::channels() == DataType<_Tp>::channels );
  1371. return DataType<_Tp>::channels;
  1372. }
  1373. template<typename _Tp> inline
  1374. size_t Mat_<_Tp>::stepT(int i) const
  1375. {
  1376. return step.p[i] / elemSize();
  1377. }
  1378. template<typename _Tp> inline
  1379. size_t Mat_<_Tp>::step1(int i) const
  1380. {
  1381. return step.p[i] / elemSize1();
  1382. }
  1383. template<typename _Tp> inline
  1384. Mat_<_Tp>& Mat_<_Tp>::adjustROI( int dtop, int dbottom, int dleft, int dright )
  1385. {
  1386. return (Mat_<_Tp>&)(Mat::adjustROI(dtop, dbottom, dleft, dright));
  1387. }
  1388. template<typename _Tp> inline
  1389. Mat_<_Tp> Mat_<_Tp>::operator()( const Range& _rowRange, const Range& _colRange ) const
  1390. {
  1391. return Mat_<_Tp>(*this, _rowRange, _colRange);
  1392. }
  1393. template<typename _Tp> inline
  1394. Mat_<_Tp> Mat_<_Tp>::operator()( const Rect& roi ) const
  1395. {
  1396. return Mat_<_Tp>(*this, roi);
  1397. }
  1398. template<typename _Tp> inline
  1399. Mat_<_Tp> Mat_<_Tp>::operator()( const Range* ranges ) const
  1400. {
  1401. return Mat_<_Tp>(*this, ranges);
  1402. }
  1403. template<typename _Tp> inline
  1404. Mat_<_Tp> Mat_<_Tp>::operator()(const std::vector<Range>& ranges) const
  1405. {
  1406. return Mat_<_Tp>(*this, ranges);
  1407. }
  1408. template<typename _Tp> inline
  1409. _Tp* Mat_<_Tp>::operator [](int y)
  1410. {
  1411. CV_DbgAssert( 0 <= y && y < size.p[0] );
  1412. return (_Tp*)(data + y*step.p[0]);
  1413. }
  1414. template<typename _Tp> inline
  1415. const _Tp* Mat_<_Tp>::operator [](int y) const
  1416. {
  1417. CV_DbgAssert( 0 <= y && y < size.p[0] );
  1418. return (const _Tp*)(data + y*step.p[0]);
  1419. }
  1420. template<typename _Tp> inline
  1421. _Tp& Mat_<_Tp>::operator ()(int i0, int i1)
  1422. {
  1423. CV_DbgAssert(dims <= 2);
  1424. CV_DbgAssert(data);
  1425. CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
  1426. CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
  1427. CV_DbgAssert(type() == traits::Type<_Tp>::value);
  1428. return ((_Tp*)(data + step.p[0] * i0))[i1];
  1429. }
  1430. template<typename _Tp> inline
  1431. const _Tp& Mat_<_Tp>::operator ()(int i0, int i1) const
  1432. {
  1433. CV_DbgAssert(dims <= 2);
  1434. CV_DbgAssert(data);
  1435. CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
  1436. CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
  1437. CV_DbgAssert(type() == traits::Type<_Tp>::value);
  1438. return ((const _Tp*)(data + step.p[0] * i0))[i1];
  1439. }
  1440. template<typename _Tp> inline
  1441. _Tp& Mat_<_Tp>::operator ()(Point pt)
  1442. {
  1443. CV_DbgAssert(dims <= 2);
  1444. CV_DbgAssert(data);
  1445. CV_DbgAssert((unsigned)pt.y < (unsigned)size.p[0]);
  1446. CV_DbgAssert((unsigned)pt.x < (unsigned)size.p[1]);
  1447. CV_DbgAssert(type() == traits::Type<_Tp>::value);
  1448. return ((_Tp*)(data + step.p[0] * pt.y))[pt.x];
  1449. }
  1450. template<typename _Tp> inline
  1451. const _Tp& Mat_<_Tp>::operator ()(Point pt) const
  1452. {
  1453. CV_DbgAssert(dims <= 2);
  1454. CV_DbgAssert(data);
  1455. CV_DbgAssert((unsigned)pt.y < (unsigned)size.p[0]);
  1456. CV_DbgAssert((unsigned)pt.x < (unsigned)size.p[1]);
  1457. CV_DbgAssert(type() == traits::Type<_Tp>::value);
  1458. return ((const _Tp*)(data + step.p[0] * pt.y))[pt.x];
  1459. }
  1460. template<typename _Tp> inline
  1461. _Tp& Mat_<_Tp>::operator ()(const int* idx)
  1462. {
  1463. return Mat::at<_Tp>(idx);
  1464. }
  1465. template<typename _Tp> inline
  1466. const _Tp& Mat_<_Tp>::operator ()(const int* idx) const
  1467. {
  1468. return Mat::at<_Tp>(idx);
  1469. }
  1470. template<typename _Tp> template<int n> inline
  1471. _Tp& Mat_<_Tp>::operator ()(const Vec<int, n>& idx)
  1472. {
  1473. return Mat::at<_Tp>(idx);
  1474. }
  1475. template<typename _Tp> template<int n> inline
  1476. const _Tp& Mat_<_Tp>::operator ()(const Vec<int, n>& idx) const
  1477. {
  1478. return Mat::at<_Tp>(idx);
  1479. }
  1480. template<typename _Tp> inline
  1481. _Tp& Mat_<_Tp>::operator ()(int i0)
  1482. {
  1483. return this->at<_Tp>(i0);
  1484. }
  1485. template<typename _Tp> inline
  1486. const _Tp& Mat_<_Tp>::operator ()(int i0) const
  1487. {
  1488. return this->at<_Tp>(i0);
  1489. }
  1490. template<typename _Tp> inline
  1491. _Tp& Mat_<_Tp>::operator ()(int i0, int i1, int i2)
  1492. {
  1493. return this->at<_Tp>(i0, i1, i2);
  1494. }
  1495. template<typename _Tp> inline
  1496. const _Tp& Mat_<_Tp>::operator ()(int i0, int i1, int i2) const
  1497. {
  1498. return this->at<_Tp>(i0, i1, i2);
  1499. }
  1500. template<typename _Tp> inline
  1501. Mat_<_Tp>::operator std::vector<_Tp>() const
  1502. {
  1503. std::vector<_Tp> v;
  1504. copyTo(v);
  1505. return v;
  1506. }
  1507. #ifdef CV_CXX_STD_ARRAY
  1508. template<typename _Tp> template<std::size_t _Nm> inline
  1509. Mat_<_Tp>::operator std::array<_Tp, _Nm>() const
  1510. {
  1511. std::array<_Tp, _Nm> a;
  1512. copyTo(a);
  1513. return a;
  1514. }
  1515. #endif
  1516. template<typename _Tp> template<int n> inline
  1517. Mat_<_Tp>::operator Vec<typename DataType<_Tp>::channel_type, n>() const
  1518. {
  1519. CV_Assert(n % DataType<_Tp>::channels == 0);
  1520. #if defined _MSC_VER
  1521. const Mat* pMat = (const Mat*)this; // workaround for MSVS <= 2012 compiler bugs (but GCC 4.6 dislikes this workaround)
  1522. return pMat->operator Vec<typename DataType<_Tp>::channel_type, n>();
  1523. #else
  1524. return this->Mat::operator Vec<typename DataType<_Tp>::channel_type, n>();
  1525. #endif
  1526. }
  1527. template<typename _Tp> template<int m, int n> inline
  1528. Mat_<_Tp>::operator Matx<typename DataType<_Tp>::channel_type, m, n>() const
  1529. {
  1530. CV_Assert(n % DataType<_Tp>::channels == 0);
  1531. #if defined _MSC_VER
  1532. const Mat* pMat = (const Mat*)this; // workaround for MSVS <= 2012 compiler bugs (but GCC 4.6 dislikes this workaround)
  1533. Matx<typename DataType<_Tp>::channel_type, m, n> res = pMat->operator Matx<typename DataType<_Tp>::channel_type, m, n>();
  1534. return res;
  1535. #else
  1536. Matx<typename DataType<_Tp>::channel_type, m, n> res = this->Mat::operator Matx<typename DataType<_Tp>::channel_type, m, n>();
  1537. return res;
  1538. #endif
  1539. }
  1540. template<typename _Tp> inline
  1541. MatConstIterator_<_Tp> Mat_<_Tp>::begin() const
  1542. {
  1543. return Mat::begin<_Tp>();
  1544. }
  1545. template<typename _Tp> inline
  1546. MatConstIterator_<_Tp> Mat_<_Tp>::end() const
  1547. {
  1548. return Mat::end<_Tp>();
  1549. }
  1550. template<typename _Tp> inline
  1551. MatIterator_<_Tp> Mat_<_Tp>::begin()
  1552. {
  1553. return Mat::begin<_Tp>();
  1554. }
  1555. template<typename _Tp> inline
  1556. MatIterator_<_Tp> Mat_<_Tp>::end()
  1557. {
  1558. return Mat::end<_Tp>();
  1559. }
  1560. template<typename _Tp> template<typename Functor> inline
  1561. void Mat_<_Tp>::forEach(const Functor& operation) {
  1562. Mat::forEach<_Tp, Functor>(operation);
  1563. }
  1564. template<typename _Tp> template<typename Functor> inline
  1565. void Mat_<_Tp>::forEach(const Functor& operation) const {
  1566. Mat::forEach<_Tp, Functor>(operation);
  1567. }
  1568. #ifdef CV_CXX_MOVE_SEMANTICS
  1569. template<typename _Tp> inline
  1570. Mat_<_Tp>::Mat_(Mat_&& m)
  1571. : Mat(std::move(m))
  1572. {
  1573. }
  1574. template<typename _Tp> inline
  1575. Mat_<_Tp>& Mat_<_Tp>::operator = (Mat_&& m)
  1576. {
  1577. Mat::operator = (std::move(m));
  1578. return *this;
  1579. }
  1580. template<typename _Tp> inline
  1581. Mat_<_Tp>::Mat_(Mat&& m)
  1582. : Mat()
  1583. {
  1584. flags = (flags & ~CV_MAT_TYPE_MASK) | traits::Type<_Tp>::value;
  1585. *this = std::move(m);
  1586. }
  1587. template<typename _Tp> inline
  1588. Mat_<_Tp>& Mat_<_Tp>::operator = (Mat&& m)
  1589. {
  1590. if (m.empty())
  1591. {
  1592. release();
  1593. return *this;
  1594. }
  1595. if( traits::Type<_Tp>::value == m.type() )
  1596. {
  1597. Mat::operator = ((Mat&&)m);
  1598. return *this;
  1599. }
  1600. if( traits::Depth<_Tp>::value == m.depth() )
  1601. {
  1602. Mat::operator = ((Mat&&)m.reshape(DataType<_Tp>::channels, m.dims, 0));
  1603. return *this;
  1604. }
  1605. CV_DbgAssert(DataType<_Tp>::channels == m.channels());
  1606. m.convertTo(*this, type());
  1607. return *this;
  1608. }
  1609. template<typename _Tp> inline
  1610. Mat_<_Tp>::Mat_(MatExpr&& e)
  1611. : Mat()
  1612. {
  1613. flags = (flags & ~CV_MAT_TYPE_MASK) | traits::Type<_Tp>::value;
  1614. *this = Mat(e);
  1615. }
  1616. #endif
  1617. ///////////////////////////// SparseMat /////////////////////////////
  1618. inline
  1619. SparseMat SparseMat::clone() const
  1620. {
  1621. SparseMat temp;
  1622. this->copyTo(temp);
  1623. return temp;
  1624. }
  1625. inline
  1626. size_t SparseMat::elemSize() const
  1627. {
  1628. return CV_ELEM_SIZE(flags);
  1629. }
  1630. inline
  1631. size_t SparseMat::elemSize1() const
  1632. {
  1633. return CV_ELEM_SIZE1(flags);
  1634. }
  1635. inline
  1636. int SparseMat::type() const
  1637. {
  1638. return CV_MAT_TYPE(flags);
  1639. }
  1640. inline
  1641. int SparseMat::depth() const
  1642. {
  1643. return CV_MAT_DEPTH(flags);
  1644. }
  1645. inline
  1646. int SparseMat::channels() const
  1647. {
  1648. return CV_MAT_CN(flags);
  1649. }
  1650. inline
  1651. const int* SparseMat::size() const
  1652. {
  1653. return hdr ? hdr->size : 0;
  1654. }
  1655. inline
  1656. int SparseMat::size(int i) const
  1657. {
  1658. if( hdr )
  1659. {
  1660. CV_DbgAssert((unsigned)i < (unsigned)hdr->dims);
  1661. return hdr->size[i];
  1662. }
  1663. return 0;
  1664. }
  1665. inline
  1666. int SparseMat::dims() const
  1667. {
  1668. return hdr ? hdr->dims : 0;
  1669. }
  1670. inline
  1671. size_t SparseMat::nzcount() const
  1672. {
  1673. return hdr ? hdr->nodeCount : 0;
  1674. }
  1675. template<typename _Tp> inline
  1676. _Tp& SparseMat::ref(int i0, size_t* hashval)
  1677. {
  1678. return *(_Tp*)((SparseMat*)this)->ptr(i0, true, hashval);
  1679. }
  1680. template<typename _Tp> inline
  1681. _Tp& SparseMat::ref(int i0, int i1, size_t* hashval)
  1682. {
  1683. return *(_Tp*)((SparseMat*)this)->ptr(i0, i1, true, hashval);
  1684. }
  1685. template<typename _Tp> inline
  1686. _Tp& SparseMat::ref(int i0, int i1, int i2, size_t* hashval)
  1687. {
  1688. return *(_Tp*)((SparseMat*)this)->ptr(i0, i1, i2, true, hashval);
  1689. }
  1690. template<typename _Tp> inline
  1691. _Tp& SparseMat::ref(const int* idx, size_t* hashval)
  1692. {
  1693. return *(_Tp*)((SparseMat*)this)->ptr(idx, true, hashval);
  1694. }
  1695. template<typename _Tp> inline
  1696. _Tp SparseMat::value(int i0, size_t* hashval) const
  1697. {
  1698. const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(i0, false, hashval);
  1699. return p ? *p : _Tp();
  1700. }
  1701. template<typename _Tp> inline
  1702. _Tp SparseMat::value(int i0, int i1, size_t* hashval) const
  1703. {
  1704. const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(i0, i1, false, hashval);
  1705. return p ? *p : _Tp();
  1706. }
  1707. template<typename _Tp> inline
  1708. _Tp SparseMat::value(int i0, int i1, int i2, size_t* hashval) const
  1709. {
  1710. const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(i0, i1, i2, false, hashval);
  1711. return p ? *p : _Tp();
  1712. }
  1713. template<typename _Tp> inline
  1714. _Tp SparseMat::value(const int* idx, size_t* hashval) const
  1715. {
  1716. const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(idx, false, hashval);
  1717. return p ? *p : _Tp();
  1718. }
  1719. template<typename _Tp> inline
  1720. const _Tp* SparseMat::find(int i0, size_t* hashval) const
  1721. {
  1722. return (const _Tp*)((SparseMat*)this)->ptr(i0, false, hashval);
  1723. }
  1724. template<typename _Tp> inline
  1725. const _Tp* SparseMat::find(int i0, int i1, size_t* hashval) const
  1726. {
  1727. return (const _Tp*)((SparseMat*)this)->ptr(i0, i1, false, hashval);
  1728. }
  1729. template<typename _Tp> inline
  1730. const _Tp* SparseMat::find(int i0, int i1, int i2, size_t* hashval) const
  1731. {
  1732. return (const _Tp*)((SparseMat*)this)->ptr(i0, i1, i2, false, hashval);
  1733. }
  1734. template<typename _Tp> inline
  1735. const _Tp* SparseMat::find(const int* idx, size_t* hashval) const
  1736. {
  1737. return (const _Tp*)((SparseMat*)this)->ptr(idx, false, hashval);
  1738. }
  1739. template<typename _Tp> inline
  1740. _Tp& SparseMat::value(Node* n)
  1741. {
  1742. return *(_Tp*)((uchar*)n + hdr->valueOffset);
  1743. }
  1744. template<typename _Tp> inline
  1745. const _Tp& SparseMat::value(const Node* n) const
  1746. {
  1747. return *(const _Tp*)((const uchar*)n + hdr->valueOffset);
  1748. }
  1749. inline
  1750. SparseMat::Node* SparseMat::node(size_t nidx)
  1751. {
  1752. return (Node*)(void*)&hdr->pool[nidx];
  1753. }
  1754. inline
  1755. const SparseMat::Node* SparseMat::node(size_t nidx) const
  1756. {
  1757. return (const Node*)(const void*)&hdr->pool[nidx];
  1758. }
  1759. inline
  1760. SparseMatIterator SparseMat::begin()
  1761. {
  1762. return SparseMatIterator(this);
  1763. }
  1764. inline
  1765. SparseMatConstIterator SparseMat::begin() const
  1766. {
  1767. return SparseMatConstIterator(this);
  1768. }
  1769. inline
  1770. SparseMatIterator SparseMat::end()
  1771. {
  1772. SparseMatIterator it(this);
  1773. it.seekEnd();
  1774. return it;
  1775. }
  1776. inline
  1777. SparseMatConstIterator SparseMat::end() const
  1778. {
  1779. SparseMatConstIterator it(this);
  1780. it.seekEnd();
  1781. return it;
  1782. }
  1783. template<typename _Tp> inline
  1784. SparseMatIterator_<_Tp> SparseMat::begin()
  1785. {
  1786. return SparseMatIterator_<_Tp>(this);
  1787. }
  1788. template<typename _Tp> inline
  1789. SparseMatConstIterator_<_Tp> SparseMat::begin() const
  1790. {
  1791. return SparseMatConstIterator_<_Tp>(this);
  1792. }
  1793. template<typename _Tp> inline
  1794. SparseMatIterator_<_Tp> SparseMat::end()
  1795. {
  1796. SparseMatIterator_<_Tp> it(this);
  1797. it.seekEnd();
  1798. return it;
  1799. }
  1800. template<typename _Tp> inline
  1801. SparseMatConstIterator_<_Tp> SparseMat::end() const
  1802. {
  1803. SparseMatConstIterator_<_Tp> it(this);
  1804. it.seekEnd();
  1805. return it;
  1806. }
  1807. ///////////////////////////// SparseMat_ ////////////////////////////
  1808. template<typename _Tp> inline
  1809. SparseMat_<_Tp>::SparseMat_()
  1810. {
  1811. flags = MAGIC_VAL | traits::Type<_Tp>::value;
  1812. }
  1813. template<typename _Tp> inline
  1814. SparseMat_<_Tp>::SparseMat_(int _dims, const int* _sizes)
  1815. : SparseMat(_dims, _sizes, traits::Type<_Tp>::value)
  1816. {}
  1817. template<typename _Tp> inline
  1818. SparseMat_<_Tp>::SparseMat_(const SparseMat& m)
  1819. {
  1820. if( m.type() == traits::Type<_Tp>::value )
  1821. *this = (const SparseMat_<_Tp>&)m;
  1822. else
  1823. m.convertTo(*this, traits::Type<_Tp>::value);
  1824. }
  1825. template<typename _Tp> inline
  1826. SparseMat_<_Tp>::SparseMat_(const SparseMat_<_Tp>& m)
  1827. {
  1828. this->flags = m.flags;
  1829. this->hdr = m.hdr;
  1830. if( this->hdr )
  1831. CV_XADD(&this->hdr->refcount, 1);
  1832. }
  1833. template<typename _Tp> inline
  1834. SparseMat_<_Tp>::SparseMat_(const Mat& m)
  1835. {
  1836. SparseMat sm(m);
  1837. *this = sm;
  1838. }
  1839. template<typename _Tp> inline
  1840. SparseMat_<_Tp>& SparseMat_<_Tp>::operator = (const SparseMat_<_Tp>& m)
  1841. {
  1842. if( this != &m )
  1843. {
  1844. if( m.hdr ) CV_XADD(&m.hdr->refcount, 1);
  1845. release();
  1846. flags = m.flags;
  1847. hdr = m.hdr;
  1848. }
  1849. return *this;
  1850. }
  1851. template<typename _Tp> inline
  1852. SparseMat_<_Tp>& SparseMat_<_Tp>::operator = (const SparseMat& m)
  1853. {
  1854. if( m.type() == traits::Type<_Tp>::value )
  1855. return (*this = (const SparseMat_<_Tp>&)m);
  1856. m.convertTo(*this, traits::Type<_Tp>::value);
  1857. return *this;
  1858. }
  1859. template<typename _Tp> inline
  1860. SparseMat_<_Tp>& SparseMat_<_Tp>::operator = (const Mat& m)
  1861. {
  1862. return (*this = SparseMat(m));
  1863. }
  1864. template<typename _Tp> inline
  1865. SparseMat_<_Tp> SparseMat_<_Tp>::clone() const
  1866. {
  1867. SparseMat_<_Tp> m;
  1868. this->copyTo(m);
  1869. return m;
  1870. }
  1871. template<typename _Tp> inline
  1872. void SparseMat_<_Tp>::create(int _dims, const int* _sizes)
  1873. {
  1874. SparseMat::create(_dims, _sizes, traits::Type<_Tp>::value);
  1875. }
  1876. template<typename _Tp> inline
  1877. int SparseMat_<_Tp>::type() const
  1878. {
  1879. return traits::Type<_Tp>::value;
  1880. }
  1881. template<typename _Tp> inline
  1882. int SparseMat_<_Tp>::depth() const
  1883. {
  1884. return traits::Depth<_Tp>::value;
  1885. }
  1886. template<typename _Tp> inline
  1887. int SparseMat_<_Tp>::channels() const
  1888. {
  1889. return DataType<_Tp>::channels;
  1890. }
  1891. template<typename _Tp> inline
  1892. _Tp& SparseMat_<_Tp>::ref(int i0, size_t* hashval)
  1893. {
  1894. return SparseMat::ref<_Tp>(i0, hashval);
  1895. }
  1896. template<typename _Tp> inline
  1897. _Tp SparseMat_<_Tp>::operator()(int i0, size_t* hashval) const
  1898. {
  1899. return SparseMat::value<_Tp>(i0, hashval);
  1900. }
  1901. template<typename _Tp> inline
  1902. _Tp& SparseMat_<_Tp>::ref(int i0, int i1, size_t* hashval)
  1903. {
  1904. return SparseMat::ref<_Tp>(i0, i1, hashval);
  1905. }
  1906. template<typename _Tp> inline
  1907. _Tp SparseMat_<_Tp>::operator()(int i0, int i1, size_t* hashval) const
  1908. {
  1909. return SparseMat::value<_Tp>(i0, i1, hashval);
  1910. }
  1911. template<typename _Tp> inline
  1912. _Tp& SparseMat_<_Tp>::ref(int i0, int i1, int i2, size_t* hashval)
  1913. {
  1914. return SparseMat::ref<_Tp>(i0, i1, i2, hashval);
  1915. }
  1916. template<typename _Tp> inline
  1917. _Tp SparseMat_<_Tp>::operator()(int i0, int i1, int i2, size_t* hashval) const
  1918. {
  1919. return SparseMat::value<_Tp>(i0, i1, i2, hashval);
  1920. }
  1921. template<typename _Tp> inline
  1922. _Tp& SparseMat_<_Tp>::ref(const int* idx, size_t* hashval)
  1923. {
  1924. return SparseMat::ref<_Tp>(idx, hashval);
  1925. }
  1926. template<typename _Tp> inline
  1927. _Tp SparseMat_<_Tp>::operator()(const int* idx, size_t* hashval) const
  1928. {
  1929. return SparseMat::value<_Tp>(idx, hashval);
  1930. }
  1931. template<typename _Tp> inline
  1932. SparseMatIterator_<_Tp> SparseMat_<_Tp>::begin()
  1933. {
  1934. return SparseMatIterator_<_Tp>(this);
  1935. }
  1936. template<typename _Tp> inline
  1937. SparseMatConstIterator_<_Tp> SparseMat_<_Tp>::begin() const
  1938. {
  1939. return SparseMatConstIterator_<_Tp>(this);
  1940. }
  1941. template<typename _Tp> inline
  1942. SparseMatIterator_<_Tp> SparseMat_<_Tp>::end()
  1943. {
  1944. SparseMatIterator_<_Tp> it(this);
  1945. it.seekEnd();
  1946. return it;
  1947. }
  1948. template<typename _Tp> inline
  1949. SparseMatConstIterator_<_Tp> SparseMat_<_Tp>::end() const
  1950. {
  1951. SparseMatConstIterator_<_Tp> it(this);
  1952. it.seekEnd();
  1953. return it;
  1954. }
  1955. ////////////////////////// MatConstIterator /////////////////////////
  1956. inline
  1957. MatConstIterator::MatConstIterator()
  1958. : m(0), elemSize(0), ptr(0), sliceStart(0), sliceEnd(0)
  1959. {}
  1960. inline
  1961. MatConstIterator::MatConstIterator(const Mat* _m)
  1962. : m(_m), elemSize(_m->elemSize()), ptr(0), sliceStart(0), sliceEnd(0)
  1963. {
  1964. if( m && m->isContinuous() )
  1965. {
  1966. CV_Assert(!m->empty());
  1967. sliceStart = m->ptr();
  1968. sliceEnd = sliceStart + m->total()*elemSize;
  1969. }
  1970. seek((const int*)0);
  1971. }
  1972. inline
  1973. MatConstIterator::MatConstIterator(const Mat* _m, int _row, int _col)
  1974. : m(_m), elemSize(_m->elemSize()), ptr(0), sliceStart(0), sliceEnd(0)
  1975. {
  1976. CV_Assert(m && m->dims <= 2);
  1977. if( m->isContinuous() )
  1978. {
  1979. CV_Assert(!m->empty());
  1980. sliceStart = m->ptr();
  1981. sliceEnd = sliceStart + m->total()*elemSize;
  1982. }
  1983. int idx[] = {_row, _col};
  1984. seek(idx);
  1985. }
  1986. inline
  1987. MatConstIterator::MatConstIterator(const Mat* _m, Point _pt)
  1988. : m(_m), elemSize(_m->elemSize()), ptr(0), sliceStart(0), sliceEnd(0)
  1989. {
  1990. CV_Assert(m && m->dims <= 2);
  1991. if( m->isContinuous() )
  1992. {
  1993. CV_Assert(!m->empty());
  1994. sliceStart = m->ptr();
  1995. sliceEnd = sliceStart + m->total()*elemSize;
  1996. }
  1997. int idx[] = {_pt.y, _pt.x};
  1998. seek(idx);
  1999. }
  2000. inline
  2001. MatConstIterator::MatConstIterator(const MatConstIterator& it)
  2002. : m(it.m), elemSize(it.elemSize), ptr(it.ptr), sliceStart(it.sliceStart), sliceEnd(it.sliceEnd)
  2003. {}
  2004. inline
  2005. MatConstIterator& MatConstIterator::operator = (const MatConstIterator& it )
  2006. {
  2007. m = it.m; elemSize = it.elemSize; ptr = it.ptr;
  2008. sliceStart = it.sliceStart; sliceEnd = it.sliceEnd;
  2009. return *this;
  2010. }
  2011. inline
  2012. const uchar* MatConstIterator::operator *() const
  2013. {
  2014. return ptr;
  2015. }
  2016. inline MatConstIterator& MatConstIterator::operator += (ptrdiff_t ofs)
  2017. {
  2018. if( !m || ofs == 0 )
  2019. return *this;
  2020. ptrdiff_t ofsb = ofs*elemSize;
  2021. ptr += ofsb;
  2022. if( ptr < sliceStart || sliceEnd <= ptr )
  2023. {
  2024. ptr -= ofsb;
  2025. seek(ofs, true);
  2026. }
  2027. return *this;
  2028. }
  2029. inline
  2030. MatConstIterator& MatConstIterator::operator -= (ptrdiff_t ofs)
  2031. {
  2032. return (*this += -ofs);
  2033. }
  2034. inline
  2035. MatConstIterator& MatConstIterator::operator --()
  2036. {
  2037. if( m && (ptr -= elemSize) < sliceStart )
  2038. {
  2039. ptr += elemSize;
  2040. seek(-1, true);
  2041. }
  2042. return *this;
  2043. }
  2044. inline
  2045. MatConstIterator MatConstIterator::operator --(int)
  2046. {
  2047. MatConstIterator b = *this;
  2048. *this += -1;
  2049. return b;
  2050. }
  2051. inline
  2052. MatConstIterator& MatConstIterator::operator ++()
  2053. {
  2054. if( m && (ptr += elemSize) >= sliceEnd )
  2055. {
  2056. ptr -= elemSize;
  2057. seek(1, true);
  2058. }
  2059. return *this;
  2060. }
  2061. inline MatConstIterator MatConstIterator::operator ++(int)
  2062. {
  2063. MatConstIterator b = *this;
  2064. *this += 1;
  2065. return b;
  2066. }
  2067. static inline
  2068. bool operator == (const MatConstIterator& a, const MatConstIterator& b)
  2069. {
  2070. return a.m == b.m && a.ptr == b.ptr;
  2071. }
  2072. static inline
  2073. bool operator != (const MatConstIterator& a, const MatConstIterator& b)
  2074. {
  2075. return !(a == b);
  2076. }
  2077. static inline
  2078. bool operator < (const MatConstIterator& a, const MatConstIterator& b)
  2079. {
  2080. return a.ptr < b.ptr;
  2081. }
  2082. static inline
  2083. bool operator > (const MatConstIterator& a, const MatConstIterator& b)
  2084. {
  2085. return a.ptr > b.ptr;
  2086. }
  2087. static inline
  2088. bool operator <= (const MatConstIterator& a, const MatConstIterator& b)
  2089. {
  2090. return a.ptr <= b.ptr;
  2091. }
  2092. static inline
  2093. bool operator >= (const MatConstIterator& a, const MatConstIterator& b)
  2094. {
  2095. return a.ptr >= b.ptr;
  2096. }
  2097. static inline
  2098. ptrdiff_t operator - (const MatConstIterator& b, const MatConstIterator& a)
  2099. {
  2100. if( a.m != b.m )
  2101. return ((size_t)(-1) >> 1);
  2102. if( a.sliceEnd == b.sliceEnd )
  2103. return (b.ptr - a.ptr)/static_cast<ptrdiff_t>(b.elemSize);
  2104. return b.lpos() - a.lpos();
  2105. }
  2106. static inline
  2107. MatConstIterator operator + (const MatConstIterator& a, ptrdiff_t ofs)
  2108. {
  2109. MatConstIterator b = a;
  2110. return b += ofs;
  2111. }
  2112. static inline
  2113. MatConstIterator operator + (ptrdiff_t ofs, const MatConstIterator& a)
  2114. {
  2115. MatConstIterator b = a;
  2116. return b += ofs;
  2117. }
  2118. static inline
  2119. MatConstIterator operator - (const MatConstIterator& a, ptrdiff_t ofs)
  2120. {
  2121. MatConstIterator b = a;
  2122. return b += -ofs;
  2123. }
  2124. inline
  2125. const uchar* MatConstIterator::operator [](ptrdiff_t i) const
  2126. {
  2127. return *(*this + i);
  2128. }
  2129. ///////////////////////// MatConstIterator_ /////////////////////////
  2130. template<typename _Tp> inline
  2131. MatConstIterator_<_Tp>::MatConstIterator_()
  2132. {}
  2133. template<typename _Tp> inline
  2134. MatConstIterator_<_Tp>::MatConstIterator_(const Mat_<_Tp>* _m)
  2135. : MatConstIterator(_m)
  2136. {}
  2137. template<typename _Tp> inline
  2138. MatConstIterator_<_Tp>::MatConstIterator_(const Mat_<_Tp>* _m, int _row, int _col)
  2139. : MatConstIterator(_m, _row, _col)
  2140. {}
  2141. template<typename _Tp> inline
  2142. MatConstIterator_<_Tp>::MatConstIterator_(const Mat_<_Tp>* _m, Point _pt)
  2143. : MatConstIterator(_m, _pt)
  2144. {}
  2145. template<typename _Tp> inline
  2146. MatConstIterator_<_Tp>::MatConstIterator_(const MatConstIterator_& it)
  2147. : MatConstIterator(it)
  2148. {}
  2149. template<typename _Tp> inline
  2150. MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator = (const MatConstIterator_& it )
  2151. {
  2152. MatConstIterator::operator = (it);
  2153. return *this;
  2154. }
  2155. template<typename _Tp> inline
  2156. const _Tp& MatConstIterator_<_Tp>::operator *() const
  2157. {
  2158. return *(_Tp*)(this->ptr);
  2159. }
  2160. template<typename _Tp> inline
  2161. MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator += (ptrdiff_t ofs)
  2162. {
  2163. MatConstIterator::operator += (ofs);
  2164. return *this;
  2165. }
  2166. template<typename _Tp> inline
  2167. MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator -= (ptrdiff_t ofs)
  2168. {
  2169. return (*this += -ofs);
  2170. }
  2171. template<typename _Tp> inline
  2172. MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator --()
  2173. {
  2174. MatConstIterator::operator --();
  2175. return *this;
  2176. }
  2177. template<typename _Tp> inline
  2178. MatConstIterator_<_Tp> MatConstIterator_<_Tp>::operator --(int)
  2179. {
  2180. MatConstIterator_ b = *this;
  2181. MatConstIterator::operator --();
  2182. return b;
  2183. }
  2184. template<typename _Tp> inline
  2185. MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator ++()
  2186. {
  2187. MatConstIterator::operator ++();
  2188. return *this;
  2189. }
  2190. template<typename _Tp> inline
  2191. MatConstIterator_<_Tp> MatConstIterator_<_Tp>::operator ++(int)
  2192. {
  2193. MatConstIterator_ b = *this;
  2194. MatConstIterator::operator ++();
  2195. return b;
  2196. }
  2197. template<typename _Tp> inline
  2198. Point MatConstIterator_<_Tp>::pos() const
  2199. {
  2200. if( !m )
  2201. return Point();
  2202. CV_DbgAssert( m->dims <= 2 );
  2203. if( m->isContinuous() )
  2204. {
  2205. ptrdiff_t ofs = (const _Tp*)ptr - (const _Tp*)m->data;
  2206. int y = (int)(ofs / m->cols);
  2207. int x = (int)(ofs - (ptrdiff_t)y * m->cols);
  2208. return Point(x, y);
  2209. }
  2210. else
  2211. {
  2212. ptrdiff_t ofs = (uchar*)ptr - m->data;
  2213. int y = (int)(ofs / m->step);
  2214. int x = (int)((ofs - y * m->step)/sizeof(_Tp));
  2215. return Point(x, y);
  2216. }
  2217. }
  2218. template<typename _Tp> static inline
  2219. bool operator == (const MatConstIterator_<_Tp>& a, const MatConstIterator_<_Tp>& b)
  2220. {
  2221. return a.m == b.m && a.ptr == b.ptr;
  2222. }
  2223. template<typename _Tp> static inline
  2224. bool operator != (const MatConstIterator_<_Tp>& a, const MatConstIterator_<_Tp>& b)
  2225. {
  2226. return a.m != b.m || a.ptr != b.ptr;
  2227. }
  2228. template<typename _Tp> static inline
  2229. MatConstIterator_<_Tp> operator + (const MatConstIterator_<_Tp>& a, ptrdiff_t ofs)
  2230. {
  2231. MatConstIterator t = (const MatConstIterator&)a + ofs;
  2232. return (MatConstIterator_<_Tp>&)t;
  2233. }
  2234. template<typename _Tp> static inline
  2235. MatConstIterator_<_Tp> operator + (ptrdiff_t ofs, const MatConstIterator_<_Tp>& a)
  2236. {
  2237. MatConstIterator t = (const MatConstIterator&)a + ofs;
  2238. return (MatConstIterator_<_Tp>&)t;
  2239. }
  2240. template<typename _Tp> static inline
  2241. MatConstIterator_<_Tp> operator - (const MatConstIterator_<_Tp>& a, ptrdiff_t ofs)
  2242. {
  2243. MatConstIterator t = (const MatConstIterator&)a - ofs;
  2244. return (MatConstIterator_<_Tp>&)t;
  2245. }
  2246. template<typename _Tp> inline
  2247. const _Tp& MatConstIterator_<_Tp>::operator [](ptrdiff_t i) const
  2248. {
  2249. return *(_Tp*)MatConstIterator::operator [](i);
  2250. }
  2251. //////////////////////////// MatIterator_ ///////////////////////////
  2252. template<typename _Tp> inline
  2253. MatIterator_<_Tp>::MatIterator_()
  2254. : MatConstIterator_<_Tp>()
  2255. {}
  2256. template<typename _Tp> inline
  2257. MatIterator_<_Tp>::MatIterator_(Mat_<_Tp>* _m)
  2258. : MatConstIterator_<_Tp>(_m)
  2259. {}
  2260. template<typename _Tp> inline
  2261. MatIterator_<_Tp>::MatIterator_(Mat_<_Tp>* _m, int _row, int _col)
  2262. : MatConstIterator_<_Tp>(_m, _row, _col)
  2263. {}
  2264. template<typename _Tp> inline
  2265. MatIterator_<_Tp>::MatIterator_(Mat_<_Tp>* _m, Point _pt)
  2266. : MatConstIterator_<_Tp>(_m, _pt)
  2267. {}
  2268. template<typename _Tp> inline
  2269. MatIterator_<_Tp>::MatIterator_(Mat_<_Tp>* _m, const int* _idx)
  2270. : MatConstIterator_<_Tp>(_m, _idx)
  2271. {}
  2272. template<typename _Tp> inline
  2273. MatIterator_<_Tp>::MatIterator_(const MatIterator_& it)
  2274. : MatConstIterator_<_Tp>(it)
  2275. {}
  2276. template<typename _Tp> inline
  2277. MatIterator_<_Tp>& MatIterator_<_Tp>::operator = (const MatIterator_<_Tp>& it )
  2278. {
  2279. MatConstIterator::operator = (it);
  2280. return *this;
  2281. }
  2282. template<typename _Tp> inline
  2283. _Tp& MatIterator_<_Tp>::operator *() const
  2284. {
  2285. return *(_Tp*)(this->ptr);
  2286. }
  2287. template<typename _Tp> inline
  2288. MatIterator_<_Tp>& MatIterator_<_Tp>::operator += (ptrdiff_t ofs)
  2289. {
  2290. MatConstIterator::operator += (ofs);
  2291. return *this;
  2292. }
  2293. template<typename _Tp> inline
  2294. MatIterator_<_Tp>& MatIterator_<_Tp>::operator -= (ptrdiff_t ofs)
  2295. {
  2296. MatConstIterator::operator += (-ofs);
  2297. return *this;
  2298. }
  2299. template<typename _Tp> inline
  2300. MatIterator_<_Tp>& MatIterator_<_Tp>::operator --()
  2301. {
  2302. MatConstIterator::operator --();
  2303. return *this;
  2304. }
  2305. template<typename _Tp> inline
  2306. MatIterator_<_Tp> MatIterator_<_Tp>::operator --(int)
  2307. {
  2308. MatIterator_ b = *this;
  2309. MatConstIterator::operator --();
  2310. return b;
  2311. }
  2312. template<typename _Tp> inline
  2313. MatIterator_<_Tp>& MatIterator_<_Tp>::operator ++()
  2314. {
  2315. MatConstIterator::operator ++();
  2316. return *this;
  2317. }
  2318. template<typename _Tp> inline
  2319. MatIterator_<_Tp> MatIterator_<_Tp>::operator ++(int)
  2320. {
  2321. MatIterator_ b = *this;
  2322. MatConstIterator::operator ++();
  2323. return b;
  2324. }
  2325. template<typename _Tp> inline
  2326. _Tp& MatIterator_<_Tp>::operator [](ptrdiff_t i) const
  2327. {
  2328. return *(*this + i);
  2329. }
  2330. template<typename _Tp> static inline
  2331. bool operator == (const MatIterator_<_Tp>& a, const MatIterator_<_Tp>& b)
  2332. {
  2333. return a.m == b.m && a.ptr == b.ptr;
  2334. }
  2335. template<typename _Tp> static inline
  2336. bool operator != (const MatIterator_<_Tp>& a, const MatIterator_<_Tp>& b)
  2337. {
  2338. return a.m != b.m || a.ptr != b.ptr;
  2339. }
  2340. template<typename _Tp> static inline
  2341. MatIterator_<_Tp> operator + (const MatIterator_<_Tp>& a, ptrdiff_t ofs)
  2342. {
  2343. MatConstIterator t = (const MatConstIterator&)a + ofs;
  2344. return (MatIterator_<_Tp>&)t;
  2345. }
  2346. template<typename _Tp> static inline
  2347. MatIterator_<_Tp> operator + (ptrdiff_t ofs, const MatIterator_<_Tp>& a)
  2348. {
  2349. MatConstIterator t = (const MatConstIterator&)a + ofs;
  2350. return (MatIterator_<_Tp>&)t;
  2351. }
  2352. template<typename _Tp> static inline
  2353. MatIterator_<_Tp> operator - (const MatIterator_<_Tp>& a, ptrdiff_t ofs)
  2354. {
  2355. MatConstIterator t = (const MatConstIterator&)a - ofs;
  2356. return (MatIterator_<_Tp>&)t;
  2357. }
  2358. /////////////////////// SparseMatConstIterator //////////////////////
  2359. inline
  2360. SparseMatConstIterator::SparseMatConstIterator()
  2361. : m(0), hashidx(0), ptr(0)
  2362. {}
  2363. inline
  2364. SparseMatConstIterator::SparseMatConstIterator(const SparseMatConstIterator& it)
  2365. : m(it.m), hashidx(it.hashidx), ptr(it.ptr)
  2366. {}
  2367. inline SparseMatConstIterator& SparseMatConstIterator::operator = (const SparseMatConstIterator& it)
  2368. {
  2369. if( this != &it )
  2370. {
  2371. m = it.m;
  2372. hashidx = it.hashidx;
  2373. ptr = it.ptr;
  2374. }
  2375. return *this;
  2376. }
  2377. template<typename _Tp> inline
  2378. const _Tp& SparseMatConstIterator::value() const
  2379. {
  2380. return *(const _Tp*)ptr;
  2381. }
  2382. inline
  2383. const SparseMat::Node* SparseMatConstIterator::node() const
  2384. {
  2385. return (ptr && m && m->hdr) ? (const SparseMat::Node*)(const void*)(ptr - m->hdr->valueOffset) : 0;
  2386. }
  2387. inline
  2388. SparseMatConstIterator SparseMatConstIterator::operator ++(int)
  2389. {
  2390. SparseMatConstIterator it = *this;
  2391. ++*this;
  2392. return it;
  2393. }
  2394. inline
  2395. void SparseMatConstIterator::seekEnd()
  2396. {
  2397. if( m && m->hdr )
  2398. {
  2399. hashidx = m->hdr->hashtab.size();
  2400. ptr = 0;
  2401. }
  2402. }
  2403. static inline
  2404. bool operator == (const SparseMatConstIterator& it1, const SparseMatConstIterator& it2)
  2405. {
  2406. return it1.m == it2.m && it1.ptr == it2.ptr;
  2407. }
  2408. static inline
  2409. bool operator != (const SparseMatConstIterator& it1, const SparseMatConstIterator& it2)
  2410. {
  2411. return !(it1 == it2);
  2412. }
  2413. ///////////////////////// SparseMatIterator /////////////////////////
  2414. inline
  2415. SparseMatIterator::SparseMatIterator()
  2416. {}
  2417. inline
  2418. SparseMatIterator::SparseMatIterator(SparseMat* _m)
  2419. : SparseMatConstIterator(_m)
  2420. {}
  2421. inline
  2422. SparseMatIterator::SparseMatIterator(const SparseMatIterator& it)
  2423. : SparseMatConstIterator(it)
  2424. {}
  2425. inline
  2426. SparseMatIterator& SparseMatIterator::operator = (const SparseMatIterator& it)
  2427. {
  2428. (SparseMatConstIterator&)*this = it;
  2429. return *this;
  2430. }
  2431. template<typename _Tp> inline
  2432. _Tp& SparseMatIterator::value() const
  2433. {
  2434. return *(_Tp*)ptr;
  2435. }
  2436. inline
  2437. SparseMat::Node* SparseMatIterator::node() const
  2438. {
  2439. return (SparseMat::Node*)SparseMatConstIterator::node();
  2440. }
  2441. inline
  2442. SparseMatIterator& SparseMatIterator::operator ++()
  2443. {
  2444. SparseMatConstIterator::operator ++();
  2445. return *this;
  2446. }
  2447. inline
  2448. SparseMatIterator SparseMatIterator::operator ++(int)
  2449. {
  2450. SparseMatIterator it = *this;
  2451. ++*this;
  2452. return it;
  2453. }
  2454. ////////////////////// SparseMatConstIterator_ //////////////////////
  2455. template<typename _Tp> inline
  2456. SparseMatConstIterator_<_Tp>::SparseMatConstIterator_()
  2457. {}
  2458. template<typename _Tp> inline
  2459. SparseMatConstIterator_<_Tp>::SparseMatConstIterator_(const SparseMat_<_Tp>* _m)
  2460. : SparseMatConstIterator(_m)
  2461. {}
  2462. template<typename _Tp> inline
  2463. SparseMatConstIterator_<_Tp>::SparseMatConstIterator_(const SparseMat* _m)
  2464. : SparseMatConstIterator(_m)
  2465. {
  2466. CV_Assert( _m->type() == traits::Type<_Tp>::value );
  2467. }
  2468. template<typename _Tp> inline
  2469. SparseMatConstIterator_<_Tp>::SparseMatConstIterator_(const SparseMatConstIterator_<_Tp>& it)
  2470. : SparseMatConstIterator(it)
  2471. {}
  2472. template<typename _Tp> inline
  2473. SparseMatConstIterator_<_Tp>& SparseMatConstIterator_<_Tp>::operator = (const SparseMatConstIterator_<_Tp>& it)
  2474. {
  2475. return reinterpret_cast<SparseMatConstIterator_<_Tp>&>
  2476. (*reinterpret_cast<SparseMatConstIterator*>(this) =
  2477. reinterpret_cast<const SparseMatConstIterator&>(it));
  2478. }
  2479. template<typename _Tp> inline
  2480. const _Tp& SparseMatConstIterator_<_Tp>::operator *() const
  2481. {
  2482. return *(const _Tp*)this->ptr;
  2483. }
  2484. template<typename _Tp> inline
  2485. SparseMatConstIterator_<_Tp>& SparseMatConstIterator_<_Tp>::operator ++()
  2486. {
  2487. SparseMatConstIterator::operator ++();
  2488. return *this;
  2489. }
  2490. template<typename _Tp> inline
  2491. SparseMatConstIterator_<_Tp> SparseMatConstIterator_<_Tp>::operator ++(int)
  2492. {
  2493. SparseMatConstIterator_<_Tp> it = *this;
  2494. SparseMatConstIterator::operator ++();
  2495. return it;
  2496. }
  2497. ///////////////////////// SparseMatIterator_ ////////////////////////
  2498. template<typename _Tp> inline
  2499. SparseMatIterator_<_Tp>::SparseMatIterator_()
  2500. {}
  2501. template<typename _Tp> inline
  2502. SparseMatIterator_<_Tp>::SparseMatIterator_(SparseMat_<_Tp>* _m)
  2503. : SparseMatConstIterator_<_Tp>(_m)
  2504. {}
  2505. template<typename _Tp> inline
  2506. SparseMatIterator_<_Tp>::SparseMatIterator_(SparseMat* _m)
  2507. : SparseMatConstIterator_<_Tp>(_m)
  2508. {}
  2509. template<typename _Tp> inline
  2510. SparseMatIterator_<_Tp>::SparseMatIterator_(const SparseMatIterator_<_Tp>& it)
  2511. : SparseMatConstIterator_<_Tp>(it)
  2512. {}
  2513. template<typename _Tp> inline
  2514. SparseMatIterator_<_Tp>& SparseMatIterator_<_Tp>::operator = (const SparseMatIterator_<_Tp>& it)
  2515. {
  2516. return reinterpret_cast<SparseMatIterator_<_Tp>&>
  2517. (*reinterpret_cast<SparseMatConstIterator*>(this) =
  2518. reinterpret_cast<const SparseMatConstIterator&>(it));
  2519. }
  2520. template<typename _Tp> inline
  2521. _Tp& SparseMatIterator_<_Tp>::operator *() const
  2522. {
  2523. return *(_Tp*)this->ptr;
  2524. }
  2525. template<typename _Tp> inline
  2526. SparseMatIterator_<_Tp>& SparseMatIterator_<_Tp>::operator ++()
  2527. {
  2528. SparseMatConstIterator::operator ++();
  2529. return *this;
  2530. }
  2531. template<typename _Tp> inline
  2532. SparseMatIterator_<_Tp> SparseMatIterator_<_Tp>::operator ++(int)
  2533. {
  2534. SparseMatIterator_<_Tp> it = *this;
  2535. SparseMatConstIterator::operator ++();
  2536. return it;
  2537. }
  2538. //////////////////////// MatCommaInitializer_ ///////////////////////
  2539. template<typename _Tp> inline
  2540. MatCommaInitializer_<_Tp>::MatCommaInitializer_(Mat_<_Tp>* _m)
  2541. : it(_m)
  2542. {}
  2543. template<typename _Tp> template<typename T2> inline
  2544. MatCommaInitializer_<_Tp>& MatCommaInitializer_<_Tp>::operator , (T2 v)
  2545. {
  2546. CV_DbgAssert( this->it < ((const Mat_<_Tp>*)this->it.m)->end() );
  2547. *this->it = _Tp(v);
  2548. ++this->it;
  2549. return *this;
  2550. }
  2551. template<typename _Tp> inline
  2552. MatCommaInitializer_<_Tp>::operator Mat_<_Tp>() const
  2553. {
  2554. CV_DbgAssert( this->it == ((const Mat_<_Tp>*)this->it.m)->end() );
  2555. return Mat_<_Tp>(*this->it.m);
  2556. }
  2557. template<typename _Tp, typename T2> static inline
  2558. MatCommaInitializer_<_Tp> operator << (const Mat_<_Tp>& m, T2 val)
  2559. {
  2560. MatCommaInitializer_<_Tp> commaInitializer((Mat_<_Tp>*)&m);
  2561. return (commaInitializer, val);
  2562. }
  2563. ///////////////////////// Matrix Expressions ////////////////////////
  2564. inline
  2565. Mat& Mat::operator = (const MatExpr& e)
  2566. {
  2567. e.op->assign(e, *this);
  2568. return *this;
  2569. }
  2570. template<typename _Tp> inline
  2571. Mat_<_Tp>::Mat_(const MatExpr& e)
  2572. {
  2573. e.op->assign(e, *this, traits::Type<_Tp>::value);
  2574. }
  2575. template<typename _Tp> inline
  2576. Mat_<_Tp>& Mat_<_Tp>::operator = (const MatExpr& e)
  2577. {
  2578. e.op->assign(e, *this, traits::Type<_Tp>::value);
  2579. return *this;
  2580. }
  2581. template<typename _Tp> inline
  2582. MatExpr Mat_<_Tp>::zeros(int rows, int cols)
  2583. {
  2584. return Mat::zeros(rows, cols, traits::Type<_Tp>::value);
  2585. }
  2586. template<typename _Tp> inline
  2587. MatExpr Mat_<_Tp>::zeros(Size sz)
  2588. {
  2589. return Mat::zeros(sz, traits::Type<_Tp>::value);
  2590. }
  2591. template<typename _Tp> inline
  2592. MatExpr Mat_<_Tp>::ones(int rows, int cols)
  2593. {
  2594. return Mat::ones(rows, cols, traits::Type<_Tp>::value);
  2595. }
  2596. template<typename _Tp> inline
  2597. MatExpr Mat_<_Tp>::ones(Size sz)
  2598. {
  2599. return Mat::ones(sz, traits::Type<_Tp>::value);
  2600. }
  2601. template<typename _Tp> inline
  2602. MatExpr Mat_<_Tp>::eye(int rows, int cols)
  2603. {
  2604. return Mat::eye(rows, cols, traits::Type<_Tp>::value);
  2605. }
  2606. template<typename _Tp> inline
  2607. MatExpr Mat_<_Tp>::eye(Size sz)
  2608. {
  2609. return Mat::eye(sz, traits::Type<_Tp>::value);
  2610. }
  2611. inline
  2612. MatExpr::MatExpr()
  2613. : op(0), flags(0), a(Mat()), b(Mat()), c(Mat()), alpha(0), beta(0), s()
  2614. {}
  2615. inline
  2616. MatExpr::MatExpr(const MatOp* _op, int _flags, const Mat& _a, const Mat& _b,
  2617. const Mat& _c, double _alpha, double _beta, const Scalar& _s)
  2618. : op(_op), flags(_flags), a(_a), b(_b), c(_c), alpha(_alpha), beta(_beta), s(_s)
  2619. {}
  2620. inline
  2621. MatExpr::operator Mat() const
  2622. {
  2623. Mat m;
  2624. op->assign(*this, m);
  2625. return m;
  2626. }
  2627. template<typename _Tp> inline
  2628. MatExpr::operator Mat_<_Tp>() const
  2629. {
  2630. Mat_<_Tp> m;
  2631. op->assign(*this, m, traits::Type<_Tp>::value);
  2632. return m;
  2633. }
  2634. template<typename _Tp> static inline
  2635. MatExpr min(const Mat_<_Tp>& a, const Mat_<_Tp>& b)
  2636. {
  2637. return cv::min((const Mat&)a, (const Mat&)b);
  2638. }
  2639. template<typename _Tp> static inline
  2640. MatExpr min(const Mat_<_Tp>& a, double s)
  2641. {
  2642. return cv::min((const Mat&)a, s);
  2643. }
  2644. template<typename _Tp> static inline
  2645. MatExpr min(double s, const Mat_<_Tp>& a)
  2646. {
  2647. return cv::min((const Mat&)a, s);
  2648. }
  2649. template<typename _Tp> static inline
  2650. MatExpr max(const Mat_<_Tp>& a, const Mat_<_Tp>& b)
  2651. {
  2652. return cv::max((const Mat&)a, (const Mat&)b);
  2653. }
  2654. template<typename _Tp> static inline
  2655. MatExpr max(const Mat_<_Tp>& a, double s)
  2656. {
  2657. return cv::max((const Mat&)a, s);
  2658. }
  2659. template<typename _Tp> static inline
  2660. MatExpr max(double s, const Mat_<_Tp>& a)
  2661. {
  2662. return cv::max((const Mat&)a, s);
  2663. }
  2664. template<typename _Tp> static inline
  2665. MatExpr abs(const Mat_<_Tp>& m)
  2666. {
  2667. return cv::abs((const Mat&)m);
  2668. }
  2669. static inline
  2670. Mat& operator += (Mat& a, const MatExpr& b)
  2671. {
  2672. b.op->augAssignAdd(b, a);
  2673. return a;
  2674. }
  2675. static inline
  2676. const Mat& operator += (const Mat& a, const MatExpr& b)
  2677. {
  2678. b.op->augAssignAdd(b, (Mat&)a);
  2679. return a;
  2680. }
  2681. template<typename _Tp> static inline
  2682. Mat_<_Tp>& operator += (Mat_<_Tp>& a, const MatExpr& b)
  2683. {
  2684. b.op->augAssignAdd(b, a);
  2685. return a;
  2686. }
  2687. template<typename _Tp> static inline
  2688. const Mat_<_Tp>& operator += (const Mat_<_Tp>& a, const MatExpr& b)
  2689. {
  2690. b.op->augAssignAdd(b, (Mat&)a);
  2691. return a;
  2692. }
  2693. static inline
  2694. Mat& operator -= (Mat& a, const MatExpr& b)
  2695. {
  2696. b.op->augAssignSubtract(b, a);
  2697. return a;
  2698. }
  2699. static inline
  2700. const Mat& operator -= (const Mat& a, const MatExpr& b)
  2701. {
  2702. b.op->augAssignSubtract(b, (Mat&)a);
  2703. return a;
  2704. }
  2705. template<typename _Tp> static inline
  2706. Mat_<_Tp>& operator -= (Mat_<_Tp>& a, const MatExpr& b)
  2707. {
  2708. b.op->augAssignSubtract(b, a);
  2709. return a;
  2710. }
  2711. template<typename _Tp> static inline
  2712. const Mat_<_Tp>& operator -= (const Mat_<_Tp>& a, const MatExpr& b)
  2713. {
  2714. b.op->augAssignSubtract(b, (Mat&)a);
  2715. return a;
  2716. }
  2717. static inline
  2718. Mat& operator *= (Mat& a, const MatExpr& b)
  2719. {
  2720. b.op->augAssignMultiply(b, a);
  2721. return a;
  2722. }
  2723. static inline
  2724. const Mat& operator *= (const Mat& a, const MatExpr& b)
  2725. {
  2726. b.op->augAssignMultiply(b, (Mat&)a);
  2727. return a;
  2728. }
  2729. template<typename _Tp> static inline
  2730. Mat_<_Tp>& operator *= (Mat_<_Tp>& a, const MatExpr& b)
  2731. {
  2732. b.op->augAssignMultiply(b, a);
  2733. return a;
  2734. }
  2735. template<typename _Tp> static inline
  2736. const Mat_<_Tp>& operator *= (const Mat_<_Tp>& a, const MatExpr& b)
  2737. {
  2738. b.op->augAssignMultiply(b, (Mat&)a);
  2739. return a;
  2740. }
  2741. static inline
  2742. Mat& operator /= (Mat& a, const MatExpr& b)
  2743. {
  2744. b.op->augAssignDivide(b, a);
  2745. return a;
  2746. }
  2747. static inline
  2748. const Mat& operator /= (const Mat& a, const MatExpr& b)
  2749. {
  2750. b.op->augAssignDivide(b, (Mat&)a);
  2751. return a;
  2752. }
  2753. template<typename _Tp> static inline
  2754. Mat_<_Tp>& operator /= (Mat_<_Tp>& a, const MatExpr& b)
  2755. {
  2756. b.op->augAssignDivide(b, a);
  2757. return a;
  2758. }
  2759. template<typename _Tp> static inline
  2760. const Mat_<_Tp>& operator /= (const Mat_<_Tp>& a, const MatExpr& b)
  2761. {
  2762. b.op->augAssignDivide(b, (Mat&)a);
  2763. return a;
  2764. }
  2765. //////////////////////////////// UMat ////////////////////////////////
  2766. template<typename _Tp> inline
  2767. UMat::UMat(const std::vector<_Tp>& vec, bool copyData)
  2768. : flags(MAGIC_VAL | traits::Type<_Tp>::value | CV_MAT_CONT_FLAG), dims(2), rows((int)vec.size()),
  2769. cols(1), allocator(0), usageFlags(USAGE_DEFAULT), u(0), offset(0), size(&rows)
  2770. {
  2771. if(vec.empty())
  2772. return;
  2773. if( !copyData )
  2774. {
  2775. // !!!TODO!!!
  2776. CV_Error(Error::StsNotImplemented, "");
  2777. }
  2778. else
  2779. Mat((int)vec.size(), 1, traits::Type<_Tp>::value, (uchar*)&vec[0]).copyTo(*this);
  2780. }
  2781. inline
  2782. UMat UMat::row(int y) const
  2783. {
  2784. return UMat(*this, Range(y, y + 1), Range::all());
  2785. }
  2786. inline
  2787. UMat UMat::col(int x) const
  2788. {
  2789. return UMat(*this, Range::all(), Range(x, x + 1));
  2790. }
  2791. inline
  2792. UMat UMat::rowRange(int startrow, int endrow) const
  2793. {
  2794. return UMat(*this, Range(startrow, endrow), Range::all());
  2795. }
  2796. inline
  2797. UMat UMat::rowRange(const Range& r) const
  2798. {
  2799. return UMat(*this, r, Range::all());
  2800. }
  2801. inline
  2802. UMat UMat::colRange(int startcol, int endcol) const
  2803. {
  2804. return UMat(*this, Range::all(), Range(startcol, endcol));
  2805. }
  2806. inline
  2807. UMat UMat::colRange(const Range& r) const
  2808. {
  2809. return UMat(*this, Range::all(), r);
  2810. }
  2811. inline
  2812. UMat UMat::operator()( Range _rowRange, Range _colRange ) const
  2813. {
  2814. return UMat(*this, _rowRange, _colRange);
  2815. }
  2816. inline
  2817. UMat UMat::operator()( const Rect& roi ) const
  2818. {
  2819. return UMat(*this, roi);
  2820. }
  2821. inline
  2822. UMat UMat::operator()(const Range* ranges) const
  2823. {
  2824. return UMat(*this, ranges);
  2825. }
  2826. inline
  2827. UMat UMat::operator()(const std::vector<Range>& ranges) const
  2828. {
  2829. return UMat(*this, ranges);
  2830. }
  2831. inline
  2832. bool UMat::isContinuous() const
  2833. {
  2834. return (flags & CONTINUOUS_FLAG) != 0;
  2835. }
  2836. inline
  2837. bool UMat::isSubmatrix() const
  2838. {
  2839. return (flags & SUBMATRIX_FLAG) != 0;
  2840. }
  2841. inline
  2842. size_t UMat::elemSize() const
  2843. {
  2844. size_t res = dims > 0 ? step.p[dims - 1] : 0;
  2845. CV_DbgAssert(res != 0);
  2846. return res;
  2847. }
  2848. inline
  2849. size_t UMat::elemSize1() const
  2850. {
  2851. return CV_ELEM_SIZE1(flags);
  2852. }
  2853. inline
  2854. int UMat::type() const
  2855. {
  2856. return CV_MAT_TYPE(flags);
  2857. }
  2858. inline
  2859. int UMat::depth() const
  2860. {
  2861. return CV_MAT_DEPTH(flags);
  2862. }
  2863. inline
  2864. int UMat::channels() const
  2865. {
  2866. return CV_MAT_CN(flags);
  2867. }
  2868. inline
  2869. size_t UMat::step1(int i) const
  2870. {
  2871. return step.p[i] / elemSize1();
  2872. }
  2873. #ifdef CV_CXX_MOVE_SEMANTICS
  2874. inline
  2875. UMat::UMat(UMat&& m)
  2876. : flags(m.flags), dims(m.dims), rows(m.rows), cols(m.cols), allocator(m.allocator),
  2877. usageFlags(m.usageFlags), u(m.u), offset(m.offset), size(&rows)
  2878. {
  2879. if (m.dims <= 2) // move new step/size info
  2880. {
  2881. step[0] = m.step[0];
  2882. step[1] = m.step[1];
  2883. }
  2884. else
  2885. {
  2886. CV_DbgAssert(m.step.p != m.step.buf);
  2887. step.p = m.step.p;
  2888. size.p = m.size.p;
  2889. m.step.p = m.step.buf;
  2890. m.size.p = &m.rows;
  2891. }
  2892. m.flags = MAGIC_VAL; m.dims = m.rows = m.cols = 0;
  2893. m.allocator = NULL;
  2894. m.u = NULL;
  2895. m.offset = 0;
  2896. }
  2897. inline
  2898. UMat& UMat::operator = (UMat&& m)
  2899. {
  2900. if (this == &m)
  2901. return *this;
  2902. release();
  2903. flags = m.flags; dims = m.dims; rows = m.rows; cols = m.cols;
  2904. allocator = m.allocator; usageFlags = m.usageFlags;
  2905. u = m.u;
  2906. offset = m.offset;
  2907. if (step.p != step.buf) // release self step/size
  2908. {
  2909. fastFree(step.p);
  2910. step.p = step.buf;
  2911. size.p = &rows;
  2912. }
  2913. if (m.dims <= 2) // move new step/size info
  2914. {
  2915. step[0] = m.step[0];
  2916. step[1] = m.step[1];
  2917. }
  2918. else
  2919. {
  2920. CV_DbgAssert(m.step.p != m.step.buf);
  2921. step.p = m.step.p;
  2922. size.p = m.size.p;
  2923. m.step.p = m.step.buf;
  2924. m.size.p = &m.rows;
  2925. }
  2926. m.flags = MAGIC_VAL; m.dims = m.rows = m.cols = 0;
  2927. m.allocator = NULL;
  2928. m.u = NULL;
  2929. m.offset = 0;
  2930. return *this;
  2931. }
  2932. #endif
  2933. inline bool UMatData::hostCopyObsolete() const { return (flags & HOST_COPY_OBSOLETE) != 0; }
  2934. inline bool UMatData::deviceCopyObsolete() const { return (flags & DEVICE_COPY_OBSOLETE) != 0; }
  2935. inline bool UMatData::deviceMemMapped() const { return (flags & DEVICE_MEM_MAPPED) != 0; }
  2936. inline bool UMatData::copyOnMap() const { return (flags & COPY_ON_MAP) != 0; }
  2937. inline bool UMatData::tempUMat() const { return (flags & TEMP_UMAT) != 0; }
  2938. inline bool UMatData::tempCopiedUMat() const { return (flags & TEMP_COPIED_UMAT) == TEMP_COPIED_UMAT; }
  2939. inline void UMatData::markDeviceMemMapped(bool flag)
  2940. {
  2941. if(flag)
  2942. flags |= DEVICE_MEM_MAPPED;
  2943. else
  2944. flags &= ~DEVICE_MEM_MAPPED;
  2945. }
  2946. inline void UMatData::markHostCopyObsolete(bool flag)
  2947. {
  2948. if(flag)
  2949. flags |= HOST_COPY_OBSOLETE;
  2950. else
  2951. flags &= ~HOST_COPY_OBSOLETE;
  2952. }
  2953. inline void UMatData::markDeviceCopyObsolete(bool flag)
  2954. {
  2955. if(flag)
  2956. flags |= DEVICE_COPY_OBSOLETE;
  2957. else
  2958. flags &= ~DEVICE_COPY_OBSOLETE;
  2959. }
  2960. //! @endcond
  2961. static inline
  2962. void swap(MatExpr& a, MatExpr& b) { a.swap(b); }
  2963. } //cv
  2964. #ifdef _MSC_VER
  2965. #pragma warning( pop )
  2966. #endif
  2967. #ifdef CV_DISABLE_CLANG_ENUM_WARNINGS
  2968. #undef CV_DISABLE_CLANG_ENUM_WARNINGS
  2969. #pragma clang diagnostic pop
  2970. #endif
  2971. #endif