o
    à’Ojæ   ã                   @   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	 ddl
mZmZ dd	lmZ G d
d„ dƒZedkrpej ej ej¡dd¡Zej e¡sTJ dƒ‚de_eeddƒ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)ÚisStr)Úfp_strÚasciiBase85Encode)ÚaspectRatioFixc                   @   sb   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   ú`/var/www/html/pharmsmart-cdr-gen/venv/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   sZ   | j }t |¡st |¡ tj |¡d tjrdpd }t	|dƒ 
¡ }tttj|ƒƒ}|S )Nr   z.a85z.binr   )r   r   ÚcachedImageExistsÚcacheImageFileÚosÚpathÚsplitextr   r0   r   Ú	readlinesÚlistÚmapÚstrÚstrip)r   r   Ú
cachednamer7   r   r   r   Úcache_imagedataJ   s   

zPDFImage.cache_imagedatac                 C   s>  dd l }| j}|jdkr|j}| d¡ |  |¡S d| _d}|jdkr*|}d}d}n#|jdkr8|}d	}d
}d
}n|jdkrD|}d	}d
}n	| d¡}d}d}|j	\}}	d||	||t
jr\dp]df g}
t|dƒri|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,   )Úzlibr   Úformatr!   r.   r   r   ÚmodeÚconvertÚsizer   r0   ÚhasattrrK   ÚtostringÚlenÚcompressr   r   r2   r/   )r   rM   r   r!   ÚbpcÚmyimager   Úbppr5   r6   r7   ÚrawÚ	rowstrider8   r   r   r   ÚPIL_imagedataV   sF   










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:   rD   ÚsplitÚint)r   r   r7   Úwordsr5   r6   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 n   |  |¡\}}}Y n|  |¡\}}}n|  ¡ \}}}|| _	|| _
|| _| jpH|| _| jpN|| _dS )z1Gets data, height, width - whatever type of imager%   )z.jpgz.JPGz.jpegz.JPEGN)r   r   r   r;   r<   r=   r#   r`   r[   Ú	imageDatar5   r6   r   r   )r   ÚpreserveAspectRatior   r7   r5   r6   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 }| j}| jD ]}|j 	|¡ qI|j 	d¡ |rc| ||	|
||¡ |rs| ¡ D ]	}tƒ | ||< qid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ÚQT)r   r   r   r   r   r5   r6   ÚbottomupÚ_coder/   r   ra   ÚdrawBoundaryÚkeysÚvars)r   Úcanvasrb   ÚanchorÚ
anchorAtXYÚshowBoundaryÚextraReturnr   r   r   r   ÚscaledÚlineÚkr   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   Újoinra   Ú	PDFStreamrN   )r   ÚdocumentÚdictr|   Ústrmr   r   r   rN   ½   s   


zPDFImage.format)NNr   )F)Frc   FFN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r#   r   rD   r[   r`   r   rr   rN   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   Úreportlab.lib.rl_accelr   r   Úreportlab.lib.boxstuffr   r	   r„   r<   r   ÚdirnameÚ__file__ÚsrcfileÚisfileÚ
LongFormatÚimgÚPDFDocumentÚdocÚprintr   rN   r   r   r   r   Ú<module>   s2    :ýõ