Beispiel #1
0
def construct_bvdata(bpm):
    # just read the last image
    image = _control_ref().ReadImage()

    last_acq_time, last_x, last_y, last_intensity,\
    last_fwhm_x, last_fwhm_y, last_max_intensity,\
    last_proj_x, last_proj_y = bpm.get_bpm_result(image.frameNumber, image.timestamp)
    lima_roi = _control_ref().image().getRoi()
    roi_top_left = lima_roi.getTopLeft()
    roi_size = lima_roi.getSize()
    jpegFile = StringIO()
    image_type = _control_ref().image().getImageType()

    bpp = bpm.ImageType2Bpp[image_type]

    # manual scaling: use the user image min/max intensity to filter
    if not bpm.autoscale:
        min_val = bpm.min_max[0]
        max_val = bpm.min_max[1]
        scale_image = image.buffer.clip(min_val, max_val)

    # auto scaling: use natural image intensity
    else:
        min_val = image.buffer.min()
        max_val = image.buffer.max()
        if max_val == 0: max_val = 1
        scale_image = image.buffer

    # logarithmic scaling
    if bpm.lut_method == "LOG":
        if min_val > 0:
            scale_image = numpy.log10(scale_image)
        else:
            scale_image = numpy.log10(scale_image.clip(1, None))
            min_val += 1
        min_val = numpy.log10(min_val)
        max_val = numpy.log10(max_val)

    # scale the image to the whole range 16bit before palette transformation for 0 to 65535
    if max_val == min_val: max_val += 1
    scaling = (2**16 - 1.) / (max_val - min_val)
    scale_image = ((scale_image - min_val) * scaling).astype(numpy.uint16)

    # last transformation ot color or greys palette
    if bpm.color_map:
        img_buffer = bpm.palette["color"].take(scale_image, axis=0)
    else:
        img_buffer = bpm.palette["grey"].take(scale_image, axis=0)

    I = Image.fromarray(img_buffer, "RGB")
    if turbo_jpeg:
        jpegFile.write(
            turbo_jpeg.encode(numpy.asarray(I),
                              pixel_format=TJPF_RGB,
                              quality=bpm.jpeg_quality))
    else:
        I.save(jpegFile, "jpeg", quality=bpm.jpeg_quality)

    raw_jpeg_data = jpegFile.getvalue()
    image_jpeg = base64.b64encode(raw_jpeg_data)
    if bpm.return_bpm_profiles:
        profile_x = last_proj_x.tobytes()
        profile_y = last_proj_y.tobytes()
    else:
        profile_y = image.buffer[:, bpm.beammark[0]].astype(numpy.uint64)
        profile_y = profile_y.tobytes()
        profile_x = image.buffer[bpm.beammark[1], :].astype(numpy.uint64)
        profile_x = profile_x.tobytes()

    bvdata_format = 'dldddliiiidd%ds%ds%ds' % (len(profile_x), len(profile_y),
                                               len(image_jpeg))
    bvdata = struct.pack(bvdata_format, last_acq_time, image.frameNumber,
                         last_x, last_y, last_intensity, last_max_intensity,
                         roi_top_left.x, roi_top_left.y, roi_size.getWidth(),
                         roi_size.getHeight(), last_fwhm_x, last_fwhm_y,
                         profile_x, profile_y, image_jpeg)
    return bvdata, bvdata_format