-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBMPDownloader.cpp
More file actions
73 lines (64 loc) · 2.45 KB
/
Copy pathBMPDownloader.cpp
File metadata and controls
73 lines (64 loc) · 2.45 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
#include "BMPDownloader.h"
#include "Exceptions.h"
#include <iostream>
Color ColorFromInts(IntColor int_color) {
double blue = static_cast<double>(int_color.blue) / static_cast<double>(Color::MaxIntValue);
double green = static_cast<double>(int_color.green) / static_cast<double>(Color::MaxIntValue);
double red = static_cast<double>(int_color.red) / static_cast<double>(Color::MaxIntValue);
return {blue, green, red};
}
void BMPDownloader::OpenFile() {
input_file_.open(path_, std::ios_base::binary);
if (!input_file_) {
std::string message = "File by path " + std::string(path_) + " can't be opened";
throw OpeningFileException(message);
}
std::cout << "File " << path_ << " opened" << '\n';
}
void BMPDownloader::CloseFile() {
input_file_.close();
std::cout << "File " << path_ << " closed" << '\n';
}
template <typename T>
void BMPDownloader::ReadIn(T &input) {
input_file_.read(reinterpret_cast<char *>(&input), sizeof(input));
}
void BMPDownloader::SaveBMPToImage(Image &image) {
BMPFileHeader file_header;
ReadIn(file_header);
if (file_header.file_type != required_type_) {
throw ReadingFileException("File's format is not .bmp");
}
image.SetFileHeader(file_header);
BMPImageHeader image_header;
ReadIn(image_header);
if (image_header.bit_count != required_bit_count_) {
throw ReadingFileException("File is not 24-bit");
}
image.SetImageHeader(image_header);
input_file_.seekg(image.GetOffsetData(), input_file_.beg);
if (image.GetWidth() < 0) {
throw ReadingFileException("Image width can't be negative");
}
bool flipped_image = (image.GetHeight() < 0);
if (flipped_image) {
image.SetHeight(abs(image.GetHeight()));
} else {
image.SetHeight(image.GetHeight());
}
image.SetWidth(image.GetWidth());
for (size_t i = 0; i < static_cast<size_t>(image.GetHeight()); ++i) {
for (size_t j = 0; j < static_cast<size_t>(image.GetWidth()); ++j) {
IntColor int_color;
ReadIn(int_color);
Color color = ColorFromInts(int_color);
if (flipped_image) {
image.SetPixel(i, j, color);
} else {
image.SetPixel(static_cast<size_t>(image.GetHeight()) - 1 - i, j, color);
}
}
input_file_.ignore((4 - (image.GetWidth() * 3) % 4) % 4);
}
std::cout << "Image from " << path_ << " downloaded" << '\n';
}