Skip to content

Commit

Permalink
2023.10.17 (1.54g32; Image>Adjust>Threshold)
Browse files Browse the repository at this point in the history
  • Loading branch information
rasband committed Oct 17, 2023
1 parent 24983ae commit 9ea5782
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 64 deletions.
2 changes: 1 addition & 1 deletion ij/ImageJ.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class ImageJ extends Frame implements ActionListener,

/** Plugins should call IJ.getVersion() or IJ.getFullVersion() to get the version string. */
public static final String VERSION = "1.54g";
public static final String BUILD = "30";
public static final String BUILD = "32";
public static Color backgroundColor = new Color(237,237,237);
/** SansSerif, 12-point, plain font. */
public static final Font SansSerif12 = new Font("SansSerif", Font.PLAIN, 12);
Expand Down
91 changes: 29 additions & 62 deletions ij/plugin/frame/ThresholdAdjuster.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class ThresholdAdjuster extends PlugInDialog implements PlugIn, Measureme
int minValue = -1; // min slider, 0-255
int maxValue = -1;
int sliderRange = 256;
boolean doAutoAdjust,doReset,doApplyLut,doStateChange,doSet,doBackground; //actions required from user interface
boolean doAutoAdjust,doReset,doApplyLut,doStateChange,doSet; //actions required from user interface

Panel panel;
Button autoB, resetB, applyB, setB;
Expand All @@ -62,12 +62,11 @@ public class ThresholdAdjuster extends PlugInDialog implements PlugIn, Measureme
boolean done;
int lutColor;
Choice methodChoice, modeChoice;
Checkbox darkBackground, stackCheckbox, noResetCheckbox, rawValues, sixteenBitCheckbox;
Checkbox darkBackgroundCheckbox, stackCheckbox, noResetCheckbox, rawValues, sixteenBitCheckbox;
boolean firstActivation = true;
boolean setButtonPressed;
boolean noReset = true;
boolean sixteenBit = false;
boolean noResetChanged;
boolean enterPressed;
boolean windowActivated;

Expand Down Expand Up @@ -210,10 +209,10 @@ public ThresholdAdjuster() {
panel = new Panel();
panel.setLayout(new GridLayout(3, 2));
boolean db = Prefs.get(DARK_BACKGROUND, Prefs.blackBackground?true:false);
darkBackground = new Checkbox("Dark background");
darkBackground.setState(db);
darkBackground.addItemListener(this);
panel.add(darkBackground);
darkBackgroundCheckbox = new Checkbox("Dark background");
darkBackgroundCheckbox.setState(db);
darkBackgroundCheckbox.addItemListener(this);
panel.add(darkBackgroundCheckbox);
stackCheckbox = new Checkbox("Stack histogram");
stackCheckbox.setState(false);
stackCheckbox.addItemListener(this);
Expand Down Expand Up @@ -382,6 +381,7 @@ void setLutColor(int mode) {

public synchronized void itemStateChanged(ItemEvent e) {
Object source = e.getSource();
boolean conditionalAutoAdjust = false;
if (source==methodChoice) {
method = methodChoice.getSelectedItem();
doAutoAdjust = true;
Expand All @@ -395,22 +395,25 @@ public synchronized void itemStateChanged(ItemEvent e) {
else
Recorder.recordString("call(\"ij.plugin.frame.ThresholdAdjuster.setMode\", \""+modes[mode]+"\");\n");
}
} else if (source==darkBackground) {
doBackground = true;
} else if (source==darkBackgroundCheckbox) {
conditionalAutoAdjust = true;
} else if (source==noResetCheckbox) {
noReset = noResetCheckbox.getState();
noResetChanged = true;
doAutoAdjust = true;
conditionalAutoAdjust = true;
} else if (source==rawValues) {
ThresholdAdjuster.update();
} else if (source==sixteenBitCheckbox) {
sixteenBit = sixteenBitCheckbox.getState();
if (sixteenBit)
noReset = true;
noResetChanged = true;
doAutoAdjust = true;
conditionalAutoAdjust = true;
} else if (source==stackCheckbox) {
conditionalAutoAdjust = true;
} else
doAutoAdjust = true;
ImagePlus imp = WindowManager.getCurrentImage();
if (conditionalAutoAdjust && imp!=null && imp.isThreshold())
doAutoAdjust = true;
notify();
}

Expand Down Expand Up @@ -489,8 +492,7 @@ private void resetMinAndMax(ImageProcessor ip) {
if (ip.getMin()!=stats.min || ip.getMax()!=stats.max) {
ip.resetMinAndMax();
ContrastAdjuster.update();
} else
ip.resetMinAndMax();
}
}
}

