diff options
author | jaseg <git@jaseg.de> | 2023-12-18 14:22:45 +0100 |
---|---|---|
committer | jaseg <git@jaseg.de> | 2023-12-18 14:22:45 +0100 |
commit | 7840074004536b44396083fcab75a9e6355e4a42 (patch) | |
tree | 3bdfd3e907e5548de679722e73a1288b8f070acd /src | |
download | infiray_irg_py-1.0.tar.gz infiray_irg_py-1.0.tar.bz2 infiray_irg_py-1.0.zip |
Initial commitv1.0
Diffstat (limited to 'src')
-rw-r--r-- | src/infiray_irg.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/infiray_irg.py b/src/infiray_irg.py new file mode 100644 index 0000000..9d573f6 --- /dev/null +++ b/src/infiray_irg.py @@ -0,0 +1,36 @@ +from PIL import Image +import numpy as np +import struct +import io + +def load(data): + def consume(n): + nonlocal data + out, data = data[:n], data[n:] + if len(out) < n: + raise ValueError('file is truncated') + return out + + header = consume(128) + if header[:2] != bytes([0xca, 0xac]) or header[-2:] != bytes([0xac, 0xca]): + raise ValueError('Header magic not found.') + + _unk0, coarse_section_length, y_res, x_res,\ + _zero0, _unk1, _zero1, fine_offset, _unk2, jpeg_length,\ + y_res_2, x_res_2, _unk3, = struct.unpack('<HIHHHHHHHIHHI', header[2:34]) + + _zero_celsius0, _zero_celsius1, *rest, high_gain_mode_flag = struct.unpack('<11I', header[34:78]) + + if (x_res, y_res) != (x_res_2, y_res_2): + raise ValueError(f'Resolution mismatch in header: {x_res}*{y_res} != {x_res_2}*{y_res_2}') + + if x_res*y_res != coarse_section_length: + raise ValueError('Resolution mismatch in header') + + coarse_img = np.frombuffer(consume(coarse_section_length), dtype=np.uint8).reshape((y_res, x_res)) + fine_img = np.frombuffer(consume(x_res*y_res*2), dtype=np.int16).reshape((y_res, x_res)) + fine_img = (fine_img / 16) - 273 + vis_jpg = Image.open(io.BytesIO(consume(jpeg_length))) + + return coarse_img, fine_img, vis_jpg + |