o
    ýÞhE  ã                   @  s†   d Z ddlmZ ddlZddlZddlZddlmZmZ ddl	m
Z
 ddlmZmZmZmZ G dd„ dƒZddd„ZG dd„ dƒZdS )z¡Provides 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                      sð   e Zd ZdZd5‡ fdd	„Zed6dd„ƒZed7dd„ƒZedd„ ƒZ	ed8dd„ƒZ
edd„ ƒZedd„ ƒZed9dd„ƒZed9dd„ƒZed9dd„ƒZed9d d!„ƒZed:d#d$„ƒZed:d%d&„ƒZ	'd;d<d,d-„Zed.d/„ ƒZe	'd=d>d3d4„ƒZ‡  ZS )?ÚImagezgGraphical image stream such as JPEG, PNG, or GIF with properties and methods
    required by ImagePart.ÚblobÚbytesÚfilenameÚstrÚimage_headerÚBaseImageHeaderc                   s$   t t| ƒ ¡  || _|| _|| _d S ©N)Úsuperr
   Ú__init__Ú_blobÚ	_filenameÚ_image_header)Úselfr   r   r   ©Ú	__class__© úP/var/www/html/premium_crap/venv/lib/python3.10/site-packages/docx/image/image.pyr      s   
zImage.__init__Úreturnc                 C  s   t  |¡}|  ||¡S )z`Return a new |Image| subclass instance parsed from the image binary contained
        in `blob`.)ÚioÚBytesIOÚ_from_stream)Úclsr   Ústreamr   r   r   Ú	from_blob   s   
zImage.from_blobÚimage_descriptorústr | IO[bytes]c                 C  s€   t |tƒr,|}t|dƒ�}| ¡ }t |¡}W d  ƒ n1 s w   Y  tj |¡}n|}| 	d¡ | ¡ }d}|  
|||¡S )z‡Return a new |Image| subclass instance loaded from the image file identified
        by `image_descriptor`, a path or file-like object.ÚrbNr   )Ú
isinstancer   ÚopenÚreadr   r   ÚosÚpathÚbasenameÚseekr   )r    r#   r*   Úfr   r!   r   r   r   r   Ú	from_file#   s   
þ
zImage.from_filec                 C  ó   | j S )zThe bytes of the image 'file'.)r   ©r   r   r   r   r   4   ó   z
Image.blobc                 C  ó   | j jS )zIMIME content type for this image, e.g. ``'image/jpeg'`` for a JPEG image.)r   Úcontent_typer0   r   r   r   r3   9   ó   zImage.content_typec                 C  s   t j | j¡d dd… S )a  The 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   r0   r   r   r   Úext>   s   z	Image.extc                 C  r/   )zpOriginal image file name, if loaded from disk, or a generic filename if
        loaded from an anonymous stream.)r   r0   r   r   r   r   H   s   zImage.filenameÚintc                 C  r2   ©z,The horizontal pixel dimension of the image.)r   Úpx_widthr0   r   r   r   r:   N   r4   zImage.px_widthc                 C  r2   ©z*The vertical pixel dimension of the image.)r   Ú	px_heightr0   r   r   r   r<   S   r4   zImage.px_heightc                 C  r2   ©z‡Integer 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_dpir0   r   r   r   r>   X   ó   zImage.horz_dpic                 C  r2   ©zˆInteger 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_dpir0   r   r   r   rA   `   r?   zImage.vert_dpir   c                 C  ó   t | j| j ƒS )z}A |Length| value representing the native width of the image, calculated from
        the values of `px_width` and `horz_dpi`.)r   r:   r>   r0   r   r   r   Úwidthh   ó   zImage.widthc                 C  rB   )zA |Length| value representing the native height of the image, calculated from
        the values of `px_height` and `vert_dpi`.)r   r<   rA   r0   r   r   r   Úheightn   rD   zImage.heightNrC   úint | Length | NonerE   úTuple[Length, Length]c                 C  sˆ   |du r|du r| j | jfS |du r(|dusJ ‚t|ƒt| jƒ }t| j | ƒ}|du r<t|ƒt| j ƒ }t| j| ƒ}t|ƒt|ƒfS )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.
        N)rC   rE   ÚfloatÚroundr   )r   rC   rE   Úscaling_factorr   r   r   Úscaled_dimensionst   s   zImage.scaled_dimensionsc                 C  s   t  | j¡ ¡ S )z#SHA1 hash digest of the image blob.)ÚhashlibÚsha1r   Ú	hexdigestr0   r   r   r   rM   ”   s   z
Image.sha1r!   ú	IO[bytes]ú
str | Nonec                 C  s&   t |ƒ}|du rd|j }| |||ƒS )zhReturn an instance of the |Image| subclass corresponding to the format of the
        image in `stream`.Nzimage.%s)Ú_ImageHeaderFactoryÚdefault_ext)r    r!   r   r   r   r   r   r   r   ™   s   	
zImage._from_stream)r   r   r   r   r   r   )r   r   r   r
   )r#   r$   ©r   r   )r   r8   )r   r   )NN)rC   rF   rE   rF   r   rG   r   )r!   rO   r   r   r   rP   r   r
   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úclassmethodr"   r.   Úpropertyr   r3   r	   r7   r   r:   r<   r>   rA   rC   rE   rK   rM   r   Ú__classcell__r   r   r   r   r
      sD    

	
ÿ 
ür
   r!   rO   c           	      C  s`   ddl m} ddd„}|| ƒ}|D ]\}}}|t|ƒ }|||… }||kr-| | ¡  S qt‚)	zRA |BaseImageHeader| subclass instance that can parse headers of image in `stream`.r   )Ú
SIGNATURESr!   rO   c                 S  s   |   d¡ |  d¡S )Nr   é    )r,   r(   )r!   r   r   r   Úread_32¬   s   

z$_ImageHeaderFactory.<locals>.read_32N©r!   rO   )Ú
docx.imager[   ÚlenÚfrom_streamr   )	r!   r[   r]   Úheaderr    ÚoffsetÚsignature_bytesÚendÚfound_bytesr   r   r   rQ   ¨   s   
ÿrQ   c                   @  sf   e Zd ZdZddd„Zeddd„ƒZeddd„ƒZedd„ ƒZedd„ ƒZ	edd„ ƒZ
edd„ ƒZdS )r   z>Base class for image header subclasses like |Jpeg| and |Tiff|.r:   r8   r<   r>   rA   c                 C  s   || _ || _|| _|| _d S r   )Ú	_px_widthÚ
_px_heightÚ	_horz_dpiÚ	_vert_dpi)r   r:   r<   r>   rA   r   r   r   r   ¼   s   
zBaseImageHeader.__init__r   r   c                 C  s   d}t |ƒ‚)zDAbstract property definition, must be implemented by all subclasses.zNcontent_type property must be implemented by all subclasses of BaseImageHeader©ÚNotImplementedError)r   Úmsgr   r   r   r3   Â   s   zBaseImageHeader.content_typec                 C  s   t dƒ‚)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 BaseImageHeaderrk   r0   r   r   r   rR   È   s   ÿzBaseImageHeader.default_extc                 C  r/   r9   )rg   r0   r   r   r   r:   Ò   r1   zBaseImageHeader.px_widthc                 C  r/   r;   )rh   r0   r   r   r   r<   ×   r1   zBaseImageHeader.px_heightc                 C  r/   r=   )ri   r0   r   r   r   r>   Ü   ó   zBaseImageHeader.horz_dpic                 C  r/   r@   )rj   r0   r   r   r   rA   ä   rn   zBaseImageHeader.vert_dpiN)r:   r8   r<   r8   r>   r8   rA   r8   rS   )rT   rU   rV   rW   r   rY   r3   rR   r:   r<   r>   rA   r   r   r   r   r   ¹   s    
	


r   r^   )rW   Ú
__future__r   rL   r   r)   Útypingr   r   Údocx.image.exceptionsr   Údocx.sharedr   r   r   r	   r
   rQ   r   r   r   r   r   Ú<module>   s     
