U
    7h4                     @   sj   d dl Z d dlZd dlmZ d dlmZ d dlmZmZ d dl	m
Z
 ddlmZmZ dZG d	d
 d
ZdS )    N)BytesIOunpack)TupleUnion)Url   )UndefinedImageSizeUnsupportedImageFormatg      X@c                   @   s  e Zd ZdZeeef dddZdd Ze	eddd	Z
e	edd
dZe	edddZe	edddZe	edddZe	eedf dddZejedddZe	edddZejedddZe	eedf dddZejedddZd.ddZdd  Zeeeeeeef d!d"d#Zeeeeeeef d!d$d%Zeeeeeeef d!d&d'Zeeeeef d!d(d)Zeeeeeeef d!d*d+Zeeeeeeef d!d,d-ZdS )/Imagez?
    A class to represent an image in an Excel worksheet.

    )sourcec                 C   s   t |tr&|| _d| _tj|| _n&t |trDd| _|| _d| _nt	dd| _
d| _d| _d| _d| _d| _d| _d| _d| _d| _d| _d| _d| _d| _d| _t| _t| _d| _|   dS )	z
        Initialize an Image instance.

        Args:
            source (Union[str, BytesIO]): The filename or BytesIO object of the image.
        N z4Source must be a filename (str) or a BytesIO object.r   g      ?   F        )
isinstancestrfilename
image_dataospathbasename
image_namer   
ValueErrorZ_rowZ_col	_x_offset	_y_offset_x_scale_y_scale_url_anchor_description_decorativeZ_header_positionZ_ref_id_image_extension_width_heightDEFAULT_DPI_x_dpi_y_dpi_digest_get_image_properties)selfr    r*   J/var/www/formularioweb/env/lib/python3.8/site-packages/xlsxwriter/image.py__init__   s8    

zImage.__init__c                 C   s>   d| j d| jd| jd| j d| j d| j d| j dS )	zf
        Return a string representation of the main properties of the Image
        instance.
        zImage:
    filename   = z
    image_name = z
    image_type = z
    width      = z
    height     = z
    x_dpi      = z
    y_dpi      = 
)r   r   
image_typer"   r#   r%   r&   r)   r*   r*   r+   __repr__F   s    <zImage.__repr__)returnc                 C   s
   | j  S )z)Get the image type (e.g., 'PNG', 'JPEG').)r!   upperr/   r*   r*   r+   r.   V   s    zImage.image_typec                 C   s   | j S )zGet the width of the image.)r"   r/   r*   r*   r+   width[   s    zImage.widthc                 C   s   | j S )zGet the height of the image.)r#   r/   r*   r*   r+   height`   s    zImage.heightc                 C   s   | j S )z$Get the horizontal DPI of the image.)r%   r/   r*   r*   r+   x_dpie   s    zImage.x_dpic                 C   s   | j S )z"Get the vertical DPI of the image.)r&   r/   r*   r*   r+   y_dpij   s    zImage.y_dpiNc                 C   s   | j S )z*Get the description/alt-text of the image.r   r/   r*   r*   r+   descriptiono   s    zImage.description)valuec                 C   s   |r
|| _ dS )z*Set the description/alt-text of the image.Nr7   r)   r9   r*   r*   r+   r8   t   s    c                 C   s   | j S )z$Get whether the image is decorative.r    r/   r*   r*   r+   
decorativez   s    zImage.decorativec                 C   s
   || _ dS )z$Set whether the image is decorative.Nr;   r:   r*   r*   r+   r<      s    c                 C   s   | j S )zGet the image url.r   r/   r*   r*   r+   url   s    z	Image.urlc                 C   s   |r
|| _ dS )zSet the image url.Nr=   r:   r*   r*   r+   r>      s    c                 C   s   |dkrdS | j s.t|| _ | j r.| j   |d| j| _|d| j| _|d| j| _|d| j| _|d| j	| _	|d| j
| _
|d| j| _|d	| j| _|d
| j| _dS )zY
        This handles the additional optional parameters to ``insert_button()``.
        NZobject_positionZx_scaleZy_scaleZx_offsetZy_offsetr<   r   r8   Zpositioning)r   r   Zfrom_optionsZ_set_object_linkgetr   r   r   r   r   r    r   r   )r)   optionsr*   r*   r+   _set_user_options   s    
