+
    åûjdœ  ã                  ó@  a € 0 t $ ^ RIHt ^ RIt^ RIHt ^ RIt^ RIHu H	t
 ^ RIHt ^ RIHtHt ^ RIHtHtHtHt ^ RIHt ^ RIHtHtHt ^ R	IHt ^ R
IHt ^ RIHt / tR] R&   / t!R] R&   RR.t"R R lt#R R lt$RBR R llt%R R lt&R R lt'R R lt(R R lt)R R  lt*R! R" lt+R# R$ lt,R% R& lt-R' R( lt.R) R* lt/R+ R, lt0R- R. lt1R/ R0 lt2R1 R2 lt3R3 R4 lt4R5 R6 lt5R7 R8 lt6R9 R: lt7]RCR; R< ll4       t8R= R> lt9R? R@ lt: ! RA R]Pv                  4      t<R# )Dé    )ÚannotationsN)ÚUnion)Únn)Úrgb_to_ycbcrÚycbcr_to_rgb)ÚKORNIA_CHECKÚKORNIA_CHECK_IS_TENSORÚKORNIA_CHECK_SAME_SIZEÚKORNIA_CHECK_SHAPE©Ú_device_literal)Ú_autocast_disabledÚ_to_device_nonblockingÚis_compiling)Ú_check_floating_image)Úgaussian_blur2d)Úperform_keep_shape_imagezhdict[tuple[Union[torch.dtype, None], Union[str, torch.device, None]], tuple[torch.Tensor, torch.Tensor]]Ú_DCT8_CACHEÚ_DEFAULT_QT_CACHEÚJPEGCodecDifferentiableÚjpeg_codec_differentiablec               ó    € V ^8„  d   QhRRRR/# ©é   Úinputútorch.TensorÚreturn© )Úformats   "ÚOC:\Ai_Coding\apps\studio\ComfyUI\.venv\Lib\site-packages\kornia\enhance\jpeg.pyÚ__annotate__r!   0   s   € ÷ 4ñ 4¨|ð 4Àñ 4ó    c                óN   € V P                  4       pWV,
          ^,          ,           # )zœDifferentiable rounding.

Args:
    input: Input tensor of any shape to be rounded.

Returns:
    Pseudo rounded tensor of the same shape as input tensor.

)Úround)r   Úinput_rounds   & r    Ú#_differentiable_polynomial_roundingr&   0   s"   € ð —+‘+“-€KØ +Õ-°!Õ3Õ3Ð3r"   c               ó    € V ^8„  d   QhRRRR/# r   r   )r   s   "r    r!   r!   >   s   € ÷ :ñ :¨Lð :¸\ñ :r"   c                ó\   € V P                  4       pWR,
          V,
          ^,          ,           # )z±Perform floor via a differentiable operation.

Args:
    input: Input tensor of any shape to be floored.

Returns:
    Pseudo rounded tensor of the same shape as input tensor.

ç      à?)Úfloor)r   Úinput_floors   & r    Ú _differentiable_polynomial_floorr,   >   s&   € ð —+‘+“-€KØ #�+¨Õ3¸Õ9Õ9Ð9r"   c          
     ó,   € V ^8„  d   QhRRRRRRRRRR/# )	r   r   r   Úmin_valzUnion[float, None]Úmax_valÚscaleÚfloatr   r   )r   s   "r    r!   r!   L   s:   € ÷ ñ Øðàðð  ðð ð	ð
 ñr"   c           
     óŽ  € T pVe_   WB,
          p\         P                  ! V^ 8„  V) \         P                  ! VP                  ^ R7      ) 4      R,
          ,          V,           V4      pVe]   WA,
          p\         P                  ! V^ 8  V\         P                  ! VP                  ^ R7      4      R,
          ,          V,           V4      pV# )a#  Clip via a differentiable and soft approximation of the clipping operation.

Args:
    input: Input tensor of any shape.
    min_val: Minimum value.
    max_val: Maximum value.
    scale: Scale value. Default 0.02.

Returns:
    Clipped output tensor of the same shape as the input tensor.

)Úminç      ð?)Úmax)ÚtorchÚwhereÚexpÚclamp)r   r.   r/   r0   ÚoutputÚoverÚunders   &&&&   r    Ú_differentiable_clippingr=   L   s§   € ð( !€FØÒØÕˆÜ—’˜T A™X¨ v´·²¸D¿J¹JÈ1¸JÓ<MÐ;MÓ1NÐQTÕ1TÕ'UÐX_Õ'_ÐagÓhˆØÒØÕ ˆÜ—’˜U Q™Y¨´·²¸5¿;¹;È1¸;Ó;MÓ1NÐQTÕ1TÕ(UÐX_Õ(_ÐagÓhˆØ€Mr"   c               ó$   € V ^8„  d   QhRRRRRR/# ©r   ÚdeviceúUnion[str, torch.device, None]ÚdtypeúUnion[torch.dtype, None]r   r   r   )r   s   "r    r!   r!   j   ó$   € ÷ ñ Ð<ð ÐE]ð Ðbnñ r"   c                óN   € \        . RO. RO. RO. RO. RO. RO. RO. R	O.V VR7      # )
z1Generate default Quantization table of Y channel.©r@   rB   )é   é   é
   rG   é   é(   é3   é=   )é   rN   é   é   é   é:   é<   é7   )rO   é   rG   rJ   rK   é9   éE   é8   )rO   é   é   é   rL   éW   éP   é>   )é   rZ   é%   rX   éD   ém   ég   éM   )rJ   é#   rT   é@   éQ   éh   éq   é\   )é1   rf   éN   r\   rc   éy   éx   ée   )éH   rj   é_   éb   ép   éd   rc   éc   r   rF   s   &&r    Ú_get_default_qt_yrv   j   s6   € ô â,Ú,Ú,Ú,Ú.Ú.Ú0Ú/ð		
