
&"V              
   @   s  d  d l  m Z m Z d  d l m Z m Z d  d l m Z d  d l m Z d  d l m Z d  d l	 Z	 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  d l Z d  d l Z d  d l Z d	 d
 l m Z m Z d Z d Z d Z d Z d Z d Z d Z e j Z e j Z d Z  d Z! d Z" d Z# d Z$ d Z% d Z& d Z' d Z( d Z) d Z* d Z+ d Z, d Z- d Z. d Z/ d  Z0 d! Z1 d" Z2 d# Z3 d$ Z4 d% Z5 d& Z6 d' Z7 d( Z8 d) Z9 d* Z: d+ Z; d, Z< d- Z= d. Z> d/ Z? i d0 d	 6d1 d2 6d3 d4 6d5 d6 6d7 d8 6d9 d: 6d; d< 6d= d> 6d? d@ 6dA dB 6dC dD 6dE dF 6dG dH 6dI dJ 6Z@ eA dK dL   e@ jB   D  ZC iE dM dN f e d  d	 f d	 d	 f f  f 6dM dN f e d  d	 f d	 d	 f f  f 6dM dO f e d  d	 f d2 d	 f f  f 6dM dO f e d  d	 f d2 d	 f f  f 6dP dQ f e d  d	 f d	 d> f f  f 6dP dQ f e d  d	 f d	 d> f f  f 6dP dR f e d  d	 f d2 d> f f  f 6dP dR f e d  d	 f d2 d> f f  f 6dS dT f e d  d4 f d	 dU f f  f 6dS dV f e d  d4 f d	 dU f f  f 6dM dM f e d	 d	 f d	 d	 f f  f 6dM dM f e d	 d	 f d	 d	 f f  f 6dP dW f e d	 d	 f d	 d6 f f  f 6dM dX f e d	 d	 f d2 d	 f f  f 6dM dX f e d	 d	 f d2 d	 f f  f 6dP dP f e d	 d	 f d	 d> f f  f 6dP dP f e d	 d	 f d	 d> f f  f 6dY dY f e d	 d	 f d	 d> d> f d2 f f 6dY dY f e d	 d	 f d	 d> d> f d2 f f 6dP dZ f e d	 d	 f d2 d> f f  f 6dP dZ f e d	 d	 f d2 d> f f  f 6d[ d\ f e d	 d	 f d	 d] f f  f 6d[ d[ f e d	 d	 f d	 d^ f f  f 6d_ d_ f e d	 d	 f d	 d^ f f  f 6d` d` f e d	 d2 f d	 d^ f f  f 6da da f e d	 d2 f d	 d^ f f  f 6db dc f e d	 d	 f d	 dU f f  f 6db dd f e d	 d2 f d	 dU f f  f 6de de f e d	 d2 f d	 dU f f  f 6dS dT f e d	 d4 f d	 dU f f  f 6dS dV f e d	 d4 f d	 dU f f  f 6df df f e d2 d	 f d	 d> d> d> f f  f 6df df f e d2 d	 f d	 d> d> d> f f  f 6df dg f e d2 d	 f d2 d> d> d> f f  f 6df dg f e d2 d	 f d2 d> d> d> f f  f 6dh dh f e d2 d	 f d	 d> d> d> d> f f  f 6dh dh f e d2 d	 f d	 d> d> d> d> f f  f 6di di f e d2 d	 f d	 d> d> d> d> f d  f f 6di di f e d2 d	 f d	 d> d> d> d> f d  f f 6dh dj f e d2 d	 f d	 d> d> d> d> f d	 f f 6dh dj f e d2 d	 f d	 d> d> d> d> f d	 f f 6dh dh f e d2 d	 f d	 d> d> d> d> f d2 f f 6dh dh f e d2 d	 f d	 d> d> d> d> f d2 f f 6dh dh f e d2 d	 f d	 d> d> d> d> f dk f f 6dh dh f e d2 d	 f d	 d> d> d> d> f dk f f 6dl dm f e d4 d	 f d	 d	 f f  f 6dl dm f e d4 d	 f d	 d	 f f  f 6dl dn f e d4 d	 f d2 d	 f f  f 6dl dn f e d4 d	 f d2 d	 f f  f 6dl do f e d4 d	 f d	 d2 f f  f 6dl do f e d4 d	 f d	 d2 f f  f 6dl dp f e d4 d	 f d2 d2 f f  f 6dl dp f e d4 d	 f d2 d2 f f  f 6dl dq f e d4 d	 f d	 d6 f f  f 6dl dq f e d4 d	 f d	 d6 f f  f 6dl dr f e d4 d	 f d2 d6 f f  f 6dl dr f e d4 d	 f d2 d6 f f  f 6dl dl f e d4 d	 f d	 d> f f  f 6dl dl f e d4 d	 f d	 d> f f  f 6ds ds f e d4 d	 f d	 d> d> f d2 f f 6ds ds f e d4 d	 f d	 d> d> f d2 f f 6dl dt f e d4 d	 f d2 d> f f  f 6dl dt f e d4 d	 f d2 d> f f  f 6du du f e d8 d	 f d	 d> d> d> d> f f  f 6du du f e d8 d	 f d	 d> d> d> d> f f  f 6dv dv f e d: d	 f d	 d> d> d> f f  f 6dv dv f e d: d	 f d	 d> d> d> f f  f 6dw dw f e d> d	 f d	 d> d> d> f f  f 6dw dw f e d> d	 f d	 d> d> d> f f  f 6ZD dx dy dz g ZE d{ d|   ZF d} d~   ZG i  ZH i  ZI Gd d   d e  ZJ Gd d   d e	 jK  ZL eH eL _H eI eL _I x` e jB   D]R \ ZM ZN eN jO d d  ZN eP eL d eN eH eM d	  eP eL d eN eI eM  qW[H [I [M [N Gd d   d eL  ZQ eQ ZR Gd d   d e j  ZS i dM e d	 d	 d	 f d f dM 6dP e d	 d	 d> f d f dP 6dY e d	 d	 d> d> f d2 f dY 6dl e d4 d	 d> f d f dl 6ds e d4 d	 d> d> f d2 f ds 6dd e d	 d2 dU f d f db 6d[ e d	 d	 d^ f d f d[ 6d` e d	 d2 d^ f d f d` 6dT e d	 d4 dU f d f dS 6df e d2 d	 d> d> d> f d f df 6di e d2 d	 d> d> d> d> f d  f di 6dh e d2 d	 d> d> d> d> f d2 f dh 6du e d8 d	 d> d> d> d> f d f du 6dv e d: d	 d> d> d> f d f dv 6dw e d> d	 d> d> d> f d f dw 6de e d	 d2 dU f d f de 6d_ e d	 d	 d^ f d f d_ 6da e d	 d2 d^ f d f da 6dV e d	 d4 dU f d f dV 6ZT d d   ZU e jV eS jW eS eF  e jX eS jW eU  e jY eS jW d  e jY eS jW d  e jZ eS jW d  d S)    )divisionprint_function)Image	ImageFile)ImagePalette)_binary)TiffTagsN)Fraction)NumberRational   )TYPESTagInfoz1.3.5FTs   IIs   MM   i  i  i  i  i
  i  i  i  i  i  i  i  i  i(  i1  i2  i;  i=  i@  iD  iR  iS  i[  i  i  iI  is  ii  i  i  i  raw
tiff_ccitt   group3   group4   tiff_lzw   	tiff_jpeg   jpeg   tiff_adobe_deflate   tiff_raw_16i  packbitsi  tiff_thunderscani)  tiff_deflatei  tiff_sgilogit  tiff_sgilog24iu  c             C   s"   g  |  ] \ } } | | f  q S r%   ).0kvr%   r%   5/tmp/pip-build-fj3bmf4v/Pillow/PIL/TiffImagePlugin.py
<listcomp>   s   	 r*   1z1;Iz1;IRLzL;IzL;IRFzF;32F    zF;32BFzL;4z1;RZLAzL;RzI;16zI;12      zI;16BzI;16SzI;16BSIzI;32NzI;32SzI;32BSRGBzRGB;RZRGBAZRGBXZRGBai  PzP;1zP;1RzP;2zP;2RzP;4zP;4RZPAzP;RZCMYKZYCbCrZLABs   MM *s   II* s   II c             C   s   |  d  d  t  k S)Nr   )PREFIXES)prefixr%   r%   r)   _accept   s    r6   c             C   sR   t  |   d k } t | r% d |  n |   j |  } | rN | d  d  d  S| S)Nr   )absIFDRationallimit_rational)valZmax_valinvZn_dr%   r%   r)   _limit_rational   s    %r=   c               @   s  e  Z d  Z d Z d3 Z d d d  Z e d d	    Z e d
 d    Z d d   Z	 d d   Z
 d d   Z d d   Z d d   Z e d  Z e d  Z e d  Z e d  Z e d  Z e d  Z e d  Z e d  Z e d  Z e d  Z e d   Z e d!  Z e d"  Z e d#  Z e d$  Z e d%  Z e d&  Z e d'  Z e d(  Z  e d)  Z! e d*  Z" e d+  Z# e d,  Z$ e d-  Z% e d.  Z& e d/  Z' e d0  Z( e d1  Z) d2 S)4r9   z Implements a rational class where 0/0 is a legal value to match
    the in the wild use of exif rationals.

    e.g., DigitalZoomRatio - 0.00/0.00  indicates that no digital zoom was used
    
