-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.nim
77 lines (66 loc) · 2.06 KB
/
image.nim
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import streams, strformat
type
Rgb* = tuple[r: uint8, g: uint8, b: uint8]
TargaImg = object
header: array[18, uint8]
pixels: seq[Rgb]
PpmImg = object
lines: seq[string]
Writeable* = concept w
var s: Stream
s.write(w)
template writeTo*(w: Writeable, fileName: string) =
let file = newFileStream(fileName, FileMode.fmReadWrite)
file.write(w)
file.flush()
file.close()
#TOOD: access rgb data directly
template toOpenArray*(rgb: Rgb) : openArray[uint8] = toOpenArray([rgb[0], rgb[1], rgb[2]], 0, 2)
iterator ritems*[T](a: seq[T]): T {.inline.} =
## Iterates over each item of `a` backwards
var i = len(a)
let L = len(a)
while i > 0:
dec(i)
yield a[i]
assert(len(a) == L, "the length of the seq changed while iterating over it")
proc clear*[T](seq: var seq[T]) =
seq.setLen(0)
proc newTarga*(width: int32, height: int32, pixels: seq[Rgb]) : TargaImg =
var tga = TargaImg()
tga.header[2] = 2
tga.header[12] = uint8(255 and width)
tga.header[13] = uint8(255 and (width shr 8))
tga.header[14] = uint8(255 and height)
tga.header[15] = uint8(255 and (height shr 8))
tga.header[16] = 24u8
#tga.header[17] = 32u8
var row = newSeq[Rgb]()
#from botton to up
for pixel in pixels.ritems:
row.add(pixel)
if row.len < width:
continue
#"unreverse" row
for pixel2 in row.ritems:
tga.pixels.add pixel2
row.clear()
return tga
proc newPpm*(width: int32, height: int32, pixels: seq[Rgb]) : PpmImg =
var ppm = PpmImg()
ppm.lines.add "P3"
ppm.lines.add fmt"{width} {height}"
ppm.lines.add "255"
for pixel in pixels:
ppm.lines.add fmt"{pixel[0]} {pixel[1]} {pixel[2]}"
return ppm
proc write*(strm: Stream, tga: TargaImg) =
strm.write(tga.header)
#targa is BGR not RGB
for pixel in tga.pixels:
strm.write(pixel.b)
strm.write(pixel.g)
strm.write(pixel.r)
proc write*(strm: Stream, ppm: PpmImg) =
for line in ppm.lines:
strm.writeLine(line)