o
    e+                 #   @  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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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/ZEd0ZFd1ZGd2ZHd3ZId4ZJd5ZKd6ZLd7ZMd8ZNi dd9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYZOdZd[ eOP D ZQi 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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dbd]fdce&d d\ddbd]fdce%d d\d:dbd]fdde&d d\d:dbd]fdde%dd\ddbd]fdee&dd\ddbd]fdee%dd\d:dbd]fdfe&dd\d:dbd]fdfe%d d\ddgd]fdhi e&d d\ddgd]fdhe%d d\d:dgd]fdie&d d\d:dgd]fdie%dd\ddgd]fdje&dd\ddgd]fdje%dd\d:dgd]fdke&dd\d:dgd]fdke%d d\ddld]fdme&d d\ddld]fdme%d d\d:dld]fdne&d d\d:dld]fdne%dd\ddld]fdoe&dd\ddld]fdoe%ddbddld]fdoe&ddbddld]fdoe%dd\d:dld]fdpe&dd\d:dld]fdpi e%dd\ddqd]fdre%d d\ddsd]fdte%dd\ddsd]fdte&dd\ddsd]fdue%dd\d:dsd]fdve%ddbddsd]fdwe&ddbddsd]fdxe%d dyddzd]fd{e&d dyddzd]fd|e%dd\ddzd]fd}e%ddbddzd]fd~e&ddbddzd]fde%ddyddzd]fd{e&ddyddzd]fd|e%dd\dddbfde&dd\dddbf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ddbfde&d:d\dddbf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ddbfdi e&d:d\dddbf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dbd]fde&d<d\ddbd]fde%d<d\d:dbd]fde&d<d\d:dbd]fde%d<d\ddgd]fde&d<d\ddgd]fde%d<d\d:dgd]fde&d<d\d:dgd]fde%d<d\ddld]fde&d<d\ddld]fde%d<d\dddbfde&d<d\dddbfde%d<d\d:dld]fde&d<d\d:dld]fde%d@d\ddd]fde&d@d\ddd]fde%d@d\dddfde&d@d\dddfde%d@d\dddfde&d@d\dddfde%d@d\ddd]fde%dBd\ddld]fdoe&dBd\ddld]fdoe%dBd\ddd]fde&dBd\ddd]fde%dFd\ddd]fde&dFd\ddd]fdiZReSdd eRD ZTg dZUdd ZVdd ZWdd ZXi ZYi ZZG dd deZ[G dd de
