
    eI                         d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
mZmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ dgZ G d de      Zy)zz
GraphicsWidget displaying an image histogram along with gradient editor. Can be used to
adjust the appearance of images.
    N   )debug)	functions)Point)QtCoreQtGui	QtWidgets   )AxisItem)GradientEditorItem)GraphicsWidget)LinearRegionItem)PlotCurveItem)ViewBoxHistogramLUTItemc                   
   e Zd ZdZ ej
                  e      Z ej
                  e      Z ej
                  e      Z		 	 ddZ
ddZd ZddZd Zd Zd	 Zd
 Zd Zd Zd ZddZd Zd ZddZd ZddZd Zd Zd Zd Zy)r   a*
  
    :class:`~pyqtgraph.GraphicsWidget` with controls for adjusting the display of an
    :class:`~pyqtgraph.ImageItem`.

    Includes:

      - Image histogram
      - Movable region over the histogram to select black/white levels
      - Gradient editor to define color lookup table for single-channel images

    Parameters
    ----------
    image : pyqtgraph.ImageItem, optional
        If provided, control will be automatically linked to the image and changes to
        the control will be reflected in the image's appearance. This may also be set
        via :meth:`setImageItem`.
    fillHistogram : bool, optional
        By default, the histogram is rendered with a fill. Performance may be improved
        by disabling the fill. Additional control over the fill is provided by
        :meth:`fillHistogram`.
    levelMode : str, optional
        'mono' (default)
            One histogram with a :class:`~pyqtgraph.LinearRegionItem` is displayed to
            control the black/white levels of the image. This option may be used for
            color images, in which case the histogram and levels correspond to all
            channels of the image.
        'rgba'
            A histogram and level control pair is provided for each image channel. The
            alpha channel histogram and level control are only shown if the image
            contains an alpha channel.
    gradientPosition : str, optional
        Position of the gradient editor relative to the histogram. Must be one of
        {'right', 'left', 'top', 'bottom'}. 'right' and 'left' options should be used
        with a 'vertical' orientation; 'top' and 'bottom' options are for 'horizontal'
        orientation.
    orientation : str, optional
        The orientation of the axis along which the histogram is displayed. Either
        'vertical' (default) or 'horizontal'.

    Attributes
    ----------
    sigLookupTableChanged : QtCore.Signal
        Emits the HistogramLUTItem itself when the gradient changes
    sigLevelsChanged : QtCore.Signal
        Emits the HistogramLUTItem itself while the movable region is changing
    sigLevelChangeFinished : QtCore.Signal
        Emits the HistogramLUTItem itself when the movable region is finished changing

    See Also
    --------
    :class:`~pyqtgraph.ImageItem`
        HistogramLUTItem is most useful when paired with an ImageItem.
    :class:`~pyqtgraph.ImageView`
        Widget containing a paired ImageItem and HistogramLUTItem.
    :class:`~pyqtgraph.HistogramLUTWidget`
        QWidget containing a HistogramLUTItem for widget-based layouts.
    Nc                    t        j                  |        d | _        d | _        || _        || _        || _        |dk(  r|dvrd| _        n|dk(  r|dvrd| _        t        j                         | _	        | j                  | j                         | j                  j                  dddd       | j                  j                  d	       t        | 
      | _        | j
                  dk(  rT| j                  j                  d       | j                  j!                  d       | j                  j#                  dd       nS| j                  j%                  d       | j                  j'                  d       | j                  j#                  dd       t)        | j                        | _        | j*                  j-                  d       | j
                  dk(  rdnd}t/        d	dg|d      t/        d	dg|ddt1        j2                  d      d      t/        d	dg|ddt1        j2                  d      d      t/        d	dg|ddt1        j2                  d      d      t/        d	dg|ddt1        j2                  d      d      g| _        | j4                  d	   | _        | j4                  D ]  }|j9                  d        | j                  j;                  |       |j<                  d	   j?                  d!d"       |j<                  d   j?                  d#d"       |j@                  jC                  | jD                         |jF                  jC                  | jH                          dd$dd%d&| j                     }tK        || j                  d'| (      | _&        | j                  d)v rd*nd+}	| j
                  dk(  r| j                  j;                  | jL                  d	|	d	          | j                  j;                  | j                  d	|	d          | j                  j;                  | j*                  d	|	d,          n~| j                  j;                  | jL                  |	d	   d	       | j                  j;                  | j                  |	d   d	       | j                  j;                  | j*                  |	d,   d	       | j*                  jO                  | j*                  jP                  jR                         | j                  jO                  | j*                  jP                  jR                         | j*                  jT                  jC                  | jV                         | j                  jX                  jC                  | jZ                         t\        j^                  j`                  jb                  }
te        d-.      te        d/|
0      te        d1|
0      te        d2|
0      te        d-|
0      g| _3        | jf                  d	   | _4        | jf                  D ]=  }| j
                  dk(  r|jk                  d3       | j                  j;                  |       ? | jm                  |       | jo                          | jq                          || js                  |       y y )4Nc                       y N r       J/usr/lib/python3/dist-packages/pyqtgraph/graphicsItems/HistogramLUTItem.py<lambda>z+HistogramLUTItem.__init__.<locals>.<lambda>\   s    r   vertical>   leftrightr   
horizontal>   topbottomr   r
   r   )parent   -   FT)xy)orientationgreyblock)swapModer)   2   r+   r+   )        UUUUUU?)r(   penbrushspang)r+   r*   r+   r+   )r-   UUUUUU?b)r+   r+   r*   P   )r2         ?wr*   r*   r*   r+   i  z<|g      ?z|>r   r   )r   r   r   r   i)linkViewmaxTickLengthr    >   r   r   )r   r
   r   )r   r
   r   r   )   r:   r:   d   )r.   )r*   r   r   r;   )r.   compositionMode)r   r*   r   r;   )r   r   r*   r;   Z   ):r   __init__lut	imageItem	levelModer%   gradientPositionr	   QGraphicsGridLayoutlayout	setLayoutsetContentsMargins
setSpacingr   vbsetMaximumWidthsetMinimumWidthsetMouseEnabledsetMaximumHeightsetMinimumHeightr   gradient
loadPresetr   fnmkBrushregionsregion	setZValueaddItemlines	addMarkersigRegionChangedconnectregionChangingsigRegionChangeFinishedregionChangedr   axissetFlagGraphicsItemFlagItemStacksBehindParentsigGradientChangedgradientChangedsigRangeChangedviewRangeChangedr   QPainterCompositionModeCompositionMode_Plusr   plotsplotsetRotationfillHistogram_showRegionsautoHistogramRangesetImageItem)selfimagerk   rA   rB   r%   regionOrientationrS   axavgcompri   s               r   r>   zHistogramLUTItem.__init__X   sH   %%"& 0*$)9AR)R$+D!L(-=EV-V$,D!335t{{#&&q!Q2q!&z)GG##C(GG##B'GG##et#4GG$$S)GG$$R(GG##de#4*t7L7LM  ( -1,<,<
,JLPZ aV%6IaV%6c#%::.?#@zSaV%6c#%::.?#@|UaV%6c#%::.?#@zSaV%6c#%::.A#BU
 ll1oll 	GFT"GGOOF#LLO%%dC0LLO%%dC0##++D,?,?@**2243E3EF	G 0040E0EGR$''TR	  004GGiYz)KK		1c!f5KKCF3KKq#a&9KK		3q615KKQ3KKs1vq9dmm<<SST66MMN((001E1EF''(=(=>~~--BB23.E.E.E2DI

 JJqM	JJ 	"D:-  $GGOOD!	"
 	=)!e$ r   c                     |ddddg}t        || j                        D ];  \  }}|r#|j                  |       |j                  |       +|j                  d       = y)a8  Control fill of the histogram curve(s).

        Parameters
        ----------
        fill : bool, optional
            Set whether or not the histogram should be filled.
        level : float, optional
            Set the fill level. See :meth:`PlotCurveItem.setFillLevel
            <pyqtgraph.PlotCurveItem.setFillLevel>`. Only used if ``fill`` is True.
        color : color_like, optional
            Color to use for the fill when the histogram ``levelMode == "mono"``. See
            :meth:`PlotCurveItem.setBrush <pyqtgraph.PlotCurveItem.setBrush>`.
        )r*   r   r   r+   )r   r*   r   r+   )r   r   r*   r+   r7   N)ziprh   setFillLevelsetBrush)ro   filllevelcolorcolorsri   s         r   rk   zHistogramLUTItem.fillHistogram   s^     /?L_`vtzz2 	(KE4!!%(e$!!$'	(r   c                    | j                   dk7  s| j                  j                         sy | j                  j                  d   j                  }| j                         \  }}| j                  j                         j                         }| j                  j                  | j                  j                  j                               }| j                  dk(  r| j                  j                  | t        |j!                         |            t        dd      z   }| j                  j                  | t        |j!                         |            t        dd      z
  }	| j"                  dk(  r!|j%                         }
|j'                         }n|j)                         }
|j+                         }n| j                  j                  | t        ||j-                                     t        dd      z
  }| j                  j                  | t        ||j-                                     t        dd      z   }	| j"                  dk(  r!|j'                         }
