Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-raw

Source of release 0.8.0

0.8.0 / src / raw / fujifilm.lucb

#==============================================================================================
#
#   raw/fujifilm - Fujifilm RAF, Bayer and X-Trans
#
#   DESCRIPTION:
#       A RAF starts "FUJIFILM" and a big-endian header of offsets: an embedded JPEG
#       whose EXIF names the camera and orientation, a directory of Fujifilm records
#       (sizes, crop, layout, and 0x131: the 6×6 X-Trans filter pattern), and the
#       raw block: a small TIFF whose 0xf000 directory gives the sensor size, bits,
#       black levels, as-shot white balance and the samples. Uncompressed samples
#       are 12 bits two per three bytes, or 16-bit words, little-endian; compressed
#       ones (header 0x4953) decode in fuji_compressed.lucb.
#
#==============================================================================================

## Fill `meta` from a Fujifilm RAF.
func parse_raf(file: const u8[], meta: Meta*) -> !:
    meta.format = "RAF"
    meta.make = "FUJIFILM"
    var header = Reader(data = file, position = 84, little = false)
    let jpeg = (usize)(try header.uint(4))
    _ = try header.uint(4)
    let records = (usize)(try header.uint(4))
    _ = try header.uint(4)
    let block = (usize)(try header.uint(4))
    var name = Reader(data = file, position = 28)
    meta.model = try name.cstring(32)
    # The embedded JPEG's EXIF: orientation, and the model's full name.
    if jpeg > 0 and jpeg + 12 < file.length:
        let exif = Tree.walk(file, jpeg + 12, jpeg + 12) catch failure:
            recover Tree()
        if exif.count > 0:
            let first = try exif.at(0)
            meta.orientation = try first.uint(274, 1)
    # Fujifilm's records: crop, and the layout flags of rotated sensors.
    var list = Reader(data = file, position = records, little = false)
    let entries = (usize)(try list.uint(4))
    if entries > 255:
        error(corrupt, "RAF record directory is too long")
    for i in 0..<entries:
        let tag = try list.uint(2)
        let length = (usize)(try list.uint(2))
        var value = Reader(data = try list.take(length), little = false)
        if tag == 0x131 and length == 36:
            # dcraw's xtrans_abs: the pattern's 36 entries, stored last first.
            let codes = try value.take(36)
            for k: usize in 0..<36:
                meta.pattern[35 - k] = codes[k] & 3
            meta.xtrans = true
        if tag == 0x130 and length >= 2:
            _ = try value.byte()
            if (try value.byte() & 8) == 0:
                error(unsupported, "rotated (SuperCCD) RAF files are not supported")
        if tag == 0x110 and length >= 4:
            meta.crop_y = (usize)(try value.uint(2))
            meta.crop_x = (usize)(try value.uint(2))
        if tag == 0x111 and length >= 4:
            meta.crop_height = (usize)(try value.uint(2))
            meta.crop_width = (usize)(try value.uint(2))
    # The raw block's TIFF and its 0xf000 directory.
    let tree = try Tree.walk(file, block, block)
    let root = try tree.at(0)
    let pointer = try root.find(0xf000) else error(unsupported, "RAF raw block has no 0xf000 directory")
    let raw = try root.child(try pointer.uint(0))
    meta.raw_width = try dimension(try raw.uint(0xf001))
    meta.raw_height = try dimension(try raw.uint(0xf002))
    meta.bits = try depth(try raw.uint(0xf003, 12))
    meta.data_offset = (usize)(try raw.locate(try raw.uint(0xf007)))
    meta.data_size = (usize)(try raw.uint(0xf008))
    let pixels = meta.raw_width * meta.raw_height
    let data = try binary.slice(file, (u64)meta.data_offset, (u64)meta.data_size)
    if data.length >= 16 and data[0] == 0x49 and data[1] == 0x53:
        # The compressed header's raw size replaces the directory's.
        var params = try fuji_params(data)
        meta.raw_width = try dimension(((u64)data[9] << 8) | (u64)data[10])
        meta.raw_height = params.lines * 6
        meta.bits = (usize)params.bits
        params.close()
        meta.coding = .fuji
    elif meta.bits == 12 and meta.data_size * 8 == pixels * 12:
        meta.coding = .packed_little
    elif meta.data_size == pixels * 2:
        meta.coding = .words
    else:
        error(unsupported, "this RAF coding is not supported")
    meta.cfa = [0, 1, 1, 2]
    # LibRaw's white for Bayer bodies outside the X series (the GFX).
    if not meta.xtrans and meta.coding != .packed_little and not starts(meta.model, "X-"):
        meta.white = [15872.0, 15872.0, 15872.0]
    if let black = try raw.find(0xf00a):
        let side: usize = 6 if black.count == 36 else 2 if black.count == 4 else 1
        meta.black_rows = side
        meta.black_columns = side
        for p in 0..<side * side:
            meta.black[p] = try black.real(p)
    if let balance = try raw.find(0xf00e):
        if balance.count == 3:
            let g = try balance.real(0)
            let r = try balance.real(1)
            let b = try balance.real(2)
            if r > 0.0 and g > 0.0 and b > 0.0:
                meta.neutral = [g / r, 1.0, g / b]
                meta.neutral_known = true

## Sixteen-bit little-endian words, row after row.
func unpack_words(file: const u8[], meta: const Meta*, mosaic: Mosaic*) -> !:
    let data = try binary.slice(file, (u64)meta.data_offset, (u64)(mosaic.data.length * 2))
    for i in 0..<mosaic.data.length:
        mosaic.data[i] = (u16)data[i * 2] | ((u16)data[i * 2 + 1] << 8)

## Twelve-bit samples two per three bytes: the little-endian 24-bit word's low
## half is the first pixel, its high half the second.
func unpack_packed_little(file: const u8[], meta: const Meta*, mosaic: Mosaic*) -> !:
    let data = try binary.slice(file, (u64)meta.data_offset, (u64)meta.data_size)
    let w = mosaic.width
    let row_bytes = w * 3 // 2
    for y in 0..<mosaic.height:
        let row = data[y * row_bytes..<(y + 1) * row_bytes]
        for k in 0..<w // 2:
            let word = (u32)row[k * 3] | ((u32)row[k * 3 + 1] << 8) | ((u32)row[k * 3 + 2] << 16)
            mosaic.data[y * w + k * 2] = (u16)(word & 0xfff)
            mosaic.data[y * w + k * 2 + 1] = (u16)(word >> 12)