o
    i=+                     @   sF  d Z ddlmZ ddlZddlmZmZ ddlmZ er"ddl	m
Z
 zddlZdZW n ey5   dZY nw zddlZdZW n eyI   dZY nw erTdd	lmZmZ zddlZdZW n eyg   dZY nw zddlZdZW n ey{   dZY nw 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 ddlm Z m!Z!m"Z"m#Z#m$Z$ ddl%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/ e0dZ1dZ2ere Z3e&e'e(e)e*e+gZ4e,e-gZ5e.gZ6e4e5 e6 Z7ddiZ8de9fddZ:de/de9fddZ;ddde9fddZ<ddde9fddZ=d&dd Z>d'ddd!e9ded fd"d#Zd&d$d%Z?dS )(a
  Use the :gh:`pylibjpeg <pylibjpeg/>` package
to convert supported pixel data to a :class:`numpy.ndarray`.

.. versionadded:: 2.1

**Supported data**

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

+------------------------------------------------+---------------+----------+
| Element                                        | Supported     |          |
+-------------+---------------------------+------+ values        |          |
| Tag         | Keyword                   | Type |               |          |
+=============+===========================+======+===============+==========+
| (0028,0002) | SamplesPerPixel           | 1    | 1, 3          | Required |
+-------------+---------------------------+------+---------------+----------+
| (0028,0004) | PhotometricInterpretation | 1    | MONOCHROME1,  | Required |
|             |                           |      | MONOCHROME2,  |          |
|             |                           |      | RGB,          |          |
|             |                           |      | YBR_FULL,     |          |
|             |                           |      | YBR_FULL_422, |          |
|             |                           |      | YBR_ICT,      |          |
|             |                           |      | YBR_RCT       |          |
+-------------+---------------------------+------+---------------+----------+
| (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    | 8, 16         | Required |
+-------------+---------------------------+------+---------------+----------+
| (0028,0101) | BitsStored                | 1    | Up to 16      | Required |
+-------------+---------------------------+------+---------------+----------+
| (0028,0103) | PixelRepresentation       | 1    | 0, 1          | Required |
+-------------+---------------------------+------+---------------+----------+

.. versionchanged:: 2.2

    Added support for *RLE Lossless* via the `pylibjpeg-rle` plugin.

    )deepcopyN)TYPE_CHECKINGcast)Iterable)DatasetTF)get_pixel_data_decodersDecoder)config)generate_frames)pixel_dtypeget_expected_lengthreshape_pixel_arrayget_j2k_parametersget_nr_frames)
JPEGBaseline8BitJPEGExtended12BitJPEGLosslessJPEGLosslessSV1JPEGLSLosslessJPEGLSNearLosslessJPEG2000LosslessJPEG2000RLELosslessUIDpydicom	pylibjpegnumpy)zhttps://numpy.org/NumPyreturnc                   C   s   t otS )z8Return ``True`` if the handler has its dependencies met.)HAVE_NPHAVE_PYLIBJPEG r!   r!   c/mnt/sdb/aimis/docanh/lib/python3.10/site-packages/pydicom/pixel_data_handlers/pylibjpeg_handler.pyis_available   s   r#   tsyntaxc                 C   s   | t v S )zReturn ``True`` if the handler supports the `tsyntax`.

    Parameters
    ----------
    tsyntax : pydicom.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_RGB      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_RGB   r+   r,   
np.ndarrayc                 C   s   t | t| S )aX  Return the entire *Pixel Data* as an :class:`~numpy.ndarray`.

    .. versionadded:: 2.1

    Parameters
    ----------
    ds : pydicom.dataset.Dataset
        The :class:`Dataset` containing an :dcm:`Image Pixel
        <part03/sect_C.7.6.3.html>` module and the *Pixel Data* to be
        converted.

    Returns
    -------
    numpy.ndarray
        The contents of (7FE0,0010) *Pixel Data* as an :class:`~numpy.ndarray`
        with shape (rows, columns), (rows, columns, components), (frames,
        rows, columns), or (frames, rows, columns, components) depending on
        the dataset.
    )r   get_pixeldatar)   r!   r!   r"   as_array   s   r/   reshapec                 #   s    j j}|tvr!|tv rd}n	|tv rd}nd}td| dg d} fdd|D }|r:td	d
