o
    i.                     @   s   d Z ddlmZmZ zddlZdZW n ey   dZY nw ddlm	Z	 ddl
mZmZmZ ddlZer:ddlmZ d	Zd
diZejjejjejjejjgZdefddZdejjdefddZdddefddZdddefddZddddeddfddZ dS )a  Use the `numpy <https://numpy.org/>`_ package to convert supported pixel
data to a :class:`numpy.ndarray`.

**Supported transfer syntaxes**

* 1.2.840.10008.1.2 : Implicit VR Little Endian
* 1.2.840.10008.1.2.1 : Explicit VR Little Endian
* 1.2.840.10008.1.2.1.99 : Deflated Explicit VR Little Endian
* 1.2.840.10008.1.2.2 : Explicit VR Big Endian

**Supported data**

The numpy handler supports the conversion of data in the (7FE0,0008) *Float
Pixel Data*, (7FE0,0009) *Double Float Pixel Data* and (7FE0,0010)
*Pixel Data* elements to a :class:`~numpy.ndarray` provided the
related :dcm:`Image Pixel<part03/sect_C.7.6.3.html>`, :dcm:`Floating Point
Image Pixel<part03/sect_C.7.6.24.html>` or  :dcm:`Double Floating Point Image
Pixel<part03/sect_C.7.6.25.html>` module elements have values given in the
table below.

+------------------------------------------------+---------------+----------+
| Element                                        | Supported     |          |
+-------------+---------------------------+------+ values        |          |
| Tag         | Keyword                   | Type |               |          |
+=============+===========================+======+===============+==========+
| (0028,0002) | SamplesPerPixel           | 1    | N             | Required |
+-------------+---------------------------+------+---------------+----------+
| (0028,0004) | PhotometricInterpretation | 1    | MONOCHROME1,  | Required |
|             |                           |      | MONOCHROME2,  |          |
|             |                           |      | RGB,          |          |
|             |                           |      | YBR_FULL,     |          |
|             |                           |      | YBR_FULL_422  |          |
+-------------+---------------------------+------+---------------+----------+
| (0028,0006) | PlanarConfiguration       | 1C   | 0, 1          | Optional |
+-------------+---------------------------+------+---------------+----------+
| (0028,0008) | NumberOfFrames            | 1C   | N             | Optional |
+-------------+---------------------------+------+---------------+----------+
| (0028,0010) | Rows                      | 1    | N             | Required |
+-------------+---------------------------+------+---------------+----------+
| (0028,0011) | Columns                   | 1    | N             | Required |
+-------------+---------------------------+------+---------------+----------+
| (0028,0100) | BitsAllocated             | 1    | 1, 8, 16, 32, | Required |
|             |                           |      | 64            |          |
+-------------+---------------------------+------+---------------+----------+
| (0028,0101) | BitsStored                | 1    | 1, 8, 12, 16  | Optional |
+-------------+---------------------------+------+---------------+----------+
| (0028,0103) | PixelRepresentation       | 1C   | 0, 1          | Optional |
+-------------+---------------------------+------+---------------+----------+

    )TYPE_CHECKINGcastNTF)warn_and_log)pixel_dtypeget_expected_lengthunpack_bits)DatasetNumpynumpy)zhttps://numpy.org/NumPyreturnc                   C   s   t S )z8Return ``True`` if the handler has its dependencies met.)HAVE_NP r   r   _/mnt/sdb/aimis/docanh/lib/python3.10/site-packages/pydicom/pixel_data_handlers/numpy_handler.pyis_availableW   s   r   transfer_syntaxc                 C   s   | t v S )zReturn ``True`` if the handler supports the `transfer_syntax`.

    Parameters
    ----------
    transfer_syntax : uid.UID
        The Transfer Syntax UID of the *Pixel Data* that is to be used with
        the handler.
    )SUPPORTED_TRANSFER_SYNTAXES)r   r   r   r   supports_transfer_syntax\   s   	r   dsr   c                 C      dS )zReturn ``True`` if the *Pixel Data* should to be converted from YCbCr to
    RGB.

    This affects JPEG transfer syntaxes.
    Fr   r   r   r   r   needs_to_convert_to_RGBh      r   c                 C   r   )zReturn ``True`` if the *Photometric Interpretation* should be changed
    to RGB.

    This affects JPEG transfer syntaxes.
    Fr   r   r   r   r   .should_change_PhotometricInterpretation_to_RGBq   r   r   	read_only
np.ndarrayc                    s&   j j}|tvrtdg d} fdd|D }t|dkr#tdg d}|d d	kr4|d
dg  fdd|D }|rHtdd|  jdkrVt	 dsVtdt
 |d }t }t|}	||d  }
