Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-png

Source of release 0.10.1

0.10.1 / tests / dump.lucb

## Decodes one PNG through the Raster API and writes its samples: `dump <in.png>
## <out.raw>`, a 12-byte header (width, height, channels as little-endian u32) then
## each sample as a little-endian u16.
from luce_std import files
import c
import memory
import png
from luce_raster.model import Raster
from luce_raster.binary import Buffer

pub func main(arguments: str[]) -> i32!:
    var raster = Raster()
    defer raster.close()
    let file = try files.read((c.str)arguments[1])
    defer file.release()
    # The raster frees its bytes on the heap, so it keeps a copy of them.
    raster.encoded = try new u8[file.value.length] --- in memory.heap
    memory.copy(raster.encoded, file.value, file.value.length)
    try png.probe(&raster)
    try raster.allocate()
    try png.decode(&raster)
    var raw = Buffer()
    defer raw.close()
    try raw.uint((u64)raster.width, 4)
    try raw.uint((u64)raster.height, 4)
    try raw.uint((u64)raster.channels.length, 4)
    for value in raster.pixels:
        try raw.uint((u64)value, 2)
    try files.write((c.str)arguments[2], raw.view())
    return 0