a
    ¦¾deæ   ã                   @   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rÜej ej ej¡dd¡Zej e¡s¨J dƒ‚de_eeddƒZe ¡ Zedejƒ ee e¡ƒ 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                   @   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Ú
colorSpaceZbitsPerComponentÚfiltersÚsourceÚgetImageData)Úselfr   r   r   r   r   Zcaching© r   úS/var/www/sistema_ama/venv/lib/python3.9/site-packages/reportlab/pdfgen/pdfimages.pyÚ__init__   s    zPDFImage.__init__c                 C   s2   t | jdƒ}z|  |¡}W | ¡  n
| ¡  0 |S )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r4d}n|d dkrFd}nd}| d¡ g }| d	|||tjrnd
ppdf ¡ | ¡ }tjrŽt|ƒ}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   ZreadJPEGInfor   ÚseekÚappendr   ÚuseA85Úreadr   Ú_chunker)r   Z	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r2dp4d }t	|dƒ 
¡ }tttj|ƒƒ}|S )Nr   z.a85z.binr   )r   r   ZcachedImageExistsÚcacheImageFileÚosÚpathÚsplitextr   r-   r   Ú	readlinesÚlistÚmapÚstrÚstrip)r   r   Z
cachednamer3   r   r   r   Úcache_imagedataJ   s    

zPDFImage.cache_imagedatac                 C   sB  dd l }| j}|jdkr2|j}| d¡ |  |¡S d| _d}|jdkrT|}d}d}nF|jdkrp|}d	}d
}d
}n*|jdkrˆ|}d	}d
}n| d¡}d}d}|j	\}}	d||	||t
jr¸dpºdf g}
t|dƒrÒ|jn|jƒ }|| | d d? }t|ƒ||	 k�sJ dƒ‚| |¡}t
j�r"t|ƒ}t ||
¡ |
 d¡ |
||	fS )Nr   r"   ZPILr   ZCMYKr'   é   Ú1r%   r#   ÚLZ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   r-   ÚhasattrrB   ÚtostringÚlenÚcompressr   r   r/   r,   )r   rD   r   r   ZbpcZmyimager   Zbppr1   r2   r3   ÚrawZ	rowstrider4   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#   )ZreturnInMemoryr&   )r   r   r5   r>   ÚsplitÚint)r   r   r3   Úwordsr1   r2   r   r   r   Únon_jpg_imagedata„   s    zPDFImage.non_jpg_imagedataFc                 C   s¨   | j }t|ƒrl|| _tj |¡d dv rZz|  ¡ \}}}W qj   |  |¡\}}}Y qj0 qz|  |¡\}}}n|  ¡ \}}}|| _	|| _
|| _| jp”|| _| jp || _dS )z1Gets data, height, width - whatever type of imager#   )z.jpgz.JPGz.jpegz.JPEGN)r   r   r   r6   r7   r8   r!   rR   rN   Ú	imageDatar1   r2   r   r   )r   ÚpreserveAspectRatior   r3   r1   r2   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 	|¡ q’|j 	d¡ |rÆ| ||	|
||¡ |ræ| ¡ D ]}tƒ | ||< qÒ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ÚQT)r   r   r   r   r   r1   r2   ZbottomupÚ_coder,   r   rS   ZdrawBoundaryÚkeysÚvars)r   ZcanvasrT   ÚanchorZ
anchorAtXYZshowBoundaryZextraReturnr   r   r   r   Z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/ImageZSubtypeZWidthZHeightr   ZBitsPerComponentZ
ColorSpaceÚ
r&   éÿÿÿÿ)Ú
dictionaryÚcontent)
r   ZPDFDictionaryr   r   ZPDFNamer   ÚjoinrS   Z	PDFStreamrE   )r   ÚdocumentÚdictrb   Ústrmr   r   r   rE   ½   s    

zPDFImage.format)NNr   )F)FrU   FFN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r!   r   r>   rN   rR   r   r]   rE   r   r   r   r   r	      s   

.

  ÿ
r	   Ú__main__Útestzpythonpowered.gifzimage not foundr#   éd   zsource=)Ú__version__rj   r6   Z	reportlabr   Zreportlab.pdfbaser   r   Zreportlab.lib.utilsr   Zreportlab.lib.rl_accelr   r   Zreportlab.lib.boxstuffr   r	   rg   r7   rc   ÚdirnameÚ__file__ÚsrcfileÚisfileZ
LongFormatÚimgZPDFDocumentÚdocÚprintr   rE   r   r   r   r   Ú<module>   s.    :ý