Skip to content

surfarray.blit_array performs undocumented rounding of float arraysย #3683

@mohammed5920

Description

@mohammed5920
def blit_array(surface, array):
    """pygame.surfarray.blit_array(Surface, array): return None

    Blit directly from an array of values.

    Directly copy values from an array into a Surface. This is faster than
    converting the array into a Surface and blitting. The array must be the
    same dimensions as the Surface and will completely replace all pixel
    values. Only integer, ascii character and record arrays are accepted.

    This function will temporarily lock the Surface as the new values are
    copied.
    """
    if isinstance(array, numpy_ndarray) and array.dtype in numpy_floats:
        array = array.round(0).astype(numpy_uint32)
    return array_to_surface(surface, array)

it's explicitly mentioned that Only integer, ascii character and record arrays are accepted., but pygame-ce will also accept floats and implicitly run a costly conversion instead, which is surprising because this is meant to be a more performance oriented API. I can't seem to find anywhere where this behaviour is documented, I discovered it by accident myself when debugging a path that mistakenly outputted a float array, and performance tanked implicitly as a result.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions