def __init__(self, fh): (shema_type, reserved, self.file_position, file_part, # reserved content_guid, content_file_type, name ) = struct.unpack('<2s10sqi16s8s80s', fh.read(128)) if shema_type != b'A1': raise ValueError("not a AttachmentEntryA1") self.content_guid = uuid.UUID(bytes=content_guid) self.content_file_type = stripnull(content_file_type) self.name = unicode(stripnull(name), 'utf-8') self._fh = fh
def __init__(self, fh): ( shema_type, reserved, self.file_position, file_part, # reserved content_guid, content_file_type, name) = struct.unpack('<2s10sqi16s8s80s', fh.read(128)) if shema_type != b'A1': raise ValueError("not a AttachmentEntryA1") self.content_guid = uuid.UUID(bytes=content_guid) self.content_file_type = stripnull(content_file_type) self.name = unicode(stripnull(name), 'utf-8') self._fh = fh
def __init__(self, fh): (size, self.time, self.event_type, description_size, ) = struct.unpack('<idii', fh.read(20)) description = stripnull(fh.read(description_size)) self.description = unicode(description, 'utf-8')
def __init__(self, fh): (self.dimension, self.start, self.size, self.start_coordinate, stored_size ) = struct.unpack('<4siifi', fh.read(20)) self.dimension = stripnull(self.dimension) self.stored_size = stored_size if stored_size else self.size
def __init__(self, fh): ( size, self.time, self.event_type, description_size, ) = struct.unpack('<idii', fh.read(20)) description = stripnull(fh.read(description_size)) self.description = unicode(description, 'utf-8')
def _tiff_header_data_2017b_v1(fp): """Extract ScanImage header data from a tiff for ScanImage 2017b 1. The header 2017b v0 and 2017b v1 have different header formats. It seems that tifffile doesn't recognize 2017b1 as a scanimage tiff, and the frame header data is stored in a different format. http://scanimage.vidriotechnologies.com/display/SI2016/ScanImage+BigTiff+Specification """ fp.seek(0) byteorder, tiff_version = struct.unpack('<2sH', fp.read(4)) if byteorder != BYTEORDER or tiff_version != BIGTIFF: raise ValueError("File is not a BigTIFF") fp.seek(16) magic, version, frame_data_size, roi_data_size = struct.unpack( '<IIII', fp.read(16)) if magic != SCANIMAGE_TIFF_MAGIC or version != SCANIMAGE_TIFF_VERSION: raise ValueError("File is not a ScanImage BigTIFF v3") frame_data = json.loads( stripnull(fp.read(frame_data_size)).decode('utf-8')) roi_data = json.loads(stripnull(fp.read(roi_data_size)).decode('utf-8')) return frame_data, roi_data
def data_segment(self): """Read and return SubBlockSegment at file_position.""" # return Segment(self._fh, self.file_position).data() fh = self._fh with fh.lock: fh.seek(self.file_position) try: sid, _, _ = struct.unpack('<16sqq', fh.read(32)) except struct.error: raise SegmentNotFoundError('can not read ZISRAW segment') sid = bytes2str(stripnull(sid)) if sid not in SEGMENT_ID: raise SegmentNotFoundError('not a ZISRAW segment') data_segment = SEGMENT_ID[sid](fh) return data_segment
def data_segment(self): """Read and return SubBlockSegment at file_position.""" # return Segment(self._fh, self.file_position).data() fh = self._fh with fh.lock: fh.seek(self.file_position) try: sid, _, _ = struct.unpack('<16sqq', fh.read(32)) except struct.error: raise SegmentNotFoundError("can not read ZISRAW segment") sid = stripnull(sid) if sid not in SEGMENT_ID: raise SegmentNotFoundError("not a ZISRAW segment") data_segment = SEGMENT_ID[sid](fh) return data_segment
def __init__(self, fh, fpos=None): """Read segment header from file.""" if fpos is not None: fh.seek(fpos) try: (self.sid, self.allocated_size, self.used_size) = struct.unpack('<16sqq', fh.read(32)) except struct.error: raise SegmentNotFoundError("can not read ZISRAW segment") self.sid = stripnull(self.sid) if self.sid not in SEGMENT_ID: if not self.sid.startswith(b'ZISRAW'): raise SegmentNotFoundError("not a ZISRAW segment") warnings.warn("unknown segment type %s" % self.sid) self.data_offset = fh.tell() self._fh = fh
def __init__(self, fh, fpos=None): """Read segment header from file.""" if fpos is not None: fh.seek(fpos) try: (self.sid, self.allocated_size, self.used_size ) = struct.unpack('<16sqq', fh.read(32)) except struct.error: raise SegmentNotFoundError("can not read ZISRAW segment") self.sid = stripnull(self.sid) if self.sid not in SEGMENT_ID: if not self.sid.startswith(b'ZISRAW'): raise SegmentNotFoundError("not a ZISRAW segment") warnings.warn("unknown segment type %s" % self.sid) self.data_offset = fh.tell() self._fh = fh
def read_lookup_table(fh, filesize=None): """Read list of LookupTableEntry from file. Return as tuple((identifier, ndarray),) CZLUT LookupTables content schema. """ # TODO: test this; CZI specification is unclear. size, number = struct.unpack('<ii', fh.read(8)) luts = [] for _ in range(number): size, identifier, number = struct.unpack('<i80si', fh.read(88)) identifier = unicode(stripnull(identifier), 'utf-8') components = [None] * number for _ in range(number): size, component_type, number = struct.unpack('<iii', fh.read(12)) intensity = fh.read_array(dtype='<i2', count=number) if component_type == -1: # RGB components = intensity.reshape(-1, 3).T else: # R, G, B components[component_type] = intensity luts.append((identifier, numpy.array(components, copy=True))) return tuple(luts)
def read_xml(fh, filesize, raw=True): """Read XML from file and return as unicode string (default) or dict.""" xml = stripnull(fh.read(filesize)) return unicode(xml, 'utf-8') if raw else xml2dict(xml)
def __init__(self, fh): size, identifier, number = struct.unpack('<i80si', fh.read(88)) self.identifier = unicode(stripnull(identifier), 'utf-8') self.components = [ComponentEntry(fh) for _ in range(number)]
def __init__(self, fh): (self.dimension, self.start, self.size, self.start_coordinate, stored_size) = struct.unpack('<4siifi', fh.read(20)) self.dimension = stripnull(self.dimension) self.stored_size = stored_size if stored_size else self.size
def xml_reader(fh, filesize): """Read XML from file and return as xml.ElementTree root Element.""" xml = unicode(stripnull(fh.read(filesize)), 'utf-8') return etree.fromstring(xml)
def __init__(self, fh): """Read DimensionEntryDV1 from file.""" (self.dimension, self.start, self.size, self.start_coordinate, stored_size) = struct.unpack('<4siifi', fh.read(20)) self.dimension = bytes2str(stripnull(self.dimension)) self.stored_size = stored_size if stored_size else self.size