
 XL@                 @   s  d  d l  m Z d  d l m Z d  d l Z d  d l Z d  d l Z d  d l Z d Z d Z	 d Z
 i d d  6d	 d! 6d d" 6d d# 6d d$ 6Z d d   Z d d   Z Gd d   d e j  Z Gd d   d e  Z Gd d   d e  Z d  d d  Z d d   Z d S)%    )Image)isPathNi   i   Fzimage buffer overrun error   zdecoding error   zunknown error   zbad configuration   zout of memory error	   c             C   sa   y t  j j |   } Wn! t k
 r9 t j |   } Yn X| sM d |  } n  t | d   d  S)Nzdecoder error %dz when reading image file)r   coreZgetcodecstatusAttributeErrorERRORSgetIOError)errormessage r   J/home/ubuntu/projects/ifolica/lib/python3.4/site-packages/PIL/ImageFile.pyraise_ioerror4   s    r   c             C   s   |  d S)Nr   r   )tr   r   r   	_tilesortB   s    r   c               @   sd   e  Z d  Z d Z d d d d  Z d d   Z d d   Z d	 d
   Z d d   Z d d   Z	 d S)	ImageFilez*Base class for image file format handlers.Nc             C   s   t  j  j |   d  |  _ d |  _ f  |  _ t |  _ t |  r^ t | d  |  _	 | |  _
 n | |  _	 | |  _
 y |  j   WnC t t t t t j f k
 r } z t |   WYd  d  } ~ Xn X|  j s |  j d d k r t d   n  d  S)Nr   rbr   znot identified by this driver)r   __init__tilereadonlydecoderconfigMAXBLOCKdecodermaxblockr   openfpfilename_open
IndexError	TypeErrorKeyErrorEOFErrorstructr   SyntaxErrormodesize)selfr   r   vr   r   r   r   N   s(    						zImageFile.__init__c             C   s   d S)zSet draft modeNr   )r)   r'   r(   r   r   r   draftl   s    zImageFile.draftc             C   s   d |  _  d S)zCheck file integrityN)r   )r)   r   r   r   verifyq   s    zImageFile.verifyc          5   C   s  t  j  j |   } |  j d k r0 t d   n  |  j s= | Sd |  _ |  j oa t |  j  d k } | ow t t d  } d } y |  j	 } d } Wn t
 k
 r |  j j } Yn Xy |  j } d } Wn t
 k
 r |  j j } Yn X| rr|  j d \ } } } }	 | d k rr|	 d |  j k rr|	 d t  j k rryt t  j d  rt  j j |  j  |  _ |  j j |  |  j j |  j |  j |	 d |	 d	  |  _ n d d l }
 t |  j d
  } t j j |  j  } |
 j | j   | d |
 j |  _ t  j j |  j |  j | | | |	  |  _ d } |  j rDd |  j _ n  Wqot
 t t  f k
 rkd |  _ YqoXqrn  |  j!   |  j sO|  j j" d t#  y |  j$ } Wn t
 k
 rd } Yn Xx|  j D]~\ } } } } t  j% |  j | | |  j&  } | |  y | j' |  j |  Wn t( k
 r2wYn X| j) rd| j* |  j  | j+ d  \ } } n | } x y | |  j,  } Wn1 t- t. j/ f k
 rt0 rPn t d   Yn X| r| j1 rg  |  _ | j2   t0 rPqt d t |    n  | | } | j+ |  \ } } | d k  r*Pn  | | d  } qmW| j2   qWn  g  |  _ | |  _3 d |  _ |  j rt0 r| d k  rt4 |  n  t |  d  r|  j j5 |  j6  |  _ |  j j |  _ n  |  j7   t  j  j |   S)z"Load image data based on tile listNzcannot load this imager   pypy_version_infor   Frawmapr   raccesskey    zimage file is truncatedz0image file is truncated (%d bytes not processed)tile_post_rotate)8r   loadr   r   r/   r   lenhasattrsys	load_readr
   r   read	load_seekseekr'   Z	_MAPMODESr	   Z	readimager(   immmapr   ospathgetsizefilenoZACCESS_READZ
map_bufferZpaletteZdirtyEnvironmentErrorImportErrorload_preparesortr   Ztile_prefix_getdecoderr   setimage
ValueErrorZpulls_fdsetfddecoder   r!   r%   r   LOAD_TRUNCATED_IMAGESZhandles_eofcleanupr   r   rotater4   load_end)r)   ZpixelZuse_mmapr   r:   r<   deoar>   r   r(   prefixZdecoder_nameZextentsoffsetargsdecoderstatusZerr_codebsnr   r   r   r5   x   s    			
	
2	&$	!	
	
		

			
zImageFile.loadc             C   s{   |  j  s4 |  j  j |  j k s4 |  j  j |  j k rU t j j |  j |  j  |  _  n  |  j d k rw t j j |   n  d  S)NP)r=   r'   r(   r   r	   newr5   )r)   r   r   r   rE      s
    
