-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPixmap.cpp
More file actions
147 lines (133 loc) · 3.68 KB
/
Pixmap.cpp
File metadata and controls
147 lines (133 loc) · 3.68 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
#include "Pixmap.hpp"
Pixmap::Pixmap(int rows, int columns, String format) : Graphics(rows, columns, format) {
}
Pixmap::~Pixmap() {
}
void Pixmap::grayscale() {
for(int i = 0; i < rows*columns; i++) {
int color = 0;
for(int j = 0; j < 3; j++) {
color += image[i][j];
}
for(int j = 0; j < 3; j++) {
image[i][j] = color / 3;
}
}
}
void Pixmap::monochrome() {
for(int i = 0; i < rows*columns; i++) {
int color = 0;
for(int j = 0; j < 3; j++) {
color += image[i][j];
}
for(int j = 0; j < 3; j++) {
if(color / 3 >= max_value / 2) {
image[i][j] = 1;
}
else {
image[i][j] = 0;
}
}
}
max_value = 1;
}
void Pixmap::rotate(String direction) {
Pixmap rotated(columns, rows, format);
rotated.max_value = max_value;
rotated.image.borrow(rows*columns);
for(int i = 0; i < rows*columns; i++) {
rotated.image[i].borrow(3);
}
if(direction == "left") {
int rotation_index = 0;
for(int temp_row = rows; temp_row != 0; temp_row--) {
for(int i = 0; i < columns; i++) {
for(int j = 0; j < 3; j++) {
rotated.image[rotation_index][j] = image[i*rows + temp_row - 1][j];
}
rotation_index++;
}
}
}
else if(direction == "right") {
int rotation_index = 0;
for(int temp_row = 0; temp_row < rows; temp_row++) {
for(int i = columns - 1; i >= 0; i--) {
for(int j = 0; j < 3; j++) {
rotated.image[rotation_index][j] = image[i*rows + temp_row][j];
}
rotation_index++;
}
}
}
*this = rotated;
}
void Pixmap::negative() {
for(int i = 0; i < rows*columns; i++) {
for(int j = 0; j < 3; j++) {
image[i][j] = max_value - image[i][j];
}
}
}
std::istream& Pixmap::read(std::istream& is) {
if(format == "P3") {
read_ascii(is);
}
else if(format == "P6") {
read_binary(is);
}
return is;
}
std::ostream& Pixmap::write(std::ostream& os) {
if(format == "P3") {
write_ascii(os);
}
else if(format == "P6") {
write_binary(os);
}
return os;
}
void Pixmap::append_horizontally(Graphics* image) {
Vector<Vector<unsigned int>> appended;
Vector<Vector<unsigned int>> copy = image->get_image();
appended.borrow(2*columns*rows);
bool switch_var = false;
int k = 0, n = 0;
for(int i = 0; i < columns*rows*2; i++) {
if(i % rows == 0) switch_var = !switch_var;
appended[i].borrow(3);
if(switch_var) {
for(int j = 0; j < 3; j++) {
appended[i][j] = this->image[k][j];
}
k++;
}
else {
for(int j = 0; j < 3; j++) {
appended[i][j] = copy[n][j];
}
n++;
}
}
this->image = appended;
rows *= 2;
}
void Pixmap::append_vertically(Graphics* image) {
Vector<Vector<unsigned int>> appended;
appended.borrow(2*columns*rows);
for(int i = 0; i < columns * rows; i++) {
appended[i].borrow(3);
for(int j = 0; j < 3; j++) {
appended[i][j] = this->image[i][j];
}
}
Vector<Vector<unsigned int>> copy = image->get_image();
for(int i = 0; i < copy.get_size(); i++) {
appended[i + columns*rows].borrow(3);
for(int j = 0; j < 3; j++) {
appended[i + columns*rows][j] = copy[i][j];
}
}
this->image = appended;
columns *= 2;
}