-
Notifications
You must be signed in to change notification settings - Fork 0
/
saveimage.cpp
183 lines (166 loc) · 4.66 KB
/
saveimage.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
#include "saveimage.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "configurations.h"
#include "image_generation.hpp"
#include "globals.hpp"
SaveImage::SaveImage(ImageGeneration *imgen, MainWindow *mw)
{
this->imgen = imgen;
this->mw = mw;
N = WIDTH * HEIGHT;
#if BW
for(int i = 0; i < mlp_count; i++)
res[i] = new uint8_t[N];
#else
for(int i = 0; i < mlp_count; i++)
res[i] = new uint8_t[N * 4];
#endif
}
#if BW
QImage im(WIDTH, HEIGHT, QImage::Format_Grayscale8);
#else
QImage im(WIDTH, HEIGHT, QImage::Format_RGB32);
#endif
void SaveImage::do_stuff(){
imgen->mut.lock();
combine_images(im);
imgen->mut.unlock();
mw->image_mutex.lock();
delete mw->image;
mw->image = new QImage(im);
mw->image_mutex.unlock();
emit image_ready();
if (mw->ui->checkBox_2->isChecked())
save_image();
}
void SaveImage::combine_images(QImage &im){
#if BW
uint8_t *rowData = (uint8_t*)im.scanLine(0);
#if COMBINE_MODE == 1
for(int i = 0; i < N; i++){
int sum = 0;
for(int j = 0; j < mlp_count; j++)
sum += res[j][i];
rowData[i] = sum / mlp_count;
}
#elif COMBINE_MODE == 2
for(int i = 0; i < N; i++){
int max = 0;
for(int j = 0; j < mlp_count; j++)
if (res[j][i] > max) max = res[j][i];
rowData[i] = max;
}
#elif COMBINE_MODE == 3
for(int i = 0; i < N; i++){
int min = INT32_MAX;
for(int j = 0; j < mlp_count; j++)
if (res[j][i] < min) min = res[j][i];
rowData[i] = min;
}
#elif COMBINE_MODE == 4
for(int i = 0; i < N; i++){
double res_ = 1.0;
for(int j = 0; j < mlp_count; j++)
res_ *= res[j][i] / 256.0;
rowData[i] = res_ * 256.0;
}
#elif COMBINE_MODE == 5
for(int i = 0; i < N; i++){
double res_ = res[0][i] / 256.0;
for(int j = 1; j < mlp_count; j++){
if (res_ < 0.5)
res_ = 2 * res_ * res[j][i] / 256.0;
else
res_ = 1.0 - 2 * (1.0 - res[j][i] / 256.0) * (1.0 - res_);
}
rowData[i] = res_ * 256.0;
}
#elif COMBINE_MODE == 6
static PerlinNoise pn;
static int im_idx = 0;
im_idx++;
for(int i = 0; i < N; i++){
double x = i % WIDTH;
double y = i / WIDTH;
double noise = pn.noise(x / WIDTH, y / HEIGHT, im_idx * 0.01);
// rowData[i] = noise * 256;
rowData[i] = noise * res[0][i] + (1.0 - noise) * res[1][i];
}
#endif
#else
uint8_t *rowData = (uint8_t*)im.scanLine(0);
#if COMBINE_MODE == 1
for(int i = 0; i < N * 4; i++){
int sum = 0;
for(int j = 0; j < mlp_count; j++)
sum += res[j][i];
rowData[i] = sum / mlp_count;
}
#elif COMBINE_MODE == 2
for(int i = 0; i < N * 4; i++){
int max = 0;
for(int j = 0; j < mlp_count; j++)
if (res[j][i] > max) max = res[j][i];
rowData[i] = max;
}
#elif COMBINE_MODE == 3
for(int i = 0; i < N * 4; i++){
int min = INT32_MAX;
for(int j = 0; j < mlp_count; j++)
if (res[j][i] < min) min = res[j][i];
rowData[i] = min;
}
#elif COMBINE_MODE == 4
for(int i = 0; i < N * 4; i++){
double res_ = 1.0;
for(int j = 0; j < mlp_count; j++)
res_ *= res[j][i] / 256.0;
rowData[i] = res_ * 256.0;
}
#elif COMBINE_MODE == 5
for(int i = 0; i < N * 4; i++){
double res_ = res[0][i] / 256.0;
for(int j = 1; j < mlp_count; j++){
if (res_ < 0.5)
res_ = 2 * res_ * res[j][i] / 256.0;
else
res_ = 1.0 - 2 * (1.0 - res[j][i] / 256.0) * (1.0 - res_);
}
rowData[i] = res_ * 256.0;
}
#elif COMBINE_MODE == 6
static PerlinNoise pn;
static int im_idx = 0;
im_idx++;
for(int i = 0; i < N * 4; i++){
int i_ = i / 4;
double x = i_ % WIDTH;
double y = i_ / WIDTH;
double noise = pn.noise(x / WIDTH, y / HEIGHT, im_idx * 0.01);
// rowData[i] = noise * 256;
rowData[i] = noise * res[0][i] + (1.0 - noise) * res[1][i];
}
#endif
#endif
if (imgen->draw_samples){
mw->other_mutex.lock();
vector<vector<double> > samples_in(MLP::train_samples_in);
vector<vector<double> > samples_out(MLP::train_samples_out);
MLP::draw_samples(im, samples_in, samples_out);
mw->other_mutex.unlock();
}
}
void SaveImage::save_image()
{
mw->image_mutex.lock();
if (mw->image != NULL){
save_cnt++;
#if RESCALE_WHEN_SAVING
mw->image->scaled(SAVE_WIDTH, SAVE_HEIGHT, Qt::IgnoreAspectRatio, Qt::SmoothTransformation).save(QString("%1_%2.bmp").arg(file_prefix).arg(cnt));
#else
mw->image->save(QString("%1_%2.bmp").arg(save_file_prefix).arg(save_cnt, 6, 10, QChar('0')));
#endif
}
mw->image_mutex.unlock();
}