Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-png

Source of release 0.10.0

0.10.0 / tests / tool.lucb

## The gate's driver.
## `tool decode <in.png> <out.raw> <rgba8|rgba16|rows8|rows16|at8|at16> [threads]` writes a
## 12-byte header (width, height, bytes a pixel) and the pixels (16-bit samples
## little-endian); `at` modes read the file in pieces through `decode_rows_at`;
## `coarse` writes an interlaced picture's first sight (Adam7's first pass) instead.
## `tool encode <in.png> <out.png> <level> <filter> <gray8|ga8|rgb8|rgba8|gray16|ga16|rgb16|rgba16>
## <whole|stream> [threads] [band rows]` encodes the input's pixels in that format.
from luce_std import files
import c
import memory
import strings
import png
from luce_raster.binary import Buffer

struct Collected:
    var out: Buffer*
    var next_row: usize
    var failed: bool

func collect(context: void*?, first: usize, rows: usize, bytes: const u8[]) -> !:
    let collected = (Collected*)(context else return)
    if first != collected.next_row:
        collected.failed = true
    collected.next_row = first + rows
    try collected.out.put(bytes)

## A coarse picture kept.
struct Coarse:
    var out: Buffer*
    var calls: usize

func take_coarse(context: void*?, width: usize, height: usize, pixels: const u8[]) -> !:
    let coarse = (Coarse*)(context else return)
    try coarse.out.uint((u64)width, 4)
    try coarse.out.uint((u64)height, 4)
    try coarse.out.uint(4, 4)
    try coarse.out.put(pixels)
    coarse.calls += 1

func ignore_rows(context: void*?, first: usize, rows: usize, bytes: const u8[]) -> !:
    _ = context

## `ReadAt` over an open file.
func read_file(context: void*?, at: u64, into: u8[]) -> usize!:
    let file = (files.File*)(context else return 0)
    _ = try file.seek((i64)at)
    return try file.read(into)

func number(arguments: str[], index: usize, fallback: u64) -> u64:
    if arguments.length <= index:
        return fallback
    return strings.parse_u64(arguments[index]) else fallback

pub func main(arguments: str[]) -> i32!:
    let data_file = try files.read((c.str)arguments[2])
    defer data_file.release()
    let data = data_file.value
    let info = try png.info(data)
    let w = info.width
    let h = info.height
    if arguments[1] == "decode" and arguments[4] == "coarse":
        var picture = Buffer()
        defer picture.close()
        var coarse = Coarse(out = &picture, calls = 0)
        var file = try files.File.open((c.str)arguments[2])
        defer file.destroy()
        try png.decode_rows_at(read_file, (void*)&file, (u64)data.length, ignore_rows, none, options = png.DecodeOptions(coarse = take_coarse, coarse_context = (void*)&coarse))
        if coarse.calls != (1 if info.interlaced else 0):
            print("a coarse picture came for a picture not interlaced, or not once for one that is")
            return 1
        try files.write((c.str)arguments[3], picture.view())
        return 0
    if arguments[1] == "decode":
        let mode = arguments[4]
        let options = png.DecodeOptions(threads = (usize)number(arguments, 5, 0))
        let deep = mode == "rgba16" or mode == "rows16" or mode == "at16"
        var raw = Buffer()
        defer raw.close()
        try raw.uint((u64)w, 4)
        try raw.uint((u64)h, 4)
        try raw.uint(8 if deep else 4, 4)
        if mode == "at8" or mode == "at16":
            var file = try files.File.open((c.str)arguments[2])
            defer file.destroy()
            var collected = Collected(out = &raw, next_row = 0, failed = false)
            try png.decode_rows_at(read_file, (void*)&file, (u64)data.length, collect, (void*)&collected, deep = deep, options = options)
            if collected.failed or collected.next_row != h:
                print("rows out of order or missing")
                return 1
        elif mode == "rows8" or mode == "rows16":
            var collected = Collected(out = &raw, next_row = 0, failed = false)
            try png.decode_rows(data, collect, (void*)&collected, deep = deep, options = options)
            if collected.failed or collected.next_row != h:
                print("rows out of order or missing")
                return 1
        elif deep:
            let out = try new u16[w * h * 4] --- in memory.heap
            try png.decode_rgba16(data, out, options)
            try raw.put(u8[]((u8*)out.data, out.length * 2))
        else:
            let out = try new u8[w * h * 4] --- in memory.heap
            try png.decode_rgba8(data, out, options)
            try raw.put(out)
        try files.write((c.str)arguments[3], raw.view())
        return 0
    # Encode: the input as 16-bit RGBA, then in the asked format.
    let level = (i32)number(arguments, 4, 6)
    let filter = (i32)(strings.parse_i64(arguments[5]) else -1)
    let kind = arguments[6]
    let stream = arguments[7] == "stream"
    let threads = (usize)number(arguments, 8, 0)
    let band_rows = (usize)number(arguments, 9, 0)
    let rgba = try new u16[w * h * 4] --- in memory.heap
    try png.decode_rgba16(data, rgba)
    let names: str[8] = ["gray8", "ga8", "rgb8", "rgba8", "gray16", "ga16", "rgb16", "rgba16"]
    let formats: png.Format[8] = [.gray8, .gray_alpha8, .rgb8, .rgba8, .gray16, .gray_alpha16, .rgb16, .rgba16]
    var index: usize = 3
    for i in 0..<(usize)8:
        if names[i] == kind:
            index = i
    let channels: usize = index % 4 + 1
    let deep = index >= 4
    let picks: usize[16] = [0, 0, 0, 0, 0, 3, 0, 0, 0, 1, 2, 0, 0, 1, 2, 3]
    let bytes = try new u8[w * h * channels] --- in memory.heap
    let words = try new u16[w * h * channels] --- in memory.heap
    for p in 0..<w * h:
        for k in 0..<channels:
            let v = rgba[p * 4 + picks[(channels - 1) * 4 + k]]
            words[p * channels + k] = v
            bytes[p * channels + k] = (u8)(v >> 8)
    var out = Buffer()
    defer out.close()
    let options = png.EncodeOptions(level = level, filter = filter, threads = threads, band_rows = band_rows)
    var encoder = try png.Encoder.begin(w, h, formats[index], &out, options)
    defer encoder.close()
    let step: usize = 7 if stream else h
    var y: usize = 0
    while y < h:
        let rows = step if y + step <= h else h - y
        let row = w * channels
        if deep:
            try encoder.write_rows16(words[y * row..<(y + rows) * row], rows)
        else:
            try encoder.write_rows(bytes[y * row..<(y + rows) * row], rows)
        y += rows
    try encoder.finish()
    try files.write((c.str)arguments[3], out.view())
    return 0