|	|
k r|	|krxtd n>td|	 d|
 d|	|
krd|	 d|	|  d} jdkr|d d |d d d  }|	|krd|	 d| d}t|  jdkrt dd}tdt|d d!d"| }nt d#|d v d$} jd s|jdkr|d d	kr d% jd&kr|jd"d' d( }tj||d)  }tj|d"| |d)}ntj|d"| |d)} jdkr|tj|d d |d)}|d"d"d* |d"d"d+< |dd"d* |dd"d+< |dd"d* |dd"d* |dd"d+< |d*d"d+< |dd"d* |dd"d* |dd"d+< |d,d"d+< |}t rd- _|s jdkr| S |S ).a  Return a :class:`numpy.ndarray` of the pixel data.

    Parameters
    ----------
    ds : Dataset
        The :class:`Dataset` containing an Image Pixel, Floating Point Image
        Pixel or Double Floating Point Image Pixel module and the
        *Pixel Data*, *Float Pixel Data* or *Double Float Pixel Data* to be
        converted. If (0028,0004) *Photometric Interpretation* is
        `'YBR_FULL_422'` then the pixel data will be
        resampled to 3 channel data as per Part 3, :dcm:`Annex C.7.6.3.1.2
        <part03/sect_C.7.6.3.html#sect_C.7.6.3.1.2>` of the DICOM Standard.
    read_only : bool, optional
        If ``False`` (default) then returns a writeable array that no longer
        uses the original memory. If ``True`` and the value of (0028,0100)
        *Bits Allocated* > 1 then returns a read-only array that uses the
        original memory buffer of the pixel data. If *Bits Allocated* = 1 then
        always returns a writeable array.

    Returns
    -------
    np.ndarray
        The contents of (7FE0,0010) *Pixel Data*, (7FE0,0008) *Float Pixel
        Data* or (7FE0,0009) *Double Float Pixel Data* as a 1D array.

    Raises
    ------
    AttributeError
        If `ds` is missing a required element.
    NotImplementedError
        If `ds` contains pixel data in an unsupported format.
    ValueError
        If the actual length of the pixel data doesn't match the expected
        length.
    ziUnable to convert the pixel data as the transfer syntax is not supported by the numpy pixel data handler.)	PixelDataFloatPixelDataDoubleFloatPixelDatac                    s   g | ]}| v r|qS r   r   ).0kwr   r   r   
<listcomp>       z!get_pixeldata.<locals>.<listcomp>   zUnable to convert the pixel data: one of Pixel Data, Float Pixel Data or Double Float Pixel Data must be present in the dataset)BitsAllocatedRowsColumnsSamplesPerPixelPhotometricInterpretationr   r   PixelRepresentation
BitsStoredc                    s   g | ]}| vr|qS r   r   )r   elemr   r   r   r!      r"   zbUnable to convert the pixel data as the following required elements are missing from the dataset: z, PlanarConfigurationzUnable to convert the pixel data as the following conditionally required element is missing from the dataset: PlanarConfiguration   z<The odd length pixel data is missing a trailing padding bytez-The length of the pixel data in the dataset (z+ bytes) doesn't match the expected length (z[ bytes). The dataset may be corrupted or there may be an issue with the pixel data handler.z. bytes) indicates it contains excess padding. z/ bytes will be removed from the end of the dataYBR_FULL_422   zeThe Photometric Interpretation of the dataset is YBR_FULL_422, however the length of the pixel data (z) bytes) is a third larger than expected (z bytes) which indicates that this may be incorrect. You may need to change the Photometric Interpretation to the correct value.pixels)unitr   T)as_arrayNFloat)as_floati OW16)dtype         RGB)	file_metaTransferSyntaxUIDr   NotImplementedErrorlenAttributeErrorextendjoinr'   hasattrgetattrr   r   
ValueErrorr(   r$   r   r   r   original_encodingitemsizeVRnamenp
frombufferbyteswaptobyteszerosr   copy)r   r   r   keywords
px_keywordrequired_elementsmissing
pixel_dataexpected_lenactual_lengthpadded_expected_lenmsgybr_full_length	nr_pixelsarrr8   dtype16boutr   r   r   get_pixeldataz   s   $




22
r`   )F)!__doc__typingr   r   r
   rK   r   ImportErrorpydicom.miscr   pydicom.pixels.utilsr   r   r   pydicom.uidpydicompydicom.datasetr   HANDLER_NAMEDEPENDENCIESuidExplicitVRLittleEndianImplicitVRLittleEndianDeflatedExplicitVRLittleEndianExplicitVRBigEndianr   boolr   UIDr   r   r   r`   r   r   r   r   <module>   s4   3		