
    3j%                    0   % S SK Jr  S SKJr  S SKJr  S SKJrJr  S SK	r	S SK
r
S SKrS SKJr  S SKJrJr   " S S	\5      rS
rSS jrSSSS.rS\S'   SS jrSS jrSS jrSS jr\R4                  S4       SS jjrS S jrS!S jrS!S jrS"S#S jjrg)$    )annotations)Enum)Path)AnyUnionN)KORNIA_CHECK)image_to_tensortensor_to_imagec                  0    \ rS rSrSrSrSrSrSrSr	Sr
S	rg
)ImageLoadType    z'Enum to specify the desired image type.r                   N)__name__
__module____qualname____firstlineno____doc__	UNCHANGEDGRAY8RGB8RGBA8GRAY32RGB32__static_attributes__r       F/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/io/io.pyr   r       s#    2IEDEFEr    r   s   PNG

c                    [        U S5       nUR                  S5      n[        U5      S:  d  USS [        :w  a
   SSS5        gUS   sSSS5        $ ! , (       d  f       g= f)zRead the color type byte from a PNG file header.

Returns None if the file is truncated or has an invalid PNG signature.
PNG color types: 0=Grayscale, 2=RGB, 3=Indexed, 4=Grayscale+Alpha, 6=RGBA.
rb   N      )openreadlen_PNG_SIGNATURE)	path_filefheaders      r!   _read_png_color_typer.   .   sV     
i	!v;vbqz^;	 
	
 bz 
		s   /AA
A!monorgba)r   r      zdict[int, str]_PNG_COLOR_TYPE_TO_MODEc                r   U R                   R                  5       S;   a   [        R                  " [	        U 5      5      nOU R                   R                  5       S:X  aj  [        U 5      n[        R                  U5      nUb  US:X  a   [        R                  " [	        U 5      5      nO@[        R                  " [	        U 5      U5      nO[        R                  " [	        U 5      5      n[        USS9n[        U[        R                  5      (       d  Uc  UO[        R                  " U5      nUR                  US9$ )a,  Read an image file and decode using the Kornia Rust backend.

The decoded image is returned as numpy array with shape HxWxC.

Args:
    path_file: Path to a valid image file.
    device: the device where you want to get your image placed.

Return:
    Image torch.Tensor with shape :math:`(3,H,W)`.

.jpg.jpeg.pngrgbT)keepdim)device)suffixlower	kornia_rsread_image_jpegturbostrr.   r2   getread_image_anyread_image_png_u8r	   
isinstancetorchr:   to)r+   r:   img
color_typemodeimg_tdevs          r!   _load_image_to_tensorrK   G   s     #44,,S^<						!V	+))4
&**:6<45=**3y>:C--c)ndCC&&s9~6 C.E vu||44&ELLY_L`C8838r    c                t    [        U R                  [        R                  :H  5        U R	                  5       S-  $ )z)Convert an image torch.Tensor to float32.     o@)r   dtyperD   uint8floatimages    r!   _to_float32rS   l   s(    +,;;=5  r    c                    [        U R                  [        R                  :H  5        U R	                  S5      R                  5       $ )z'Convert an image torch.Tensor to uint8.rM   )r   rN   rD   float32mulbyterQ   s    r!   	_to_uint8rX   r   s/    -.99U  ""r    c           	        U R                   S   nU[        R                  :X  a  U $ US:X  aG  U[        R                  :w  a3  [	        [
        R                  R                  [        U 5      5      5      n SnX4= a#   S:H  a  u  [        R                  :X  a  S:X  a    U $   = a@   S:H  a:  u  [        R                  :X  a%  S:X  a!    [
        R                  R                  U 5      $   = a#   S:H  a  u  [        R                  :X  a  S:X  a    U $   = a@   S:H  a:  u  [        R                  :X  a%  S:X  a!    [
        R                  R                  U 5      $   = a#   S:H  a  u  [        R                  :X  a  S:X  a    U $   = aS   S:H  aM  u  [        R                  :X  a8  S:X  a4    [	        [
        R                  R                  [        U 5      S5      5      $   = ap   S:H  aj  u  [        R                  :X  aU  S:X  aQ    [	        [
        R                  R                  [
        R                  R                  [        U 5      5      S5      5      $   = a,   S:H  a&  u  [        R                  :X  a  S:X  a    [        U 5      $   = aI   S:H  aC  u  [        R                  :X  a.  S:X  a*    [
        R                  R                  [        U 5      5      $   = a,   S:H  a&  u  [        R                  :X  a  S:X  a    [        U 5      $    aH   S:H  aB  u  [        R                  :X  a-  S:X  a)  [
        R                  R                  [        U 5      5      $    [!        SU SU 35      e)	z;Convert a raw CxHxW uint8 image tensor to the desired type.r   r   r   r   r   g        zUnsupported conversion: z channels to )shaper   r   r   rX   korniacolorrgba_to_rgbrS   r   rgb_to_grayscaler   grayscale_to_rgbrgb_to_rgbar   r   NotImplementedError)rR   desired_typechannelss      r!   _convert_image_typerd   x   s[   {{1~H}... 1})<)<<&,,22;u3EFG
"%%m!!1%L &%%m!!1%<<0077 &$$m  !$L %$$m  !$<<0077 %%%m!!1%L &%%m!!1%V\\55k%6H#NOO &%%m!!1%V\\55fll6S6ST_`eTf6gilmnn &&&m""A&u%% '&&m""A&<<00U1CDD '%%m!!1%u%% &%%m!!1<<00U1CDD &%(@
-XdWe&fggr    cpuc                n    [        U [        5      (       d  [        U 5      n [        X5      n[        X15      $ )a8  Read an image file and decode using the Kornia Rust backend.

Args:
    path_file: Path to a valid image file.
    desired_type: the desired image type, defined by color space and dtype.
    device: the device where you want to get your image placed.

Return:
    Image torch.Tensor with shape :math:`(3,H,W)`.

)rC   r   rK   rd   )r+   rb   r:   rR   s       r!   
load_imagerg      s1      i&&O	 0	BEu33r    c                   U R                   R                  5       S;   aW  UR                  S:X  d#  UR                  S:X  a  UR                  S   S:X  a  SOSn[        R
                  " [        U 5      XUS9  gU R                   R                  5       S	:X  aV  UR                  S:X  d#  UR                  S:X  a  UR                  S   S:X  a  SOSn[        R                  " [        U 5      XS
9  gU R                   R                  5       S:X  aV  UR                  S:X  d#  UR                  S:X  a  UR                  S   S:X  a  SOSn[        R                  " [        U 5      XS
9  g[        SU R                    S35      e)zWrite uint8 image to file.r4   r   r   r   r/   r8   )rH   qualityr7   rH   .tiffUnsupported file extension: z for uint8 imageN)
r;   r<   ndimrZ   r=   write_image_jpegr?   write_image_png_u8write_image_tiff_u8ra   )r+   img_nprj   rH   s       r!   _write_uint8_imagers      s3   #44)fkkQ.>6<<PRCSWXCXv_d""3y>6gV						!V	+)fkkQ.>6<<PRCSWXCXv_d$$S^VG						!W	,)fkkQ.>6<<PRCSWXCXv_d%%c)nfH!$@AQAQ@RRb"cddr    c                   UR                   S:X  d#  UR                   S:X  a  UR                  S   S:X  a  SOSnU R                  R                  5       S:X  a  [        R
                  " [        U 5      XS9  gU R                  R                  5       S	:X  a  [        R                  " [        U 5      XS9  g[        S
U R                   S35      e)zWrite uint16 image to file.r   r   ri   r   r/   r8   r7   rk   rl   rm   z for uint16 imageN)	rn   rZ   r;   r<   r=   write_image_png_u16r?   write_image_tiff_u16ra   r+   rr   rH   s      r!   _write_uint16_imagerx      s    [[A%&++*:v||B?OST?T6[`D6)%%c)nfH						!W	,&&s9~vI!$@AQAQ@RRc"deer    c                   UR                   S:X  d#  UR                   S:X  a  UR                  S   S:X  a  SOSnU R                  R                  5       S:X  a  [        R
                  " [        U 5      XS9  g[        S	U R                   S
35      e)zWrite float32 image to file.r   r   ri   r   r/   r8   rl   rk   rm   z for float32 imageN)rn   rZ   r;   r<   r=   write_image_tiff_f32r?   ra   rw   s      r!   _write_float32_imager{      sy    [[A%&++*:v||B?OST?T6[`D7*&&s9~vI!$@AQAQ@RRd"effr    c                @   [        U [        5      (       d  [        U 5      n [        U R                  S;   SU  S35        [        UR	                  5       S:  SUR
                   S35        [        USSS9nUR                  S:X  a  US	   nUR                  [        R                  :X  a  [        XU5        gUR                  [        R                  :X  a  [        X5        gUR                  [        R                  :X  a  [        X5        g[!        S
UR                   35      e)a7  Save an image file using the Kornia Rust backend.

Args:
    path_file: Path to a valid image file.
    image: Image torch.Tensor with shape :math:`(3,H,W)`, `(1,H,W)` and `(H,W)`.
    quality: The quality of the JPEG encoding. If the file extension is .png or .tiff, the quality is ignored.

Return:
    None.
)r5   r6   r7   rl   zInvalid file extension: z1, only .jpg, .jpeg, .png and .tiff are supported.r   zInvalid image shape: z. Must be at least 2D.T)r9   force_contiguous).NzUnsupported image dtype: N)rC   r   r   r;   dimrZ   r
   rn   rN   rD   rO   rs   uint16rx   rU   r{   ra   )r+   rR   rj   rr   s       r!   write_imager      s     i&&O	>>
"9+-^_
 !%:5;;-G]#^_UD4HF{{a	"{{ekk!9g6		$I.		%Y/!$=ekk]"KLLr    )r+   r   returnz
int | None)r+   r   r:   Union[str, torch.device, None]r   torch.Tensor)rR   r   r   r   )rR   r   rb   r   r   r   )r+   
str | Pathrb   r   r:   r   r   r   )r+   r   rr   r   rj   intr   None)r+   r   rr   r   r   r   )P   )r+   r   rR   r   rj   r   r   r   ) 
__future__r   enumr   pathlibr   typingr   r   r=   rD   r[   kornia.core.checkr   kornia.image.utilsr	   r
   r   r*   r.   r2   __annotations__rK   rS   rX   rd   r   rg   rs   rx   r{   r   r   r    r!   <module>r      s   $ #       * ?D  && +  " J!#$hR #0"5"5-2444 +4 	42efgMr    