Skip to content

Commit

Permalink
#1: Allow image rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
PropaneDragon committed Apr 14, 2019
1 parent ea00b39 commit 8f6b84c
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 27 deletions.
53 changes: 36 additions & 17 deletions HueEntertainmentCentre/GeneratedFiles/moc_mainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

QT_BEGIN_MOC_NAMESPACE
struct qt_meta_stringdata_MainWindow_t {
QByteArrayData data[11];
char stringdata0[182];
QByteArrayData data[16];
char stringdata0[271];
};
#define QT_MOC_LITERAL(idx, ofs, len) \
Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(len, \
Expand All @@ -39,14 +39,21 @@ QT_MOC_LITERAL(6, 86, 15), // "connectToCamera"
QT_MOC_LITERAL(7, 102, 20), // "disconnectFromBridge"
QT_MOC_LITERAL(8, 123, 20), // "disconnectFromCamera"
QT_MOC_LITERAL(9, 144, 27), // "changeImageUpdatePreference"
QT_MOC_LITERAL(10, 172, 9) // "canUpdate"
QT_MOC_LITERAL(10, 172, 9), // "canUpdate"
QT_MOC_LITERAL(11, 182, 20), // "rotateImageClockwise"
QT_MOC_LITERAL(12, 203, 24), // "rotateImageAntiClockwise"
QT_MOC_LITERAL(13, 228, 19), // "flipImageHorizontal"
QT_MOC_LITERAL(14, 248, 4), // "flip"
QT_MOC_LITERAL(15, 253, 17) // "flipImageVertical"

},
"MainWindow\0captureTimerUpdated\0\0"
"connectToNewBridge\0connectToNewCamera\0"
"connectToBridge\0connectToCamera\0"
"disconnectFromBridge\0disconnectFromCamera\0"
"changeImageUpdatePreference\0canUpdate"
"changeImageUpdatePreference\0canUpdate\0"
"rotateImageClockwise\0rotateImageAntiClockwise\0"
"flipImageHorizontal\0flip\0flipImageVertical"
};
#undef QT_MOC_LITERAL

