o
    eO.                     @  s   d Z ddlmZ g dZddlmZmZmZ ddlZzddl	Z		dAdddBddZ
dddCddZW n eyM   	dAdddBddZ
dddCddZY nw zddlZ	dAdddBddZdddCddZW n ey   	dAdddBddZdddCddZY nw dddDddZe	dEdddFd"d#Ze	dEdddGd%d#Z		dEdddHd'd#ZedId*d+ZedJd,d+Z		dEdddHd-d+Zedg d.dKd1d2Zedg d.dLd3d2Zdg d.dMd4d2Z	dNdddOd9d:Z	dPdddQd;d<ZdRd?d@ZdS )SzFallback imagecodecs codecs.

This module provides alternative, pure Python and NumPy implementations of
some functions of the `imagecodecs`_ package. The functions may raise
NotImplementedError.

.. _imagecodecs: https://github.com/cgohlke/imagecodecs

    )annotations)bitorder_decodedelta_decodedelta_encodefloat24_decodelzma_decodelzma_encodepackbits_decodepackints_decodepackints_encodezlib_decodezlib_encode)AnyLiteraloverloadNoutlevel
int | Nonedatabytes | numpy.ndarrayreturnbytesc               C  s   t | tjr
|  } t| S )zCompress LZMA.)
isinstancenumpyndarraytobyteslzmacompressr   r   r    r    ED:\Projects\ConvertPro\env\Lib\site-packages\tifffile/_imagecodecs.pyr   A   s   
r   c               C  
   t | S )zDecompress LZMA.)r   
decompressr   r   r    r    r!   r   I      
r   c               C     ddl }dS zRaise ImportError.r   N    r   )r   r   r   r   r    r    r!   r   O      c               C     ddl }dS r'   r)   )r   r   r   r    r    r!   r   W      c               C  s.   t | tjr
|  } t| |du rdS |S )zCompress Zlib DEFLATE.N   )r   r   r   r   zlibr   r   r    r    r!   r   a   s   r   c               C  r"   )zDecompress Zlib DEFLATE.)r.   r#   r$   r    r    r!   r   i   r%   r   c               C  r&   r'   r.   )r   r   r   r.   r    r    r!   r   p   r*   c               C  r+   r'   r/   )r   r   r.   r    r    r!   r   x   r,   encodedc               C  s   g }|j }d}z;	 t| ||d  d }|d7 }|dkr0|| ||d  d|   |d7 }n|dk rB|| |||   ||7 }q	 tyN   Y t|S w )aS  Decompress PackBits encoded byte string.

    >>> packbits_decode(b'\x80\x80')  # NOP
    b''
    >>> packbits_decode(b'\x02123')
    b'123'
    >>> packbits_decode(
    ...   b'\xfe\xaa\x02\x80\x00\x2a\xfd\xaa\x03\x80\x00\x2a\x22\xf7\xaa'
    ...     )[:-5]
    b'\xaa\xaa\xaa\x80\x00*\xaa\xaa\xaa\xaa\x80\x00*"\xaa\xaa\xaa\xaa\xaa'

    r   T      i  )extendord	TypeErrorr   )r0   r   Z
out_extendinr    r    r!   r	      s$   
r	   r1   axisintdistbytes | bytearrayc               C     d S Nr    r   r9   r;   r   r    r    r!   r         r   numpy.ndarrayc               C  r=   r>   r    r?   r    r    r!   r      r@   !bytes | bytearray | numpy.ndarrayc               C  s   |dkrt d|dt| ttfr-tj| tjd} tj| dd}t|d| d 	 S | j
}|jdkrA| |j d|j } tj| |d}td	g| j }d||< tj|d| t| |d}| j
jsl|d
 }|jdkrv||S |S )zEncode Delta.r1   zdelta_encode with dist=# requires the 'imagecodecs' packagedtyper   )r9   fuNT)NotImplementedErrorr   r   	bytearrayr   
frombufferuint8diffinsertr   rE   kindview	byteorderitemsizeslicendimtupleisnativebyteswapZnewbyteorder)r   r9   r;   r   rL   rE   keyr    r    r!   r      s(   	



r   r   c               C  r=   r>   r    r?   r    r    r!   r      r@   r   c               C  r=   r>   r    r?   r    r    r!   r      r@   c               C  s   |dkrt d|d|dur|jjsd}t| ttfr1tj| tjd} tj	| dtj|d
 S | jjdkra| jjsDt d	| jd| | jj d
