@@ -71,6 +71,70 @@ jsi::Value OpenCVPlugin::get(jsi::Runtime& runtime, const jsi::PropNameID& propN
7171 memcpy (mat.data , vec.data (), (int )rows * (int )cols * (int )channels);
7272 auto id = FOCV_Storage::save (mat);
7373
74+ return FOCV_JsiObject::wrap (runtime, " mat" , id);
75+ });
76+ } else if (propName == " bufferToMat" ) {
77+ return jsi::Function::createFromHostFunction (
78+ runtime, jsi::PropNameID::forAscii (runtime, " bufferToMat" ), 4 ,
79+ [=](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
80+ size_t count) -> jsi::Object {
81+ auto type = arguments[0 ].asString (runtime).utf8 (runtime);
82+ auto rows = arguments[1 ].asNumber ();
83+ auto cols = arguments[2 ].asNumber ();
84+ auto channels = arguments[3 ].asNumber ();
85+ auto input = arguments[4 ].asObject (runtime);
86+
87+ auto modeType = -1 ;
88+ auto typeSize = 1 ;
89+
90+ if (type == " uint8" ) {
91+ typeSize = 1 ;
92+ if (channels == 1 ) modeType = CV_8U;
93+ if (channels == 3 ) modeType = CV_8UC3;
94+ if (channels == 4 ) modeType = CV_8UC4;
95+ } else if (type == " uint16" ) {
96+ typeSize = 2 ;
97+ if (channels == 1 ) modeType = CV_16U;
98+ if (channels == 3 ) modeType = CV_16UC3;
99+ if (channels == 4 ) modeType = CV_16UC4;
100+ } else if (type == " int8" ) {
101+ typeSize = 1 ;
102+ if (channels == 1 ) modeType = CV_8S;
103+ if (channels == 3 ) modeType = CV_8SC3;
104+ if (channels == 4 ) modeType = CV_8SC4;
105+ } else if (type == " int16" ) {
106+ typeSize = 2 ;
107+ if (channels == 1 ) modeType = CV_16S;
108+ if (channels == 3 ) modeType = CV_16SC3;
109+ if (channels == 4 ) modeType = CV_16SC4;
110+ } else if (type == " int32" ) {
111+ typeSize = 4 ;
112+ if (channels == 1 ) modeType = CV_32S;
113+ if (channels == 3 ) modeType = CV_32SC3;
114+ if (channels == 4 ) modeType = CV_32SC4;
115+ } else if (type == " float32" ) {
116+ typeSize = 4 ;
117+ if (channels == 1 ) modeType = CV_32F;
118+ if (channels == 3 ) modeType = CV_32FC3;
119+ if (channels == 4 ) modeType = CV_32FC4;
120+ } else if (type == " float64" ) {
121+ typeSize = 8 ;
122+ if (channels == 1 ) modeType = CV_64F;
123+ if (channels == 3 ) modeType = CV_64FC3;
124+ if (channels == 4 ) modeType = CV_64FC4;
125+ }
126+
127+ if (channels == -1 ) {
128+ throw std::runtime_error (" Fast OpenCV Error: Invalid channel count passed to frameBufferToMat!" );
129+ }
130+
131+ auto inputBuffer = getTypedArray (runtime, std::move (input));
132+ auto vec = inputBuffer.toVector (runtime);
133+
134+ cv::Mat mat (rows, cols, modeType);
135+ memcpy (mat.data , vec.data (), (int )rows * (int )cols * (int )channels * typeSize);
136+ auto id = FOCV_Storage::save (mat);
137+
74138 return FOCV_JsiObject::wrap (runtime, " mat" , id);
75139 });
76140 }
0 commit comments