Bitmap/Write a PPM file: Difference between revisions

Content added Content deleted
(Added Wren)
(Added 11l)
Line 6: Line 6:
(Read [[wp:Netpbm_format|the definition of PPM file]] on Wikipedia.)
(Read [[wp:Netpbm_format|the definition of PPM file]] on Wikipedia.)
<br><br>
<br><br>

=={{header|11l}}==
{{trans|Python}}

<lang 11l>T Colour
Byte r, g, b

F (r, g, b)
.r = r
.g = g
.b = b

F ==(other)
R .r == other.r & .g == other.g & .b == other.b

V black = Colour(0, 0, 0)
V white = Colour(255, 255, 255)

T Bitmap
Int width, height
Colour background
[[Colour]] map

F (width = 40, height = 40, background = white)
assert(width > 0 & height > 0)
.width = width
.height = height
.background = background
.map = (0 .< height).map(h -> (0 .< @width).map(w -> @@background))

F fillrect(x, y, width, height, colour = black)
assert(x >= 0 & y >= 0 & width > 0 & height > 0)
L(h) 0 .< height
L(w) 0 .< width
.map[y + h][x + w] = colour

F set(x, y, colour = black)
.map[y][x] = colour

F get(x, y)
R .map[y][x]

F writeppmp3()
V magic = "P3\n"
V comment = "# generated from Bitmap.writeppm3\n"
V s = magic‘’comment‘’("#. #.\n#.\n".format(.width, .height, 255))
L(h) (.height - 1 .< -1).step(-1)
L(w) 0 .< .width
V (r, g, b) = .get(w, h)
s ‘’= ‘ #3 #3 #3’.format(r, g, b)
s ‘’= "\n"
R s

F writeppm6()
V magic = "P6\n"
V comment = "# generated from Bitmap.writeppm6\n"
[Byte] b
b [+]= magic.encode()
b [+]= comment.encode()
b [+]= ("#. #.\n#.\n".format(.width, .height, 255)).encode()
L(h) (.height - 1 .< -1).step(-1)
L(w) 0 .< .width
V (r, g, bl) = .get(w, h)
b [+]= [r, g, bl]
R b

V bitmap = Bitmap(4, 4, black)
bitmap.fillrect(1, 0, 1, 2, white)
bitmap.set(3, 3, Colour(127, 0, 63))
print(bitmap.writeppmp3())

File(‘tmp.ppm’, ‘w’).write_bytes(bitmap.writeppm6())</lang>

{{out}}
<pre>
P3
# generated from Bitmap.writeppm3
4 4
255
0 0 0 0 0 0 0 0 0 127 0 63
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 255 255 255 0 0 0 0 0 0
0 0 0 255 255 255 0 0 0 0 0 0
</pre>


=={{header|Ada}}==
=={{header|Ada}}==