o
    hTte,>                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZddlmZmZ ddl	m
Z ddlmZ edZedZdadadd	 ZdddZG dd dZdd ZG dd dejZdddZeejee eeje eejddg eejd dS )    N   )Image	ImageFile)i32le)	deprecatez^%%([^:]*):[ \t]*(.*)[ \t]*$z^%[%!\w]([^:]*)[ \t]*$c                  C   s   t d u rJtjdr*td u r$dd l} dD ]}| |d ur!|a nqdata t duS ztjddgtj	d da W t duS  t
yI   da Y t duS w t duS )Nwinr   )gswin32cgswin64cgsFr
   z	--version)stdout)	gs_binarysysplatform
startswithgs_windows_binaryshutilwhich
subprocess
check_callDEVNULLOSError)r   binary r   ^/var/www/social.2tendo.club/virtuCedar/venv/lib/python3.10/site-packages/PIL/EpsImagePlugin.pyhas_ghostscript,   s*   r   Fc                 C   s  t  s	d}t|| d \}} }}|\}	}
t|pd}|d | |d | f}d|d  |
d |
d   d|d  |
d |
d   f}t \}}t| d}t|dr`tj	|j
r`|j
}nRt \}}t| |}t|d	8}|dtj | }|d |}|dkr|t|d
}|sn|t|8 }|| |dksW d   n1 sw   Y  |rdnd}tdd| d| dddd| d| d|
d   d|
d   dd|ddg}z?d}tjdrt }| jtjO  _tj||d t|}|  W zt| |rt| W n& ty   Y nw zt| |r/t| W w W w  ty:   Y w w |j ! }|  |S )z!Render an image using Ghostscriptz%Unable to locate Ghostscript on pathsr   r   g      R@      Nnamewbi  pngalphappmrawz-qz-g%dx%dz-r%fx%fz-dBATCHz	-dNOPAUSEz-dSAFERz	-sDEVICE=z-sOutputFile=z-c z
 translatez-fshowpager   )startupinfo)"r   r   inttempfilemkstemposclosehasattrpathexistsr   openseekioSEEK_ENDtellreadminlenwriter   r   r   r   r   STARTUPINFOdwFlagsSTARTF_USESHOWWINDOWr   r   loadunlinkimcopy)tilesizefpscaletransparencymsgdecoderoffsetdatalengthbboxresout_fdoutfileinfile_tempinfilein_fdffsize
lengthfilesdevicecommandr#   out_imr:   r   r   r   GhostscriptC   s   









rT   c                   @   s.   e Zd ZdZdd ZejfddZdd ZdS )	PSFilez
    Wrapper for bytesio object that treats either CR or LF as end of line.
    This class is no longer used internally, but kept for backwards compatibility.
    c                 C   s   t dddd || _d | _d S )NrU      zSIf you need the functionality of this class you will need to implement it yourself.)action)r   r>   char)selfr>   r   r   r   __init__   s   
zPSFile.__init__c                 C   s   d | _ | j|| d S N)rX   r>   r-   )rY   rC   whencer   r   r   r-      s   zPSFile.seekc                 C   s   | j pdg}d | _ | jd}|dvr*t|r*|| | jd}|dvr*t|s| jd| _ | j dv r9d | _ d|dS )N    r      
latin-1)rX   r>   r1   r3   appendjoindecode)rY   rP   cr   r   r   readline   s   

zPSFile.readlineN)	__name__
__module____qualname____doc__rZ   r.   SEEK_SETr-   rd   r   r   r   r   rU      s
    
rU   c                 C   s(   | d d dkpt | dkot| dkS )N      %!PS   P )r3   i32)prefixr   r   r   _accept   s   (ro   c                   @   sH   e Zd ZdZdZdZdddddZd	d
 Zdd ZdddZ	dd Z
dS )EpsImageFilez.EPS File Parser for the Python Imaging LibraryEPSzEncapsulated PostscriptLLABRGBCMYK)r   r   r   rj   c                    s   j\ j d_d _td}t|}d}d}ddfdd} fdd	}	 jd
}|dkrE|dkrDnn0|dv rO|dkrNq4n&|dkrj|d tdkrad}t	||rh|  d}d}|d ||< |d
7 }q4|r|d tdks|d d dkr|  d}q4t
|d | d}	||	st|	}
|
r|
d
}|d d dkr|dd  jd< ndj|< n|	d dkrnzd}t|ns|d d dkr|d| d d}dd |d d D \}}}}|d
krd_n|dkrzj| _W n ty   Y n5w n3||f_d S r4r4|d d dkr&nt
|d | d}	||	 n|d d d kr?dd}q5|  jsOd!}t|d S )"Nrt      r   TFc                     s0   d j vrd} t| d j vrd} t| d S )NPS-Adobez'EPS header missing "%!PS-Adobe" commentBoundingBoxz*EPS header missing "%%BoundingBox" comment)infoSyntaxError)rA   )rY   r   r   check_required_header_comments   s   

