
    hK                        S r SSKJr  SSKrSSKrSSKrSSKJrJr  SSK	J
r
  SSKJrJrJrJr   " S S5      rSS	 jr " S
 S5      rg)zProvides objects that can characterize image streams.

That characterization is as to content type and size, as a required step in including
them in a document.
    )annotationsN)IOTuple)UnrecognizedImageError)EmuInchesLengthlazypropertyc                  f  ^  \ rS rSrSrSU 4S jjr\SS j5       r\SS j5       r\	S 5       r
\	SS j5       r\S 5       r\	S	 5       r\	SS
 j5       r\	SS j5       r\	SS j5       r\	SS j5       r\	SS j5       r\	SS j5       r S     SS jjr\S 5       r\ S       SS jj5       rSrU =r$ )Image   zcGraphical image stream such as JPEG, PNG, or GIF with properties and methods
required by ImagePart.c                N   > [         [        U ]  5         Xl        X l        X0l        g N)superr   __init___blob	_filename_image_header)selfblobfilenameimage_header	__class__s       B/var/www/html/env/lib/python3.13/site-packages/docx/image/image.pyr   Image.__init__   s     eT#%
!)    c                P    [         R                  " U5      nU R                  X!5      $ )zXReturn a new |Image| subclass instance parsed from the image binary contained
in `blob`.)ioBytesIO_from_stream)clsr   streams      r   	from_blobImage.from_blob   s#     D!--r   c                x   [        U[        5      (       a]  Un[        US5       nUR                  5       n[        R
                  " U5      nSSS5        [        R                  R                  U5      nO%UnUR                  S5        UR                  5       nSnU R                  WWU5      $ ! , (       d  f       Nf= f)zReturn a new |Image| subclass instance loaded from the image file identified
by `image_descriptor`, a path or file-like object.rbNr   )
isinstancestropenreadr   r   ospathbasenameseekr    )r!   image_descriptorr,   fr   r"   r   s          r   	from_fileImage.from_file#   s     &,,#DdD!QvvxD) " ww''-H%FKKN;;=DHh77 "!s   'B++
B9c                    U R                   $ )zThe bytes of the image 'file'.)r   r   s    r   r   
Image.blob4   s     zzr   c                .    U R                   R                  $ )zIMIME content type for this image, e.g. ``'image/jpeg'`` for a JPEG image.)r   content_typer4   s    r   r7   Image.content_type9   s     !!...r   c                `    [         R                  R                  U R                  5      S   SS $ )zThe file extension for the image.

If an actual one is available from a load filename it is used. Otherwise a
canonical extension is assigned based on the content type. Does not contain the
leading period, e.g. 'jpg', not '.jpg'.
   N)r+   r,   splitextr   r4   s    r   ext	Image.ext>   s)     ww/21266r   c                    U R                   $ )zhOriginal image file name, if loaded from disk, or a generic filename if
loaded from an anonymous stream.)r   r4   s    r   r   Image.filenameH   s     ~~r   c                .    U R                   R                  $ z,The horizontal pixel dimension of the image.)r   px_widthr4   s    r   rB   Image.px_widthN   s     !!***r   c                .    U R                   R                  $ z*The vertical pixel dimension of the image.)r   	px_heightr4   s    r   rF   Image.px_heightS   s     !!+++r   c                .    U R                   R                  $ zwInteger dots per inch for the width of this image.

Defaults to 72 when not present in the file, as is often the case.
)r   horz_dpir4   s    r   rJ   Image.horz_dpiX        !!***r   c                .    U R                   R                  $ zxInteger dots per inch for the height of this image.

Defaults to 72 when not present in the file, as is often the case.
)r   vert_dpir4   s    r   rO   Image.vert_dpi`   rL   r   c                F    [        U R                  U R                  -  5      $ )zuA |Length| value representing the native width of the image, calculated from
the values of `px_width` and `horz_dpi`.)r   rB   rJ   r4   s    r   widthImage.widthh   s     dmmdmm344r   c                F    [        U R                  U R                  -  5      $ )zwA |Length| value representing the native height of the image, calculated from
the values of `px_height` and `vert_dpi`.)r   rF   rO   r4   s    r   heightImage.heightn   s     dnnt}}455r   c                d   Uc  Uc  U R                   U R                  4$ Uc>  Uc   e[        U5      [        U R                  5      -  n[        U R                   U-  5      nUc9  [        U5      [        U R                   5      -  n[        U R                  U-  5      n[	        U5      [	        U5      4$ )a  (cx, cy) pair representing scaled dimensions of this image.

The native dimensions of the image are scaled by applying the following rules to
the `width` and `height` arguments.

* If both `width` and `height` are specified, the return value is (`width`,
`height`); no scaling is performed.
* If only one is specified, it is used to compute a scaling factor that is then
applied to the unspecified dimension, preserving the aspect ratio of the image.
* If both `width` and `height` are |None|, the native dimensions are returned.

The native dimensions are calculated using the dots-per-inch (dpi) value
embedded in the image, defaulting to 72 dpi if no value is specified, as is
often the case. The returned values are both |Length| objects.
)rR   rU   floatroundr   )r   rR   rU   scaling_factors       r   scaled_dimensionsImage.scaled_dimensionst   s    $ =V^::t{{**=%%%"6]U4;;-??N$**~56E>"5\E$**,==N4;;78F5z3v;&&r   c                ^    [         R                  " U R                  5      R                  5       $ )z#SHA1 hash digest of the image blob.)hashlibsha1r   	hexdigestr4   s    r   r_   
Image.sha1   s      ||DJJ'1133r   c                N    [        U5      nUc  SUR                  -  nU " X#U5      $ )z`Return an instance of the |Image| subclass corresponding to the format of the
image in `stream`.zimage.%s)_ImageHeaderFactorydefault_ext)r!   r"   r   r   r   s        r   r    Image._from_stream   s1     +62!L$<$<<H4<00r   )r   r   r   )r   bytesr   r(   r   BaseImageHeader)r   rf   returnr   )r/   zstr | IO[bytes]rh   r(   )rh   int)rh   r   )NN)rR   int | Length | NonerU   rk   rh   zTuple[Length, Length]r   )r"   	IO[bytes]r   rf   r   z
str | Nonerh   r   )__name__
__module____qualname____firstlineno____doc__r   classmethodr#   r1   propertyr   r7   r
   r<   r   rB   rF   rJ   rO   rR   rU   r[   r_   r    __static_attributes____classcell__)r   s   @r   r   r      su   * . . 8 8    / / 7 7  
 + + , , + + + + 5 5
 6 6 PT'('9L'	'@ 4 4 
  $	11 1 	1
 
1 1r   r   c                    SSK Jn  SS jnU" U 5      nU H0  u  pEnU[        U5      -   nX5U nX:X  d  M  UR                  U 5      s  $    [        e)zRA |BaseImageHeader| subclass instance that can parse headers of image in `stream`.r   )
SIGNATURESc                F    U R                  S5        U R                  S5      $ )Nr       )r.   r*   )r"   s    r   read_32$_ImageHeaderFactory.<locals>.read_32   s    A{{2r   r"   rl   )
docx.imagerw   lenfrom_streamr   )	r"   rw   rz   headerr!   offsetsignature_bytesendfound_bytess	            r   rc   rc      sZ    % V_F(2$_s?++C()??6**	 )3
 ! r   c                      \ rS rSrSrSS jr\SS j5       r\SS j5       r\S 5       r	\S 5       r
