Repository navigation
Expand file tree
/
Copy pathpixelize_cpu.cpp
More file actions
164 lines (130 loc) · 4.5 KB
/
Copy pathpixelize_cpu.cpp
File metadata and controls
164 lines (130 loc) · 4.5 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
#include <iostream>
#include <filesystem>
#include <vector>
#include <thread>
#include <mutex>
#include <atomic>
#include "../utils/image_utils.h"
void processPixelBlock(
int startX,
int startY,
int blockSize,
int width,
int height,
const std::vector<Pixel>& input,
std::vector<Pixel>& result
)
{
int endX = std::min(startX + blockSize, width);
int endY = std::min(startY + blockSize, height);
int actualBlockSize = (endX - startX) * (endY - startY);
if (actualBlockSize == 0) return;
unsigned long long sumR = 0, sumG = 0, sumB = 0;
for (int y = startY; y < endY; ++y) {
for (int x = startX; x < endX; ++x) {
int index = y * width + x;
sumR += input[index].r;
sumG += input[index].g;
sumB += input[index].b;
}
}
Pixel averageColor = {
static_cast<unsigned char>(sumR / actualBlockSize),
static_cast<unsigned char>(sumG / actualBlockSize),
static_cast<unsigned char>(sumB / actualBlockSize)
};
for (int y = startY; y < endY; ++y) {
for (int x = startX; x < endX; ++x) {
int index = y * width + x;
result[index] = averageColor;
}
}
}
void processPixelBlocksRange(
int startY,
int endY,
int blockSize,
int width,
int height,
const std::vector<Pixel>& input,
std::vector<Pixel>& result
) {
for (int y = startY; y < endY; y += blockSize) {
for (int x = 0; x < width; x += blockSize) {
processPixelBlock(x, y, blockSize, width, height, input, result);
}
}
}
std::vector<Pixel> pixelizeImageParallel(const std::vector<Pixel>& input, int width, int height, int blockSize) {
if (blockSize <= 1) return input;
std::vector<Pixel> result(input.size());
std::vector<std::thread> threads;
unsigned int numThreads = std::thread::hardware_concurrency();
if (numThreads == 0) numThreads = 4;
int rowsPerThread = (height + numThreads - 1) / numThreads;
rowsPerThread = ((rowsPerThread + blockSize - 1) / blockSize) * blockSize;
for (int i = 0; i < numThreads; ++i) {
int startY = i * rowsPerThread;
int endY = std::min((i + 1) * rowsPerThread, height);
if (startY >= height) break;
threads.emplace_back(
processPixelBlocksRange,
startY,
endY,
blockSize,
width,
height,
std::cref(input),
std::ref(result)
);
}
for (auto& thread : threads) {
if (thread.joinable()) {
thread.join();
}
}
return result;
}
int main() {
std::string inputPath, outputPath;
int blockSize;
std::cout << "Enter input image path: ";
std::getline(std::cin, inputPath);
if (!std::filesystem::exists(inputPath)) {
std::cerr << "File not found: " << inputPath << std::endl;
return 1;
}
std::cout << "Enter output image path: ";
std::getline(std::cin, outputPath);
std::filesystem::path outputDir = std::filesystem::path(outputPath).parent_path();
if (!outputDir.empty() && !std::filesystem::exists(outputDir)) {
std::filesystem::create_directories(outputDir);
}
std::cout << "Enter pixelization block size: ";
std::cin >> blockSize;
if (blockSize < 1) {
std::cerr << "Block size must be a positive number" << std::endl;
return 1;
}
std::cout << "Loading image: " << inputPath << std::endl;
int width, height;
auto image = loadImage(inputPath, width, height);
if (image.empty()) {
std::cerr << "Failed to load image" << std::endl;
return 1;
}
std::cout << "Image loaded: " << width << "x" << height << std::endl;
std::cout << "Pixelizing image with block size " << blockSize << "x" << blockSize << std::endl;
auto start = std::chrono::high_resolution_clock::now();
auto pixelized = pixelizeImageParallel(image, width, height, blockSize);
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
std::cout << "Pixelization completed in " << duration.count() << " ns" << std::endl;
if (saveImage(outputPath, pixelized, width, height)) {
std::cout << "Image successfully saved!" << std::endl;
} else {
std::cerr << "Error saving image" << std::endl;
return 1;
}
return 0;
}