dymokomi/luce-raw
Source of release 0.8.1
0.8.1 / src / raw / pentax.lucb
#==============================================================================================
#
# raw/pentax - Pentax and Ricoh PEF
#
# DESCRIPTION:
# A PEF is a TIFF with the raw in IFD0: uncompressed, or Huffman-coded JPEG-style
# differences from the pixel two columns left (compression 65535), with the
# code table in the maker note (tag 0x220). The maker note ("AOC\0" counting
# offsets from the file, or "PENTAX \0" counting from itself) holds the black levels (0x200)
# and white balance (0x201) in R, G, G, B order.
#
#==============================================================================================
## Pentax sensors with masked borders: model prefix, raw width, and the
## image rectangle (a zero height runs to the bottom), after LibRaw.
struct PentaxCrop:
var model: str
var raw_width: u16
var left: u16
var top: u16
var width: u16
var height: u16
let pentax_crops: PentaxCrop[12] = [
PentaxCrop(model = "K-3 Mark III", raw_width = 6304, left = 26, top = 34, width = 6224, height = 4160),
PentaxCrop(model = "K-3", raw_width = 6080, left = 4, top = 0, width = 6040, height = 0),
PentaxCrop(model = "K-70", raw_width = 6080, left = 60, top = 32, width = 6020, height = 4016),
PentaxCrop(model = "KF", raw_width = 6080, left = 56, top = 28, width = 6024, height = 4024),
PentaxCrop(model = "KP", raw_width = 6112, left = 54, top = 28, width = 6028, height = 0),
PentaxCrop(model = "K-1", raw_width = 7392, left = 6, top = 18, width = 7376, height = 0),
PentaxCrop(model = "K-5", raw_width = 4992, left = 10, top = 0, width = 4950, height = 0),
PentaxCrop(model = "K-r", raw_width = 4352, left = 0, top = 0, width = 4309, height = 0),
PentaxCrop(model = "K-x", raw_width = 4352, left = 0, top = 0, width = 4309, height = 0),
PentaxCrop(model = "K-7", raw_width = 4736, left = 0, top = 2, width = 4684, height = 3122),
PentaxCrop(model = "K20D", raw_width = 4864, left = 0, top = 0, width = 4688, height = 3124),
PentaxCrop(model = "K10D", raw_width = 3936, left = 0, top = 0, width = 3896, height = 2616)]
## Fill `meta` from a Pentax PEF.
func parse_pef(file: const u8[], meta: Meta*, tree: const Tree*) -> !:
meta.format = "PEF"
let model = table_model(meta.make, meta.model)
let main = try tree.at(0)
meta.orientation = try main.uint(274, 1)
meta.raw_width = try dimension(try main.uint(256))
meta.raw_height = try dimension(try main.uint(257))
meta.bits = try depth(try main.uint(258, 12))
meta.little = main.little
let compression = try main.uint(259, 1)
let offsets = try main.find(273) else error(corrupt, "PEF raw has no strips")
let counts = try main.find(279) else error(corrupt, "PEF strips lack byte counts")
meta.data_offset = (usize)(try offsets.uint(0))
meta.data_size = (usize)(try counts.uint(0))
if compression == 65535:
meta.coding = .pentax
elif compression == 1:
meta.offsets = offsets
meta.counts = counts
meta.tile_width = meta.raw_width
meta.tile_height = (usize)(try main.uint(278, (u64)meta.raw_height))
meta.coding = .unpacked if meta.bits == 16 else .packed
else:
error(unsupported, "this PEF compression is not supported")
for crop in pentax_crops:
if starts(model, crop.model) and meta.raw_width == (usize)crop.raw_width and meta.width == 0:
meta.left = (usize)crop.left
meta.top = (usize)crop.top
meta.width = (usize)crop.width
meta.height = (usize)crop.height if crop.height > 0 else meta.raw_height - (usize)crop.top
let exif = tree.exif else error(corrupt, "PEF has no EXIF directory")
meta.cfa = [0, 1, 1, 2]
try exif_cfa(&exif, meta)
let note = try exif.find(37500) else error(corrupt, "PEF has no maker note")
# "AOC\0" and a byte order, offsets from the file; or, on newer bodies,
# "PENTAX \0" and a byte order, offsets from the maker note.
var little = false
var maker = Directory(file = file, base = 0, offset = 0, count = 0)
if note.data.length > 10 and (str)note.data[..<4] == "AOC\0":
little = note.data[4] == 'I'
maker = try Directory.open(file, 0, note.position + 6, little)
elif note.data.length > 12 and (str)note.data[..<8] == "PENTAX \0":
little = note.data[8] == 'I'
maker = try Directory.open(file, (i64)note.position, note.position + 10, little)
else:
error(unsupported, "PEF maker note has an unknown layout")
if let black = try maker.find(0x200):
if black.count == 4:
try canonical_black(meta, &black)
if let balance = try maker.find(0x201):
if balance.count == 4:
let r = try balance.real(0)
let g = (try balance.real(1) + try balance.real(2)) * 0.5
let b = try balance.real(3)
if r > 0.0 and g > 0.0 and b > 0.0:
meta.neutral = [g / r, 1.0, g / b]
meta.neutral_known = true
if meta.coding == .pentax:
let table = try maker.find(0x220) else error(corrupt, "compressed PEF lacks its code table")
meta.extra = table.data
meta.flags = 1 if little else 0
## dcraw's pentax_load_raw: JPEG-style differences with a code table from the
## maker note, predicted from two columns left (two rows up in the first two).
func unpack_pentax(file: const u8[], meta: const Meta*, mosaic: Mosaic*) -> !:
var header = Reader(data = meta.extra, little = (meta.flags & 1) != 0)
let used = (usize)((try header.uint(2) + 12) & 15)
_ = try header.take(12)
var codes: u16[16]
var lengths: u8[16]
for c in 0..<used:
codes[c] = (u16)(try header.uint(2))
for c in 0..<used:
lengths[c] = try header.byte()
let entries = try new u16[4096] in memory.heap
defer free(entries) in memory.heap
for c in 0..<used:
let length = (usize)lengths[c]
if length == 0 or length > 12:
error(corrupt, "invalid PEF code table")
let first = (usize)codes[c]
let span = (usize)4096 >> length
if first + span > 4096:
error(corrupt, "invalid PEF code table")
for k in 0..<span:
entries[first + k] = (u16)((length << 8) | c)
var table = try Huffman.custom(12, entries)
defer table.close()
var reader = Bits(data = try binary.slice(file, (u64)meta.data_offset, (u64)meta.data_size))
var vertical: u16[4]
let w = mosaic.width
for y in 0..<mosaic.height:
var horizontal: u16[2]
for x in 0..<w:
let delta = (u16)(try difference(&reader, &table))
if x < 2:
let v = (y & 1) * 2 + x
vertical[v] = vertical[v] +% delta
horizontal[x] = vertical[v]
else:
horizontal[x & 1] = horizontal[x & 1] +% delta
mosaic.data[y * w + x] = horizontal[x & 1]