fixed improper generic parameter use in Tree.duplicateAs > Map, necessary
[trakem2.git] / TrakEM2_ / src / main / java / ini / trakem2 / imaging / ContrastPlot.java
blobc7a436513841cc755978af683813fe5f2548a3e6
1 package ini.trakem2.imaging;
3 import ij.process.ImageStatistics;
5 import java.awt.Canvas;
6 import java.awt.Color;
7 import java.awt.Dimension;
8 import java.awt.Graphics;
9 import java.awt.Image;
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;
20 double min = 0;
21 double max = 255;
22 int[] histogram;
23 int hmax;
24 Image os;
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;
31 this.min = firstMin;
32 this.max = firstMax;
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) {
42 this.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;
48 int maxCount = 0;
49 int mode = 0;
50 for (int i=0; i<128; i++) {
51 if (histogram[i]>maxCount) {
52 maxCount = histogram[i];
53 mode = i;
56 int maxCount2 = 0;
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;
66 os = null;
69 public void update(Graphics g) {
70 paint(g);
73 public void paint(Graphics g) {
74 g.setColor(Color.white);
75 g.fillRect(0, 0, getWidth(), getHeight());
76 int x1, y1, x2, y2;
77 double scale = (double)WIDTH/(defaultMax-defaultMin);
78 double slope = 0.0;
79 if (max!=min)
80 slope = HEIGHT/(max-min);
81 if (min>=defaultMin) {
82 x1 = (int)(scale*(min-defaultMin));
83 y1 = HEIGHT;
84 } else {
85 x1 = 0;
86 if (max>min)
87 y1 = HEIGHT-(int)((defaultMin-min)*slope);
88 else
89 y1 = HEIGHT;
91 if (max<=defaultMax) {
92 x2 = (int)(scale*(max-defaultMin));
93 y2 = 0;
94 } else {
95 x2 = WIDTH;
96 if (max>min)
97 y2 = HEIGHT-(int)((defaultMax-min)*slope);
98 else
99 y2 = 0;
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);
107 osg.setColor(color);
108 for (int i = 0; i < WIDTH; i++)
109 osg.drawLine(i, HEIGHT, i, HEIGHT - ((int)(HEIGHT * histogram[i])/hmax));
110 osg.dispose();
112 if (os!=null) g.drawImage(os, 0, 0, this);
113 } else {
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) {
127 this.min = min;
128 this.max = max;
129 repaint();
132 /** Set default min and max (of the image, not of the plot). */
133 public void setDefaultMinAndMax(double min, double max) {
134 defaultMin = min;
135 defaultMax = max;
136 System.out.println("default min/max are " + min + ", " + max);