o
    Wf-                 #   @  s(  d dl 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Zd dl	m
Z
 d dlmZ d dlmZmZ d dlmZmZmZ ddlmZmZmZmZmZmZ dd	lmZ dd
lmZ ddlm Z  ddlm!Z! e"e#Z$dZ%dZ&dZ'dZ(dZ)dZ*dZ+dZ,dZ-dZ.dZ/dZ0dZ1dZ2dZ3dZ4dZ5dZ6dZ7dZ8d Z9d!Z:d"Z;d#Z<d$Z=d%Z>d&Z?d'Z@d(ZAd)ZBd*ZCd+ZDd,ZEd-ZFd.ZGd/ZHd0ZId1ZJd2ZKd3ZLd4ZMd5ZNd6ZOd7ZPd8ZQd9ZRd:ZSi dd;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZd[ZTd\d] eTU D ZVi e)d d^dd^d_fd`e*d d^dd^d_fd`e)d d^d<d^d_fdae*d d^d<d^d_fdae)dd^dd^d_fdbe*dd^dd^d_fdbe)dd^d<d^d_fdce*dd^d<d^d_fdce)d d^dddd_fdee*d d^dddd_fdee)d d^d<ddd_fdfe*d d^d<ddd_fdfe)dd^dddd_fdge*dd^dddd_fdge)dd^d<ddd_fdhe*dd^d<ddd_fdhe)d d^ddid_fdji e*d d^ddid_fdje)d d^d<did_fdke*d d^d<did_fdke)dd^ddid_fdle*dd^ddid_fdle)dd^d<did_fdme*dd^d<did_fdme)d d^ddnd_fdoe*d d^ddnd_fdoe)d d^d<dnd_fdpe*d d^d<dnd_fdpe)dd^ddnd_fdqe*dd^ddnd_fdqe)dddddnd_fdqe*dddddnd_fdqe)dd^d<dnd_fdre*dd^d<dnd_fdri e)dd^ddsd_fdte)d d^ddud_fdve)dd^ddud_fdve*dd^ddud_fdwe)dd^d<dud_fdxe)dddddud_fdye*dddddud_fdze)d d{dd|d_fd}e*d d{dd|d_fd~e)dd^dd|d_fde)ddddd|d_fde*ddddd|d_fde)dd{dd|d_fd}e*dd{dd|d_fd~e)dd^ddddfde*dd^ddddfde)d<d^ddd_fdi e*d<d^ddd_fde)d<d^d<dd_fde*d<d^d<dd_fde)d<d^ddd_fde*d<d^ddd_fde)d<d^dddfde*d<d^dddfde)d<d^dddfde*d<d^dddfde)d<d^dddfde*d<d^dddfde)d<d^ddd^fde*d<d^ddd^fde)d<d^dddfde*d<d^dddfde)d<d^dddfde*d<d^dddfdi e)d<d^ddddfde*d<d^ddddfde)d<d^dddfde*d<d^dddfde)d<d^dddfde*d<d^dddfde)d<d^dddfde*d<d^dddfde)d<d^ddd_fde*d<d^ddd_fde)d<d^ddd_fde*d<d^ddd_fde)d<d^dddfde*d<d^dddfde)d<d^ddd^fde*d<d^ddd^fde)d<d^ddddfdi e*d<d^ddddfde)d>d^dd^d_fde*d>d^dd^d_fde)d>d^d<d^d_fde*d>d^d<d^d_fde)d>d^dddd_fde*d>d^dddd_fde)d>d^d<ddd_fde*d>d^d<ddd_fde)d>d^ddid_fde*d>d^ddid_fde)d>d^d<did_fde*d>d^d<did_fde)d>d^ddnd_fde*d>d^ddnd_fde)d>d^ddddfde*d>d^ddddfde)d>d^d<dnd_fde*d>d^d<dnd_fde)dBd^ddd_fde*dBd^ddd_fde)dBd^dddfde*dBd^dddfde)dBd^dddfde*dBd^dddfde)dBd^ddd_fde)dDd^ddnd_fdqe*dDd^ddnd_fdqe)dDd^ddd_fde*dDd^ddd_fde)dHd^ddd_fde*dHd^ddd_fdiZWeXdd eWD ZYg dZZdd Z[dd Z\dd Z]i Z^i Z_dd Z`G dd deZadd ZbddĄ ZcddƄ Zderde
eeef Zfne
ZfG ddȄ defZge^eg_^e_eg__e!U D ]!\ZhZieijddʡZiekegdei e^eh d  ekegdei e_eh  qx[^[_[h[iG dd΄ degZlelZmG ddЄ dejZni dde)ddd^dfdde)dddndfdde)dddd<fdde)d>ddndfdde)d>ddd<fdde)dd<d|dfdde)dddudfdde)dd<dudfdde)dd>d|dfdde)d<dddfdde)d<ddd fdde)d<ddd<fdde)dBdddfdde)dDdddfdde)dHdddfdde*dd<d|dfdde*dddudfde*dd<dudfde*dd>d|dfdZodd ZpG dd dZqdd Zresenjtene[ euenjtep evenjter ewenjtddg exenjtd dS )    )annotationsN)MutableMapping)Fraction)NumberRational)TYPE_CHECKINGAnyCallable   )ExifTagsImage	ImageFileImageOpsImagePaletteTiffTags)i16be)i32beo8TYPESFT   s   IIs   MM      i  i  i  i  i
  i    i  i  i  i  i  i  i(  i-  i1  i2  i;  i=  i@  iB  iC  D  iE  iJ  iR  iS  i[  i  i  i  i  iI  is  ii  i  i  i  i  raw   
tiff_ccitt   group3   group4   tiff_lzw   	tiff_jpeg   jpeg   tiff_adobe_deflatei  tiff_raw_16i  packbitsi)  tiff_thunderscani  tiff_deflateit  tiff_sgilogiu  tiff_sgilog24im  lzmaiP  zstdiQ  webpc                 C  s   i | ]\}}||qS  r4   ).0kvr4   r4   {/media/darth/New Volume E/PROJECTS/DRIVERZZ/DRIVERZZ_CUSTOMER_APIS/venv/lib/python3.10/site-packages/PIL/TiffImagePlugin.py
<dictcomp>   s    r9   r
   r4   )1z1;I)r;   z1;IR)r;   r;   )r;   z1;R)r   )LzL;2I)r<   zL;2IR)r<   zL;2)r<   zL;2R)r!   )r<   zL;4I)r<   zL;4IR)r<   zL;4)r<   zL;4R)r)   )r<   zL;I)r<   zL;IR)r<   r<   )r<   zL;R)   )I;16zI;12)   )r>   r>   )I;16Br@   )r>   zI;16R)II;16S)rA   I;16BS)r   )    )FF;32F)rE   F;32BF)rA   zI;32N)rA   I;32S)rA   I;32BS)r)   r)   )LArJ   )r)   r)   r)   )RGBrK   )rK   zRGB;R)r)   r)   r)   r)   )RGBArL   r   )RGBXrN   )r)   r)   r)   r)   r)   r   r   )rN   RGBXX)r)   r)   r)   r)   r)   r)   )r   r   r   )rN   RGBXXX)rL   RGBa)r
   r   )rL   RGBaX)r
   r   r   )rL   RGBaXX)r   r   )rL   RGBAX)r   r   r   )rL   RGBAXX)i  )r?   r?   r?   )rK   zRGB;16L)rK   zRGB;16B)r?   r?   r?   r?   )rL   zRGBA;16L)rL   zRGBA;16B)rN   zRGBX;16L)rN   zRGBX;16B)rL   zRGBa;16L)rL   zRGBa;16B)PzP;1)rW   zP;1R)rW   zP;2)rW   zP;2R)rW   zP;4)rW   zP;4R)rW   rW   )PArX   )rW   zP;R)CMYKrY   )rY   CMYKX)rY   CMYKXX)rY   zCMYK;16L)rK   rN   )LABr\   c                 c  s    | ]	}t |d  V  qdS )r!   N)len)r5   key_tpr4   r4   r8   	<genexpr>      r_   )   MM *   II* s   MM* s   II *s   MM +s   II+ c                 C  s   | d d t v S Nr!   )PREFIXESprefixr4   r4   r8   _accept     rg   c                 C  s<   t | dk}t|rd|  n| |}|r|d d d S |S )Nr
   )absIFDRationallimit_rational)valmax_valinvn_dr4   r4   r8   _limit_rational  s   rq   c                 C  sP   t | }|j|jf}t||k rt| t|}t||kr&t | } t| |}|S N)r   	numeratordenominatorminrq   rj   max)rm   rn   min_valfracrp   r4   r4   r8   _limit_signed_rational"  s   
