If palette columns are the same width, read it in one step. #210

Should be more efficient, hopefully.
This commit is contained in:
John Evans 2014-04-02 10:35:24 -04:00
commit 8e6dc486da

View file

@ -1933,10 +1933,26 @@ class PaletteBox(Jp2kBox):
# Need to determine bps and signed or not # Need to determine bps and signed or not
read_buffer = fptr.read(num_columns) read_buffer = fptr.read(num_columns)
data = struct.unpack('>' + 'B' * num_columns, read_buffer) bps_signed = struct.unpack('>' + 'B' * num_columns, read_buffer)
bps = [((x & 0x7f) + 1) for x in data] bps = [((x & 0x7f) + 1) for x in bps_signed]
signed = [((x & 0x80) > 1) for x in data] signed = [((x & 0x80) > 1) for x in bps_signed]
if any(b != bps_signed[0] for b in bps_signed):
# Ok the palette has the same datatype for all columns. We should
# be able to efficiently read it.
if bps <= 8:
dtype = np.uint8
elif bps <= 16:
dtype = np.uint16
elif bps <= 32:
dtype = np.uint32
read_buffer = fptr.read(num_entries * np.sum(bps) / 8)
palette = np.frombuffer(read_buffer, dtype)
palette.reshape((num_entries, num_columns))
else:
# General case where the columns may not be the same width.
fmt = '>' fmt = '>'
for bits in bps: for bits in bps:
if bits <= 8: if bits <= 8: