Skip to content

Commit 74f2ba7

Browse files
committed
add seam carve
1 parent e298a5c commit 74f2ba7

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

seamCarve/extract.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env python3
2+
3+
from subprocess import call
4+
import base64
5+
6+
call(["gcc", "-O2", "-c", "seamcarve.c", "-o", "seamcarve.o"])
7+
call(["objcopy", "--only-section=.text", "-O", "binary", "seamcarve.o", "text.o"])
8+
with open("text.o", "rb") as fil:
9+
with open("text.py", "w") as ut:
10+
ut.write(str(fil.read()))
11+
#ut.write(f"base64.standard_b64decode({base64.standard_b64encode(fil.read())})")

seamCarve/seamcarve.c

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
#define at(x,y) (1+(x)+(y)*(1+w))
3+
#define min(a,b) ((a)<(b)?(a):(b))
4+
5+
int seamcarve(int w, int h, int* values, int* output) {
6+
for(int y = h-2; y >= 0; y--) {
7+
for(int x = 0; x < w; x++) {
8+
int minst = values[at(x,y+1)];
9+
minst = min(minst, values[at(x-1,y+1)]);
10+
minst = min(minst, values[at(x+1,y+1)]);
11+
values[at(x,y)] += minst;
12+
}
13+
}
14+
15+
//first find best starting point
16+
int pos = 0;
17+
int val = 0x7FFFFFFF;
18+
for(int x = 0; x < w; x++) {
19+
if(values[at(x,0)] < val) {
20+
val = values[at(x,0)];
21+
pos = x;
22+
}
23+
}
24+
25+
output[0] = pos;
26+
for(int y = 1; y < h; y++) {
27+
int npos = pos;
28+
int val = values[at(pos,y)];
29+
if(values[at(pos+1,y)]<val) {
30+
npos = pos+1;
31+
val = values[at(pos+1,y)];
32+
}
33+
if(values[at(pos-1,y)]<val) {
34+
npos = pos-1;
35+
val = values[at(pos-1,y)];
36+
}
37+
output[y] = pos = npos;
38+
}
39+
40+
return 0;
41+
}

