def _get_endianic_raw_data(self, size): big_endian = self._header_dictionary["BYTE_ORDER"] == "big_endian" with self.open_file(self._image_file, "rb") as fh: fh.seek(self._header_size) if big_endian == is_big_endian(): raw_data = read_uint16(streambuf(fh), int(size[0] * size[1])) else: raw_data = read_uint16_bs(streambuf(fh), int(size[0] * size[1])) # note that x and y are reversed here raw_data.reshape(flex.grid(size[1], size[0])) return raw_data
def get_raw_data(self): """Get the pixel intensities (i.e. read the image and return as a flex array of integers.)""" # It is better to catch FORMAT 86 here and fail with a sensible error msg # as soon as something is attempted with the image data rather than in # the understand method. Otherwise the user gets FormatBruker reading the # image improperly but without failing if self.header_dict["FORMAT"] != "100": raise NotImplementedError( "Only FORMAT 100 images from the Photon II are currently supported" ) f = self.open_file(self._image_file, "rb") header_size = int(self.header_dict["HDRBLKS"]) * 512 f.read(header_size) if is_big_endian(): read_2b = read_uint16_bs read_4b = read_uint32_bs else: read_2b = read_uint16 read_4b = read_uint32 # NPIXELB stores the number of bytes/pixel for the data and the underflow # table. We expect 1 byte for underflows and either 2 or 1 byte per pixel # for the data npixelb = [int(e) for e in self.header_dict["NPIXELB"].split()] assert npixelb[1] == 1 if npixelb[0] == 1: read_data = read_uint8 elif npixelb[0] == 2: read_data = read_2b else: raise IncorrectFormatError( "{} bytes per pixel is not supported".format(npixelb[0])) nrows = int(self.header_dict["NROWS"].split()[0]) ncols = int(self.header_dict["NCOLS"].split()[0]) raw_data = read_data(streambuf(f), nrows * ncols) image_size = (nrows, ncols) raw_data.reshape(flex.grid(*image_size)) (num_underflows, num_2b_overflows, num_4b_overflows) = [ int(e) for e in self.header_dict["NOVERFL"].split() ] # read underflows if num_underflows > 0: # stored values are padded to a multiple of 16 bytes nbytes = num_underflows + 15 & ~(15) underflow_vals = read_uint8(streambuf(f), nbytes)[:num_underflows] else: underflow_vals = None # handle 2 byte overflows if num_2b_overflows > 0: # stored values are padded to a multiple of 16 bytes nbytes = num_2b_overflows * 2 + 15 & ~(15) overflow_vals = read_2b(streambuf(f), nbytes // 2)[:num_2b_overflows] overflow = flex.int(nrows * ncols, 0) sel = (raw_data == 255).as_1d() overflow.set_selected(sel, overflow_vals - 255) overflow.reshape(flex.grid(*image_size)) raw_data += overflow # handle 4 byte overflows if num_4b_overflows > 0: # stored values are padded to a multiple of 16 bytes nbytes = num_4b_overflows * 4 + 15 & ~(15) overflow_vals = read_4b(streambuf(f), nbytes // 4)[:num_4b_overflows] overflow = flex.int(nrows * ncols, 0) sel = (raw_data == 65535).as_1d() overflow.set_selected(sel, overflow_vals - 65535) overflow.reshape(flex.grid(*image_size)) raw_data += overflow # handle underflows if underflow_vals is not None: sel = (raw_data == 0).as_1d() underflow = flex.int(nrows * ncols, 0) underflow.set_selected(sel, underflow_vals) underflow.reshape(flex.grid(*image_size)) raw_data += underflow # handle baseline. num_underflows == -1 means no baseline subtraction. See # https://github.com/cctbx/cctbx_project/files/1262952/BISFrameFileFormats.zip if num_underflows != -1: num_exposures = [int(e) for e in self.header_dict["NEXP"].split()] baseline = num_exposures[2] raw_data += baseline return raw_data