zImage._set_user_optionsc              	   C   s  d}d}t }t }| jr"| j }n t| jd}| }W 5 Q R X t| }t	d|dd d }t	d|d d d }	t	d	|d d d }
t	d
|d d d }t	d
|dd d }t	d|d d d }|dkr| 
|\}}}}}n|	dkr| |\}}}}}n|
dkr.| |\}}}nz|dkrN| |\}}}}}nZ|dkrx|dkrx| |\}}}}}n0|dkr| |\}}}}}nt| j d|r|st| j d|dkrt }|dkrt }|| _|| _|| _|| _|| _|| _d S )Nr   rbZ3sr      r   >Hr   Z2s4s(   ,   <Ls   PNGi  s   BMl   M5 s    EMFs   GIF8z+: Unknown or unsupported image file format.z#: no size data found in image file.)r$   r   getvalueopenr   readhashlibsha256	hexdigestr   _process_png_process_jpg_process_bmp_process_wmf_process_emf_process_gifr
   r	   r!   r"   r#   r%   r&   r'   )r)   r4   r3   r5   r6   datafhdigestZ
png_markerZ
jpg_markerZ
bmp_markerZ
gif_markerZ
emf_markerZemf_marker1r.   r*   r*   r+   r(      sX    







zImage._get_image_properties)rU   r1   c                 C   sn  d}t |}d}d}d}t}t}|s`||k r`td||d |d  d }	td||d |d  d }
|
dkrtd||d |d	  d }td||d	 |d
  d }|
dkrBtd||d |d	  d }td||d	 |d
  d }td||d
 |d  d }|dkrB|dkrB|dkrB|d }|d }|
dkrRd}q ||	 d	 }q d||||fS )N   Fr   z>Ir   rC   rE   s   IHDR      s   pHYsb   r   g
F%u?s   IENDTZpnglenr$   r   )r)   rU   offsetdata_length
end_markerr3   r4   r5   r6   lengthmarker	x_density	y_densityunitsr*   r*   r+   rO      s2    

zImage._process_pngc                 C   s  d}t |}d}d}d}t}t}|s||k rtd||d |d  d }	td||d |d  d }
|	d@ dkr|	d	kr|	d
kr|	dkrtd||d |d  d }td||d |d  d }|	dkrxtd||d |d  d }td||d |d  d }td||d |d  d }|dkrB|}|}|dkr\|d }|d }|dkrjt}|dkrxt}|	dkrd}q ||
 d }q d||||fS )Nr   Fr   rD   r   rC   i  i  i  i  i        	   i  r[      rY      rZ   r   gRQ@i  TZjpegr]   )r)   rU   r_   r`   ra   r3   r4   r5   r6   rc   rb   rf   rd   re   r*   r*   r+   rP     sN    






zImage._process_jpgc                 C   sB   t }t }td|dd d }td|dd d }d||||fS )N<h   rX   r   
   Zgif)r$   r   )r)   rU   r5   r6   r3   r4   r*   r*   r+   rT   K  s
    zImage._process_gifc                 C   s6   t d|dd d }t d|dd d }d||fS )NrH         r      Zbmpr   )r)   rU   r3   r4   r*   r*   r+   rQ   U  s    zImage._process_bmpc                 C   s   t }t }td|dd d }td|dd d }td|dd d }td|dd d }td|dd	 d }t|| | | }	t|| | | }
d
|	|
||fS )Nrl   rm   rX   r   rn   rY   rk   z<HrZ   Zwmf)r$   r   float)r)   rU   r5   r6   x1y1Zx2y2Zinchr3   r4   r*   r*   r+   rR   [  s    zImage._process_wmfc                 C   s  t d|dd d }t d|dd d }t d|dd d }t d|dd d }|| }|| }t d|dd d }t d|dd	 d }	t d|d	d
 d }
t d|d
d d }d|
|  }d||	  }|d | }|d | }|d7 }|d7 }d||||fS )Nz<lrX   rY   r   rZ                $   rF   g{Gz?gffffff9@r   Zemfr   )r)   rU   Zbound_x1Zbound_y1Zbound_x2Zbound_y2r3   r4   Zframe_x1Zframe_y1Zframe_x2Zframe_y2Zwidth_mmZ	height_mmr5   r6   r*   r*   r+   rS   o  s"    zImage._process_emf)N) __name__
__module____qualname____doc__r   r   r   r,   r0   propertyr.   rr   r3   r4   r5   r6   r8   setterboolr<   r   r>   rA   r(   bytesr   rO   rP   rT   rQ   rR   rS   r*   r*   r*   r+   r      sF   )
F*6
r   )rL   r   ior   structr   typingr   r   Zxlsxwriter.urlr   
exceptionsr	   r
   r$   r   r*   r*   r*   r+   <module>
   s   