#!/usr/bin/env python3 # # Copyright (C) 2020 Herbert Poetzl # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # import sys import numpy as np import itertools as it RAW_WIDTH = 4096 RAW_HEIGHT = 3072 NUM_REGS = 128 def raw12_reader(path): with open(path, 'rb') as f: for row in range(RAW_HEIGHT): for col in range(RAW_WIDTH >> 1): val = f.read(3) yield (val[0] << 4) | (val[1] >> 4) yield ((val[1] & 0xF) << 8) | val[2] for reg in range(NUM_REGS): val = f.read(2) yield (val[1] << 8) | val[0] reader = raw12_reader(sys.argv[1]) data = it.islice(reader, RAW_WIDTH * RAW_HEIGHT) array = np.fromiter(data, np.int) raw = np.reshape(array, (RAW_HEIGHT, RAW_WIDTH), 'C') plan_r = raw[::2, ::2] plan_g1 = raw[::2, 1::2] plan_g2 = raw[1::2, ::2] plan_b = raw[1::2, 1::2] flat_r = plan_r.flatten() flat_g1 = plan_g1.flatten() flat_g2 = plan_g2.flatten() flat_b = plan_b.flatten() IMG_WIDTH = RAW_WIDTH >> 1 IMG_HEIGHT = RAW_HEIGHT >> 1 MAX_VALUE = (1 << 12) - 1 print("P3") print(IMG_WIDTH, IMG_HEIGHT) print(MAX_VALUE) for r, g1, g2, b in zip(flat_r, flat_g1, flat_g2, flat_b): g = (g1 + g2) >> 1 print(r, g, b) regs = it.islice(reader, NUM_REGS) for idx, reg in enumerate(regs): print("# {} = {}".format(idx, reg))