o
    i                     @   s   d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	 e	diZ
ded	efd
dZdeded	efddZdeded	efddZded	efddZdS )zFInterface for *Pixel Data* encoding, not intended to be used directly.    )groupbyN)pack)EncodeRunner)RLELossless uidreturnc                 C   s   dS )zeReturn ``True`` if a pixel data encoder for `uid` is available for use,
    ``False`` otherwise.
    Tr   )r   r   r   T/mnt/sdb/aimis/docanh/lib/python3.10/site-packages/pydicom/pixels/encoders/native.pyis_available   s   r
   srcrunnerc                 C   s4  | dddkrtdt|jd }||j }|dkr!tdt }g }|j}t|jD ]'}t	t|D ]}|||  }	t
| |	d| |}
||
 |t|
 q6q.ttd	t|}d
g}t|dd D ]\}}||| |  qj|tdt| dg|R   |dd
t|   t|| S )aW  Wrapper for use with the encoder interface.

    Parameters
    ----------
    src : bytes
        A single frame of little-endian ordered image data to be RLE encoded.
    runner : pydicom.pixels.encoders.base.EncodeRunner
        The runner managing the encoding process.

    Returns
    -------
    bytes
        An RLE encoded frame.
    	byteorder<>z$Unsupported option "byteorder = '>'"      z_Unable to encode as the DICOM standard only allows a maximum of 15 segments in RLE encoded dataNz<L@   L    )
get_option
ValueErrormathceilbits_allocatedsamples_per_pixel	bytearraycolumnsrangereversed_encode_segmentextendappendlenr   	enumeratebytes)r   r   bytes_allocatednr_segmentsrle_dataseg_lengthsr   	sample_nrbyte_offsetidxsegment
rle_headeroffsetsiilengthr   r   r	   _encode_frame   s2   

"r2   r   c                 C   sP   t  }tdt| |D ]}|t| |||   q|dt|d   |S )a~  Return `src` as an RLE encoded bytearray.

    Each row of the image is encoded separately as required by the DICOM
    Standard.

    Parameters
    ----------
    src : bytes
        The little-endian ordered data to be encoded, representing a Byte
        Segment as in the DICOM Standard, Part 5,
        :dcm:`Annex G.2<part05/sect_G.2.html>`.
    columns : int
        The number of columns in the image.

    Returns
    -------
    bytearray
        The RLE encoded segment, following the format specified by the DICOM
        Standard. Odd length encoded segments are padded by a trailing ``0x00``
        to be even length.
    r   r      )r   r   r#   r!   _encode_row)r   r   outr,   r   r   r	   r    L   s
   r    c                 C   sp  g }|j }|j}g }t| D ]}\}}t|}t|dkr$| |d  q|r[tt|d\}}	t|D ]}
|
d9 }
|d |||
|
d   q3|	rY||	d  |||	 d  g }tt|d\}}	|rp|d|d f|  |	dkr|d|	 |d f q|	dkr|d|d f qtdt|dD ]}|||d  }|t|d  || qtt| dg|R  S )	a  Return `src` as RLE encoded bytes.

    Parameters
    ----------
    src : bytes
        The little-endian ordered data to be encoded.

    Returns
    -------
    bytes
        The RLE encoded row, following the format specified by the DICOM
        Standard, Part 5, :dcm:`Annex G<part05/chapter_G.html>`

    Notes
    -----
    * 2-byte repeat runs are always encoded as Replicate Runs rather than
      only when not preceded by a Literal Run as suggested by the Standard.
       r         N   i  B)r"   r!   r   listr#   divmodr   r   )r   r5   
out_append
out_extendliteral_
iter_groupgroupnr_full_runslen_partial_runr,   r0   _runr   r   r	   r4   l   s>   
r4   )__doc__	itertoolsr   r   structr   pydicom.pixels.encoders.baser   pydicom.uidr   ENCODER_DEPENDENCIESstrboolr
   r%   r2   intr   r    r4   r   r   r   r	   <module>   s   6 