-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrendermosaicthread.cpp
More file actions
429 lines (397 loc) · 17.7 KB
/
Copy pathrendermosaicthread.cpp
File metadata and controls
429 lines (397 loc) · 17.7 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/***************************************************************************************************
*
* FILE: rendermosaicthread.cpp
*
* CREATED: 02-08-2010
*
* AUTHOR: Benjamin Caspari (becaspari@googlemail.com)
*
* PURPOSE: allows to render the mosaic in a seperate tread
*
* This program is licensed under the terms of the GPL Version 2
*
* Copyright 2010 by Benjamin Caspari
*
***************************************************************************************************/
#include "rendermosaicthread.h"
#include <math.h>
#include <QImage>
#include <QPainter>
#include <QMessageBox>
#include <QFileDialog>
RenderMosaicThread::RenderMosaicThread(QObject *parent) :
QThread(parent)
{
m_cancelNow = false;
m_criticalError = false;
}
void RenderMosaicThread::renderMosaic(PictureDatabase *database,
const QString &imageFile,
int tileWidth,
int tileHeight,
int tileCount,
bool cutEdges,
int alphaChannel,
const QString &outputFile,
QWidget *parentWindow,
bool minDistanceChecker,
int minDistance,
bool maxTileRepeatChecker,
int maxTileRepeatCount)
{
m_cancelNow = false;
this->m_criticalError = false;
this->m_database = database;
this->m_imageFile = imageFile;
this->m_tileWidth = tileWidth;
this->m_tileHeight = tileHeight;
this->m_tileCount = tileCount;
this->m_cutEdges = cutEdges;
this->m_alphaChannel = alphaChannel;
this->m_outputFile = outputFile;
this->m_parentWindow = parentWindow;
this->m_minDistanceChecker = minDistanceChecker;
this->m_minDistance = minDistance;
this->m_maxTileRepeatCount = maxTileRepeatCount;
this->m_maxTileRepeatChecker = maxTileRepeatChecker;
start();
}
void RenderMosaicThread::cancel()
{
this->m_cancelNow = true;
}
float RenderMosaicThread::smallestDistance(int**tileMap,
int mapWidth,
int mapHeight,
int currentTile,
int xPos,
int yPos)
{
bool found = false;
float distance = -1.f;
for (int i = 0; i < mapWidth; i++) {
for (int j = 0; j < mapHeight; j++) {
if (tileMap[i][j] == currentTile) {
if ((xPos != i) || (yPos != j)) {
//now calculate distance to second occurance of tile
float currentDistance = sqrt((xPos - i) * (xPos - i) + (yPos - j) * (yPos - j));
if (!found) {
found = true;
distance = currentDistance;
}
if (currentDistance < distance) {
distance = currentDistance;
}
}
}
}
}
return distance;
}
void RenderMosaicThread::run()
{
//this function will build the new mosaic picture according to the infos
//passed to the corresponding class
emit renderComplete(0.f);
emit logText(tr("Rendering the mosaic..."));
if (this->m_cancelNow) {
emit logText(tr("Rendering was canceled - no mosaic was saved!"));
return;
}
//first, load the original image into memory
emit logText(tr("loading original image %1.").arg(this->m_imageFile));
QImage originalImage(this->m_imageFile);
if (originalImage.isNull()) {
emit logText(tr("Error loading the original image!"));
this->m_criticalError = true;
return;
}
if (this->m_cancelNow) {
emit logText(tr("Rendering was canceled - no mosaic was saved!"));
return;
}
//now, compute the exact amount of tiles in vertical (x) and horizontal
//(y) direction
/*
tx * ty = tileCount => tx = tileCount / ty
and
imageW / imageH = (tx * tileWidth) / (ty * tileHeight)
=> ty * tileHeight * imageW /imageH = (tx * tileWidth)
=> ty * tileHeight/tileWidth * imageW/imageH = tileCount / ty
with tileHeight/tileWidth * imageW/imageH = aspectFactor
=> ty * ty = tileCount / aspectFactor
=> ty = sqrt(tileCount / aspectFactor)
*/
double aspectFactor = ((double)this->m_tileHeight / (double)this->m_tileWidth)
* ((double)originalImage.width() / (double)originalImage.height());
int tilesY = qRound(sqrt(((double)this->m_tileCount) / aspectFactor));
int tilesX = this->m_tileCount / tilesY;
emit logText(tr("The mosaic will be built of %1 x %2 tiles.").arg(
QString::number(tilesX), QString::number(tilesY)));
//resize the original file to get the color values for the
//unique tiles
QImage tilesImages = originalImage.scaled(tilesX, tilesY,
Qt::IgnoreAspectRatio,
Qt::SmoothTransformation
);
if (tilesImages.isNull()) {
emit logText(tr("Error scaling the original picture!"));
this->m_criticalError = true;
return;
}
if (this->m_cancelNow) {
emit logText(tr("Rendering was canceled - no mosaic was saved!"));
return;
}
//reserve memory for tiles map
int **tilesMap = new int*[tilesX];
if (!tilesMap) {
emit logText(tr("Error reserving memory for the tiles map!"));
this->m_criticalError = true;
return;
}
for (int i = 0; i < tilesX; i++) {
tilesMap[i] = new int[tilesY];
if (!tilesMap[i]) {
emit logText(tr("Error reserving memory for tiles map!"));
this->m_criticalError = true;
return;
}
}
for (int i = 0; i < tilesX; i++) {
for (int j = 0; j < tilesY; j++) {
tilesMap[i][j] = -1;
}
}
//reserve image for the mosaic
QImage mosaic(tilesX * this->m_tileWidth, tilesY * this->m_tileHeight,
QImage::Format_ARGB32);
if (mosaic.isNull()) {
emit logText(tr("Error reserving memory for the mosaic image!"));
this->m_criticalError = true;
return;
}
QPainter mosaicPainter(&mosaic);
emit renderComplete(1.f);
if (this->m_cancelNow) {
emit logText(tr("Rendering was canceled - no mosaic was saved!"));
return;
}
//loop through all tiles
int tilesDone = 0;
for (int i = 0; i < tilesX; i++) {
for (int j = 0; j < tilesY; j++) {
if (this->m_cancelNow) {
emit logText(tr("Rendering was canceled - no mosaic was saved!"));
return;
}
//look for best matching tile for current pixel
QRgb thisPixel = tilesImages.pixel(i, j);
int minDiff = 1000;
int nearestIndex = -1;
//loop through all tiles and look for the one with the best matching
//color
for (int k = 0; k < this->m_database->size(); k++) {
if ((this->m_database->pictureAt(k)->processed())
&& (this->m_database->pictureAt(k)->validFile())) {
int diff = 0;
diff += qAbs(qRed(thisPixel)
- this->m_database->pictureAt(k)->getRed());
diff += qAbs(qGreen(thisPixel)
- this->m_database->pictureAt(k)->getGreen());
diff += qAbs(qBlue(thisPixel)
- this->m_database->pictureAt(k)->getBlue());
if (diff < minDiff) {
bool tileIsOk = true;
//potentially found a matching tile, but check for min distance condition
if (this->m_minDistanceChecker) {
float distance = this->smallestDistance(tilesMap,
tilesX,
tilesY,
k,
i,
j);
if (distance >= 0.f) {
if (distance < (float)this->m_minDistance) {
tileIsOk = false;
//emit logText(tr("distance too small to equal tile!"));
}
}
}
//check for max tile repeat count condition
if (this->m_maxTileRepeatChecker) {
int tileInMap = this->tileCountInMap(tilesMap, tilesX, tilesY, k);
if (tileInMap >= this->m_maxTileRepeatCount) {
tileIsOk = false;
}
}
if (tileIsOk) {
minDiff = diff;
nearestIndex = k;
}
}
}
}
//found matching tile
if (nearestIndex != -1) {
tilesMap[i][j] = nearestIndex;
//emit logText(tr("found matching tile!"));
//emit logText(tr("filename is %1").arg(
// this->m_database->pictureAt(nearestIndex)->getFile()));
//load tile image
QImage tileImage(this->m_database->pictureAt(nearestIndex)->getFile());
if (tileImage.isNull()) {
emit logText(tr("Error loading the tile image %1!").arg(
this->m_database->pictureAt(nearestIndex)->getFile()));
this->m_criticalError = true;
return;
}
//if tile aspect ratio doesn't fit the tile aspect ratio given
//for the mosaic
float currentTileAspectRatio = ((float)tileImage.width()/(float)tileImage.height());
float generalTileAspectRatio = ((float)m_tileWidth/(float)m_tileHeight);
if (qAbs(currentTileAspectRatio - generalTileAspectRatio)
> 0.001) {
//do we have to cut the edges?
if (this->m_cutEdges) {
if (currentTileAspectRatio > generalTileAspectRatio) {
//example: tileWidth = 100
//tileHeight = 100
//tileImage.width() = 2000
//tileImage.height() = 1000
// => currentTileAspectRatio = 2.0
// => generalTileAspectRatio = 1.0
// => newCurrentTileWith = (1000 / 100) * 100 = 1000
int newCurrentTileWidth =
((float)tileImage.height() / (float)m_tileHeight) * m_tileWidth;
tileImage = tileImage.copy(
(tileImage.width()-newCurrentTileWidth) / 2,
0,
newCurrentTileWidth,
tileImage.height());
} else {
//example: tileWidth = 100
//tileHeight = 100
//tileImage.width() = 10
//tileImage.height() = 20
// => currentTileAspectRatio = 0.5
// => generalTileAspectRatio = 1.0
// => newCurrentTileHeight = (10 / 100) * 100 = 10
int newCurrentTileHeight =
((float)tileImage.width() / (float)m_tileWidth) * m_tileHeight;
tileImage = tileImage.copy(
0,
(tileImage.height()-newCurrentTileHeight)/2,
tileImage.width(),
newCurrentTileHeight);
}
}
}
tileImage = tileImage.scaled(this->m_tileWidth,
this->m_tileHeight,
Qt::IgnoreAspectRatio,
Qt::SmoothTransformation);
if (tileImage.isNull()) {
emit logText(tr("Error scaling the tile image %1!").arg(
this->m_database->pictureAt(nearestIndex)->getFile()));
this->m_criticalError = true;
return;
}
//copy into mosaic
mosaicPainter.drawImage(i * this->m_tileWidth,
j * this->m_tileHeight,
tileImage);
} else {
emit logText(tr("Error - no matching tile was found!"));
if (this->m_maxTileRepeatChecker) {
emit logText(tr("You have selected a maximum repeat rate for equal tiles. Propably there are not enough images in the database to fullfill this option. Choose a higher value or uncheck the option."));
}
if (this->m_minDistanceChecker) {
emit logText(tr("You have selected a minimum distance between two equal tiles. Propably there are not enough images in the database to fullfill this option. Choose a lower value or uncheck the option."));
}
emit logText(tr("Choosing a image database with more entries might also remove the problem."));
this->m_criticalError = true;
return;
}
tilesDone++;
emit renderComplete(98.f*(float)tilesDone/(float)(tilesX*tilesY) + 1.f);
}
}
if (this->m_cancelNow) {
emit logText(tr("Rendering was canceled - no mosaic was saved!"));
return;
}
//blend in alpha channel now
if (this->m_alphaChannel > 0) {
QRect srcRect(0, 0, originalImage.width(), originalImage.height());
QRect destRect(0, 0, mosaic.width(), mosaic.height());
mosaicPainter.setOpacity((float)this->m_alphaChannel / 100.f);
mosaicPainter.drawImage(destRect,
originalImage,
srcRect);
}
if (this->m_cancelNow) {
emit logText(tr("Rendering was canceled - no mosaic was saved!"));
return;
}
if (!mosaic.save(this->m_outputFile)) {
emit logText(tr("Error saving the built mosaic in the file %1!").arg(this->m_outputFile));
bool tryAgain = true;
while (tryAgain) {
if (QMessageBox::question(this->m_parentWindow,
tr("Error saving mosaic"),
tr("The mosaic could not be saved as \"%1\"! Do you want to select another file to write to?").arg(
QDir::toNativeSeparators(
QDir::cleanPath(
this->m_outputFile))),
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
this->m_outputFile =
QFileDialog::getSaveFileName(this->m_parentWindow,
tr("Select mosaic output file"),
QDir::toNativeSeparators(
QDir::cleanPath(
QDir::homePath()
+ "/mosaic.jpg")),
tr("Images (*.jpg)"));
if (mosaic.save(this->m_outputFile)) {
tryAgain = false;
}
} else {
tryAgain = false;
this->m_criticalError = true;
return;
}
}
}
//free tile map
for (int i = 0; i < tilesX; i++) {
delete tilesMap[i];
}
delete tilesMap;
emit renderComplete(100.f);
emit logText(tr("The mosaic was created."));
}
bool RenderMosaicThread::criticalError()
{
return this->m_criticalError;
}
bool RenderMosaicThread::wasCanceled()
{
return this->m_cancelNow;
}
QString RenderMosaicThread::outputFile()
{
return this->m_outputFile;
}
int RenderMosaicThread::tileCountInMap(int**tileMap, int mapWidth, int mapHeight, int tileToSearch)
{
int result = 0;
for (int i = 0; i < mapWidth; i++) {
for (int j = 0; j < mapHeight; j++) {
if (tileMap[i][j] == tileToSearch) {
result++;
}
}
}
return result;
}