_numerator_denominator_valr   c             C   s  | |  _  | |  _ t d  |  _ t |  t k rW | j |  _ | j |  _  | |  _ n  t |  t k r | j |  _  | j |  _ | j |  _ d S| d k r t d  |  _ d S| d k rt	 j
 d k  r t |  t k r t j |  |  _ qt |  |  _ n t | |  |  _ d S)z
        :param value: either an integer numerator, a
        float/rational/other number, or an IFDRational
        :param denominator: Optional integer denominator
        r   Nr   nani  )r?   r>   floatr@   typer	   	numeratordenominatorr9   sys
hexversion
from_float)selfvaluerE   r%   r%   r)   __init__   s(    		!zIFDRational.__init__c             C   s   |  j  S)N)r>   )ar%   r%   r)   rD     s    zIFDRational.numeratorc             C   s   |  j  S)N)r?   )rL   r%   r%   r)   rE     s    zIFDRational.denominatorc             C   sA   |  j  d k r |  j |  j  f S|  j j |  } | j | j  f S)z
        
        :param max_denominator: Integer, the maximum denominator value
        :returns: Tuple of (numerator, denominator)
        r   )rE   rD   r@   Zlimit_denominator)rI   Zmax_denominatorfr%   r%   r)   r:      s    zIFDRational.limit_rationalc             C   s   t  t |  j   S)N)strrB   r@   )rI   r%   r%   r)   __repr__-  s    zIFDRational.__repr__c             C   s   |  j  j   S)N)r@   __hash__)rI   r%   r%   r)   rP   0  s    zIFDRational.__hash__c             C   s   |  j  | k S)N)r@   )rI   otherr%   r%   r)   __eq__3  s    zIFDRational.__eq__c                s     f d d   } | S)Nc                s   t  |  j    |   S)N)getattrr@   )rI   args)opr%   r)   delegate7  s    z'IFDRational._delegate.<locals>.delegater%   )rU   rV   r%   )rU   r)   	_delegate6  s    zIFDRational._delegate__add____radd____sub____rsub____div____rdiv____mul____rmul____truediv____rtruediv____floordiv____rfloordiv____mod____rmod____pow____rpow____pos____neg____abs__	__trunc____lt____gt____le____ge____nonzero____ceil__	__floor__	__round__N)z