Z\eYe\_YeZe\_ZeP D ]!\Z]Z^e^_ddZ^e`e\de^ eYe] d  e`e\de^ eZe]  qP[Y[Z[][^G ddĄ de\ZaeaZbG ddƄ dejZci dde%ddd\dfdde%dddldfdde%dddd:fdde%d<ddldfdde%d<ddd:fdde%dd:dzdfdde%dddsdfdde%dd:dsdfdde%dd<dzdfdde%d:dddfdde%d:ddd fdde%d:ddd:fdde%d@dddfdde%dBdddfdde%dFdddfdde&dd:dzdfdde&dddsdfde&dd:dsdfde&dd<dzdfdܜZdddބ ZeG dd dZfdd ZgehecjieceV ejecjiee ekecjieg elecjiddg emecjid dS )    )annotationsN)MutableMapping)Fraction)NumberRational   )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  r0   ).0kvr0   r0   F/var/www/html/venv/lib/python3.10/site-packages/PIL/TiffImagePlugin.py
<dictcomp>   s    r5   r   r0   )1z1;I)r7   z1;IR)r7   r7   )r7   z1;R)r   )LzL;2I)r8   zL;2IR)r8   zL;2)r8   zL;2R)r   )r8   zL;4I)r8   zL;4IR)r8   zL;4)r8   zL;4R)r%   )r8   zL;I)r8   zL;IR)r8   r8   )r8   zL;R)   )I;16zI;12)   )r:   r:   )I;16Br<   )r:   zI;16R)II;16S)r=   I;16BS)r   )    )FF;32F)rA   F;32BF)r=   zI;32N)r=   I;32S)r=   I;32BS)r%   r%   )LArF   )r%   r%   r%   )RGBrG   )rG   zRGB;R)r%   r%   r%   r%   )RGBArH   r   )RGBXrJ   )r%   r%   r%   r%   r%   r   r   )rJ   RGBXX)r%   r%   r%   r%   r%   r%   )r   r   r   )rJ   RGBXXX)rH   RGBa)r   r   )rH   RGBaX)r   r   r   )rH   RGBaXX)r   r   )rH   RGBAX)r   r   r   )rH   RGBAXX)i  )r;   r;   r;   )rG   zRGB;16L)rG   zRGB;16B)r;   r;   r;   r;   )rH   zRGBA;16L)rH   zRGBA;16B)rJ   zRGBX;16L)rJ   zRGBX;16B)rH   zRGBa;16L)rH   zRGBa;16B)PzP;1)rS   zP;1R)rS   zP;2)rS   zP;2R)rS   zP;4)rS   zP;4R)rS   rS   )PArT   )rS   zP;R)CMYKrU   )rU   CMYKX)rU   CMYKXX)rU   zCMYK;16L)rG   rJ   )LABrX   c                 c  s    | ]	}t |d  V  qdS )r   N)len)r1   key_tpr0   r0   r4   	<genexpr>
      r[   )   MM *   II* s   MM* s   II *s   MM +s   II+ c                 C  s   | d d t v S Nr   )PREFIXESprefixr0   r0   r4   _accept     rc   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_dr0   r0   r4   _limit_rational  s   rm   c                 C  sP   t | }|j|jf}t||k rt| t|}t||kr&t | } t| |}|S N)r   	numeratordenominatorminrm   rf   max)ri   rj   min_valfracrl   r0   r0   r4   _limit_signed_rational   s   
ru   c                   @  sX  e Zd ZdZ	 dZd4d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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)e*e+d2red2Z,d3S d3S )5rg   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_denominator_valr   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   )	
isinstancerg   ro   rv   rp   rw   rx   r   float)selfvaluerp   r0   r0   r4   __init__C  s   


zIFDRational.__init__c                 C     | j S rn   )rv   r|   r0   r0   r4   ro   ]     zIFDRational.numeratorc                 C  r   rn   )rw   r   r0   r0   r4   rp   a  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   )rp   ro   rx   limit_denominator)r|   max_denominatorfr0   r0   r4   rh   e  s   
zIFDRational.limit_rationalc                 C  s   t t| jS rn   )strr{   rx   r   r0   r0   r4   __repr__r  s   zIFDRational.__repr__c                 C  s
   | j  S rn   )rx   __hash__r   r0   r0   r4   r   u  s   
zIFDRational.__hash__c                 C  s0   | j }t|tr|j }t|trt|}||kS rn   )rx   rz   rg   r{   )r|   otherri   r0   r0   r4   __eq__x  s   

zIFDRational.__eq__c                 C  s   | j | j| jgS rn   )rx   rv   rw   r   r0   r0   r4   __getstate__  rd   zIFDRational.__getstate__c                 C  s,   t | d |\}}}|| _|| _|| _d S )Nr   )rg   r~   rx   rv   rw   )r|   staterx   rv   rw   r0   r0   r4   __setstate__  s
   

zIFDRational.__setstate__c                       fdd}|S )Nc                   s   t | j | S rn   )getattrrx   )r|   argsopr0   r4   delegate  rd   z'IFDRational._delegate.<locals>.delegater0   )r   r   r0   r   r4   	_delegate  s   zIFDRational._delegate__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__Nr6   )-__name__
__module____qualname____doc__	__slots__r~   propertyro   rp   rh   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   r0   r0   r0   r4   rg   5  s\    



rg   c                   @  s  e Zd ZdZ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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d0d1fej d2d3fej!d4d5fej"d6d7fej#d,d-fej$d8d9fg	 ed:d:d_d<d=Z%ed:d>d? Z&ed@d:d_dAdBZ'ed@dCdD Z(edEdFd_dGdHZ)edEdIdJ Z*edKd:d_dLdMZ+edKdNdO Z,edPdFd_dQdRZ-edPdSdT Z.dUdV Z/dWdX Z0d`dZd[Z1d\d] Z2dS )a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``.

       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%   r8   r   F)rc   reprSyntaxError_prefixMM_endianII_bigtiffgrouptagtypereset_unpacknext_legacy_api)r|   ifhrb   r   msgr0   r0   r4   r~     s$   

,
zImageFileDirectory_v2.__init__c                 C  r   rn   )r   r   r0   r0   r4   <lambda>      zImageFileDirectory_v2.<lambda>c                 C  r   rn   )_offsetr   r0   r0   r4   r     r   c                 C  r   rn   )r   r   r0   r0   r4   r     r   c                 C  s   d}t |)Nz"Not allowing setting of legacy api)	Exception)r|   r}   r   r0   r0   r4   
legacy_api  s   z ImageFileDirectory_v2.legacy_apic                 C  s(   i | _ i | _i | _i | _d | _d | _d S rn   )_tags_v1_tags_v2_tagdatar   _nextr   r   r0   r0   r4   r     s   
zImageFileDirectory_v2.resetc                 C  s   t t| S rn   )r   dictr   r0   r0   r4   __str__%     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 r0   )r   lookupr   name)r1   coder}   r   r0   r4   r5   .  s    z/ImageFileDirectory_v2.named.<locals>.<dictcomp>)itemsr   r0   r   r4   named(  s   
zImageFileDirectory_v2.namedc                 C     t t| jt| jB S rn   )rY   setr   r   r   r0   r0   r4   __len__3     zImageFileDirectory_v2.__len__c                 C  sf   || j vr| j| }| j| }| j| \}}|| || j| |< | j | }| jr1t|ttfs1|f}|S rn   )r   r   r   _load_dispatchr   rz   tuplebytes)r|   tagdatatypsizehandlerri   r0   r0   r4   __getitem__6  s   



z!ImageFileDirectory_v2.__getitem__c                 C     || j v p	|| jv S rn   )r   r   r|   r   r0   r0   r4   __contains__A     z"ImageFileDirectory_v2.__contains__c                 C  s   |  ||| j d S rn   )_setitemr   )r|   r   r}   r0   r0   r4   __setitem__D  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 rn   )rz   rg   r1   r3   r0   r0   r4   r[   R      z1ImageFileDirectory_v2._setitem.<locals>.<genexpr>c                 s      | ]}|d kV  qdS r   Nr0   r   r0   r0   r4   r[   U      c                 s  r   rn   )rz   intr   r0   r0   r4   r[   X  r   c                 s  s(    | ]}d |  kodk n  V  qdS )r   r   Nr0   r   r0   r0   r4   r[   Y     & c                 s  s(    | ]}d |  k odk n  V  qdS )i i   Nr0   r   r0   r0   r4   r[   [  r   c                 s  r   r   r0   r   r0   r0   r4   r[   `  r   c                 s  r   rn   )rz   r{   r   r0   r0   r4   r[   c  r   c                 s  r   rn   )rz   r   r   r0   r0   r4   r[   e  r   c                 s  r   rn   )rz   r   r   r0   r0   r4   r[   g  r   c                 S  s&   g | ]}t |tr|d dn|qS )asciireplace)rz   r   encoder   r0   r0   r4   
<listcomp>k  s    z2ImageFileDirectory_v2._setitem.<locals>.<listcomp>c                 S  s"   g | ]}t |trt|n|qS r0   )rz   r   r{   r   r0   r0   r4   r   p  s   " c                 3  s    | ]}  |V  qd S rn   )cvt_enumr1   r}   infor0   r4   r[   t  r   r   zMetadata Warning, tag z had too many entries: z, expected 1r   )r   r   r   r   r   r   rz   r   type	UNDEFINEDallRATIONALSIGNED_RATIONALSHORTSIGNED_SHORTLONGSIGNED_LONGDOUBLEASCIIBYTEr   r   r   r   lengthrY   
ValueErrorwarningswarn)r|   r   r}   r   	basetypesvaluesis_ifddestr0   r   r4   r   G  sr   



 zImageFileDirectory_v2._setitemc                 C  s.   | j |d  | j|d  | j|d  d S rn   )r   popr   r   r   r0   r0   r4   __delitem__  s   z!ImageFileDirectory_v2.__delitem__c                 C  r   rn   )iterr   r   r   r   r0   r0   r4   __iter__  r   zImageFileDirectory_v2.__iter__c                 C  s   t | j| |S rn   )structunpackr   )r|   fmtr   r0   r0   r4   r     s   zImageFileDirectory_v2._unpackc                 G  s   t j| j| g|R  S rn   )r  packr   )r|   r  r  r0   r0   r4   _pack  r   zImageFileDirectory_v2._packc                   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   
