3
V^6/  ã               @   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 ejdƒZejdƒZdZejjdƒržd dlZx$dD ]Zejeƒdk	r~eZP q~W dZ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jejeeƒ ejejeƒ ejejddgƒ ejejdƒ dS )é    Né   )ÚImageÚ	ImageFile)Úi32lez^%%([^:]*):[ \t]*(.*)[ \t]*$z^%[%!\w]([^:]*)[ \t]*$ÚwinÚgswin32cÚgswin64cÚgsFc               C   sF   t rdS tjjdƒsBytjddgtjd� dS  tk
r@   Y nX dS )NTr   r	   z	--version)ÚstdoutF)Úgs_windows_binaryÚsysÚplatformÚ
startswithÚ
subprocessÚ
check_callÚDEVNULLÚOSError© r   r   ú4/tmp/pip-build-zn8x4q5s/pillow/PIL/EpsImagePlugin.pyÚhas_ghostscript3   s    r   c             C   s>  | d \}} }}|\}}t |ƒp"d}|d | |d | f}td|d  |d |d   ƒtd|d  |d |d   ƒf}	tjƒ \}
}tj|
ƒ d}t|dƒr¶tjj|j	ƒr¶|j	}n”tjƒ \}}tj|ƒ |}t
|dƒ�j}|jdtjƒ |jƒ }|jdƒ |}x>|dk�r>|jt|dƒƒ}|�s$P |t|ƒ8 }|j|ƒ �qW W dQ R X ddd| d|	 ddddd| dd|d  |d  f d|ddg}tdk	�r¬t�s¤tdƒ‚t|d< zNd}tjjdƒ�rØtjƒ }| jtjO  _tj||d� tj
|ƒ}|jƒ  W dytj|ƒ |�rtj|ƒ W n tk
�r0   Y nX X |j j!ƒ S )z!Render an image using Ghostscriptr   r   g      R@é   é   NÚnameÚwbéd   i   r	   z-qz-g%dx%dz-r%fx%fz-dBATCHz	-dNOPAUSEz-dSAFERz-sDEVICE=ppmrawz-sOutputFile=%sz-cz%d %d translatez-fZshowpagez%Unable to locate Ghostscript on pathsr   )Ústartupinfoi � )"ÚintÚfloatÚtempfileÚmkstempÚosÚcloseÚhasattrÚpathÚexistsr   ÚopenÚseekÚioÚSEEK_ENDÚtellÚreadÚminÚlenÚwriter   r   r   r   r   r   ZSTARTUPINFOZdwFlagsZSTARTF_USESHOWWINDOWr   r   ÚloadÚunlinkÚimÚcopy)ÚtileÚsizeÚfpÚscaleÚdecoderÚoffsetÚdataÚlengthZbboxÚresZout_fdÚoutfileZinfile_tempÚinfileZin_fdÚfÚfsizeZ
lengthfileÚsÚcommandr   r0   r   r   r   ÚGhostscript@   sv    "





rA   c               @   s.   e Zd ZdZdd„ Zejfdd„Zdd„ ZdS )	ÚPSFilezP
    Wrapper for bytesio object that treats either CR or LF as end of line.
    c             C   s   || _ d | _d S )N)r4   Úchar)Úselfr4   r   r   r   Ú__init__¢   s    zPSFile.__init__c             C   s   d | _ | jj||ƒ d S )N)rC   r4   r&   )rD   r7   Úwhencer   r   r   r&   ¦   s    zPSFile.seekc             C   sf   | j pd}d | _ | jjdƒ}x |dkr<|| }| jjdƒ}qW | jjdƒ| _ | j dkr\d | _ |jdƒS )Nó    r   s   
zlatin-1)rC   r4   r*   Údecode)rD   r?   Úcr   r   r   Úreadlineª   s    


zPSFile.readlineN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__rE   r'   ÚSEEK_SETr&   rJ   r   r   r   r   rB   �   s   rB   c             C   s(   | d d… dkp&t | ƒdko&t| ƒdkS )Né   s   %!PSl   ÅP§ )r,   Úi32)Úprefixr   r   r   Ú_accept»   s    rS   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 LibraryZEPSzEncapsulated PostscriptÚLZLABÚRGBÚCMYK)r   r   r   rP   c             C   s´  | j | jƒ\}}t| jƒ}|j|ƒ d }d| _d| _|jƒ }|jdƒ}�xˆ|�rÐ|�r¦t|ƒdkrjt	dƒ‚yt
j|ƒ}W n tjk
r–   t	dƒ‚Y nX |�r.|jddƒ\}}	|	| j|< |dk�r¦yRdd	„ |	j
ƒ D ƒ}|d |d
  |d |d  f| _dd| j |||ffg| _W n tk
