U
    Ð‹ÑhŽ1  ã                
   @   sî  d dl mZ d dlmZmZ d dlmZ d dlZddlm	Z	 ddl
mZ ze	dƒ W n6 eefk
rŠ Z zed	e› d
�ƒ W 5 dZ[X Y nX G dd„ deƒZeejdœdd„Zeejddœdd„Zejdfejeeejdœdd„Zd4ejeejdœdd„Zd5ejeedœdd„Zejddfejeeeejdœd d!„Zd6eejeej f eeejeej f d#œd$d%„Zd7ejeed&œd'd(„Zejdfejeeejdœd)d*„Zejdfeeeejd+œd,d-„Zejfeeejd.œd/d0„Zejejd1œd2d3„Z dS )8é    )ÚEnum)ÚListÚUnion)ÚwarnNé   )Ú_load_library)Ú_log_api_usage_onceÚimagez(Failed to load image Python extension: 'zü'If you don't plan on using image functionality from `torchvision.io`, you can ignore this warning. Otherwise, there might be something wrong with your environment. Did you have `libjpeg` or `libpng` installed before building `torchvision` from source?c                   @   s$   e Zd ZdZdZdZdZdZdZdS )ÚImageReadModeaV  
    Support for various modes while reading images.

    Use ``ImageReadMode.UNCHANGED`` for loading the image as-is,
    ``ImageReadMode.GRAY`` for converting to grayscale,
    ``ImageReadMode.GRAY_ALPHA`` for grayscale with transparency,
    ``ImageReadMode.RGB`` for RGB and ``ImageReadMode.RGB_ALPHA`` for
    RGB with transparency.
    r   é   r   é   é   N)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	UNCHANGEDZGRAYZ
GRAY_ALPHAZRGBZ	RGB_ALPHA© r   r   úB/tmp/user/2202/pip-unpacked-wheel-mjxxo1pc/torchvision/io/image.pyr
      s   
r
   )ÚpathÚreturnc                 C   s2   t j ¡ st j ¡ sttƒ t jj t| ƒ¡}|S )zÙ
    Reads and outputs the bytes contents of a file as a uint8 Tensor
    with one dimension.

    Args:
        path (str or ``pathlib.Path``): the path to the file to be read

    Returns:
        data (Tensor)
    )	ÚtorchÚjitÚis_scriptingÚ
is_tracingr   Ú	read_fileÚopsr	   Ústr)r   Údatar   r   r   r   (   s    r   )Úfilenamer   r   c                 C   s4   t j ¡ st j ¡ sttƒ t jj t| ƒ|¡ dS )zñ
    Writes the contents of an uint8 tensor with one dimension to a
    file.

    Args:
        filename (str or ``pathlib.Path``): the path to the file to be written
        data (Tensor): the contents to be written to the output file
    N)	r   r   r   r   r   Ú
write_filer   r	   r   )r   r   r   r   r   r    9   s    	r    F)ÚinputÚmodeÚapply_exif_orientationr   c                 C   s6   t j ¡ st j ¡ sttƒ t jj | |jd|¡}|S )aö  
    Decodes a PNG image into a 3 dimensional RGB or grayscale Tensor.
    Optionally converts the image to the desired format.
    The values of the output tensor are uint8 in [0, 255].

    Args:
        input (Tensor[1]): a one dimensional uint8 tensor containing
            the raw bytes of the PNG image.
        mode (ImageReadMode): the read mode used for optionally
            converting the image. Default: ``ImageReadMode.UNCHANGED``.
            See `ImageReadMode` class for more information on various
            available modes.
        apply_exif_orientation (bool): apply EXIF orientation transformation to the output tensor.
            Default: False.

    Returns:
        output (Tensor[image_channels, image_height, image_width])
    F)	r   r   r   r   r   Ú
decode_pngr   r	   Úvalue©r!   r"   r#   Úoutputr   r   r   r$   G   s    r$   é   )r!   Úcompression_levelr   c                 C   s0   t j ¡ st j ¡ sttƒ t jj | |¡}|S )a	  
    Takes an input tensor in CHW layout and returns a buffer with the contents
    of its corresponding PNG file.

    Args:
        input (Tensor[channels, image_height, image_width]): int8 image tensor of
            ``c`` channels, where ``c`` must 3 or 1.
        compression_level (int): Compression factor for the resulting file, it must be a number
            between 0 and 9. Default: 6

    Returns:
        Tensor[1]: A one dimensional int8 tensor that contains the raw bytes of the
            PNG file.
    )r   r   r   r   r   Ú