ð Øôð r"   c               ó$   € V ^8„  d   QhRRRRRR/# r?   r   )r   s   "r    r!   r!   }   rD   r"   c                óN   € \        . RO. RO. RO. RO. RO. RO. RO. RO.V VR7      # )z2Generate default Quantization table of C channels.rF   )rY   r_   rJ   é/   ru   ru   ru   ru   )r_   é   rQ   éB   ru   ru   ru   ru   )rJ   rQ   rX   ru   ru   ru   ru   ru   )ry   r{   ru   ru   ru   ru   ru   ru   )ru   ru   ru   ru   ru   ru   ru   ru   r   rF   s   &&r    Ú_get_default_qt_cr|   }   s6   € ô â,Ú,Ú,Ú,Ú,Ú,Ú,Ú,ð		
ð Øôð r"   c               ó$   € V ^8„  d   QhRRRRRR/# )r   r@   rA   rB   rC   r   ú!tuple[torch.Tensor, torch.Tensor]r   )r   s   "r    r!   r!   �   s$   € ÷ "ñ "Ø*ð"Ø3Kð"à&ñ"r"   c                óä  € \        4       '       d   \        W4      \        W4      3# W3pV\        9  dž   \        P
                  ! R4      ;_uu_ 4        \        W4      \        W4      3pRRR4       X^ ,          P                  '       d?   \        P                  P                  V^ ,          P                  4      P                  4        V\        V&   \        V,          #   + '       g   i     L}; i)a?  Return the default Y and C quantization tables, cached per (dtype, device) in eager mode.

As in :func:`_get_dct8_basis_scale`, a captured forward builds them inside the graph and leaves the cache alone,
and the cache is filled outside inference mode, so that later calls in which autograd tracks the input can use it.
FN)r   rv   r|   r   r6   Úinference_modeÚis_cudaÚcudaÚcurrent_streamr@   Úsynchronize)r@   rB   ÚkeyÚtabless   &&  r    Ú_default_quantization_tablesr‡   �   s²   € ô ‡~‚~Ü  Ó/Ô1BÀ6Ó1QÐQÐQØˆ/€CØ
Ô#Ô#Ü×!Ò! %×(Õ(Ü'¨Ó6Ô8IÈ&Ó8XÐYˆF÷ )à�!�9××Ðô �J‰J×%Ñ% f¨Q¥i×&6Ñ&6Ó7×CÑCÔEØ!'Ô˜#ÑÜ˜SÕ!Ð!÷ )×(ús   ÁCÃC/	c               ó    € V ^8„  d   QhRRRR/# r   r   )r   s   "r    r!   r!   ¦   s   € ÷ ñ ˜ð ¨,ñ r"   c                ó®   € V P                   w  rpV P                  W^,          ^V^,          ^4      P                  ^ ^^^^4      P                  ^^4      pV# )zßExtract non-overlapping 8 x 8 patches from the given input image.

Args:
    input (torch.Tensor): Input image of the shape ``(B, H, W)``.

Returns:
    output (torch.Tensor): Image patchify of the shape ``(B, N, 8, 8)``.

)ÚshapeÚviewÚpermuteÚflatten)r   ÚBÚHÚWr:   s   &    r    Ú_patchify_8x8r‘   ¦   sV   € ð �k‰k�G€Aˆ!à Ÿ:™: a¨a­°°A¸µF¸AÓ>×FÑFÀqÈ!ÈQÐPQÐSTÓU×]Ñ]Ð^_ÐabÓc€FØ€Mr"   c               ó(   € V ^8„  d   QhRRRRRRRR/# )r   r   r   r�   Úintr�   r   r   )r   s   "r    r!   r!   ·   s(   € ÷ ñ ˜<ð ¨Cð °Cð ¸Lñ r"   c                óº   € V P                   R,          w  r4V P                  W1^,          V^,          ^^4      P                  ^ ^^^^4      P                  W1V4      pV# )a  Reverse non-overlapping 8 x 8 patching.

Args:
    input (torch.Tensor): Input image of the shape ``(B, N, 8, 8)``.
    H: height of resulting torch.Tensor.
    W: width of resulting torch.Tensor.

Returns:
    output (torch.Tensor): Image patchify of the shape ``(B, H, W)``.

:Nr   N)rŠ   r‹   rŒ   Úreshape)r   r�   r�   rŽ   Ú_Nr:   s   &&&   r    Ú_unpatchify_8x8r—   ·   sX   € ð �K‰K˜�O�E€Aà Ÿ:™: a¨a­°°aµ¸¸AÓ>×FÑFÀqÈ!ÈQÐPQÐSTÓU×]Ñ]Ð^_ÐdeÓf€FØ€Mr"   c               ó$   € V ^8„  d   QhRRRRRR/# )r   Úinput_cbr   Úinput_crr   r   )r   s   "r    r!   r!   Ê   s&   € ÷  Gñ  G˜|ð  G°|ð  GÈñ  Gr"   c                ój  € V P                   w  r#pV^,          p^V^,          ,          V,          p\        P                  ! W3^R7      P                  ^4      P	                  W&^,          ^4      p\        P
                  ! W`P                  R7      p\        P                  ! W…RR7      p	V	^V,          ,          W‰V,          ,
          ,           p
V
R,          \        P
                  ! ^V P                  R7      V,          ,           pVP                  ^VP	                  R4      4      P	                  W&^^4      # )aÃ  Patchify the Cb and Cr planes together into one tensor of 8 x 8 patches.

``patches = _patchify_chroma(input_cb, input_cr)`` takes the Cb and Cr planes of the shape ``(B, H, W)``, with
``H`` and ``W`` multiples of 8, and returns their non-overlapping 8 x 8 patches, the Cb ones followed by the Cr
ones, with the values and the order of ``_patchify_8x8`` on each plane. The planes are stacked along the height
and their rows of 8 pixels read with one ``torch.index_select``: no intermediate tensor has an axis of the patch
rows or columns of one plane, either of which can be 1, so ``torch.export`` keeps the image size dynamic.

Args:
    input_cb: Cb plane of the shape ``(B, H, W)``.
    input_cr: Cr plane of the shape ``(B, H, W)``.

Returns:
    The Cb patches followed by the Cr patches, of the shape ``(B, 2N, 8, 8)`` with ``N = (H // 8) * (W // 8)``.

See Also:
    - :func:`jpeg_codec_differentiable`: the public codec, whose encoder calls this helper.

©Údim©r@   r*   ©Úrounding_mode©ºNNNNéÿÿÿÿ)	rŠ   r6   Úcatr�   r‹   Úaranger@   ÚdivÚindex_select)r™   rš   rŽ   r�   r�   Ún_colsÚ	n_patchesÚrowsÚpatchÚ	block_rowÚ	first_rowÚsrcs   &&          r    Ú_patchify_chromar¯   Ê   së   € ð( �n‰n�G€Aˆ!Ø�!�V€FØ�Q˜!•V•˜vÕ%€Iô �9Š9�hÐ)¨qÔ1×9Ñ9¸!Ó<×AÑAÀ!ÐQRÅ]ÐTUÓV€Dô �LŠL˜¯?©?Ô;€EÜ—	’	˜%°wÔ?€IØ˜Q �ZÕ(¨EÀÕ4FÕ,FÕG€IØ
�GÕ
œuŸ|š|¨A°h·o±oÔFÈÕOÕ
O€CØ×Ñ˜Q §¡¨£Ó-×2Ñ2°1ÀÀAÓFÐFr"   c               ó(   € V ^8„  d   QhRRRRRRRR/# )r   r   r   r�   r“   r�   r   r~   r   )r   s   "r    r!   r!   í   s)   € ÷ &ñ &˜lð &¨sð &°sð &Ð?`ñ &r"   c                ó  € V P                   ^ ,          pV^,          p^V,          V,          p\        P                  ! WPP                  R7      p\        P                  ! WdRR7      p\        P                  ! V^RR7      pW„,          WgV,          ,
          ,           ^,          Wx^,          ,
          ,           p	V P                  W5^4      P                  ^V	4      P                  ^4      P                  V^W4      p
