σ
;c]c           @` sϊ   d  Z  d d l m Z m Z m Z d d l Z d d l Z d d l Z d d l Z d d l	 m
 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 e f d     YZ e
 j e d d d d   d S(   s!    Plugin for reading DICOM files.
i    (   t   absolute_importt   print_functiont   divisionNi   (   t   formats(   t   Formatt   BaseProgressIndicatort   StdoutProgressIndicator(   t   read_n_bytesc           C` s   d d l  m a t S(   Ni   (   t   _dicom(   t    R   (    (    (    s4   lib/python2.7/site-packages/imageio/plugins/dicom.pyt   load_lib   s    t   littlec          C` s   d d t  j j d  }  x9 d	 D]1 } t j j | |   } t j j |  r! | Sq! Wy t j |  d g  |  SWn t	 k
 r d  SXd  S(
   Nt   dcmdjpegs   .exet   wins   c:\dcmtks   c:\Program Filess   c:\Program Files\dcmtks   c:\Program Files (x86)\dcmtks	   --version(   s   c:\dcmtks   c:\Program Filess   c:\Program Files\dcmtks   c:\Program Files (x86)\dcmtk(   t   syst   platformt
   startswitht   ost   patht   joint   isfilet
   subprocesst
   check_callt	   Exceptiont   None(   t   fnamet   dirt   filename(    (    s4   lib/python2.7/site-packages/imageio/plugins/dicom.pyt   get_dcmdjpeg_exe*   s       
t   DicomFormatc           B` s9   e  Z d  Z d   Z d   Z d e j f d     YZ RS(   s   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,
    and (to the best of our knowledge) has the same limitations as
    pydicom with regard to the type of files that it can handle. 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. We plan to allow reading all
    tags in the future (by using pydicom).

    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 for reading
    ----------------------
    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.

    c         C` sΗ   t  j j | j  r³ t  j | j  } x t |  D]t } t  j j | j |  } t  j j |  r4 d | k r4 t | d   } t	 | d  } Wd  QX| d d !d k Sq4 Wt
 Sn  | j d d !d k S(   Nt   DICOMDIRt   rbi   i   i   t   DICM(   R   R   t   isdirR   t   listdirt   sortedR   R   t   openR   t   Falset
   firstbytes(   t   selft   requestt   filesR   R   t   ft   first_bytes(    (    s4   lib/python2.7/site-packages/imageio/plugins/dicom.pyt	   _can_readY   s    c         C` s   t  S(   N(   R%   (   R'   R(   (    (    s4   lib/python2.7/site-packages/imageio/plugins/dicom.pyt
   _can_writeh   s    t   Readerc           B` sG   e  Z e d   Z d   Z e d    Z d   Z d   Z d   Z	 RS(   c         C` s  t  s t   n  t j j |  j j  r= i  |  _ d  |  _	 nζ y t  j
 |  j j    } Wn¬ t  j k
 r} d t |  k rt   } | s   n  |  j j   } | d } y t j | | | g  Wn t k
 rα |  n Xt j d  t  j
 |  } q  n X| j |  _ | j   |  _	 d  |  _ t | t  rG| |  _ nT | t k rkt d  } | |  _ n0 | d  t f k rt d  |  _ n t d   d  S(   Nt   JPEGs   .raws?   DICOM file contained compressed data. Used dcmtk to convert it.s   Reading DICOMt   Dummys   Invalid value for progress.(   R   R
   R   R   R!   R(   R   t   _infoR   t   _datat   SimpleDicomReadert   get_filet   CompressedDicomt   strR   t   get_local_filenameR   R   R   t   loggert   warningt   get_numpy_arrayt   _seriest
   isinstanceR   t   _progressIndicatort   TrueR   R%   t
   ValueError(   R'   t   progresst   dcmt   errt   exet   fname1t   fname2t   p(    (    s4   lib/python2.7/site-packages/imageio/plugins/dicom.pyt   _openp   sB    
		

	c         C` s   d  |  _ d  |  _ d  |  _ d  S(   N(   R   R1   R2   R;   (   R'   (    (    s4   lib/python2.7/site-packages/imageio/plugins/dicom.pyt   _close   s    		c         C` s:   |  j  d  k r3 |  j } t j |  j |  |  _  n  |  j  S(   N(   R;   R   R=   R   t   process_directoryR(   (   R'   t   pi(    (    s4   lib/python2.7/site-packages/imageio/plugins/dicom.pyt   series₯   s    	c         C` s8  |  j  d  k r> |  j d d } | j |  _ | j   |  _  n  |  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¦ | St	 g  |  j D] } t
 |  ^ q³  Sne |  j j d d k r| d k rυ d St
 |  j  Sn/ |  j j d d k r(t
 |  j  St d   d  S(	   Ni    i   i   t   it   It   vt   Vs(   DICOM plugin should know what to expect.(   R2   R   RK   R1   R:   t   ndimt   shapeR(   t   modet   sumt   lent   RuntimeError(   R'   RA   t   nslicest   serie(    (    s4   lib/python2.7/site-packages/imageio/plugins/dicom.pyt   _get_length¬   s$    ()c         C` sΣ  |  j  d  k r> |  j d d } | j |  _ | j   |  _  n  |  j  j d k r` |  j  j d n d } |  j j d d k rΗ | d k r |  j  | |  j f S| d k rΈ |  j  |  j f St	 d   n|  j j d d k ra| d k r	| d k r	|  j  | |  j f Sg  } x1 |  j D]& } | j
 g  | D] } | ^ q, qW| | j   | | j f Snn |  j j d d k rΓ| d k r| d k r|  j  |  j f S|  j | j   |  j | j f Sn t d   d  S(	   Ni    i   i   RL   s#   Dicom file contains only one slice.RM   t   vVs(   DICOM plugin should know what to expect.(   R2   R   RK   R1   R:   RP   RQ   R(   RR   t
   IndexErrort   extendt   infoR?   (   R'   t   indexRA   RV   t   LRW   t   dcm_(    (    s4   lib/python2.7/site-packages/imageio/plugins/dicom.pyt	   _get_dataΛ   s0    ($c         C` si  |  j  d  k r> |  j d d } | j |  _ | j   |  _  n  |  j  j d k r` |  j  j d n d } | d  k ry |  j S|  j j d d k r |  j S|  j j d d k r| d k rΛ | d k rΛ |  j Sg  } x1 |  j D]& } | j	 g  | D] } | ^ qξ  qΫ W| | j
 SnR |  j j d d k rY| d k rH| d k rH|  j S|  j | j
 Sn t d   d  S(   Ni    i   i   RL   RM   RY   s(   DICOM plugin should know what to expect.(   R2   R   RK   R1   R:   RP   RQ   R(   RR   R[   R\   R?   (   R'   R]   RA   RV   R^   RW   R_   (    (    s4   lib/python2.7/site-packages/imageio/plugins/dicom.pyt   _get_meta_dataπ   s*    ($(
   t   __name__t
   __module__R>   RG   RH   t   propertyRK   RX   R`   Ra   (    (    (    s4   lib/python2.7/site-packages/imageio/plugins/dicom.pyR.   o   s   /			%(   Rb   Rc   t   __doc__R,   R-   R   R.   (    (    (    s4   lib/python2.7/site-packages/imageio/plugins/dicom.pyR   =   s   		t   DICOMs.   Digital Imaging and Communications in Medicines   .dcm .ct .mrit   iIvV(   Re   t
   __future__R    R   R   R   R   t   loggingR   R	   R   t   coreR   R   R   R   R   R   t	   getLoggerRb   R8   R
   t	   byteordert   sys_is_little_endianR   R   t
   add_format(    (    (    s4   lib/python2.7/site-packages/imageio/plugins/dicom.pyt   <module>   s(   
		Υ