Bitmap/Write a PPM file: Difference between revisions

added alternate Lua implementation
m (→‎{{header|Sidef}}: Fix link: Perl 6 --> Raku)
(added alternate Lua implementation)
Line 722:
 
=={{header|Lua}}==
===Original===
<lang lua>
 
Line 831 ⟶ 832:
example_colorful_stripes():writeP6('p6.ppm')
</lang>
===Alternate===
Uses the alternate Bitmap implementation [[Bitmap#Alternate|here]], extending it with..
<lang lua>Bitmap.savePPM = function(self, filename)
local fp = io.open(filename, "wb")
if fp == nil then return false end
fp:write(string.format("P6\n%d %d\n%d\n", self.width, self.height, 255))
for y = 1, self.height do
for x = 1, self.width do
local pix = self.pixels[y][x]
fp:write(string.char(pix[1]), string.char(pix[2]), string.char(pix[3]))
end
end
fp:close()
return true
end</lang>
Example usage:
<lang lua>local bitmap = Bitmap(11,5)
bitmap:clear({255,255,255})
for y = 1, 5 do
for x = 1, 11 do
if x==1 or x==5 or x==7 or (y>1 and (x==9 or x==11)) or (y==5 and x~=4 and x~=8 and x~=10) or (x==10 and (y==1 or y==3)) then
bitmap:set(x-1, y-1, {0,0,0}) -- creates "LUA" with 3x5 font
end
end
end
bitmap:savePPM("lua3x5.ppm")</lang>
 
=={{header|M2000 Interpreter}}==
Anonymous user