o
    `bM                      @   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
 ddlmZmZ dd	lmZ G d
d dZedkrrejejejddZejesVJ 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)
haveImages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 )PDFImagezWrapper 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   </usr/lib/python3/dist-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   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   sb   | j }t|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   ZcachedImageExistsr   cacheImageFileospathsplitextr   r.   r   	readlineslistmapstrstrip)r   r   Z
cachednamer4   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sJ d||}t
jrt|}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   modeZconvertsizer   r.   hasattrrC   Ztostringlencompressr   r   r0   r-   )r   rE   r   r    ZbpcZmyimager   Zbppr2   r3   r4   rawZ	rowstrider5   r   r   r   PIL_imagedataW   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   r6   r?   splitint)r   r   r4   Zwordsr2   r3   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   r7   r8   r9   r"   rP   rM   	imageDatar2   r3   r   r   )r   preserveAspectRatior   r4   r2   r3   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]||||	|| dS )zDraw 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   r2   r3   Zbottomup_coder-   r   rQ   ZdrawBoundary)r   ZcanvasrR   ZanchorZ
anchorAtXYZshowBoundaryr   r   r   r   Z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/XObjectZTypez/ImageZSubtypeZWidthZHeightr   ZBitsPerComponentZ
ColorSpace
r'   )Z
dictionarycontent)
r   ZPDFDictionaryr   r   ZPDFNamer   joinrQ   Z	PDFStreamrF   )r   ZdocumentdictrZ   Zstrmr   r   r   rF      s   


zPDFImage.format)NNr   )F)FrS   FF)__name__
__module____qualname____doc__r   r"   r   r?   rM   rP   r   rW   rF   r   r   r   r   r
      s    

.


r
   __main__testzpythonpowered.gifzimage not foundr$   d   zsource=)__version__r`   r7   Z	reportlabr   Zreportlab.pdfbaser   r   Zreportlab.lib.utilsr   r   Zreportlab.lib.rl_accelr   r   Zreportlab.lib.boxstuffr	   r
   r]   r8   r[   dirname__file__ZsrcfileisfileZ
LongFormatZimgZPDFDocumentdocprintr   rF   r   r   r   r   <module>   s2    5