00001 00031 #ifndef __VETLIB_VETBUFFERARRAY_H__ 00032 #define __VETLIB_VETBUFFERARRAY_H__ 00033 00034 #include "../vetBuffer.h" 00035 00036 00037 #define VETDEF_STACK_FRAMECOUNT 100 /* */ 00038 #define VETDEF_STACK_FRAMEREALLOCSTEP 10 /* */ 00039 00040 00041 00042 #define VETDEF_FPS_MAX 1000 /* maximum number of frames per second */ 00043 #define VETDEF_FCOUNT_MAX 32000 /* maximum number of frames */ 00044 00045 00046 template<class T> 00047 class vetBufferArray : public vetBuffer<T> 00048 { 00049 00050 protected: 00051 00055 T** v_frames; //frames* array 00056 00060 unsigned int v_current_frame; 00061 00065 unsigned int v_storageLenght; 00066 00067 00068 public: 00069 00073 vetBufferArray() : vetBuffer<T>() 00074 { 00075 INFO("vetBufferArray::vetBufferArray() : vetBuffer() [CONTRUCTOR]") 00076 v_frames = NULL; 00077 reset(); 00078 }; 00079 00085 VETRESULT reset() 00086 { 00087 INFO("int vetBufferArray::reset() [SET DEFAULT PARAMETERS]") 00088 00089 if (v_frames != NULL) 00090 delete v_frames; 00091 00092 v_frames = NULL; 00093 v_fcount = 0; 00094 v_storageLenght = VETDEF_STACK_FRAMECOUNT; 00095 v_current_frame = 0; 00096 copyData = true; 00097 00098 return VETRET_OK; 00099 }; 00100 00104 ~vetBufferArray() 00105 { 00106 INFO("vetBufferArray::~vetBufferArray() [DESTRUCTOR]") 00107 00108 if (v_frames != NULL) 00109 delete v_frames; 00110 00111 }; 00112 00118 VETRESULT deleteFrames() 00119 { 00120 if (v_frames != NULL) 00121 { 00122 delete [] v_frames; 00123 v_frames = NULL; 00124 return reset(); 00125 } 00126 00127 return VETRET_ILLEGAL_USE; 00128 }; 00129 00130 00140 VETRESULT addFrame(T* newFrame) 00141 { 00142 if (v_fcount+1 > VETDEF_FCOUNT_MAX) 00143 return VETRET_ILLEGAL_USE; 00144 00145 if (v_frames == NULL) // this one will be the first frame 00146 { 00147 v_storageLenght = VETDEF_STACK_FRAMECOUNT; 00148 // allocate a new frames array 00149 v_frames = new T*[v_storageLenght]; // first frames array allocation 00150 v_fcount = 1; 00151 // add the new frame 00152 if ( copyData ) 00153 v_frames[0] = new T(*newFrame); 00154 else 00155 v_frames[0] = newFrame; 00156 00157 } 00158 else if (v_fcount < (long)v_storageLenght) // there's enough space for this frame 00159 { 00160 00161 if ( copyData ) 00162 v_frames[v_fcount] = new T(*newFrame); 00163 else 00164 v_frames[v_fcount] = newFrame; 00165 00166 // update frame count to VetInfo 00167 ++v_fcount; 00168 00169 } 00170 else { 00171 // recalculates new storage (array) length using default step size (>1) 00172 v_storageLenght = v_fcount + VETDEF_STACK_FRAMEREALLOCSTEP; 00173 // allocate a new frames array 00174 T** new_frames = new T*[v_storageLenght]; 00175 // copy frames* to the new array 00176 for (int i=0; i<v_fcount; i++) { 00177 new_frames[i] = v_frames[i]; 00178 } 00179 // remove old pointer and update with new array 00180 delete v_frames; 00181 v_frames = new_frames; 00182 // add the new frame 00183 if ( copyData ) 00184 v_frames[v_fcount++] = new T(*newFrame); 00185 else 00186 v_frames[v_fcount++] = newFrame; 00187 } 00188 00189 00190 return VETRET_OK; 00191 }; 00192 00203 VETRESULT insertFrame(long index, T* newFrame) 00204 { 00205 if (v_fcount+1 > VETDEF_FCOUNT_MAX) 00206 return VETRET_ILLEGAL_USE; 00207 00208 if (v_frames == NULL) // this one will be the first frame 00209 return addFrame(newFrame); 00210 if ( index < 0 || index >= v_fcount) 00211 return VETRET_PARAM_ERR; 00212 00213 if (v_fcount < (long)v_storageLenght) { // there's enough space for this frame 00214 //move frames* after INDEX one position next 00215 for (int i=v_fcount-1; i>=(int)index; i--) { 00216 v_frames[i+1] = v_frames[i]; 00217 } 00218 //insert the new frame 00219 if ( copyData ) 00220 v_frames[index] = new T(*newFrame); 00221 else 00222 v_frames[index] = newFrame; 00223 00224 ++v_fcount; 00225 00226 } 00227 else { 00228 // recalculates new storage (array) length using default step size (>1) 00229 v_storageLenght = v_fcount + VETDEF_STACK_FRAMEREALLOCSTEP; 00230 // allocate a new frames array 00231 T** new_frames = new T*[v_storageLenght]; 00232 // copy frames* before INDEX to the new array 00233 for (int i=0; i<(int)index; i++) { 00234 new_frames[i] = v_frames[i]; 00235 } 00236 //insert the new frame 00237 if ( copyData ) 00238 v_frames[index] = new T(*newFrame); 00239 else 00240 v_frames[index] = newFrame; 00241 // copy frames* after INDEX to the new array 00242 for (int j=(int)index; j<v_fcount; j++) { 00243 new_frames[j] = v_frames[j]; 00244 } 00245 // remove old pointer and update with new array 00246 delete v_frames; 00247 v_frames = new_frames; 00248 // update frame count to VetInfo 00249 ++v_fcount; 00250 00251 } 00252 return VETRET_OK; 00253 }; 00254 00265 VETRESULT updateFrame(long index, T* newFrame, bool removeold) 00266 { 00267 if ( v_frames == NULL ) 00268 return VETRET_ILLEGAL_USE; 00269 if ( index < 0 || index >= v_fcount ) 00270 return VETRET_PARAM_ERR; 00271 00272 // remove old frame 00273 if ( removeold ) 00274 delete v_frames[ (int)index ]; 00275 00276 // update frame* with given address 00277 if ( copyData ) 00278 v_frames[index] = new T(*newFrame); 00279 else 00280 v_frames[index] = newFrame; 00281 00282 return VETRET_OK; 00283 }; 00284 00293 VETRESULT removeFrame(T* frameToDelete, bool freeData = false) 00294 { 00295 if (v_frames == NULL) 00296 return VETRET_ILLEGAL_USE; 00297 00298 int i = 0; 00299 while ( i < v_fcount ) { 00300 00301 if ( v_frames[i] == frameToDelete ) 00302 return removeFrame(i, freeData); 00303 i++; 00304 } 00305 00306 return VETRET_PARAM_ERR; 00307 00308 }; 00309 00318 VETRESULT removeFrame(long index, bool freeData = false) 00319 { 00320 DEBUGMSG("int vetBufferArray::removeFrame(long index)", index) 00321 00322 if (v_frames == NULL) 00323 return VETRET_ILLEGAL_USE; 00324 if ( index < 0 || index >= v_fcount) 00325 return VETRET_PARAM_ERR; 00326 00327 if ( freeData ) 00328 delete v_frames[index]; 00329 00330 //move frames* after INDEX one position back 00331 for (int i=(int)index; i<v_fcount-1; i++) { 00332 v_frames[i] = v_frames[i+1]; 00333 } 00334 00335 --v_fcount; 00336 00337 return VETRET_OK; 00338 }; 00339 00340 00346 long getCurrentFrameIndex() const { return v_current_frame; }; 00347 00348 00349 00357 T* getFrame(long index) 00358 { 00359 if (v_frames == NULL) 00360 throw "Buffer is empty."; 00361 if ( (int)index < 0 || (int)index >= v_fcount) 00362 throw "Index out of range."; 00363 00364 return v_frames[ (int)index]; 00365 }; 00366 00372 T* getCurrentFrame() 00373 { 00374 if (v_frames == NULL) 00375 throw "Video is Empty."; 00376 00377 return v_frames[v_current_frame]; 00378 }; 00379 00385 T* getPreviousFrame() 00386 { 00387 if (v_frames == NULL) 00388 throw "Video is Empty."; 00389 if ( v_current_frame < 1 || v_current_frame > (unsigned int)v_fcount) 00390 throw "Index out of range."; 00391 00392 return v_frames[v_current_frame-1]; 00393 }; 00394 00400 T* getNextFrame() 00401 { 00402 if (v_frames == NULL) 00403 throw "Video is Empty."; 00404 if ( v_current_frame > (unsigned int)v_fcount-2) 00405 throw "Index out of range."; 00406 00407 return v_frames[v_current_frame+1]; 00408 }; 00409 00415 T* getFirstFrame() 00416 { 00417 if ( v_frames == NULL ) 00418 throw "Video is Empty."; 00419 00420 return v_frames[0]; 00421 }; 00422 00428 T* getLastFrame() 00429 { 00430 if ( v_frames == NULL ) 00431 throw "Video is Empty."; 00432 00433 return v_frames[v_fcount-1]; 00434 }; 00435 00442 VETRESULT goToNextFrame() 00443 { 00444 INFO("int vetBufferArray::goToNextFrame()") 00445 00446 if ( v_frames == NULL ) 00447 return VETRET_ILLEGAL_USE; 00448 00449 if ( v_current_frame > (unsigned int)v_fcount-2) 00450 return VETRET_ILLEGAL_USE; 00451 00452 v_current_frame++; 00453 00454 return VETRET_OK; 00455 }; 00456 00463 VETRESULT goToPreviousFrame() 00464 { 00465 INFO("") 00466 00467 if ( v_frames == NULL ) 00468 return VETRET_ILLEGAL_USE; 00469 00470 if ( v_current_frame == 0 ) 00471 return VETRET_ILLEGAL_USE; 00472 00473 v_current_frame--; 00474 00475 return VETRET_OK; 00476 }; 00477 00483 VETRESULT goToFirstFrame() 00484 { 00485 INFO("int vetBufferArray::goToFirstFrame()") 00486 if ( v_frames == NULL ) 00487 return VETRET_ILLEGAL_USE; 00488 00489 v_current_frame = 0; 00490 00491 00492 return VETRET_OK; 00493 }; 00494 00500 VETRESULT goToLastFrame() 00501 { 00502 INFO("int vetBufferArray::goToLastFrame()") 00503 00504 if ( v_frames == NULL ) 00505 return VETRET_ILLEGAL_USE; 00506 00507 v_current_frame = v_fcount-1; 00508 00509 00510 return VETRET_OK; 00511 }; 00512 00521 VETRESULT goToFrame(long index) 00522 { 00523 DEBUGMSG("int vetBufferArray::goToFrame(long index)", index) 00524 00525 if ( v_frames == NULL ) 00526 return VETRET_ILLEGAL_USE; 00527 00528 if ( (int)index < 0 || (int)index >= v_fcount) 00529 return VETRET_PARAM_ERR; 00530 00531 v_current_frame = (int)index; 00532 00533 return VETRET_OK; 00534 }; 00535 00544 VETRESULT goToStepFrame(long offset) 00545 { 00546 DEBUGMSG("int vetBufferArray::goToStepFrame(long offset)", offset) 00547 00548 if ( v_frames == NULL ) 00549 return VETRET_ILLEGAL_USE; 00550 00551 if ( ( offset > 0 && v_current_frame + (int)offset < (unsigned int)v_fcount-1) 00552 ||( offset < 0 && v_current_frame + (int)offset > 0) ) { 00553 00554 v_current_frame = v_current_frame + (int)offset; 00555 return VETRET_OK; 00556 } 00557 00558 return VETRET_PARAM_ERR; 00559 }; 00560 00561 00562 00563 00564 }; 00565 00566 00567 #endif//__VETLIB_VETBUFFERARRAY_H__
1.4.4