
    0-PhM1                         d Z ddlZddlZddlZddlZddlmZmZmZ ddlm	Z	 da
 ej        e          Zd Zej        dk    Zd Zd	 Z G d
 de          ZdS )a  Read DICOM files.

Backend Library: internal

A format for reading DICOM images: a common format used to store
medical image data, such as X-ray, CT and MRI.

This format borrows some code (and ideas) from the pydicom project. However,
only a predefined subset of tags are extracted from the file. This allows
for great simplifications allowing us to make a stand-alone reader, and
also results in a much faster read time.

By default, only uncompressed and deflated transfer syntaxes are supported.
If gdcm or dcmtk is installed, these will be used to automatically convert
the data. See https://github.com/malaterre/GDCM/releases for installing GDCM.

This format provides functionality to group images of the same
series together, thus extracting volumes (and multiple volumes).
Using volread will attempt to yield a volume. If multiple volumes
are present, the first one is given. Using mimread will simply yield
all images in the given directory (not taking series into account).

Parameters
----------
progress : {True, False, BaseProgressIndicator}
    Whether to show progress when reading from multiple files.
    Default True. By passing an object that inherits from
    BaseProgressIndicator, the way in which progress is reported
    can be costumized.

    N   )FormatBaseProgressIndicatorStdoutProgressIndicator)read_n_bytesc                      ddl ma t          S )N   )_dicom) r
        U/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/imageio/plugins/dicom.pyload_libr   9   s    Mr   littlec                  4   ddt           j                            d          z  z   } dD ]F}t          j                            ||           }t          j                            |          r|gc S G	 t          j        | dg           | gS # t          $ r Y d S w xY w)Ndcmdjpeg.exewin)zc:\dcmtkc:\Program Fileszc:\Program Files\dcmtkzc:\Program Files (x86)\dcmtk	--version)
sysplatform
startswithospathjoinisfile
subprocess
check_call	Exception)fnamedirfilenames      r   get_dcmdjpeg_exer$   D   s    #,"9"9%"@"@@@E   7<<U++7>>(## 	:	uk2333w   tts   0B	 	
BBc            	      l   ddt           j                            d          z  z   } 	 t          j        | dg           | dgS # t
          $ r Y nw xY wg }dg}|D ]}t          j                            |          rt          j	        |          D ]z}|
                                                    d          rQ|dd                                          }|                    |t          j                            ||          f           {|                    d	
           d }|D ]\  }}t          j                            |d          }	t          j                            |dd          }
t          j                            |	          r|	} n&t          j                            |
          r|
} nd S |dgS )Ngdcmconvr   r   r   z--rawr   gdcm   T)reversezgdcmconv.exebin)r   r   r   r   r   r    r   r   isdirlistdirlowerstripappendr   sortr   )r!   
candidates	base_dirsbase_dirdnamesuffixr#   _dirnameexe1exe2s              r   get_gdcmconv_exer:   W   s   #,"9"9%"@"@@@Euk2333w    J$%I O O7=="" 	OH-- O O;;==++F33 O"122Y__..F%%vrw||He/L/L&MNNNOODO!!!H  
 

7w||G^44w||GUN;;7>>$ 	HE7>>$ 	HE	 tgs   A 
AAc                   D    e Zd ZdZd Zd Z G d dej                  ZdS )DicomFormatz See :mod:`imageio.plugins.dicom`c                    t           j                            |j                  rt          j        |j                  }t          |          D ]}t           j                            |j        |          }t           j                            |          rLd|vrHt          |d          5 }t          |d          }d d d            n# 1 swxY w Y   |dd         dk    c S dS |j
        dd         dk    S )NDICOMDIRrb         s   DICMF)r   r   r+   r#   r,   sortedr   r   openr   
firstbytes)selfrequestfilesr!   r#   ffirst_bytess          r   	_can_readzDicomFormat._can_read}   s"   7==)** 		Jw/00E  7<<(8%@@7>>(++ ;
%0G0Gh-- ;&21c&:&:; ; ; ; ; ; ; ; ; ; ; ; ; ; ;&s3w/7::::u!#c'*g55s   'CC	C	c                     dS NFr   )rF   rG   s     r   
_can_writezDicomFormat._can_write   s	     ur   c                   ^    e Zd Z e            Zd
dZd Zed             Zd Z	d Z
d Zd Zd	S )DicomFormat.ReaderTc                    t           st                       t          j                            | j        j                  ri | _        d | _        n	 t           	                    | j        
                                          }nY# t           j        $ rF}t                      }|sdt          |          v rt                      }|s@|j        d                             dd          }|                    dd          }|f|_         | j                                        }|dz   }	 t%          j        |||gz              n# t(          $ r |w xY wt          j                            |          }|| j        vr@| j                            |           t0                              d|d         z   d	z              t           	                    |          }Y d }~nd }~ww xY w|j        | _        |                                | _        d | _        t9          |t:                    r	|| _        d S |d
u rt?          d          }	|	| _        d S |dv rt;          d          | _        d S tA          d          )NJPEGr   using
installingconvertzauto-convertz.rawz:DICOM file contained compressed data. Autoconverting with z0 (this warning is shown once for each directory)TzReading DICOMrM   DummyzInvalid value for progress.)!r
   r   r   r   r+   rG   r#   _info_dataSimpleDicomReaderget_fileCompressedDicomr:   strr$   argsreplaceget_local_filenamer   r   r    r7   _compressed_warning_dirsaddloggerwarningget_numpy_array_series
