-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmp.pony
92 lines (77 loc) · 2.15 KB
/
bmp.pony
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use "buffered"
use "collections"
use "files"
use "random"
use "time"
class val RGB
let _r: U64
let _g: U64
let _b: U64
new val create(r: U64, g: U64, b: U64) =>
_r = r
_g = g
_b = b
fun value(): (U64, U64, U64) =>
(_r, _g, _b)
class Bitmap
let _pix_array_offset: USize
let _width: USize
let _height: USize
let _row_size: USize
let _row_padding: USize
let _pixels: Array[RGB]
new read_from(bmp_data: Array[U8] val) ? =>
let reader: Reader = Reader.>append(bmp_data)
reader.skip(10)? // read to pix array offset
_pix_array_offset = reader.u32_le()?.usize()
reader.skip(4)?
_width = reader.u32_le()?.usize()
_height = reader.u32_le()?.usize()
_pixels = Array[RGB]
_row_size = (((3 * _width) + 3) / 4) * 4
_row_padding = _row_size - (3 * _width)
reader.clear()
reader.append(bmp_data)
reader.skip(_pix_array_offset)?
for _ in Range(0, _height) do
for _ in Range(0, _width) do
let b = reader.u8()?.u64()
let g = reader.u8()?.u64()
let r = reader.u8()?.u64()
_pixels.push(RGB(r, g, b))
end
reader.skip(_row_padding)?
end
fun string(): String iso^ =>
" ".join([
"h=" + _height.string()
"w=" + _width.string()
"rs=" + _row_size.string()
].values())
fun bw_image(): String =>
let rows = Array[String ref]
for h in Range(0, _height) do
let s = String
for w in Range(0, _width) do
let p = try _pixels(((_height - h - 1) * _width) + w)?.value() else (0, 0, 0) end
s.append(if ((0.3 * p._1.f64()) + (0.59 * p._2.f64()) + (0.11 * p._3.f64())) > 0x80 then
" "
else
"X"
end)
end
rows.push(s)
end
"\n".join(rows.values())
fun intensity(): Array[Array[F64]] =>
let rows = Array[Array[F64]]
for w in Range(0, _width) do
let s = Array[F64]
for h in Range(0, _height) do
let p = try _pixels(((_height - h - 1) * _width) + w)?.value() else (0, 0, 0) end
let i = ((0.3 * p._1.f64()) + (0.59 * p._2.f64()) + (0.11 * p._3.f64())) / 0xFF
s.push(i)
end
rows.push(s)
end
rows