o
    4ήc?.                     @  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    </tmp/pip-target-vg8gfxp4/lib/python/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   
out_extendinr    r    r!   r	      s$   
r	   r1   axisintdistbytes | bytearrayc               C     d S Nr    r   r:   r<   r   r    r    r!   r         r   numpy.ndarrayc               C  r>   r?   r    r@   r    r    r!   r      rA   !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   )r:   fuNT)NotImplementedErrorr   r   	bytearrayr   
frombufferuint8diffinsertr   rF   kindview	byteorderitemsizeslicendimtupleisnativebyteswapnewbyteorder)r   r:   r<   r   rM   rF   keyr    r    r!   r      s(   	



r   r   c               C  r>   r?   r    r@   r    r    r!   r      rA   r   c               C  r>   r?   r    r@   r    r    r!   r      rA   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=rD   NrE   r   )r:   rF   r   rG   zdelta_decode with rH   )r:   rF   )rI   flags	writeabler   r   rJ   r   rK   rL   cumsumr   rF   rO   rV   rP   rQ   rR   )r   r:   r<   r   rP   r    r    r!   r      s$   	
)r   	_bitorderr]   	list[Any]c               C  r>   r?   r    r   r   r]   r    r    r!   r      rA   r   c               C  r>   r?   r    r_   r    r    r!   r     rA   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
y?   td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   rE   rL   r1   r   z<bitorder_decode of slices requires the 'imagecodecs' package)appendr   rK   rL   r   r   rJ   	translaterP   take
ValueErrorrI   )r   r   r]   rP   r    r    r!   r     s    
rF   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   r9   N)rg          @   zpackints_decode of z0-bit integers requires the 'imagecodecs' package)r   rK   
unpackbitsreshapeastyperI   )r   rF   re   rf   r   
data_arrayr    r    r!   r
   A  s   


r
   c               C     t d)zTightly pack integers.z2packints_encode requires the 'imagecodecs' packagerI   )r   re   r:   r   r    r    r!   r   m     r   rQ   Literal['>', '<']c                C  ro   )z"Return float32 array from float24.z1float24_decode requires the 'imagecodecs' packagerp   )r   rQ   r    r    r!   r   v  rq   r   r?   )r   r   r   r   r   r   )r   r   r   r   )r0   r   r   r   )r9   r1   )r:   r;   r<   r;   r   r=   r   r   )r:   r;   r<   r;   r   rB   r   rB   )r:   r;   r<   r;   r   rC   r   r   )
r:   r;   r<   r;   r   r=   r   r   r   r   )
r:   r;   r<   r;   r   rB   r   r   r   rB   )r   r=   r]   r^   r   r   )r   rB   r]   r^   r   rB   )r   rC   r]   r^   r   r   )r   )
rF   rd   re   r;   rf   r;   r   r   r   rB   )r9   )re   r;   r:   r;   r   rB   r   r   )rQ   rr   r   r   r   rB   )__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   
!	 	
>-	