Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-develop

Source of release 0.1.0

0.1.0 / src / export.lucb

## Export: a photo developed at full size (or shrunk to a long edge) on the CPU
## (its film on the GPU where there is one, the grain rendered grain by grain to
## `grain_samples` a pixel) and written as a file: JPEG (8-bit, a quality), or PNG (16-bit, lossless),
## for sRGB or Display P3 (the view renders for that display's primaries), with
## the matching ICC profile embedded so readers know which. One call does it all on the calling thread; an application
## runs it on a thread of its own.
import c
import memory
import strings
from luce_std import files
from luce_png import png
from luce_color import icc
from luce_std.collections import Buffer as Bytes
from luce_raster.binary import Buffer
from settings import parse
import process
import view
import geometry
import codes

pub let invalid: ErrorCode = codes.invalid

## The stochastic render's samples a pixel for an export: as far as the
## viewer refines.
pub let grain_samples: usize = 256

## The file formats.
pub let jpeg: i64 = 0
pub let png16: i64 = 1

## The color spaces: what the pixels mean, and the profile they carry.
pub let srgb: i64 = 0
pub let display_p3: i64 = 1

## Develop the photo at `source` with the settings `settings` (their text form)
## and write it to `target` as `format` (jpeg or png16), its longer side `edge`
## pixels at most (0: full size); `quality` 1–100 for JPEG; `space` srgb or
## display_p3.
pub func export(source: str, settings: str, target: str, format: i64, quality: i64 = 92, edge: i64 = 0, space: i64 = 0) -> !:
    if format != jpeg and format != png16:
        error(invalid, "export writes JPEG or 16-bit PNG")
    if space != srgb and space != display_p3:
        error(invalid, "export writes sRGB or Display P3")
    let output = view.Output.display_p3 if space == display_p3 else view.Output.srgb
    var profile: Bytes
    defer profile.destroy()
    try icc.write_named_profile(&profile, "Display P3" if space == display_p3 else "sRGB")
    if edge < 0 or quality < 1 or quality > 100:
        error(invalid, "an export needs a size of 0 or more and a quality of 1 to 100")
    let wanted = parse(settings)
    var opened = try process.opened(source, &wanted, (usize)edge)
    defer opened.close()
    if format == jpeg:
        var picture = try process.render(&opened, wanted, output, samples = grain_samples)
        defer picture.close()
        try picture.save_jpeg(target, quality, profile.view())
        return
    let words = try process.render16(&opened, wanted, output, samples = grain_samples)
    # The crop decides the output's size, as render16 rendered it.
    let (out_width, out_height) = geometry.output_size(&wanted, (f64)opened.width(), (f64)opened.height())
    defer free(words) in memory.heap
    var encoded = Buffer()
    defer encoded.close()
    try png.encode_rgba16(words, (usize)out_width, (usize)out_height, &encoded, png.EncodeOptions(icc_profile = profile.view()))
    let terminated = try strings.copy(target)
    defer strings.release(terminated)
    try files.write((c.str)terminated, encoded.data[..<encoded.used])