55 lines
1.4 KiB
Text
55 lines
1.4 KiB
Text
T Colour = BVec3
|
||
|
||
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 togreyscale()
|
||
L(h) 0 .< .height
|
||
L(w) 0 .< .width
|
||
V (r, g, b) = .get(w, h)
|
||
V l = Int(0.2126 * r + 0.7152 * g + 0.0722 * b)
|
||
.set(w, h, Colour(l, l, l))
|
||
|
||
F writeppmp3()
|
||
V magic = "P3\n"
|
||
V comment = "# generated from Bitmap.writeppmp3\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
|
||
|
||
V bitmap = Bitmap(4, 4, white)
|
||
bitmap.fillrect(1, 0, 1, 2, Colour(127, 0, 63))
|
||
bitmap.set(3, 3, Colour(0, 127, 31))
|
||
print(‘Colour:’)
|
||
print(bitmap.writeppmp3())
|
||
print(‘Grey:’)
|
||
bitmap.togreyscale()
|
||
print(bitmap.writeppmp3())
|