-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathppm.v
More file actions
63 lines (56 loc) · 1.43 KB
/
ppm.v
File metadata and controls
63 lines (56 loc) · 1.43 KB
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
module main
import packer as pk
import fmt {Category}
@[direct_array_access]
fn encode_ppm(image &C.pixman_image_t, is_hdr bool) []u8 {
width := C.pixman_image_get_width(image)
height := C.pixman_image_get_height(image)
stride := C.pixman_image_get_stride(image)
format := C.pixman_image_get_format(image)
category := Category.new(format)
header := 'P6\n${width} ${height}\n255\n'
pixels := unsafe {&u8(C.pixman_image_get_data(image))}
mut buffer := []u8{}
row_buffer := []u8{len: int(width) * 3}
match category {
._10c_32b {
if is_hdr {
for y in 0 .. height {
unsafe {
row_ptr := &u32(&u8(pixels) + y * stride)
pk.pack_row32_10_hdr_to_32_8(row_ptr, width, row_buffer.data, true, format)
}
buffer << row_buffer
}
} else {
for y in 0 .. height {
unsafe {
row_ptr := &u32(&u8(pixels) + y * stride)
pk.pack_row32_10_to_32_8(row_ptr, width, row_buffer.data, true, format)
}
buffer << row_buffer
}
}
}
._8c_32b {
for y in 0 .. height {
unsafe {
row_ptr := &u32(&u8(pixels) + y * stride)
pk.pack_row32_8(row_ptr, width, row_buffer.data, true, format)
}
buffer << row_buffer
}
}
._8c_24b {
for y in 0 .. height {
unsafe {
row_ptr := &u32(&u8(pixels) + y * stride)
pk.pack_row24_8(&u8(row_ptr), width, row_buffer.data, format)
}
buffer << row_buffer
}
}
}
buffer.prepend(header.bytes())
return buffer
}