-
Notifications
You must be signed in to change notification settings - Fork 5
/
ppm.jl
48 lines (44 loc) · 995 Bytes
/
ppm.jl
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
function save(name, img)
open("$name", "w") do f
w, h = size(img)
write(f, collect(Uint8, "P6 $w $h 255\n"))
for p in img
psize = size(p, 1)
if psize == 3
write(f, uint8(p[1]))
write(f, uint8(p[2]))
write(f, uint8(p[3]))
elseif psize == 1
write(f, uint8(p))
write(f, uint8(p))
write(f, uint8(p))
else
error("Weird-dimensional object given to ppm.save. size(obj, 1) = $psize")
end
end
end
end
function save_and_convert(name, img)
save(name, img)
outname = "$name.png"
run(`convert ppm:$name png:$outname`)
isfile(name) && rm(name)
outname
end
function png_data(img)
(ppmpath, _) = mktemp()
(pngpath, _) = mktemp()
save(ppmpath, img)
run(`convert ppm:$ppmpath png:$pngpath`)
f = open(pngpath)
output = readbytes(f)
close(f)
isfile(ppmpath) && rm(ppmpath)
isfile(pngpath) && rm(pngpath)
output
end
function s(name::String, img)
run(`mkdir -p output`)
outname = save_and_convert("output/$name", img)
run(`open $outname`)
end