Permalink
Cannot retrieve contributors at this time
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?
ofxDabVideoTracker/src/dab_tracker_image_save_queue.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
96 lines (79 sloc)
2.18 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** \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(); | |
} |