Browse Source

made a test model just to figure out how to refactor things

zolfa-add_slices_loading
jim 10 years ago
parent
commit
b4878e28dc
2 changed files with 68 additions and 2 deletions
  1. +1
    -1
      nd2reader/__init__.py
  2. +67
    -1
      nd2reader/model/__init__.py

+ 1
- 1
nd2reader/__init__.py View File

@ -23,7 +23,7 @@ class Nd2(BaseNd2):
"""
for i in range(self._image_count):
for fov in range(self._field_of_view_count):
for fov in range(self.field_of_view_count):
for z_level in range(self._z_level_count):
for channel in self.channels:
image = self.get_image(i, fov, channel.name, z_level)


+ 67
- 1
nd2reader/model/__init__.py View File

@ -73,4 +73,70 @@ class Image(object):
def show(self):
skimage.io.imshow(self.data)
skimage.io.show()
skimage.io.show()
from io import BytesIO
import array
import struct
class Chunkmap(object):
def __init__(self):
pass
def read(self, filename):
with open(filename, "rb") as f:
data = f.read(-1)
self.parse(data, 1)
def parse(self, data, count):
data = BytesIO(data)
res = {}
total_count = 0
for c in range(count):
lastpos = data.tell()
total_count += 1
hdr = data.read(2)
if not hdr:
break
typ = ord(hdr[0])
bname = data.read(2*ord(hdr[1]))
name = bname.decode("utf16")[:-1].encode("utf8")
if typ == 1:
value, = struct.unpack("B", data.read(1))
elif typ in [2, 3]:
value, = struct.unpack("I", data.read(4))
elif typ == 5:
value, = struct.unpack("Q", data.read(8))
elif typ == 6:
value, = struct.unpack("d", data.read(8))
elif typ == 8:
value = data.read(2)
while value[-2:] != "\x00\x00":
value += data.read(2)
value = value.decode("utf16")[:-1].encode("utf8")
elif typ == 9:
cnt, = struct.unpack("Q", data.read(8))
value = array.array("B", data.read(cnt))
elif typ == 11:
curpos = data.tell()
newcount, length = struct.unpack("<IQ", data.read(12))
curpos = data.tell()
length -= data.tell()-lastpos
nextdata = data.read(length)
value = self.parse(nextdata, newcount)
print("WE GOT A NEW DICT")
# Skip some offsets
data.read(newcount * 8)
else:
assert 0, "%s hdr %x:%x unknown" % (name, ord(hdr[0]), ord(hdr[1]))
if not name in res:
res[name] = value
else:
if not isinstance(res[name], list):
res[name] = [res[name]]
res[name].append(value)
x = data.read()
assert not x, "skip %d %s" % (len(x), repr(x[:30]))
return res

Loading…
Cancel
Save