Skip to content
Permalink
16e941848f
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
96 lines (79 sloc) 2.18 KB
/** \file dab_tracker_image_save_queue.h
*/
#include "dab_tracker_image_save_queue.h"
#include "ofxCvColorImage.h"
#include "ofxCvGrayscaleImage.h"
#include "ofxCvFloatImage.h"
#include "ofxCvShortImage.h"
using namespace dab;
using namespace dab::tracker;
ImageSaveQueue::ImageSaveQueue()
{}
ImageSaveQueue::~ImageSaveQueue()
{}
void
ImageSaveQueue::start()
{
if (isThreadRunning() == false) startThread();
}
void
ImageSaveQueue::stop()
{
if (isThreadRunning() == true) stopThread();
}
void
ImageSaveQueue::addImage(ofxCvImage* pImage, const std::string& pFileName)
{
mMutex.lock();
if (dynamic_cast<ofxCvColorImage*>(pImage) != nullptr)
{
ofxCvColorImage* _imageCopy = new ofxCvColorImage(*static_cast<ofxCvColorImage*>(pImage));
mImageQueue.push_back(_imageCopy);
mFileNameQueue.push_back(pFileName);
}
else if (dynamic_cast<ofxCvGrayscaleImage*>(pImage) != nullptr)
{
ofxCvGrayscaleImage* _imageCopy = new ofxCvGrayscaleImage(*static_cast<ofxCvGrayscaleImage*>(pImage));
mImageQueue.push_back(_imageCopy);
mFileNameQueue.push_back(pFileName);
}
else if (dynamic_cast<ofxCvFloatImage*>(pImage) != nullptr)
{
ofxCvFloatImage* _imageCopy = new ofxCvFloatImage(*static_cast<ofxCvFloatImage*>(pImage));
mImageQueue.push_back(_imageCopy);
mFileNameQueue.push_back(pFileName);
}
else if (dynamic_cast<ofxCvShortImage*>(pImage) != nullptr)
{
ofxCvShortImage* _imageCopy = new ofxCvShortImage(*static_cast<ofxCvShortImage*>(pImage));
mImageQueue.push_back(_imageCopy);
mFileNameQueue.push_back(pFileName);
}
mMutex.unlock();
}
void
ImageSaveQueue::threadedFunction()
{
while (isThreadRunning())
{
saveImages();
std::this_thread::sleep_for(std::chrono::microseconds(100));
}
}
void
ImageSaveQueue::saveImages()
{
mMutex.lock();
while (mImageQueue.size() > 0)
{
ofxCvImage* _img = mImageQueue.front();
std::string _fileName = mFileNameQueue.front();
bool success = ofSaveImage(_img->getPixels(), _fileName);
// very hacky, since deleting an image creates a memory leak, scaling the image down to mini size alleviates this problems somewhat
_img->resize(1, 1);
delete _img;
mImageQueue.pop_front();
mFileNameQueue.pop_front();
}
mMutex.unlock();
}