Anonymous user
Bitmap: Difference between revisions
Changed "x" and "y" by "col" and "row"; modified "print"; added "print" in example.
(Added export marker for Image fields.) |
(Changed "x" and "y" by "col" and "row"; modified "print"; added "print" in example.) |
||
Line 2,782:
for x in 0 ..< img.w:
for y in 0 ..< img.h:
yield (x, y)type
Luminance* = uint8
Index* = int
Pixel* = tuple
proc `[]`*(img: Image, x, y: int): Pixel =▼
r, g, b: Luminance
img.pixels[y * img.w + x]▼
Image* = object
proc `[]=`*(img: var Image, x, y: int, c: Pixel) =▼
w*, h*: Index
img.pixels[y * img.w + x] = c▼
pixels*: seq[Pixel]
Point* = tuple
col, row: Index
proc px*(r, g, b: SomeInteger): Pixel =
## Build a pixel value from R, G and B values.
result.r = r.uint8
result.g = g.uint8
result.b = b.uint8
const
Black* = px( 0, 0, 0)
White* = px(255, 255, 255)
proc initImage*(width, height: int): Image =
## Create an image with given width and height.
result.w = width
result.h = height
result.pixels.setLen(width * height)
iterator indices*(img: Image): Point =
## Return a tuple
for row in 0 ..< img.h:
for col in 0 ..< img.w:
yield (col, row)
proc fill*(img: var Image, color: Pixel) =
for
img[
proc print*(img: Image) =
for
stdout.write '\n'
stdout.write if img[col, row] == White: '.' else: 'H'
stdout.write '\n'
when isMainModule:
var
img.print
</lang>
|