encode_pngr   r	   )r!   r)   r'   r   r   r   r*   d   s    r*   )r!   r   r)   c                 C   s4   t j ¡ st j ¡ sttƒ t| |ƒ}t||ƒ dS )aÖ  
    Takes an input tensor in CHW layout (or HW in the case of grayscale images)
    and saves it in a PNG file.

    Args:
        input (Tensor[channels, image_height, image_width]): int8 image tensor of
            ``c`` channels, where ``c`` must be 1 or 3.
        filename (str or ``pathlib.Path``): Path to save the image.
        compression_level (int): Compression factor for the resulting file, it must be a number
            between 0 and 9. Default: 6
    N)r   r   r   r   r   Ú	write_pngr*   r    )r!   r   r)   r'   r   r   r   r+   y   s    
r+   Úcpu)r!   r"   Údevicer#   r   c                 C   s^   t j ¡ st j ¡ sttƒ t  |¡}|jdkrFt jj	 
| |j|¡}nt jj	 | |j|¡}|S )a  
    Decodes a JPEG image into a 3 dimensional RGB or grayscale Tensor.
    Optionally converts the image to the desired format.
    The values of the output tensor are uint8 between 0 and 255.

    Args:
        input (Tensor[1]): a one dimensional uint8 tensor containing
            the raw bytes of the JPEG image. This tensor must be on CPU,
            regardless of the ``device`` parameter.
        mode (ImageReadMode): the read mode used for optionally
            converting the image. The supported modes are: ``ImageReadMode.UNCHANGED``,
            ``ImageReadMode.GRAY`` and ``ImageReadMode.RGB``
            Default: ``ImageReadMode.UNCHANGED``.
            See ``ImageReadMode`` class for more information on various
            available modes.
        device (str or torch.device): The device on which the decoded image will
            be stored. If a cuda device is specified, the image will be decoded
            with `nvjpeg <https://developer.nvidia.com/nvjpeg>`_. This is only
            supported for CUDA version >= 10.1

            .. betastatus:: device parameter

            .. warning::
                There is a memory leak in the nvjpeg library for CUDA versions < 11.6.
                Make sure to rely on CUDA 11.6 or above before using ``device="cuda"``.
        apply_exif_orientation (bool): apply EXIF orientation transformation to the output tensor.
            Default: False. Only implemented for JPEG format on CPU.

    Returns:
        output (Tensor[image_channels, image_height, image_width])
    Úcuda)r   r   r   r   r   Údecode_jpegr-   Útyper   r	   Zdecode_jpeg_cudar%   )r!   r"   r-   r#   r'   r   r   r   r/   ‹   s    %

r/   éK   )r!   Úqualityr   c                    s´   t j ¡ st j ¡ sttƒ ˆ dk s,ˆ dkr4tdƒ‚t| tƒr~| sJtdƒ‚| d j	j
dkrjt jj | ˆ ¡S ‡ fdd„| D ƒS n2| j	j
dkr t jj | gˆ ¡d S t jj | ˆ ¡S d	S )
a-  
    Takes a (list of) input tensor(s) in CHW layout and returns a (list of) buffer(s) with the contents
    of the corresponding JPEG file(s).

    .. note::
        Passing a list of CUDA tensors is more efficient than repeated individual calls to ``encode_jpeg``.
        For CPU tensors the performance is equivalent.

    Args:
        input (Tensor[channels, image_height, image_width] or List[Tensor[channels, image_height, image_width]]):
            (list of) uint8 image tensor(s) of ``c`` channels, where ``c`` must be 1 or 3
        quality (int): Quality of the resulting JPEG file(s). Must be a number between
            1 and 100. Default: 75

    Returns:
        output (Tensor[1] or list[Tensor[1]]): A (list of) one dimensional uint8 tensor(s) that contain the raw bytes of the JPEG file.
    r   éd   z;Image quality should be a positive number between 1 and 100zDencode_jpeg requires at least one input tensor when a list is passedr   r.   c                    s   g | ]}t jj |ˆ ¡‘qS r   )r   r   r	   Úencode_jpeg)Ú.0r	   ©r2   r   r   Ú