| tt	t| }t
d|j d|  t dd}t d}t }	tt j}
tt j}t j|dD ]}|||d}|ttfv rtjrt|}|dd}tt|d|
}|| }|r|s| jkrd|_|t|}t||}| |	}t!||}|j"|	kr||	}|s|V  qq j#dkr|$ j% j&V  qq|t'kr|$ j# j% j&}|(dddV  qq|$ j% j& j#V  qqdS )al  Yield a *Pixel Data* frame from `ds` as an :class:`~numpy.ndarray`.

    .. versionadded:: 2.1

    Parameters
    ----------
    ds : pydicom.dataset.Dataset
        The :class:`Dataset` containing an :dcm:`Image Pixel
        <part03/sect_C.7.6.3.html>` module and the *Pixel Data* to be
        converted.
    reshape : bool, optional
        If ``True`` (default), then the returned :class:`~numpy.ndarray` will
        be reshaped to the correct dimensions. If ``False`` then no reshaping
        will be performed.

    Yields
    -------
    numpy.ndarray
        A single frame of (7FE0,0010) *Pixel Data* as an
        :class:`~numpy.ndarray` with an appropriate dtype for the data.

    Raises
    ------
    AttributeError
        If `ds` is missing a required element.
    RuntimeError
        If the plugin required to decode the pixel data is not installed.
    zpylibjpeg-openjpegzpylibjpeg-libjpegzpylibjpeg-rlez)Unable to convert the Pixel Data as the 'z' plugin is not installed)BitsAllocatedRowsColumnsPixelRepresentationSamplesPerPixelPhotometricInterpretation	PixelDatac                    s   g | ]}| vr|qS r!   r!   ).0elemr)   r!   r"   
<listcomp>   s    z#generate_frames.<locals>.<listcomp>zbUnable to convert the pixel data as the following required elements are missing from the dataset: z, z	Decoding z encoded Pixel Data using F)warn(   )number_of_framesr)   	is_signedT	precisionr         N))	file_metaTransferSyntaxUID	_DECODERS_OPENJPEG_SYNTAXES_LIBJPEG_SYNTAXESRuntimeErrorAttributeErrorjoinr   r   LOGGERdebugnamer   r   group_datasetr   int
BitsStoredr1   frame_generatorr7   r   r   r	   APPLY_J2K_CORRECTIONSr   
setdefaultr4   viewnp
left_shiftastyperight_shiftdtyper5   r0   r2   r3   r   	transpose)r'   r0   r$   pluginrequired_elementsmissingdecoder	nr_framespixel_modulerX   bits_storedbits_allocatedframearrparamj2k_signj2k_precisionshiftr!   r)   r"   r
      sf   
	



r
   c                 C   s`   t | d}|t|  }t|t| }td||}tt| d|D ]\}}||||| < q!|S )a  Return a :class:`numpy.ndarray` of the pixel data.

    .. versionadded:: 2.1

    Parameters
    ----------
    ds : pydicom.dataset.Dataset
        The :class:`Dataset` containing an :dcm:`Image Pixel
        <part03/sect_C.7.6.3.html>` module and the *Pixel Data* to be
        converted.

    Returns
    -------
    numpy.ndarray
        The contents of (7FE0,0010) *Pixel Data* as a 1D array.
    pixelsr   F)r   r   rT   emptyr   rangezipr
   )r'   expected_len	frame_lenrc   generate_offsetsrb   offsetr!   r!   r"   r.   4  s   
r.   )r'   r   r   r-   )T)@__doc__copyr   loggingtypingr   r   collections.abcr   pydicom.datasetr   r   rT   r   ImportErrorr   r    pylibjpeg.utilsr   r   openjpegHAVE_OPENJPEGlibjpegHAVE_LIBJPEGrleHAVE_RLEr   r	   pydicom.encapsr
   rP   pydicom.pixels.utilsr   r   r   r   r   pydicom.uidr   r   r   r   r   r   r   r   r   r   	getLoggerrJ   HANDLER_NAMErD   rF   rE   _RLE_SYNTAXESr%   DEPENDENCIESboolr#   r&   r*   r,   r/   r.   r!   r!   r!   r"   <module>   s   00
	
	m