a
    ]jE                     @  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dZG dd dZdS )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                      s,  e Zd ZdZdddd fddZedd dd	d
ZeddddZedd Z	eddddZ
edd Zedd ZeddddZeddddZeddddZedddd Zed!dd"d#Zed!dd$d%Zd3d'd'd(d)d*d+Zed,d- Zed4d.dd/d d0d1d2Z  ZS )5ImagezgGraphical image stream such as JPEG, PNG, or GIF with properties and methods
    required by ImagePart.bytesstrBaseImageHeader)blobfilenameimage_headerc                   s$   t t|   || _|| _|| _d S N)superr
   __init___blob	_filename_image_header)selfr   r   r   	__class__ L/var/www/html/assistant/venv/lib/python3.9/site-packages/docx/image/image.pyr      s    zImage.__init__)r   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_blobzstr | IO[bytes])image_descriptorc                 C  s   t |trX|}t|d"}| }t|}W d   n1 s@0    Y  tj|}n|}|	d | }d}| 
|||S )zReturn 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  s   | j S )zThe bytes of the image 'file'.)r   r   r   r   r   r   4   s    z
Image.blobr   c                 C  s   | j jS )zIMIME content type for this image, e.g. ``'image/jpeg'`` for a JPEG image.)r   content_typer.   r   r   r   r0   9   s    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   r.   r   r   r   ext>   s    z	Image.extc                 C  s   | j S )zpOriginal image file name, if loaded from disk, or a generic filename if
        loaded from an anonymous stream.)r   r.   r   r   r   r   H   s    zImage.filenameintc                 C  s   | j jS z,The horizontal pixel dimension of the image.)r   px_widthr.   r   r   r   r6   N   s    zImage.px_widthc                 C  s   | j jS z*The vertical pixel dimension of the image.)r   	px_heightr.   r   r   r   r8   S   s    zImage.px_heightc                 C  s   | j jS zInteger 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_dpir.   r   r   r   r:   X   s    zImage.horz_dpic                 C  s   | j jS zInteger 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_dpir.   r   r   r   r<   `   s    zImage.vert_dpir   c                 C  s   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   r6   r:   r.   r   r   r   widthh   s    zImage.widthc                 C  s   t | j| j S )zA |Length| value representing the native height of the image, calculated from
        the values of `px_height` and `vert_dpi`.)r   r8   r<   r.   r   r   r   heightn   s    zImage.heightNzint | Length | NonezTuple[Length, Length])r=   r>   r   c                 C  s   |du r|du r| j | jfS |du rP|dus0J t|t| j }t| j | }|du rxt|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)r=   r>   floatroundr   )r   r=   r>   Z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   	hexdigestr.   r   r   r   rC      s    z
Image.sha1	IO[bytes]z
str | None)r!   r   r   r   c                 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)NN)N)__name__
__module____qualname____doc__r   classmethodr"   r-   propertyr   r0   r	   r3   r   r6   r8   r:   r<   r=   r>   rA   rC   r   __classcell__r   r   r   r   r
      sB   

	
  
 r
   rE   r!   c           	      C  sh   ddl m} dddd}|| }|D ]8\}}}|t| }||| }||kr&||   S q&tdS )zRA |BaseImageHeader| subclass instance that can parse headers of image in `stream`.r   )
SIGNATURESrE   rO   c                 S  s   |  d | dS )Nr       )r+   r'   rO   r   r   r   read_32   s    
z$_ImageHeaderFactory.<locals>.read_32N)Z
docx.imagerP   lenfrom_streamr   )	r!   rP   rR   headerr    offsetZsignature_bytesendZfound_bytesr   r   r   rF      s    rF   c                   @  sx   e Zd ZdZdddddddZedddd	Zed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|.r4   )r6   r8   r:   r<   c                 C  s   || _ || _|| _|| _d S r   )	_px_width
_px_height	_horz_dpi	_vert_dpi)r   r6   r8   r:   r<   r   r   r   r      s    zBaseImageHeader.__init__r   r/   c                 C  s   d}t |dS )zDAbstract property definition, must be implemented by all subclasses.zNcontent_type property must be implemented by all subclasses of BaseImageHeaderNNotImplementedError)r   msgr   r   r   r0      s    zBaseImageHeader.content_typec                 C  s   t ddS )zDefault 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 BaseImageHeaderNr\   r.   r   r   r   rG      s    zBaseImageHeader.default_extc                 C  s   | j S r5   )rX   r.   r   r   r   r6      s    zBaseImageHeader.px_widthc                 C  s   | j S r7   )rY   r.   r   r   r   r8      s    zBaseImageHeader.px_heightc                 C  s   | j S r9   )rZ   r.   r   r   r   r:      s    zBaseImageHeader.horz_dpic                 C  s   | j S r;   )r[   r.   r   r   r   r<      s    zBaseImageHeader.vert_dpiN)rH   rI   rJ   rK   r   rM   r0   rG   r6   r8   r:   r<   r   r   r   r   r      s   	


r   )rK   
__future__r   rB   r   r(   typingr   r   Zdocx.image.exceptionsr   Zdocx.sharedr   r   r   r	   r
   rF   r   r   r   r   r   <module>   s    