V
R,          V
R,          3# )aõ  Unpatchify the joint Cb and Cr patches back into two planes.

``cb, cr = _unpatchify_chroma(input, H, W)`` takes the Cb patches followed by the Cr patches, of the shape
``(B, 2N, 8, 8)``, and returns the Cb and the Cr plane, each of the shape ``(B, H, W)``. It undoes
``_patchify_chroma`` and gives the values of ``_unpatchify_8x8`` on each half, read as rows of 8 pixels with one
``torch.index_select``.

Args:
    input: Cb patches followed by Cr patches, of the shape ``(B, 2N, 8, 8)``.
    H: height of each plane, a multiple of 8.
    W: width of each plane, a multiple of 8.

Returns:
    The Cb and the Cr plane, each of the shape ``(B, H, W)``.

See Also:
    - :func:`jpeg_codec_differentiable`: the public codec, whose decoder calls this helper.

rž   r*   rŸ   ©r¢   r   ©r¢   é   )	rŠ   r6   r¥   r@   r¦   r•   r§   r�   r‹   )r   r�   r�   rŽ   r¨   Ún_rowsÚkÚ	plane_rowr¬   r®   Úplaness   &&&        r    Ú_unpatchify_chromar¹   í   sÐ   € ð( 	�‰�A�€AØ�!�V€FØ��U�V�^€Fô 	�Š�V§L¡LÔ1€AÜ—	’	˜!°7Ô;€IÜ—	’	˜) Q°gÔ>€IØÕ °Õ%7Õ!7Õ8¸AÕ
=ÀÐYZÍ]ÕAZÕ
[€CØ�]‰]˜1 aÓ(×5Ñ5°a¸Ó=×EÑEÀaÓH×MÑMÈaÐQRÐTUÓY€FØ�$�<˜ �Ð%Ð%r"   c               ó    € V ^8„  d   QhRRRR/# r   r   )r   s   "r    r!   r!     s   € ÷ ñ �Lð  \ñ r"   c                ó¨   € V P                   pV P                  p\        W4      w  r4VR,          \        P                  ! V R,
          V4      ,          pV# )zäPerform an 8 x 8 discrete cosine transform.

Args:
    input (torch.Tensor): Patched input torch.tensor of the shape ``(B, N, 8, 8)``.

Returns:
    output (torch.Tensor): DCT output torch.tensor of the shape ``(B, N, 8, 8)``.

ç      `@©NN)rB   r@   Ú_get_dct8_basis_scaler6   Ú	tensordot)r   rB   r@   Ú
dct_tensorÚ	dct_scaler:   s   &     r    Ú_dct_8x8rÂ     sG   € ð ',§k¡k€EØ-2¯\©\€FÜ1°%Ó@Ñ€Jà$ ZÕ0´5·?²?À5È5Å=ÐR\Ó3]Õ]€FØ€Mr"   c               ó    € V ^8„  d   QhRRRR/# r   r   )r   s   "r    r!   r!   !  s   € ÷ !ñ !�\ð ! lñ !r"   c                ó,  € V P                   pV P                  p\        P                  ! ^WR7      pVP	                  ^ 4      pVP	                  ^4      p\        P
                  ! RV,          R,           V,          \        P                  ,          R,          4      p\        R.R.^,          ,           WR7      p\        P                  ! Ww4      pW,          p W,          p	V	P                  RR	4      V,          P                  RR	4      p
V
R,          R,           # )
zíPerform an 8 x 8 inverse discrete cosine transform.

Args:
    input (torch.Tensor): Patched input torch.tensor of the shape ``(B, N, 8, 8)``.

Returns:
    output (torch.Tensor): IDCT output torch.tensor of the shape ``(B, N, 8, 8)``.

©rB   r@   ç       @r4   ç      0@ç      Ð?r¼   çÌ;fž æ?r£   éþÿÿÿ)rB   r@   r6   r¥   Ú	unsqueezeÚcosÚmathÚpir   ÚouterÚ	transpose)r   rB   r@   ÚidxÚspatial_idxÚfreq_idxÚbasisÚalpharÁ   Útmpr:   s   &          r    Ú	_idct_8x8r×   !  s×   € ð �K‰K€EØ�\‰\€Fä
�,Š,�q Ô
5€CØ—-‘- Ó"€KØ�}‰}˜QÓ€Hä�IŠI�s˜[Õ(¨3Õ.°(Õ:¼T¿W¹WÕDÀtÕKÓL€Eô ˜^Ð,°¨u°q­yÕ8ÀÔU€EÜ—’˜EÓ)€IØÕ€Eà
�-€CØ�m‰m˜B Ó# eÕ+×6Ñ6°r¸2Ó>€Fà�D�=˜5Õ Ð r"   c               ó    € V ^8„  d   QhRRRR/# )r   Úcompression_strengthr   r   r   )r   s   "r    r!   r!   ?  s   € ÷ ñ Ø&ðàñr"   c           	     óÖ   € \         P                  ! V R8H  \         P                  ! V 4      V 4      p\        \         P                  ! V^28  RV,          RRV,          ,
          4      4      pV# )aë  Convert a given JPEG quality to the scaling factor.

Args:
    compression_strength (torch.Tensor): Compression strength ranging from 0 to 100. Any shape is supported.
        A strength of exactly 0 is given the scale of strength 1, matching libjpeg, which gives quality 0
        the quality-1 table. Fractional strengths in (0, 1) are left alone and scale as the formula says.

Returns:
    scale (torch.Tensor): Scaling factor to be applied to quantization matrix. Same shape as input.

ç        g     ˆ³@g      i@rÆ   )r6   r7   Ú	ones_liker,   )rÙ   Ústrengthr0   s   &  r    Ú_jpeg_quality_to_scalerÞ   ?  sc   € ô( #Ÿ[š[Ø Ñ#¤U§_¢_Ð5IÓ%JÐL`ó€Hô ;Ü�ŠØ�r‰MØ�XÕØ�C˜(•NÕ"ó	
ó€Eð €Lr"   c               ó(   € V ^8„  d   QhRRRRRRRR/# ©r   r   r   Újpeg_qualityÚquantization_tabler   r   )r   s   "r    r!   r!   a  s0   € ÷ 7ñ 7Øð7àð7ð %ð7ð ñ	7r"   c                ó°   € VR,          \        V4      R,          ,          p\        \        VR,           R,          ^^ÿ4      4      pW,          p\        V4      # )aŠ  Perform quantization.

Args:
    input (torch.Tensor): Input torch.tensor of the shape ``(B, N, 8, 8)``.
    jpeg_quality (torch.Tensor): Compression strength to be applied, shape is ``(B)``.
    quantization_table (torch.Tensor): Quantization table of the shape ``(1, 8, 8)`` or ``(B, 8, 8)``.

Returns:
    output (torch.Tensor): Quantized output torch.tensor of the shape ``(B, N, 8, 8)``.

ç      I@ç      Y@r¡   ©r¢   NNN)rÞ   r,   r=   r&   ©r   rá   râ   Úquantization_table_scaledr:   s   &&&  r    Ú	_quantizeré   a  s\   € ð$ 	˜7Õ#Ô&<¸\Ó&JÐK^Õ&_Õ_ð ô :Ü Ð";¸dÕ"BÀeÕ!KÈQÐPSÓTóÐð !Õ5€Fä.¨vÓ6Ð6r"   c               ó(   € V ^8„  d   QhRRRRRRRR/# rà   r   )r   s   "r    r!   r!   ~  s0   € ÷ ñ Øðàðð %ðð ñ	r"   c                óœ   € VR,          \        V4      R,          ,          pV \        \        VR,           R,          ^^ÿ4      4      ,          pV# )aŒ  Perform dequantization.

Args:
    input (torch.Tensor): Input torch.tensor of the shape ``(B, N, 8, 8)``.
    jpeg_quality (torch.Tensor): Compression strength to be applied, shape is ``(B)``.
    quantization_table (torch.Tensor): Quantization table of the shape ``(1, 8, 8)`` or ``(B, 8, 8)``.

Returns:
    output (torch.Tensor): Quantized output torch.tensor of the shape ``(B, N, 8, 8)``.

rä   rå   r¡   ræ   )rÞ   r,   r=   rç   s   &&&  r    Ú_dequantizerì   ~  sU   € ð$ 	˜7Õ#Ô&<¸\Ó&JÐK^Õ&_Õ_ð ð !Ô#CÜ Ð";¸dÕ"BÀeÕ!KÈQÐPSÓTó$õ €Fð €Mr"   c               ó    € V ^8„  d   QhRRRR/# )r   Úinput_ycbcrr   r   z/tuple[torch.Tensor, torch.Tensor, torch.Tensor]r   )r   s   "r    r!   r!   ™  s   € ÷ !6ñ !6 \ð !6Ð6eñ !6r"   c                óv  € V R,          pV R,          pV R,          pV P                   R,          ^,          V P                   R,          ^,          rT\        P                  ! \        VR	,          R
R4      WE3RRR7      p\        P                  ! \        VR	,          R
R4      WE3RRR7      pWR,          VR,          3# )a‘  Implement chroma subsampling.

