-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage.cpp
More file actions
76 lines (60 loc) · 1.75 KB
/
Copy pathimage.cpp
File metadata and controls
76 lines (60 loc) · 1.75 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
#include "image.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
Image::Image(const std::string& filename) {
data = stbi_load(filename.c_str(), &width, &height, &channels, 0);
if (!data) {
throw std::runtime_error("Could not open or find the image");
}
}
Image::Image(const Image& other) {
width = other.width;
height = other.height;
channels = other.channels;
data = new unsigned char[width * height * channels];
std::copy(other.data, other.data + (width * height * channels), data);
}
Image::~Image() {
stbi_image_free(data);
}
unsigned char* Image::getData() {
return data;
}
int Image::getWidth() const {
return width;
}
int Image::getHeight() const {
return height;
}
int Image::getChannels() const {
return channels;
}
void Image::save(const std::string& filename) {
stbi_write_jpg(filename.c_str(), width, height, channels, data, 100);
}
bool Image::operator==(const Image& other) const {
if (width != other.width || height != other.height || channels != other.channels) {
return false;
}
for (int i = 0; i < width * height * channels; ++i) {
if (data[i] != other.data[i]) {
return false;
}
}
return true;
}
Image& Image::operator=(const Image& other) {
if (this == &other) {
return *this;
}
width = other.width;
height = other.height;
channels = other.channels;
data = new unsigned char[width * height * channels];
std::copy(other.data, other.data + (width * height * channels), data);
return *this;
}
JPGImage::JPGImage(const std::string& filename) : Image(filename) {}
JPGImage::JPGImage(const JPGImage& other) : Image(other) {}