ry   c                       fdd}|S )Nc                   s   t | j | S rr   )getattr_val)selfargsopr4   r8   delegate8  rh   z_delegate.<locals>.delegater4   )r   r   r4   r   r8   	_delegate7  s   r   c                   @  sP  e Zd ZdZ	 dZd2d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	 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*e+d0red0Z,d1S d1S )3rk   zImplements 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_denominatorr|   r
   c                 C  s   t |tr|j| _|j| _|j| _dS t |tr!|j| _|j| _n|| _|| _|dkr2td| _dS |dkr=t|| _dS t||| _dS )z
        :param value: either an integer numerator, a
        float/rational/other number, or an IFDRational
        :param denominator: Optional integer denominator
        Nr   nanr
   )	
isinstancerk   rs   r   rt   r   r|   r   float)r}   valuert   r4   r4   r8   __init__L  s   


zIFDRational.__init__c                 C     | j S rr   )r   r}   r4   r4   r8   rs   f     zIFDRational.numeratorc                 C  r   rr   )r   r   r4   r4   r8   rt   j  r   zIFDRational.denominatorc                 C  s.   | j dkr| j| j fS | j|}|j|j fS )z

        :param max_denominator: Integer, the maximum denominator value
        :returns: Tuple of (numerator, denominator)
        r   )rt   rs   r|   limit_denominator)r}   max_denominatorfr4   r4   r8   rl   n  s   
zIFDRational.limit_rationalc                 C  s   t t| jS rr   )strr   r|   r   r4   r4   r8   __repr__{  s   zIFDRational.__repr__c                 C  s
   | j  S rr   )r|   __hash__r   r4   r4   r8   r   ~  s   
zIFDRational.__hash__c                 C  s0   | j }t|tr|j }t|trt|}||kS rr   )r|   r   rk   r   )r}   otherrm   r4   r4   r8   __eq__  s   

zIFDRational.__eq__c                 C  s   | j | j| jgS rr   )r|   r   r   r   r4   r4   r8   __getstate__  rh   zIFDRational.__getstate__c                 C  s,   t | d |\}}}|| _|| _|| _d S )Nr   )rk   r   r|   r   r   )r}   stater|   r   r   r4   r4   r8   __setstate__  s
   

zIFDRational.__setstate____add____radd____sub____rsub____mul____rmul____truediv____rtruediv____floordiv____rfloordiv____mod____rmod____pow____rpow____pos____neg____abs__	__trunc____lt____gt____le____ge____bool____ceil__	__floor__	__round____int__Nr:   )-__name__
__module____qualname____doc__	__slots__r   propertyrs   rt   rl   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   hasattrr   r   r4   r4   r4   r8   rk   >  sZ    



rk   c                   s    fdd}|S )Nc                   sB   ddl m} | jdr| jdd  dd| < | ft < | S )Nr
   r   load_r#   _ )r   r   r   
startswithreplace_load_dispatch)funcr   idxsizer4   r8   	decorator  s
   z#_register_loader.<locals>.decoratorr4   )r   r   r   r4   r   r8   _register_loader  s   r   c                   rz   )Nc                   s   | t  < | S rr   )_write_dispatch)r   r   r4   r8   r     s   z#_register_writer.<locals>.decoratorr4   )r   r   r4   r   r8   _register_writer  s   r   c                   sX   ddl m} | \} }|||< td  d fdd	ft|<  fddt|< d S )	Nr
   r   =Tc                   s   |  t|    |S rr   )_unpackr]   r}   data