z:EpsImageFile._open.<locals>.check_required_header_commentsc              
      s   zt | }W n tjy } zd}t||d }~ww |ru|dd\}}|j|< |dkrs|dkr7ddS jr>rsrsz*dd |  D }|d |d	  |d
 |d  f_ddj  |ffg_	W dS  t
yr   Y dS w dS d S )Nnot an EPS filer   r   rx   z(atend)Tc                 S   s   g | ]}t t|qS r   )r$   float).0ir   r   r   
<listcomp>  s    z=EpsImageFile._open.<locals>._read_comment.<locals>.<listcomp>r   r   epsr   r   )splitmatchreerrorrz   groupry   _sizer=   r<   	Exception)rP   merA   kvboxrE   rC   reading_trailer_commentsrY   trailer_reachedr   r   _read_comment   s>   

"z)EpsImageFile._open.<locals>._read_commentr   r]   r^   %r|      s   %%EndCommentsr_      rw   	    zbad EPS headerrV   s   %ImageData:   c                 s   s    | ]}t |V  qd S r[   )r$   )r~   valuer   r   r   	<genexpr>Y  s    
z%EpsImageFile._open.<locals>.<genexpr>rj   1   s   %%EOFs	   %%Trailerz!cannot determine EPS bounding box)_find_offsetr>   r-   _moder   	bytearray
memoryviewr1   ordrz   strfieldr   r   ry   r   r   mode_map
ValueError)rY   byte_arrbytes_mv
bytes_readreading_header_commentsr{   r   byterA   rP   r   r   image_data_valuescolumnsrows	bit_depthmode_idr   r   r   _open   s    





hzEpsImageFile._openc                 C   sp   | d}|dkr|dtj | }d}||fS t|dkr2| d}t|}t|d}||fS d}t|)Nrj   rk   r   rl   r   r|   )r1   r-   r.   r/   r0   rm   rz   )rY   r>   rP   rE   rC   rA   r   r   r   r   }  s   


zEpsImageFile._find_offsetr   Fc                 C   sD   | j rt| j | j| j||| _| jj| _| jj| _g | _ tj	| S r[   )
r<   rT   r=   r>   r:   moder   r   r   r8   )rY   r?   r@   r   r   r   r8     s   

zEpsImageFile.loadc                 O   s   d S r[   r   )rY   argskwargsr   r   r   	load_seek  s   zEpsImageFile.load_seekNr   F)re   rf   rg   rh   formatformat_descriptionr   r   r   r8   r   r   r   r   r   rp      s     %
	rp   c                 C   s  |    | jdkrd}n| jdkrd}n| jdkrd}nd}t||rT|d |d	 |d
| j  |d |d |d |d| j  |d|  |d |d |d| jd |d    |d| j  |d| j  |d| jd | jd | jd f  |d ||d d  t|dr|  t| |dd| j ddfg |d |d  t|dr|  dS dS )!z*EPS Writer for the Python Imaging Library.rr   )r   r   s   imagert   )r   r   s   false 3 colorimageru   )r   rj   s   false 4 colorimagezimage mode is not supporteds   %!PS-Adobe-3.0 EPSF-3.0
s   %%Creator: PIL 0.1 EpsEncode
s   %%%%BoundingBox: 0 0 %d %d
s   %%Pages: 1
s   %%EndComments
s   %%Page: 1 1
s   %%ImageData: %d %d s   %d %d 0 1 1 "%s"
s   gsave
s   10 dict begin
s   /buf %d string def
r   r   s   %d %d scale
s   %d %d 8
s   [%d 0 0 -%d 0 %d]
s+   { currentfile buf readhexstring pop } bind
r      
flushr   r   Ns   
%%%%EndBinary
s   grestore end
)	r8   r   r   r4   r=   r)   r   r   _save)r:   r>   filenamer   operatorrA   r   r   r   r     sD   









&




r   z.psz.epszapplication/postscriptr   )r   )r.   r'   r   r   r   r%   r   r   r   _binaryr   rm   
_deprecater   compiler   r   r   r   r   rT   rU   ro   rp   r   register_openr   register_saveregister_extensionsregister_mimer   r   r   r   <module>   s0   


`%	 
V4