dymokomi/luce-raw
Source of release 0.8.2
## The test driver tests/oracle/gate.py builds: it decodes a raw with this package and
## writes what it found for the Python checks to compare with LibRaw.
## driver info FILE
## driver recognize FILE whether it is developable, and a raw at all
## driver sensor FILE OUT u16 samples, then a text header OUT.txt
## driver develop FILE OUT QUALITY CROP EXPOSURE [SPACE [TEMPERATURE TINT]]
## f32 RGB rows, then OUT.txt with the size
## driver bench FILE REPEAT milliseconds per decode
import c
from luce_std import files
import memory
import strings
import time
import raw
from luce_raster.model import Raster
from luce_raster.binary import Buffer
func number(text: str) -> f64:
var value = 0.0
var scale = 0.0
var negative = false
for ch in text:
if ch == '-':
negative = true
elif ch == '.':
scale = 1.0
elif ch >= '0' and ch <= '9':
value = value * 10.0 + (f64)((u32)ch - 48)
if scale > 0.0:
scale *= 10.0
let result = value / scale if scale > 0.0 else value
return -result if negative else result
func text_file(path: str, body: fmt) -> !:
var buffer: u8[4096]
let text = try strings.format(buffer, body)
var name: u8[1024]
let where = try strings.format(name, f"{path}.txt")
try files.write((c.str)where, text.bytes)
pub func main(arguments: str[]) -> i32!:
let command = arguments[1]
let file = try files.read((c.str)arguments[2])
defer file.release()
let data = file.value
if command == "preview":
var best: u64 = 0
var found: raw.Preview? = none
for i in 0..<50:
let start = time.now()
found = try raw.preview(data)
let spent = time.now() - start
if best == 0 or spent < best:
best = spent
if let shown = found:
print(f"preview {shown.width}x{shown.height} orientation {shown.orientation} bytes {shown.jpeg.length} offset {(usize)shown.jpeg.data - (usize)data.data} in {best // 1000} us\n")
if arguments.length > 3:
try files.write((c.str)arguments[3], shown.jpeg)
else:
print(f"preview none in {best // 1000} us\n")
return 0
if command == "recognize":
print(f"detect {raw.detect(data)} recognize {raw.recognize(data)}\n")
return 0
if command == "info":
let about = try raw.info(data)
let wb = about.as_shot
print(f"{about.format} {about.make} | {about.model} raw {about.raw_width}x{about.raw_height} out {about.width}x{about.height} bits {about.bits} matrix {about.matrix} exposure {about.baseline_exposure} orientation {about.orientation} temperature {wb.temperature} tint {wb.tint} multipliers {wb.multipliers[0]} {wb.multipliers[1]} {wb.multipliers[2]}\n")
return 0
if command == "sensor":
var found = try raw.sensor(data)
defer found.close()
let bytes = u8[]((u8*)found.samples.data, found.samples.length * 2)
try files.write((c.str)arguments[3], bytes)
var pattern: u8[36]
for i: usize in 0..<36:
pattern[i] = found.pattern[i] + '0'
try text_file(arguments[3], f"{found.width} {found.height} {found.channels} {found.top} {found.left} {found.active_width} {found.active_height} {found.cfa[0]} {found.cfa[1]} {found.cfa[2]} {found.cfa[3]} {found.black[0]} {found.black[1]} {found.black[2]} {found.black[3]} {found.white} {(str)pattern[0..]}\n")
return 0
var options = raw.Options()
if command == "develop" or command == "bench" or command == "tiles":
if arguments.length > 4:
options.quality = raw.Quality.fast if arguments[4] == "fast" else raw.Quality.best
options.half = arguments[4] == "half"
if arguments.length > 5:
options.crop = arguments[5] == "1"
if arguments.length > 6:
options.exposure = arguments[6] == "1"
if arguments.length > 7:
if command == "develop":
options.camera = arguments[7] == "camera"
else:
options.threads = (usize)number(arguments[7])
if arguments.length > 9:
options.temperature = number(arguments[8])
options.tint = number(arguments[9])
if command == "develop":
var image = Raster(max_bytes = 8589934592, max_pixels = 1073741824)
defer image.close()
with memory.heap:
image.encoded = try new u8[data.length] ---
memory.copy(image.encoded, data, data.length)
try raw.probe(&image, options)
try raw.decode(&image, options)
let floats = try new f32[image.pixels.length]
defer free(floats)
for i in 0..<floats.length:
floats[i] = (f32)image.pixels[i]
try files.write((c.str)arguments[3], u8[]((u8*)floats.data, floats.length * 4))
try text_file(arguments[3], f"{image.width} {image.height}\n")
return 0
if command == "tiles":
let repeat = (usize)number(arguments[3])
var best: u64 = 0
var stages = raw.Timings()
for i in 0..<repeat:
var timings = raw.Timings()
options.timings = &timings
options.alpha = true
var counter = Counter()
let start = time.now()
try raw.develop(data, options, (void*)&counter, count_tile)
let spent = time.now() - start
if best == 0 or spent < best:
best = spent
stages = timings
let ms: u64 = 1000000
print(f"{best // ms} ms (parse {stages.parse // ms}, unpack {stages.unpack // ms}, levels {stages.levels // ms}, develop {stages.develop // ms} [cpu: demosaic {stages.demosaic // ms}, output {stages.output // ms}])\n")
return 0
if command == "bench":
let repeat = (usize)number(arguments[3])
var best: u64 = 0
var stages = raw.Timings()
for i in 0..<repeat:
var image = Raster(max_bytes = 8589934592, max_pixels = 1073741824)
defer image.close()
with memory.heap:
image.encoded = try new u8[data.length] ---
memory.copy(image.encoded, data, data.length)
var timings = raw.Timings()
options.timings = &timings
let start = time.now()
try raw.probe(&image, options)
try raw.decode(&image, options)
let spent = time.now() - start
if best == 0 or spent < best:
best = spent
stages = timings
let ms: u64 = 1000000
print(f"{best // ms} ms (parse {stages.parse // ms}, unpack {stages.unpack // ms}, levels {stages.levels // ms}, develop {stages.develop // ms} [cpu: demosaic {stages.demosaic // ms}, output {stages.output // ms}])\n")
return 0
print("unknown command\n")
return 2
## A sink that counts the pixels it is handed.
struct Counter:
var pixels: @usize
func count_tile(context: void*?, tile: const raw.Tile*) -> !:
let counter = (Counter*)(context else return)
_ = counter.pixels.add(tile.width * tile.height)