2020-09-07 08:54:24 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
*------> x
|
|
|
|
|0 1 2
|
|
|
|
|3 4 5
|
|
|
|
|6 7 8
|
|
|
|
↓
|
|
|
|
y
|
|
|
|
|
|
|
|
*/
|
|
|
|
class Image
|
|
|
|
{
|
|
|
|
uint mx;
|
|
|
|
uint my;
|
|
|
|
uint *data;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Image(uint tmx, uint tmy)
|
|
|
|
{ // Должен инициализировать изображение 0
|
|
|
|
mx = tmx;
|
|
|
|
my = tmy;
|
|
|
|
data = (uint *)malloc(sizeof(uint) * mx * my);
|
|
|
|
for (uint y = 0; y < my; y++)
|
|
|
|
{
|
|
|
|
for (uint x = 0; x < mx; x++)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
for (uint y = 0; y < my; y++)
|
|
|
|
{
|
|
|
|
for (uint x = 0; x < mx; x++)
|
|
|
|
{
|
|
|
|
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-07 09:07:27 +00:00
|
|
|
if (x >= mx)
|
|
|
|
{
|
2020-09-07 08:54:24 +00:00
|
|
|
throw(x);
|
|
|
|
}
|
2020-09-07 09:07:27 +00:00
|
|
|
if (y >= my)
|
|
|
|
{
|
2020-09-07 08:54:24 +00:00
|
|
|
throw(y);
|
|
|
|
}
|
|
|
|
data[mx * (y) + (x)] = color;
|
|
|
|
}
|
|
|
|
|
2020-09-11 08:53:27 +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-07 08:54:24 +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-11 08:53:27 +00:00
|
|
|
bool compare(Image &original)
|
2020-09-07 08:54:24 +00:00
|
|
|
{
|
2020-09-11 08:53:27 +00:00
|
|
|
if (original.getMx() == mx && original.getMy() == my)
|
2020-09-07 08:54:24 +00:00
|
|
|
{
|
|
|
|
for (uint y = 0; y < my; y++)
|
|
|
|
{
|
|
|
|
for (uint x = 0; x < mx; x++)
|
|
|
|
{
|
2020-09-11 08:53:27 +00:00
|
|
|
if (data[mx * y + x] != original.get(x, y))
|
2020-09-07 09:07:27 +00:00
|
|
|
{
|
2020-09-07 08:54:24 +00:00
|
|
|
cout << "Not equal: x = " << x << "; y = " << y << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (y >= my)
|
|
|
|
{
|
2020-09-07 08:54:24 +00:00
|
|
|
throw(y);
|
|
|
|
}
|
2020-09-07 09:07:27 +00:00
|
|
|
for (uint x = 0; x < mx; x++)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (x >= mx)
|
|
|
|
{
|
2020-09-07 08:54:24 +00:00
|
|
|
throw(x);
|
|
|
|
}
|
2020-09-07 09:07:27 +00:00
|
|
|
for (uint y = 0; y < my; y++)
|
|
|
|
{
|
|
|
|
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-07 09:07:27 +00:00
|
|
|
if (writefile.is_open())
|
|
|
|
{
|
|
|
|
writefile << mx << "\n"
|
|
|
|
<< my << "\n";
|
2020-09-07 08:54:24 +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-07 09:07:27 +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-07 09:07:27 +00:00
|
|
|
if (readfile.is_open())
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
getline(readfile, line);
|
|
|
|
mx = stoi(line);
|
|
|
|
|
|
|
|
getline(readfile, line);
|
|
|
|
my = stoi(line);
|
2020-09-07 08:54:24 +00:00
|
|
|
|
2020-09-07 09:07:27 +00:00
|
|
|
for (uint y = 0; y < my; y++)
|
|
|
|
{
|
|
|
|
for (uint x = 0; x < mx; x++)
|
|
|
|
{
|
|
|
|
getline(readfile, line);
|
|
|
|
data[mx * y + x] = stoi(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const exception &e)
|
|
|
|
{
|
|
|
|
cerr << e.what() << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
if (b.compare(a) == true)
|
2020-09-07 09:07:27 +00:00
|
|
|
{
|
2020-09-07 08:54:24 +00:00
|
|
|
cout << "a == b after copy\n";
|
2020-09-07 09:07:27 +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);
|
|
|
|
for (int i = 5; i < 9; i++)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|