_numeratorz_denominatorz_val)*__name__
__module____qualname____doc__	__slots__rK   propertyrD   rE   r:   rO   rP   rR   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   rf   rg   rh   ri   rj   rk   rl   rm   rn   ro   rp   rq   rr   rs   r%   r%   r%   r)   r9      sL   #r9   c               @   s  e  Z d  Z d Z d d d d  Z e d d    Z e d d    Z e d	 d    Z e j	 d
 d    Z d d   Z
 d d   Z d d   Z d d   Z d d   Z d d   Z d d   Z e e k r d d   Z n  d d   Z d d   Z d  d!   Z d" d#   Z d$ d%   Z d& d'   Z d( d)   Z d* d+   Z d, d-   Z e e e dc dd de df dg dh di g   e dC dC  dD dE dF   Z e dC  dG dH    Z  e dI dC  dD dJ dK   Z! e dI  dL dM    Z" e dN d7  dD dO dP   Z# e dN  dQ dR    Z$ e dS dC  dD dT dU   Z% e dS  dV dW    Z& e dX d7  dD dY dZ   Z' e dX  d[ d\    Z( d] d^   Z) d_ d`   Z* da db   Z+ d S)jImageFileDirectory_v2a|  This class represents a TIFF tag directory.  To speed things up, we
    don't decode tags unless they're asked for.

    Exposes a dictionary interface of the tags in the directory::

        ifd = ImageFileDirectory_v2()
        ifd[key] = 'Some Data'
        ifd.tagtype[key] = 2
        print(ifd[key])
        'Some Data'

    Individual values are returned as the strings or numbers, sequences are
    returned as tuples of the values.

    The tiff metadata type of each item is stored in a dictionary of
    tag types in
    `~PIL.TiffImagePlugin.ImageFileDirectory_v2.tagtype`. The types
    are read from a tiff file, guessed from the type added, or added
    manually.

    Data Structures:

        * self.tagtype = {}

          * Key: numerical tiff tag number
          * Value: integer corresponding to the data type from `~PIL.TiffTags.TYPES`

    .. versionadded:: 3.0.0
    s   II*     Nc             C   s   | d d  t  k r) t d |   n  | d k	 r; | n | d d  |  _ |  j t k ri d |  _ n' |  j t k r d |  _ n t d   |  j   |  j d | d d   \ |  _ d	 |  _	 d S)
a  Initialize an ImageFileDirectory.

        To construct an ImageFileDirectory from a real file, pass the 8-byte
        magic header to the constructor.  To only set the endianness, pass it
        as the 'prefix' keyword argument.

        :param ifh: One of the accepted magic headers (cf. PREFIXES); also sets
              endianness.
        :param prefix: Override the endianness of the file.
        Nr   z%not a TIFF file (header %r not valid)r   ><znot a TIFF IFDr,   F)
r4   SyntaxError_prefixMM_endianIIreset_unpacknext_legacy_api)rI   ifhr5   r%   r%   r)   rK     s    %
"zImageFileDirectory_v2.__init__c             C   s   |  j  S)N)r~   )rI   r%   r%   r)   <lambda>  s    zImageFileDirectory_v2.<lambda>c             C   s   |  j  S)N)_offset)rI   r%   r%   r)   r     s    c             C   s   |  j  S)N)r   )rI   r%   r%   r)   r     s    c             C   s   t  d   d  S)Nz"Not allowing setting of legacy api)	Exception)rI   rJ   r%   r%   r)   
legacy_api  s    z ImageFileDirectory_v2.legacy_apic             C   s:   i  |  _  i  |  _ i  |  _ i  |  _ d  |  _ d  |  _ d  S)N)_tags_v1_tags_v2_tagdatatagtypeZ_nextr   )rI   r%   r%   r)   r     s    					zImageFileDirectory_v2.resetc             C   s   t  t |    S)N)rN   dict)rI   r%   r%   r)   __str__  s    zImageFileDirectory_v2.__str__c             C   s
   t  |   S)zrReturn a dictionary of the image's tags.

        use `dict(ifd)` instead.

        .. deprecated:: 3.0.0
        )r   )rI   r%   r%   r)   as_dict  s    zImageFileDirectory_v2.as_dictc             C   s   t  d d   |  j   D  S)z
        :returns: dict of name|key: value

        Returns the complete tag dictionary, with named tags where possible.
        c             s   s-   |  ]# \ } } t  j |  j | f Vq d  S)N)r   lookupname)r&   coderJ   r%   r%   r)   	<genexpr>  s   z.ImageFileDirectory_v2.named.<locals>.<genexpr>)r   items)rI   r%   r%   r)   named  s    zImageFileDirectory_v2.namedc             C   s    t  t |  j  t |  j  B S)N)lensetr   r   )rI   r%   r%   r)   __len__  s    zImageFileDirectory_v2.__len__c             C   s   | |  j  k rX |  j | } |  j | } |  j | \ } } | |  | |  j  |  | <n  |  j  | } |  j r t | t t f  r | f } n  | S)N)r   r   r   _load_dispatchr   
