B
    &T]                 @   s   d Z ddlmZmZm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 G d	d
 d
eZdd ZG dd deZdS )z
Provides objects that can characterize image streams as to content type and
size, as a required step in including them in a document.
    )absolute_importdivisionprint_functionN   )BytesIO	is_string   )UnrecognizedImageError)EmuIncheslazypropertyc                   s   e Zd ZdZ fddZedd Zedd Zedd	 Z	ed
d Z
edd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zd#ddZedd  Zed$d!d"Z  ZS )%Imagezq
    Graphical image stream such as JPEG, PNG, or GIF with properties and
    methods required by ImagePart.
    c                s$   t t|   || _|| _|| _d S )N)superr   __init___blob	_filename_image_header)selfblobfilenameimage_header)	__class__ B/var/www/html/venv/lib/python3.7/site-packages/docx/image/image.pyr      s    zImage.__init__c             C   s   t |}| ||S )zr
        Return a new |Image| subclass instance parsed from the image binary
        contained in *blob*.
        )r   _from_stream)clsr   streamr   r   r   	from_blob   s    zImage.from_blobc          	   C   sh   t |r@|}t|d}| }t|}W dQ R X 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   )	r   openreadr   ospathbasenameseekr   )r   Zimage_descriptorr"   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   9   s    z
Image.blobc             C   s   | j jS )zb
        MIME content type for this image, e.g. ``'image/jpeg'`` for a JPEG
        image
        )r   content_type)r   r   r   r   r'   @   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'.
        r   N)r!   r"   splitextr   )r   r   r   r   extH   s    z	Image.extc             C   s   | j S )z
        Original image file name, if loaded from disk, or a generic filename
        if loaded from an anonymous stream.
        )r   )r   r   r   r   r   R   s    zImage.filenamec             C   s   | j jS )z=
        The horizontal pixel dimension of the image
        )r   px_width)r   r   r   r   r*   Z   s    zImage.px_widthc             C   s   | j jS )z;
        The vertical pixel dimension of the image
        )r   	px_height)r   r   r   r   r+   a   s    zImage.px_heightc             C   s   | j jS )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_dpi)r   r   r   r   r,   h   s    zImage.horz_dpic             C   s   | j jS )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_dpi)r   r   r   r   r-   p   s    zImage.vert_dpic             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   r*   r,   )r   r   r   r   widthx   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   r+   r-   )r   r   r   r   height   s    zImage.heightNc             C   s|   |dkr|dkr| j | jfS |dkrDt|t| j }t| j | }|dkrlt|t| j  }t| j| }t|t|fS )a  
        Return a (cx, cy) 2-tuple representing the native dimensions of this
        image scaled by applying the following rules to *width* and *height*.
        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_dimensions   s    zImage.scaled_dimensionsc             C   s   t | j S )z4
        SHA1 hash digest of the image blob
        )hashlibsha1r   	hexdigest)r   r   r   r   r4      s    z
Image.sha1c             C   s&   t |}|dkrd|j }| |||S )zz
        Return 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   r'   r   r)   r   r*   r+   r,   r-   r.   r/   r2   r4   r   __classcell__r   r   )r   r   r      s$   	

r   c       	      C   sb   ddl m} dd }|| }x<|D ]4\}}}|t| }||| }||kr"|| S q"W tdS )zx
    Return a |BaseImageHeader| subclass instance that knows how to parse the
    headers of the image in *stream*.
    r   )
SIGNATURESc             S   s   |  d | dS )Nr       )r$   r    )r   r   r   r   read_32   s    
z$_ImageHeaderFactory.<locals>.read_32N)Z
docx.imager?   lenZfrom_streamr	   )	r   r?   rA   headerr   offsetZsignature_bytesendZfound_bytesr   r   r   r6      s    r6   c               @   s`   e Zd ZdZdd Zedd Zedd Zedd	 Zed
d Z	edd Z
edd ZdS )BaseImageHeaderzH
    Base class for image header subclasses like |Jpeg| and |Tiff|.
    c             C   s   || _ || _|| _|| _d S )N)	_px_width
_px_height	_horz_dpi	_vert_dpi)r   r*   r+   r,   r-   r   r   r   r      s    zBaseImageHeader.__init__c             C   s   d}t |dS )zV
        Abstract property definition, must be implemented by all subclasses.
        zNcontent_type property must be implemented by all subclasses of BaseImageHeaderN)NotImplementedError)r   msgr   r   r   r'      s    zBaseImageHeader.content_typec             C   s   d}t |dS )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 BaseImageHeaderN)rK   )r   rL   r   r   r   r7      s    zBaseImageHeader.default_extc             C   s   | j S )z=
        The horizontal pixel dimension of the image
        )rG   )r   r   r   r   r*      s    zBaseImageHeader.px_widthc             C   s   | j S )z;
        The vertical pixel dimension of the image
        )rH   )r   r   r   r   r+      s    zBaseImageHeader.px_heightc             C   s   | j S )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.
        )rI   )r   r   r   r   r,      s    zBaseImageHeader.horz_dpic             C   s   | j S )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.
        )rJ   )r   r   r   r   r-     s    zBaseImageHeader.vert_dpiN)r8   r9   r:   r;   r   r=   r'   r7   r*   r+   r,   r-   r   r   r   r   rF      s   rF   )r;   
__future__r   r   r   r3   r!   compatr   r   
exceptionsr	   sharedr
   r   r   objectr   r6   rF   r   r   r   r   <module>   s    %