-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
269 lines (218 loc) · 6.92 KB
/
mainwindow.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QImageWriter>
#include <QByteArray>
#include <QBitArray>
#include <QFileDialog>
#include <bitset>
#include <vector>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->spinBoxPlane->setValue(4);
ui->spinBoxPlane->setEnabled(false);
ui->btnOpenRAWImage->setEnabled(false);
ui->btnSaveImage->setEnabled(false);
}
MainWindow::~MainWindow()
{
delete ui;
}
QString MainWindow::Converter(QByteArray line)
{
QBitArray bits(line.count()*8);
// Convert from QByteArray to QBitArray
for(int i=0; i<line.count(); ++i)
{
for(int b=0; b<8;b++)
{
bits.setBit( i*8+b, line.at(i)&(1<<(7-b)));
}
}
// https://stackoverflow.com/questions/46101782/qt-how-do-i-convert-qbitarray-to-qstring
QString arrStr = "";
for(int i = 0; i<bits.size(); i++)
{
// https://stackoverflow.com/questions/3211771/how-to-convert-int-to-qstring
arrStr += QString::number(bits.at(i));
}
return arrStr;
}
void MainWindow::ClearImage()
{
plane1.clear();
plane2.clear();
plane3.clear();
plane4.clear();
dortlu.clear();
}
QByteArray MainWindow::OpenImage()
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open RAW file"), "", tr("RAW Files (*.*)"));
file = new QFile(fileName);
QFileInfo fileinfo(fileName);
// https://forum.qt.io/topic/79053/how-can-i-read-large-files-to-qbytearray
if(!file->open(QIODevice::ReadOnly))
{
return "";
}
int fileSize = fileinfo.size() * 2; // TODO: why 2?
qInfo() << fileSize << '\n';
for(int i = 1; i <= fileSize; ++i)
{
if(fileSize % i == 0)
{
vect.push_back(std::make_pair(i, fileSize / i));
}
}
for (auto i = vect.begin(); i != vect.end(); ++i)
{
ui->comboBoxResolution->addItem(QString::number(std::get<0>(*i)) + " x " + QString::number(std::get<1>(*i)) + " = " + QString::number(fileSize));
qInfo() << std::get<0>(*i) << " x " << std::get<1>(*i) << " = " << fileSize << endl;
}
QByteArray line = file->readAll();
return line;
}
void MainWindow::DrawImage()
{
bool ok;
int i = 0;
while(i < imgX * imgY * ui->spinBoxPlane->value())
{
//QString str1 = hadi.mid(i, 1);
//QString str2 = hadi.mid(32 * 34 + i, 1);
//QString str3 = hadi.mid(32 * 34 + 32 * 34 + i, 1);
//QString str4 = hadi.mid(32 * 34 + 32 * 34 + 32 * 34 +i, 1);
QString str1 = hadi.mid(i, 1);
QString str2 = hadi.mid(imgX * imgY + i, 1);
QString str3 = hadi.mid(imgX * imgY + imgX * imgY + i, 1);
QString str4 = hadi.mid(imgX * imgY + imgX * imgY + imgX * imgY +i, 1);
//QString string = str1 + str2 + str3 + str4;
QString string = str4 + str3 + str2 + str1;
int ival = string.toInt(&ok, 2);
int iVal1 = str1.toInt(&ok,2);
int iVal2 = str2.toInt(&ok,2);
int iVal3 = str3.toInt(&ok,2);
int iVal4 = str4.toInt(&ok,2);
dortlu.push_back(ival);
plane1.push_back(iVal1);
plane2.push_back(iVal2);
plane3.push_back(iVal3);
plane4.push_back(iVal4);
i = i + 1;
}
//QImage image(32, 34, QImage::Format_ARGB32);
image = new QImage(imgX, imgY, QImage::Format_ARGB32);
int l = 0;
int x = 0;
int y = 0;
do
{
//image.setPixel(x, y, qRgba(red.at(l)*255 * 0.33, green.at(l)*255*0.6, blue.at(l)*255*0.11, alpha.at(l)*255));
//QColor renk = QColor::fromCmyk(red.at(l)*255, green.at(l)*255, blue.at(l)*255, alpha.at(l)*255);
//image.setPixel(x,y,renk.rgba());
//image.setPixel(x, y, qRgb(dortlu.at(l)*255 * 0.33, dortlu.at(l)*255*0.6, dortlu.at(l)*255*0.11));
if (dortlu.at(l) == 0)
image->setPixel(x, y, qRgba(red.at(dortlu.at(l))*17, green.at(dortlu.at(l))*17, blue.at(dortlu.at(l))*17, 0 ));
else
image->setPixel(x, y, qRgba(red.at(dortlu.at(l))*17, green.at(dortlu.at(l))*17, blue.at(dortlu.at(l))*17, 255 ));
x = x + 1;
if (x == imgX)
{
x = 0;
y = y +1;
}
l = l + 1;
} while(l <= imgX * imgY);
QPixmap a;
a.convertFromImage(*image);
ui->labelImage->setPixmap(a.scaled(ui->labelImage->width(), ui->labelImage->height(), Qt::KeepAspectRatio));
}
void MainWindow::on_btnOpenRAWImage_clicked()
{
ClearImage();
hadi = Converter(OpenImage());
DrawImage();
ui->btnSaveImage->setEnabled(true);
}
void MainWindow::on_btnSaveImage_clicked()
{
QString fileName = QFileDialog::getSaveFileName(this,
tr("Save Chunky Image"), "",
tr("Portable Network Graphics (*.png)"));
QImageWriter writer(fileName);
writer.setFormat("png");
writer.write(*image);
}
void MainWindow::ClearPalette()
{
red.clear();
green.clear();
blue.clear();
alpha.clear();
}
void MainWindow::on_btnOpenPalette_clicked()
{
ClearPalette();
bool ok;
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Palette File"), "", tr("All Files (*)"));
paletteFile = new QFile(fileName);
QFileInfo fileinfo(fileName);
// https://forum.qt.io/topic/79053/how-can-i-read-large-files-to-qbytearray
if(!paletteFile->open(QIODevice::ReadOnly))
{
return;
}
QByteArray line = paletteFile->readAll();
QString palet_xxx = Converter(line);
int palet_i = 4;
while(palet_i < 32*8)
{
QString str1 = palet_xxx.mid(palet_i, 4);
QString str2 = palet_xxx.mid(palet_i + 4, 4);
QString str3 = palet_xxx.mid(palet_i + 4 + 4, 4);
QString str4 = palet_xxx.mid(palet_i + 4 + 4 + 4, 4);
int iVal1 = str1.toInt(&ok,2);
int iVal2 = str2.toInt(&ok,2);
int iVal3 = str3.toInt(&ok,2);
int iVal4 = str4.toInt(&ok,2);
red.push_back(iVal1);
green.push_back(iVal2);
blue.push_back(iVal3);
alpha.push_back(iVal4);
qInfo() << palet_i / 12 << iVal1*17 << " " << iVal2*17 << " " << iVal3*17 << iVal4*17 << '\n';
palet_i = palet_i + 16;
}
int x = 0;
int y = 0;
image2 = new QImage(4, 4, QImage::Format_RGB32);
for (int i = 0; i < 16; ++i)
{
image2->setPixel(x, y, qRgb(red.at(i)*17, green.at(i)*17, blue.at(i)*17));
x = x + 1;
if (x == 4)
{
x = 0;
y = y +1;
}
}
QPixmap a2;
a2.convertFromImage(*image2);
ui->labelPalatte->setPixmap(a2.scaled(ui->labelPalatte->width(), ui->labelPalatte->height(), Qt::KeepAspectRatio));
if(hadi != "")
{
DrawImage();
}
ui->btnOpenRAWImage->setEnabled(true);
}
void MainWindow::on_comboBoxResolution_currentIndexChanged(int index)
{
imgX = vect.at(index).first;
imgY = vect.at(index).second;
DrawImage();
}