diff --git a/src/Acid.Cam.v2.Linux.Qt.pro b/src/Acid.Cam.v2.Linux.Qt.pro index 674d0ec..07e9152 100644 --- a/src/Acid.Cam.v2.Linux.Qt.pro +++ b/src/Acid.Cam.v2.Linux.Qt.pro @@ -3,7 +3,7 @@ ###################################################################### TEMPLATE = app -TARGET = Acid.Cam.v2.Qt +TARGET = Acid_Cam_v2_Qt QT += core gui widgets DEPENDPATH += . INCLUDEPATH += . /usr/include/ /usr/local/include @@ -13,5 +13,5 @@ QMAKE_CXXFLAGS += -std=c++11 RESOURCES += qresource.qrc # Input -HEADERS += main_window.h new_dialog.h plugin.h qtheaders.h select_image.h ac.h fractal.h -SOURCES += main.cpp main_window.cpp new_dialog.cpp plugin.cpp select_image.cpp ac.cpp fractal.cpp +HEADERS += main_window.h new_dialog.h plugin.h qtheaders.h select_image.h ac.h fractal.h display_window.h playback_window.h +SOURCES += main.cpp main_window.cpp new_dialog.cpp plugin.cpp select_image.cpp ac.cpp fractal.cpp display_window.cpp playback_window.cpp diff --git a/src/display_window.cpp b/src/display_window.cpp new file mode 100644 index 0000000..145c1af --- /dev/null +++ b/src/display_window.cpp @@ -0,0 +1,28 @@ + +#include"display_window.h" + +DisplayWindow::DisplayWindow(QWidget *parent) : QDialog(parent) { + createControls(); + setGeometry(950, 200, 640, 480); + setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint); + setWindowTitle(tr("Acid Cam v2 - Display Window")); + hide(); +} +void DisplayWindow::createControls() { + img_label = new QLabel(this); + img_label->setGeometry(0,0,640, 480); +} +void DisplayWindow::displayImage(const QImage &img) { + QRect src(QPoint(0, 0), size()); + QPixmap p = QPixmap::fromImage(img).scaled(size(),Qt::KeepAspectRatio, Qt::FastTransformation); + QRect dst(QPoint(0,0),p.size()); + dst.moveCenter(src.center()); + img_label->setGeometry(dst); + img_label->setPixmap(p); +} + + +void DisplayWindow::paintEvent(QPaintEvent *) { + QPainter painter(this); + painter.fillRect(QRect(QPoint(0, 0), size()), QColor(0,0,0)); +} diff --git a/src/display_window.h b/src/display_window.h new file mode 100644 index 0000000..5f8582e --- /dev/null +++ b/src/display_window.h @@ -0,0 +1,17 @@ +#ifndef __DISPLAY_WINDOW_H__ +#define __DISPLAY_WINDOW_H__ + +#include"qtheaders.h" + +class DisplayWindow : public QDialog { + Q_OBJECT +public: + DisplayWindow(QWidget *parent = 0); + void createControls(); + void displayImage(const QImage &img); + void paintEvent(QPaintEvent *paint); +private: + QLabel *img_label; +}; + +#endif diff --git a/src/main_window.cpp b/src/main_window.cpp index 8f92a9d..f1f4488 100644 --- a/src/main_window.cpp +++ b/src/main_window.cpp @@ -31,7 +31,6 @@ void generate_map() { std::string name = "plugin " + plugins.plugin_list[j]->name(); filter_map[name] = std::make_pair(2, j); } - } void custom_filter(cv::Mat &) { @@ -39,195 +38,11 @@ void custom_filter(cv::Mat &) { } -Playback::Playback(QObject *parent) : QThread(parent) { - stop = true; - isStep = false; - isPaused = false; -} - -void Playback::Play() { - if(!isRunning()) { - if(isStopped()) { - stop = false; - } - } - start(LowPriority); -} - -void Playback::setVideo(cv::VideoCapture cap, cv::VideoWriter wr, bool record) { - mutex.lock(); - capture = cap; - writer = wr; - recording = record; - if(capture.isOpened()) { - frame_rate = (int) capture.get(CV_CAP_PROP_FPS); - if(frame_rate <= 0) frame_rate = 24; - } - mutex.unlock(); -} - -void Playback::setVector(std::vector> v) { - mutex_add.lock(); - current = v; - mutex_add.unlock(); -} - -void Playback::setOptions(bool n, int c) { - mutex.lock(); - ac::isNegative = n; - negate = n; - reverse = c; - ac::color_order = c; - ac::in_custom = true; - mutex.unlock(); -} - -void Playback::setDisplayed(bool shown) { - mutex_shown.lock(); - video_shown = shown; - mutex_shown.unlock(); -} - -void Playback::run() { - while(!stop) { - mutex.lock(); - if(!capture.read(frame)) { - stop = true; - mutex.unlock(); - emit stopRecording(); - return; - } - - static std::vector> cur; - mutex_shown.lock(); - cur = current; - mutex_shown.unlock(); - - - ac::orig_frame = frame.clone(); - if(cur.size()>0) { - ac::in_custom = true; - for(unsigned int i = 0; i < cur.size(); ++i) { - if(i == cur.size()-1) - ac::in_custom = false; - - if(cur[i].first == 0) { - ac::draw_func[cur[i].second](frame); - } else if(cur[i].first == 1) { - current_filterx = cur[i].second; - ac::alphaFlame(frame); - } else if(cur[i].first == 2) { - draw_plugin(frame, cur[i].second); - } - } - } - if(recording && writer.isOpened()) { - writer.write(frame); - } - mutex.unlock(); - bool shown_var; - mutex_shown.lock(); - shown_var = video_shown; - mutex_shown.unlock(); - - if(shown_var == true) { - if(frame.channels()==3) { - cv::cvtColor(frame, rgb_frame, CV_BGR2RGB); - img = QImage((const unsigned char*)(rgb_frame.data), rgb_frame.cols, rgb_frame.rows, QImage::Format_RGB888); - } else { - img = QImage((const unsigned char*)(frame.data), frame.cols, frame.rows, QImage::Format_Indexed8); - } - emit procImage(img); - if(isStep == true) { - isStep = false; - return; - } - } else { - emit frameIncrement(); - } - } -} - -Playback::~Playback() { - mutex.lock(); - stop = true; -#if defined(__linux__) || defined(__APPLE__) - condition.wakeOne(); -#endif - mutex.unlock(); -#if defined(__linux__) || defined(__APPLE__) - wait(); -#endif -} - -void Playback::Stop() { - mutex.lock(); - stop = true; - mutex.unlock(); -} - -void Playback::Release() { - mutex.lock(); - stop = true; - if(capture.isOpened()) capture.release(); - if(writer.isOpened()) writer.release(); - mutex.unlock(); -} - -void Playback::msleep(int ms) { - QThread::msleep(ms); -} - -bool Playback::isStopped() const { - return this->stop; -} - -void Playback::setStep() { - mutex.lock(); - isStep = true; - mutex.unlock(); -} - -void Playback::setImage(const cv::Mat &frame) { - mutex.lock(); - blend_set = true; - blend_image = frame; - mutex.unlock(); -} - -DisplayWindow::DisplayWindow(QWidget *parent) : QDialog(parent) { - createControls(); - setGeometry(950, 200, 640, 480); - setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint); - setWindowTitle(tr("Acid Cam v2 - Display Window")); - hide(); -} -void DisplayWindow::createControls() { - img_label = new QLabel(this); - img_label->setGeometry(0,0,640, 480); -} -void DisplayWindow::displayImage(const QImage &img) { - QRect src(QPoint(0, 0), size()); - QPixmap p = QPixmap::fromImage(img).scaled(size(),Qt::KeepAspectRatio, Qt::FastTransformation); - QRect dst(QPoint(0,0),p.size()); - dst.moveCenter(src.center()); - img_label->setGeometry(dst); - img_label->setPixmap(p); -} - - -void DisplayWindow::paintEvent(QPaintEvent *) { - QPainter painter(this); - painter.fillRect(QRect(QPoint(0, 0), size()), QColor(0,0,0)); -} - - AC_MainWindow::~AC_MainWindow() { - controls_Stop(); - delete playback; } + AC_MainWindow::AC_MainWindow(QWidget *parent) : QMainWindow(parent) { programMode = MODE_CAMERA; init_plugins(); diff --git a/src/main_window.h b/src/main_window.h index 44bd1c7..c3a797e 100644 --- a/src/main_window.h +++ b/src/main_window.h @@ -3,55 +3,8 @@ #include "qtheaders.h" #include "new_dialog.h" - -class Playback : public QThread { -Q_OBJECT -private: - bool stop; - QMutex mutex,mutex_shown,mutex_add; - QWaitCondition condition; - cv::Mat frame; - int frame_rate; - bool recording; - cv::VideoCapture capture; - cv::VideoWriter writer; - cv::Mat rgb_frame; - QImage img; - std::vector> current; - bool isPaused, isStep; - bool video_shown; -public: - Playback(QObject *parent = 0); - ~Playback(); - void Play(); - void Stop(); - void Release(); - void setVideo(cv::VideoCapture cap, cv::VideoWriter writer, bool record); - bool isStopped() const; - void run(); - void msleep(int ms); - void setVector(std::vector> s); - void setOptions(bool n, int c); - void setImage(const cv::Mat &image); - void setStep(); - void setDisplayed(bool shown); -signals: - void procImage(const QImage image); - void stopRecording(); - void frameIncrement(); - -}; - -class DisplayWindow : public QDialog { - Q_OBJECT -public: - DisplayWindow(QWidget *parent = 0); - void createControls(); - void displayImage(const QImage &img); - void paintEvent(QPaintEvent *paint); -private: - QLabel *img_label; -}; +#include "display_window.h" +#include "playback_window.h" enum VideoMode { MODE_CAMERA = 0, MODE_VIDEO }; diff --git a/src/playback_window.cpp b/src/playback_window.cpp new file mode 100644 index 0000000..5cc4e28 --- /dev/null +++ b/src/playback_window.cpp @@ -0,0 +1,157 @@ +#include"playback_window.h" + +Playback::Playback(QObject *parent) : QThread(parent) { + stop = true; + isStep = false; + isPaused = false; +} + +void Playback::Play() { + if(!isRunning()) { + if(isStopped()) { + stop = false; + } + } + start(LowPriority); +} + +void Playback::setVideo(cv::VideoCapture cap, cv::VideoWriter wr, bool record) { + mutex.lock(); + capture = cap; + writer = wr; + recording = record; + if(capture.isOpened()) { + frame_rate = (int) capture.get(CV_CAP_PROP_FPS); + if(frame_rate <= 0) frame_rate = 24; + } + mutex.unlock(); +} + +void Playback::setVector(std::vector> v) { + mutex_add.lock(); + current = v; + mutex_add.unlock(); +} + +void Playback::setOptions(bool n, int c) { + mutex.lock(); + ac::isNegative = n; + negate = n; + reverse = c; + ac::color_order = c; + ac::in_custom = true; + mutex.unlock(); +} + +void Playback::setDisplayed(bool shown) { + mutex_shown.lock(); + video_shown = shown; + mutex_shown.unlock(); +} + +void Playback::run() { + while(!stop) { + mutex.lock(); + if(!capture.read(frame)) { + stop = true; + mutex.unlock(); + emit stopRecording(); + return; + } + + static std::vector> cur; + mutex_shown.lock(); + cur = current; + mutex_shown.unlock(); + + + ac::orig_frame = frame.clone(); + if(cur.size()>0) { + ac::in_custom = true; + for(unsigned int i = 0; i < cur.size(); ++i) { + if(i == cur.size()-1) + ac::in_custom = false; + + if(cur[i].first == 0) { + ac::draw_func[cur[i].second](frame); + } else if(cur[i].first == 1) { + current_filterx = cur[i].second; + ac::alphaFlame(frame); + } else if(cur[i].first == 2) { + draw_plugin(frame, cur[i].second); + } + } + } + if(recording && writer.isOpened()) { + writer.write(frame); + } + mutex.unlock(); + bool shown_var; + mutex_shown.lock(); + shown_var = video_shown; + mutex_shown.unlock(); + + if(shown_var == true) { + if(frame.channels()==3) { + cv::cvtColor(frame, rgb_frame, CV_BGR2RGB); + img = QImage((const unsigned char*)(rgb_frame.data), rgb_frame.cols, rgb_frame.rows, QImage::Format_RGB888); + } else { + img = QImage((const unsigned char*)(frame.data), frame.cols, frame.rows, QImage::Format_Indexed8); + } + emit procImage(img); + if(isStep == true) { + isStep = false; + return; + } + } else { + emit frameIncrement(); + } + } +} + +Playback::~Playback() { + mutex.lock(); + stop = true; +#if defined(__linux__) || defined(__APPLE__) + condition.wakeOne(); +#endif + mutex.unlock(); +#if defined(__linux__) || defined(__APPLE__) + wait(); +#endif +} + +void Playback::Stop() { + mutex.lock(); + stop = true; + mutex.unlock(); +} + +void Playback::Release() { + mutex.lock(); + stop = true; + if(capture.isOpened()) capture.release(); + if(writer.isOpened()) writer.release(); + mutex.unlock(); +} + +void Playback::msleep(int ms) { + QThread::msleep(ms); +} + +bool Playback::isStopped() const { + return this->stop; +} + +void Playback::setStep() { + mutex.lock(); + isStep = true; + mutex.unlock(); +} + +void Playback::setImage(const cv::Mat &frame) { + mutex.lock(); + blend_set = true; + blend_image = frame; + mutex.unlock(); +} diff --git a/src/playback_window.h b/src/playback_window.h new file mode 100644 index 0000000..94bac60 --- /dev/null +++ b/src/playback_window.h @@ -0,0 +1,44 @@ +#ifndef __PLAYBACK_WINDOW_H__ +#define __PLAYBACK_WINDOW_H__ + +#include "qtheaders.h" + +class Playback : public QThread { + Q_OBJECT +private: + bool stop; + QMutex mutex,mutex_shown,mutex_add; + QWaitCondition condition; + cv::Mat frame; + int frame_rate; + bool recording; + cv::VideoCapture capture; + cv::VideoWriter writer; + cv::Mat rgb_frame; + QImage img; + std::vector> current; + bool isPaused, isStep; + bool video_shown; +public: + Playback(QObject *parent = 0); + ~Playback(); + void Play(); + void Stop(); + void Release(); + void setVideo(cv::VideoCapture cap, cv::VideoWriter writer, bool record); + bool isStopped() const; + void run(); + void msleep(int ms); + void setVector(std::vector> s); + void setOptions(bool n, int c); + void setImage(const cv::Mat &image); + void setStep(); + void setDisplayed(bool shown); +signals: + void procImage(const QImage image); + void stopRecording(); + void frameIncrement(); + +}; + +#endif