2020-09-07 08:54:24 +00:00
|
|
|
#include <fstream>
|
2020-09-19 11:41:40 +00:00
|
|
|
#include <iostream>
|
2020-09-07 08:54:24 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
*------> x
|
|
|
|
|0 1 2
|
|
|
|
|3 4 5
|
|
|
|
|6 7 8
|
|
|
|
↓
|
|
|
|
y
|
|
|
|
|
|
|
|
*/
|
2020-09-19 11:41:40 +00:00
|
|
|
class Image {
|
2020-09-07 08:54:24 +00:00
|
|
|
uint mx;
|
|
|
|
uint my;
|
2020-09-19 11:41:40 +00:00
|
|
|
uint* data;
|
2020-09-07 08:54:24 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
Image(uint tmx, uint tmy)
|
|
|
|
{ // Должен инициализировать изображение 0
|
|
|
|
mx = tmx;
|
|
|
|
my = tmy;
|
2020-09-19 11:41:40 +00:00
|
|
|
data = (uint*)malloc(sizeof(uint) * mx * my);
|
|
|
|
for (uint y = 0; y < my; y++) {
|
|
|
|
for (uint x = 0; x < mx; x++) {
|
2020-09-07 08:54:24 +00:00
|
|
|
data[mx * y + x] = 27;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-07 09:07:27 +00:00
|
|
|
void freemem()
|
2020-09-07 08:54:24 +00:00
|
|
|
{
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
int getMx()
|
|
|
|
{
|
|
|
|
return mx;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getMy()
|
|
|
|
{
|
|
|
|
return my;
|
|
|
|
}
|
|
|
|
|
|
|
|
void show()
|
|
|
|
{ // Должен выдавать на экран изображение при помощи printf или std::cout
|
2020-09-19 11:41:40 +00:00
|
|
|
for (uint y = 0; y < my; y++) {
|
|
|
|
for (uint x = 0; x < mx; x++) {
|
2020-09-07 08:54:24 +00:00
|
|
|
cout << "\033[48;5;" << data[mx * y + x] << "m \033[0m";
|
|
|
|
}
|
|
|
|
cout << "\n";
|
|
|
|
}
|
|
|
|
cout << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
int get(uint x, uint y)
|
|
|
|
{
|
|
|
|
return data[mx * (y) + (x)];
|
|
|
|
}
|
|
|
|
|
|
|
|
void set(uint x, uint y, uint color)
|
|
|
|
{
|
2020-09-19 11:41:40 +00:00
|
|
|
if (x >= mx) {
|
2020-09-07 08:54:24 +00:00
|
|
|
throw(x);
|
|
|
|
}
|
2020-09-19 11:41:40 +00:00
|
|
|
if (y >= my) {
|
2020-09-07 08:54:24 +00:00
|
|
|
throw(y);
|
|
|
|
}
|
|
|
|
data[mx * (y) + (x)] = color;
|
|
|
|
}
|
|
|
|
|
2020-09-19 11:41:40 +00:00
|
|
|
void copy(Image& original)
|
2020-09-07 08:54:24 +00:00
|
|
|
{
|
|
|
|
freemem();
|
2020-09-11 08:53:27 +00:00
|
|
|
mx = original.getMx();
|
|
|
|
my = original.getMy();
|
2020-09-19 11:41:40 +00:00
|
|
|
data = (uint*)malloc(sizeof(uint) * mx * my);
|
|
|
|
for (uint y = 0; y < my; y++) {
|
|
|
|
for (uint x = 0; x < mx; x++) {
|
2020-09-11 08:53:27 +00:00
|
|
|
data[mx * y + x] = original.get(x, y);
|
2020-09-07 08:54:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if equal
|
2020-09-19 11:41:40 +00:00
|
|
|
bool compare(Image& original)
|
2020-09-07 08:54:24 +00:00
|
|
|
{
|
2020-09-19 11:41:40 +00:00
|
|
|
if (original.getMx() == mx && original.getMy() == my) {
|
|
|
|
for (uint y = 0; y < my; y++) {
|
|
|
|
for (uint x = 0; x < mx; x++) {
|
|
|
|
if (data[mx * y + x] != original.get(x, y)) {
|
2020-09-07 08:54:24 +00:00
|
|
|
cout << "Not equal: x = " << x << "; y = " << y << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2020-09-19 11:41:40 +00:00
|
|
|
} else {
|
2020-09-11 08:53:27 +00:00
|
|
|
cout << "original size: " << original.getMx() << "x" << original.getMy() << "\n";
|
2020-09-07 08:54:24 +00:00
|
|
|
cout << "current size: " << mx << "x" << my << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-07 09:07:27 +00:00
|
|
|
void draw_horizontal(uint y, uint color)
|
|
|
|
{
|
2020-09-19 11:41:40 +00:00
|
|
|
if (y >= my) {
|
2020-09-07 08:54:24 +00:00
|
|
|
throw(y);
|
|
|
|
}
|
2020-09-19 11:41:40 +00:00
|
|
|
for (uint x = 0; x < mx; x++) {
|
2020-09-07 09:07:27 +00:00
|
|
|
data[mx * y + x] = color;
|
2020-09-07 08:54:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-07 09:07:27 +00:00
|
|
|
void draw_vertical(uint x, uint color)
|
|
|
|
{
|
2020-09-19 11:41:40 +00:00
|
|
|
if (x >= mx) {
|
2020-09-07 08:54:24 +00:00
|
|
|
throw(x);
|
|
|
|
}
|
2020-09-19 11:41:40 +00:00
|
|
|
for (uint y = 0; y < my; y++) {
|
2020-09-07 09:07:27 +00:00
|
|
|
data[mx * y + x] = color;
|
2020-09-07 08:54:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-07 09:07:27 +00:00
|
|
|
void save_to_file(string filename)
|
|
|
|
{
|
2020-09-07 08:54:24 +00:00
|
|
|
ofstream writefile;
|
|
|
|
writefile.open(filename);
|
2020-09-19 11:41:40 +00:00
|
|
|
if (writefile.is_open()) {
|
2020-09-07 09:07:27 +00:00
|
|
|
writefile << mx << "\n"
|
|
|
|
<< my << "\n";
|
2020-09-19 11:41:40 +00:00
|
|
|
for (uint y = 0; y < my; y++) {
|
|
|
|
for (uint x = 0; x < mx; x++) {
|
2020-09-07 09:07:27 +00:00
|
|
|
writefile << data[mx * y + x] << "\n";
|
2020-09-07 08:54:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-07 09:07:27 +00:00
|
|
|
writefile << "\n";
|
2020-09-07 08:54:24 +00:00
|
|
|
writefile.close();
|
2020-09-19 11:41:40 +00:00
|
|
|
} else {
|
2020-09-07 08:54:24 +00:00
|
|
|
cerr << "saving to file failed\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-07 09:07:27 +00:00
|
|
|
void read_from_file(string filename)
|
|
|
|
{
|
2020-09-07 08:54:24 +00:00
|
|
|
string line;
|
|
|
|
ifstream readfile;
|
|
|
|
readfile.open(filename);
|
2020-09-19 11:41:40 +00:00
|
|
|
if (readfile.is_open()) {
|
|
|
|
try {
|
2020-09-07 09:07:27 +00:00
|
|
|
getline(readfile, line);
|
|
|
|
mx = stoi(line);
|
|
|
|
|
|
|
|
getline(readfile, line);
|
|
|
|
my = stoi(line);
|
2020-09-07 08:54:24 +00:00
|
|
|
|
2020-09-19 11:41:40 +00:00
|
|
|
for (uint y = 0; y < my; y++) {
|
|
|
|
for (uint x = 0; x < mx; x++) {
|
2020-09-07 09:07:27 +00:00
|
|
|
getline(readfile, line);
|
|
|
|
data[mx * y + x] = stoi(line);
|
|
|
|
}
|
|
|
|
}
|
2020-09-19 11:41:40 +00:00
|
|
|
} catch (const exception& e) {
|
2020-09-07 09:07:27 +00:00
|
|
|
cerr << e.what() << '\n';
|
|
|
|
}
|
2020-09-19 11:41:40 +00:00
|
|
|
} else {
|
2020-09-07 08:54:24 +00:00
|
|
|
cerr << "reading file failed";
|
|
|
|
}
|
|
|
|
readfile.close();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Image a(30, 10);
|
|
|
|
Image b(25, 15);
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
cout << "Image a \n";
|
|
|
|
a.show();
|
|
|
|
cout << "Image b \n";
|
|
|
|
b.show();
|
2020-09-11 08:53:27 +00:00
|
|
|
b.copy(a);
|
2020-09-19 11:41:40 +00:00
|
|
|
if (b.compare(a) == true) {
|
2020-09-07 08:54:24 +00:00
|
|
|
cout << "a == b after copy\n";
|
2020-09-19 11:41:40 +00:00
|
|
|
} else {
|
2020-09-07 08:54:24 +00:00
|
|
|
cout << "a != b after copy!\n";
|
|
|
|
}
|
2020-09-07 09:07:27 +00:00
|
|
|
|
2020-09-07 08:54:24 +00:00
|
|
|
b.set(4, 3, 20);
|
2020-09-19 11:41:40 +00:00
|
|
|
for (int i = 5; i < 9; i++) {
|
2020-09-07 08:54:24 +00:00
|
|
|
b.set(i, 5, 45);
|
|
|
|
}
|
|
|
|
b.set(9, 3, 20);
|
|
|
|
cout << "Image b, copied from a and edited \n";
|
|
|
|
b.show();
|
|
|
|
|
|
|
|
b.draw_horizontal(8, 214);
|
|
|
|
cout << "Image b, with a horizontal line \n";
|
|
|
|
b.show();
|
|
|
|
b.save_to_file("image");
|
2020-09-07 09:07:27 +00:00
|
|
|
a.read_from_file("image");
|
|
|
|
cout << "Image a, read from file \n";
|
|
|
|
a.show();
|
2020-09-11 08:53:27 +00:00
|
|
|
a.freemem();
|
|
|
|
b.freemem();
|
2020-09-07 08:54:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|