You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

24 lines
830 B

  1. import struct
  2. def read_chunk(fh, chunk_location):
  3. """
  4. Reads a piece of data given the location of its pointer.
  5. :param fh: an open file handle to the ND2
  6. :param chunk_location: a pointer
  7. :type chunk_location: int
  8. :rtype: bytes
  9. """
  10. fh.seek(chunk_location)
  11. # The chunk metadata is always 16 bytes long
  12. chunk_metadata = fh.read(16)
  13. header, relative_offset, data_length = struct.unpack("IIQ", chunk_metadata)
  14. if header != 0xabeceda:
  15. raise ValueError("The ND2 file seems to be corrupted.")
  16. # We start at the location of the chunk metadata, skip over the metadata, and then proceed to the
  17. # start of the actual data field, which is at some arbitrary place after the metadata.
  18. fh.seek(chunk_location + 16 + relative_offset)
  19. return fh.read(data_length)