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 `[]`*(img: Image, xcol, yrow: int): Pixel =
img.pixels[yrow * img.w + xcol]
 
proc `[]=`*(img: var Image, xcol, yrow: int, c: Pixel) =
img.pixels[yrow * img.w + xcol] = c
 
proc fill*(img: var Image, color: Pixel) =
for xcol,y row in img.indices:
img[xcol, yrow] = color
 
proc print*(img: Image) =
for xcol,y row in img.indices:
echo if col mod img[x,y].w == White0: ' ' else: 'H'
stdout.write '\n'
stdout.write if img[col, row] == White: '.' else: 'H'
stdout.write '\n'
 
when isMainModule:
var ximg = imginitImage(64100, 6420)
ximg.fill px(255, 255, 255)
ximg[1, 2] = px(255, 0, 0)
ximg[3, 4] = ximg[1, 2]
img.print
</lang>
 
Anonymous user