def as_values(self, raw_row): """Convert a row of raw bytes into a flat row. Result may or may not share with argument""" if self.bit_depth == 8: return raw_row if self.bit_depth == 16: raw_row = tostring(raw_row) return array('H', struct.unpack('!%dH' % (len(raw_row) // 2), raw_row)) assert self.bit_depth < 8 width = self.width # Samples per byte spb = 8 // self.bit_depth out = array('B') mask = 2 ** self.bit_depth - 1 shifts = map(self.bit_depth.__mul__, reversed(range(spb))) for o in raw_row: out.extend(map(lambda i: mask&(o>>i), shifts)) return out[:width]
def serialtoflat_16(self, bytes, width=None): stringed_bytes = tostring(bytes) return array('H', struct.unpack('!%dH' % (len(stringed_bytes)//2), stringed_bytes))