Skip to content
Snippets Groups Projects
Commit 972744f7 authored by Sascha Lambert's avatar Sascha Lambert
Browse files

Merge branch 'nc_patch' into 'main'

Changed Offset calculation, Y-X ordering of pixels

See merge request !1
parents 897a08fe ae20c81f
Branches main
No related tags found
1 merge request!1Changed Offset calculation, Y-X ordering of pixels
......@@ -17,6 +17,27 @@ import re
These functions parse the input files.
'''
def find_binary_data_offset(filename):
'''
Find the offset given the position of the "data" keyword in the file.
divinding the file from the number of pixels and the data type and starting from the end,
I found can lead to data from a buffer ending up in the next buffer.
As i understand it, the data keyword marks the end of the ascii header and the beginning of the binary data.
Thats what this function is for. It returns the offset in bytes where the binary data starts.
'''
with open(filename, 'rb') as file:
while True:
line = file.readline()
if b"data" in line:
break
offset = file.tell()
return offset
def _load_mi_image(filename, allow_16_bit=False):
''' filename = .mi file to load.
......@@ -58,6 +79,8 @@ def _load_mi_image(filename, allow_16_bit=False):
with open(filename, encoding='latin-1') as file: # latin-1 seems to work, fingers crossed
line_counter = 0 #for knowing where the binary blob starts
#We know the binary blob starts after the line where the data is defined
for line in file:
''' First 14 characters seem to be the name of the property. Value is the rest of the line '''
key = line[:14].strip()
......@@ -116,9 +139,9 @@ def _load_mi_image(filename, allow_16_bit=False):
bytes_per_pixel = 4 if meta['data'] == 'BINARY_32' else 2 # int32 ~ _4_*8bit
pixels = meta['xPixels'] * meta['yPixels']
field_size_bytes = pixels * bytes_per_pixel # Size of a single buffer
offset = filesize - field_size_bytes * meta['buffer_count'] + b * field_size_bytes # Starting location of the buffer b
offset = find_binary_data_offset(filename) + b * field_size_bytes # Starting location of the buffer b
data = np.fromfile(filename, dtype=np.int32, offset=offset, count=pixels) # count is num of ints to load, not bytes.
data = data.reshape((meta['xPixels'], meta['yPixels']))
data = data.reshape((meta['yPixels'], meta['xPixels']))
''' Convert the range and units of the buffer data '''
range_ = meta['buffers'][b]['bufferRange']
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment