o
    vh                      @   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 )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
colorSpacebitsPerComponentfilterssourcegetImageData)selfr   r   r   r   r   caching r   X/var/www/html/hyperkenya/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sJ d||}t
jrt|}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 )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   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    :