Expand All @@ -500,7 +502,7 @@ boolean entireStack(ImagePlus imp) {

void autoSetLevels(ImagePlus imp) {
int bitDepth = imp.getBitDepth();
boolean darkb = darkBackground!=null && darkBackground.getState();
boolean darkb = darkBackgroundCheckbox!=null && darkBackgroundCheckbox.getState();
boolean stack = entireStack(imp);
boolean hist16 = sixteenBit && bitDepth!=32;
String methodAndOptions = method+(darkb?" dark":"")+(hist16?" 16-bit":"")+(stack?" stack":"")+(noReset?" no-reset":"");
Expand Down Expand Up @@ -552,7 +554,7 @@ void autoSetLevels(ImagePlus imp) {
* (E.g. dark background and non-inverting LUT)
*/
boolean thresholdHigh(ImageProcessor ip) {
boolean darkb = darkBackground!=null && darkBackground.getState();
boolean darkb = darkBackgroundCheckbox!=null && darkBackgroundCheckbox.getState();
boolean invertedLut = ip.isInvertedLut();
return invertedLut ? !darkb : darkb;
}
Expand Down Expand Up @@ -669,7 +671,11 @@ void updateLabels(ImagePlus imp, ImageProcessor ip) {

/** Converts a number to a String, such that it should not take much space (for the minLabel, maxLabel TextFields) */
String d2s(double x) {
return Math.abs(x)>=1e6 ? IJ.d2s(x,-2) : ResultsTable.d2s(x,2); //the latter uses exp notation also for small x
int digits = 2;
if (Math.abs(x)<100) digits=3;
if (Math.abs(x)<10) digits=4;
if (x<0 && digits==4) digits=3;
return Math.abs(x)>=1e6 ? IJ.d2s(x,-2) : ResultsTable.d2s(x,digits); //the latter uses exp notation also for small x
}

void updateScrollBars() {
Expand Down Expand Up @@ -724,16 +730,6 @@ void adjustMaxThreshold(ImagePlus imp, ImageProcessor ip, int cvalue) {

void reset(ImagePlus imp, ImageProcessor ip) {
//IJ.log("reset1: "+noReset+" "+sixteenBitChanged+" "+mode);
if (noResetChanged) {
noResetChanged = false;
if ((noReset&&mode!=OVER_UNDER) || ip.getBitDepth()==8)
return;
if (!noReset) {
ImageStatistics stats = ip.getStats();
if (ip.getMin()==stats.min && ip.getMax()==stats.max)
return; // not contrast enhanced; no need to reset
}
}
ip.resetThreshold();
if (!noReset)
resetMinAndMax(ip);
Expand Down Expand Up @@ -838,33 +834,6 @@ void autoThreshold(ImagePlus imp, ImageProcessor ip) {
updateScrollBars();
}

/** User has clicked 'Dark background'.
* Switch only if the current thresholds are consistent with
* the previous 'Dark background' state.
*/
void switchBackground(ImagePlus imp, ImageProcessor ip) {
if (minThreshold < 0) { //remove NO_THRESHOLD
autoThreshold(imp, ip);
return;
}
if (thresholdHigh(ip)) {
if (minThreshold == 0) {
minThreshold = maxThreshold+1;
if (minThreshold > 255) minThreshold = 255;
maxThreshold = 255;
}
} else {
if (maxThreshold == 255) {
maxThreshold = minThreshold-1;
if (maxThreshold < 0) maxThreshold = 0;
minThreshold = 0;
}
}
minSlider.setValue((int)minThreshold);
maxSlider.setValue((int)maxThreshold);
scaleUpAndSet(ip, minThreshold, maxThreshold);
}

void apply(ImagePlus imp) {
if (imp.getProcessor().getMinThreshold()==ImageProcessor.NO_THRESHOLD) {
IJ.error("Thresholder", "Threshold is not set");
Expand All @@ -889,7 +858,7 @@ else if (!d.yesPressed()) {

void runThresholdCommand() {
Thresholder.setMethod(method);
Thresholder.setBackground(darkBackground.getState()?"Dark":"Light");
Thresholder.setBackground(darkBackgroundCheckbox.getState()?"Dark":"Light");
if (Recorder.record) {
Recorder.setCommand("Convert to Mask");
(new Thresholder()).run("mask");
Expand All @@ -898,13 +867,13 @@ void runThresholdCommand() {
(new Thresholder()).run("mask");
}

static final int RESET=0, AUTO=1, HIST=2, APPLY=3, STATE_CHANGE=4, MIN_THRESHOLD=5, MAX_THRESHOLD=6, SET=7, BACKGROUND=8;
static final int RESET=0, AUTO=1, HIST=2, APPLY=3, STATE_CHANGE=4, MIN_THRESHOLD=5, MAX_THRESHOLD=6, SET=7;

// Separate thread that does the potentially time-consuming processing
public void run() {
while (!done) {
synchronized(this) {
if (!doAutoAdjust && !doReset && !doApplyLut && !doStateChange && !doSet && !doBackground && minValue<0 && maxValue<0) {
if (!doAutoAdjust && !doReset && !doApplyLut && !doStateChange && !doSet && minValue<0 && maxValue<0) {
try {wait();}
catch(InterruptedException e) {}
}
Expand All @@ -925,7 +894,6 @@ void doUpdate() {
else if (doApplyLut) { action = APPLY; doApplyLut = false; }
else if (doStateChange) { action = STATE_CHANGE; doStateChange = false; }
else if (doSet) { action = SET; doSet = false; }
else if (doBackground) { action = BACKGROUND; doBackground = false; }
else if (minValue>=0) { action = MIN_THRESHOLD; minValue = -1; }
else if (maxValue>=0) { action = MAX_THRESHOLD; maxValue = -1; }
else return;
Expand Down Expand Up @@ -953,7 +921,6 @@ void doUpdate() {
case SET: doSet(imp, ip); break;
case MIN_THRESHOLD: adjustMinThreshold(imp, ip, min); break;
case MAX_THRESHOLD: adjustMaxThreshold(imp, ip, max); break;
case BACKGROUND: switchBackground(imp, ip); break;
}
updatePlot(ip);
updateLabels(imp, ip);
Expand All @@ -969,7 +936,7 @@ public void close() {
done = true;
Prefs.saveLocation(LOC_KEY, getLocation());
Prefs.set(MODE_KEY, mode);
Prefs.set(DARK_BACKGROUND, darkBackground.getState());
Prefs.set(DARK_BACKGROUND, darkBackgroundCheckbox.getState());
Prefs.set(NO_RESET, noResetCheckbox.getState());
Prefs.set(SIXTEEN_BIT, sixteenBitCheckbox.getState());
Prefs.set(RAW_VALUES, rawValues.getState());
Expand Down Expand Up @@ -1013,7 +980,7 @@ public static void update() {
}

public static boolean isDarkBackground() {
return instance!=null?instance.darkBackground.getState():false;
return instance!=null?instance.darkBackgroundCheckbox.getState():false;
}


Expand Down
2 changes: 1 addition & 1 deletion release-notes.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</head>
<body>

<li> <u>1.54g30 16 October 2023</u>
<li> <u>1.54g32 17 October 2023</u>
<ul>
<li> Thanks to Yuekan Jiao, added a “16-bit histogram”
option to the <i>Image&gt;Adjust&gt;Threshold</i> dialog
Expand Down

0 comments on commit 9ea5782

Please sign in to comment.