-
Notifications
You must be signed in to change notification settings - Fork 0
/
practicum.cpp
125 lines (111 loc) · 2.99 KB
/
practicum.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include<iostream>
#include<fstream>
#include<string>
#include<cstring>
using namespace std;
void cutBMP(const string& sourceFile, const string& newFile, int x, int y, int width, int height) {
ofstream writeFile(newFile, ios::out | ios::binary);
ifstream readFile(sourceFile, ios::in | ios::binary);
if (!readFile) {
cerr << "Error while opening the source BMP file!" << endl;
}
char BMPSourceFileHeader[54];
readFile.read((char*)&BMPSourceFileHeader, 54);
int sourceFileWidth = *(int*)&BMPSourceFileHeader[18];
int sourceFileHeight = *(int*)&BMPSourceFileHeader[22];
readFile.seekg(54+((y*sourceFileWidth)+x)*3);
int filesize = 54 + 3 * width * height;
char bmpfileheader[14] = { 'B','M', 0,0,0,0, 0,0,0,0, 54,0,0,0 };
char exitBMPinfoHeader[40] = { 40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0,24,0 };
bmpfileheader[2] = (char)(filesize);
bmpfileheader[3] = (char)(filesize >> 8);
bmpfileheader[4] = (char)(filesize >> 16);
bmpfileheader[5] = (char)(filesize >> 24);
exitBMPinfoHeader[4] = (char)(width);
exitBMPinfoHeader[5] = (char)(width >> 8);
exitBMPinfoHeader[6] = (char)(width >> 16);
exitBMPinfoHeader[7] = (char)(width >> 24);
exitBMPinfoHeader[8] = (char)(height);
exitBMPinfoHeader[9] = (char)(height >> 8);
exitBMPinfoHeader[10] = (char)(height >> 16);
exitBMPinfoHeader[11] = (char)(height >> 24);
writeFile.write((const char*)bmpfileheader, 14);
writeFile.write((const char*)exitBMPinfoHeader, 40);
char currentPixel[3];
for (int i = 0; i < height * 3; i++)
{
for (int j = 0; j < 3 * width; j += 3)
{
readFile.read((char*)& currentPixel, 3 * sizeof(char));
writeFile.write((const char*)& currentPixel, 3 * sizeof(char));
}
readFile.seekg((sourceFileWidth - width) * 3 , ios::cur);
}
}
string generateString(string line,int &count,char stop)
{
string name="";
while (true)
{
if (line[count] == stop)
{
count++;
break;
}
else
{
name += line[count];
count++;
}
}
return name;
}
void readFromFile()
{
string name,line;
cout << "Enter file name:" << endl;
cin >> name;
ifstream readFile(name, ios::in);
while (getline(readFile,line))
{
cout << line << endl;
if (line[0] != ';')
{
string imageToCut="";
string newImage="",cur="";
int count = 0,x,y,width,height;
imageToCut=generateString(line, count, '(');
cur=generateString(line, count, ',');
x = atoi(cur.c_str());
cur = "";
cur=generateString(line, count, ',');
y = atoi(cur.c_str());
cur = "";
cur=generateString(line, count, ',');
width = atoi(cur.c_str());
cur = "";
cur=generateString(line, count, ')');
height = atoi(cur.c_str());
cur = "";
while (true)
{
if (count == line.length())
{
break;
}
else
{
newImage += line[count];
count++;
}
}
cutBMP(imageToCut, newImage, x, y, width, height);
}
}
}
int main()
{
readFromFile();
system("pause");
return 0;
}