seamCarve/seamcarve.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import ctypes
2+
import mmap
3+
import array
4+
5+
libc = ctypes.cdll.LoadLibrary(None)
6+
mmap_function = libc.mmap
7+
mmap_function.restype = ctypes.c_void_p
8+
mmap_function.argtypes = (ctypes.c_void_p, ctypes.c_size_t,
9+
ctypes.c_int, ctypes.c_int,
10+
ctypes.c_int, ctypes.c_size_t)
11+
CODE_SIZE = 10000
12+
code_address = mmap_function(None, CODE_SIZE,
13+
mmap.PROT_READ | mmap.PROT_WRITE | mmap.PROT_EXEC,
14+
mmap.MAP_PRIVATE | mmap.MAP_ANONYMOUS,
15+
-1, 0)
16+
if code_address == -1:
17+
raise OSError('mmap failed to allocate memory')
18+
19+
#import base64
20+
code=b'AVD\x8dv\xfeA\x89\xf9H\x89\xd7AUI\x89\xcdATA\x89\xf4USE\x85\xf6xlE\x8dA\x01\x8dF\xffE\x89\xcbE\x89\xf2D\x0f\xaf\xc0A\xf7\xd3A\x8di\xffH\x8dZ\x04\x0f\x1f\x80\x00\x00\x00\x00E\x85\xc9~7Ic\xd0C\x8d\x0c\x03H\x8dt\x15\x00Hc\xc9H\x8d\x04\x97H\x8d4\xb3H)\xd1\x90\x8bP\x049\x10\x0fN\x109P\x08\x0fNP\x08\x01T\x88\x04H\x83\xc0\x04H9\xc6u\xe4A\x83\xea\x01E\x01\xd8A\x83\xfa\xffu\xb7E\x85\xc9\x0f\x8e\x99\x00\x00\x00Mc\xc11\xd2\xbe\xff\xff\xff\x7f1\xc0f\x90\x8bL\x97\x049\xf1}\x04\x89\xce\x89\xd0H\x83\xc2\x01I9\xd0u\xebA\x89E\x00A\x83\xfc\x01~aA\x83\xc1\x01I\x8dM\x04O\x8d\\\xb5\x08D\x89\xca\xeb*\x0f\x1f\x80\x00\x00\x00\x00A\x89\xf0\x8d4\x02\x83\xe8\x01Hc\xf6D9\x04\xb7|\x03D\x89\xd0\x89\x01H\x83\xc1\x04D\x01\xcaI9\xcbt%D\x8dP\x01A\x8d4\x12Hc\xf6D\x8b\x04\xb7\x8dt\x10\x02Hc\xf6\x8b4\xb7D9\xc6|\xbfA\x89\xc2\xeb\xbdf\x90[1\xc0]A\\A]A^\xc31\xc0\xeb\x86'
21+
assert len(code) <= CODE_SIZE
22+
ctypes.memmove(code_address, code, len(code))
23+
24+
seamcarve_type = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p)
25+
seamcarve_c = ctypes.cast(code_address, seamcarve_type)
26+
27+
def find_path(weights):
28+
h = len(weights)
29+
w = len(weights[0])
30+
31+
values = array.array('i', [w for row in weights for w in [0x7FFFFFFF]+row])
32+
values.append(0x7FFFFFFF)
33+
output = array.array('i', [0 for _ in range(h)])
34+
35+
values_address, _ = values.buffer_info()
36+
output_address, _ = output.buffer_info()
37+
38+
seamcarve_c(w, h, values_address, output_address)
39+
40+
return list(zip(output, range(h)))
41+
42+
highscore = True
43+
44+
print(find_path([[1, 10, 3, 3], [1, 10, 3, 3], [10, 10, 3, 3]]))

seamCarve/text.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
b'AVD\x8dv\xfeA\x89\xf9H\x89\xd7AUI\x89\xcdATA\x89\xf4USE\x85\xf6xlE\x8dA\x01\x8dF\xffE\x89\xcbE\x89\xf2D\x0f\xaf\xc0A\xf7\xd3A\x8di\xffH\x8dZ\x04\x0f\x1f\x80\x00\x00\x00\x00E\x85\xc9~7Ic\xd0C\x8d\x0c\x03H\x8dt\x15\x00Hc\xc9H\x8d\x04\x97H\x8d4\xb3H)\xd1\x90\x8bP\x049\x10\x0fN\x109P\x08\x0fNP\x08\x01T\x88\x04H\x83\xc0\x04H9\xc6u\xe4A\x83\xea\x01E\x01\xd8A\x83\xfa\xffu\xb7E\x85\xc9\x0f\x8e\x99\x00\x00\x00Mc\xc11\xd2\xbe\xff\xff\xff\x7f1\xc0f\x90\x8bL\x97\x049\xf1}\x04\x89\xce\x89\xd0H\x83\xc2\x01I9\xd0u\xebA\x89E\x00A\x83\xfc\x01~aA\x83\xc1\x01I\x8dM\x04O\x8d\\\xb5\x08D\x89\xca\xeb*\x0f\x1f\x80\x00\x00\x00\x00A\x89\xf0\x8d4\x02\x83\xe8\x01Hc\xf6D9\x04\xb7|\x03D\x89\xd0\x89\x01H\x83\xc1\x04D\x01\xcaI9\xcbt%D\x8dP\x01A\x8d4\x12Hc\xf6D\x8b\x04\xb7\x8dt\x10\x02Hc\xf6\x8b4\xb7D9\xc6|\xbfA\x89\xc2\xeb\xbdf\x90[1\xc0]A\\A]A^\xc31\xc0\xeb\x86'

0 commit comments

Comments
 (0)