|j+                         }n |j%                         }
|j)                         }|j/                  t0        j2                  j4                  j6                         t9        j:                  dd	      |fD ]   }|j=                  |       |j?                  ||
       |j?                  |	|       | j                  dk(  r]|j?                  |j'                         |j+                                |j?                  |j%                         |j)                                |j?                  |j'                         |j%                                |j?                  |j+                         |j)                                 y )
Nmonor   r      r   r   )r   r   r   r;      )width) rA   rS   	isVisiblerV   r.   	getLevelsrH   viewRectcenterrN   mapRectToParentgradRectrectr%   mapFromViewToItemr   r#   rB   
bottomLefttopLeftbottomRighttopRightr$   setRenderHintr   re   
RenderHintAntialiasingrP   mkPensetPendrawLine)ro   pargsr.   mnmxvbcr   p1mnp1mxp2mnp2mxs               r   paintzHistogramLUTItem.paint   s    >>V#4;;+@+@+Bkk"&&!Bgg '')==001G1G1L1L1NOz)77,,T5"3EFqRSTD77,,T5"3EFqRSTD$$/**,'')++-((*77,,T5SUUW3EFqRSTD77,,T5SUUW3EFqRSTD$$0'')((***,++-	11>>?HH^15s; 	HCHHSM JJtT"JJtT" :-