legacy_apifmtr   r4   r8   <lambda>  s   z!_register_basic.<locals>.<lambda>c                   s   d  fdd|D S )N    c                 3  s    | ]	}  |V  qd S rr   )_packr5   r   )r   r}   r4   r8   r_     r`   z4_register_basic.<locals>.<lambda>.<locals>.<genexpr>joinr}   values)r   r   r8   r     s   T)r   r   structcalcsizer   r   )idx_fmt_namer   r   namer4   r   r8   _register_basic  s   
r   c                   @  s  e Zd ZU dZi Zded< i Zde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dd Zd d! Zd"d# Zd$d% Zd&d' Zeeeejd(d)fejd*d+fejd,d-fej d.d/fej!d0d1fej"d2d3fej#d4d5fej$d*d+fej%d6d7fg	 e&d8d8d]d:d;Z'e(d8d<d= Z)e&d>d8d]d?d@Z*e(d>dAdB Z+e&dCdDd]dEdFZ,e(dCdGdH Z-e&dId8d]dJdKZ.e(dIdLdM Z/e&dNdDd]dOdPZ0e(dNdQdR Z1dSdT Z2dUdV Z3d^dXdYZ4dZd[ Z5dS )_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] = TiffTags.ASCII
        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
    :attr:`~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
            :py:data:`.TiffTags.TYPES`

          .. versionadded:: 3.0.0

    'Internal' data structures:

        * ``self._tags_v2 = {}``

          * Key: numerical TIFF tag number
          * Value: decoded data, as tuple for multiple values

        * ``self._tagdata = {}``

          * Key: numerical TIFF tag number
          * Value: undecoded byte string from file

        * ``self._tags_v1 = {}``

          * Key: numerical TIFF tag number
          * Value: decoded data in the v1 format

    Tags will be found in the private attributes ``self._tagdata``, and in
    ``self._tags_v2`` once decoded.

    ``self.legacy_api`` is a value for internal use, and shouldn't be changed
    from outside code. In cooperation with
    :py:class:`~PIL.TiffImagePlugin.ImageFileDirectory_v1`, if ``legacy_api``
    is true, then decoded tags will be populated into both ``_tags_v1`` and
    ``_tags_v2``. ``_tags_v2`` will be used if this IFD is used in the TIFF
    save routine. Tags should be read from ``_tags_v1`` if
    ``legacy_api == true``.

    z>dict[int, Callable[[ImageFileDirectory_v2, bytes, bool], Any]]r   zdict[int, Callable[..., Any]]r      II*     Nc                 C  s   t |sdt| d}t||dur|n|dd | _| jtkr&d| _n| jtkr/d| _nd}t||d dk| _|| _i | _		 | 
  | jrT| d	|d
d n	| 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.
        znot a TIFF file (header z not valid)Nr   ><znot a TIFF IFD+   Qr)   r<   r!   F)rg   reprSyntaxError_prefixMM_endianII_bigtiffgrouptagtyperesetr   next_legacy_api)r}   ifhrf   r   msgr4   r4   r8   r   %  s$   

,
zImageFileDirectory_v2.__init__c                 C  r   rr   )r   r   r4   r4   r8   r   E      zImageFileDirectory_v2.<lambda>c                 C  r   rr   )_offsetr   r4   r4   r8   r   F  r   c                 C  r   rr   )r   r   r4   r4   r8   r   H  r   z ImageFileDirectory_v2.legacy_apic                 C  s   d}t |)Nz"Not allowing setting of legacy api)	Exception)r}   r   r   r4   r4   r8   r   L  s   c                 C  s(   i | _ i | _i | _i | _d | _d | _d S rr   )_tags_v1_tags_v2_tagdatar   _nextr   r   r4   r4   r8   r   Q  s   
zImageFileDirectory_v2.resetc                 C  s   t t| S rr   )r   dictr   r4   r4   r8   __str__Y     zImageFileDirectory_v2.__str__c                   s    fdd   D S )z
        :returns: dict of name|key: value

        Returns the complete tag dictionary, with named tags where possible.
        c                   s"   i | ]\}}t | jj|qS r4   )r   lookupr   r   )r5   coder   r   r4   r8   r9   b  s    z/ImageFileDirectory_v2.named.<locals>.<dictcomp>)itemsr   r4   r   r8   named\  s   
zImageFileDirectory_v2.namedc                 C     t t| jt| jB S rr   )r]   setr   r   r   r4   r4   r8   __len__g     zImageFileDirectory_v2.__len__c                 C  sf   || j vr| j| }| j| }| j| \}}|| || j| |< | j | }| jr1t|ttfs1|f}|S rr   )r   r   r   r   r   r   tuplebytes)r}   tagr   typr   handlerrm   r4   r4   r8   __getitem__j  s   



z!ImageFileDirectory_v2.__getitem__c                 C     || j v p	|| jv S rr   )r   r   r}   r  r4   r4   r8   __contains__u     z"ImageFileDirectory_v2.__contains__c                 C  s   |  ||| j d S rr   )_setitemr   )r}   r  r   r4   r4   r8   __setitem__x  r  z!ImageFileDirectory_v2.__setitem__c              
     s  t ttf}t|| j t||r|gn|}|| jvr jr% j| j|< ntj	| j|< t
