-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimagemaker.cpp
More file actions
161 lines (148 loc) · 6.28 KB
/
imagemaker.cpp
File metadata and controls
161 lines (148 loc) · 6.28 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
#include "imagemaker.h"
#include <vector>
#include "QString"
#include "bitmap.h"
#include "QDirIterator"
#include <cassert>
#include <cmath>
#include <QDebug>
//Проверка расширения картинки
bool ImageMaker::checkFileExtenson(QString filename)
{
for(auto extension = this->goodExtension.begin(); extension != this->goodExtension.end(); ++extension)
{
if(filename.endsWith(*extension))
{
return true;
}
}
return false;
}
//Контур изображения
void ImageMaker::generatePixels()
{
for(auto filePath = imagePaths.begin(); filePath != imagePaths.end(); ++filePath)
{
Bitmap bitmap = Bitmap(*filePath);
if (this->averagePixels.size() != 0)
{
assert(this->smallImageH == bitmap.height && this->smallImageW == bitmap.width);
}
this->smallImageH = bitmap.height;
this->smallImageW = bitmap.width;
Pixel averagePixel = bitmap.averagePixel();
this->averagePixels.push_back(averagePixel);
}
assert(this->averagePixels.size() == this->imagePaths.size());
}
//Считаем средние пиксели для каждого изображения и подбираем маленькое изображения для коллажа
QString ImageMaker::getNearestImage(Pixel pixel)
{
Pixel smallest = this->averagePixels.front();
QString smallestPath = this->imagePaths.front();
for(size_t imageIndex = 0; imageIndex < this->averagePixels.size(); ++imageIndex)
{
Pixel currentPixel = this->averagePixels[imageIndex];
double smallestDistance = fabs(smallest.red - pixel.red) + fabs(smallest.green - pixel.green) + fabs(smallest.blue - pixel.blue);
double currentDistance = fabs(currentPixel.red - pixel.red) + fabs(currentPixel.green - pixel.green) + fabs(currentPixel.blue - pixel.blue);
if (currentDistance < smallestDistance)
{
smallest = currentPixel;
smallestPath = this->imagePaths[imageIndex];
}
}
return smallestPath;
}
//Собирает изображение из предподсчитынных маленьких
Bitmap ImageMaker::constructImage(std::vector<QString> replacingImages, size_t wCount, size_t hCount, QImage::Format imageFormat)
{
assert(replacingImages.size() == wCount * hCount);
int newBitmapH = this->smallImageH * (hCount);
int newBitmapW = this->smallImageW * (wCount);
std::vector<Pixel> pixels(newBitmapH * newBitmapW, Pixel(0, 0, 0));
Bitmap resultBitmap = Bitmap(newBitmapW, newBitmapH, pixels, imageFormat);
for(size_t imageIndexH = 0; imageIndexH < hCount; ++imageIndexH)
{
for(size_t imageIndexW = 0; imageIndexW < wCount; ++imageIndexW)
{
int selectedIndex = imageIndexH * wCount + imageIndexW;
// каждый раз загружать с диска - долго, можно оптимизировать
Bitmap image = Bitmap(replacingImages[selectedIndex]);
int startH = image.height * imageIndexH;
int startW = image.width * imageIndexW;
for(size_t dH = 0; dH < image.height; ++dH)
{
for(size_t dW = 0; dW < image.width; ++dW)
{
size_t posH = startH + dH;
size_t posW = startW + dW;
assert(posH < resultBitmap.height);
assert(posW < resultBitmap.width);
Pixel pixel = image.getPixel(dW, dH);
resultBitmap.setPixel(posW, posH, pixel);
}
}
}
}
return resultBitmap;
}
//Конструктор создает объект ImageMaker, где imagePaths - пути к изображениям
ImageMaker::ImageMaker(std::vector<QString> imagePaths)
{
this->imagePaths = imagePaths;
this->generatePixels();
}
//Путь к папке с изображениями (все изображения должны быть одинакового размера)
ImageMaker::ImageMaker(QString imageFolderPath)
{
QDirIterator directoryIterator(imageFolderPath);
while(directoryIterator.hasNext())
{
QString currentFileName = directoryIterator.next();
if(this->checkFileExtenson(currentFileName))
{
this->imagePaths.push_back(currentFileName);
}
}
this->generatePixels();
}
//Создает коллаж из маленьких изображений, fromPath - путь к файлу изображения, который хотим создать, scaleValue - коэффициент, на который будут помножены линейные размеры изображения
Bitmap ImageMaker::makeImage(QString fromPath, double scaleValue)
{
Bitmap bitmap(fromPath, scaleValue);
int wCount = bitmap.width / this->smallImageW;
int hCount = bitmap.height / this->smallImageH;
assert(wCount != 0 && hCount != 0);
std::vector<QString> replacingImages;
for(int hIndex = 0; hIndex < hCount; ++hIndex)
{
for(int wIndex = 0; wIndex < wCount; ++wIndex)
{
// Начальные координаты отрезка.
int h = hIndex * this->smallImageH;
int w = wIndex * this->smallImageW;
long long red = 0;
long long green = 0;
long long blue = 0;
for(size_t subImageH = 0; subImageH < this->smallImageH; ++subImageH)
{
for(size_t subImageW = 0; subImageW < this->smallImageW; ++subImageW)
{
int newH = h + subImageH;
int newW = w + subImageW;
//check, maybe (newH, newW);
Pixel pixel = bitmap.getPixel(newW, newH);
red += pixel.red;
green += pixel.green;
blue += pixel.blue;
}
}
int pixCount = this->smallImageH * this->smallImageW;
red /= pixCount, green /= pixCount, blue /= pixCount;
Pixel averagePixel = Pixel(red, green, blue);
replacingImages.push_back(this->getNearestImage(averagePixel));
}
}
Bitmap output = this->constructImage(replacingImages, wCount, hCount, bitmap.imageFormat);
return output;
}