ó
î Xc           @  sL  d  d l  m Z d  d l m Z m Z d  d l Z d  d l Z d  d l Z d „  Z d d d d d	 d
 g Z	 d „  Z
 d „  Z d e j f d „  ƒ  YZ d d „ Z d „  Z d „  Z d „  Z e j d e ƒ e j d e ƒ e d k rHe j d se d ƒ e j ƒ  n  e j d Z e e ƒ s=e d ƒ e j ƒ  n  d Z e e j d ƒ d k rle j d Z n  e j e ƒ Z e d e e ƒ ƒ e d e e j ƒ ƒ e d e e j ƒ ƒ e d e e j  ƒ ƒ e d d d ƒe e j! ƒ  ƒ e d k rHe j" e j# ƒ Z e d  e j$ j% e ƒ e f ƒ e j& e d ƒ qHn  d S(!   iÿÿÿÿ(   t   print_function(   t   Imaget	   ImageFileNc         C  s7   y( t  |  ƒ } |  | d k r# d Sd SWn d SXd  S(   Ni    i   (   t   int(   t   ft   i(    (    s9   /usr/lib/python2.7/dist-packages/PIL/SpiderImagePlugin.pyt   isInt,   s    i   i   iõÿÿÿiôÿÿÿiëÿÿÿiêÿÿÿc         C  s¬   d
 |  } x7 d d d d d d d g D] } t  | | ƒ s& d	 Sq& Wt | d ƒ } | t k rd d	 St | d ƒ } t | d ƒ } t | d ƒ } | | | k r¨ d	 S| S(   Nic   i   i   i   i   i   i   i   i    (   ic   (   R   R   t   iforms(   t   tt   hR   t   iformt   labrect   labbytt   lenbyt(    (    s9   /usr/lib/python2.7/dist-packages/PIL/SpiderImagePlugin.pyt   isSpiderHeader>   s    
"c         C  sw   t  |  d ƒ } | j d ƒ } | j ƒ  t j d | ƒ } t | ƒ } | d k rs t j d | ƒ } t | ƒ } n  | S(   Nt   rbi\   s   >23fi    s   <23f(   t   opent   readt   closet   structt   unpackR   (   t   filenamet   fpR   R   t   hdrlen(    (    s9   /usr/lib/python2.7/dist-packages/PIL/SpiderImagePlugin.pyt   isSpiderImageS   s    
t   SpiderImageFilec           B  sD   e  Z d  Z d Z d „  Z d „  Z d „  Z d d „ Z d „  Z RS(   t   SPIDERs   Spider 2D imagec         C  s  d } |  j  j | ƒ } y| d |  _ t j d | ƒ } t | ƒ } | d k rx d |  _ t j d | ƒ } t | ƒ } n  | d k r“ t d ƒ ‚ n  Wn  t j k
 r¶ t d ƒ ‚ n Xd | } t | d	 ƒ } | d k rì t d
 ƒ ‚ n  t | d ƒ t | d ƒ f |  _	 t | d ƒ |  _
 t | d ƒ |  _ |  j
 d k re|  j d k re| } d |  _ n¸ |  j
 d k rÚ|  j d k rÚt | d ƒ t | d ƒ d |  _ | |  _ t | d ƒ |  _ | d } d |  _ nC |  j
 d k r|  j d k r| |  j } d |  _
 n t d ƒ ‚ |  j r2d |  _ n	 d |  _ d |  _ d d |  j	 | |  j d d f f g |  _ |  j  |  _ d  S(   Ni   i   i   s   >27fi    s   <27fs   not a valid Spider fileic   i   s   not a Spider 2D imagei   i   i   i   s    inconsistent stack header valuess   F;32BFs   F;32Ft   Ft   rawil   (   ic   (   i    i    (   R   R   t	   bigendianR   R   R   t   SyntaxErrort   errorR   t   sizet   istackt	   imgnumbert   nimagest   imgbytesR   t	   stkoffsett   rawmodet   modet   tilet   _SpiderImageFile__fp(   t   selft   nR   R   R   R	   R
   t   offset(    (    s9   /usr/lib/python2.7/dist-packages/PIL/SpiderImagePlugin.pyt   _opend   sR    		
#%	
			c         C  s"   |  j  d k  r d S|  j  d Sd  S(   Ni   i    (   R"   (   R*   (    (    s9   /usr/lib/python2.7/dist-packages/PIL/SpiderImagePlugin.pyt   tellž   s    c         C  s|   |  j  d k r d  S| |  j k r1 t d ƒ ‚ n  |  j | |  j |  j |  _ |  j |  _ |  j j |  j ƒ |  j	 ƒ  d  S(   Ni    s    attempt to seek past end of file(
   R!   R#   t   EOFErrorR   R$   R%   R)   R   t   seekR-   (   R*   t   frame(    (    s9   /usr/lib/python2.7/dist-packages/PIL/SpiderImagePlugin.pyR0   ¤   s    iÿ   c         C  s_   |  j  ƒ  \ } } d } | | k r5 | | | } n  | | } |  j | | d „ ƒ j d ƒ S(   Ni   c         S  s   |  | | S(   N(    (   R   t   mt   b(    (    s9   /usr/lib/python2.7/dist-packages/PIL/SpiderImagePlugin.pyt   <lambda>µ   s    t   L(   t
   getextremat   pointt   convert(   R*   t   deptht   mint   maxR2   R3   (    (    s9   /usr/lib/python2.7/dist-packages/PIL/SpiderImagePlugin.pyt   convert2byte¯   s    c         C  s)   d d l  m } | j |  j ƒ  d d ƒS(   Niÿÿÿÿ(   t   ImageTkt   palettei   (   t   PILR=   t
   PhotoImageR<   (   R*   R=   (    (    s9   /usr/lib/python2.7/dist-packages/PIL/SpiderImagePlugin.pyt   tkPhotoImage¸   s    (	   t   __name__t
   __module__t   formatt   format_descriptionR-   R.   R0   R<   RA   (    (    (    s9   /usr/lib/python2.7/dist-packages/PIL/SpiderImagePlugin.pyR   _   s   	:			c         C  sÀ   |  d k s t |  ƒ d k  r" d Sg  } x‘ |  D]‰ } t j j | ƒ s[ t d | ƒ q/ n  y t j | ƒ j ƒ  } Wn' t	 | ƒ s/ t | d ƒ q/ q/ n X| | j
 d <| j | ƒ q/ W| S(   s2    create a list of Image.images for use in montage i   Ns   unable to find %ss    is not a Spider image fileR   (   t   Nonet   lent   ost   patht   existst   printR   R   R<   R   t   infot   append(   t   filelistt   imglistt   imgt   im(    (    s9   /usr/lib/python2.7/dist-packages/PIL/SpiderImagePlugin.pyt   loadImageSeriesÁ   s     c         C  sI  |  j  \ } } | d } d | } d | d k r@ | d 7} n  | | } g  } t | d ƒ } x! t | ƒ D] } | j d ƒ qm Wt | ƒ d k  rš g  Sd | d <t | ƒ | d <d | d	 <t | ƒ | d
 <t | ƒ | d <t | ƒ | d <t | ƒ | d <| d } | j d ƒ g  }	 x' | D] }
 |	 j t j d |
 ƒ ƒ q"W|	 S(   Ni   i   i    i   g        i   g      ð?i   i   i   i   i   R   (   R    R   t   rangeRM   RG   t   floatR   t   pack(   RQ   t   nsamt   nrowR   R   R   t   hdrt   nvaluesR   t   hdrstrt   v(    (    s9   /usr/lib/python2.7/dist-packages/PIL/SpiderImagePlugin.pyt   makeSpiderHeaderÙ   s2    





c      	   C  sÒ   |  j  d d k r% |  j d ƒ }  n  t |  ƒ } t | ƒ d k  rR t d ƒ ‚ n  y t | d ƒ } Wn t d | ƒ ‚ n X| j | ƒ d } t j |  | d d
 |  j	 d | d d	 f f g ƒ | j
 ƒ  d  S(   Ni    R   i   s   Error creating Spider headert   wbs   Unable to open %s for writings   F;32NFR   i   (   i    i    (   R'   R8   R\   RG   t   IOErrorR   t
   writelinesR   t   _saveR    R   (   RQ   R   R   RX   R&   (    (    s9   /usr/lib/python2.7/dist-packages/PIL/SpiderImagePlugin.pyR`   û   s    2c         C  s<   t  j j | ƒ \ } } t j d | ƒ t |  | | ƒ d  S(   NR   (   RH   RI   t   splitextR   t   register_extensionR`   (   RQ   R   R   t   fnt   ext(    (    s9   /usr/lib/python2.7/dist-packages/PIL/SpiderImagePlugin.pyt   _save_spider  s    R   t   __main__s9   Syntax: python SpiderImagePlugin.py Spiderimage [outfile]s$   input image must be in Spider formatt    i   s   image: s   format: s   size: s   mode: s
   max, min: t   endt    s%   saving a flipped version of %s as %s ('   t
   __future__R    R?   R   R   RH   R   t   sysR   R   R   R   R   RF   RR   R\   R`   Re   t   register_opent   register_saveRB   t   argvRK   t   exitR   t   outfileRG   R   RQ   t   strRD   R    R'   R6   t	   transposet   FLIP_LEFT_RIGHTRI   t   basenamet   save(    (    (    s9   /usr/lib/python2.7/dist-packages/PIL/SpiderImagePlugin.pyt   <module>$   sN   	
		b	"		