Args:
    input_ycbcr (torch.Tensor): YCbCr input torch.tensor of the shape ``(B, 3, H, W)``.

Returns:
    output_y (torch.Tensor): Y component (not-subsampled), shape is ``(B, H, W)``.
    output_cb (torch.Tensor): Cb component (subsampled), shape is ``(B, H // 2, W // 2)``.
    output_cr (torch.Tensor): Cr component (subsampled), shape is ``(B, H // 2, W // 2)``.

ÚbilinearF©ÚsizeÚmodeÚalign_cornersr²   r³   )r¢   r   rÊ   r£   r¡   )é   rõ   )r)   r)   )rŠ   ÚFÚinterpolater   )rî   Úoutput_yÚ	output_cbÚ	output_crÚh_halfÚw_halfs   &     r    Ú_chroma_subsamplingrý   ™  s¸   € ð )¨Õ.€HØ)¨$Õ/€IØ)¨$Õ/€Ið
 !×&Ñ& rÕ*¨aÕ/°×1BÑ1BÀ2Õ1FÈ!Õ1KˆFÜ—’Ü˜	 'Õ*¨F°JÓ?ØÐØØô	€Iô —’Ü˜	 'Õ*¨F°JÓ?ØÐØØô	€Ið ˜t•_ i°¥oÐ5Ð5r"   c               ó    € V ^8„  d   QhRRRR/# )r   Úinput_cr   r   r   )r   s   "r    r!   r!   ½  s   € ÷ ñ  ð °ñ r"   c                óº   € \         P                  ! V R,          V P                  R,          ^,          V P                  R,          ^,          3RRR7      pVR,          # )zçPerform chroma upsampling.

Args:
    input_c (torch.Tensor): Cb or Cr component to be upsampled of the shape ``(B, H, W)``.

Returns:
    output_c (torch.Tensor): Upsampled C(b or r) component of the shape ``(B, H * 2, W * 2)``.

