Commit 23748f0b authored by Elijah Andrews's avatar Elijah Andrews

Added CIHX support to mraw.py.

parent 5c6a2ae0
......@@ -10,36 +10,87 @@ CINE library: Dustin Kleckner (dkleckner@uchicago.edu)
pyMRAW.py: Jaka Javh (jaka.javh@fs.uni-lj.si)
"""
from os import path
import numpy as np
import warnings
import xmltodict
# Processing the data in chunks keeps it in the L2 catch of the processor, increasing speed for large arrays by ~50%
CHUNK_SIZE = 6 * 10 ** 5 # Should be divisible by 3, 4 and 5! This seems to be near-optimal.
SUPPORTED_FILE_FORMATS = ['mraw', 'tiff']
SUPPORTED_EFFECTIVE_BIT_SIDE = ['lower', 'higher']
def read_cih(filename):
# create a dictionary which will contain all the properties defined in the cih file
cih = dict()
# read the cih (Camera Information Header)
with open(filename, 'r') as f:
# work thorugh the file line by line
for line in f:
if line == '\n': # end of cif header
break
# split the line in property and value pairs (line_sp is a list with two entries)
line_sp = line.replace('\n', '').split(' : ')
# check if line_sp indeed contains a property name and a value
if len(line_sp) == 2:
key, value = line_sp
# try to turn the value into a number (float or integer):
try:
if '.' in value:
cih[key] = float(value)
else:
cih[key] = int(value)
# otherwise the value is a string:
except:
cih[key] = value
name, ext = path.splitext(filename)
if ext == '.cih':
cih = dict()
# read the cif header
with open(filename, 'r') as f:
for line in f:
if line == '\n': # end of cif header
break
line_sp = line.replace('\n', '').split(' : ')
if len(line_sp) == 2:
key, value = line_sp
try:
if '.' in value:
value = float(value)
else:
value = int(value)
cih[key] = value
except:
cih[key] = value
elif ext == '.cihx':
with open(filename, 'r', encoding='utf-8', errors='ignore') as f:
lines = f.readlines()
first_last_line = [i for i in range(len(lines)) if '<cih>' in lines[i] or '</cih>' in lines[i]]
xml = ''.join(lines[first_last_line[0]:first_last_line[-1] + 1])
raw_cih_dict = xmltodict.parse(xml)
cih = {
'Date': raw_cih_dict['cih']['fileInfo']['date'],
'Camera Type': raw_cih_dict['cih']['deviceInfo']['deviceName'],
'Record Rate(fps)': float(raw_cih_dict['cih']['recordInfo']['recordRate']),
'Shutter Speed(s)': float(raw_cih_dict['cih']['recordInfo']['shutterSpeed']),
'Total Frame': int(raw_cih_dict['cih']['frameInfo']['totalFrame']),
'Original Total Frame': int(raw_cih_dict['cih']['frameInfo']['recordedFrame']),
'Image Width': int(raw_cih_dict['cih']['imageDataInfo']['resolution']['width']),
'Image Height': int(raw_cih_dict['cih']['imageDataInfo']['resolution']['height']),
'File Format': raw_cih_dict['cih']['imageFileInfo']['fileFormat'],
'EffectiveBit Depth': int(raw_cih_dict['cih']['imageDataInfo']['effectiveBit']['depth']),
'EffectiveBit Side': raw_cih_dict['cih']['imageDataInfo']['effectiveBit']['side'],
'Color Bit': int(raw_cih_dict['cih']['imageDataInfo']['colorInfo']['bit']),
'Comment Text': raw_cih_dict['cih']['basicInfo'].get('comment', ''),
'Signal Delay Trigger Out Width(nsec)': int(raw_cih_dict['cih']['deviceInfo']['delayInfos']['delayInfo'][3]['value']) / 100 # 4th signal is Trigger Out Width in 100ns units
}
else:
raise Exception('Unsupported configuration file ({:s})!'.format(ext))
# check exceptions
# ff = cih['File Format']
# if ff.lower() not in SUPPORTED_FILE_FORMATS:
# raise Exception('Unexpected File Format: {:g}.'.format(ff))
# bits = cih['Color Bit']
# if bits < 12:
# warnings.warn('Not 12bit ({:g} bits)! clipped values?'.format(bits))
# - may cause overflow')
# 12-bit values are spaced over the 16bit resolution - in case of photron filming at 12bit
# this can be meanded by dividing images with //16
# if cih['EffectiveBit Depth'] != 12:
# warnings.warn('Not 12bit image!')
# ebs = cih['EffectiveBit Side']
# if ebs.lower() not in SUPPORTED_EFFECTIVE_BIT_SIDE:
# raise Exception('Unexpected EffectiveBit Side: {:g}'.format(ebs))
# if (cih['File Format'].lower() == 'mraw') & (cih['Color Bit'] not in [8, 16]):
# raise Exception('pyMRAW only works for 8-bit and 16-bit files!')
# if cih['Original Total Frame'] > cih['Total Frame']:
# warnings.warn('Clipped footage! (Total frame: {}, Original total frame: {})'.format(cih['Total Frame'], cih[
# 'Original Total Frame']))
return cih
......@@ -77,7 +128,12 @@ class mraw(object):
# calculate the size of an image in bytes:
self.imageSize = self.width * self.height * self.bit_depth // 8
self.fileName = fn[:-4] + '.mraw'
if fn[-4:] == '.cih':
self.fileName = fn[:-4] + '.mraw'
elif fn[-5:] == '.cihx':
self.fileName = fn[:-5] + '.mraw'
else:
raise ValueError("Input file is neither CIH or CIHX.")
self.f = open(self.fileName, 'rb')
def __len__(self):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment