mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
samples: add Terra samples
source: https://github.com/zdevito/terra/tree/master/tests
This commit is contained in:
68
samples/Terra/blocking2-fixed.t
Normal file
68
samples/Terra/blocking2-fixed.t
Normal file
@@ -0,0 +1,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})
|
||||
Reference in New Issue
Block a user