1 /*
2  * GStreamer
3  * Copyright (C) 2011 Robert Jobbagy <jobbagy.robert@gmail.com>
4  * Copyright (C) 2011 - 2018 Nicola Murino <nicola.murino@gmail.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Alternatively, the contents of this file may be used under the
25  * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
26  * which case the following provisions apply instead of the ones
27  * mentioned above:
28  *
29  * This library is free software; you can redistribute it and/or
30  * modify it under the terms of the GNU Library General Public
31  * License as published by the Free Software Foundation; either
32  * version 2 of the License, or (at your option) any later version.
33  *
34  * This library is distributed in the hope that it will be useful,
35  * but WITHOUT ANY WARRANTY; without even the implied warranty of
36  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
37  * Library General Public License for more details.
38  *
39  * You should have received a copy of the GNU Library General Public
40  * License along with this library; if not, write to the
41  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
42  * Boston, MA 02110-1301, USA.
43  */
44 
45 /* This breaks the build for reasons that aren't entirely clear to me yet */
46 #if 0
47 //#ifdef HAVE_CONFIG_H
48 //#include "config.h"
49 //#endif
50 #endif
51 
52 #include <stdio.h>
53 #include <limits.h>
54 #include "motioncells_wrapper.h"
55 
56 static int instanceCounter = 0;
57 static bool element_id_was_max = false;
58 
59 vector < instanceOfMC > motioncellsvector;
60 vector < int >motioncellsfreeids;
61 
62 MotionCells *mc;
63 char p_str[] = "idx failed";
64 
65 int
motion_cells_init()66 motion_cells_init ()
67 {
68   mc = new MotionCells ();
69   instanceOfMC tmpmc;
70   tmpmc.id = instanceCounter;
71   tmpmc.mc = mc;
72   motioncellsvector.push_back (tmpmc);
73   if ((instanceCounter < INT_MAX) && !element_id_was_max) {
74     instanceCounter++;
75     element_id_was_max = false;
76   } else {
77     element_id_was_max = true;
78     instanceCounter = motioncellsfreeids.back ();
79     motioncellsfreeids.pop_back ();
80   }
81   return tmpmc.id;
82 }
83 
84 int
perform_detection_motion_cells(cv::Mat p_image,double p_sensitivity,double p_framerate,int p_gridx,int p_gridy,long int p_timestamp_millisec,bool p_isVisible,bool p_useAlpha,int motionmaskcoord_count,motionmaskcoordrect * motionmaskcoords,int motionmaskcells_count,motioncellidx * motionmaskcellsidx,cellscolor motioncellscolor,int motioncells_count,motioncellidx * motioncellsidx,gint64 starttime,char * p_datafile,bool p_changed_datafile,int p_thickness,int p_id)85 perform_detection_motion_cells (cv::Mat p_image, double p_sensitivity,
86     double p_framerate, int p_gridx, int p_gridy, long int p_timestamp_millisec,
87     bool p_isVisible, bool p_useAlpha, int motionmaskcoord_count,
88     motionmaskcoordrect * motionmaskcoords, int motionmaskcells_count,
89     motioncellidx * motionmaskcellsidx, cellscolor motioncellscolor,
90     int motioncells_count, motioncellidx * motioncellsidx, gint64 starttime,
91     char *p_datafile, bool p_changed_datafile, int p_thickness, int p_id)
92 {
93   int idx = 0;
94   idx = searchIdx (p_id);
95   if (idx > -1)
96     return motioncellsvector.at (idx).mc->performDetectionMotionCells (p_image,
97         p_sensitivity, p_framerate, p_gridx, p_gridy, p_timestamp_millisec,
98         p_isVisible, p_useAlpha, motionmaskcoord_count, motionmaskcoords,
99         motionmaskcells_count, motionmaskcellsidx, motioncellscolor,
100         motioncells_count, motioncellsidx, starttime, p_datafile,
101         p_changed_datafile, p_thickness);
102   else
103     return -1;
104 }
105 
106 
107 void
setPrevFrame(cv::Mat p_prevFrame,int p_id)108 setPrevFrame (cv::Mat p_prevFrame, int p_id)
109 {
110   int idx = 0;
111   idx = searchIdx (p_id);
112   if (idx > -1)
113     motioncellsvector.at (idx).mc->setPrevFrame (p_prevFrame);
114 }
115 
116 char *
getMotionCellsIdx(int p_id)117 getMotionCellsIdx (int p_id)
118 {
119   int idx = 0;
120   idx = searchIdx (p_id);
121   if (idx > -1)
122     return motioncellsvector.at (idx).mc->getMotionCellsIdx ();
123   else
124     return p_str;
125 }
126 
127 int
getMotionCellsIdxCnt(int p_id)128 getMotionCellsIdxCnt (int p_id)
129 {
130   int idx = 0;
131   idx = searchIdx (p_id);
132   if (idx > -1)
133     return motioncellsvector.at (idx).mc->getMotionCellsIdxCount ();
134   else
135     return 0;
136 }
137 
138 bool
getChangedDataFile(int p_id)139 getChangedDataFile (int p_id)
140 {
141   int idx = 0;
142   idx = searchIdx (p_id);
143   if (idx > -1)
144     return motioncellsvector.at (idx).mc->getChangedDataFile ();
145   else
146     return false;
147 }
148 
149 int
searchIdx(int p_id)150 searchIdx (int p_id)
151 {
152   for (unsigned int i = 0; i < motioncellsvector.size (); i++) {
153     instanceOfMC tmpmc;
154     tmpmc = motioncellsvector.at (i);
155     if (tmpmc.id == p_id) {
156       return i;
157     }
158   }
159   return -1;
160 }
161 
162 char *
getInitDataFileFailed(int p_id)163 getInitDataFileFailed (int p_id)
164 {
165   int idx = 0;
166   idx = searchIdx (p_id);
167   if (idx > -1)
168     return motioncellsvector.at (idx).mc->getDatafileInitFailed ();
169   else
170     return p_str;
171 }
172 
173 char *
getSaveDataFileFailed(int p_id)174 getSaveDataFileFailed (int p_id)
175 {
176   int idx = 0;
177   idx = searchIdx (p_id);
178   if (idx > -1)
179     return motioncellsvector.at (idx).mc->getDatafileSaveFailed ();
180   else
181     return p_str;
182 }
183 
184 int
getInitErrorCode(int p_id)185 getInitErrorCode (int p_id)
186 {
187   int idx = 0;
188   idx = searchIdx (p_id);
189   if (idx > -1)
190     return motioncellsvector.at (idx).mc->getInitErrorCode ();
191   else
192     return -1;
193 }
194 
195 int
getSaveErrorCode(int p_id)196 getSaveErrorCode (int p_id)
197 {
198   int idx = 0;
199   idx = searchIdx (p_id);
200   if (idx > -1)
201     return motioncellsvector.at (idx).mc->getSaveErrorCode ();
202   else
203     return -1;
204 }
205 
206 void
motion_cells_free(int p_id)207 motion_cells_free (int p_id)
208 {
209   int idx = 0;
210   idx = searchIdx (p_id);
211   if (idx > -1) {
212     delete motioncellsvector.at (idx).mc;
213     motioncellsvector.erase (motioncellsvector.begin () + idx);
214     motioncellsfreeids.push_back (p_id);
215   }
216 }
217 
218 void
motion_cells_free_resources(int p_id)219 motion_cells_free_resources (int p_id)
220 {
221   int idx = 0;
222   idx = searchIdx (p_id);
223   if (idx > -1)
224     motioncellsvector.at (idx).mc->freeDataFile ();
225 }
226