startswithr   r   )funcr   idxr   r0   r4   	decorator  s
   z9ImageFileDirectory_v2._register_loader.<locals>.decoratorr0   )r"  r   r#  r0   r!  r4   _register_loader  s   z&ImageFileDirectory_v2._register_loaderc                   r   )Nc                   s   | t  < | S rn   )_write_dispatch)r   r"  r0   r4   r#    s   z9ImageFileDirectory_v2._register_writer.<locals>.decoratorr0   )r"  r#  r0   r&  r4   _register_writer  s   z&ImageFileDirectory_v2._register_writerc                   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 rn   )r   rY   r|   r   r   r  r   r0   r4   r     s   z7ImageFileDirectory_v2._register_basic.<locals>.<lambda>c                   s   d  fdd|D S )N    c                 3  s    | ]	}  |V  qd S rn   )r  r   )r  r|   r0   r4   r[     r\   zJImageFileDirectory_v2._register_basic.<locals>.<lambda>.<locals>.<genexpr>joinr|   r  )r  r   r4   r     s   T)r   r   r  calcsizer   r%  )idx_fmt_namer   r"  r   r0   r*  r4   _register_basic  s   
z%ImageFileDirectory_v2._register_basicHshortr8   longbzsigned bytehzsigned shortlzsigned longr   r{   ddoubler   long8r   Tc                 C     |S rn   r0   r)  r0   r0   r4   	load_byte     zImageFileDirectory_v2.load_bytec                 C  s*   t |tr	t|}t |trt|f}|S rn   )rz   rg   r   r   r|   r   r0   r0   r4   
write_byte  s
   


