bufferedslaveimagesource.cc

来自「VC视频对象的跟踪提取原代码(vc视频监控源码)」· CC 代码 · 共 475 行 · 第 1/2 页

CC
475
字号
			// copy current Image			master_image_source->set_current			    (master_image_source->get_current()->copy());			// set the window handle to the original so that we use the same window#ifndef NO_DISPLAY#ifdef USE_GL			master_image_source->get_current()->set_glwin(old_glwin);#else#error Tugdual, please put the Qt equivalent or whatever here so that the window is preserved.  Thanks.#endif#endif // ifndef NO_DISPLAY		    }		    else		    {			// copy all but the data to our new image memory			unsigned char *saved_data_ptr = master_image_source->get_current()->get_data();			master_image_source->get_current()->virtual_copy(new_image_memory);			new_image_memory->set_data(saved_data_ptr);						// give it over to master_image_source			master_image_source->set_current(new_image_memory);		    }		}				//		// get a new image if available   		Image *new_image = master_image_source->get_next();		//		// dispose of the old current Image if we had one		if (old_current != NULL)		{		    // do not de-allocate current but re-use its memory for speed reasons		    keep_image_memory_for_later(old_current);		}				if (new_image != NULL)		{		    // place new image into image_buffer		    image_buffer[buffer_entries] = new_image;  // (buffer_entries == 0)		    //		    buffer_entries++;		    // signal ``new image in buffer'' just in case someone waits for it		    pthread_cond_signal(&new_image_in_buffer);		    //   		    cdebug << ", filled up from master. " << endl;		}		else		{		    // unlock write access to image_buffer before returning with NULL		    pthread_mutex_unlock(&image_buffer_modification);		    		    // signal no more images (probably end of sequence)		    assert (buffer_entries == 0);  // no-one should have changed it		    image_buffer[0] = NULL;		    		    cdebug <<  " and no more images from master. " << endl;		    		    current = NULL;		    return current;  // return "no current image"		}	    }	    //	    pthread_mutex_unlock(&image_buffer_modification);	}    	else	{	    // wait for a new image to arrive (done via handle_new_image(...))	    pthread_mutex_lock(&image_buffer_modification);  // avoid deadlocks	    //	    while (buffer_entries < 1)	    {		pthread_cond_wait(&new_image_in_buffer, &image_buffer_modification);	    }	    //	    pthread_mutex_unlock(&image_buffer_modification);	}    }        // At least one image is available in the image_buffer.    //   We always use the first Image pointer in the buffer for `current'.    current = image_buffer[0];        frame_count++;    return current;}/////////////////////////////////////////////////////////////////////////////////                                                                           ////  BufferedSlaveImageSource::fill_buffer: fill up buffer from master        ////                                                                           ////  Call this method to fill up the image_buffer from master_image_source.   ////    Up to number_of_images_to_add images will be read from the master      ////    using the ImageSource::get_next() call.  The buffer will be filled     ////    up to buffer_size if possible.  No more than that many images will be  ////    read from the master_image_source.                                     ////                                                                           ////  Returns the number of images read from image_source and buffered.        ////                                                                           /////////////////////////////////////////////////////////////////////////////////unsigned int BufferedSlaveImageSource::fill_buffer(unsigned int number_of_images_to_add){    unsigned int count;  // to count how many images we are adding        for (count = 0; (count < number_of_images_to_add); count++)    {	// check whether buffer is already full	if (buffer_entries >= buffer_size) // no more room in buffer	    break;		//  1 - get next image from image_source	Image *new_image = master_image_source->get_next();		if (new_image == NULL)	    break;  // just break for now.  We will check for end of sequence elsewhere.		//  2 - tell master not to use this memory any more, we take over	master_image_source->set_current(get_new_image_memory());		//  3 - check image dimensions	assert (new_image->get_width() == xdim);	assert (new_image->get_height() == ydim);		//  4 - add image to image_buffer	// this requires exclusive write access to image_buffer pointers	// (we are competing with get_next() which could be removing an old image from the buffer)	pthread_mutex_lock(&image_buffer_modification);	//	image_buffer[buffer_entries] = new_image;	buffer_entries++;	//	// signal ``new image in buffer'' to other thread in case it waits (in get_next())	pthread_cond_signal(&new_image_in_buffer);	//	pthread_mutex_unlock(&image_buffer_modification);    }        return count;}/////////////////////////////////////////////////////////////////////////////////                                                                           ////  BufferedSlaveImageSource::keep_image_memory_for_later(Image *&old_image) ////                                                                           ////  Given an Image which is no longer used, the method tries to reserve the  ////    Image memory for later re-use to avoid slow memory allocation and de-  ////    allocation.  The pointer is simply put into an unused slot in the      ////    image_buffer (beyond buffer_entries).  Call get_new_image_memory()     ////    to access the memory which has been reserved by this method.           ////                                                                           ////  This method assumes that it has exclusive write access to image_buffer   ////                                                                           ////  The old_image pointer is set to NULL to avoid possible problems.         ////                                                                           /////////////////////////////////////////////////////////////////////////////////void BufferedSlaveImageSource::keep_image_memory_for_later (Image *&old_image){    if (old_image == NULL)	return;        unsigned int buffer_index;        // find a place to store the old image    for (buffer_index = buffer_size-1; buffer_index >= buffer_entries; buffer_index--)    {	// check whether entry is empty an can hold our image	if (image_buffer[buffer_index] == NULL)	{	    // OK: use the place to store old_image	    image_buffer[buffer_index] = old_image;	    //	    // set old_image to NULL so that we will not access it from two locations	    old_image = NULL;	}		if (old_image == NULL)  // we have stored it	    break;    }        if (old_image != NULL)  // we have not found a place to store old_image    {	// make sure old_image's window is not closed#ifndef NO_DISPLAY#ifdef USE_GL	old_image->set_glwin(NULLWIN);#else#error Tugdual, please put the Qt equivalent or whatever here so that the window is not closed by Image::~Image.  Thanks.#endif#endif  // ifndef NO_DISPLAY	    	delete old_image;	old_image = NULL;    }}/////////////////////////////////////////////////////////////////////////////////                                                                           ////  BufferedSlaveImageSource::get_new_image_memory()                         ////                                                                           ////  Get Image memory reserved earlier by keep_image_memory_for_later()       ////                                                                           ////  This will scan the image_buffer for unused Image pointers.  If one is    ////    found it will be returned and the image_buffer entry cleared (set to   ////    NULL).                                                                 ////                                                                           ////  This method assumes that it has exclusive write access to image_buffer   ////                                                                           ////  Returns the pointer to an unused Image or NULL if none is available.     ////                                                                           /////////////////////////////////////////////////////////////////////////////////Image *BufferedSlaveImageSource::get_new_image_memory(){    unsigned int buffer_index;    Image *image = NULL;        for (buffer_index = buffer_size - 1; buffer_index > buffer_entries; buffer_index--)    {	// check whether entry can be re-used	if (image_buffer[buffer_index] != NULL)	{	    // OK: use the Image* found	    image = image_buffer[buffer_index];	    image_buffer[buffer_index] = NULL;	}		if (image != NULL)  // we have found one	    break;    }        return image;}} // namespace ReadingPeopleTracker

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?