dymokomi/luce-jpeg
Source of release 0.10.1
## Decodes one JPEG to 8-bit pixels at 1/scale through `decode_rgb8`, `decode_rgba8`
## or `decode_rows` (`at`: `decode_rows_at`, the file read in pieces):
## `scaled <in.jpg> <out.raw> <scale> [rgb|rgba|rows|at|coarse] [threads]`,
## `coarse` writing a progressive picture's first sight (1/8, RGBA) instead,
## writing a 12-byte header (width, height, bytes a pixel as little-endian u32)
## then the pixel bytes.
from luce_std import files
import c
import memory
import strings
import jpeg
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)
pub func main(arguments: str[]) -> i32!:
let encoded_file = try files.read((c.str)arguments[1])
defer encoded_file.release()
let encoded = encoded_file.value
let scale = (usize)(strings.parse_u64(arguments[3]) else 1)
let mode = arguments[4] if arguments.length > 4 else "rgb"
let threads = (usize)(strings.parse_u64(arguments[5]) else 0) if arguments.length > 5 else 0
let info = try jpeg.info(encoded)
let width = info.scaled_width(scale)
let height = info.scaled_height(scale)
let pixel: usize = 4 if mode == "rgba" else 3
var raw = Buffer()
defer raw.close()
try raw.uint((u64)width, 4)
try raw.uint((u64)height, 4)
try raw.uint((u64)pixel, 4)
let options = jpeg.Options(scale = scale, threads = threads)
if mode == "coarse":
var picture = Buffer()
defer picture.close()
var coarse = Coarse(out = &picture, calls = 0)
var file = try files.File.open((c.str)arguments[1])
defer file.destroy()
try jpeg.decode_rows_at(read_file, (void*)&file, (u64)encoded.length, ignore_rows, none, options = jpeg.Options(threads = threads, coarse = take_coarse, coarse_context = (void*)&coarse))
if coarse.calls != (1 if info.progressive else 0):
print("a coarse picture came for a baseline picture, or not once for a progressive one")
return 1
try files.write((c.str)arguments[2], picture.view())
return 0
if mode == "at":
var file = try files.File.open((c.str)arguments[1])
defer file.destroy()
var collected = Collected(out = &raw, next_row = 0, failed = false)
try jpeg.decode_rows_at(read_file, (void*)&file, (u64)encoded.length, collect, (void*)&collected, layout = .rgb, options = options)
if collected.failed or collected.next_row != height:
print("rows out of order or missing")
return 1
elif mode == "rows":
var collected = Collected(out = &raw, next_row = 0, failed = false)
try jpeg.decode_rows(encoded, collect, (void*)&collected, layout = .rgb, options = options)
if collected.failed or collected.next_row != height:
print("rows out of order or missing")
return 1
else:
let out = try new u8[width * height * pixel] --- in memory.heap
defer free(out) in memory.heap
if pixel == 4:
try jpeg.decode_rgba8(encoded, out, options)
else:
try jpeg.decode_rgb8(encoded, out, options)
try raw.put(out)
try files.write((c.str)arguments[2], raw.view())
return 0