isinstancetuplebytes)rI   tagdatatypsizehandlerr;   r%   r%   r)   __getitem__  s    z!ImageFileDirectory_v2.__getitem__c             C   s   | |  j  k p | |  j k S)N)r   r   )rI   r   r%   r%   r)   __contains__  s    z"ImageFileDirectory_v2.__contains__c             C   s
   | |  k S)Nr%   )rI   r   r%   r%   r)   has_key  s    zImageFileDirectory_v2.has_keyc             C   s   |  j  | | |  j  d  S)N)_setitemr   )rI   r   rJ   r%   r%   r)   __setitem__  s    z!ImageFileDirectory_v2.__setitem__c                s:  t  t t f } t t k r+ | t f 7} n  t j |    t | |  rR | g n | } | |  j k r|  j r   j |  j | <q|d |  j | <t	 d d   | D  r d |  j | <q|t	 d d   | D  rt	 d d   | D  r d |  j | <qyd |  j | <q|t	 d	 d   | D  r4d
 |  j | <q|t t k rPd |  j | <q|t	 d d   | D  r|d |  j | <q|n  |  j | d k rt t k	 rd d   | D } n  t
   f d d   | D  } | r|  j n |  j }   j d k r,| r|  j | d k r| f } n  | \ | | <n
 | | | <d  S)Nr   c             s   s   |  ] } t  | t  Vq d  S)N)r   r9   )r&   r(   r%   r%   r)   r     s    z1ImageFileDirectory_v2._setitem.<locals>.<genexpr>r   c             s   s   |  ] } t  | t  Vq d  S)N)r   int)r&   r(   r%   r%   r)   r     s    c             s   s   |  ] } | d k  Vq d S)r   r0   Ni   r%   )r&   r(   r%   r%   r)   r     s    r   r   c             s   s   |  ] } t  | t  Vq d  S)N)r   rB   )r&   r(   r%   r%   r)   r     s    r/   r   c             s   s   |  ] } t  | t  Vq d  S)N)r   rN   )r&   r(   r%   r%   r)   r     s    c             S   s7   g  |  ]- } t  | t  r- | j d  d  n |  q S)asciireplace)r   rN   encode)r&   rJ   r%   r%   r)   r*     s   	z2ImageFileDirectory_v2._setitem.<locals>.<listcomp>c             3   s   |  ] }   j  |  Vq d  S)N)Zcvt_enum)r&   rJ   )infor%   r)   r     s    r   
   )r   r   )r
   r   rN   unicoder   r   r   r   rC   allr   r   r   length)rI   r   rJ   r   Z	basetypesvaluesdestr%   )r   r)   r     s>    		zImageFileDirectory_v2._setitemc             C   s=   |  j  j | d   |  j j | d   |  j j | d   d  S)N)r   popr   r   )rI   r   r%   r%   r)   __delitem__  s    z!ImageFileDirectory_v2.__delitem__c             C   s    t  t |  j  t |  j  B S)N)iterr   r   r   )rI   r%   r%   r)   __iter__  s    zImageFileDirectory_v2.__iter__c             C   s   t  j |  j | |  S)N)structunpackr   )rI   fmtr   r%   r%   r)   r   "  s    zImageFileDirectory_v2._unpackc             G   s   t  j |  j | |  S)N)r   packr   )rI   r   r   r%   r%   r)   _pack%  s    zImageFileDirectory_v2._packc                s      f d d   } | S)Nc                s\   d d l  m } |  j j d  rH |  j d d   j d d  |   <n   |  f t   <|  S)Nr   )r   load_r   _ )PIL.TiffTagsr   rt   
startswithr   r   )funcr   )idxr   r%   r)   	decorator)  s
    &z9ImageFileDirectory_v2._register_loader.<locals>.decoratorr%   )r   r   r   r%   )r   r   r)   _register_loader(  s    z&ImageFileDirectory_v2._register_loaderc                s     f d d   } | S)Nc                s   |  t    <|  S)N)_write_dispatch)r   )r   r%   r)   r   2  s    
z9ImageFileDirectory_v2._register_writer.<locals>.decoratorr%   )r   r   r%   )r   r)   _register_writer1  s    z&ImageFileDirectory_v2._register_writerc                sx   d d l  m } |  \ }   } | | | <t j d      d    f d d  f t | <  f d d   t | <d  S)Nr   )r   =Tc                s&   |  j  d j t |      |  S)Nz{0}{1})r   formatr   )rI   r   r   )r   r   r%   r)   r   <  s    z7ImageFileDirectory_v2._register_basic.<locals>.<lambda>c                s#   d j     f d d   | D  S)N    c             3   s!   |  ] }  j    |  Vq d  S)N)r   )r&   rJ   )r   rI   r%   r)   r   ?  s    zJImageFileDirectory_v2._register_basic.<locals>.<lambda>.<locals>.<genexpr>)join)rI   r   )r   )rI   r)   r   >  s    )r   r   r   calcsizer   r   )Zidx_fmt_namer   r   r   r%   )r   r   r)   _register_basic7  s    
"z%ImageFileDirectory_v2._register_basicr   Hshortr   r,   longr   bsigned byter   hsigned short	   lsigned long   rM   rB   r/   ddoubler   Tc             C   s/   | r
 | St  t t k r( t t |  n |  S)N)r   r   rN   mapord)rI   r   r   r%   r%   r)   	load_byteF  s    
