syphon.utils.numpy

 1from typing import Any
 2
 3import numpy as np
 4
 5from syphon.utils.raw import copy_mtl_texture_to_bytes, copy_bytes_to_mtl_texture
 6
 7
 8def copy_image_to_mtl_texture(image: np.ndarray, texture: Any):
 9    """
10    Copy pixel data from a NumPy array representing an image to a Metal texture.
11
12    Parameters:
13    - image (np.ndarray): The input image as a NumPy array of shape (m, n, 4).
14    - texture (Any): The target Metal texture to copy the image data into.
15
16    Raises:
17    - AssertionError: If the input image has an incorrect shape or number of channels.
18    """
19    assert len(image.shape) == 3, "Image has to be of shape (m, n, 4)"
20    assert image.shape[2] == 4, "Image has to be of shape (m, n, 4)"
21
22    data = image.tobytes()
23    copy_bytes_to_mtl_texture(data, texture)
24
25
26def copy_mtl_texture_to_image(texture: Any) -> np.ndarray:
27    """
28    Copy pixel data from a Metal texture to a NumPy array representing an image.
29
30    Parameters:
31    - texture (Any): The source Metal texture to copy pixel data from.
32
33    Returns:
34    - np.ndarray: The resulting image as a NumPy array of shape (height, width, 4).
35    
36    Note:
37    - todo: Add support for buffer re-use.
38    """
39    # todo: support buffer re-use
40    data = copy_mtl_texture_to_bytes(texture)
41    image = np.frombuffer(data, dtype=np.uint8)
42    return image.reshape((texture.height(), texture.width(), 4))
def copy_image_to_mtl_texture(image: numpy.ndarray, texture: Any):
 9def copy_image_to_mtl_texture(image: np.ndarray, texture: Any):
10    """
11    Copy pixel data from a NumPy array representing an image to a Metal texture.
12
13    Parameters:
14    - image (np.ndarray): The input image as a NumPy array of shape (m, n, 4).
15    - texture (Any): The target Metal texture to copy the image data into.
16
17    Raises:
18    - AssertionError: If the input image has an incorrect shape or number of channels.
19    """
20    assert len(image.shape) == 3, "Image has to be of shape (m, n, 4)"
21    assert image.shape[2] == 4, "Image has to be of shape (m, n, 4)"
22
23    data = image.tobytes()
24    copy_bytes_to_mtl_texture(data, texture)

Copy pixel data from a NumPy array representing an image to a Metal texture.

Parameters:

  • image (np.ndarray): The input image as a NumPy array of shape (m, n, 4).
  • texture (Any): The target Metal texture to copy the image data into.

Raises:

  • AssertionError: If the input image has an incorrect shape or number of channels.
def copy_mtl_texture_to_image(texture: Any) -> numpy.ndarray:
27def copy_mtl_texture_to_image(texture: Any) -> np.ndarray:
28    """
29    Copy pixel data from a Metal texture to a NumPy array representing an image.
30
31    Parameters:
32    - texture (Any): The source Metal texture to copy pixel data from.
33
34    Returns:
35    - np.ndarray: The resulting image as a NumPy array of shape (height, width, 4).
36    
37    Note:
38    - todo: Add support for buffer re-use.
39    """
40    # todo: support buffer re-use
41    data = copy_mtl_texture_to_bytes(texture)
42    image = np.frombuffer(data, dtype=np.uint8)
43    return image.reshape((texture.height(), texture.width(), 4))

Copy pixel data from a Metal texture to a NumPy array representing an image.

Parameters:

  • texture (Any): The source Metal texture to copy pixel data from.

Returns:

  • np.ndarray: The resulting image as a NumPy array of shape (height, width, 4).

Note:

  • todo: Add support for buffer re-use.