<listcomp>Ø   s     zencode_jpeg.<locals>.<listcomp>N)r   r   r   r   r   r4   Ú
ValueErrorÚ
isinstanceÚlistr-   r0   r   r	   Zencode_jpegs_cuda)r!   r2   r   r6   r   r4   º   s    
r4   )r!   r   r2   c                 C   sD   t j ¡ st j ¡ sttƒ t| |ƒ}t|t jƒs6t	‚t
||ƒ dS )a�  
    Takes an input tensor in CHW layout and saves it in a JPEG file.

    Args:
        input (Tensor[channels, image_height, image_width]): int8 image tensor of ``c``
            channels, where ``c`` must be 1 or 3.
        filename (str or ``pathlib.Path``): Path to save the image.
        quality (int): Quality of the resulting JPEG file, it must be a number
            between 1 and 100. Default: 75
    N)r   r   r   r   r   Ú
write_jpegr4   r9   ÚTensorÚAssertionErrorr    )r!   r   r2   r'   r   r   r   r;   à   s
    
r;   c                 C   s4   t j ¡ st j ¡ sttƒ t jj | |j|¡}|S )a{  
    Detect whether an image is a JPEG, PNG or GIF and performs the appropriate
    operation to decode the image into a 3 dimensional RGB or grayscale Tensor.

    Optionally converts the image to the desired format.
    The values of the output tensor are uint8 in [0, 255].

    Args:
        input (Tensor): a one dimensional uint8 tensor containing the raw bytes of the
            PNG or JPEG image.
        mode (ImageReadMode): the read mode used for optionally converting the image.
            Default: ``ImageReadMode.UNCHANGED``.
            See ``ImageReadMode`` class for more information on various
            available modes. Ignored for GIFs.
        apply_exif_orientation (bool): apply EXIF orientation transformation to the output tensor.
            Ignored for GIFs. Default: False.

    Returns:
        output (Tensor[image_channels, image_height, image_width])
    )	r   r   r   r   r   Údecode_imager   r	   r%   r&   r   r   r   r>   ò   s    r>   )r   r"   r#   r   c                 C   s2   t j ¡ st j ¡ sttƒ t| ƒ}t|||d�S )a  
    Reads a JPEG, PNG or GIF image into a 3 dimensional RGB or grayscale Tensor.
    Optionally converts the image to the desired format.
    The values of the output tensor are uint8 in [0, 255].

    Args:
        path (str or ``pathlib.Path``): path of the JPEG, PNG or GIF image.
        mode (ImageReadMode): the read mode used for optionally converting the image.
            Default: ``ImageReadMode.UNCHANGED``.
            See ``ImageReadMode`` class for more information on various
            available modes. Ignored for GIFs.
        apply_exif_orientation (bool): apply EXIF orientation transformation to the output tensor.
            Ignored for GIFs. Default: False.

    Returns:
        output (Tensor[image_channels, image_height, image_width])
    )r#   )r   r   r   r   r   Ú
read_imager   r>   )r   r"   r#   r   r   r   r   r?     s    r?   )r   r"   r   c                 C   s   t | ƒ}tjj ||jd¡S )NT)r   r   r   r	   r$   r%   )r   r"   r   r   r   r   Ú_read_png_16-  s    r@   )r!   r   c                 C   s*   t j ¡ st j ¡ sttƒ t jj | ¡S )a&  
    Decode a GIF image into a 3 or 4 dimensional RGB Tensor.

    The values of the output tensor are uint8 between 0 and 255.
    The output tensor has shape ``(C, H, W)`` if there is only one image in the
    GIF, and ``(N, C, H, W)`` if there are ``N`` images.

    Args:
        input (Tensor[1]): a one dimensional contiguous uint8 tensor containing
            the raw bytes of the GIF image.

    Returns:
        output (Tensor[image_channels, image_height, image_width] or Tensor[num_images, image_channels, image_height, image_width])
    )r   r   r   r   r   Ú
decode_gifr   r	   )r!   r   r   r   rA   2  s    rA   )r(   )r(   )r1   )r1   )!Úenumr   Útypingr   r   Úwarningsr   r   Ú	extensionr   Úutilsr   ÚImportErrorÚOSErrorÚer
   r   r<   r   r    r   Úboolr$   Úintr*   r+   r/   r4   r;   r>   r?   r@   rA   r   r   r   r   Ú<module>   sv   
ÿýüüû0 ÿ þ&ýü!ýü