
    khT                     $   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 	  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ej"                  eej"                     f   dedeeej4                  f   dedeej"                  eej"                     f   f
dZ	 d)deej"                  eej"                     f   dedeej"                  eej"                     f   fdZd)dej"                  dedefd Zej(                  dfdeej"                  ef   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dej"                  dej"                  fd#Z ej(                  fdej"                  dedej"                  fd$Z!da"d% Z#ej(                  fdej"                  dedej"                  fd&Z$ej(                  fdej"                  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)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                   (    e Zd ZdZdZdZdZdZdZeZ	y)ImageReadModea  Allow automatic conversion to RGB, RGBA, etc while decoding.

    .. note::

        You don't need to use this struct, you can just pass strings to all
        ``mode`` parameters, e.g. ``mode="RGB"``.

    The different available modes are the following.

    - UNCHANGED: loads the image as-is
    - RGB: converts to RGB
    - RGBA: converts to RGB with transparency (also aliased as RGB_ALPHA)
    - GRAY: converts to grayscale
    - GRAY_ALPHA: converts to grayscale with transparency

    .. note::

        Some decoders won't support all possible values, e.g. GRAY and
        GRAY_ALPHA are only supported for PNG and JPEG images.
    r      r         N)
__name__
__module____qualname____doc__	UNCHANGEDGRAY
GRAY_ALPHARGB	RGB_ALPHARGBA     P/var/www/teggl/fontify/venv/lib/python3.12/site-packages/torchvision/io/image.pyr   r      s'    * IDJ
CIDr   r   pathreturnc                     t         j                  j                         s-t         j                  j                         st	        t
               t         j                  j                  j                  t        |             }|S )z
    Return the bytes contents of a file as a uint8 1D Tensor.

    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$   4   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
    Write the content of an uint8 1D tensor 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*   D   sG     99!!#EII,@,@,BJ'	IIOOs8}d3r   Finputmodeapply_exif_orientationc                 T   t         j                  j                         s-t         j                  j                         st	        t
               t        |t              rt        |j                            }t         j                  j                  j                  | |j                  |      }|S )a  
    Decodes a PNG image into a 3 dimensional RGB or grayscale Tensor.

    The values of the output tensor are in uint8 in [0, 255] for most cases. If
    the image is a 16-bit png, then the output tensor is uint16 in [0, 65535]
    (supported from torchvision ``0.21``). Since uint16 support is limited in
    pytorch, we recommend calling
    :func:`torchvision.transforms.v2.functional.to_dtype()` with ``scale=True``
    after this function to convert the decoded image into a uint8 or float
    tensor.

    Args:
        input (Tensor[1]): a one dimensional uint8 tensor containing
            the raw bytes of the PNG image.
        mode (str or ImageReadMode): The mode to convert the image to, e.g. "RGB".
            Default is "UNCHANGED".  See :class:`~torchvision.io.ImageReadMode`
            for 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_png
isinstancer&   r   upperr%   r
   valuer+   r,   r-   outputs       r   r/   r/   Q   sk    8 99!!#EII,@,@,BJ'$TZZ\*YY__''tzz;QRF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+   r5   r4   s      r   r7   r7   u   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_pngr7   r*   )r+   r(   r5   r4   s       r   r9   r9      sB     99!!#EII,@,@,BI&01Fx r   cpudevicec                 P   t         j                  j                         s-t         j                  j                         st	        t
               t        |t              rt        j                  |      }t        |t              rt        |j                            }t        | t              rt        |       dk(  rt        d      t        d | D              st        d      t        d | D              st        d      |j                  dk(  r5t         j                   j"                  j%                  | |j&                  |      S | D cg c]7  }t         j                   j"                  j                  ||j&                  |      9 c}S | j                  j                  dk7  rt        d	      |j                  dk(  r9t         j                   j"                  j%                  | g|j&                  |      d   S t         j                   j"                  j                  | |j&                  |      S c c}w )