Expand All @@ -56,22 +63,26 @@ static const uint qt_meta_data_MainWindow[] = {
7, // revision
0, // classname
0, 0, // classinfo
8, 14, // methods
12, 14, // methods
0, 0, // properties
0, 0, // enums/sets
0, 0, // constructors
0, // flags
0, // signalCount

// slots: name, argc, parameters, tag, flags
1, 0, 54, 2, 0x09 /* Protected */,
3, 0, 55, 2, 0x09 /* Protected */,
4, 0, 56, 2, 0x09 /* Protected */,
5, 0, 57, 2, 0x09 /* Protected */,
6, 0, 58, 2, 0x09 /* Protected */,
7, 0, 59, 2, 0x09 /* Protected */,
8, 0, 60, 2, 0x09 /* Protected */,
9, 1, 61, 2, 0x09 /* Protected */,
1, 0, 74, 2, 0x09 /* Protected */,
3, 0, 75, 2, 0x09 /* Protected */,
4, 0, 76, 2, 0x09 /* Protected */,
5, 0, 77, 2, 0x09 /* Protected */,
6, 0, 78, 2, 0x09 /* Protected */,
7, 0, 79, 2, 0x09 /* Protected */,
8, 0, 80, 2, 0x09 /* Protected */,
9, 1, 81, 2, 0x09 /* Protected */,
11, 0, 84, 2, 0x09 /* Protected */,
12, 0, 85, 2, 0x09 /* Protected */,
13, 1, 86, 2, 0x09 /* Protected */,
15, 1, 89, 2, 0x09 /* Protected */,

// slots: parameters
QMetaType::Void,
Expand All @@ -82,6 +93,10 @@ static const uint qt_meta_data_MainWindow[] = {
QMetaType::Void,
QMetaType::Void,
QMetaType::Void, QMetaType::Bool, 10,
QMetaType::Void,
QMetaType::Void,
QMetaType::Void, QMetaType::Bool, 14,
QMetaType::Void, QMetaType::Bool, 14,

0 // eod
};
Expand All @@ -100,6 +115,10 @@ void MainWindow::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id,
case 5: _t->disconnectFromBridge(); break;
case 6: _t->disconnectFromCamera(); break;
case 7: _t->changeImageUpdatePreference((*reinterpret_cast< bool(*)>(_a[1]))); break;
case 8: _t->rotateImageClockwise(); break;
case 9: _t->rotateImageAntiClockwise(); break;
case 10: _t->flipImageHorizontal((*reinterpret_cast< bool(*)>(_a[1]))); break;
case 11: _t->flipImageVertical((*reinterpret_cast< bool(*)>(_a[1]))); break;
default: ;
}
}
Expand Down Expand Up @@ -134,13 +153,13 @@ int MainWindow::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
if (_id < 0)
return _id;
if (_c == QMetaObject::InvokeMetaMethod) {
if (_id < 8)
if (_id < 12)
qt_static_metacall(this, _c, _id, _a);
_id -= 8;
_id -= 12;
} else if (_c == QMetaObject::RegisterMethodArgumentMetaType) {
if (_id < 8)
if (_id < 12)
*reinterpret_cast<int*>(_a[0]) = -1;
_id -= 8;
_id -= 12;
}
return _id;
}
Expand Down
22 changes: 20 additions & 2 deletions HueEntertainmentCentre/GeneratedFiles/ui_mainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class Ui_MainWindow
QAction *actionUpdate_image;
QAction *actionRotate_90_clockwise;
QAction *actionRotate_90_anti_clockwise;
QAction *actionFlip_horizontal;
QAction *actionFlip_vertical;
QWidget *centralwidget;
QVBoxLayout *verticalLayout;
QWidget *widget;
Expand Down Expand Up @@ -89,10 +91,16 @@ class Ui_MainWindow
actionUpdate_image->setChecked(true);
actionRotate_90_clockwise = new QAction(MainWindow);
actionRotate_90_clockwise->setObjectName(QStringLiteral("actionRotate_90_clockwise"));
actionRotate_90_clockwise->setEnabled(false);
actionRotate_90_clockwise->setEnabled(true);
actionRotate_90_anti_clockwise = new QAction(MainWindow);
actionRotate_90_anti_clockwise->setObjectName(QStringLiteral("actionRotate_90_anti_clockwise"));
actionRotate_90_anti_clockwise->setEnabled(false);
actionRotate_90_anti_clockwise->setEnabled(true);
actionFlip_horizontal = new QAction(MainWindow);
actionFlip_horizontal->setObjectName(QStringLiteral("actionFlip_horizontal"));
actionFlip_horizontal->setCheckable(true);
actionFlip_vertical = new QAction(MainWindow);
actionFlip_vertical->setObjectName(QStringLiteral("actionFlip_vertical"));
actionFlip_vertical->setCheckable(true);
centralwidget = new QWidget(MainWindow);
centralwidget->setObjectName(QStringLiteral("centralwidget"));
verticalLayout = new QVBoxLayout(centralwidget);
Expand Down Expand Up @@ -149,8 +157,12 @@ class Ui_MainWindow
menuCamera->addSeparator();
menuCamera->addAction(actionChangeCamera);
menuDisplay->addAction(actionUpdate_image);
menuDisplay->addSeparator();
menuDisplay->addAction(actionRotate_90_clockwise);
menuDisplay->addAction(actionRotate_90_anti_clockwise);
menuDisplay->addSeparator();
menuDisplay->addAction(actionFlip_horizontal);
menuDisplay->addAction(actionFlip_vertical);
menuSettings->addAction(actionOptions);

retranslateUi(MainWindow);
Expand All @@ -162,6 +174,10 @@ class Ui_MainWindow
QObject::connect(actionChangeHub, SIGNAL(triggered()), MainWindow, SLOT(connectToNewBridge()));
QObject::connect(actionChangeCamera, SIGNAL(triggered()), MainWindow, SLOT(connectToNewCamera()));
QObject::connect(actionUpdate_image, SIGNAL(toggled(bool)), MainWindow, SLOT(changeImageUpdatePreference(bool)));
QObject::connect(actionRotate_90_clockwise, SIGNAL(triggered()), MainWindow, SLOT(rotateImageClockwise()));
QObject::connect(actionRotate_90_anti_clockwise, SIGNAL(triggered()), MainWindow, SLOT(rotateImageAntiClockwise()));
QObject::connect(actionFlip_horizontal, SIGNAL(toggled(bool)), MainWindow, SLOT(flipImageHorizontal(bool)));
QObject::connect(actionFlip_vertical, SIGNAL(toggled(bool)), MainWindow, SLOT(flipImageVertical(bool)));

