This repository has been archived by the owner on Sep 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
propagate.cpp
234 lines (223 loc) · 5.05 KB
/
propagate.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
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <vector>
#include "heap_priority_queue.h"
#include "image.h"
using namespace std;
struct pixel
{
int s;
int x;
int y;
bool operator<(const pixel& rhs) const
{
if (s < rhs.s)
{
return true;
}
else {
return false;
}
}
bool operator==(const pixel& rhs) const
{
if (s == rhs.s)
{
return true;
}
else {
return false;
}
}
bool operator>(const pixel& rhs) const
{
if (s > rhs.s)
{
return true;
}
else {
return false;
}
}
};
void fillheap(Image<Pixel> &input, Image<Pixel> &speed, Image<Pixel> &output, HeapPriorityQueue<pixel> &heap);
void paint(Image<Pixel> &input, Image<Pixel> &speed, Image<Pixel> &output, HeapPriorityQueue<pixel> &heap);
int main(int argc, char *argv[])
{
HeapPriorityQueue<pixel> heap;//HEAP
unsigned long int T = atoi(argv[4]);
if (argc != 5 || T < 0) {//COMMAND LINE
std::cerr << "Error1" << std::endl;
return EXIT_FAILURE;
}
////////////IMAGE LIBRARY//////////////////////////////////////////////////////
Image<Pixel> input = readFromFile(argv[1]);
Image<Pixel> speed = readFromFile(argv[2]);
Image<Pixel> output = input;
///////////////CHECKING ERRORS/////////////////////////////////////////////////
if ((input.width() != speed.width()) || (input.height() != speed.height())) {
std::cerr << "Error2" << std::endl;
return EXIT_FAILURE;
}
for (unsigned int i = 0; i < input.width(); ++i) {
for (unsigned int j = 0; j < input.height(); ++j) {
int red = input(i, j).red;
int green = input(i, j).green;
int blue = input(i, j).blue;
if ((red != 0 && red != 255) || (green != 0 && green != 255) || (blue != 0 && blue != 255)) {
std::cerr << "Error3" << std::endl;
return EXIT_FAILURE;
}
else {
}
}
}
///////////////////////////////////////////////////////////////////////////////
fillheap(input, speed, output, heap);
for (int i = 0; i < T; i++) {
paint(input, speed, output, heap);
}
for (unsigned int i = 0; i < input.width(); ++i)
{
for (unsigned int j = 0; j < input.height(); ++j)
{
int b = output(i, j).red;
if (b == 100) {
output(i, j).red = 255;
}
}
}
writeToFile(output, argv[3]);
std::cout << "done" << std::endl;
return EXIT_SUCCESS;
}
void paint(Image<Pixel> &input, Image<Pixel> &speed, Image<Pixel> &output, HeapPriorityQueue<pixel> &heap) {
if (!heap.isEmpty())
{
//paint peeked pixel
pixel p = heap.peek();
int i = p.x;
int j = p.y;
output(i, j).red = 0;
output(i, j).green = 0;
output(i, j).blue = 0;
heap.remove();
//right pixel
if (i != input.width() - 1) {
int right = output(i + 1, j).red;
if (right == 255) {
int s = speed(i + 1, j).red;
output(i+1, j).red = 100;
pixel inforight;
inforight.s = s;
inforight.x = i + 1;
inforight.y = j;
heap.add(inforight);
}
}
//top pixel
if (j != 0) {
int top = output(i, j - 1).red;
if (top == 255) {
int s = speed(i, j - 1).red;
output(i, j-1).red = 100;
pixel infotop;
infotop.s = s;
infotop.x = i;
infotop.y = j - 1;
heap.add(infotop);
}
}
//left pixel
if (i != 0) {
int left = output(i - 1, j).red;
if (left == 255) {
int s = speed(i - 1, j).red;
output(i-1, j).red = 100;
pixel infoleft;
infoleft.s = s;
infoleft.x = i - 1;
infoleft.y = j;
heap.add(infoleft);
}
}
//bot pixel
if (j != input.height() - 1) {
int bot = output(i, j + 1).red;
if (bot == 255) {
int s = speed(i, j + 1).red;
output(i, j+1).red = 100;
pixel infobot;
infobot.s = s;
infobot.x = i;
infobot.y = j + 1;
heap.add(infobot);
}
}
}
}
void fillheap(Image<Pixel> &input, Image<Pixel> &speed, Image<Pixel> &output, HeapPriorityQueue<pixel> &heap) {
for (unsigned int i = 0; i < input.width(); ++i)
{
for (unsigned int j = 0; j < input.height(); ++j)
{
int c = input(i, j).red;
if (c == 0)
{
//right pixel
if (i != input.width() - 1) {
int right = input(i + 1, j).red;
if (right == 255) {
int s = speed(i + 1, j).red;
output(i+1, j).red = 100;
pixel inforight;
inforight.s = s;
inforight.x = i + 1;
inforight.y = j;
heap.add(inforight);
}
}
//top pixel
if (j != 0) {
int top = input(i, j - 1).red;
if (top == 255) {
int s = speed(i, j - 1).red;
output(i, j-1).red = 100;
pixel infotop;
infotop.s = s;
infotop.x = i;
infotop.y = j - 1;
heap.add(infotop);
}
}
//left pixel
if (i != 0) {
int left = input(i - 1, j).red;
if (left == 255) {
int s = speed(i - 1, j).red;
output(i-1, j).red = 100;
pixel infoleft;
infoleft.s = s;
infoleft.x = i - 1;
infoleft.y = j;
heap.add(infoleft);
}
}
//bot pixel
if (j != input.height() - 1) {
int bot = input(i, j + 1).red;
if (bot == 255) {
int s = speed(i, j + 1).red;
output(i, j+1).red = 100;
pixel infobot;
infobot.s = s;
infobot.x = i;
infobot.y = j + 1;
heap.add(infobot);
}
}
}
}
}
}