mat.inl.hpp 97 KB

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