mat.inl.hpp 97 KB

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