-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMilestone 1 imageCPP.cpp
More file actions
224 lines (193 loc) · 6.65 KB
/
Milestone 1 imageCPP.cpp
File metadata and controls
224 lines (193 loc) · 6.65 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
#include "image.h"
#include <iostream>
#include <fstream>
Image::Image() {
width = 0;
height = 0;
}
Image::Image(const Image& other) {
width = other.width;
height = other.height;
pixels = other.pixels;
header = other.header;
}
Image& Image::operator=(const Image& other) {
if(this != &other) {
width = other.width;
height = other.height;
pixels = other.pixels;
header = other.header;
}
return *this;
}
Image::~Image() {
}
bool Image::load(const string& filename) {
ifstream file(filename, ios::binary);
if(!file.is_open()) {
cerr << "Error opening file: " << filename << endl;
return false;
}
file.read(reinterpret_cast<char*>(&header), sizeof(header));
width = header.width;
height = header.height;
pixels.resize(width * height);
file.read(reinterpret_cast<char*>(pixels.data()), pixels.size() * sizeof(Pixel));
file.close();
return true;
}
bool Image::save(const string& filename) const{
ofstream file(filename, ios::binary);
if(!file.is_open()) {
cerr << "Error opening file: " << filename << endl;
return false;
}
file.write(reinterpret_cast<const char*>(&header), sizeof(header));
file.write(reinterpret_cast<const char*>(pixels.data()), pixels.size() * sizeof(Pixel));
file.close();
return true;
}
void Image::clamp(Pixel& pix) {
if(pix.red < 0){
pix.red = 0;
}
else if(pix.red > 255) {
pix.red = 255;
}
if(pix.green < 0){
pix.green = 0;
}
else if(pix.green > 255) {
pix.green = 255;
}
if(pix.blue < 0){
pix.blue = 0;
}
else if(pix.blue > 255) {
pix.blue = 255;
}
}
void Image::multiply(const Image& other) {
if(width != other.width || height != other.height) {
cerr <<"Image dimensions do not match for multiplication." << endl;
return;
}
for(int i = 0; i < width * height; ++i) {
pixels[i].red = (pixels[i].red * other.pixels[i].red) / 255.0f + 0.5f;
pixels[i].green = (pixels[i].green * other.pixels[i].green) / 255.0f + 0.5f;
pixels[i].blue = (pixels[i].blue * other.pixels[i].blue) / 255.0f + 0.5f;
}
}
void Image::subtract(const Image& other) {
for(int i = 0; i < width * height; ++i) {
int red = pixels[i].red - other.pixels[i].red;
int green = pixels[i].green - other.pixels[i].green;
int blue = pixels[i].blue - other.pixels[i].blue;
if(red < 0) {
pixels[i].red = 0;
}
else {
pixels[i].red = red;
}
if(green < 0) {
pixels[i].green = 0;
}
else {
pixels[i].green = green;
}
if(blue < 0) {
pixels[i].blue = 0;
}
else {
pixels[i].blue = blue;
}
}
}
void Image::screen(const Image& other) {
for(int i = 0; i < width * height; ++i) {
pixels[i].red = static_cast<unsigned char>(255 - ((255 - pixels[i].red) * (255 - other.pixels[i].red) / 255.0f) + 0.5f);
pixels[i].green = static_cast<unsigned char>(255 - ((255 - pixels[i].green) * (255 - other.pixels[i].green) / 255.0f) + 0.5f);
pixels[i].blue = static_cast<unsigned char>(255 - ((255 - pixels[i].blue) * (255 - other.pixels[i].blue) / 255.0f) + 0.5f);
}
}
void Image::overlay(const Image& other) {
for(int i = 0; i < width * height; ++i) {
float norm1_red = pixels[i].red / 255.0f;
float norm2_red = other.pixels[i].red / 255.0f;
if(norm2_red <= 0.5f) {
pixels[i].red = static_cast<unsigned char>(2 * norm1_red * norm2_red * 255.0f + 0.5f);
}
else {
pixels[i].red = static_cast<unsigned char>((1 - 2 * (1-norm1_red) * (1 - norm2_red)) * 255.0f + 0.5f);
}
float norm1_green = pixels[i].green / 255.0f;
float norm2_green = other.pixels[i].green / 255.0f;
if(norm2_green <= 0.5f) {
pixels[i].green = static_cast<unsigned char>(2 * norm1_green * norm2_green * 255.0f + 0.5f);
}
else {
pixels[i].green = static_cast<unsigned char>((1 - 2 * (1-norm1_green) * (1 - norm2_green)) * 255.0f + 0.5f);
}
float norm1_blue = pixels[i].blue / 255.0f;
float norm2_blue = other.pixels[i].blue / 255.0f;
if(norm2_blue <= 0.5f) {
pixels[i].blue = static_cast<unsigned char>(2 * norm1_blue * norm2_blue * 255.0f + 0.5f);
}
else {
pixels[i].blue = static_cast<unsigned char>((1 - 2 * (1 - norm1_blue) * (1 - norm2_blue)) * 255.0f + 0.5f);
}
}
}
void Image::add_green() {
for(int i = 0; i < width * height; ++i){
int green = pixels[i].green + 200;
if(green > 255) {
pixels[i].green = 255;
}
else {
pixels[i].green = green;
}
}
}
void Image::scale_red_blue() {
for(int i = 0; i < width * height; ++i) {
int new_red = static_cast<int>(pixels[i].red) * 4;
if(new_red > 255) {
pixels[i].red = 255;
}
else {
pixels[i].red = new_red;
}
pixels[i].blue = 0;
}
}
void Image::get_channels(const string &red_file, const string &green_file, const string &blue_file) {
Image red_image = *this, green_image = *this, blue_image = *this;
for(int i = 0; i < width * height; ++i) {
red_image.pixels[i].green = red_image.pixels[i].blue = red_image.pixels[i].red;
green_image.pixels[i].red = green_image.pixels[i].blue = green_image.pixels[i].green;
blue_image.pixels[i].red = blue_image.pixels[i].green = blue_image.pixels[i].blue;
}
red_image.save(red_file);
green_image.save(green_file);
blue_image.save(blue_file);
}
void Image::mix_channels(Image &out_img, const Image &red_img, const Image &green_img, const Image &blue_img) {
out_img.header = red_img.header;
out_img.width = red_img.width;
out_img.height = red_img.height;
out_img.pixels.resize(out_img.width * out_img.height);
for(int i = 0; i < out_img.width * out_img.height; ++i) {
out_img.pixels[i].red = red_img.pixels[i].red;
out_img.pixels[i].green = green_img.pixels[i].green;
out_img.pixels[i].blue = blue_img.pixels[i].blue;
}
}
void Image::rotate_180() {
int n = pixels.size();
for(int i = 0; i < n / 2; ++i) {
Pixel temp = pixels[i];
pixels[i] = pixels[n -1 -i];
pixels[n -1 -i] = temp;
}
}