rð   Frñ   r¡   rÊ   r£   r²   )rö   r÷   rŠ   )rÿ   Úoutput_cs   & r    Ú_chroma_upsamplingr  ½  sP   € ô Ÿ]š]Ø�ÕØ�m‰m˜BÕ !Õ# W§]¡]°2Õ%6¸Õ%:Ð;ØØô	€Hð �D�>Ðr"   c          
     ó,   € V ^8„  d   QhRRRRRRRRRR/# )r   Ú	image_rgbr   rá   Úquantization_table_yÚquantization_table_cr   r~   r   )r   s   "r    r!   r!   Ò  s:   € ÷ *$ñ *$Øð*$àð*$ð 'ð*$ð 'ð	*$ð
 'ñ*$r"   c                óØ   € \        V 4      pRV,          p\        V4      w  rVp\        V4      p\        Wg4      p\	        V4      p	\	        V4      p
\        V	VV4      p\        V
VV4      pW¼3# )aå  Perform JPEG encoding.

Args:
    image_rgb (torch.Tensor): RGB input images of the shape ``(B, 3, H, W)``.
    jpeg_quality (torch.Tensor): Compression strength of the shape ``(B)``.
    quantization_table_y (torch.Tensor): Quantization table for Y channel.
    quantization_table_c (torch.Tensor): Quantization table for C channels.

Returns:
    y_encoded (torch.Tensor): Encoded Y component of the shape ``(B, N, 8, 8)``.
    cb_cr_encoded (torch.Tensor): Encoded Cb component followed by the encoded Cr component, of the shape
        ``(B, 2N_c, 8, 8)``. They stay together between encoding and decoding: a ``(B, N_c, 8, 8)`` half of them
        can have a single patch, which ``torch.export`` cannot trace for a dynamic image size.

ç     ào@)r   rý   r‘   r¯   rÂ   ré   )r  rá   r  r  Úimage_ycbcrÚinput_yr™   rš   Úinput_cb_crÚdct_yÚ	dct_cb_crÚ	y_encodedÚcb_cr_encodeds   &&&&         r    Ú_jpeg_encoder  Ò  s‚   € ô, !-¨YÓ 7€Kà˜+Õ%€Kä"5°kÓ"BÑ€G�xä˜GÓ$€GÜ" 8Ó6€KÜ�WÓ€EÜ˜Ó%€IÜ'ØØØó€Iô
 #,ØØØó#€Mð
 Ð#Ð#r"   c               ó8   € V ^8„  d   QhRRRRRRRRRRRRR	RR
R/# )r   r
  r   r  rá   r�   r“   r�   r  r  r   r   )r   s   "r    r!   r!   ÿ  sX   € ÷ 1ñ 1Øð1àð1ð ð1ð ð	1ð
 ð1ð 'ð1ð 'ð1ð ñ1r"   c                ó:  € \        V VV4      p \        VVV4      p\        V 4      p\        V4      p\        WsV4      p	\        Wƒ^,          V^,          4      w  r«\	        V
4      p
\	        V4      p\
        P                  ! WšV3^R7      R,          p\        V4      pV# )a†  Perform JPEG decoding.

Args:
    input_y (torch.Tensor): Compressed Y component of the shape ``(B, N, 8, 8)``.
    input_cb_cr (torch.Tensor): Compressed Cb component followed by the compressed Cr component, of the shape
        ``(B, 2N_c, 8, 8)``.
    jpeg_quality (torch.Tensor): Compression strength of the shape ``(B)``.
    H (int): Original image height.
    W (int): Original image width.
    quantization_table_y (torch.Tensor): Quantization table for Y channel.
    quantization_table_c (torch.Tensor): Quantization table for C channels.

Returns:
    rgb_decoded (torch.Tensor): Decompressed RGB image of the shape ``(B, 3, H, W)``.

rœ   r  )rì   r×   r—   r¹   r  r6   Ústackr   )r
  r  rá   r�   r�   r  r  Úidct_yÚ