zImageFileDirectory_v2.load_bytec             C   s   | S)Nr%   )rI   r   r%   r%   r)   
write_byteK  s    z ImageFileDirectory_v2.write_byter   c             C   s2   | j  d  r" | d  d  } n  | j d d  S)Ns    r   zlatin-1r   r7   )endswithdecode)rI   r   r   r%   r%   r)   load_stringO  s    z!ImageFileDirectory_v2.load_stringc             C   s@   t  j d d k r( | j d d  } n  d | j d d  d S)Nr   r   r   r   r   s    )rF   version_infor   r   )rI   rJ   r%   r%   r)   write_stringU  s    z"ImageFileDirectory_v2.write_stringr   c                sw   |  j  d j t |  d  |  }  f d d     t   f d d   t | d  d  d  | d d  d   D  S)	Nz{0}Lr   c                s     r |  | f St  |  |  S)N)r9   )rL   r   )r   r%   r)   r   _  s    z5ImageFileDirectory_v2.load_rational.<locals>.<lambda>c             3   s$   |  ] \ } }   | |  Vq d  S)Nr%   )r&   numdenom)combiner%   r)   r   `  s   z6ImageFileDirectory_v2.load_rational.<locals>.<genexpr>r   r   )r   r   r   r   zip)rI   r   r   valsr%   )r   r   r)   load_rational\  s    %z#ImageFileDirectory_v2.load_rationalc                s    d j    f d d   | D  S)Nr   c             3   s*   |  ]  }   j  d  t | d   Vq d S)2Lr      Nl        )r   r=   )r&   frac)rI   r%   r)   r   e  s   z7ImageFileDirectory_v2.write_rational.<locals>.<genexpr>)r   )rI   r   r%   )rI   r)   write_rationalc  s    z$ImageFileDirectory_v2.write_rationalr   c             C   s   | S)Nr%   )rI   r   r   r%   r%   r)   load_undefinedh  s    z$ImageFileDirectory_v2.load_undefinedc             C   s   | S)Nr%   )rI   rJ   r%   r%   r)   write_undefinedl  s    z%ImageFileDirectory_v2.write_undefinedr   c                sw   |  j  d j t |  d  |  }  f d d     t   f d d   t | d  d  d  | d d  d   D  S)	Nz{0}lr   c                s     r |  | f St  |  |  S)N)r9   )rL   r   )r   r%   r)   r   s  s    z<ImageFileDirectory_v2.load_signed_rational.<locals>.<lambda>c             3   s$   |  ] \ } }   | |  Vq d  S)Nr%   )r&   r   r   )r   r%   r)   r   t  s   z=ImageFileDirectory_v2.load_signed_rational.<locals>.<genexpr>r   r   )r   r   r   r   r   )rI   r   r   r   r%   )r   r   r)   load_signed_rationalp  s    %z*ImageFileDirectory_v2.load_signed_rationalc                s    d j    f d d   | D  S)Nr   c             3   s*   |  ]  }   j  d  t | d   Vq d S)r   r      Ni   @)r   r=   )r&   r   )rI   r%   r)   r   y  s   z>ImageFileDirectory_v2.write_signed_rational.<locals>.<genexpr>)r   )rI   r   r%   )rI   r)   write_signed_rationalw  s    z+ImageFileDirectory_v2.write_signed_rationalc             C   sH   | j  |  } t |  | k rD t d d | t |  f   n  | S)NzCorrupt EXIF data.  z,Expecting to read %d bytes but only got %d. )readr   IOError)rI   fpr   retr%   r%   r)   _ensure_read|  s    z"ImageFileDirectory_v2._ensure_readc             C   s  |  j    | j   |  _ y9xt |  j d |  j | d   d  D]} |  j d |  j | d   \ } } } } t r t j |  j	 } t
 j | d  } t d | | | | f d d	 n  y |  j | \ }	 }
 Wn+ t k
 rt r t d
 |  n  wE Yn X| |	 } | d k r| j   } |  j d |  \ } t rat d | | f d d	 n  | j |  t j | |  } | j |  n | d  |  } t |  | k rt j d | t |  | f  qE n  | |  j | <| |  j | <t rE | d k rt d |  q)t d |  |  qE qE W|  j d |  j | d   \ |  _ Wn< t k
 r} z t j t |   d  SWYd  d  } ~ Xn Xd  S)Nr   r   r   HHL4sr/   unknownztag: %s (%d) - type: %s (%d)endr   z- unsupported typer   r,   z$Tag Location: %s - Data Location: %szXPossibly corrupt EXIF data.  Expecting to read %d bytes but only got %d. Skipping tag %sr.   z- value: <table: %d bytes>z- value:)r   tellr   ranger   r   DEBUGr   r   r   r   getprintr   KeyErrorseekr   
_safe_readr   warningswarnr   r   r   r   rN   )rI   r   ir   r   countr   tagnametypnameZ	unit_sizer   r   hereoffsetmsgr%   r%   r)   load  sR    
/*
	(zImageFileDirectory_v2.loadc             C   s  | j    d k r8 | j |  j |  j d d d   n  | j |  j d t |  j    g  } | j    t |  j  d d } d  } xt |  j j    D]\ } } | t k r t |  } n  |  j	 j
 |  } t r t d | | | f  n  t | t  r| n | f } |  j | |  |  }	 t rt j |  j }
 t j
 | d	  } t d
 |
 | | | f d d t |	  d k rt d t |	   qt d |  n  | d k rt |	  n	 t |  } t |	  d k r| j | | | |	 j d d  d f  q | j | | | |  j d |  |	 f  | t |	  d d d 7} q W| d  k	 r| | \ } } } } }	 |	 rt d   n  |  j d |  j d |  d |  } | | | | |	 f | | <n  xp | D]h \ } } } } }	 t d k rt | | | t |  t |	   n  | j |  j d | | | |   qW| j d  xJ | D]B \ } } } } }	 | j |	  t |	  d @rY| j d  qYqYW| S)Nr   ZHL*   r   r   r/   r   zTag %s, Type: %s, Value: %sr   zsave: %s (%d) - type: %s (%d)r   r   r0   z- value: <table: %d bytes>z- value:r   r   s    r   r,   r   z&multistrip support not yet implementedr   s       )r   r   )r   writer~   r   r   r   sortedr   STRIPOFFSETSr   r   r   r   r   r   r   r   r   r   r   appendljustNotImplementedErrorr   repr)rI   r   entriesr
  Zstripoffsetsr   rJ   r   r   r   r  r  r  r%   r%   r)   save  sX    &"!"$+( &%&zImageFileDirectory_v2.save)r   r   zshort)r   r,   zlong)r   r   r   )r   r   r   )r   r   r   )r   rM   zfloat)r/   r   zdouble),rt   ru   rv   rw   rK   ry   r5   r
  r   setterr   r   r   r   r   r   r   r   rN   r   r   r   r   r   r   r   r   r   r   listr   r   r   r   r   r   r   r   r   r   r   r   r  r  r%   r%   r%   r)   rz   b  s\   
	-	
	5rz   r   r   r   Zwrite_c               @   s   e  Z d  Z d Z d d   Z e d d    Z e d d    Z e d d    Z	 d	 d
   Z
 d d   Z d d   Z d d   Z d d   Z d d   Z d S)ImageFileDirectory_v1a  This class represents the **legacy** interface to a TIFF tag directory.

    Exposes a dictionary interface of the tags in the directory::

        ifd = ImageFileDirectory_v1()
        ifd[key] = 'Some Data'
        ifd.tagtype[key] = 2
        print ifd[key]
        ('Some Data',)

    Also contains a dictionary of tag types as read from the tiff image file,
    `~PIL.TiffImagePlugin.ImageFileDirectory_v1.tagtype`.

    Values are returned as a tuple.

    ..  deprecated:: 3.0.0
    c             O   s    t  j |  | |  d |  _ d  S)NT)rz   rK   r   )rI   rT   kwargsr%   r%   r)   rK     s    zImageFileDirectory_v1.__init__c             C   s   |  j  S)N)r   )rI   r%   r%   r)   r     s    zImageFileDirectory_v1.<lambda>c             C   s   |  j  S)N)r   )rI   r%   r%   r)   r     s    c             C   s:   |  d | j   } | j | _ | j | _ | j | _ | S)a3   Returns an
        :py:class:`~PIL.TiffImagePlugin.ImageFileDirectory_v1`
        instance with the same data as is contained in the original
        :py:class:`~PIL.TiffImagePlugin.ImageFileDirectory_v2`
        instance.

        :returns: :py:class:`~PIL.TiffImagePlugin.ImageFileDirectory_v1`

        r5   )r5   r   r   r   )clsZoriginalifdr%   r%   r)   from_v2   s
    zImageFileDirectory_v1.from_v2c             C   sL   t  d |  j  } t |  j  | _ t |  j  | _ t |  j  | _ | S)a3   Returns an
        :py:class:`~PIL.TiffImagePlugin.ImageFileDirectory_v2`
        instance with the same data as is contained in the original
        :py:class:`~PIL.TiffImagePlugin.ImageFileDirectory_v1`
        instance.

        :returns: :py:class:`~PIL.TiffImagePlugin.ImageFileDirectory_v2`

        r5   )rz   r5   r   r   r   r   )rI   r  r%   r%   r)   to_v22  s
    zImageFileDirectory_v1.to_v2c             C   s   | |  j  k p | |  j k S)N)r   r   )rI   r   r%   r%   r)   r   C  s    z"ImageFileDirectory_v1.__contains__c             C   s    t  t |  j  t |  j  B S)N)r   r   r   r   )rI   r%   r%   r)   r   F  s    zImageFileDirectory_v1.__len__c             C   s    t  t |  j  t |  j  B S)N)r   r   r   r   )rI   r%   r%   r)   r   I  s    zImageFileDirectory_v1.__iter__c             C   s(   x! d D] } |  j  | | |  q Wd  S)NFT)FT)r   )rI   r   rJ   r   r%   r%   r)   r   L  s    z!ImageFileDirectory_v1.__setitem__c             C   s   | |  j  k ro |  j | } |  j | } |  j | \ } } x0 d D]% } |  j | | |  | |  |  qC Wn  |  j  | } t | t t f  s | f } n  | S)NFT)FT)r   r   r   r   r   r   r   r   )rI   r   r   r   r   r   legacyr;   r%   r%   r)   r   P  s    &z!ImageFileDirectory_v1.__getitem__N)rt   ru   rv   rw   rK   ry   tagsZtagdataclassmethodr  r  r   r   r   r   r   r%   r%   r%   r)   r    s   r  c               @   s   e  Z d  Z d Z d Z d d   Z e d d    Z e d d    Z d	 d
   Z	 d d   Z
 d d   Z d d d  Z d d   Z d d   Z d S)TiffImageFileZTIFFz
Adobe TIFFc             C   s   |  j  j d  } t |  |  _ d |  _ |  _ |  j j |  _ |  _ d |  _	 |  j  |  _
 g  |  _ d |  _ d |  _ t r t d  t d |  j  t d |  n  |  j d  d S)	z#Open the first image in a TIFF filer   Nr   z*** TiffImageFile._open ***z
- __first:z- ifh: r   r7   )r   r   rz   tag_v2r   r  r   Z_TiffImageFile__first_TiffImageFile__next_TiffImageFile__frame_TiffImageFile__fp
_frame_pos	_n_frames_is_animatedr   r   _seek)rI   r   r%   r%   r)   _openi  s    				
zTiffImageFile._openc             C   s|   |  j  d  k ru |  j   } y" x |  j |  j   d  q! WWn% t k
 rd |  j   d |  _  Yn X|  j |  n  |  j  S)Nr   )r(  r   r*  EOFErrorr  )rI   currentr%   r%   r)   n_frames  s    zTiffImageFile.n_framesc             C   sj   |  j  d  k rc |  j   } y |  j d  d |  _  Wn t k
 rR d |  _  Yn X|  j |  n  |  j  S)Nr   TF)r)  r   r  r,  )rI   r-  r%   r%   r)   is_animated  s    zTiffImageFile.is_animatedc             C   sH   |  j  t | d   t j |  j  t j j |  j |  j  |  _ d S)z%Select a given frame as current imager   N)	r*  maxr   Z_decompression_bomb_checkr   corenewmodeim)rI   framer%   r%   r)   r    s    zTiffImageFile.seekc             C   sS  |  j  |  _ x t |  j  | k r |  j s< t d   n  t rn t d | |  j |  j |  j j	   f  n  |  j j	   |  j j
 |  j  |  j j |  j  t r t d |  j j	    n  |  j j |  j  |  j j |  _ |  j d 7_ q W|  j j
 |  j |  |  j j |  j  t j |  j  |  _ |  _ | |  _ |  j   d  S)Nzno more images in TIFF filez9Seeking to frame %s, on frame %s, __next %s, location: %szLoading tags, location: %sr   )r&  r   r   r'  r$  r,  r   r   r%  r   r  r  r#  r  r   r  r  r   r  _setup)rI   r5  r%   r%   r)   r*    s(    	&	zTiffImageFile._seekc             C   s   |  j  S)zReturn the current frame number)r%  )rI   r%   r%   r)   r     s    zTiffImageFile.tellNc             C   s  d } | d k r. |  j  d k r. | | } n  |  j } | d k rU | d d f } n | d k r | d f } t |  j k r |  j t \ |  _ q nO | d	 k r | } n: | d
 k r | } t |  j k r |  j t f |  _ q n  t |  j k r
|  j t |  j d <n  | S)zSetup decoder contextsNr2   r   r   r   r   r    r    r   icc_profile)	_planar_configuration_compression
JPEGTABLESr#  Ztile_prefix	PREDICTORdecoderconfig
ICCPROFILEr   )rI   rawmodeZlayertilerT   compressionr%   r%   r)   _decoder  s&    		zTiffImageFile._decoderc             C   s2  t  j  j |   } |  j d k r0 t d   n  |  j s= | S|  j   t |  j  d k sk t d   n  |  j d d } |  j d d |  j j f } t  j |  j	 d | |  j
  } y | j |  j |  Wn t k
 r t d   Yn Xt |  j d	  r6t rt d
  n  | j |  j j    \ } } n~ t |  j d  rt r[t d  n  |  j j d  | j d  \ } } n1 t rt d  n  | j |  j j    \ } } g  |  _ d |  _ t |  j d  r|  j s|  j j   qn  d |  _ | d k  rt |   n  |  j   t  j  j |   S)z] Overload method triggered when we detect a compressed tiff
            Calls out to libtiff Nzcannot load this imager   zNot exactly one tiler   r   libtiffzCouldn't set the imagegetvaluez5have getvalue. just sending in a string from getvaluefilenoz3have fileno, calling fileno version of the decoder.s   fpfpz+don't have fileno or getvalue. just readingclose)r   r  r@  r   Zload_preparer   r#  r
  Z_getdecoderr3  r=  setimager4  
ValueErrorhasattrr   r   r   r   rD  r  r   readonlyr$  rF  Zload_end)rI   ZpixelZextentsrT   decodernerrr%   r%   r)   _load_libtiff  sL    	

!				
zTiffImageFile._load_libtiffc             C   s  d |  j  k r t d   n  t |  j  j t d  |  _ |  j  j t d  |  _ |  j  j t d  } |  j  j t	 d  } t
 r t d  t d |  j  t d |  t d |  j  t d	 |  n  |  j  j t  } |  j  j t  } | | f |  _ t
 rt d
 |  j  n  |  j  j t d1  } t |  d k rmt |  t |  k o_d k n rmd2 } n  |  j  j | | | |  j  j t d3  |  j  j t f   f } t
 rt d |  n  y t | \ |  _ } Wn1 t k
 r	t
 rt d  n  t d   Yn Xt
 r0t d |  t d |  j  n  |  j |  j d <|  j  j t d  } |  j  j t d  }	 | r|	 r|  j  j t d  }
 |
 d k r| |	 f |  j d <q|
 d k r| d |	 d f |  j d <q| |	 f |  j d <n  d } } } g  |  _ t |  j  k ry|  j  t } |  j  j t  |  } |  j d } t! sW|  j d4 k r|  j" |  _# yP t$ |  j% d   ot& j' |  j% j(    } t$ |  j% d!  r|  j% j)   n  Wn t k
 rd" } Yn X| d k rB|  j  j | | d |  j  j t d5  |  j  j t f   f } t
 r,t d |  n  t | \ |  _ } n  |  j d6 k rfd$ | k rfd% } n  | |  j | f } |  j j* |  j d d | | f d | f  d& } qxt+ t |   D] } |  j, | | |  } |  j j* |  j d t | |  | t | | |  f | | | f  t
 r8t d' |  j  n  | | } | |  j d k rld } } | d 7} n  d& } qWn"t- |  j  k r||  j  j d(  } |  j  j d)  } d& } x |  j  t- D] } | s|  j, | |  } n  |  j j* |  j | | | | | | f | | f  | | } | |  j d k rd | | } } | |  j d k rud } } | d 7} d& } quqqWn t
 rt d*  n  t d+   |  j d, k rd- d.   |  j  t. D } t/ j0 d/ d0 j1 |   |  _2 n  d& S)7z-Setup this image object based on current tagsi  z+Windows Media Photo files not yet supportedr   r   z*** Summary ***z- compression:z- photometric_interpretation:z- planar_configuration:z- fill_order:z- size:zformat key:z- unsupported formatzunknown pixel modez- raw mode:z- pil mode:rA  r   dpir   gRQ@
resolutionr   r   r   r   r   r!   r"   r#   r$   r   rE  flushFI;16BI;16zI;16NNztiles: iB  iC  z- unsupported data organizationzunknown data organizationr3   c             S   s    g  |  ] } t  | d    q S)r   )o8)r&   r   r%   r%   r)   r*     s   	 z(TiffImageFile._setup.<locals>.<listcomp>zRGB;Lr   )r   )r   )r   )
r   r   r   r   r   r!   r"   r#   r$   r   )r   )rR  rS  )3r#  r   COMPRESSION_INFOr   COMPRESSIONr:  PLANAR_CONFIGURATIONr9  PHOTOMETRIC_INTERPRETATION	FILLORDERr   r   
IMAGEWIDTHIMAGELENGTHr   SAMPLEFORMATr   r0  minr5   BITSPERSAMPLEEXTRASAMPLES	OPEN_INFOr3  r   r}   r   X_RESOLUTIONY_RESOLUTIONRESOLUTION_UNITr@  r  ROWSPERSTRIPREAD_LIBTIFFrN  r  rI  r   osduprE  rQ  r  r   rB  TILEOFFSETSCOLORMAPr   r   r   palette)rI   photoZ	fillorderZxsizeZysizer   keyr?  ZxresZyresZresunitxyr   offsetsr   wr   rL   r  orj  r%   r%   r)   r6  '  s    
:		      					%

	


zTiffImageFile._setup)rt   ru   rv   r   format_descriptionr+  ry   r.  r/  r  r*  r   rB  rN  r6  r%   r%   r%   r)   r"  d  s   	Er"  c             C   sW  y# t  |  j \ } } } } } } Wn% t k
 rJ t d |  j   Yn Xt d |  }	 |  j j d |  j j d d   }
 t p |
 d k } t	 |  d d  |	 t
 <|  j d |	 t <|  j d |	 t <|  j j d i   } t r t d	 t |   n  t | t  r| j   } n  xD | D]< } | j |  |	 | <y | j | |	 j | <WqYqXqWt |  d
  rxb t t t t t t f D]H } | |  j k r|  j | |	 | <n  |  j j j | d   |	 j | <qWd |  j k r|  j d |	 t <qn  x t d f t d f t d f t d f t d f t d f t d f t  d f t! d f t" d f g
 D] \ } } | j# d d  } d | k r| |  j k rt$ j% d | | f t&  |  j | j# d d  |	 | <n  | |  j k rZ|  j | |	 | <qZqZW|  j j d  } | r5d |	 t <| d |	 t <| d |	 t <n  | d1 k rs| |	 t' <t( |  d k rst( |  |	 t) <qsn  | d  k	 r| |	 t* <n  | d k r| |	 t+ <n  | |	 t, <|  j d k r|  j- j. d d  } t/ d d   | D  |	 t0 <n  t( |  |  j d | d d d  } |  j d |	 t1 <| |  j d |	 t2 <d |	 t3 <t4 j |
 d  |	 t5 <| rt rt d!  t d" t6 |	 j7     n  d } t | d#  ry& | j8 d  t9 j: | j;    } Wqt< j= k
 rYqXn  t3 t2 g } i  } | d | t' <i  } t |  d$  r0|  j> j   } n  x t? j@ |	 j7   t	 |  d
 i   j7   | j7    D] \ } } | tA jB k rqdn  | | k rd| | k rdt | tC tD k rtE n tD  r| jF d% d&  d' | | <q
t | tG  rtH |  | | <q
| | | <qdqdWt r1t d( t6 | j7     n  |  j d2 k rId+ } n  | |
 | | | f } tI jJ |  j d, | |  jK  } | jL |  j- d3 |  j  x< | jF d4  \ } } } | s| jM |  n  | rPqqW| d k  r8t d/ |   q8nA |	 jN |  } tO jP |  | d d5 |  j | | | d f f g  d0 |  j k rS|	 |  _Q n  d  S)6Nzcannot write mode %s as TIFFr5   rA  r   r9  r   r   ZtiffinfozTiffinfo Keys: %sr#  r8  descriptionrP  Zx_resolutionZy_resolutionZresolution_unitZsoftware	date_timeZartist	copyrightr   r   z %r is deprecated; use %r insteadrO  r   r3   r2   zRGB;Lc             s   s   |  ] } t  |  d  Vq d S)r   N)i8)r&   r(   r%   r%   r)   r   c  s    z_save.<locals>.<genexpr>r   r   zSaving using libtiff encoderz	Items: %srE  r   r   r   s    zConverted items: %sI;16BI;16zI;16NrC  r0   i   z(encoder error %d when writing image file_debug_multipage)r   )rw  rx  )r   r   i @  )r   r   )R	SAVE_INFOr3  r   r   rz   Zencoderinfor   r   WRITE_LIBTIFFrS   rW  r   rZ  r[  r   r   r  r   r  r  r   rI  rc  ra  rb  IPTC_NAA_CHUNKPHOTOSHOP_CHUNKXMPr#  r>  IMAGEDESCRIPTIONSOFTWARE	DATE_TIMEARTIST	COPYRIGHTr   r  r  DeprecationWarningr^  r   SAMPLESPERPIXELr_  r\  rX  r4  Z
getpaletter   ri  rd  STRIPBYTECOUNTSr  COMPRESSION_INFO_REVrV  r  r   r  rf  rg  rE  ioUnsupportedOperationr   	itertoolschainr   ZLIBTIFF_COREr   rN   r   r   r9   rB   r   Z_getencoderZencoderconfigrG  r  r  r   _savery  )r4  r   filenamer?  r5   rk  r   bitsextrar  rA  rC  r   rl  r   Zname_with_spacesrO  ZlutZstride_fpZ	blocklistZattsZ
legacy_ifdr   rJ   rL   er   sr   r
  r%   r%   r)   r    s    #	#									 


 '

 !	&r  z.tifz.tiffz
image/tiff)[
__future__r   r   ZPILr   r   r   r   r   collectionsZ	fractionsr	   numbersr
   r   r  r  rf  r   rF   r  r   r   __version__r   re  r{  ZIFD_LEGACY_APIr   r   rv  rT  rZ  r[  r^  rV  rX  rY  r  r  r  rd  r  ra  rb  rW  rc  r  r  r  r<  ri  rh  r_  r\  r;  r  r|  r}  r>  ZEXIFIFDr~  ZIMAGEJ_META_DATA_BYTE_COUNTSZIMAGEJ_META_DATArU  r   r   r  r`  r4   r6   r=   r   r   r9   MutableMappingrz   r   r   r   setattrr  ZImageFileDirectoryr"  rz  r  Zregister_openr   Zregister_saveZregister_extensionZregister_mimer%   r%   r%   r)   <module>*   st  		
%%%%%%%%%%%%%%%%%++%%%%%%%%%%%%++++..11111111%%%%%%%%%%%%%%++%%..+++.| 		W "%%%""