use of ij.astro.AstroImageJ in project astroimagej by keastrid.
the class PlotObject method drawAxesTicksGridNumbers.
/**
* Draws ticks, grid and axis label for each tick/grid line.
* The grid or major tick spacing in each direction is given by steps
*/
@AstroImageJ(reason = "Set font size to 12 for axis labels", modified = true)
void drawAxesTicksGridNumbers(double[] steps) {
if (ip == null)
return;
// create categories for the axes (if any)
String[] xCats = labelsInBraces('x');
String[] yCats = labelsInBraces('y');
// for scientific notation
String multiplySymbol = getMultiplySymbol();
Font scFont = scFont(pp.frame.getFont());
// for axis numbers if full size does not fit
Font scFontMedium = scFont.deriveFont(scFont.getSize2D() * 10f / 12f);
// for subscripts
Font scFontSmall = scFont.deriveFont(scFont.getSize2D() * 9f / 12f);
ip.setFont(scFont);
ip.setFontSize(12);
FontMetrics fm = ip.getFontMetrics();
int fontAscent = fm.getAscent();
ip.setJustification(LEFT);
// --- A l o n g X A x i s
int yOfXAxisNumbers = topMargin + frameHeight + fm.getHeight() * 5 / 4 + sc(2);
if (hasFlag(X_NUMBERS | (logXAxis ? (X_TICKS | X_MINOR_TICKS) : X_LOG_TICKS) + X_GRID)) {
Font baseFont = scFont;
boolean majorTicks = logXAxis ? hasFlag(X_LOG_TICKS) : hasFlag(X_TICKS);
boolean minorTicks = hasFlag(X_MINOR_TICKS);
minorTicks = minorTicks && (xCats == null);
double step = steps[0];
int i1 = (int) Math.ceil(Math.min(xMin, xMax) / step - 1.e-10);
int i2 = (int) Math.floor(Math.max(xMin, xMax) / step + 1.e-10);
// is not given, NaN cast to 0
int suggestedDigits = (int) Tools.getNumberFromList(pp.frame.options, "xdecimals=");
int digits = getDigits(xMin, xMax, step, 7, suggestedDigits);
int y1 = topMargin;
int y2 = topMargin + frameHeight;
if (xMin == xMax) {
if (hasFlag(X_NUMBERS)) {
String s = IJ.d2s(xMin, getDigits(xMin, 0.001 * xMin, 5, suggestedDigits));
int y = yBasePxl;
ip.drawString(s, xBasePxl - ip.getStringWidth(s) / 2, yOfXAxisNumbers);
}
} else {
if (hasFlag(X_NUMBERS)) {
int w1 = ip.getStringWidth(IJ.d2s(currentMinMax[0], logXAxis ? -1 : digits));
int w2 = ip.getStringWidth(IJ.d2s(currentMinMax[1], logXAxis ? -1 : digits));
int wMax = Math.max(w1, w2);
if (wMax > Math.abs(step * xScale) - sc(8)) {
// small font if there is not enough space for the numbers
baseFont = scFontMedium;
ip.setFont(baseFont);
}
}
for (int i = 0; i <= (i2 - i1); i++) {
double v = (i + i1) * step;
int x = (int) Math.round((v - xMin) * xScale) + leftMargin;
if (xCats != null) {
int index = (int) v;
double remainder = Math.abs(v - Math.round(v));
if (index >= 0 && index < xCats.length && remainder < 1e-9) {
String s = xCats[index];
String[] parts = s.split("\n");
int w = 0;
for (int jj = 0; jj < parts.length; jj++) w = Math.max(w, ip.getStringWidth(parts[jj]));
ip.drawString(s, x - w / 2, yOfXAxisNumbers);
// ip.drawString(s, x-ip.getStringWidth(s)/2, yOfXAxisNumbers);
}
continue;
}
if (hasFlag(X_GRID)) {
ip.setColor(gridColor);
ip.drawLine(x, y1, x, y2);
ip.setColor(Color.black);
}
if (majorTicks) {
ip.drawLine(x, y1, x, y1 + sc(tickLength));
ip.drawLine(x, y2, x, y2 - sc(tickLength));
}
if (hasFlag(X_NUMBERS)) {
if (logXAxis || digits < 0) {
drawExpString(logXAxis ? Math.pow(10, v) : v, logXAxis ? -1 : -digits, x, yOfXAxisNumbers - fontAscent / 2, CENTER, fontAscent, baseFont, scFontSmall, multiplySymbol);
} else {
String s = IJ.d2s(v, digits);
ip.drawString(s, x - ip.getStringWidth(s) / 2, yOfXAxisNumbers);
}
}
}
// nunbers on log minor ticks only if < 2 decades
boolean haveMinorLogNumbers = i2 - i1 < 2;
if (minorTicks && (!logXAxis || step > 1.1)) {
// 'standard' log minor ticks only for full decades
// non-log: 4 or 5 minor ticks per major tick
double mstep = niceNumber(step * 0.19);
double minorPerMajor = step / mstep;
if (// major steps are not an integer multiple of minor steps? (e.g. user step 90 deg)
Math.abs(minorPerMajor - Math.round(minorPerMajor)) > 1e-10)
mstep = step / 4;
if (logXAxis && mstep < 1)
mstep = 1;
i1 = (int) Math.ceil(Math.min(xMin, xMax) / mstep - 1.e-10);
i2 = (int) Math.floor(Math.max(xMin, xMax) / mstep + 1.e-10);
for (int i = i1; i <= i2; i++) {
double v = i * mstep;
int x = (int) Math.round((v - xMin) * xScale) + leftMargin;
ip.drawLine(x, y1, x, y1 + sc(minorTickLength));
ip.drawLine(x, y2, x, y2 - sc(minorTickLength));
}
} else if (logXAxis && majorTicks && Math.abs(xScale) > sc(MIN_X_GRIDSPACING)) {
// minor ticks for log
// more numbers on minor ticks when zoomed in
int minorNumberLimit = haveMinorLogNumbers ? (int) (0.12 * Math.abs(xScale) / (fm.charWidth('0') + sc(2))) : 0;
i1 = (int) Math.floor(Math.min(xMin, xMax) - 1.e-10);
i2 = (int) Math.ceil(Math.max(xMin, xMax) + 1.e-10);
for (int i = i1; i <= i2; i++) {
for (int m = 2; m < 10; m++) {
double v = i + Math.log10(m);
if (v > Math.min(xMin, xMax) && v < Math.max(xMin, xMax)) {
int x = (int) Math.round((v - xMin) * xScale) + leftMargin;
ip.drawLine(x, y1, x, y1 + sc(minorTickLength));
ip.drawLine(x, y2, x, y2 - sc(minorTickLength));
if (m <= minorNumberLimit)
drawExpString(Math.pow(10, v), 0, x, yOfXAxisNumbers - fontAscent / 2, CENTER, fontAscent, baseFont, scFontSmall, multiplySymbol);
}
}
}
}
}
}
// --- A l o n g Y A x i s
ip.setFont(scFont);
ip.setFontSize(12);
int maxNumWidth = 0;
int xNumberRight = leftMargin - sc(2) - ip.getStringWidth("0") / 2;
Rectangle rect = ip.getStringBounds("0169");
int yNumberOffset = -rect.y - rect.height / 2;
if (hasFlag(Y_NUMBERS | (logYAxis ? (Y_TICKS | Y_MINOR_TICKS) : Y_LOG_TICKS) + Y_GRID)) {
ip.setJustification(RIGHT);
Font baseFont = scFont;
boolean majorTicks = logYAxis ? hasFlag(Y_LOG_TICKS) : hasFlag(Y_TICKS);
boolean minorTicks = logYAxis ? hasFlag(Y_LOG_TICKS) : hasFlag(Y_MINOR_TICKS);
minorTicks = minorTicks && (yCats == null);
double step = steps[1];
int i1 = (int) Math.ceil(Math.min(yMin, yMax) / step - 1.e-10);
int i2 = (int) Math.floor(Math.max(yMin, yMax) / step + 1.e-10);
// is not given, NaN cast to 0
int suggestedDigits = (int) Tools.getNumberFromList(pp.frame.options, "ydecimals=");
int digits = getDigits(yMin, yMax, step, 5, suggestedDigits);
int x1 = leftMargin;
int x2 = leftMargin + frameWidth;
if (yMin == yMax) {
if (hasFlag(Y_NUMBERS)) {
String s = IJ.d2s(yMin, getDigits(yMin, 0.001 * yMin, 5, suggestedDigits));
maxNumWidth = ip.getStringWidth(s);
int y = yBasePxl;
ip.drawString(s, xNumberRight, y + fontAscent / 2 + sc(1));
}
} else {
int digitsForWidth = logYAxis ? -1 : digits;
if (digitsForWidth < 0) {
// "1.0*10^5" etc. needs more space than 1.0*5, simulate by adding one decimal
digitsForWidth--;
xNumberRight += sc(1) + ip.getStringWidth("0") / 4;
}
String str1 = IJ.d2s(currentMinMax[2], digitsForWidth);
String str2 = IJ.d2s(currentMinMax[3], digitsForWidth);
if (digitsForWidth < 0) {
str1 = str1.replaceFirst("E", multiplySymbol);
str2 = str2.replaceFirst("E", multiplySymbol);
}
int w1 = ip.getStringWidth(str1);
int w2 = ip.getStringWidth(str2);
int wMax = Math.max(w1, w2);
if (hasFlag(Y_NUMBERS)) {
if (wMax > xNumberRight - sc(4) - (pp.yLabel.label.length() > 0 ? fm.getHeight() : 0)) {
// small font if there is not enough space for the numbers
baseFont = scFontMedium;
ip.setFont(baseFont);
}
}
// IJ.log(IJ.d2s(currentMinMax[2],digits)+": w="+w1+"; "+IJ.d2s(currentMinMax[3],digits)+": w="+w2+baseFont+" Space="+(leftMargin-sc(4+5)-fm.getHeight()));
for (int i = i1; i <= i2; i++) {
double v = step == 0 ? yMin : i * step;
int y = topMargin + frameHeight - (int) Math.round((v - yMin) * yScale);
if (yCats != null) {
int index = (int) v;
double remainder = Math.abs(v - Math.round(v));
if (index >= 0 && index < yCats.length && remainder < 1e-9) {
String s = yCats[index];
// multi-line cat labels
int multiLineOffset = 0;
for (int jj = 0; jj < s.length(); jj++) if (s.charAt(jj) == '\n')
multiLineOffset -= rect.height / 2;
ip.drawString(s, xNumberRight, y + yNumberOffset + multiLineOffset);
}
continue;
}
if (hasFlag(Y_GRID)) {
ip.setColor(gridColor);
ip.drawLine(x1, y, x2, y);
ip.setColor(Color.black);
}
if (majorTicks) {
ip.drawLine(x1, y, x1 + sc(tickLength), y);
ip.drawLine(x2, y, x2 - sc(tickLength), y);
}
if (hasFlag(Y_NUMBERS)) {
int w = 0;
if (logYAxis || digits < 0) {
w = drawExpString(logYAxis ? Math.pow(10, v) : v, logYAxis ? -1 : -digits, xNumberRight, y, RIGHT, fontAscent, baseFont, scFontSmall, multiplySymbol);
} else {
String s = IJ.d2s(v, digits);
w = ip.getStringWidth(s);
ip.drawString(s, xNumberRight, y + yNumberOffset);
}
if (w > maxNumWidth)
maxNumWidth = w;
}
}
// numbers on log minor ticks only if < 2 decades
boolean haveMinorLogNumbers = i2 - i1 < 2;
if (minorTicks && (!logYAxis || step > 1.1)) {
// 'standard' log minor ticks only for full decades
// non-log: 4 or 5 minor ticks per major tick
double mstep = niceNumber(step * 0.19);
double minorPerMajor = step / mstep;
if (// major steps are not an integer multiple of minor steps? (e.g. user step 90 deg)
Math.abs(minorPerMajor - Math.round(minorPerMajor)) > 1e-10)
mstep = step / 4;
if (logYAxis && step < 1)
mstep = 1;
i1 = (int) Math.ceil(Math.min(yMin, yMax) / mstep - 1.e-10);
i2 = (int) Math.floor(Math.max(yMin, yMax) / mstep + 1.e-10);
for (int i = i1; i <= i2; i++) {
double v = i * mstep;
int y = topMargin + frameHeight - (int) Math.round((v - yMin) * yScale);
ip.drawLine(x1, y, x1 + sc(minorTickLength), y);
ip.drawLine(x2, y, x2 - sc(minorTickLength), y);
}
}
if (logYAxis && majorTicks && Math.abs(yScale) > sc(MIN_X_GRIDSPACING)) {
// minor ticks for log within the decade
// more numbers on minor ticks when zoomed in
int minorNumberLimit = haveMinorLogNumbers ? (int) (0.4 * Math.abs(yScale) / fm.getHeight()) : 0;
i1 = (int) Math.floor(Math.min(yMin, yMax) - 1.e-10);
i2 = (int) Math.ceil(Math.max(yMin, yMax) + 1.e-10);
for (int i = i1; i <= i2; i++) {
for (int m = 2; m < 10; m++) {
double v = i + Math.log10(m);
if (v > Math.min(yMin, yMax) && v < Math.max(yMin, yMax)) {
int y = topMargin + frameHeight - (int) Math.round((v - yMin) * yScale);
ip.drawLine(x1, y, x1 + sc(minorTickLength), y);
ip.drawLine(x2, y, x2 - sc(minorTickLength), y);
if (m <= minorNumberLimit) {
int w = drawExpString(Math.pow(10, v), 0, xNumberRight, y, RIGHT, fontAscent, baseFont, scFontSmall, multiplySymbol);
if (w > maxNumWidth)
maxNumWidth = w;
}
}
}
}
}
}
}
// --- Write min&max of range if simple style without any axis format flags
ip.setFont(scFont);
ip.setJustification(LEFT);
String xLabelToDraw = pp.xLabel.label;
String yLabelToDraw = pp.yLabel.label;
if (simpleYAxis()) {
// y-axis min&max
int digits = getDigits(yMin, yMax, 0.001 * (yMax - yMin), 6, 0);
String s = IJ.d2s(yMax, digits);
int sw = ip.getStringWidth(s);
if ((sw + sc(4)) > leftMargin)
ip.drawString(s, sc(4), topMargin - sc(4));
else
ip.drawString(s, leftMargin - ip.getStringWidth(s) - sc(4), topMargin + 10);
s = IJ.d2s(yMin, digits);
sw = ip.getStringWidth(s);
if ((sw + 4) > leftMargin)
ip.drawString(s, sc(4), topMargin + frame.height);
else
ip.drawString(s, leftMargin - ip.getStringWidth(s) - sc(4), topMargin + frame.height);
if (logYAxis)
yLabelToDraw += " (LOG)";
}
int y = yOfXAxisNumbers;
if (simpleXAxis()) {
// x-axis min&max
int digits = getDigits(xMin, xMax, 0.001 * (xMax - xMin), 7, 0);
ip.drawString(IJ.d2s(xMin, digits), leftMargin, y);
String s = IJ.d2s(xMax, digits);
ip.drawString(s, leftMargin + frame.width - ip.getStringWidth(s) + 6, y);
y -= fm.getHeight();
if (logXAxis)
xLabelToDraw += " (LOG)";
} else
y += sc(1);
// --- Write x and y axis text labels
if (xCats == null) {
ip.setFont(pp.xLabel.getFont() == null ? scFont : scFont(pp.xLabel.getFont()));
ip.setFontSize(12);
ImageProcessor xLabel = stringToPixels(xLabelToDraw);
if (xLabel != null) {
int xpos = leftMargin + (frame.width - xLabel.getWidth()) / 2;
// topMargin + frame.height + bottomMargin-xLabel.getHeight();
int ypos = y + scFont.getSize() / 3;
ip.insert(xLabel, xpos, ypos);
}
}
if (yCats == null) {
ip.setFont(pp.yLabel.getFont() == null ? scFont : scFont(pp.yLabel.getFont()));
ImageProcessor yLabel = stringToPixels(yLabelToDraw);
ip.setFontSize(12);
if (yLabel != null) {
yLabel = yLabel.rotateLeft();
int xRightOfYLabel = xNumberRight - maxNumWidth - sc(2);
int xpos = xRightOfYLabel - yLabel.getWidth() - sc(2);
int ypos = topMargin + (frame.height - yLabel.getHeight()) / 2;
ip.insert(yLabel, xpos, ypos);
}
}
}
use of ij.astro.AstroImageJ in project astroimagej by keastrid.
the class ImagePlus method setSlice.
/**
* Displays the specified stack image, where 1<=n<=stackSize.
* Does nothing if this image is not a stack.
* @see #setPosition
* @see #setC
* @see #setZ
* @see #setT
*/
@AstroImageJ(reason = "Add reset if color type is RGB", modified = true)
public synchronized void setSlice(int n) {
if (getType() == ImagePlus.COLOR_RGB)
ip.reset();
if (stack == null || (n == currentSlice && ip != null)) {
if (!noUpdateMode)
updateAndRepaintWindow();
return;
}
if (n >= 1 && n <= stack.size()) {
Roi roi = getRoi();
if (roi != null)
roi.endPaste();
if (isProcessor()) {
if (currentSlice == 0)
currentSlice = 1;
stack.setPixels(ip.getPixels(), currentSlice);
}
setCurrentSlice(n);
Object pixels = null;
Overlay overlay2 = null;
if (stack.isVirtual() && !((stack instanceof FileInfoVirtualStack) || (stack instanceof AVI_Reader))) {
ImageProcessor ip2 = stack.getProcessor(currentSlice);
overlay2 = ip2 != null ? ip2.getOverlay() : null;
if (overlay2 != null)
setOverlay(overlay2);
if (stack instanceof VirtualStack) {
Properties props = ((VirtualStack) stack).getProperties();
if (props != null)
setProperty("FHT", props.get("FHT"));
}
if (ip2 != null)
pixels = ip2.getPixels();
} else
pixels = stack.getPixels(currentSlice);
if (ip != null && pixels != null) {
try {
ip.setPixels(pixels);
ip.setSnapshotPixels(null);
} catch (Exception e) {
}
} else {
ImageProcessor ip2 = stack.getProcessor(n);
if (ip2 != null)
ip = ip2;
}
if (compositeImage && getCompositeMode() == IJ.COMPOSITE && ip != null) {
int channel = getC();
if (channel > 0 && channel <= getNChannels())
ip.setLut(((CompositeImage) this).getChannelLut(channel));
}
if (win != null && win instanceof StackWindow)
((StackWindow) win).updateSliceSelector();
if (Prefs.autoContrast && nChannels == 1 && imageType != COLOR_RGB) {
(new ContrastEnhancer()).stretchHistogram(ip, 0.35, ip.getStats());
ContrastAdjuster.update();
// IJ.showStatus(n+": min="+ip.getMin()+", max="+ip.getMax());
}
if (imageType == COLOR_RGB)
ContrastAdjuster.update();
if (!noUpdateMode)
updateAndRepaintWindow();
else
img = null;
}
}
use of ij.astro.AstroImageJ in project astroimagej by keastrid.
the class PlotWindow method draw.
/**
* Displays the plot.
*/
@AstroImageJ(reason = "Disable 'More' plot option by setting it invisiblel List -> PDF; add copy button", modified = true)
public void draw() {
Panel bottomPanel = new Panel();
int hgap = IJ.isMacOSX() ? 1 : 5;
list = new Button(" PDF ");
list.addActionListener(this);
if (getTitle().startsWith("Seeing Profile")) {
copy.addActionListener(this);
bottomPanel.add(copy);
}
bottomPanel.add(list);
bottomPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, hgap, 0));
data = new Button(dataButtonLabel);
data.addActionListener(this);
bottomPanel.add(data);
more = new Button(moreButtonLabel);
more.setVisible(false);
more.addActionListener(this);
bottomPanel.add(more);
if (plot != null && plot.getPlotMaker() != null) {
live = new Button("Live");
live.addActionListener(this);
bottomPanel.add(live);
}
statusLabel = new Label();
statusLabel.setFont(new Font("Monospaced", Font.PLAIN, 12));
statusLabel.setBackground(new Color(220, 220, 220));
bottomPanel.add(statusLabel);
add(bottomPanel);
data.add(getDataPopupMenu());
more.add(getMorePopupMenu());
plot.draw();
LayoutManager lm = getLayout();
if (lm instanceof ImageLayout)
// don't expand size to make the panel fit
((ImageLayout) lm).ignoreNonImageWidths(true);
GUI.scale(bottomPanel);
maximizeCoordinatesLabelWidth();
pack();
ImageProcessor ip = plot.getProcessor();
boolean ipIsColor = ip instanceof ColorProcessor;
boolean impIsColor = imp.getProcessor() instanceof ColorProcessor;
if (ipIsColor != impIsColor)
imp.setProcessor(null, ip);
else
imp.updateAndDraw();
if (listValues)
showList(/*useLabels=*/
false);
else
// have focus on the canvas, not the button, so that pressing the space bar allows panning
ic.requestFocus();
}
use of ij.astro.AstroImageJ in project astroimagej by keastrid.
the class GenericDialog method setIcon.
@AstroImageJ(reason = "Use astronomy icon instead of default image")
void setIcon() {
URL url = this.getClass().getClassLoader().getResource("astronomy_icon.png");
if (url == null)
return;
Image img = null;
try {
img = createImage((ImageProducer) url.getContent());
} catch (IOException ignored) {
}
if (img != null)
setIconImage(img);
}
use of ij.astro.AstroImageJ in project astroimagej by keastrid.
the class ImageWindow method setImageJMenuBar.
@AstroImageJ(modified = true, reason = "Don't run for AstroStackWindow")
public static void setImageJMenuBar(ImageWindow win) {
if (win.getClass().getName().contains("AstroStackWindow"))
return;
ImageJ ij = IJ.getInstance();
boolean setMenuBar = true;
ImagePlus imp = win.getImagePlus();
if (imp != null)
setMenuBar = imp.setIJMenuBar();
MenuBar mb = Menus.getMenuBar();
if (mb != null && mb == win.getMenuBar())
setMenuBar = false;
setMenuBarTime = 0L;
if (setMenuBar && ij != null && !ij.quitting() && !Interpreter.nonBatchMacroRunning()) {
// may be needed for Java 1.4 on OS X
IJ.wait(10);
long t0 = System.currentTimeMillis();
win.setMenuBar(mb);
long time = System.currentTimeMillis() - t0;
setMenuBarTime = time;
Menus.setMenuBarCount++;
if (IJ.debugMode)
IJ.log("setMenuBar: " + time + "ms (" + Menus.setMenuBarCount + ")");
if (time > 2000L)
Prefs.setIJMenuBar = false;
}
if (imp != null)
imp.setIJMenuBar(true);
}
Aggregations