dd |D rGt
dd |D r@tjntj| j|< nkt
dd |D rt
dd |D r`tj| j|< nRt
dd |D rptj| j|< nBt
dd |D r|tjntj| j|< n/t
dd |D rtj| j|< nt
d	d |D rtj| j|< nt
d
d |D rtj| j|< | j| tj	krdd |D }n| j| tjkrdd |D }| j| tjkot|t}|st fdd|D }|r| jn| j}|sO jdks| j| tjks jd u rOt|dkrO|sO|r'| j| tjtjfv r'|f}z|\||< W d S  tyN   td| dt| d |d ||< Y d S w |||< d S )Nc                 s      | ]}t |tV  qd S rr   )r   rk   r5   r7   r4   r4   r8   r_         z1ImageFileDirectory_v2._setitem.<locals>.<genexpr>c                 s      | ]}|d kV  qdS r   Nr4   r  r4   r4   r8   r_         c                 s  r  rr   )r   intr  r4   r4   r8   r_     r  c                 s  s(    | ]}d |  kodk n  V  qdS )r   r   Nr4   r  r4   r4   r8   r_        & c                 s  s(    | ]}d |  k odk n  V  qdS )i i   Nr4   r  r4   r4   r8   r_     r  c                 s  r  r  r4   r  r4   r4   r8   r_     r  c                 s  r  rr   )r   r   r  r4   r4   r8   r_     r  c                 s  r  rr   )r   r   r  r4   r4   r8   r_     r  c                 s  r  rr   )r   r  r  r4   r4   r8   r_     r  c                 S  s&   g | ]}t |tr|d dn|qS )asciir   )r   r   encoder  r4   r4   r8   
<listcomp>  s    z2ImageFileDirectory_v2._setitem.<locals>.<listcomp>c                 S  s"   g | ]}t |trt|n|qS r4   )r   r  r   r  r4   r4   r8   r    s   " c                 3  s    | ]}  |V  qd S rr   )cvt_enumr   infor4   r8   r_     r  r
   zMetadata Warning, tag z had too many entries: z, expected 1r   )r   r  r   r   r   r   r   r   type	UNDEFINEDallRATIONALSIGNED_RATIONALSHORTSIGNED_SHORTLONGSIGNED_LONGDOUBLEASCIIBYTEr   r  r   r   lengthr]   
ValueErrorwarningswarn)r}   r  r   r   	basetypesr   is_ifddestr4   r  r8   r  {  s   




zImageFileDirectory_v2._setitemc                 C  s.   | j |d  | j|d  | j|d  d S rr   )r   popr   r   r  r4   r4   r8   __delitem__  s   z!ImageFileDirectory_v2.__delitem__c                 C  r   rr   )iterr  r   r   r   r4   r4   r8   __iter__  r  zImageFileDirectory_v2.__iter__c                 C  s   t | j| |S rr   )r   unpackr   )r}   r   r   r4   r4   r8   r     s   zImageFileDirectory_v2._unpackc                 G  s   t j| j| g|R  S rr   )r   packr   )r}   r   r   r4   r4   r8   r     r  zImageFileDirectory_v2._packHshortr<   longbzsigned bytehzsigned shortlzsigned longr   r   ddoubler   long8r
   Tc                 C     |S rr   r4   r   r4   r4   r8   	load_byte     zImageFileDirectory_v2.load_bytec                 C  s*   t |tr	t|}t |trt|f}|S rr   )r   rk   r  r  r}   r   r4   r4   r8   
write_byte  s
   


z ImageFileDirectory_v2.write_byter   c                 C  s"   | dr|d d }|ddS )N    ri   zlatin-1r   )endswithdecoder   r4   r4   r8   load_string  s   
z!ImageFileDirectory_v2.load_stringc                 C  s0   t |tr	t|}t |ts|dd}|d S )Nr  r   rE  )r   r  r   r  r  r}   r   r4   r4   r8   write_string  s
   

z"ImageFileDirectory_v2.write_stringr#   r)   c                   V   |  t|d  d|}fdd t fddt|d d d |dd d D S )	Nr!   r<   c                       r| |fS t | |S rr   rk   ar:  r   r4   r8   combine	     z4ImageFileDirectory_v2.load_rational.<locals>.combinec                 3      | ]
\}} ||V  qd S rr   r4   r5   numdenomrQ  r4   r8   r_         z6ImageFileDirectory_v2.load_rational.<locals>.<genexpr>r   r
   r   r]   r  zipr}   r   r   valsr4   rQ  r   r8   load_rational     0z#ImageFileDirectory_v2.load_rationalc                      d  fdd|D S )Nr   c                 3  s(    | ]} j d gt|dR  V  qdS )2Ll    N)r   rq   r5   rx   r   r4   r8   r_     s    
z7ImageFileDirectory_v2.write_rational.<locals>.<genexpr>r   r   r4   r   r8   write_rational  s   z$ImageFileDirectory_v2.write_rationalr'   c                 C  r@  rr   r4   r   r4   r4   r8   load_undefined  rB  z$ImageFileDirectory_v2.load_undefinedc                 C  s0   t |tr	t|}t |trt|dd}|S )Nr  r   )r   rk   r  r   r  rI  r4   r4   r8   write_undefined  s
   

z%ImageFileDirectory_v2.write_undefined
   c                   rK  )	Nr!   r<  c                   rL  rr   rM  rN  rP  r4   r8   rQ  $  rR  z;ImageFileDirectory_v2.load_signed_rational.<locals>.combinec                 3  rS  rr   r4   rT  rW  r4   r8   r_   '  rX  z=ImageFileDirectory_v2.load_signed_rational.<locals>.<genexpr>r   r
   rY  r[  r4   r]  r8   load_signed_rational   r_  z*ImageFileDirectory_v2.load_signed_rationalc                   r`  )Nr   c                 3  s*    | ]} j d gt|ddR  V  qdS )2lii   N)r   ry   rb  r   r4   r8   r_   +  s
    
z>ImageFileDirectory_v2.write_signed_rational.<locals>.<genexpr>r   r   r4   r   r8   write_signed_rational)  s   z+ImageFileDirectory_v2.write_signed_rationalc                 C  s8   | |}t||krd| dt| d}t||S )Nz&Corrupt EXIF data.  Expecting to read  bytes but only got z. )readr]   OSError)r}   fpr   retr   r4   r4   r8   _ensure_read0  s   
z"ImageFileDirectory_v2._ensure_readc              
   C  sl  |    | | _z| jr| d| |dn	| d| |dd }t|D ]}| jr7| d| |dn	| d| |d	\}}}}t|| j	j
}t|d
}	d| d| d|	 d| d	}
z	| j| \}}W n ty{   td|
| Y q(w || }|| jrdndkr| }| | jrdnd|\}|
d| d| 7 }
|| t||}|| n|d | }t||krtd| dt| d|  t|
 q(|st|
 q(|| j|< || j|< |
d|dkrd| nt| 7 }
t|
 q(| jr| d| |dn	| d| |d\| _W d S  ty5 }
 ztt|
 W Y d }
~
d S d }
~
ww )Nr   r)   r7  r   r   HHQ8s   HHL4sr=   unknownztag:  (
) - type: )z%s - unsupported type %sr!   r<   z Tag Location: z - Data Location: z/Possibly corrupt EXIF data.  Expecting to read rj  z. Skipping tag 
 - value: rD   <table: %d bytes>)r   tellr   r   r   ro  ranger   r   r   r   r   getr   KeyErrorloggerdebugseekr   
_safe_readr]   r,  r-  r   r   r   r   rl  r   )r}   rm  	tag_countir  r  countr   tagnametypnamer   	unit_sizer  r   hereoffsetr4   r4   r8   load:  sz   






zImageFileDirectory_v2.loadr   c              
   C  s  |  dt| j}g }|t| t| jd  d }d }t| j D ]\}}|tkr/t|}| j|}t	d||t
| |tjkoHt|t}|rs| jdkrSd}	nd}	t|	|d}
| j| }| D ]\}}||
|< qd|
|}nt|trz|n|f}| j| | g|R  }t|| jj}|rd	nt|d
}d| d| d| d| d	}|dt|dkrdt| nt| 7 }t	| |rd}n|tjtjtjfv rt|}nt|}t|dkr|||||dddf q#|||||  d||f |t|d d d 7 }q#|d ur8|| \}}}}}|r!d}t||  d| d|d | }|||||f||< |D ]!\}}}}}t	d|||t
|t
| ||  d||||7 }q:|d7 }|D ]\}}}}}||7 }t|d@ rx|d7 }qb|S )Nr7  r=   r!   zTag %s, Type: %s, Value: %sr   s   II*    s   MM *   )r   ifdrs  zsave: rt  ru  rv  rw  r?   rx  r
   rE  r   r<   r   z&multistrip support not yet implementedr   z%s %s %s %s %srr  s       ) r   r]   r   sortedr   STRIPOFFSETSr   r{  r}  r~  r   r   r%  r   r   r   r   tobytesr  r   r   r   r   r   r   r)  r(  r  appendljustNotImplementedErrorr   )r}   r  resultentriesstripoffsetsr  r   r  r/  r   r  r   ifd_tag	ifd_valuer   r  r  r   r  r4   r4   r8   r  }  sl   





zImageFileDirectory_v2.tobytesc                 C  sN   |  dkr|| j| ddd  |  }| |}|| |t| S )Nr   HL*   r)   )ry  writer   r   r  r]   )r}   rm  r  r  r4   r4   r8   save  s   

zImageFileDirectory_v2.save)r   NNr   rM   )6r   r   r   r   r   __annotations__r   r   r   rf   r  r   setterr   r   r   r  r	  r  r  r  r2  r4  r   r   listmapr   r   r#  r%  SIGNED_BYTEr$  r&  FLOATr'  IFDLONG8r   rA  r   rD  rH  rJ  r^  rc  rd  re  rg  ri  ro  r  r  r  r4   r4   r4   r8   r     s|   
 <
 

P















CMr   r   r   r   write_c                      s   e Zd ZU dZ fddZedd Zedd Zded< 	 e	d	d
 Z
dd Zdd Zdd Zdd Zdd Zdd Z  Z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] = TiffTags.ASCII
        print(ifd[key])
        ('Some Data',)

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

    Values are returned as a tuple.

    ..  deprecated:: 3.0.0
    c                   s   t  j|i | d| _d S )NT)superr   r   )r}   r~   kwargs	__class__r4   r8   r     s   
zImageFileDirectory_v1.__init__c                 C  r   rr   )r   r   r4   r4   r8   r     r   zImageFileDirectory_v1.<lambda>c                 C  r   rr   )r   r   r4   r4   r8   r     r   zdict[int, int]r   c                 C  s(   | |j d}|j|_|j|_|j|_|S )a2  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`

        re   )rf   r   r   r   )clsoriginalr  r4   r4   r8   from_v2  s
   zImageFileDirectory_v1.from_v2c                 C  s4   t | jd}t| j|_t| j|_t| j|_|S )a2  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`

        re   )r   rf   r   r   r   r   )r}   r  r4   r4   r8   to_v2  s
   zImageFileDirectory_v1.to_v2c                 C  r
  rr   )r   r   r  r4   r4   r8   r     r  z"ImageFileDirectory_v1.__contains__c                 C  r   rr   )r]   r  r   r   r   r4   r4   r8   r  #  r  zImageFileDirectory_v1.__len__c                 C  r   rr   )r3  r  r   r   r   r4   r4   r8   r4  &  r  zImageFileDirectory_v1.__iter__c                 C  s   dD ]	}|  ||| qd S N)FT)r  )r}   r  r   r   r4   r4   r8   r  )  s   z!ImageFileDirectory_v1.__setitem__c                 C  sn   || j vr&| j| }| j| }| j| \}}dD ]}| ||| ||| q| j | }t|ttfs5|f}|S r  )r   r   r   r   r  r   r  r  )r}   r  r   r  r   r  legacyrm   r4   r4   r8   r	  -  s   



z!ImageFileDirectory_v1.__getitem__)r   r   r   r   r   r   tagstagdatar  classmethodr  r  r  r  r4  r  r	  __classcell__r4   r4   r  r8   r    s   
 
r  c                      s   e Zd ZdZdZdZd fdd	Zdd Zed	d
 Z	dd Z
dd Zdd Zdd Zdd Z fddZdd Zdd Zdd Z  ZS )TiffImageFileTIFFz
Adobe TIFFFNc                   s"   d | _ 	 d | _	 t || d S rr   )tag_v2r  r  r   )r}   rm  filenamer  r4   r8   r   G  s
   zTiffImageFile.__init__c                 C  s   | j d}|d dkr|| j d7 }t|| _d| _| jj | _| _d| _| j | _	g | _
d| _td td| j tdt| | d	 dS )
z#Open the first image in a TIFF filer)   r   r   Nri   z*** TiffImageFile._open ***z- __first: %sz	- ifh: %sr   )rm  rk  r   r  r  r   _TiffImageFile__first_TiffImageFile__next_TiffImageFile__frame_fp
_frame_pos	_n_framesr}  r~  r   _seek)r}   r   r4   r4   r8   _openP  s   

zTiffImageFile._openc                 C  sX   | j d u r)|  }| t| j | j d u r$| |  d  | j d u s| | | j S )Nr
   )r  ry  r  r]   r  r  )r}   currentr4   r4   r8   n_framesk  s   



zTiffImageFile.n_framesc                 C  s<   |  |sdS | | t| j tj| j| j| _dS )z%Select a given frame as current imageN)	_seek_checkr  r   _decompression_bomb_checkr   corenewmodeim)r}   framer4   r4   r8   r  u  s
   

zTiffImageFile.seekc                 C  sX  | j | _| j  t| j|kr| jsd}t|td|| j	| j| j  | jdkr2d}t
|| j| j | j| j td| j  | j| j | jj| jv r[d| _n| jj| _| jdkrj|d | _t| jdkrw| jdk| _|  j	d7  _	t| j|ks| j| j|  | j| j |   t| j | _| _|| _	|   d S )Nzno more images in TIFF filez9Seeking to frame %s, on frame %s, __next %s, location: %sl            zUnable to seek to framezLoading tags, location: %sr   r
   )r  rm  ry  r]   r  r  EOFErrorr}  r~  r  r+  r  r  r  r  r   r  is_animated_reload_exifr  r  r  r  _setup)r}   r  r   r4   r4   r8   r    sF   




zTiffImageFile._seekc                 C  r   )zReturn the current frame number)r  r   r4   r4   r8   ry    r   zTiffImageFile.tellc                 C  s   t | jv r| | jt  S i S )z
        Returns a dictionary containing the XMP tags.
        Requires defusedxml to be installed.

        :returns: XMP tags in a dictionary.
        )XMPr  _getxmpr   r4   r4   r8   getxmp  s   zTiffImageFile.getxmpc                 C  s   i }| j tjj}|r`|dd dkr`t|dd }t|d d d d }t|d| d|  }|d| d| |  }d|i||< |td| | d d d }|dd dks|S )	a5  
        Returns a dictionary of Photoshop "Image Resource Blocks".
        The keys are the image resource ID. For more information, see
        https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#50577409_pgfId-1037727

        :returns: Photoshop "Image Resource Blocks" in a dictionary.
        Nr!   s   8BIMr%   r
   r   rf  r   )	r  r{  r   BaseImageResourcesi16mathceili32)r}   blocksrm   idnr   r   r4   r4   r8   get_photoshop_blocks  s   "z"TiffImageFile.get_photoshop_blocksc                   s   | j r
