-
Notifications
You must be signed in to change notification settings - Fork 203
/
blocking2-fixed.t
68 lines (63 loc) · 1.68 KB
/
blocking2-fixed.t
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
C = terralib.includec("stdio.h")
terra min(a: int, b: int) : int
if a < b then return a
else return b end
end
std = terralib.includec("stdlib.h")
function Image(PixelType)
local struct ImageImpl {
data : &PixelType,
N : int
}
terra ImageImpl:init(N: int): {} --returns nothing
self.data = [&PixelType](
std.malloc(N*N*sizeof(PixelType)))
self.N = N
end
terra ImageImpl:get(x: int, y: int) : PixelType
return self.data[x*self.N + y]
end
terra ImageImpl:set(x: int, y: int, v : PixelType)
self.data[x*self.N + y] = v
end
terra ImageImpl:save(i : rawstring)
for i = 0, 2 do for j = 0, 2 do
C.printf("%d %d %f\n", i,j,self:get(i,j))
end end
end
terra ImageImpl:load(i : rawstring) self:init(16)
for i = 0, 4 do for j = 0, 4 do
self:set(i,j,(i*4+j)%3)
--C.printf("%f\n",self:get(i,j))
end end
end
terra ImageImpl:free() end
--omitted methods for: set, save, load, free
return ImageImpl
end
GreyscaleImage = Image(float)
terra laplace(img: &GreyscaleImage,
out: &GreyscaleImage) : {}
--shrink result, do not calculate boundaries
var newN = img.N - 2
out:init(newN)
for i = 0,newN do
for j = 0,newN do
var v = img:get(i+0,j+1) + img:get(i+2,j+1)
+ img:get(i+1,j+2) + img:get(i+1,j+0)
- 4 * img:get(i+1,j+1)
out:set(i,j,v)
end
end
end
terra runlaplace(input: rawstring,
output: rawstring) : {}
var i: GreyscaleImage, o : GreyscaleImage
i:load(input)
laplace(&i,&o)
o:save(output)
i:free(); o:free()
end
runlaplace("myinput","myoutput")
terralib.saveobj("runlaplace.o",
{runlaplace = runlaplace})