z ImageFileDirectory_v2.write_byter   c                 C  s"   | dr|d d }|ddS )N    re   zlatin-1r   )endswithdecoder)  r0   r0   r4   load_string  s   
z!ImageFileDirectory_v2.load_stringc                 C  s0   t |tr	t|}t |ts|dd}|d S )Nr   r   rA  )rz   r   r   r   r   r|   r}   r0   r0   r4   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   r8   c                       r| |fS t | |S rn   rg   ar6  r   r0   r4   combine     z4ImageFileDirectory_v2.load_rational.<locals>.combinec                 3      | ]
\}} ||V  qd S rn   r0   r1   numdenomrM  r0   r4   r[         z6ImageFileDirectory_v2.load_rational.<locals>.<genexpr>r   r   r   rY   r   zipr|   r   r   valsr0   rM  r   r4   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  rm   r1   rt   r   r0   r4   r[     s    
z7ImageFileDirectory_v2.write_rational.<locals>.<genexpr>r,  r.  r0   r   r4   write_rational  s   z$ImageFileDirectory_v2.write_rationalr#   c                 C  r<  rn   r0   r)  r0   r0   r4   load_undefined  r>  z$ImageFileDirectory_v2.load_undefinedc                 C  s   t |trt|dd}|S )Nr   r   )rz   r   r   r   rE  r0   r0   r4   write_undefined  s   
z%ImageFileDirectory_v2.write_undefined
   c                   rG  )	Nr   r8  c                   rH  rn   rI  rJ  rL  r0   r4   rM    rN  z;ImageFileDirectory_v2.load_signed_rational.<locals>.combinec                 3  rO  rn   r0   rP  rS  r0   r4   r[     rT  z=ImageFileDirectory_v2.load_signed_rational.<locals>.<genexpr>r   r   rU  rW  r0   rY  r4   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  ru   r^  r   r0   r4   r[     s
    
z>ImageFileDirectory_v2.write_signed_rational.<locals>.<genexpr>r,  r.  r0   r   r4   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. )readrY   OSError)r|   fpr   retr   r0   r0   r4   _ensure_read  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%   r3  r   r   HHQ8s   HHL4sr9   unknownztag:  (
) - type: )z%s - unsupported type %sr   r8   z Tag Location: z - Data Location: z/Possibly corrupt EXIF data.  Expecting to read rf  z. Skipping tag 
 - value: r@   <table: %d bytes>)r   tellr   r   r   rk  ranger   r   r   r   r   getr   KeyErrorloggerdebugseekr
   
_safe_readrY   r  r  r   r   r   r   rh  r   )r|   ri  	tag_countir   r   countr   tagnametypnamer   	unit_sizer   r   hereoffsetr0   r0   r4   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 )Nr3  r9   r   zTag %s, Type: %s, Value: %sr   s   II*    s   MM *   )r   ifdro  zsave: rp  rq  rr  rs  r;   rt  r   rA  r+  r8   r   z&multistrip support not yet implementedr   z%s %s %s %s %srn  s       ) r  rY   r   sortedr   STRIPOFFSETSr   rw  ry  rz  r   r   r  rz   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  r0   r0   r4   r  i  sl   





zImageFileDirectory_v2.tobytesc                 C  sN   |  dkr|| j| ddd  |  }| |}|| |t| S )Nr   HL*   r%   )ru  writer   r  r  rY   )r|   ri  r  r  r0   r0   r4   save  s   

zImageFileDirectory_v2.save)r   NNr/  rI   )3r   r   r   r   r~   r   rb   r  r   setterr   r   r   r   r   r   r   r   r  r  r   r  r$  r'  r2  listmapr   r  r  SIGNED_BYTEr  r  FLOATr  IFDLONG8r=  r@  rD  rF  rZ  r_  r`  ra  rc  re  rk  r  r  r  r0   r0   r0   r4   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__r0   r4   r~     s   
zImageFileDirectory_v1.__init__c                 C  r   rn   )r   r   r0   r0   r4   r     r   zImageFileDirectory_v1.<lambda>c                 C  r   rn   )r   r   r0   r0   r4   r     r   r   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`

        ra   )rb   r   r   r   )clsoriginalr  r0   r0   r4   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`

        ra   )r   rb   r   r   r   r   )r|   r  r0   r0   r4   to_v2  s
   zImageFileDirectory_v1.to_v2c                 C  r   rn   )r   r   r   r0   r0   r4   r     r   z"ImageFileDirectory_v1.__contains__c                 C  r   rn   )rY   r   r   r   r   r0   r0   r4   r     r   zImageFileDirectory_v1.__len__c                 C  r   rn   )r  r   r   r   r   r0   r0   r4   r    r   zImageFileDirectory_v1.__iter__c                 C  s   dD ]	}|  ||| qd S N)FT)r   )r|   r   r}   r   r0   r0   r4   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   rz   r   r   )r|   r   r   r   r   r   legacyri   r0   r0   r4   r     s   