QMetaObject::connectSlotsByName(MainWindow);
} // setupUi
Expand All @@ -180,6 +196,8 @@ class Ui_MainWindow
actionUpdate_image->setText(QApplication::translate("MainWindow", "Update image", Q_NULLPTR));
actionRotate_90_clockwise->setText(QApplication::translate("MainWindow", "Rotate 90\302\260 clockwise", Q_NULLPTR));
actionRotate_90_anti_clockwise->setText(QApplication::translate("MainWindow", "Rotate 90\302\260 anti-clockwise", Q_NULLPTR));
actionFlip_horizontal->setText(QApplication::translate("MainWindow", "Flip horizontal", Q_NULLPTR));
actionFlip_vertical->setText(QApplication::translate("MainWindow", "Flip vertical", Q_NULLPTR));
label_cameraImage->setText(QString());
menuFile->setTitle(QApplication::translate("MainWindow", "File", Q_NULLPTR));
menuHuib->setTitle(QApplication::translate("MainWindow", "Hub", Q_NULLPTR));
Expand Down
58 changes: 52 additions & 6 deletions HueEntertainmentCentre/Qt/mainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,18 @@ void MainWindow::connectToGroup(std::shared_ptr<huestream::Group> group)
void MainWindow::processImage(const QImage &image)
{
if (!image.isNull() && _stream) {
auto size = image.size();

auto transformedImage = image;
auto centre = transformedImage.rect().center();

QMatrix rotationMatrix;
rotationMatrix.translate(centre.x(), centre.y());
rotationMatrix.rotate(_imageRotation);
rotationMatrix.scale(_imageFlippedVertically ? -1 : 1, _imageFlippedHorizontally ? -1 : 1);

transformedImage = transformedImage.transformed(rotationMatrix, Qt::TransformationMode::FastTransformation);

auto size = transformedImage.size();

std::vector<ColourArea> areas = {
ColourArea(_capture, { huestream::Area::Left, huestream::Area::FrontLeft, huestream::Area::BackLeft, huestream::Area::CenterLeft }, Area(0, 0, 25, 0)),
Expand All @@ -110,7 +121,7 @@ void MainWindow::processImage(const QImage &image)

for (auto y = 0; y < size.height(); ++y) {
for (auto x = 0; x < size.width(); ++x) {
auto pixelColour = image.pixelColor(QPoint(x, y));
auto pixelColour = transformedImage.pixelColor(QPoint(x, y));

for (auto area : areas) {
if (area.pointIsInside(x, y)) {
Expand All @@ -134,6 +145,10 @@ void MainWindow::processImage(const QImage &image)
}

_stream->UnlockMixer();

if (_imageAllowedToUpdate) {
label_cameraImage->setPixmap(QPixmap::fromImage(transformedImage));
}
}
}

Expand Down Expand Up @@ -161,10 +176,6 @@ void MainWindow::captureTimerUpdated()
if (!image.isNull()) {

processImage(image);

if (_imageAllowedToUpdate) {
label_cameraImage->setPixmap(QPixmap::fromImage(image));
}
}
}
}
Expand Down Expand Up @@ -226,3 +237,38 @@ void MainWindow::changeImageUpdatePreference(bool canUpdate)
{
_imageAllowedToUpdate = canUpdate;
}

void MainWindow::rotateImageClockwise()
{
rotateImage(90);
}

void MainWindow::rotateImageAntiClockwise()
{
rotateImage(-90);
}

void MainWindow::flipImageHorizontal(bool flip)
{
_imageFlippedHorizontally = flip;
}

void MainWindow::flipImageVertical(bool flip)
{
_imageFlippedVertically = flip;
}

void MainWindow::rotateImage(int degrees)
{
auto newRotation = _imageRotation + degrees;

while (newRotation >= 360) {
newRotation -= 360;
}

while (newRotation < 0) {
newRotation += 360;
}

_imageRotation = newRotation;
}
10 changes: 10 additions & 0 deletions HueEntertainmentCentre/Qt/mainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,22 @@ protected slots:
void disconnectFromBridge();
void disconnectFromCamera();
void changeImageUpdatePreference(bool canUpdate);
void rotateImageClockwise();
void rotateImageAntiClockwise();
void flipImageHorizontal(bool flip);
void flipImageVertical(bool flip);

protected:
void rotateImage(int degrees);

private:
bool _connected = false;
bool _imageAllowedToUpdate = true;
bool _imageFlippedHorizontally = false;
bool _imageFlippedVertically = false;

int _targetFramerate = 10;
int _imageRotation = 0;

QDateTime _lastRequestTime = QDateTime::currentDateTime();

Expand Down
Loading

0 comments on commit 8f6b84c

Please sign in to comment.