\S 5       r\S	 5       rS
rg)rg      z>Base class for image header subclasses like |Jpeg| and |Tiff|.c                4    Xl         X l        X0l        X@l        g r   )	_px_width
_px_height	_horz_dpi	_vert_dpi)r   rB   rF   rJ   rO   s        r   r   BaseImageHeader.__init__   s    !#!!r   c                    Sn[        U5      e)zDAbstract property definition, must be implemented by all subclasses.zNcontent_type property must be implemented by all subclasses of BaseImageHeaderNotImplementedError)r   msgs     r   r7   BaseImageHeader.content_type   s     b!#&&r   c                    [        S5      e)z}Default filename extension for images of this type.

An abstract property definition, must be implemented by all subclasses.
zMdefault_ext property must be implemented by all subclasses of BaseImageHeaderr   r4   s    r   rd   BaseImageHeader.default_ext   s     "^
 	
r   c                    U R                   $ rA   )r   r4   s    r   rB   BaseImageHeader.px_width   s     ~~r   c                    U R                   $ rE   )r   r4   s    r   rF   BaseImageHeader.px_height   s     r   c                    U R                   $ rI   )r   r4   s    r   rJ   BaseImageHeader.horz_dpi        ~~r   c                    U R                   $ rN   )r   r4   s    r   rO   BaseImageHeader.vert_dpi   r   r   )r   r   r   r   N)rB   rj   rF   rj   rJ   rj   rO   rj   ri   )rm   rn   ro   rp   rq   r   rs   r7   rd   rB   rF   rJ   rO   rt    r   r   rg   rg      s    H" ' '
 
 
        r   rg   r|   )rq   
__future__r   r^   r   r+   typingr   r   docx.image.exceptionsr   docx.sharedr   r   r	   r
   r   rc   rg   r   r   r   <module>r      s@    #  	 	  8 9 9S1 S1l!"1 1r   