z!ImageFileDirectory_v1.__getitem__)r   r   r   r   r~   r   tagstagdata__annotations__classmethodr  r  r   r   r  r   r   __classcell__r0   r0   r  r4   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 rn   )tag_v2r   r  r~   )r|   ri  filenamer  r0   r4   r~   3  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   Nre   z*** TiffImageFile._open ***z- __first: %sz	- ifh: %sr   )ri  rg  r   r  r  r   _TiffImageFile__first_TiffImageFile__next_TiffImageFile__frame_fp
_frame_pos	_n_framesry  rz  r   _seek)r|   r   r0   r0   r4   _open<  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  ru  r  rY   r  r{  )r|   currentr0   r0   r4   n_framesW  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|   framer0   r0   r4   r{  a  s
   

zTiffImageFile.seekc                 C  sB  | j | _| j  t| j|krz| jsd}t|td|| j	| j| j  | j
| j | j| j td| j  | j| j | jj| jv rPd| _n| jj| _| jdkr_|d | _t| jdkrl| 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: %szLoading tags, location: %sr   r   )r  ri  ru  rY   r  r  EOFErrorry  rz  r  r{  r  r  r  r   r  is_animated_reload_exifr  r  r   r  _setup)r|   r  r   r0   r0   r4   r  l  s@   



zTiffImageFile._seekc                 C  r   )zReturn the current frame number)r  r   r0   r0   r4   ru    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   r0   r0   r4   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   rb  r   )	r  rw  r   BaseImageResourcesi16mathceili32)r|   blocksri   idnr   r   r0   r0   r4   get_photoshop_blocks  s   "z"TiffImageFile.get_photoshop_blocksc                   s   | j r
| jr
|  S t  S rn   )tileuse_load_libtiff_load_libtiffr  r  r   r  r0   r4   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_loadingri  ru  getexifr   TAGS_V2_GROUPSget_ifdr   exif_transposer   r  Orientationr  )r|   exifkeyr0   r0   r4   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_preparerY   r  rh  r  r   ri  r  r  _getdecoderr  r   decoderconfigsetimager  r  _exclusive_fpr  ry  rz  rC  r  r{  rg  readonlyr  close)
r|   r   extentsr   ri  decodereclose_self_fpr  errr0   r0   r4   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: %sr6   r0   )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$   rG   Fr  r%   z- unsupported data organizationicc_profilerS   rT   c                 S  s   g | ]}t |d  qS r   r   )r1   r6  r0   r0   r4   r     s    z(TiffImageFile._setup.<locals>.<listcomp>RGB;Lr+  )8r  rh  COMPRESSION_INFOrw  COMPRESSION_compressionPLANAR_CONFIGURATION_planar_configurationPHOTOMETRIC_INTERPRETATION	FILLORDERry  rz  r   YCBCRSUBSAMPLINGr   
IMAGEWIDTHIMAGELENGTH_sizer   SAMPLEFORMATrY   rr   rq   BITSPERSAMPLEEXTRASAMPLESSAMPLESPERPIXELMAX_SAMPLESPERPIXELerrorr   rb   	OPEN_INFO_moderx  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layerrK  offsetsr7  wr  stridetile_rawmoder  r0   r0   r4   r  &  s  



,












zTiffImageFile._setup)NN)r   r   r   formatformat_descriptionr  r~   r  r   r  r{  r  ru  r  r  r  r  r  r  r  r0   r0   r  r4   r  .  s"    	
	)	Ur  r7   r8   rF   rS   rT   r=   rD   r:   r>   rA   rB   rG   rJ   rH   rU   YCbCrrX   rE   r<   r?   rC   )r?   rC   c           4      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}'|rgd.|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 }*|'tEt?tAtUtVg7 }'t,|d i}+i },t| d5rT| jW },i tX| di |,}-t0|-v rf|-t0= tYZ|F |-F D ]j\}%}&|%tj[vrtXtj\d6d7sqp|%|jv r|j|% |*|%< nt	|&t