idct_cb_crÚimage_yÚimage_cbÚimage_crr	  Úrgb_decodeds   &&&&&&&       r    Ú_jpeg_decoder  ÿ  s¤   € ô4 ØØØó€Gô
 ØØØó€Kô % WÓ-€FÜ(¨Ó5€Jä+¨F°qÓ9€GÜ+¨J¸Q½ÀÀQÅÓGÑ€Hä! (Ó+€HÜ! (Ó+€Hä %§¢¨WÀÐ,IÈqÔ QÐTYÕ Y€Kä ,¨[Ó 9€KØÐr"   c               ó    € V ^8„  d   QhRRRR/# )r   Úimager   r   ztuple[torch.Tensor, int, int]r   )r   s   "r    r!   r!   3  s   € ÷ &ñ &˜Lð &Ð-Jñ &r"   c                óÜ   € V P                   RR w  rV^,           ^,          ^,          V,
          pV^,           ^,          ^,          V,
          p\        P                  ! V ^ V^ V3R4      pWSV3# )a  Pad a given image to be dividable by 16.

Args:
    image: Image of the shape ``(*, 3, H, W)``.

Returns:
    image_padded: Padded image of the shape :math:`(*, 3, H_{new}, W_{new})`.
    h_pad: Padded pixels along the horizontal axis.
    w_pad: Padded pixels along the vertical axis.

NÚ	replicaterÊ   ©rŠ   rö   Úpad)r  r�   r�   Úh_padÚw_padÚimage_paddeds   &     r    Ú_perform_paddingr$  3  sl   € ð �;‰;�r�sÐ�D€Að �b•&˜R• "Õ$ qÕ(€EØ�b•&˜R• "Õ$ qÕ(€Eä!"§¢ u¨q°%¸¸EÐ.BÀKÓ!P€LØ Ð%Ð%r"   c               ó(   € V ^8„  d   QhRRRRRRRR/# )r   r  r   r�   r“   r�   r   r   )r   s   "r    r!   r!   J  s(   € ÷ 0ñ 0˜ð 0¨#ð 0°#ð 0¸,ñ 0r"   c                ó¢   € V P                   R,          V,
          V P                   R,          V,
          rC\        P                  ! V ^ V) ^ V) 34      # )ao  Crop the bottom and right padding off an image padded to a multiple of 16.

``cropped = _crop_padding(image, H, W)`` takes an image of the shape ``(*, C, H_new, W_new)`` that
``_perform_padding`` padded from ``H`` x ``W``, and returns its top-left ``(*, C, H, W)`` part, as a copy.

Args:
    image: Padded image of the shape ``(*, C, H_new, W_new)``.
    H: height of the image before the padding.
    W: width of the image before the padding.

Returns:
    The top-left part of ``image``, of the shape ``(*, C, H, W)``.

See Also:
    - :func:`jpeg_codec_differentiable`: the public codec, whose last step is this crop.

rÊ   r£   r  )r  r�   r�   r!  r"  s   &&&  r    Ú_crop_paddingr'  J  sC   € ð( —;‘;˜r•? QÕ&¨¯©°B­¸!Õ(;ˆ5Ü�5Š5�˜˜U˜F A¨ vÐ.Ó/Ð/r"   c          
     ó,   € V ^8„  d   QhRRRRRRRRRR/# )r   r   r   rá   r  ztorch.Tensor | Noner  r   r   )r   s   "r    r!   r!   c  sH   € ÷ dCñ dCØðdCàðdCð .ðdCð .ð	dCð
 ñdCr"   c                ó   a€ \        V R4       \        V 4       \        S4       V P                  pV P                  pV\        P
                  \        P                  39   d   \        P                  MTpV P                  V4      p Ve   Vf   \        WV4      w  rxVf   TMTpVf   TMTp\        V4       \        V4       \        V . RO4       \        SR.4       VP                  ^8X  d   VP                  ^ R7      pVP                  ^8X  d   VP                  ^ R7      p\        V. RO4       \        V. RO4       \        4       '       gt   \        SP                  4       ^ 8H  ;'       gK    SP!                  4       P#                  4       R8¬  ;'       d"    SP%                  4       P#                  4       R8*  V3R l4       V P&                  RR w  rš\)        V 4      w  p  pV P&                  RR w  rÍ\+        VV RR	R
7       \+        VV RRR
7       \+        SV RRR
7       \-        SWV4      o\-        W%V4      p\-        W5V4      p\/        V P                  P0                  4      ;_uu_ 4        \3        V SVVR7      w  rï\5        VVSVVVVR7      p\7        VRRR7      pRRR4       \9        XWš4      P                  V4      #   + '       g   i     L+; i)av  Compress and decompress an RGB image with a differentiable JPEG model.

``out = jpeg_codec_differentiable(input, jpeg_quality)`` returns an RGB image in :math:`[0, 1]` after a JPEG encode
and decode at ``jpeg_quality``, following :cite:`reich2024` and :cite:`shin2017`. Gradients reach ``input``,
``jpeg_quality`` and the quantization tables.

.. image:: _static/img/jpeg_codec_differentiable.png

Convention:
    - Input and output pixels are in :math:`[0, 1]`: denormalize a normalized image first and normalize the output
      again. Any ``H`` and ``W`` work: the codec pads the bottom and right to a multiple of 16 and crops back.
    - The decoded image is clamped to :math:`[0, 1]` by :func:`~kornia.color.ycbcr_to_rgb`, so a pixel the codec
      pushes past either end passes no gradient.
    - The result approximates libjpeg and does not reproduce its output exactly.
    - float16 and bfloat16 inputs are computed in float32, quality and tables included, and the output is rounded
      once to the input dtype.
    - The codec runs with autocast disabled, so inside a ``torch.autocast`` region it gives the same output as
      outside it, and the same gradients when the backward pass runs after the region (on CUDA, up to the run-to-run
      spread of its atomic accumulation unless deterministic algorithms are enabled). A backward pass inside the
      region, which ``torch.compile`` assumes by default, still computes part of the gradient in the autocast dtype.

Args:
    input: RGB image with shape ``(*, 3, H, W)``.
    jpeg_quality: JPEG quality in :math:`[0, 100]`, with shape ``(1,)`` for all images or ``(N,)`` for ``N`` the
        number of flattened images; a 0-d tensor raises. A quality of exactly 0 is treated as 1, as in libjpeg; a
        quality between 0 and 1 is left alone and compresses harder still.
    quantization_table_y: luma quantization table with shape ``(8, 8)`` for all images or ``(N, 8, 8)``. The default
        is the standard table.
    quantization_table_c: chroma quantization table, in the same shapes. The default is the standard table.

Returns:
    JPEG-coded image with the shape of ``input``.

.. note::
   The model produces no JPEG byte file and cannot read one. To load JPEG files use ``kornia.io``.

See Also:
    - :class:`JPEGCodecDifferentiable`: the module form, which keeps the tables.
    - :class:`~kornia.augmentation.RandomJPEG`: applies JPEG compression at random quality per image.

Example:
    To perform JPEG coding with the standard quantization tables just provide a JPEG quality

    >>> img = torch.rand(3, 3, 64, 64, requires_grad=True, dtype=torch.float)
    >>> jpeg_quality = torch.tensor((99.0, 25.0, 1.0), requires_grad=True)
    >>> img_jpeg = jpeg_codec_differentiable(img, jpeg_quality)
    >>> img_jpeg.sum().backward()

    You also have the option to provide custom quantization tables

    >>> img = torch.rand(3, 3, 64, 64, requires_grad=True, dtype=torch.float)
    >>> jpeg_quality = torch.tensor((99.0, 25.0, 1.0), requires_grad=True)
    >>> quantization_table_y = torch.randint(1, 256, size=(3, 8, 8), dtype=torch.float)
    >>> quantization_table_c = torch.randint(1, 256, size=(3, 8, 8), dtype=torch.float)
    >>> img_jpeg = jpeg_codec_differentiable(img, jpeg_quality, quantization_table_y, quantization_table_c)
    >>> img_jpeg.sum().backward()

    To control the quantization purely with the quantization tables use a JPEG quality of 99.5, which scales the
    tables by 1 (Eq. 2 of :cite:`reich2024`).

    >>> img = torch.rand(3, 3, 64, 64, requires_grad=True, dtype=torch.float)
    >>> jpeg_quality = torch.ones(3) * 99.5
    >>> quantization_table_y = torch.randint(1, 256, size=(3, 8, 8), dtype=torch.float)
    >>> quantization_table_c = torch.randint(1, 256, size=(3, 8, 8), dtype=torch.float)
    >>> img_jpeg = jpeg_codec_differentiable(img, jpeg_quality, quantization_table_y, quantization_table_c)
    >>> img_jpeg.sum().backward()

r   NrŽ   rœ   rÛ   rå   c                 ó†   <€ R S P                  4       P                  4        RS P                  4       P                  4        R2# )z?JPEG quality is out of range. Expected range is [0, 100], got [z, z"]. Consider clipping jpeg_quality.)ÚaminÚitemÚamax)rá   s   €r    Ú<lambda>Ú+jpeg_codec_differentiable.<locals>.<lambda>Ñ  sB   ø€ ðØ$×)Ñ)Ó+×0Ñ0Ó2Ð3°2°l×6GÑ6GÓ6I×6NÑ6NÓ6PÐ5QÐQsñur"   Tz?quantization_table_y needs one table per image, or one for all.)Ú	allow_oneÚmsgz?quantization_table_c needs one table per image, or one for all.z9jpeg_quality needs one quality per image, or one for all.)r  rá   r  r  )r
  r  rá   r�   r�   r  r  r  )r   r.   r/   )Ú*Ú3r�   r�   )rŽ   Ú8r4  rÊ   )r   r	   rB   r@   r6   Úfloat16Úbfloat16Úfloat32Útor‡   r   ÚndimrË   r   r   Únumelr+  r,  r-  rŠ   r$  r
   r   r   Útyper  r  r=   r'  )r   rá   r  r  Ú	out_dtyper@   rB   Ú	default_yÚ	default_cÚH_inÚW_inÚ_r�   r�   r  r  Úimage_rgb_jpegs   &f&&             r    r   r   b  s²  ø€ ôV ˜%Ð!<Ô=ä˜5Ô!Ü˜<Ô(à"Ÿ[™[€IØ-2¯\©\€Fð +4¼¿¹ÄuÇ~Á~Ð7VÔ*VœŸšÐ\e€EØ�H‰H�U‹O€EàÒ#Ð';Ò'CÜ;¸FÓJÑˆ	Ø,@Ò,H™yÐNbÐØ,@Ò,H™yÐNbÐÜÐ/Ô0ÜÐ/Ô0ä�uÒ2Ô3Ü�| c UÔ+à× Ñ  AÔ%Ø3×=Ñ=À!Ð=ÓDÐØ× Ñ  AÔ%Ø3×=Ñ=À!Ð=ÓDÐäÐ+ª_Ô=ÜÐ+ª_Ô=ô �>Š>ÜØ×ÑÓ  AÑ%÷ _ð _Ø×"Ñ"Ó$×)Ñ)Ó+¨sÑ2×]Ð]¸×9JÑ9JÓ9L×9QÑ9QÓ9SÐW\Ñ9\ôô	
