-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseamcarve.c
More file actions
41 lines (36 loc) · 896 Bytes
/
seamcarve.c
File metadata and controls
41 lines (36 loc) · 896 Bytes
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
#define at(x,y) (1+(x)+(y)*(1+w))
#define min(a,b) ((a)<(b)?(a):(b))
int seamcarve(int w, int h, int* values, int* output) {
for(int y = h-2; y >= 0; y--) {
for(int x = 0; x < w; x++) {
int minst = values[at(x,y+1)];
minst = min(minst, values[at(x-1,y+1)]);
minst = min(minst, values[at(x+1,y+1)]);
values[at(x,y)] += minst;
}
}
//first find best starting point
int pos = 0;
int val = 0x7FFFFFFF;
for(int x = 0; x < w; x++) {
if(values[at(x,0)] < val) {
val = values[at(x,0)];
pos = x;
}
}
output[0] = pos;
for(int y = 1; y < h; y++) {
int npos = pos;
int val = values[at(pos,y)];
if(values[at(pos+1,y)]<val) {
npos = pos+1;
val = values[at(pos+1,y)];
}
if(values[at(pos-1,y)]<val) {
npos = pos-1;
val = values[at(pos-1,y)];
}
output[y] = pos = npos;
}
return 0;
}