�r*   Y nX nxtj|ƒ}|�rŽ|jdƒ}|dk�rTP |d d… dk�r‚|dd … | j|d d… < n
d| j|< n|d
 dk�ržntdƒ‚|jƒ }|jdƒ}|rJ|d d… dkrJP qJW xÎ|d d… dk�r t|ƒdk�rüt	dƒ‚|d d… dk�r†|dd … j
d dƒd d… \}
}}}t|ƒdk�rBP y| jt|ƒ | _W n tk
�rn   P Y nX t|
ƒt|ƒf| _d S |jƒ jdƒ}|�sÔP �qÔW |�s°tdƒ‚d S )NrV   r   z
éÿ   znot an EPS filer   ZBoundingBoxc             S   s   g | ]}t t|ƒƒ‘qS r   )r   r   )Ú.0Úir   r   r   ú
<listcomp>ó   s    z&EpsImageFile._open.<locals>.<listcomp>r   r   ÚepsZEndCommentsé   zPS-Adobeé	   Ú ú%zbad EPS headeré   z%ImageData:é   rP   z!cannot determine EPS bounding box)r   r   )r   r   )Ú_find_offsetr4   rB   r&   ÚmodeÚ_sizerJ   Ústripr,   ÚSyntaxErrorÚsplitÚmatchÚreÚerrorÚgroupÚinfor3   r2   Ú	ExceptionÚfieldr   r   Úmode_mapÚ
ValueError)rD   r9   r7   r4   ZboxZs_rawr?   ÚmÚkÚvÚxÚyZbiÚmor   r   r   Ú_openÌ   sv    





"



$zEpsImageFile._openc             C   s|   |j dƒ}|d d… dkr6|jdtjƒ |jƒ }d}n>t|dd… ƒdkrlt|dd… ƒ}t|dd… ƒ}ntdƒ‚||fS )	Né    rP   s   %!PSr   l   ÅP§ r]   é   znot an EPS file)r*   r&   r'   r(   r)   rQ   rg   )rD   r4   r?   r9   r7   r   r   r   rc   0  s    
zEpsImageFile._find_offsetr   c             C   s>   | j s
d S t| j | j| j|ƒ| _| jj| _| jj| _g | _ d S )N)r2   rA   r3   r4   r0   rd   re   )rD   r5   r   r   r   r.   F  s    

zEpsImageFile.loadc             O   s   d S )Nr   )rD   ÚargsÚkwargsr   r   r   Ú	load_seekO  s    zEpsImageFile.load_seekN)r   )rK   rL   rM   rN   ÚformatÚformat_descriptionrp   rx   rc   r.   r}   r   r   r   r   rT   Ä   s   d
	rT   c             C   sÐ  | j ƒ  | jdkrd'}n(| jdkr(d(}n| jdkr8d)}ntdƒ‚|}d}|tjkrdtj|dd�}d}�zT|rÌ|jdƒ |jdƒ |jd| j ƒ |jdƒ |jdƒ |jdƒ |jd| j ƒ |jd| ƒ |jdƒ |jdƒ |jd| jd |d   ƒ |jd| j ƒ |jd| j ƒ |jd| jd | jd | jd f ƒ |jdƒ |j|d  d! ƒ t	|d"ƒ�rr|j
ƒ  tj| |d#d*| j dd$fgƒ |jd%ƒ |jd&ƒ t	|d"ƒ�r¸|j
ƒ  W d$|�rÊ|jƒ  X d$S )+z*EPS Writer for the Python Imaging Library.rU   r]   r   ÚimagerV   r   úfalse 3 colorimagerW   rP   úfalse 4 colorimagezimage mode is not supportedFzlatin-1)ÚencodingTz%!PS-Adobe-3.0 EPSF-3.0
z%%Creator: PIL 0.1 EpsEncode
z%%%%BoundingBox: 0 0 %d %d
z%%Pages: 1
z%%EndComments
z%%Page: 1 1
z%%ImageData: %d %d z%d %d 0 1 1 "%s"
zgsave
z10 dict begin
z/buf %d string def
r   z%d %d scale
z%d %d 8
z[%d 0 0 -%d 0 %d]
z+{ currentfile buf readhexstring pop } bind
r   Ú
Úflushr\   Nz
%%%%EndBinary
zgrestore end
)r]   r   r€   )r]   r   r�   )r]   rP   r‚   )r   r   )r.   rd   rq   r   r
   r'   ÚTextIOWrapperr-   r3   r"   r…   r   Ú_saveÚdetach)r0   r4   Úfilenamer\   ÚoperatorZbase_fpZ
wrapped_fpr   r   r   r‡   Y  sP    










&


r‡   z.psz.epszapplication/postscript)r   r   r	   )r   )r   ) r'   r    rj   r   r   r   r_   r   r   Ú_binaryr   rQ   Úcompilerh   ro   r   r   r   ÚshutilÚbinaryÚwhichr   rA   rB   rS   rT   r‡   Zregister_openr~   Zregister_saveZregister_extensionsZregister_mimer   r   r   r   Ú<module>   s8   



]	 
B