isinstancer   _progressIndicatorr   
ValueError)
rF   progressdcmerrcmdmsgfname1fname2dps
             r   _openzDicomFormat.Reader._open   s    


w}}T\233 %3
!

? 224<3H3H3J3JKKCC- ? ? ?*,,C 16SXX#5#5.00 ?!hqk11'<HH!kk)^DD$'6!%!@!@!B!B!'&&&1#8H2HIIII( & & &"%I&GOOF33D$AAA 9==a@@@"NN!9"%a&!) #U!U   %66v>>5?8 !Y
 0022
  DL ($9:: @*2'''T!!+O<<*+''']***?*H*H''' !>???s2   1B GBG#D=<G=E

BGGc                 0    d | _         d | _        d | _        d S N)rW   rX   re   )rF   s    r   _closezDicomFormat.Reader._close   s    DJDJDLLLr   c                 v    | j         ,| j        }t                              | j        |          | _         | j         S rt   )re   rg   r
   process_directoryrG   )rF   pis     r   serieszDicomFormat.Reader.series   s3    |#,%77bII<r   c                 F   | j         8| j        d         d         }|j        | _        |                                | _         | j         j        dk    r| j         j        d         nd}| j        j        d         dk    r|S | j        j        d         dk    r&|dk    r|S t          d | j        D                       S | j        j        d         dk    r|dk    rdS t          | j                  S | j        j        d         dk    rt          | j                  S t          d	          )
Nr      r	   iIc                 ,    g | ]}t          |          S r   )len).0series     r   
<listcomp>z2DicomFormat.Reader._get_length.<locals>.<listcomp>   s    DDDuE

DDDr   vV(DICOM plugin should know what to expect.)rX   ry   rW   rd   ndimshaperG   modesumr   RuntimeError)rF   rj   nslicess      r   _get_lengthzDicomFormat.Reader._get_length   s   z!k!nQ' Y
 0022
.2jo.B.Bdj&q))G| #s**"1%,, Q;;"NDDDDDEEE"1%,, Q;;1t{+++"1%,,4;'''"#MNNNr   c                     | j         j        dk    r| j         j        d         nd}|dk    r| j         |         | j        fS |dk    r| j         | j        fS t	          d          )Nr{   r   r	   z#Dicom file contains only one slice.)rX   r   r   rW   
IndexError)rF   indexr   s      r   _get_slice_dataz"DicomFormat.Reader._get_slice_data   sj    .2jo.B.Bdj&q))G {{z%($*44!z4:-- !FGGGr   c                    | j         8| j        d         d         }|j        | _        |                                | _         | j         j        dk    r| j         j        d         nd}| j        j        d         dk    r|                     |          S | j        j        d         dk    rr|dk    r|dk    r| j         |         | j        fS g }| j        D ]!}|	                    d |D                        "||                                         ||         j
        fS | j        j        d         dv rK|dk    r|dk    r| j         | j        fS | j        |                                         | j        |         j
        fS t          | j                  dk    r1| j        |                                         | j        |         j
        fS |                     |          S )Nr   r{   r	   r|   r}   c                     g | ]}|S r   r   r   dcm_s     r   r   z0DicomFormat.Reader._get_data.<locals>.<listcomp>      !9!9!94$!9!9!9r   vV)rX   ry   rW   rd   r   r   rG   r   r   extendinfor   rF   r   rj   r   Lr   s         r   	_get_datazDicomFormat.Reader._get_data  s   z!k!nQ' Y
 0022
.2jo.B.Bdj&q))G| #s**++E222"1%,,A::'A++:e,dj88A!% ; ;!9!95!9!9!9::::U83355qx}DD"1%--A::'A++:tz11 E*::<<E*/  T[!!A%% K&6688K&+  ++E222r   c                 f   | j         8| j        d         d         }|j        | _        |                                | _         | j         j        dk    r| j         j        d         nd}|| j        S | j        j        d         dk    r| j        S | j        j        d         dk    rK|dk    r|dk    r| j        S g }| j        D ]!}|                    d |D                        "||         j	        S | j        j        d         dv r%|dk    r|dk    r| j        S | j        |         j	        S t          d          )	Nr   r{   r	   r|   r}   c                     g | ]}|S r   r   r   s     r   r   z5DicomFormat.Reader._get_meta_data.<locals>.<listcomp>D  r   r   r   r   )rX   ry   rW   rd   r   r   rG   r   r   r   rh   r   s         r   _get_meta_dataz!DicomFormat.Reader._get_meta_data/  sE   z!k!nQ' Y
 0022
.2jo.B.Bdj&q))G }z!| #s**z!"1%,,A::'A++:%A!% ; ;!9!95!9!9!9::::U8=("1%--A::'A++:%;u-22 !KLLLr   N)T)__name__
__module____qualname__setr`   rr   ru   propertyry   r   r   r   r   r   r   r   ReaderrP      s        #&355 6	@ 6	@ 6	@ 6	@p	  	  	  
	  	  
	 	O 	O 	O>		H 		H 		H(	3 (	3 (	3T	M 	M 	M 	M 	Mr   r   N)r   r   r   __doc__rK   rN   r   r   r   r   r   r<   r<   z   sx        **6 6 6  zM zM zM zM zM zM zM zM zM zMr   r<   )r   r   r   loggingr   corer   r   r   r   r
   	getLoggerr   rb   r   	byteordersys_is_little_endianr$   r:   r<   r   r   r   <module>r      s   P 
			 



      I I I I I I I I I I      			8	$	$   }0   &     FSM SM SM SM SM& SM SM SM SM SMr   