a  Decode JPEG image(s) into 3D RGB or grayscale Tensor(s), on CPU or CUDA.

    The values of the output tensor are uint8 between 0 and 255.

    .. note::
        When using a CUDA device, passing a list of tensors is more efficient than repeated individual calls to ``decode_jpeg``.
        When using CPU the performance is equivalent.
        The CUDA version of this function has explicitly been designed with thread-safety in mind.
        This function does not return partial results in case of an error.

    Args:
        input (Tensor[1] or list[Tensor[1]]): a (list of) one dimensional uint8 tensor(s) containing
            the raw bytes of the JPEG image. The tensor(s) must be on CPU,
            regardless of the ``device`` parameter.
        mode (str or ImageReadMode): The mode to convert the image to, e.g. "RGB".
            Default is "UNCHANGED".  See :class:`~torchvision.io.ImageReadMode`
            for 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] or list[Tensor[image_channels, image_height, image_width]]):
            The values of the output tensor(s) are uint8 between 0 and 255.
            ``output.device`` will be set to the specified ``device``


    r   z,Input list must contain at least one elementc              3   P   K   | ]  }t        |t        j                           y w)N)r0   r    Tensor.0ts     r   	<genexpr>zdecode_jpeg.<locals>.<genexpr>   s     >1:a.>s   $&z/All elements of the input list must be tensors.c              3   N   K   | ]  }|j                   j                  d k(    yw)r:   N)r;   typer?   s     r   rB   zdecode_jpeg.<locals>.<genexpr>   s     9a188==E)9s   #%z'Input list must contain tensors on CPU.cudar:   z!Input tensor must be a CPU tensor)r    r!   r"   r#   r	   decode_jpegr0   r&   r;   r   r1   listlen
ValueErrorallrD   r%   r
   decode_jpegs_cudar2   )r+   r,   r;   r-   imgs        r   rF   rF      s   V 99!!#EII,@,@,BK(&#f%$TZZ\*%u:?KLL>>>NOO9599FGG;;& 99??44UDJJOOdij]`EIIOO//TZZAWXjj <<%@AA;;& 99??44eWdjj&QRSTT99??..udjjBXYY ks   <H#qualityc                    t         j                  j                         s-t         j                  j                         st	        t
               |dk  s|dkD  rt        d      t        | t              r| st        d      | d   j                  j                  dk(  r*t         j                  j                  j                  | |      S | D cg c],  }t         j                  j                  j                  ||      . c}S | j                  j                  dk(  r.t         j                  j                  j                  | g|      d   S t         j                  j                  j                  | |      S c c}w )a  Encode RGB tensor(s) into raw encoded jpeg bytes, on CPU or CUDA.

    .. 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   rE   )r    r!   r"   r#   r	   encode_jpegrI   r0   rG   r;   rD   r%   r
   encode_jpegs_cuda)r+   rM   r
   s      r   rP   rP      s	   $ 99!!#EII,@,@,BK({gmVWW%cdd8??6)99??44UGDDMRSEEIIOO//w?SS<<&99??44eWgFqII99??..ug>> Ts   1E-c                    t         j                  j                         s-t         j                  j                         st	        t
               t        | |      }t        |t         j                        sJ 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_jpegrP   r0   r>   r*   )r+   r(   rM   r4   s       r   rS   rS     sU     99!!#EII,@,@,BJ'(Ffell+++x r   c                    t         j                  j                         s-t         j                  j                         st	        t
               t        | t         j                        st        t        |             } t        |t              rt        |j                            }t         j                  j                  j                  | |j                  |      }|S )a  Decode an image into a uint8 tensor, from a path or from raw encoded bytes.

    Currently supported image formats are jpeg, png, gif and webp.

    The values of the output tensor are in uint8 in [0, 255] for most cases.

    If the image is a 16-bit png, then the output tensor is uint16 in [0, 65535]
    (supported from torchvision ``0.21``). Since uint16 support is limited in
    pytorch, we recommend calling
    :func:`torchvision.transforms.v2.functional.to_dtype()` with ``scale=True``
    after this function to convert the decoded image into a uint8 or float
    tensor.

    .. note::

        ``decode_image()`` doesn't work yet on AVIF or HEIC images. For these
        formats, directly call  :func:`~torchvision.io.decode_avif` or
        :func:`~torchvision.io.decode_heic`.

    Args:
        input (Tensor or str or ``pathlib.Path``): The image to decode. If a
            tensor is passed, it must be one dimensional uint8 tensor containing
            the raw bytes of the image. Otherwise, this must be a path to the image file.
        mode (str or ImageReadMode): The mode to convert the image to, e.g. "RGB".
            Default is "UNCHANGED".  See :class:`~torchvision.io.ImageReadMode`
            for available modes.
        apply_exif_orientation (bool): apply EXIF orientation transformation to the output tensor.
           Only applies to JPEG and PNG images. Default: False.

    Returns:
        output (Tensor[image_channels, image_height, image_width])
    )r    r!   r"   r#   r	   decode_imager0   r>   r$   r&   r   r1   r%   r
   r2   r3   s       r   rU   rU     s    J 99!!#EII,@,@,BL)eU\\*#e*%$TZZ\*YY__))%=STFMr   c                     t         j                  j                         s-t         j                  j                         st	        t
               t        |       }t        |||      S )z<[OBSOLETE] Use :func:`~torchvision.io.decode_image` instead.)r-   )r    r!   r"   r#   r	   