| jr
|  S t  S rr   )tileuse_load_libtiff_load_libtiffr  r  r   r  r4   r8   r    s   
zTiffImageFile.loadc                 C  sn   | j sd| _| j  |  }tjD ]}||vrq|| qtj	| dd t
jj| jv r5| jt
jj= d S d S )NT)in_place)r  !_close_exclusive_fp_after_loadingrm  ry  getexifr   TAGS_V2_GROUPSget_ifdr   exif_transposer   r  Orientationr  )r}   exifkeyr4   r4   r8   load_end  s   

zTiffImageFile.load_endc           
   
   C  s  t j |  |   t| jdksd}t|| jd d }t| jd d }zt| jdo2| j	 }t| jdr>| j
  W n tyJ   d}Y nw |rQ||d< t | jd	t|| j}z	|| j| W n tyy } zd
}t||d}~ww | jo| j }t| jdrtd || j \}}	n,|rtd |s| jd |d\}}	ntd | jd || j \}}	g | _d| _|   |r| j  d| _|	dk rt|	t j | S )zWOverload method triggered when we detect a compressed tiff
        Calls out to libtiffr
   zNot exactly one tiler   r   filenoflushFr   libtiffzCouldn't set the imageNgetvaluez5have getvalue. just sending in a string from getvaluez3have fileno, calling fileno version of the decoder.s   fpfpz+don't have fileno or getvalue. just reading)r   r  load_preparer]   r  rl  r  r   rm  r  r  _getdecoderr  r  decoderconfigsetimager  r+  _exclusive_fpr  r}  r~  rG  r  r  rk  readonlyr  close)
r}   r   extentsr~   rm  decodereclose_self_fpr  errr4   r4   r8   r    s`   






zTiffImageFile._load_libtiffc              
   C  s  d| j v rd}t|t| j td | _| j td| _| j td}| jdkr+d}| j t	d}t
d t
d| j t
d	| t
d
| j t
d| t
d| jt t| j t}t| j t}||f| _t
d| j | j td}t|dkrt|t|  krdkrn nd}| j td}| j td}|dv rd}	n	|dkrd}	nd}	|	t|7 }	t|}
| j t| jdkr|dv rdnd}|tkrt
d| d}t|||
k r|d| }n||
kr|
dkr|| }t||krd}t|| j j|||||f}t
d| z	t| \| _}W n t y6 } zt
d d}t||d}~ww t
d| t
d| j! | j| j"d< | j t#d}| j t$d}|r|r| j t%}|dkrq||f| j"d < n,|dkr|d! |d! f| j"d < n|du r||f| j"d < ||f| j"d"< n||f| j"d"< d } }}g | _&t'p| jd#k| _(| j(r|dkr|dd d |dd  }t
d| t| \| _}|d$krd%}d&|v r|)d&d'}d(|v r|)d(d'}|dkr| jd)kr| jdkrd*}|| jd+| j j*f}| j&+d,dd||fd|f nt,| j v s't-| j v rt,| j v r?| j t, }| j t.|}| jd }n| j t- }| j t/}| j t0}|D ]h}|| |krd|t1| d- }nd}|}| jdkrv|| }||	 }|t|df}| j&+| j||t|| |t|| |f||f || }|| jd krd|| }}|| jd krd }}|d7 }qRnt
d. d}t|t2| j v r| j t2 | j"d/< | j!d0v rd1d2 | j t3 D }t45d3d46|| _7dS dS )5z-Setup this image object based on current tagsi  z+Windows Media Photo files not yet supportedr
   r   r&   r%   z*** Summary ***z- compression: %sz - photometric_interpretation: %sz- planar_configuration: %sz- fill_order: %sz- YCbCr subsampling: %sz
- size: %sr:   r4   )r   r%   r)   r   r#   r!   )r   r%   z.More samples per pixel than can be decoded: %sz#Invalid value for samples per pixelNzunknown data organizationzformat key: %sz- unsupported formatzunknown pixel modez- raw mode: %sz- pil mode: %scompressionr   dpigRQ@
resolutionr   r>   I;16Nz;16Bz;16Nz;16Lr(   rK   Fr  r)   z- unsupported data organizationicc_profilerW   rX   c                 S  s   g | ]}t |d  qS r   r   )r5   r:  r4   r4   r8   r    s    z(TiffImageFile._setup.<locals>.<listcomp>RGB;Lr   )8r  rl  COMPRESSION_INFOr{  COMPRESSION_compressionPLANAR_CONFIGURATION_planar_configurationPHOTOMETRIC_INTERPRETATION	FILLORDERr}  r~  r  YCBCRSUBSAMPLINGr  
IMAGEWIDTHIMAGELENGTH_sizer   SAMPLEFORMATr]   rv   ru   BITSPERSAMPLEEXTRASAMPLESSAMPLESPERPIXELMAX_SAMPLESPERPIXELerrorr   rf   	OPEN_INFO_moder|  r  r  X_RESOLUTIONY_RESOLUTIONRESOLUTION_UNITr  READ_LIBTIFFr  r   r  r  r  TILEOFFSETSROWSPERSTRIP	TILEWIDTH
TILELENGTHsum
ICCPROFILECOLORMAPr   r   r   palette)r}   r   photo	fillorderxsizeysizesample_format	bps_tupleextra_tuple	bps_countbps_actual_countsamples_per_pixelr  rawmoder  xresyresresunitxylayerrO  offsetsr;  wr  stridetile_rawmoder  r4   r4   r8   r  =  s  



,











zTiffImageFile._setup)NN)r   r   r   formatformat_descriptionr  r   r  r   r  r  r  ry  r  r  r  r  r  r  r  r4   r4   r  r8   r  B  s"    	
	,	Ur  r;   r<   rJ   rW   rX   rA   rH   r>   rB   rE   rF   rK   rN   rL   rY   YCbCrr\   rI   r@   rC   rG   )rC   rG   c           5      C  s(	  zt | j \}}}}}}W n ty% }	 zd| j d}
t|
|	d }	~	ww t|d}| j}| j}z|d }W n tyM   | jd}t	|t
rKd }Y nw |d u rUd}n|dkr\d}n|dkrbd	}tpg|dk}d
|t< | jd |t< | jd
 |t< d|v r|d }nd|v r|d }t	|trt }|| |}ni }tdt| t	|tr| }|D ]/}t	|tjr|tjv r||||< n||||< z
|j| |j|< W q ty   Y qw t| drtt t!t"t#t$fD ]}|| j%v r| j%| ||< | j%j| |j|< q|d| jd}|r||t&< 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f
D ]\}}||v rH|| ||< q9|d}|rbd|t< |d |t < |d
 |t!< |dkrx||t,< t-|d
krxt-||t.< |d ur||t/< |d
kr||t0< t1|vr||t1< nF| jdv r|t1 dkr| jdkr| 2 }| }t3|j4D ]}t3|j5D ]}|||f dkrdnd|||f< qq|} nt67| } | jd v r| j89d!d"}g }t-|d# }t3d#D ]}|d$d% ||| ||d
   D 7 }|dgd&|  7 }q||t:< |t |t }}t-|||d  d' d(  } t;|vrh|r[|d)t<}!| dkrCd
nt=|!|  |}"|dkrZt=|"d' d( d( |}"n|}"|"dkrdd
}"|"|t;< | dkrod
n| |t;  }#||t;  d
 |t;  }$|#d*krtj>|jt?< |#f|$d
  | | |#|$d
   f |t?< t@t3d|#|$ |#|tA< tB|d
|tC< | jd+krtDd,tEd-iF D ]\}%}&|G|%|& qtHtItJtKg}'|rhd.|v r|d. }(t	|(t
r|(dk s|(d/krd0}
tL|
|dkrd1}
tL|
|(|tM< td2 td3tN|F  d})t|d4r6z|Od tPQ|R })W n tSjTy5   Y nw i }*|'tUtEt?tAtVtWg7 }'t,|d i}+i },t| d5rU| jX },i tY| di |,}-t0|-v rg|-t0= tZ[|F |-F D ]j\}%}&|%tj\vrtYtj]d6d7sqq|%|jv r|j|% |*|%< nt	|&t
t^t_tfsqqt`|%ja}.|.r|.|*|%< |%|+vr|%|'vrt	|&t_r|&bd8d9d: |+|%< qqt	|&tcrt^|&|+|%< qq|&|+|%< qqt0|+v rt-|+t0 d
kr|+t0 d |+t0< td;tN|+F  | jd<v rd=}t|+F }/|/d  |||)||/|*f}0te| jd>|0|}1|1f| j8d?| j  	 |1bdAd
d  \}2}3|)s=|g|3 |2rAnq*|)rXztPh|) W n
 tyW   Y nw |2dk rgdB|2 dC}
