o
    ,h4                     @   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deeef fddZdd Ze	defdd	Z
e	defd
dZe	defddZe	defddZe	defddZe	deedf fddZejdefddZe	defddZejdefddZe	deedf fddZejdefddZd.ddZdd  Zd!edeeeeeef fd"d#Zd!edeeeeeef fd$d%Zd!edeeeeeef fd&d'Zd!edeeeef fd(d)Zd!edeeeeeef fd*d+Zd!edeeeeeef f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r"d| _|| _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   
ValueError_row_col	_x_offset	_y_offset_x_scale_y_scale_url_anchor_description_decorative_header_position_ref_id_image_extension_width_heightDEFAULT_DPI_x_dpi_y_dpi_digest_get_image_properties)selfr    r.   F/var/www/html/rh/venv/lib/python3.10/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%   upperr3   r.   r.   r/   r2   V      
zImage.image_typec                 C      | j S )zGet the width of the image.)r&   r3   r.   r.   r/   width[      zImage.widthc                 C   r8   )zGet the height of the image.)r'   r3   r.   r.   r/   height`   r:   zImage.heightc                 C   r8   )z$Get the horizontal DPI of the image.)r)   r3   r.   r.   r/   x_dpie   r:   zImage.x_dpic                 C   r8   )z"Get the vertical DPI of the image.)r*   r3   r.   r.   r/   y_dpij   r:   zImage.y_dpiNc                 C   r8   )z*Get the description/alt-text of the image.r!   r3   r.   r.   r/   descriptiono   r:   zImage.descriptionvaluec                 C      |r|| _ dS dS )z*Set the description/alt-text of the image.Nr>   r-   r@   r.   r.   r/   r?   t      
c                 C   r8   )z$Get whether the image is decorative.r"   r3   r.   r.   r/   
decorativez   r:   zImage.decorativec                 C   s
   || _ dS )z$Set whether the image is decorative.NrD   rB   r.   r.   r/   rE      r7   c                 C   r8   )zGet the image url.r   r3   r.   r.   r/   url   r:   z	Image.urlc                 C   rA   )zSet the image url.NrF   rB   r.   r.   r/   rG      rC   c                 C   s   |du 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()``.
        Nobject_positionx_scaley_scalex_offsety_offsetrE   r   r?   positioning)r   r   from_options_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 d    n1 s&w   Y  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| 
|\}}}}}nU|	dkr| |\}}}}}nF|
dkr| |\}}}n9|dkr| |\}}}}}n*|dkr|dkr| |\}}}}}n|dkr| |\}}}}}nt| j d|r|st| j d|dkrt }|dkrt }|| _|| _|| _|| _|| _|| _d S )Nr   rb3sr      r   >Hr   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-   r;   r9   r<   r=   datafhdigest
png_marker
jpg_marker
bmp_marker
gif_marker
emf_markeremf_marker1r2   r.   r.   r/   r,      sZ   



zImage._get_image_propertiesrh   c                 C   sj  d}t |}d}d}d}t}t}|s||k rtd||d |d  d }	td||d |d  d }
|
dkrVtd||d |d	  d }td||d	 |d
  d }|
dkrtd||d |d	  d }td||d	 |d
  d }td||d
 |d  d }|dkr|dkr|dkr|d }|d }|
dkrd}q||	 d	 }|s||k sd||||fS )N   Fr   z>Ir   rU   rX   s   IHDR      s   pHYsb   r   g
F%u?s   IENDTpnglenr(   r   )r-   rh   offsetdata_length
end_markerr9   r;   r<   r=   lengthmarker	x_density	y_densityunitsr.   r.   r/   rb      s4   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rd|	d	krd|	d
krd|	dkrdtd||d |d  d }td||d |d  d }|	dkrtd||d |d  d }td||d |d  d }td||d |d  d }|dkr|}|}|dkr|d }|d }|dkrt}|dkrt}|	dkrd}q||
 d }|s||k sd||||fS )Nr   Fr   rV   r   rU   i  i  i  i  i        	   i  rt      rr      rs   r   gRQ@i  Tjpegrw   )r-   rh   ry   rz   r{   r9   r;   r<   r=   r}   r|   r   r~   r   r.   r.   r/   rc     sH   )zImage._process_jpgc                 C   sB   t }t }td|dd d }td|dd d }d||||fS )N<h   rq   r   
   gif)r(   r   )r-   rh   r<   r=   r9   r;   r.   r.   r/   rg   K  s
   zImage._process_gifc                 C   s6   t d|dd d }t d|dd d }d||fS )Nr[         r      bmpr   )r-   rh   r9   r;   r.   r.   r/   rd   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 )Nr   r   rq   r   r   rr   r   z<Hrs   wmf)r(   r   float)r-   rh   r<   r=   x1y1x2y2inchr9   r;   r.   r.   r/   re   [  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<lrq   rr   r   rs                $   rY   g{Gz?gffffff9@r   emfr   )r-   rh   bound_x1bound_y1bound_x2bound_y2r9   r;   frame_x1frame_y1frame_x2frame_y2width_mm	height_mmr<   r=   r.   r.   r/   rf   o  s"   zImage._process_emf)N) __name__
__module____qualname____doc__r   r   r   r0   r4   propertyr2   r   r9   r;   r<   r=   r?   setterboolrE   r   rG   rR   r,   bytesr   rb   rc   rg   rd   re   rf   r.   r.   r.   r/   r      sL    )
D
 * 6
 $r   )r_   r   ior   structr   typingr   r   xlsxwriter.urlr   
exceptionsr	   r
   r(   r   r.   r.   r.   r/   <module>   s   	