| jj }tj	|||jd}|| jS tj	| || j|dS )zDecode Delta.r1   zdelta_decode with dist=rC   NrD   r   )r9   rE   r   rF   zdelta_decode with rG   )r9   rE   )rH   flagsZ	writeabler   r   rI   r   rJ   rK   Zcumsumr   rE   rN   rU   rO   rP   rQ   )r   r9   r;   r   rO   r    r    r!   r      s$   	
)r   	_bitorderrY   	list[Any]c               C  r=   r>   r    r   r   rY   r    r    r!   r      r@   r   c               C  r=   r>   r    r[   r    r    r!   r     r@   c            
   C  s   |s| d | tj|d tjd t| ttfr"| |d S z| d}tj	|d ||d | W S  t
yE } ztd|d}~ww )	aK  Reverse bits in each byte of bytes or numpy array.

    Decode data where pixels with lower column values are stored in the
    lower-order bits of the bytes (TIFF FillOrder is LSB2MSB).

    Parameters:
        data:
            Data to bit-reversed. If bytes type, a new bit-reversed
            bytes is returned. NumPy arrays are bit-reversed in-place.

    Examples:
        >>> bitorder_decode(b'\x01\x64')
        b'\x80&'
        >>> data = numpy.array([1, 666], dtype='uint16')
        >>> bitorder_decode(data)
        >>> data
        array([  128, 16473], dtype=uint16)

    s    @ `P0pH(hX8xD$dT4tL,l\<|B"bR2r
J*jZ:zF&fV6vN.n^>~A!aQ1q	I)iY9yE%eU5uM-m]=}C#cS3sK+k[;{G'gW7wO/o_?r   rD   rK   r1   r   z<bitorder_decode of slices requires the 'imagecodecs' packageN)appendr   rJ   rK   r   r   rI   	translaterO   Ztake
ValueErrorrH   )r   r   rY   rO   excr    r    r!   r     s&   
rE   numpy.dtype | strbitspersamplerunlenc               C  s   |dkr1t | d}t |}|d r,|d|d|d   }|ddd|f d}||S |dv r;t | |S td| d)	aA  Decompress bytes to array of integers.

    This implementation only handles itemsizes 1, 8, 16, 32, and 64 bits.
    Install the Imagecodecs package for decoding other integer sizes.

    Parameters:
        data:
            Data to decompress.
        dtype:
            Numpy boolean or integer type.
        bitspersample:
            Number of bits per integer.
        runlen:
            Number of consecutive integers after which to start at next byte.

    Examples:
        >>> packints_decode(b'a', 'B', 1)
        array([0, 1, 1, 0, 0, 0, 0, 1], dtype=uint8)

    r1   z|B   r8   N)rc          @   zpackints_decode of z0-bit integers requires the 'imagecodecs' package)r   rJ   Z
unpackbitsZreshapeZastyperH   )r   rE   ra   rb   r   Z
data_arrayr    r    r!   r
   A  s   


r
   c               C     t d)zTightly pack integers.z2packints_encode requires the 'imagecodecs' packagerH   )r   ra   r9   r   r    r    r!   r   m     r   rP   Literal['>', '<']c                C  rg   )z"Return float32 array from float24.z1float24_decode requires the 'imagecodecs' packagerh   )r   rP   r    r    r!   r   v  ri   r   r>   )r   r   r   r   r   r   )r   r   r   r   )r0   r   r   r   )r8   r1   )r9   r:   r;   r:   r   r<   r   r   )r9   r:   r;   r:   r   rA   r   rA   )r9   r:   r;   r:   r   rB   r   r   )
r9   r:   r;   r:   r   r<   r   r   r   r   )
r9   r:   r;   r:   r   rA   r   r   r   rA   )r   r<   rY   rZ   r   r   )r   rA   rY   rZ   r   rA   )r   rB   rY   rZ   r   r   )r   )
rE   r`   ra   r:   rb   r:   r   r   r   rA   )r8   )ra   r:   r9   r:   r   rA   r   r   )rP   rj   r   r   r   rA   )__doc__
__future__r   __all__typingr   r   r   r   r   r   r   ImportErrorr.   r   r   r	   r   r   r   r
   r   r   r    r    r    r!   <module>   s   
!	 	
>-	