8++-x/@/@/BC

8..0(2F2F2HI

8++-x/B/B/DE

8,,.0D0D0FG	Hr   c                 X   | j                   dk(  rN| j                  j                  | j                  j                  d       | j                  j	                  |||       y| j                  j                  | j                  j
                  d       | j                  j                  |||       y)zbSet the X/Y range on the histogram plot, depending on the orientation. This disables auto-scaling.r   FN)r%   rH   enableAutoRangeYAxis	setYRangeXAxis	setXRange)ro   r   r   paddings       r   setHistogramRangez"HistogramLUTItem.setHistogramRange   st    z)GG##DGGMM59GGb"g.GG##DGGMM59GGb"g.r   c                     | j                   dk(  r| j                  j                         d   S | j                  j                         d   S )z$Returns range on the histogram plot.r   r
   r   )r%   rH   	viewRangero   s    r   getHistogramRangez"HistogramLUTItem.getHistogramRange	  s@    z)77$$&q))77$$&q))r   c                 b    | j                   j                  | j                   j                         y)z*Enable auto-scaling on the histogram plot.N)rH   r   XYAxesr   s    r   rm   z#HistogramLUTItem.autoHistogramRange  s    /r   c                 b    | j                   j                  | j                   j                         y)z+Disable auto-scaling on the histogram plot.N)rH   disableAutoRanger   r   s    r   disableAutoHistogramRangez*HistogramLUTItem.disableAutoHistogramRange  s      0r   c                     t        j                  |      | _        t        |d      r%|j                  j                  | j                         | j                          | j                          | j                  d       y)zoSet an ImageItem to have its levels and LUT automatically controlled by this
        HistogramLUTItem.
        sigImageChangedT)	autoLevelN)	weakrefrefr@   hasattrr   rY   imageChanged_setImageLookupTabler\   )ro   imgs     r   rn   zHistogramLUTItem.setImageItem  sb     !S)3)*''(9(9:!!#D)r   c                 $    | j                          y r   )updater   s    r   rd   z!HistogramLUTItem.viewRangeChanged#  s    r   c                     | j                         | j                          d | _        | j                  j	                  |        y r   )r@   r   r?   sigLookupTableChangedemitr   s    r   rb   z HistogramLUTItem.gradientChanged&  s6    >>'%%'""''-r   c                     | j                   j                         r | j                         j                  d        y | j                         j                  | j                         y r   )rN   isLookupTrivialr@   setLookupTablegetLookupTabler   s    r   r   z%HistogramLUTItem._setImageLookupTable-  sB    ==((*NN++D1NN++D,?,?@r   c                     | j                   dk7  ry|"|j                  t        j                  k(  rd}nd}| j                  "| j
                  j                  ||      | _        | j                  S )z`Return a lookup table from the color gradient defined by this
        HistogramLUTItem.
        r~   N   i   )alpha)rA   dtypenpuint8r?   rN   r   )ro   r   nr   s       r   r   zHistogramLUTItem.getLookupTable3  s`     >>V#9yyBHH$88}}33AU3CDHxxr   c                     | j                         -| j                         j                  | j                                | j                  j	                  |        y r   )r@   	setLevelsr   sigLevelChangeFinishedr   r   s    r   r\   zHistogramLUTItem.regionChangedB  s?    >>'NN&&t~~'78##((.r   c                     | j                         -| j                         j                  | j                                | j                          | j                  j                  |        y r   )r@   r   r   r   sigLevelsChangedr   r   s    r   rZ   zHistogramLUTItem.regionChangingG  sH    >>'NN&&t~~'78""4(r   c                    | j                         y | j                  dk(  r| j                  dd  D ]  }|j                  d        | j                  d   j                  d       t	        j
                         }| j                         j                         } |d       |d   y  | j                  j                  |   |d       |r6|d   d   }|d   d   }| j                  j                  ||g        |d	       y | j                         j                         \  }}| j                  j                  ||g       y | j                  d   j                  d       | j                         j                  d
      }|d   y t        dd      D ]  }	t        |      |	k\  rv||	dz
     }| j                  |	   j                  d        | j                  |	   j                  |  |sV|d   d   }|d   d   }| j                  |	   j                  ||g       | j                  |	   j                  d        | j                          y )Nr~   r
   Fr   Tzget histogramzset plotz
