
    ,}hW*                     \   d dl mZ d dlmZ d dlZddlmZ ddlmZ 	  ed        G d
 de      Zdedej                  fdZdedej                  ddfdZej"                  dfdej                  dededej                  fdZd#dej                  dedej                  fdZd#dej                  dedefdZej"                  ddfdej                  dedededej                  f
dZd$dej                  dedej                  fdZd$dej                  dedefdZej"                  dfdej                  dededej                  fd Zej"                  dfdedededej                  fd!Zej"                  fdededej                  fd"Zy# e	e
f$ rZ ede d	       Y dZ[dZ[ww xY w)%    )Enum)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                   $    e Zd ZdZdZdZdZdZdZy)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__	UNCHANGEDGRAY
GRAY_ALPHARGB	RGB_ALPHA     S/var/www/html/test/engine/venv/lib/python3.12/site-packages/torchvision/io/image.pyr
   r
      s"     IDJ
CIr   r
   pathreturnc                     t         j                  j                         s-t         j                  j                         st	        t
               t         j                  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   datas     r   r!   r!   '   sL     99!!#EII,@,@,BI&99??$$SY/DKr   filenamer$   c                     t         j                  j                         s-t         j                  j                         st	        t
               t         j                  j                  j                  t        |       |       y)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$   s     r   r'   r'   8   sG     99!!#EII,@,@,BJ'	IIOOs8}d3r   Finputmodeapply_exif_orientationc                    t         j                  j                         s-t         j                  j                         st	        t
               t         j                  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*   outputs       r   r,   r,   F   sS    * 99!!#EII,@,@,BJ'YY__''tzz5BXYFMr   compression_levelc                     t         j                  j                         s-t         j                  j                         st	        t
               t         j                  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(   r0   r/   s      r   r2   r2   a   sK     99!!#EII,@,@,BJ'YY__''/@AFMr   c                     t         j                  j                         s-t         j                  j                         st	        t
               t        | |      }t        ||       y)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_pngr2   r'   )r(   r%   r0   r/   s       r   r4   r4   v   sB     99!!#EII,@,@,BI&01Fx r   cpudevicec                    t         j                  j                         s-t         j                  j                         st	        t
               t        j                  |      }|j                  dk(  r7t         j                  j                  j                  | |j                  |      }|S t         j                  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_jpegr6   typer"   r   decode_jpeg_cudar-   )r(   r)   r6   r*   r/   s        r   r9   r9      s    J 99!!#EII,@,@,BK(\\&!F{{f11%VL M ,,UDJJ@VWMr   qualityc                    t         j                  j                         s-t         j                  j                         st	        t
               |dk  s|dkD  rt        d      t         j                  j                  j                  | |      }|S )a
  
    Takes an input tensor in CHW layout and returns a buffer with the contents
    of its corresponding JPEG file.

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

    Returns:
        output (Tensor[1]): A one dimensional int8 tensor that contains the raw bytes of the
            JPEG file.
    r   d   z;Image quality should be a positive number between 1 and 100)	r   r   r   r    r   encode_jpeg
ValueErrorr"   r   )r(   r<   r/   s      r   r?   r?      sc     99!!#EII,@,@,BK({gmVWWYY__((8FMr   c                     t         j                  j                         s-t         j                  j                         st	        t
               t        | |      }t        ||       y)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_jpegr?   r'   )r(   r%   r<   r/   s       r   rB   rB      sA     99!!#EII,@,@,BJ'(Fx r   c                    t         j                  j                         s-t         j                  j                         st	        t
               t         j                  j                  j                  | |j                  |      }|S )aS  
    Detects whether an image is a JPEG or PNG 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.
        apply_exif_orientation (bool): apply EXIF orientation transformation to the output tensor.
            Default: False.

    Returns:
        output (Tensor[image_channels, image_height, image_width])
    )	r   r   r   r    r   decode_imager"   r   r-   r.   s       r   rD   rD      sQ    . 99!!#EII,@,@,BL)YY__))%=STFMr   c                     t         j                  j                         s-t         j                  j                         st	        t
               t        |       }t        |||      S )a  
    Reads a JPEG or 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:
        path (str or ``pathlib.Path``): path of the JPEG or 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])
    )r*   )r   r   r   r    r   
read_imager!   rD   )r   r)   r*   r$   s       r   rF   rF      sD    ( 99!!#EII,@,@,BJ'T?Dd;QRRr   c                     t        |       }t        j                  j                  j	                  ||j
                  d      S )NT)r!   r   r"   r   r,   r-   )r   r)   r$   s      r   _read_png_16rH     s,    T?D99??%%dDJJ==r   )   )K   )enumr   warningsr   r   	extensionr   utilsr   ImportErrorOSErrorer
   r#   Tensorr!   r'   r   boolr,   intr2   r4   r9   r?   rB   rD   rF   rH   r   r   r   <module>rU      sC      % ''D $C ELL "4 4ELL 4T 4 0=/F/Fgl<<,`d
\\6ell s 5<< *!U\\ !S !S !( (11#(	,<<,
, , !	,
 \\,^u|| c 5<< 0!ell !c !C !$ 0=/F/Fgl<<,`d
\\< &3%<%<]bS
S"SVZS
\\S4 3@2I2I >s >- >ell >W 	W 
21# 6b 	c s   F F+F&&F+