about summary refs log tree commit diff stats
path: root/src/img/bitmap.nim
blob: c5df19fc1b83fa8a3dca2d40dd121ce0b9e9092c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import types/color

type
  Bitmap* = ref object of RootObj
    px*: seq[RGBAColorBE]
    width*: uint64
    height*: uint64

  ImageBitmap* = ref object of Bitmap

  NetworkBitmap* = ref object of Bitmap
    cacheId*: int
    imageId*: int
    contentType*: string

proc newBitmap*(width, height: uint64): ImageBitmap =
  return ImageBitmap(
    px: newSeq[RGBAColorBE](width * height),
    width: width,
    height: height
  )

proc setpx*(bmp: Bitmap; x, y: uint64; color: RGBAColorBE) {.inline.} =
  bmp.px[bmp.width * y + x] = color

proc setpx*(bmp: Bitmap; x, y: uint64; color: ARGBColor) {.inline.} =
  bmp.px[bmp.width * y + x] = rgba_be(color.r, color.g, color.b, color.a)

proc getpx*(bmp: Bitmap; x, y: uint64): RGBAColorBE {.inline.} =
  return bmp.px[bmp.width * y + x]

proc setpxb*(bmp: Bitmap; x, y: uint64; c: RGBAColorBE) {.inline.} =
  if c.a == 255:
    bmp.setpx(x, y, c)
  else:
    bmp.setpx(x, y, bmp.getpx(x, y).blend(c))

proc setpxb*(bmp: Bitmap; x, y: uint64; c: ARGBColor) {.inline.} =
  bmp.setpxb(x, y, rgba_be(c.r, c.g, c.b, c.a))