-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSeamCarver.java
163 lines (144 loc) · 7.14 KB
/
SeamCarver.java
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import edu.princeton.cs.algs4.Picture;
import java.awt.*;
//import edu.princeton.cs.introcs.Picture;
public class SeamCarver {
final private Picture picture;
final private int[][] color;
final private double[][] energy;
private boolean flag = false;
public SeamCarver(Picture picture) {
this.picture = new Picture(picture);
this.color = new int[this.picture.width()][this.picture.height()];
this.energy = new double[this.picture.width()][this.picture.height()];
for (int x = 0; x < this.picture.width(); x++) {
for (int y = 0; y < this.picture.height(); y++) {
Color c = this.picture.get(x, y);
this.color[x][y] = c.getRGB(); // Store the full RGB value
}
}
for (int x = 0; x < this.picture.width(); x++) {
for (int y = 0; y < this.picture.height(); y++) {
this.energy[x][y] = this.energy(x, y);
}
}
this.flag = true;
}
public Picture picture() {
return new Picture(this.picture);
} // current picture
public int width() {
return this.picture.width();
} // width of current picture
public int height() {
return this.picture.height();
} // height of current picture
public double energy(int x, int y) {
if (x < 0 || x >= this.picture.width() || y < 0 || y >= this.picture.height()) {
throw new IndexOutOfBoundsException();
}
if (flag) {
return this.energy[x][y];
}
int Rx, Gx, Bx;
int Ry, Gy, By;
Rx = Math.abs(this.color((x - 1 + this.picture.width()) % this.picture.width(),y, 'R') - this.color((x + 1) % this.picture.width(), y, 'R'));
Gx = Math.abs(this.color((x - 1 + this.picture.width()) % this.picture.width(),y, 'G') - this.color((x + 1) % this.picture.width(), y, 'G'));
Bx = Math.abs(this.color((x - 1 + this.picture.width()) % this.picture.width(),y, 'B') - this.color((x + 1) % this.picture.width(), y, 'B'));
Ry = Math.abs(this.color(x,(y - 1 + this.picture.height()) % this.picture.height(), 'R') - this.color(x, (y + 1) % this.picture.height(), 'R'));
Gy = Math.abs(this.color(x,(y - 1 + this.picture.height()) % this.picture.height(), 'G') - this.color(x, (y + 1) % this.picture.height(), 'G'));
By = Math.abs(this.color(x,(y - 1 + this.picture.height()) % this.picture.height(), 'B') - this.color(x, (y + 1) % this.picture.height(), 'B'));
return Math.pow(Rx, 2) + Math.pow(Ry, 2) + Math.pow(Gx, 2) + Math.pow(Gy, 2) + Math.pow(Bx, 2) + Math.pow(By, 2);
} // energy of pixel at column x and row y
private int color(int x, int y, char type) {
switch (type) {
case 'R':
return (this.color[x][y] & 0xff0000) >> 16;
case 'G':
return (this.color[x][y] & 0xff00) >> 8;
case 'B':
return (this.color[x][y] & 0xff);
default:
throw new RuntimeException("parameter is not RGB type");
}
}
public int[] findHorizontalSeam() {
double[][] minEnergy = new double[this.picture.width()][this.picture.height()];
for (int x = 0; x < this.picture.width(); x++) {
for (int y = 0; y < this.picture.height(); y++) {
if (x == 0) {
minEnergy[x][y] = this.energy(x, y);
} else {
if (y == 0) {
minEnergy[x][y] = this.energy[x][y] + Math.min(minEnergy[x - 1][y], minEnergy[x - 1][(y + 1) % this.picture.height()]);
} else if (y == this.picture.height() - 1) {
minEnergy[x][y] = this.energy[x][y] + Math.min(minEnergy[x - 1][y], minEnergy[x - 1][(y - 1 + this.picture.height()) % this.picture.height()]);
} else {
minEnergy[x][y] = this.energy[x][y] + Math.min(minEnergy[x - 1][(y - 1 + this.picture.height()) % this.picture.height()], Math.min(minEnergy[x - 1][y], minEnergy[x - 1][(y + 1) % this.picture.height()]));
}
}
}
}
int[] seam = new int[this.picture.width()];
int upper = this.picture.height() - 1, lower = 0;
for (int x = this.picture.width() - 1; x >= 0; x--) {
int row = -1;
double min = Double.MAX_VALUE;
for (int y = lower; y <= upper; y++) {
if (minEnergy[x][y] < min) {
row = y;
min = minEnergy[x][y];
}
}
seam[x] = row;
lower = row == 0 ? 0 : row - 1;
upper = row == this.picture.height() - 1 ? this.picture.height() - 1 : row + 1;
}
return seam;
} // sequence of indices for horizontal seam
public int[] findVerticalSeam() {
double[][] minEnergy = new double[this.picture.width()][this.picture.height()];
for (int y = 0; y < this.picture.height(); y++) {
for (int x = 0; x < this.picture.width(); x++) {
if (y == 0) {
minEnergy[x][y] = this.energy(x, y);
} else {
if (x == 0) {
minEnergy[x][y] = this.energy[x][y] + Math.min(minEnergy[x][y - 1], minEnergy[(x + 1) % this.picture.width()][y - 1]);
} else if (x == this.picture.width() - 1) {
minEnergy[x][y] = this.energy[x][y] + Math.min(minEnergy[x][y - 1], minEnergy[(x - 1 + this.picture.width()) % this.picture.width()][y - 1]);
} else {
minEnergy[x][y] = this.energy[x][y] + Math.min(minEnergy[x][y - 1], Math.min(minEnergy[(x - 1 + this.picture.width()) % this.picture.width()][y - 1], minEnergy[(x + 1) % this.picture.width()][y - 1]));
}
}
}
}
int[] seam = new int[this.picture.height()];
int right = this.picture.width() - 1, left = 0;
for (int y = this.picture.height() - 1; y >= 0; y--) {
int column = -1;
double min = Double.MAX_VALUE;
for (int x = left; x <= right; x++) {
if (minEnergy[x][y] < min) {
column = x;
min = minEnergy[x][y];
}
}
seam[y] = column;
left = column == 0 ? 0 : column - 1;
right = column == this.picture.width() - 1 ? this.picture.width() - 1 : column + 1;
}
return seam;
} // sequence of indices for vertical seam
public void removeHorizontalSeam(int[] seam) {
if (seam.length != picture().width()) {
throw new IllegalArgumentException();
}
SeamRemover.removeHorizontalSeam(this.picture, seam);
} // remove horizontal seam from picture
public void removeVerticalSeam(int[] seam) {
if (seam.length != picture().height()) {
throw new IllegalArgumentException();
}
SeamRemover.removeVerticalSeam(this.picture, seam);
} // remove vertical seam from picture
}