set region)
perChannelr   )r@   rA   rh   
setVisibler   ProfilergetHistogramri   setDatarS   	setRegionr   rangelenrR   rl   )
ro   r   	autoRangepltprofilerhr   r   chis
             r   r   zHistogramLUTItem.imageChangedM  s
   >>#>>V#zz!"~ &u%&JJqM$$T*~~'H --/A_%t|DIIq!Z qT!WqT"X%%r2h/&)335B%%r2h/ JJqM$$U+!..$.?B!u}1a[ 4r7a<1Q3AJJqM,,T2)DJJqM))1- qT!WqT"XQ112r(; JJqM,,U34 r   c                     | j                   dk(  r| j                  j                         S | j                         j	                         }|d}| j
                  d|dz    D cg c]  }|j                          c}S c c}w )zsReturn the min and max levels.

        For rgba mode, this returns a list of the levels for each channel.
        r~   r   r
   )rA   rS   	getRegionr@   channelsrR   )ro   nchr)   s      r   r   zHistogramLUTItem.getLevelsz  sk    
 >>V#;;((**.."++-C{+/<<#a%+@AaAKKMAAAs    A:c                    d||hv r|d|d   v rt        d      | j                  dk(  r(||d   \  }}| j                  j                  ||f       y|d||fgz  }t	        || j
                  dd       D ]  \  }}|j                  |        y)a4  Set the min/max (bright and dark) levels.

        Parameters
        ----------
        min : float, optional
            Minimum level.
        max : float, optional
            Maximum level.
        rgba : list, optional
            Sequence of (min, max) pairs for each channel for 'rgba' mode.
        Nr   zMust specify min and max levelsr~      r
   )
