-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImage.cpp
64 lines (54 loc) · 1.5 KB
/
Image.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
#include "Image.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#include <iostream>
Image::Image(const std::string &a_path)
{
// 0 заменил на 4
if((data = (Pixel*)stbi_load(a_path.c_str(), &width, &height, &channels, 0)) != nullptr)
{
size = width * height * channels;
}
}
Image::Image(int a_width, int a_height, int a_channels)
{
data = new Pixel[a_width * a_height ]{};
if(data != nullptr)
{
width = a_width;
height = a_height;
size = a_width * a_height * a_channels;
channels = a_channels;
self_allocated = true;
}
}
int Image::Save(const std::string &a_path)
{
auto extPos = a_path.find_last_of('.');
if(a_path.substr(extPos, std::string::npos) == ".png" || a_path.substr(extPos, std::string::npos) == ".PNG")
{
stbi_write_png(a_path.c_str(), width, height, channels, data, width * channels);
}
else if(a_path.substr(extPos, std::string::npos) == ".jpg" || a_path.substr(extPos, std::string::npos) == ".JPG" ||
a_path.substr(extPos, std::string::npos) == ".jpeg" || a_path.substr(extPos, std::string::npos) == ".JPEG")
{
stbi_write_jpg(a_path.c_str(), width, height, channels, data, 100);
}
else
{
std::cerr << "Unknown file extension: " << a_path.substr(extPos, std::string::npos) << "in file name" << a_path << "\n";
return 1;
}
return 0;
}
Image::~Image()
{
if(self_allocated)
delete [] data;
else
{
stbi_image_free(data);
}
}