-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageHistory.cpp
More file actions
124 lines (122 loc) · 3.31 KB
/
ImageHistory.cpp
File metadata and controls
124 lines (122 loc) · 3.31 KB
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
#include "ImageHistory.h"
ImageHistory::ImageHistory(Image* i) {
name = i->getName();
changes.push_back(i->getCopy());
}
ImageHistory::ImageHistory(std::string n, Image* i) {
name = n;
changes.push_back(i->getCopy());
}
ImageHistory::ImageHistory(const ImageHistory& other) {
name = other.name;
for (unsigned int i = 0; i < other.changes.size(); i++) {
Image* im = other.changes[i]->getCopy();
changes.push_back(im);
}
list_of_changes = other.list_of_changes;
}
ImageHistory& ImageHistory::operator=(const ImageHistory& other) {
if (this != &other) {
name = other.name;
for (unsigned int i = 0; i < other.changes.size(); i++) {
Image* im = other.changes[i]->getCopy();
changes.push_back(im);
}
}
list_of_changes = other.list_of_changes;
return *this;
}
void ImageHistory::deleteHistory() {
for (unsigned int i = 0; i < changes.size(); i++) {
delete changes[i];
}
changes.clear();
}
ImageHistory::~ImageHistory(){
deleteHistory();
}
bool ImageHistory::anyChanges() {
return (list_of_changes.size() != 0);
}
void ImageHistory::undoChange() {
if (list_of_changes.size() <= 0) {
throw std::exception("No changes to be undone");
}
delete changes.back();
changes.pop_back();
list_of_changes.pop_back();
}
Image* ImageHistory::getLastChange()const {
return changes.back();
}
Image* ImageHistory::getOriginal()const {
return changes.front();
}
void ImageHistory::addChange(std::string command) {
if (command == "undo")
undoChange();
else if (command == "monochrome") {
Image* i = changes.back()->getCopy();
i->monochrome();
changes.push_back(i);
list_of_changes.push_back("monochrome");
}
else if (command == "grayscale") {
Image* i = changes.back()->getCopy();
i->grayscale();
changes.push_back(i);
list_of_changes.push_back("grayscale");
}
else if (command == "negative") {
Image* i = changes.back()->getCopy();
i->negative();
changes.push_back(i);
list_of_changes.push_back("negative");
}
else if (command == "rotateleft") {
Image* i = changes.back()->getCopy();
i->rotate("left");
changes.push_back(i);
list_of_changes.push_back("rotate left");
}
else if (command == "rotateright") {
Image* i = changes.back()->getCopy();
i->rotate("right");
changes.push_back(i);
list_of_changes.push_back("rotate right");
}
else std::cout << "Invalid command!\n";
}
void ImageHistory::printChanges() {
std::cout << "Changes over \"" << name << "\" are:\n";
if (anyChanges()){
for (unsigned int i = 0; i < list_of_changes.size(); i++)
std::cout << i + 1 << '.' << list_of_changes[i] << '\n';
}
else std::cout << "none\n";
}
void ImageHistory::save() {
Image* i = changes.back()->getCopy();
deleteHistory();
list_of_changes.clear();
changes.push_back(i);
if (i->getMagicNumber() == "P4" || i->getMagicNumber() == "P5" || i->getMagicNumber() == "P6"){
std::fstream out(i->getName(), std::ios::out | std::ios::binary);
i->save(out);
out.close();
}
else{
std::fstream out(i->getName(), std::ios::out);
i->save(out);
out.close();
}
}
void ImageHistory::saveAs(std::string _name) {
Image* i = changes.back()->getCopy();
deleteHistory();
list_of_changes.clear();
changes.push_back(i);
std::fstream out(_name, std::ios::out);
i->save(out);
out.close();
}