211 lines
4.4 KiB
C++
211 lines
4.4 KiB
C++
#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;
|
|
}
|
|
}
|
|
}
|
|
|
|
void freemem()
|
|
{
|
|
free(data);
|
|
}
|
|
|
|
int getMx()
|
|
{
|
|
return mx;
|
|
}
|
|
|
|
int getMy()
|
|
{
|
|
return my;
|
|
}
|
|
|
|
void show()
|
|
{ // Должен выдавать на экран изображение при помощи printf или std::cout
|
|
// TODO реализовать
|
|
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)
|
|
{
|
|
|
|
// TODO реализовать логику
|
|
return data[mx * (y) + (x)];
|
|
}
|
|
|
|
void set(uint x, uint y, uint color)
|
|
{
|
|
if (x >= mx) {
|
|
throw(x);
|
|
}
|
|
if (y >= my) {
|
|
throw(y);
|
|
}
|
|
data[mx * (y) + (x)] = color;
|
|
// TODO реализовать логику
|
|
}
|
|
|
|
void copy(Image* original)
|
|
{
|
|
freemem();
|
|
mx = original->getMx();
|
|
my = original->getMy();
|
|
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] = original->get(x, y);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Returns true if equal
|
|
bool compare(Image* original)
|
|
{
|
|
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)){
|
|
cout << "Not equal: x = " << x << "; y = " << y << "\n";
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
cout << "original size: " << original->getMx() << "x" << original->getMy() << "\n";
|
|
cout << "current size: " << mx << "x" << my << "\n";
|
|
return false;
|
|
}
|
|
}
|
|
|
|
void draw_horizontal(uint y, uint color) {
|
|
if (y >= my) {
|
|
throw(y);
|
|
}
|
|
for (uint x = 0; x < mx; x++) {
|
|
data[mx*y + x] = color;
|
|
}
|
|
}
|
|
|
|
void draw_vertical(uint x, uint color) {
|
|
if (x >= mx) {
|
|
throw(x);
|
|
}
|
|
for (uint y = 0; y < my; y++) {
|
|
data[mx*y + x] = color;
|
|
}
|
|
}
|
|
|
|
void save_to_file(string filename) {
|
|
ofstream writefile;
|
|
writefile.open(filename);
|
|
if (writefile.is_open()){
|
|
writefile << mx << "\t" << my << "\n";
|
|
for (uint y = 0; y < my; y++)
|
|
{
|
|
for (uint x = 0; x < mx; x++)
|
|
{
|
|
writefile << data[mx * y + x] << "\t";
|
|
}
|
|
writefile << "\n";
|
|
}
|
|
writefile.close();
|
|
} else
|
|
{
|
|
cerr << "saving to file failed\n";
|
|
}
|
|
|
|
}
|
|
|
|
void read_from_file(string filename) {
|
|
string line;
|
|
ifstream readfile;
|
|
readfile.open(filename);
|
|
if(readfile.is_open()) {
|
|
// TODO
|
|
|
|
} else {
|
|
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();
|
|
b.copy(&a);
|
|
if (b.compare(&a) == true) {
|
|
cout << "a == b after copy\n";
|
|
} else
|
|
{
|
|
cout << "a != b after copy!\n";
|
|
}
|
|
|
|
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");
|
|
return 0;
|
|
} |