*!zImageFile.load_preparec             C   s   d  S)Nr   )r)   r   r   r   rO     s    zImageFile.load_end)
__name__
__module____qualname____doc__r   r+   r,   r5   rE   rO   r   r   r   r   r   K   s   	r   c               @   s:   e  Z d  Z d Z d d   Z d d   Z d d   Z d S)	StubImageFilez
    Base class for stub image loaders.

    A stub loader is an image loader that can identify files of a
    certain format, but relies on external code to load the file.
    c             C   s   t  d   d  S)Nz+StubImageFile subclass must implement _open)NotImplementedError)r)   r   r   r   r      s    zStubImageFile._openc             C   sk   |  j    } | d  k r. t d |  j   n  | j |   } | d  k	 sO t  | j |  _ | j |  _ d  S)Nz#cannot find loader for this %s file)_loadr   formatr5   AssertionError	__class____dict__)r)   loaderimager   r   r   r5   "  s    zStubImageFile.loadc             C   s   t  d   d S)z (Hook) Find actual image loader.z+StubImageFile subclass must implement _loadN)rc   )r)   r   r   r   rd   ,  s    zStubImageFile._loadN)r^   r_   r`   ra   r    r5   rd   r   r   r   r   rb     s   
rb   c               @   s^   e  Z d  Z d Z d Z d Z d Z d Z d Z d Z	 d d   Z
 d d   Z d d	   Z d S)
Parserzj
    Incremental image parser.  This class implements the standard
    feed/close consumer interface.
    Nr   c             C   s   |  j  d k s t d   d S)z
        (Consumer) Reset the parser.  Note that you can only call this
        method immediately after you've created a parser; parser
        instances cannot be reused.
        Nzcannot reuse parsers)datarf   )r)   r   r   r   reset?  s    zParser.resetc             C   s  |  j  r d S|  j d k r( | |  _ n |  j | |  _ |  j r,|  j d k r t t |  j  |  j  } |  j | d  |  _ |  j | |  _ |  j d k s |  j r d Sn  |  j j |  j  \ } } | d k  rd |  _ d |  _  | d k  rd |  _ t |  qd Sn  |  j | d  |  _ nO|  j r8nCy7 z% t	 j
 |  j  } t j |  } Wd | j   XWn t k
 rYn Xt | d  pt | d  } | st | j  d k rd |  _ n | j   | j d \ } } }	 }
 g  | _ t j | j | |
 | j  |  _ |  j j | j |  |	 |  _ |  j t |  j  k rr|  j |  j d  |  _ d |  _ n  | |  _ d S)z
        (Consumer) Feed data to the parser.

        :param data: A string buffer.
        :exception IOError: If the parser failed to parse the image file.
        Nr   r   r;   r9   )finishedrl   rW   rU   minr6   rK   rj   r   ioBytesIOr   r   closer   r7   r   rE   rG   r'   r   rH   r=   )r)   rl   skipr[   rQ   r   r=   flagrP   rR   rS   r   r   r   feedG  sX    							
		zParser.feedc          
   C   s   |  j  rA |  j d  d |  _ |  _  |  j sA t d   qA n  |  j sY t d   n  |  j r z( t j |  j  } t j	 |  |  _ Wd |  j j
   | j   Xn  |  j S)a  
        (Consumer) Close the stream.

        :returns: An image object.
        :exception IOError: If the parser failed to parse the image file either
                            because it cannot be identified or cannot be
                            decoded.
        r3   Nzimage was incompletezcannot parse this image)rW   ru   rl   rn   r   rj   rp   rq   r   r   r5   rr   )r)   r   r   r   r   rr     s    
				zParser.close)r^   r_   r`   ra   Zincrementalrj   rl   rW   rU   rn   rm   ru   rr   r   r   r   r   rk   3  s   Urk   c             C   s  |  j    t |  d  s% f  |  _ n  | j d t  t t | |  j d d  } | t j	 k ro | j
   d Sy | j   } | j
   Wnt t j f k
 rx | D] \ } } } } t j |  j | | |  j  } | d k r | j | d  n  | j |  j |  | j r6| j |  | j   \ }	 }
 n6 x3 | j |  \ }	 }
 } | j |  |
 r9Pq9q9W|
 d k  rt d |
   n  | j   q WYn Xx | D] \ } } } } t j |  j | | |  j  } | d k r| j | d  n  | j |  j |  | j r2| j |  | j   \ }	 }
 n | j | |  }
 |
 d k  rct d |
   n  | j   qWt | d  r| j
   n  d S)zHelper to save image based on tile list

    :param im: Image object.
    :param fp: File object.
    :param tile: Tile list.
    :param bufsize: Optional buffer size
    encoderconfigr2   r      Nz(encoder error %d when writing image fileflush)r5   r7   rv   rF   r   maxr   r(   r8   stdoutrx   rB   r
   rp   UnsupportedOperationr   Z_getencoderr'   r<   rH   r=   Z	pushes_fdrJ   Zencode_to_pyfdencodewriter   rM   Zencode_to_file)r=   r   r   bufsizeZfhrQ   rY   rR   rS   lrZ   rP   r   r   r   _save  sT    	

		r   c             C   s   | d k r d S| t  k r) |  j |  Sg  } xO | d k r |  j t | t    } | s` Pn  | j |  | t |  8} q2 Wd j |  S)ao  
    Reads large blocks in a safe way.  Unlike fp.read(n), this function
    doesn't trust the user.  If the requested size is larger than
    SAFEBLOCK, the file is read block by block.

    :param fp: File handle.  Must implement a <b>read</b> method.
    :param size: Number of bytes to read.
    :returns: A string containing up to <i>size</i> bytes of data.
    r   r3   )	SAFEBLOCKr:   ro   appendr6   join)r   r(   rl   blockr   r   r   
_safe_read  s    
r   i   ii)ZPILr   Z	PIL._utilr   rp   r?   r8   r%   r   r   rL   r   r   r   r   rb   objectrk   r   r   r   r   r   r   <module>   s*   
	>