ValueErrorrA   rS   r   rv   rR   )ro   minmaxrgbalevelsrS   s         r   r   zHistogramLUTItem.setLevels  s     C:4<447?>??>>V#{7SKK!!3*-|3*~"%dDLL,<"= )  ()r   c                    |dvrt        d|       || j                  k(  ry| j                         }|| _        | j                          |dk(  r5t	        j
                  |      j                  d      } | j                  |  n|gdz  }| j                  |       | j                         -| j                         j                  | j                                | j                          | j                          y)	zsSet the method of controlling the image levels offered to the user.

        Options are 'mono' or 'rgba'.
        >   r~   r   z0Level mode must be one of {'mono', 'rgba'}, got Nr~   r   )r]   r   )r   )r   rA   r   rl   r   arraymeanr   r@   r   r   )ro   mode	oldLevelsr   s       r   setLevelModezHistogramLUTItem.setLevelMode  s    
 ''QRVQWXYY4>>!NN$	 6>XXi(--1-5FDNNF#[1_FNNN' >>'NN&&t~~'78r   c                    t        t        | j                              D ]   }| j                  |   j                  d       " | j                  dk(  rd}| j                         "| j                         j                         }|d}d|z  }t        d|dz         D ]H  }| j                  |   j                  d       | j                  |   j                  |dz
  |z  ||z         J | j                  j                          y | j                  dk(  r9| j                  d	   j                  d       | j                  j                          y t        d
| j                         )NFr   r   r   r5   r
   Tr~   r   zUnknown level mode )r   r   rR   r   rA   r@   r   setSpanrN   hideshowr   )ro   r   r   xdifs       r   rl   zHistogramLUTItem._showRegions  s.   s4<<() 	.ALLO&&u-	. >>V#C~~+nn&//1;C9D1c!e_ @Q**40Q''1a$h?@ MM ^^v%LLO&&t,MM 24>>2BCDDr   c                 n    | j                   j                         | j                         | j                  dS )N)rN   r   r   )rN   	saveStater   rA   r   s    r   r   zHistogramLUTItem.saveState  s-    //1nn&NN
 	
r   c                     d|v r| j                  |d          | j                  j                  |d           | j                  |d     y )Nr   rN   r   )r   rN   restoreStater   )ro   states     r   r   zHistogramLUTItem.restoreState  sD    U?eFm,""5#45h(r   )NTr~   r   r   )Tr,   )r;   r;   r:   )g?)NNN)FF)__name__
__module____qualname____doc__r   Signalobjectr   r   r   r>   rk   r   r   r   rm   r   rn   rd   rb   r   r   r\   rZ   r   r   r   r   rl   r   r   r   r   r   r   r      s    8t *FMM&1$v}}V,*V]]62AG7Ab%H(,,H\/*01	*.A/
)+ ZB)2>E,
)r   )r   r   numpyr    r   r   rP   r   Qtr   r   r	   r   r   r   r   r   r   __all__r   r   r   r   <module>r     sI         ) )  2 * . ( 
G)~ G)r   