1 package ini
.trakem2
.imaging
;
3 import ij
.process
.ImageStatistics
;
5 import java
.awt
.Canvas
;
7 import java
.awt
.Dimension
;
8 import java
.awt
.Graphics
;
11 /** Copied and modified from Wayne Rasband's ImageJ ContrastPlot inner class in
12 * ij.plugin.frame.ContrastAdjuster class, in ImageJ 1.43h. */
13 public class ContrastPlot
extends Canvas
15 private static final long serialVersionUID
= 1L;
17 static final int WIDTH
= 128, HEIGHT
=64;
18 double defaultMin
= 0;
19 double defaultMax
= 255;
25 Color color
= Color
.gray
;
27 public ContrastPlot(final double defaultMin
, final double defaultMax
, final double firstMin
, final double firstMax
) {
28 setSize(WIDTH
+1, HEIGHT
+1);
29 this.defaultMin
= defaultMin
;
30 this.defaultMax
= defaultMax
;
35 /** Overrides Component getPreferredSize(). Added to work
36 around a bug in Java 1.4.1 on Mac OS X.*/
37 public Dimension
getPreferredSize() {
38 return new Dimension(WIDTH
+1, HEIGHT
+1);
41 public void setHistogram(ImageStatistics stats
, Color color
) {
43 histogram
= stats
.histogram
;
44 if (histogram
.length
!=256)
45 {histogram
=null; return;}
46 for (int i
=0; i
<128; i
++)
47 histogram
[i
] = (histogram
[2*i
]+histogram
[2*i
+1])/2;
50 for (int i
=0; i
<128; i
++) {
51 if (histogram
[i
]>maxCount
) {
52 maxCount
= histogram
[i
];
57 for (int i
=0; i
<128; i
++) {
58 if ((histogram
[i
]>maxCount2
) && (i
!=mode
))
59 maxCount2
= histogram
[i
];
61 hmax
= stats
.maxCount
;
62 if ((hmax
>(maxCount2
*2)) && (maxCount2
!=0)) {
63 hmax
= (int)(maxCount2
*1.5);
64 histogram
[mode
] = hmax
;
69 public void update(Graphics g
) {
73 public void paint(Graphics g
) {
74 g
.setColor(Color
.white
);
75 g
.fillRect(0, 0, getWidth(), getHeight());
77 double scale
= (double)WIDTH
/(defaultMax
-defaultMin
);
80 slope
= HEIGHT
/(max
-min
);
81 if (min
>=defaultMin
) {
82 x1
= (int)(scale
*(min
-defaultMin
));
87 y1
= HEIGHT
-(int)((defaultMin
-min
)*slope
);
91 if (max
<=defaultMax
) {
92 x2
= (int)(scale
*(max
-defaultMin
));
97 y2
= HEIGHT
-(int)((defaultMax
-min
)*slope
);
101 if (histogram
!=null) {
102 if (os
==null && hmax
!=0) {
103 os
= createImage(WIDTH
,HEIGHT
);
104 Graphics osg
= os
.getGraphics();
105 osg
.setColor(Color
.white
);
106 osg
.fillRect(0, 0, WIDTH
, HEIGHT
);
108 for (int i
= 0; i
< WIDTH
; i
++)
109 osg
.drawLine(i
, HEIGHT
, i
, HEIGHT
- ((int)(HEIGHT
* histogram
[i
])/hmax
));
112 if (os
!=null) g
.drawImage(os
, 0, 0, this);
114 g
.setColor(Color
.white
);
115 g
.fillRect(0, 0, WIDTH
, HEIGHT
);
117 g
.setColor(Color
.black
);
118 g
.drawLine(x1
, y1
, x2
, y2
);
119 g
.drawLine(x2
, HEIGHT
-5, x2
, HEIGHT
);
120 g
.drawRect(0, 0, WIDTH
, HEIGHT
);
122 //System.out.println(" hmax " + hmax + "\n x1,y1 " + x1 +", "+ y1 + "\n min,max " + min +", " + max + "\n defaultMin,Max: " + defaultMin +"," + defaultMax + "\n WIDTH,HEIGHT " + WIDTH +"," + HEIGHT);
125 /** Set new min and max (of the image, not of the plot) and repaint. */
126 public void update(double min
, double max
) {
132 /** Set default min and max (of the image, not of the plot). */
133 public void setDefaultMinAndMax(double min
, double max
) {
136 System
.out
.println("default min/max are " + min
+ ", " + max
);