ð —‘˜R˜SÐ!�J€DÜ" 5Ó)�K€Eˆ1ˆaà�;‰;�r�sÐ�D€AäØØØØMõ	ô ØØØØMõ	ô Ø�e tÐ1lõô *¨,¸ÓF€Lä1Ð2FÐPUÓVÐÜ1Ð2FÐPUÓVÐô 
˜EŸL™L×-Ñ-×	.Õ	.ä#/ØØ%Ø!5Ø!5ô	$
Ñ ˆ	ô (4ØØ%Ø%ØØØ!5Ø!5ô(
ˆô 2¸ÐPSÐ]bÔcˆ÷% 
/ô( ˜¨Ó4×7Ñ7¸	ÓBÐB÷) 
/×	.ús   É(2J=Ê=K	c               ó$   € V ^8„  d   QhRRRRRR/# ©r   rB   rC   r@   rA   r   r~   r   )r   s   "r    r!   r!   
  s$   € ÷ ñ Ø#ðØ-Kðà&ñr"   c                ó¸  € \        4       '       d   \        W4      # W3pV\        9  d“   \        P                  ! R 4      ;_uu_ 4        \        W4      pRRR4       X^ ,          P
                  '       d?   \        P                  P                  V^ ,          P                  4      P                  4        V\        V&   \        V,          #   + '       g   i     L}; i)FN)
r   Ú_build_dct8_basis_scaler   r6   r€   r�   r‚   rƒ   r@   r„   )rB   r@   r…   rÔ   s   &&  r    r¾   r¾   
  s˜   € ô ‡~‚~Ü& uÓ5Ð5Øˆ/€CØ