t]t^tfsqpt_|%j`}.|.r|.|*|%< |%|+vr|%|'vrt	|&t^r|&ad8d9d: |+|%< qpt	|&tbrt]|&|+|%< qp|&|+|%< qpt0|+v rt-|+t0 d
kr|+t0 d |+t0< td;tN|+F  | jd<v rd=}t|+F }/|/c  |||)||/|*f}0td| jd>|0|}	|	e| j8d?| j  	 |	adAd
d  \}1}2|)s<|f|2 |1r@nq)|)rWztPg|) W n
 tyV   Y nw |1dk rfdB|1 dC}
t|
n |'D ]}%||%= qi|h|}3tij| |dd?| j |3|| d
ffg dD|v r|| _kd S d S )ENzcannot write mode z as TIFFra   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   r6   )r7   r8   r7      r  rG   r  r   c                 S  s   g | ]}|d  qS r  r0   r   r0   r0   r4   r     s    z_save.<locals>.<listcomp>r   r#   r%   
strip_sizer   r/  )r   r   )r   r9     r9  r;  r9  qualityd   zInvalid quality settingz5quality setting only supported for 'jpeg' compressionzSaving using libtiff encoderz	Items: %sr  r   libtiff_support_custom_tagsFr   r   rA  zConverted items: %s)r<   r:   r  r  rK   Ti @  zencoder error z when writing image file_debug_multipage)l	SAVE_INFOr  rx  rh  r   encoderinfoencoderconfigr   rw  rz   r   WRITE_LIBTIFFr  r   r  r  r   r	   Exifr  ry  rz  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  rY   r  r  r  r  copyrv  heightwidthr   invertr  
getpaletter  r  
STRIP_SIZErq   r  STRIPBYTECOUNTSr   r  COMPRESSION_INFO_REVr  r   REFERENCEBLACKWHITEr   
setdefaultr  r  r  TILEBYTECOUNTSr  JPEGQUALITYr  r{  osdupr  ioUnsupportedOperationTRANSFERFUNCTIONSUBIFDr   r   	itertoolschainLIBTIFF_COREr  r{   r   r   r   r   rg   sort_getencoderr  r  r  r  r
   _saver?  )4r  ri  r  r"  rb   r  r-  bitsextrar  r   r  rA  rB  r  r  r   r  r  iccr   r  inverted_impxr'  r&  lutcolormapcolorsr~  r*  r7  r+  im_strip_sizerows_per_stripstrip_byte_countsstrips_per_imager   r}   	blocklistr<  r  typesatts
legacy_ifdsupplied_tagsr   r  rK  errcoder   r  r0   r0   r4   rc  '  s  













&
&






 


	







rc  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 )Nrg  FTzw+bzr+b)	r   r   close_fpr   openrh  ru  	beginningsetup)r|   fnr  r0   r0   r4   r~   m  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   TFr^   r   r]   r   zInvalid TIFF file header)r   r{  r}  rX  SEEK_SETwhereToWriteNewIFDOffsetoffsetOfNewPagerg  IIMMisFirst	setEndianRuntimeErrorskipIFDsgoToEnd)r|   iimmr   r0   r0   r4   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  rg  r  r  readLongr  	writeLongfixIFD)r|   r  r   
ifd_offsetr0   r0   r4   finalize  s   


zAppendingTiffWriter.finalizec                 C  s   |    |   d S rn   )r  r~  r   r0   r0   r4   newFrame  s   zAppendingTiffWriter.newFramec                 C  s   | S rn   r0   r   r0   r0   r4   	__enter__  s   zAppendingTiffWriter.__enter__c                 C  s   | j r|   dS )NF)r{  r  )r|   exc_type	exc_value	tracebackr0   r0   r4   __exit__  s   zAppendingTiffWriter.__exit__c                 C  s   | j  | j S rn   )r   ru  r  r   r0   r0   r4   ru    rd   zAppendingTiffWriter.tellc                 C  s*   |t jkr
|| j7 }| j|| |  S rn   )rX  r  r  r   r{  ru  )r|   r  whencer0   r0   r4   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{  rX  SEEK_ENDru  r  r   r  )r|   pos	pad_bytesr0   r0   r4   r    s   
zAppendingTiffWriter.goToEndc                 C  s.   || _ | j d | _| j d | _| j d | _d S )Nr8   r3  HHL)endianlongFmtshortFmt	tagFormat)r|   r  r0   r0   r4   r    s   zAppendingTiffWriter.setEndianc                 C  sP   	 |   }|dkr| j d | _d S | j| |  }| j|d tj q)NTr   r   r9   )r  r   ru  r  r{  	readShortrX  SEEK_CUR)r|   r  num_tagsr0   r0   r4   r    s   zAppendingTiffWriter.skipIFDsc                 C  s   | j |S rn   )r   r  r?  r0   r0   r4   r    r   zAppendingTiffWriter.writec                 C     t | j| jd\}|S )Nr   )r  r  r  r   rg  rE  r0   r0   r4   r       zAppendingTiffWriter.readShortc                 C  r  r_   )r  r  r  r   rg  rE  r0   r0   r4   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{  rX  r  r  r  r  r  r  r|   r}   bytes_writtenr   r0   r0   r4   rewriteLastShortToLong     z*AppendingTiffWriter.rewriteLastShortToLongc                 C  r  )Nr  r   r   bytes but wanted 2)	r   r{  rX  r  r  r  r  r  r  r  r0   r0   r4   rewriteLastShort  r  z$AppendingTiffWriter.rewriteLastShortc                 C  r  )Nr   r  r  r  r  r0   r0   r4   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  r  r  r  r  r0   r0   r4   
writeShort  
   zAppendingTiffWriter.writeShortc                 C  r  )Nr   r  r  )r   r  r  r  r  r  r  r0   r0   r4   r    r  zAppendingTiffWriter.writeLongc                 C  s   |    | j  d S rn   )r  r   r  r   r0   r0   r4   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  rv  r  r  r  r   rg  
fieldSizesr  r  r  Tagsru  
fixOffsetsr{  rX  r  )r|   r  r~  r   
field_typer  
field_size
total_sizeis_localr  cur_posr0   r0   r4   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  rv  r  r  r  r  r   r{  rX  r  r  r   r  r  r  )r|   r  r  r  r   r~  r  r0   r0   r4   r  0  s$   

zAppendingTiffWriter.fixOffsetsN)F)FF)r   r   r   r  r  r~   r~  r  r  r  r  ru  rZ  r  r{  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r0   r0   r0   r4   rv  O  s2    
	
#rv  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   )rA  rL  rB  r  rw  r   rc  ru  rv  r  rv  r{  r  r  )r  ri  r  rA  rB  r  cur_idxtfimsnfrr"  r0   r0   r4   	_save_allJ  s2   




r  z.tifz.tiffz
image/tiff)n
__future__r   rZ  r^  loggingr  rX  r  r  collections.abcr   	fractionsr   numbersr   r    r   r	   r
   r   r   r   _binaryr   r  r   r  r   r   	getLoggerr   ry  r  rC  IFD_LEGACY_APIrQ  r   r   r  r  r  r  r  r  rG  r  r  r  rR  r  r  r  r  r\  rH  rI  rJ  	PREDICTORr  r  r  r  rV  r]  r  r  
JPEGTABLESr   rT  rK  rE  rF  r  EXIFIFDr  rW  IMAGEJ_META_DATA_BYTE_COUNTSIMAGEJ_META_DATAr  r   rS  r
  rr   r  r`   rc   rm   ru   r   r%  rg   r   r"  r   r   setattrr  ImageFileDirectoryr  r@  rc  rv  r  register_openr-  register_saveregister_save_allregister_extensionsregister_mimer0   r0   r0   r4   <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|
     \   c	
  * |