o
    <­ÔZ§!  ã                   @   sø   d Z dZddlZddlZddlmZ ddlmZ ddlmZ ddlm	Z	m
Z
mZmZ ddlmZmZ dd	lmZ G d
d„ dƒZedkrzej ej ej¡dd¡Zej e¡sZJ dƒ‚de_eeddƒZddlZe ¡ Zedej ƒ ee !e¡ƒ dS dS )z3.3.0z@
Image functionality sliced out of canvas.py for generalization
é    N)Ú	rl_config)Úpdfutils)Úpdfdoc)Úimport_zlibÚ
haveImagesÚ
getBytesIOÚisStr)Úfp_strÚasciiBase85Encode)ÚaspectRatioFixc                   @   s^   e Zd ZdZddd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	ddd„Z
ddd„Zdd„ ZdS )ÚPDFImagezùWrapper around different "image sources".  You can make images
    from a PIL Image object, a filename (in which case it uses PIL),
    an image we previously cached (optimisation, hardly used these
    days) or a JPEG (which PDF supports natively).Nr   c                 C   sN   || _ || _|| _|| _|| _d | _|| _d| _d| _g | _	d | _
|  ¡  d S )NÚ	DeviceRGBé   )ÚimageÚxÚyÚwidthÚheightÚfilenameÚimageCachingÚ
colorSpaceÚbitsPerComponentÚfiltersÚsourceÚgetImageData)Úselfr   r   r   r   r   Úcaching© r   úL/var/www/html/env/lib/python3.10/site-packages/reportlab/pdfgen/pdfimages.pyÚ__init__   s   zPDFImage.__init__c                 C   s0   t | jdƒ}z|  |¡}W | ¡  |S | ¡  w )NÚrb)Úopenr   Ú_jpg_imagedataÚclose)r   ÚfpÚresultr   r   r   Újpg_imagedata*   s   
ÿzPDFImage.jpg_imagedatac                 C   s®   t  |¡}d| _|d |d }}|d dkrd}n|d dkr#d}nd}| d¡ g }| d	|||tjr7d
p8df ¡ | ¡ }tjrGt|ƒ}t  	||¡ | d¡ |||fS )NÚJPEGr   é   é   Ú
DeviceGrayé   r   Ú
DeviceCMYKz,BI /W %d /H %d /BPC 8 /CS /%s /F [%s/DCT] IDú/A85 Ú ÚEI)
r   ÚreadJPEGInfor   ÚseekÚappendr   ÚuseA85Úreadr
   Ú_chunker)r   Ú	imageFileÚinfoÚimgwidthÚ	imgheightr   Ú	imagedataÚdatar   r   r   r"   4   s"   

 

zPDFImage._jpg_imagedatac                 C   sp   | j }t |¡stƒ }|sd S tsd S t |¡ tj |¡d t	j
r$dp%d }t|dƒ ¡ }tttj|ƒƒ}|S )Nr   z.a85z.binr    )r   r   ÚcachedImageExistsr   r   ÚcacheImageFileÚosÚpathÚsplitextr   r3   r!   Ú	readlinesÚlistÚmapÚstrÚstrip)r   r   ÚzlibÚ
cachednamer:   r   r   r   Úcache_imagedataJ   s   

zPDFImage.cache_imagedatac                 C   sD  | j }|jdkr|j}| d¡ |  |¡S d| _tƒ }|sd S d}|jdkr-|}d}d}n#|jdkr;|}d	}d
}d
}n|jdkrG|}d	}d
}n	| d¡}d}d}|j	\}}	d||	||t
jr_dp`df g}
t|dƒrl|jn|jƒ }|| | d d? }t|ƒ||	 ks†J dƒ‚| |¡}t
jr’t|ƒ}t ||
¡ |
 d¡ |
||	fS )Nr'   r   ÚPILr   ÚCMYKr,   é   Ú1r*   r(   ÚLÚRGBr+   z,BI /W %d /H %d /BPC %d /CS /%s /F [%s/Fl] IDr-   r.   Útobytesé   zWrong amount of data for imager/   )r   Úformatr$   r1   r"   r   r   ÚmodeÚconvertÚsizer   r3   ÚhasattrrO   ÚtostringÚlenÚcompressr
   r   r5   r2   )r   r   r$   rF   ÚbpcÚmyimager   Úbppr8   r9   r:   ÚrawÚ	rowstrider;   r   r   r   ÚPIL_imagedataY   sH   










zPDFImage.PIL_imagedatac                 C   sL   | j stj|dd�}n|  ¡ }|d  ¡ }t|d ƒ}t|d ƒ}|||fS )Nr(   )ÚreturnInMemoryr+   )r   r   r=   rH   ÚsplitÚint)r   r   r:   Úwordsr8   r9   r   r   r   Únon_jpg_imagedataˆ   s   
zPDFImage.non_jpg_imagedataFc                 C   sÎ   | j }t|ƒr4|| _tj |¡d dv r+z	|  ¡ \}}}W n2   |  |¡\}}}Y n%|  |¡\}}}nddl}|j	dd… dkrI|  
¡ \}}}n|  ¡ \}}}|| _|| _|| _| jp]|| _| jpc|| _dS )z1Gets data, height, width - whatever type of imager(   )z.jpgz.JPGz.jpegz.JPEGr   NrK   Újava)r   r   r   r>   r?   r@   r&   rc   ÚsysÚplatformÚJAVA_imagedatar^   Ú	imageDatar8   r9   r   r   )r   ÚpreserveAspectRatior   r:   r8   r9   re   r   r   r   r   ’   s$   zPDFImage.getImageDataÚswc              
   C   sÂ   | j }| j}|dk s|dk rdS t||| j| j||| j| j|ƒ	\}}	| _ | _}
|js.|	| }	|j 	dt
| j ƒt
| j||	ƒf ¡ | jD ]}|j 	|¡ qC|j 	d¡ |r_|j||	||ddd� dS )	zØDraw an Image into the specified rectangle.  If width and
        height are omitted, they are calculated from the image size.
        Also allow file names as well as images.  This allows a
        caching mechanismg�íµ ÷Æ°>Fzq %s 0 0 %s cmÚQr(   r   )ÚstrokeÚfillT)r   r   r   r   r   r8   r9   ÚbottomupÚ_coder2   r	   rh   Úrect)r   Úcanvasri   ÚanchorÚ
anchorAtXYÚshowBoundaryr   r   r   r   ÚscaledÚliner   r   r   ÚdrawInlineImage¬   s   .$
zPDFImage.drawInlineImagec                 C   st   t  ¡ }d|d< d|d< | j|d< | j|d< d|d< t  | j¡|d	< d
 | jdd… ¡d
 }t j||d�}| 	|¡S )ztAllow it to be used within pdfdoc framework.  This only
        defines how it is stored, not how it is drawn later.z/XObjectÚTypez/ImageÚSubtypeÚWidthÚHeightr   ÚBitsPerComponentÚ
ColorSpaceÚ
r+   éÿÿÿÿ)Ú
dictionaryÚcontent)
r   ÚPDFDictionaryr   r   ÚPDFNamer   Újoinrh   Ú	PDFStreamrQ   )r   ÚdocumentÚdictr�   Ústrmr   r   r   rQ   À   s   


zPDFImage.format)NNr   )F)Frj   FF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r&   r"   rH   r^   rc   r   rw   rQ   r   r   r   r   r      s    

/


r   Ú__main__Útestzpythonpowered.gifzimage not foundr(   éd   zsource=)"Ú__version__rŒ   r>   Ú	reportlabr   Úreportlab.pdfbaser   r   Úreportlab.lib.utilsr   r   r   r   Úreportlab.lib.rl_accelr	   r
   Úreportlab.lib.boxstuffr   r   r‰   r?   r„   ÚdirnameÚ__file__ÚsrcfileÚisfileÚ
LongFormatÚimgÚpprintÚPDFDocumentÚdocÚprintr   rQ   r   r   r   r   Ú<module>   s4    =ýô