-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAllRgbWidget.cpp
154 lines (126 loc) · 4.6 KB
/
AllRgbWidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "AllRgbWidget.h"
#include <QPainter>
#include <QFileDialog>
#include <QMessageBox>
#include <cstdlib>
#include <vector>
#include <memory>
AllRgbWidget::AllRgbWidget(QWidget *parent)
: PanAndZoomWidget{parent}
{
onResetPixelsRequested();
getUpdateTimer_.setSingleShot(false);
getUpdateTimer_.setInterval(1000);
}
void AllRgbWidget::SetTargetImage(const QImage& targetImage)
{
if (targetImage != target_) {
target_ = targetImage;
targetThumbnail_ = target_.scaled(100, 100);
emit onTargetUpdated(target_);
update();
}
}
void AllRgbWidget::SetAlgorithm(std::unique_ptr<AlgorithmBase>&& algoritm)
{
algorithm_ = std::move(algoritm);
algorithm_->SetTargetImage(target_);
algorithm_->SetRgbImage(allRgb_);
connect(&getUpdateTimer_, &QTimer::timeout, algorithm_.get(), &AlgorithmBase::onGetUpdateRequested, Qt::QueuedConnection);
connect(this, &AllRgbWidget::onTargetUpdated, algorithm_.get(), &AlgorithmBase::SetTargetImage, Qt::QueuedConnection);
connect(this, &AllRgbWidget::onAllRgbUpdated, algorithm_.get(), &AlgorithmBase::SetRgbImage, Qt::QueuedConnection);
connect(algorithm_.get(), &AlgorithmBase::onIterationsChanged, this, &AllRgbWidget::onIterationsChanged, Qt::QueuedConnection);
connect(algorithm_.get(), &AlgorithmBase::onImprovementsChanged, this, &AllRgbWidget::onImprovementsChanged, Qt::QueuedConnection);
connect(algorithm_.get(), &AlgorithmBase::onUpdated, this, &AllRgbWidget::onUpdateFromAlgorithm, Qt::QueuedConnection);
}
QImage AllRgbWidget::GetAllRgbImage() const
{
return allRgb_;
}
void AllRgbWidget::onResetPixelsRequested()
{
allRgb_ = QImage(4096, 4096, QImage::Format::Format_RGB32);
ForEachPixel(allRgb_, [](QImage& target, int x, int y)
{
target.setPixel(x, y, x + (target.width() * y));
});
emit onAllRgbUpdated(allRgb_);
}
void AllRgbWidget::onLoadPixelsRequested()
{
QString fileName = QFileDialog::getOpenFileName(this, "Select Image", "", "Images (*.png *.xpm *.jpg)");
QImage newPixels(fileName);
if (newPixels.width() != 4096 || newPixels.height() != 4096) {
QMessageBox::warning(this, "Invalid allRGB image", QString("%1 is not the correct size (4096 * 4096)").arg(fileName));
return;
}
// Allocate a count per pixel
std::vector<qulonglong> colourCounts(0x00FFFFFF, 0);
ForEachPixel(newPixels, [&colourCounts](QImage& source, int x, int y) mutable -> void
{
colourCounts[0x00FFFFFF & source.pixel(x, y)]++;
});
for (qulonglong colourCount : colourCounts) {
if (colourCount != 1) {
QString uniqueColourCount = QLocale::system().toString(std::ranges::count(colourCounts, 1));
QString expectedUniqueColourCount = QLocale::system().toString(4096u * 4096u);
QMessageBox::warning(this, "Invalid allRGB image", QString("%1 only contains %2 out of %3 colours)").arg(fileName, uniqueColourCount, expectedUniqueColourCount));
return;
}
}
emit onAllRgbUpdated(newPixels);
}
void AllRgbWidget::onResetViewRequested()
{
qreal edge = std::min(width(), height());
qreal scale = edge / 4096;
SetScale(scale);
ResetTranslation();
ResetRotation();
}
void AllRgbWidget::onStartRequested()
{
if (target_.isNull()) {
QMessageBox::warning(this, "No Target", "Please click 'Update Target', or go to the other tab to load and set a target image.");
return;
}
algorithm_->onStartRequested();
}
void AllRgbWidget::onStopRequested()
{
algorithm_->onStopRequested();
}
void AllRgbWidget::onSaveImageRequested()
{
QString fileName = QFileDialog::getSaveFileName(this, "Select Image", "", "Images (*.png *.xpm *.jpg)");
allRgb_.save(fileName);
}
void AllRgbWidget::paintEvent(QPaintEvent* /*event*/)
{
QPainter p(this);
p.save();
// centre the image around the origin
TransformPainter(p);
p.drawImage(QRectF(allRgb_.rect()).translated(allRgb_.rect().width() / -2, allRgb_.height() / -2), allRgb_, QRectF(allRgb_.rect()));
p.restore();
// draw thumbnail of target
p.drawImage(targetThumbnail_.rect(), targetThumbnail_, targetThumbnail_.rect());
}
void AllRgbWidget::showEvent(QShowEvent* /*event*/)
{
onResetViewRequested();
getUpdateTimer_.start();
}
void AllRgbWidget::onUpdateFromAlgorithm(QImage allRgb)
{
allRgb_ = allRgb.copy();
update();
}
void AllRgbWidget::ForEachPixel(QImage& target, const std::function<void (QImage&, int, int)>& action)
{
for (int x = 0; x < target.width(); ++x) {
for (int y = 0; y < target.height(); ++y) {
action(target, x, y);
}
}
}