t|
n |'D ]}%||%= qj|i|}4tjk| |dd?| j |4|| d
ffg dD|v r|| _ld S d S )ENzcannot write mode z as TIFFre   r  r   r&   r(   r.   r*   r
   r   tiffinfor  zTiffinfo Keys: %sr  r  descriptionr  x_resolutiony_resolutionresolution_unitsoftware	date_timeartist	copyrightr  r   r:   )r;   r<   r;   r   r  rK   r  r   c                 S  s   g | ]}|d  qS r  r4   r  r4   r4   r8   r    s    z_save.<locals>.<listcomp>r   r'   r)   
strip_sizer   r3  )r
   r
   )r   r      r   r>  r   qualityd   zInvalid quality settingz5quality setting only supported for 'jpeg' compressionzSaving using libtiff encoderz	Items: %sr  r  libtiff_support_custom_tagsFr  r   rE  zConverted items: %s)r@   r>   r  r  rO   Ti @  zencoder error z when writing image file_debug_multipage)m	SAVE_INFOr  r|  rl  r   encoderinfoencoderconfigr  r{  r   r  WRITE_LIBTIFFr   r   r  r  r  r   Exifr  r}  r~  r  r  r  r   r  r  r   r   r   r  r  r  IPTC_NAA_CHUNKPHOTOSHOP_CHUNKr  r  r  IMAGEDESCRIPTIONSOFTWARE	DATE_TIMEARTIST	COPYRIGHTr	  r]   r  r
  r  r  copyrz  heightwidthr   invertr  
getpaletter  r  
STRIP_SIZEru   r%  STRIPBYTECOUNTSr  r  COMPRESSION_INFO_REVr  r  REFERENCEBLACKWHITEr   
setdefaultr  r  r  TILEBYTECOUNTSr+  JPEGQUALITYr  r  osdupr  ioUnsupportedOperationOSUBFILETYPETRANSFERFUNCTIONSUBIFDr  r{   	itertoolschainLIBTIFF_COREr  r   r   r   r  r  rk   sort_getencoderr  r  r  r  r   _saverB  )5r  rm  r  r&  rf   r  r1  bitsextrar  r   r  rD  rE  r  r  r  r  r  iccr   r  inverted_impxr+  r*  lutcolormapcolorsr  r.  r;  r/  im_strip_sizerows_per_stripstrip_byte_countsstrips_per_imager  r   	blocklistr?  r  typesatts
legacy_ifdsupplied_tagsr  r  rO  encodererrcoder   r  r4   r4   r8   rg  >  s  













&
&






 











rg  c                   @  s   e Zd Zg dZh dZd1ddZdd Zdd	 Zd
d Zdd Z	dd Z
dd Zejf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d$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd2d.d/Zd0S )3AppendingTiffWriter)r   r
   r
   r   r!   r)   r
   r
   r   r!   r)   r!   r)   r!   r   r!   r)   >       	     r   r   Fc                 C  st   t |dr|| _d| _n"|| _d| _zt||rdnd| _W n ty-   t|d| _Y nw | j | _|   d S )Nrk  FTzw+bzr+b)	r   r   close_fpr   openrl  ry  	beginningsetup)r}   fnr  r4   r4   r8   r     s   
zAppendingTiffWriter.__init__c                 C  s   | j | jtj d | _d| _| j d | _}|sd| _	d S d| _	|dkr,| 
d n|dkr6| 
d nd	}t||   |   d S )
Nr   r!   TFrb   r   ra   r   zInvalid TIFF file header)r   r  r  r[  SEEK_SETwhereToWriteNewIFDOffsetoffsetOfNewPagerk  IIMMisFirst	setEndianRuntimeErrorskipIFDsgoToEnd)r}   iimmr   r4   r4   r8   r    s    zAppendingTiffWriter.setupc                 C  s   | j rd S | j| j | jd}|sd S || jkr!d}t||  }|| j7 }| j| j | 	| | j| | 
  d S )Nr!   z1IIMM of new page doesn't match IIMM of first page)r  r   r  r  rk  r  r  readLongr  	writeLongfixIFD)r}   r  r   