read_imager$   rU   )r   r,   r-   r'   s       r   rW   rW   H  sD     99!!#EII,@,@,BJ'T?Dd;QRRr   c                     t         j                  j                         s-t         j                  j                         st	        t
               t         j                  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+   s    r   rY   rY   T  sD     99!!#EII,@,@,BJ'99??%%e,,r   c                 N   t         j                  j                         s-t         j                  j                         st	        t
               t        |t              rt        |j                            }t         j                  j                  j                  | |j                        S )a'  
    Decode a WEBP image into a 3 dimensional RGB[A] Tensor.

    The values of the output tensor are uint8 between 0 and 255.

    Args:
        input (Tensor[1]): a one dimensional contiguous uint8 tensor containing
            the raw bytes of the WEBP image.
        mode (str or ImageReadMode): The mode to convert the image to, e.g. "RGB".
            Default is "UNCHANGED".  See :class:`~torchvision.io.ImageReadMode`
            for available modes.

    Returns:
        Decoded image (Tensor[image_channels, image_height, image_width])
    )r    r!   r"   r#   r	   decode_webpr0   r&   r   r1   r%   r
   r2   r+   r,   s     r   r[   r[   h  sd    & 99!!#EII,@,@,BK($TZZ\*99??&&udjj99r   c                      t         ry 	 dd l} t        | d      sJ 	 | j                          da y # t        t        f$ r}t        d      |d }~ww xY w)Nr   expose_extra_decodersa  In order to enable the AVIF and HEIC decoding capabilities of torchvision, you need to `pip install torchvision-extra-decoders`. Just install the package, you don't need to update your code. This is only supported on Linux, and this feature is still in BETA stage. Please let us know of any issue: https://github.com/pytorch/vision/issues/new/choose. Note that `torchvision-extra-decoders` is released under the LGPL license. T)_EXTRA_DECODERS_ALREADY_LOADEDtorchvision_extra_decodershasattrAssertionErrorImportErrorRuntimeErrorr^   )r`   es     r   _load_extra_decoders_oncerf     sg    %) 13JKKK 446%)" K( Z
 	s   / AA

Ac                     t                | j                  t        j                  k7  rt	        d| j                         t        j
                  j                  j                  | |j                        S )a  Decode an AVIF image into a 3 dimensional RGB[A] Tensor.

    .. warning::
        In order to enable the AVIF decoding capabilities of torchvision, you
        first need to run ``pip install torchvision-extra-decoders``. Just
        install the package, you don't need to update your code. This is only
        supported on Linux, and this feature is still in BETA stage. Please let
        us know of any issue:
        https://github.com/pytorch/vision/issues/new/choose. Note that
        `torchvision-extra-decoders
        <https://github.com/pytorch-labs/torchvision-extra-decoders/>`_ is
        released under the LGPL license.

    The values of the output tensor are in uint8 in [0, 255] for most images. If
    the image has a bit-depth of more than 8, then the output tensor is uint16
    in [0, 65535]. Since uint16 support is limited in pytorch, we recommend
    calling :func:`torchvision.transforms.v2.functional.to_dtype()` with
    ``scale=True`` after this function to convert the decoded image into a uint8
    or float tensor.

    Args:
        input (Tensor[1]): a one dimensional contiguous uint8 tensor containing
            the raw bytes of the AVIF image.
        mode (str or ImageReadMode): The mode to convert the image to, e.g. "RGB".
            Default is "UNCHANGED".  See :class:`~torchvision.io.ImageReadMode`
            for available modes.

    Returns:
        Decoded image (Tensor[image_channels, image_height, image_width])
    ,Input tensor must have uint8 data type, got )	rf   dtyper    uint8rd   r%   extra_decoders_nsdecode_avifr2   r\   s     r   rl   rl     R    > {{ekk!I%++WXX99&&225$**EEr   c                     t                | j                  t        j                  k7  rt	        d| j                         t        j
                  j                  j                  | |j                        S )a  Decode an HEIC image into a 3 dimensional RGB[A] Tensor.

    .. warning::
        In order to enable the AVIF decoding capabilities of torchvision, you
        first need to run ``pip install torchvision-extra-decoders``. Just
        install the package, you don't need to update your code. This is only
        supported on Linux, and this feature is still in BETA stage. Please let
        us know of any issue:
        https://github.com/pytorch/vision/issues/new/choose. Note that
        `torchvision-extra-decoders
        <https://github.com/pytorch-labs/torchvision-extra-decoders/>`_ is
        released under the LGPL license.

    The values of the output tensor are in uint8 in [0, 255] for most images. If
    the image has a bit-depth of more than 8, then the output tensor is uint16
    in [0, 65535]. Since uint16 support is limited in pytorch, we recommend
    calling :func:`torchvision.transforms.v2.functional.to_dtype()` with
    ``scale=True`` after this function to convert the decoded image into a uint8
    or float tensor.

    Args:
        input (Tensor[1]): a one dimensional contiguous uint8 tensor containing
            the raw bytes of the HEIC image.
        mode (str or ImageReadMode): The mode to convert the image to, e.g. "RGB".
            Default is "UNCHANGED".  See :class:`~torchvision.io.ImageReadMode`
            for available modes.

    Returns:
        Decoded image (Tensor[image_channels, image_height, image_width])
    rh   )	rf   ri   r    rj   rd   r%   rk   decode_heicr2   r\   s     r   ro   ro     rm   r   )   )K   )&enumr   typingr   r   warningsr   r    	extensionr   utilsr	   rc   OSErrorre   r   r&   r>   r$   r*   r   boolr/   intr7   r9   r;   rF   rP   rS   rU   rW   rY   r[   r_   rf   rl   ro   r   r   r   <module>rz      sl       % ''D <C ELL  
4 
4ELL 
4T 
4 (11#(!<<!
! !! \\	!Hell s 5<< *!U\\ !S !S !( (11',#(	DZtELL112DZ
DZ #u||#$DZ !	DZ
 5<<ell++,DZP DF!?tELL112!?=@!?
5<<ell++,!?H!ell !c !C !( (11#(,s"#,
, !, \\	,b (11#(	S
	S
	S !	S \\		S-ell -u|| -, (11:<<:
: \\:Z "' *@ <I;R;R "Fu|| "F= "FW\WcWc "FJ <I;R;R "Fu|| "F= "FW\WcWc "Fa 	W 
21# 6b 	c s   I1 1J8J

J