”+Ôô ×!Ò! %×(Õ(Ü+¨EÓ:ˆE÷ )à��8××Ðä�J‰J×%Ñ% e¨A¥h§o¡oÓ6×BÑBÔDØ Œ�CÑÜ�sÕÐ÷ )×(ús   ÁC	Ã	C	c               ó$   € V ^8„  d   QhRRRRRR/# rD  r   )r   s   "r    r!   r!     s$   € ÷ 
!ñ 
!Ø#ð
!Ø-Kð
!à&ñ
!r"   c                óˆ  € \         P                  ! ^WR7      pRV,          R,           R,          VR,          ,          \        P                  R,          ,          p\         P                  ! V4      pVR,          VR	,          ,          p\         P
                  ! ^WR7      pR
V^ &   \         P                  ! Wf4      R,          pWW3# )é   rÅ   rÆ   r4   rÇ   rÈ   r¡   )Nr¢   )r¢   Nr¢   N)Nr¢   Nr¢   rÉ   )r6   r¥   rÍ   rÎ   rÌ   ÚonesrÏ   )rB   r@   ÚiÚfreqÚbasis_1drÀ   rÕ   rÁ   s   &&      r    rF  rF    s”   € ô 	�Š�Q˜eÔ3€AØ�!�G�c�M˜7Õ# a¨¥jÕ0´D·G±G¸dµNÕC€DÜ�yŠy˜‹€HØÐ*Õ+¨hÐ7GÕ.HÕH€JÜ�JŠJ�q Ô5€EØ€Eˆ!�HÜ—’˜EÓ)¨DÕ0€IØÐ Ð r"   c                  óB   a € ] tR tRtRtRR V 3R llltR R ltRtV ;t# )	r   i,  a'
  Compress and decompress an RGB image with a differentiable JPEG model (module form).

``op = JPEGCodecDifferentiable(quantization_table_y, quantization_table_c)`` stores the tables, and
``out = op(image_rgb, jpeg_quality)`` is :func:`jpeg_codec_differentiable` with them. A table is stored as a buffer,
or as a learnable parameter if it is an ``nn.Parameter``.

.. image:: _static/img/jpeg_codec_differentiable.png

Convention:
    - See the convention block of :func:`jpeg_codec_differentiable`.

Args:
    quantization_table_y: luma quantization table with shape ``(8, 8)`` for all images or ``(N, 8, 8)``. The default
        is the standard table.
    quantization_table_c: chroma quantization table, in the same shapes. The default is the standard table.

Shape:
    - image_rgb: ``(*, 3, H, W)``.
    - jpeg_quality: ``(1,)`` or ``(N,)`` for ``N`` the number of flattened images.
    - Output: same shape as ``image_rgb``.

See Also:
    - :func:`jpeg_codec_differentiable`: the one-call form.

Example:
    You can use the differentiable JPEG module with standard quantization tables by

    >>> diff_jpeg_module = JPEGCodecDifferentiable()
    >>> img = torch.rand(2, 3, 32, 32, requires_grad=True, dtype=torch.float)
    >>> jpeg_quality = torch.tensor((99.0, 1.0), requires_grad=True)
    >>> img_jpeg = diff_jpeg_module(img, jpeg_quality)
    >>> img_jpeg.sum().backward()

    You can also specify custom quantization tables to be used by

    >>> quantization_table_y = torch.randint(1, 256, size=(2, 8, 8), dtype=torch.float)
    >>> quantization_table_c = torch.randint(1, 256, size=(2, 8, 8), dtype=torch.float)
    >>> diff_jpeg_module = JPEGCodecDifferentiable(quantization_table_y, quantization_table_c)
    >>> img = torch.rand(2, 3, 32, 32, requires_grad=True, dtype=torch.float)
    >>> jpeg_quality = torch.tensor((99.0, 1.0), requires_grad=True)
    >>> img_jpeg = diff_jpeg_module(img, jpeg_quality)
    >>> img_jpeg.sum().backward()

    In case you want to learn the quantization tables just pass parameters `nn.Parameter`

    >>> quantization_table_y = torch.nn.Parameter(torch.randint(1, 256, size=(2, 8, 8), dtype=torch.float))
    >>> quantization_table_c = torch.nn.Parameter(torch.randint(1, 256, size=(2, 8, 8), dtype=torch.float))
    >>> diff_jpeg_module = JPEGCodecDifferentiable(quantization_table_y, quantization_table_c)
    >>> img = torch.rand(2, 3, 32, 32, requires_grad=True, dtype=torch.float)
    >>> jpeg_quality = torch.tensor((99.0, 1.0), requires_grad=True)
    >>> img_jpeg = diff_jpeg_module(img, jpeg_quality)
    >>> img_jpeg.sum().backward()

c               ó$   € V ^8„  d   QhRRRRRR/# )r   r  z"torch.Tensor | nn.Parameter | Noner  r   ÚNoner   )r   s   "r    r!   Ú$JPEGCodecDifferentiable.__annotate__d  s.   € ÷ Oñ Oà@ðOð AðOð 
ñ	Or"   c                	ó‚  <€ \         SV `  4        Vf   \        R R 4      MTpVf   \        R R 4      MTp\	        V\
        P                  4      '       d   V P                  RV4       MV P                  RV4       \	        V\
        P                  4      '       d   V P                  RV4       R # V P                  RV4       R # )Nr  r  )	ÚsuperÚ__init__rv   r|   Ú
isinstancer   Ú	ParameterÚregister_parameterÚregister_buffer)Úselfr  r  Ú	__class__s   &&&€r    rT  Ú JPEGCodecDifferentiable.__init__d  s¨   ø€ ô
 	‰ÑÔà@TÒ@\Ô0°°tÔ<ÐbvÐØ@TÒ@\Ô0°°tÔ<ÐbvÐÜÐ*¬B¯L©L×9Ò9Ø×#Ñ#Ð$:Ð<PÕQà× Ñ Ð!7Ð9MÔNÜÐ*¬B¯L©L×9Ò9Ø×#Ñ#Ð$:Ð<PÖQà× Ñ Ð!7Ð9MÖNr"   c               ó$   € V ^8„  d   QhRRRRRR/# )r   r  r   rá   r   r   )r   s   "r    r!   rQ  v  s(   € ÷ ñ àðð #ðð 
ñ	r"   c                óL   € \        VVV P                  V P                  R7      pV# )aT  Apply differentiable JPEG compression and decompression.

The method runs an RGB image through the internal JPEG codec pipeline
using the module's quantization tables, then reconstructs an RGB tensor.
Quantization tables are moved to the input device and to the dtype
the codec computes in before encoding/decoding.

Args:
    image_rgb: Input RGB tensor with shape ``(*, 3, H, W)``.
    jpeg_quality: JPEG quality factor with shape (1,) or (N,), N being the number of images.

Returns:
    Reconstructed RGB tensor after differentiable JPEG processing, with
    the same shape as ``image_rgb``.
)rá   r  r  )r   r  r  )rY  r  rá   rB  s   &&& r    ÚforwardÚJPEGCodecDifferentiable.forwardv  s/   € ô. (AØØ%Ø!%×!:Ñ!:Ø!%×!:Ñ!:ô	(
ˆð Ðr"   r   r½   )	Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__rT  r^  Ú__static_attributes__Ú__classcell__)rZ  s   @r    r   r   ,  s   ø† ñ5÷nOò O÷$ó r"   )NNg{®Gáz”?r½   )=Ú__conditional_annotations__Ú
__future__r   rÍ   Útypingr   r6   Útorch.nn.functionalr   Ú
functionalrö   Úkornia.colorr   r   Úkornia.core.checkr   r	   r
   r   Úkornia.core.opsr   Úkornia.core.utilsr   r   r   Úkornia.enhance.adjustr   Úkornia.filtersr   Úkornia.image.utilsr   r   Ú__annotations__r   Ú__all__r&   r,   r=   rv   r|   r‡   r‘   r—   r¯   r¹   rÂ   r×   rÞ   ré   rì   rý   r  r  r  r$  r'  r   r¾   rF  ÚModuler   )rg  s   @r    Ú<module>rv     s  øð÷$ #Ð "ã Ý ã ß Ð Ý ç 3ß nÓ nÝ +ß VÑ VÝ 7Ý *Ý 7ð ð ð ó ð ð ð ó ð %Ð&AÐ
B€õ4õ:÷õ<õ&õ&"õ,õ"õ& GõF&õBõ&!õ<õD7õ:õ6!6õHõ**$õZ1õh&õ.0ð0 ödCó ðdCõNõ*
!ôg˜bŸi™iö gr"   