ifd_offsetr4   r4   r8   finalize  s   


zAppendingTiffWriter.finalizec                 C  s   |    |   d S rr   )r  r  r   r4   r4   r8   newFrame  s   zAppendingTiffWriter.newFramec                 C  s   | S rr   r4   r   r4   r4   r8   	__enter__  s   zAppendingTiffWriter.__enter__c                 C  s   | j r|   dS )NF)r  r  )r}   exc_type	exc_value	tracebackr4   r4   r8   __exit__  s   zAppendingTiffWriter.__exit__c                 C  s   | j  | j S rr   )r   ry  r  r   r4   r4   r8   ry    rh   zAppendingTiffWriter.tellc                 C  s*   |t jkr
|| j7 }| j|| |  S rr   )r[  r  r  r   r  ry  )r}   r  whencer4   r4   r8   r    s   

zAppendingTiffWriter.seekc                 C  s^   | j dtj | j  }d|d  }d|  k rdk r'n n| j t| | j  | _d S )Nr   r?   )r   r  r[  SEEK_ENDry  r  r  r  )r}   pos	pad_bytesr4   r4   r8   r    s   
zAppendingTiffWriter.goToEndc                 C  s.   || _ | j d | _| j d | _| j d | _d S )Nr<   r7  HHL)endianlongFmtshortFmt	tagFormat)r}   r  r4   r4   r8   r    s   zAppendingTiffWriter.setEndianc                 C  sP   	 |   }|dkr| j d | _d S | j| |  }| j|d tj q)NTr   r!   r=   )r  r   ry  r  r  	readShortr[  SEEK_CUR)r}   r  num_tagsr4   r4   r8   r    s   zAppendingTiffWriter.skipIFDsc                 C  s   | j |S rr   )r   r  rC  r4   r4   r8   r    r   zAppendingTiffWriter.writec                 C     t | j| jd\}|S )Nr   )r   r5  r  r   rk  rI  r4   r4   r8   r       zAppendingTiffWriter.readShortc                 C  r  rc   )r   r5  r  r   rk  rI  r4   r4   r8   r    r  zAppendingTiffWriter.readLongc                 C  R   | j dtj | j t| j|}|d ur%|dkr'd| d}t|d S d S )Nr!   wrote only  bytes but wanted 4	r   r  r[  r  r  r   r6  r  r  r}   r   bytes_writtenr   r4   r4   r8   rewriteLastShortToLong     z*AppendingTiffWriter.rewriteLastShortToLongc                 C  r  )Nr  r   r   bytes but wanted 2)	r   r  r[  r  r  r   r6  r  r  r  r4   r4   r8   rewriteLastShort  r  z$AppendingTiffWriter.rewriteLastShortc                 C  r  )Nr!   r  r  r  r  r4   r4   r8   rewriteLastLong  r  z#AppendingTiffWriter.rewriteLastLongc                 C  B   | j t| j|}|d ur|dkrd| d}t|d S d S )Nr   r  r  )r   r  r   r6  r  r  r  r4   r4   r8   
writeShort  
   zAppendingTiffWriter.writeShortc                 C  r  )Nr!   r  r  )r   r  r   r6  r  r  r  r4   r4   r8   r    r  zAppendingTiffWriter.writeLongc                 C  s   |    | j  d S rr   )r  r   r  r   r4   r4   r8   r  "  s   zAppendingTiffWriter.closec                 C  s   |   }t|D ]u}t| j| jd\}}}| j| }|| }|dk}|s5|  }	|	| j	7 }	| 
|	 || jv rs| j }
|rV| j||dk|dkd | j|
d  n| j|	 | j||dk|dkd | j|
 d  }	}
q|r}| jdtj qd S )Nr)   r!   r   )isShortisLong)r  rz  r   r5  r  r   rk  
fieldSizesr  r  r  Tagsry  
fixOffsetsr  r[  r  )r}   r  r  r  
field_typer  
field_size
total_sizeis_localr  cur_posr4   r4   r8   r  &  s6   





zAppendingTiffWriter.fixIFDc                 C  s   |s
|s
d}t |t|D ]J}|r|  n|  }|| j7 }|rK|dkrK|dkr/d}t || | | jdtj	 | 
tj | jdtj	 q|rS| | q| | qd S )Nz offset is neither short nor longr   r
   znot implementedir)   )r  rz  r  r  r  r  r   r  r[  r  r  r   r%  r  r  )r}   r  r  r  r   r  r  r4   r4   r8   r  I  s$   

zAppendingTiffWriter.fixOffsetsN)F)FF)r   r   r   r  r  r   r  r  r  r  r  ry  r]  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r4   r4   r4   r8   r{  h  s2    
	
#r{  c              	   C  s   | j  }| j}t|dg }t| ds|st| ||S |  }zWt|;}| g| D ]-}||_ ||_t|ds<d}	n|j	}	t
|	D ]}
||
 |  t||| |  qCq,W d    n1 sdw   Y  W | | d S W | | d S | | w )Nappend_imagesr  r
   )rD  rO  rE  r  r{  r   rg  ry  r{  r  rz  r  r  r  )r  rm  r  rD  rE  r  cur_idxtfimsnfrr   r4   r4   r8   	_save_allc  s2   




r  z.tifz.tiffz
image/tiff)y
__future__r   r]  rb  loggingr  r[  r   r,  collections.abcr   	fractionsr   numbersr   r   typingr   r   r	    r   r   r   r   r   r   _binaryr   r  r   r  r   r   	getLoggerr   r}  r  rF  IFD_LEGACY_APIrT  r   r   r_  r  r  r	  r  r  r  rJ  r  r  r  rU  r  r  r   r  r`  rK  rL  rM  	PREDICTORr  r  r  r  rY  ra  r
  r  
JPEGTABLESr  rW  rN  rH  rI  r  EXIFIFDr  rZ  IMAGEJ_META_DATA_BYTE_COUNTSIMAGEJ_META_DATAr  r   rV  r  rv   r  rd   rg   rq   ry   r   r   r   rk   r   r   r   r  
_IFDv2Baser   r   r   r   setattrr  ImageFileDirectoryr  rC  rg  r{  r  register_openr1  register_saveregister_save_allregister_extensionsregister_mimer4   r4   r4   r8   <module>   s&  ( 
	
	
 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghi|
|   s\   f	
  , |