-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathseamcarving.cpp
109 lines (96 loc) · 3.54 KB
/
seamcarving.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
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <cmath>
#include "functions.h"
using namespace std;
int main() {
string filename;
int width = 0;
int height = 0;
int targetWidth = 0;
int targetHeight = 0;
// Add code to validate input (Do in part 1)
cout << "Input filename: ";
cin >> filename;
cout << "Input width and height: ";
// Get the width and verify it.
cin >> width;
if (!cin.good()) {
cout << "Error: width is a non-integer value" << endl;
return -1;
}
if (width <= 0) {
cout << "Error: width must be greater than 0. You entered " << width << endl;
return -1;
}
// Get the height and verify it.
cin >> height;
if (!cin.good()) {
cout << "Error: height is a non-integer value" << endl;
return -1;
}
if (height <= 0) {
cout << "Error: height must be greater than 0. You entered " << height << endl;
return -1;
}
cout << "Input target width and target height: ";
// Get the target width and verify it.
cin >> targetWidth;
if (!cin.good()) {
cout << "Error: target width is a non-integer value" << endl;
return -1;
}
if (targetWidth <= 0) {
cout << "Error: target width must be greater than 0. You entered " << targetWidth << endl;
return -1;
}
// Get the target height and verify it.
cin >> targetHeight;
if (!cin.good()) {
cout << "Error: target height is a non-integer value" << endl;
return -1;
}
if (targetHeight <= 0) {
cout << "Error: target height must be greater than 0. You entered " << targetHeight << endl;
return -1;
}
// Verify width and height are less than the target width and height.
if (targetWidth > width) {
cout << "Error: target width must be less than width, " << targetWidth << " is greater than " << width << endl;
return -1;
}
if (targetHeight > height) {
cout << "Error: target height must be less than height, " << targetHeight << " is greater than " << height << endl;
return -1;
}
int originalWidth = width; // need to delete image array at end of program
Pixel ** image = createImage(width, height); // create array of size that we need
if (image != nullptr) {
if (loadImage(filename, image, width, height)) {
cout << "Start carving..." << endl;
// Add code to remove seams from image (Do in part 2)
while (width > targetWidth || height > targetHeight) {
if (width > targetWidth) {
int* vertSeam = findMinVerticalSeam(image, width, height);
removeVerticalSeam(image, width, height, vertSeam);
width--;
deleteSeam(vertSeam);
}
if (height > targetHeight) {
int* horSeam = findMinHorizontalSeam(image, width, height);
removeHorizontalSeam(image, width, height, horSeam);
height--;
deleteSeam(horSeam);
}
}
// set up output filename
stringstream ss;
ss << "carved" << width << "X" << height << "." << filename;
outputImage(ss.str().c_str(), image, width, height);
}
// call last to remove the memory from the heap
deleteImage(image, originalWidth);
}
}