From 37dd61b89edd61b2d9033d47e5969aba4c5bb493 Mon Sep 17 00:00:00 2001 From: abrewe Date: Wed, 15 Jun 2022 16:57:45 -0500 Subject: [PATCH 1/8] log gui functionality --- .../EPICS_areaDetector/EPICS_AD_Viewer.java | 193 ++++++++++----- .../EPICS_NTNDA_Viewer.java | 227 +++++++++++------- 2 files changed, 275 insertions(+), 145 deletions(-) diff --git a/ImageJ/EPICS_areaDetector/EPICS_AD_Viewer.java b/ImageJ/EPICS_areaDetector/EPICS_AD_Viewer.java index 393f310..8472986 100755 --- a/ImageJ/EPICS_areaDetector/EPICS_AD_Viewer.java +++ b/ImageJ/EPICS_areaDetector/EPICS_AD_Viewer.java @@ -24,7 +24,9 @@ public class EPICS_AD_Viewer implements PlugIn { ImagePlus img; ImageStack imageStack; - + //ADDITION + Object snapBackup = null; + //END ADDITION int imageSizeX = 0; int imageSizeY = 0; int imageSizeZ = 0; @@ -78,6 +80,10 @@ public class EPICS_AD_Viewer implements PlugIn boolean isSaveToStack; boolean isNewStack; boolean isConnected; + //ADDITION + boolean isLogOn; + boolean startedLog; + //END ADDITION volatile boolean isNewImageAvailable; javax.swing.Timer timer; @@ -94,6 +100,10 @@ public void run(String arg) isNewImageAvailable = false; isSaveToStack = false; isNewStack = false; + //ADDITION + isLogOn = false; + startedLog = false; + //END ADDITION Date date = new Date(); prevTime = date.getTime(); numImageUpdates = 0; @@ -103,18 +113,18 @@ public void run(String arg) if (isDebugFile) { debugFile = new FileOutputStream(System.getProperty("user.home") + - System.getProperty("file.separator") + "IJEPICS_debug.txt"); + System.getProperty("file.separator") + "IJEPICS_debug.txt"); debugPrintStream = new PrintStream(debugFile); } javax.swing.SwingUtilities.invokeLater( - new Runnable() - { - public void run() - { - createAndShowGUI(); - } - } - ); + new Runnable() + { + public void run() + { + createAndShowGUI(); + } + } + ); img = new ImagePlus(PVPrefix, new ByteProcessor(100, 100)); img.show(); @@ -210,9 +220,9 @@ public void connectPVs() ch_image = createEPICSChannel(PVPrefix + "ArrayData"); ch_image_id = createEPICSChannel(PVPrefix + "ArrayCounter_RBV"); ch_image_id.addMonitor( - Monitor.VALUE, - new newArrayCounterCallback() - ); + Monitor.VALUE, + new newArrayCounterCallback() + ); ctxt.flushIO(); checkConnections(); } @@ -313,12 +323,12 @@ public void checkConnections() try { connected = (ch_nx != null && ch_nx.getConnectionState() == Channel.ConnectionState.CONNECTED && - ch_ny != null && ch_ny.getConnectionState() == Channel.ConnectionState.CONNECTED && - ch_nz != null && ch_nz.getConnectionState() == Channel.ConnectionState.CONNECTED && - ch_colorMode != null && ch_colorMode.getConnectionState() == Channel.ConnectionState.CONNECTED && - ch_dataType != null && ch_dataType.getConnectionState() == Channel.ConnectionState.CONNECTED && - ch_image != null && ch_image.getConnectionState() == Channel.ConnectionState.CONNECTED && - ch_image_id != null && ch_image_id.getConnectionState() == Channel.ConnectionState.CONNECTED); + ch_ny != null && ch_ny.getConnectionState() == Channel.ConnectionState.CONNECTED && + ch_nz != null && ch_nz.getConnectionState() == Channel.ConnectionState.CONNECTED && + ch_colorMode != null && ch_colorMode.getConnectionState() == Channel.ConnectionState.CONNECTED && + ch_dataType != null && ch_dataType.getConnectionState() == Channel.ConnectionState.CONNECTED && + ch_image != null && ch_image.getConnectionState() == Channel.ConnectionState.CONNECTED && + ch_image_id != null && ch_image_id.getConnectionState() == Channel.ConnectionState.CONNECTED); if (connected && !isConnected) { isConnected = true; @@ -402,7 +412,7 @@ public void updateImage() } } catch (Exception ex) { - IJ.log("updateImage got exception: " + ex.getMessage()); + IJ.log("updateImage got exception: " + ex.getMessage()); } makeNewWindow = false; } @@ -439,6 +449,9 @@ else if (ADDataType == 3) img.show(); if (oldWindowLocation != null) img.getWindow().setLocation(oldWindowLocation); madeNewWindow = true; + //ADDITION + startedLog = false; + //END ADDITION } if (isNewStack) @@ -487,11 +500,11 @@ else if (colorMode >= 2 && colorMode <= 4) prevDispMax = dispMax; double slope = 255/(dispMax - dispMin); for (i=0; i<256; i++) { - if (idispMax) colorLUT[i] = (byte)255; - else + else colorLUT[i] = (byte)((i-dispMin)*slope + 0.5); } } @@ -502,44 +515,54 @@ else if (i>dispMax) switch (colorMode) { case 2: + { + int in = 0, out = 0; + while (in < getsize) { - int in = 0, out = 0; - while (in < getsize) - { - pixels[out++] = (inpixels[in++] & 0xFF) << 16 | (inpixels[in++] & 0xFF) << 8 | (inpixels[in++] & 0xFF); - } + pixels[out++] = (inpixels[in++] & 0xFF) << 16 | (inpixels[in++] & 0xFF) << 8 | (inpixels[in++] & 0xFF); } - break; + } + break; case 3: + { + int nCols = imageSizeX, nRows = imageSizeZ, row, col; + int redIn, greenIn, blueIn, out = 0; + for (row = 0; row < nRows; row++) { - int nCols = imageSizeX, nRows = imageSizeZ, row, col; - int redIn, greenIn, blueIn, out = 0; - for (row = 0; row < nRows; row++) + redIn = row * nCols * 3; + greenIn = redIn + nCols; + blueIn = greenIn + nCols; + for (col = 0; col < nCols; col++) { - redIn = row * nCols * 3; - greenIn = redIn + nCols; - blueIn = greenIn + nCols; - for (col = 0; col < nCols; col++) - { - pixels[out++] = (inpixels[redIn++] & 0xFF) << 16 | (inpixels[greenIn++] & 0xFF) << 8 | (inpixels[blueIn++] & 0xFF); - } + pixels[out++] = (inpixels[redIn++] & 0xFF) << 16 | (inpixels[greenIn++] & 0xFF) << 8 | (inpixels[blueIn++] & 0xFF); } } - break; + } + break; case 4: + { + int imageSize = imageSizeX * imageSizeY; + int redIn = 0, greenIn = imageSize, blueIn = 2 * imageSize, out = 0; + while (redIn < imageSize) { - int imageSize = imageSizeX * imageSizeY; - int redIn = 0, greenIn = imageSize, blueIn = 2 * imageSize, out = 0; - while (redIn < imageSize) - { - pixels[out++] = (inpixels[redIn++] & 0xFF) << 16 | (inpixels[greenIn++] & 0xFF) << 8 | (inpixels[blueIn++] & 0xFF); - } + pixels[out++] = (inpixels[redIn++] & 0xFF) << 16 | (inpixels[greenIn++] & 0xFF) << 8 | (inpixels[blueIn++] & 0xFF); } - break; + } + break; } img.getProcessor().setPixels(pixels); } + //ADDITION + if (isLogOn && startedLog) + { + img.getProcessor().snapshot(); + snapBackup = img.getProcessor().getSnapshotPixels(); + img.getProcessor().resetMinAndMax(); + img.getProcessor().log(); + } + //ADDITION + if (isSaveToStack) { img.getStack().addSlice(PVPrefix + ArrayCounter, img.getProcessor().duplicate()); @@ -553,6 +576,9 @@ else if (i>dispMax) img.mouseMoved(loc.x,loc.y); img.updateStatusbarValue(); numImageUpdates++; + //ADDITION + if (isLogOn) startedLog = true; + //END ADDITION // Automatically set brightness and contrast if we made a new window if (madeNewWindow) new ContrastEnhancer().stretchHistogram(img, 0.5); } @@ -640,6 +666,9 @@ public void createAndShowGUI() stopButton.setEnabled(false); snapButton = new JButton("Snap"); JCheckBox captureCheckBox = new JCheckBox(""); + //ADDITION + JCheckBox logCheckBox = new JCheckBox(""); + //END ADDITION frame = new JFrame("Image J EPICS_AD_Viewer Plugin"); JPanel panel = new JPanel(new BorderLayout()); @@ -666,6 +695,10 @@ public void createAndShowGUI() panel.add(new JLabel("Frames/s"), c); c.gridx = 5; panel.add(new JLabel("Capture to Stack"), c); + //ADDITION + c.gridx = 9; + panel.add(new JLabel("Log"), c); + //END ADDITION // Middle row // These widgets should be centered @@ -689,6 +722,10 @@ public void createAndShowGUI() panel.add(startButton, c); c.gridx = 8; panel.add(stopButton, c); + //ADDITION + c.gridx = 9; + panel.add(logCheckBox, c); + //END ADDITION // Bottom row c.gridy = 2; @@ -756,6 +793,36 @@ public void actionPerformed(ActionEvent event) logMessage("Image display stopped", true, true); } }); + //ADDITION + logCheckBox.addItemListener(new ItemListener() { + public void itemStateChanged(ItemEvent e) { + if(e.getStateChange() == ItemEvent.SELECTED) + { + isLogOn = true; + logMessage("Log display on", true, true); + if(!stopButton.isEnabled()){ + img.getProcessor().snapshot(); + snapBackup = img.getProcessor().getSnapshotPixels(); + img.getProcessor().resetMinAndMax(); + img.getProcessor().log(); + img.updateAndDraw(); + } + startedLog = true; + } + else + { + logMessage("Log display off", true, true); + isLogOn = false; + if(!stopButton.isEnabled()){ + WindowManager.setCurrentWindow(img.getWindow()); + img.getProcessor().setSnapshotPixels(snapBackup); + Undo.undo(); + } + startedLog = false; + } + } + }); + //END ADDITION snapButton.addActionListener(new ActionListener() { @@ -766,23 +833,23 @@ public void actionPerformed(ActionEvent event) }); captureCheckBox.addItemListener(new ItemListener() - { - public void itemStateChanged(ItemEvent e) - { - if (e.getStateChange() == ItemEvent.SELECTED) - { - isSaveToStack = true; - isNewStack = true; - IJ.log("record on"); - } - else - { - isSaveToStack = false; - IJ.log("record off"); - } - - } - } + { + public void itemStateChanged(ItemEvent e) + { + if (e.getStateChange() == ItemEvent.SELECTED) + { + isSaveToStack = true; + isNewStack = true; + IJ.log("record on"); + } + else + { + isSaveToStack = false; + IJ.log("record off"); + } + + } + } ); } diff --git a/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java b/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java index 4b3e935..75dc2b3 100755 --- a/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java +++ b/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java @@ -4,12 +4,7 @@ // Tim Madden, APS // Mark Rivers, University of Chicago // Marty Kraimer -import java.awt.BorderLayout; -import java.awt.Color; -import java.awt.GridBagConstraints; -import java.awt.GridBagLayout; -import java.awt.Insets; -import java.awt.Point; +import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; @@ -63,28 +58,33 @@ import ij.process.FloatProcessor; import ij.process.ImageProcessor; import ij.process.ShortProcessor; +import ij.Undo; +import ij.WindowManager; /** * ImageJ viewer for NTNDArray data. * */ -public class EPICS_NTNDA_Viewer - implements PvaClientChannelStateChangeRequester,PlugIn +public class EPICS_NTNDA_Viewer + implements PvaClientChannelStateChangeRequester,PlugIn { // may want to change these private String channelName = "13SIM1:Pva1:Image"; private boolean isDebugMessages = false; private boolean isDebugFile = false; private String propertyFile = "EPICS_NTNDA_Viewer.properties"; - + private static final int QUEUE_SIZE = 1; private static final int MS_WAIT = 100; private static PvaClient pva=PvaClient.get("pva"); private static Convert convert = ConvertFactory.getConvert(); private PvaClientChannel pvaClientChannel = null; private PvaClientMonitor pvaClientMonitor = null; - + private ImagePlus img = null; + //ADDITION + private Object snapBackup = null; + //END ADDITION private ImageStack imageStack = null; private int imageSizeX = 0; private int imageSizeY = 0; @@ -104,13 +104,17 @@ public class EPICS_NTNDA_Viewer private volatile boolean isPluginRunning = false; private volatile boolean isSaveToStack = false; private volatile boolean isNewStack = false; - + //ADDITION + private volatile boolean isLogOn = false; + private volatile boolean startedLog = false; + //END ADDITION + // These are used for the frames/second calculation private long prevTime = 0; private volatile int numImageUpdates = 0; - + private NTNDCodec ntndCodec = null; - + private JFrame frame = null; private JTextField channelNameText = null; private JTextField nxText = null; @@ -155,7 +159,7 @@ public void channelStateChange(PvaClientChannel channel, boolean connected) { numImageUpdates = 0; } } - + private void connectPV() { try @@ -176,7 +180,7 @@ private void connectPV() logMessage("Could not connect to : " + channelName + " " + ex.getMessage(), true, false); } } - + private void disconnectPV() { try @@ -197,13 +201,13 @@ private void disconnectPV() logMessage("Cannot disconnect from EPICS PV:" + channelName + ex.getMessage(), true, true); } } - + private void startMonitor() { isStarted = true; pvaClientMonitor.start(); } - + private void stopMonitor() { synchronized(this) { @@ -211,7 +215,7 @@ private void stopMonitor() isStarted = false; } } - + private void startDisplay() { startButton.setEnabled(false); @@ -221,7 +225,7 @@ private void startDisplay() if (isChannelConnected) startMonitor(); logMessage("Display started", true, false); } - + private void stopDisplay() { startButton.setEnabled(true); @@ -230,8 +234,8 @@ private void stopDisplay() startIsTrue = false; if (isChannelConnected) stopMonitor(); logMessage("Display stopped", true, false); - } - + } + private void handleEvents() { boolean gotEvent = pvaClientMonitor.poll(); @@ -324,7 +328,7 @@ public void run(String arg) catch (Exception ee) { } } } - + private void makeImageCopy() { ImageProcessor ip = img.getProcessor(); @@ -468,7 +472,7 @@ private boolean updateImage(PvaClientMonitorData monitorData) img.close(); } } - catch (Exception ex) { + catch (Exception ex) { logMessage("Exception closing window " + ex.getMessage(), true, true); } makeNewWindow = false; @@ -478,36 +482,39 @@ private boolean updateImage(PvaClientMonitorData monitorData) { switch (colorMode) { - case 0: - case 1: - if(dataType==ScalarType.pvUByte) - { - img = new ImagePlus(channelName, new ByteProcessor(imageSizeX, imageSizeY)); - } - else if(dataType==ScalarType.pvUShort) - { - img = new ImagePlus(channelName, new ShortProcessor(imageSizeX, imageSizeY)); - } - else if (dataType.isNumeric()) - { - img = new ImagePlus(channelName, new FloatProcessor(imageSizeX, imageSizeY)); - } else { - throw new RuntimeException("illegal array type " + dataType); - } - break; - case 2: - img = new ImagePlus(channelName, new ColorProcessor(imageSizeY, imageSizeZ)); - break; - case 3: - img = new ImagePlus(channelName, new ColorProcessor(imageSizeX, imageSizeZ)); - break; - case 4: - img = new ImagePlus(channelName, new ColorProcessor(imageSizeX, imageSizeY)); - break; + case 0: + case 1: + if(dataType==ScalarType.pvUByte) + { + img = new ImagePlus(channelName, new ByteProcessor(imageSizeX, imageSizeY)); + } + else if(dataType==ScalarType.pvUShort) + { + img = new ImagePlus(channelName, new ShortProcessor(imageSizeX, imageSizeY)); + } + else if (dataType.isNumeric()) + { + img = new ImagePlus(channelName, new FloatProcessor(imageSizeX, imageSizeY)); + } else { + throw new RuntimeException("illegal array type " + dataType); + } + break; + case 2: + img = new ImagePlus(channelName, new ColorProcessor(imageSizeY, imageSizeZ)); + break; + case 3: + img = new ImagePlus(channelName, new ColorProcessor(imageSizeX, imageSizeZ)); + break; + case 4: + img = new ImagePlus(channelName, new ColorProcessor(imageSizeX, imageSizeY)); + break; } img.show(); if (oldWindowLocation != null) img.getWindow().setLocation(oldWindowLocation); madeNewWindow = true; + //ADDITION + startedLog = false; + //END ADDITION } if (isNewStack) @@ -565,11 +572,11 @@ else if (colorMode >= 2 && colorMode <= 4) prevDispMax = dispMax; double slope = 255/(dispMax - dispMin); for (i=0; i<256; i++) { - if (idispMax) colorLUT[i] = (byte)255; - else + else colorLUT[i] = (byte)((i-dispMin)*slope + 0.5); } } @@ -580,45 +587,54 @@ else if (i>dispMax) switch (colorMode) { - case 2: - { - int in = 0, out = 0; - while (in < numElements) + case 2: { - pixels[out++] = (inpixels[in++] & 0xFF) << 16 | (inpixels[in++] & 0xFF) << 8 | (inpixels[in++] & 0xFF); + int in = 0, out = 0; + while (in < numElements) + { + pixels[out++] = (inpixels[in++] & 0xFF) << 16 | (inpixels[in++] & 0xFF) << 8 | (inpixels[in++] & 0xFF); + } } - } - break; - case 3: - { - int nCols = imageSizeX, nRows = imageSizeZ, row, col; - int redIn, greenIn, blueIn, out = 0; - for (row = 0; row < nRows; row++) + break; + case 3: { - redIn = row * nCols * 3; - greenIn = redIn + nCols; - blueIn = greenIn + nCols; - for (col = 0; col < nCols; col++) + int nCols = imageSizeX, nRows = imageSizeZ, row, col; + int redIn, greenIn, blueIn, out = 0; + for (row = 0; row < nRows; row++) { - pixels[out++] = (inpixels[redIn++] & 0xFF) << 16 | (inpixels[greenIn++] & 0xFF) << 8 | (inpixels[blueIn++] & 0xFF); + redIn = row * nCols * 3; + greenIn = redIn + nCols; + blueIn = greenIn + nCols; + for (col = 0; col < nCols; col++) + { + pixels[out++] = (inpixels[redIn++] & 0xFF) << 16 | (inpixels[greenIn++] & 0xFF) << 8 | (inpixels[blueIn++] & 0xFF); + } } } - } - break; - case 4: - { - int imageSize = imageSizeX * imageSizeY; - int redIn = 0, greenIn = imageSize, blueIn = 2 * imageSize, out = 0; - while (redIn < imageSize) + break; + case 4: { - pixels[out++] = (inpixels[redIn++] & 0xFF) << 16 | (inpixels[greenIn++] & 0xFF) << 8 | (inpixels[blueIn++] & 0xFF); + int imageSize = imageSizeX * imageSizeY; + int redIn = 0, greenIn = imageSize, blueIn = 2 * imageSize, out = 0; + while (redIn < imageSize) + { + pixels[out++] = (inpixels[redIn++] & 0xFF) << 16 | (inpixels[greenIn++] & 0xFF) << 8 | (inpixels[blueIn++] & 0xFF); + } } - } - break; + break; } img.getProcessor().setPixels(pixels); + } + //ADDITION + if (isLogOn && startedLog) + { + img.getProcessor().snapshot(); + snapBackup = img.getProcessor().getSnapshotPixels(); + img.getProcessor().resetMinAndMax(); + img.getProcessor().log(); } + //END ADDITION if (isSaveToStack) { @@ -633,6 +649,9 @@ else if (i>dispMax) img.mouseMoved(loc.x,loc.y); img.updateStatusbarValue(); numImageUpdates++; + //ADDITION + if (isLogOn) startedLog = true; + //END ADDITION // Automatically set brightness and contrast if we made a new window if (madeNewWindow) new ContrastEnhancer().stretchHistogram(img, 0.5); return true; @@ -666,6 +685,9 @@ private void createAndShowGUI() stopButton = new JButton("Stop"); snapButton = new JButton("Snap"); JCheckBox captureCheckBox = new JCheckBox(""); + //ADDITION + JCheckBox logCheckBox = new JCheckBox(""); + //END ADDITION frame = new JFrame("Image J EPICS_NTNDA_Viewer Plugin"); JPanel panel = new JPanel(new BorderLayout()); @@ -692,6 +714,10 @@ private void createAndShowGUI() panel.add(new JLabel("Frames/s"), c); c.gridx = 7; panel.add(new JLabel("Capture to Stack"), c); + //ADDITION + c.gridx = 8; + panel.add(new JLabel("Log"), c); + //END ADDITION // Middle row // These widgets should be centered @@ -714,8 +740,12 @@ private void createAndShowGUI() c.gridx = 7; panel.add(captureCheckBox, c); c.gridx = 8; + //ADDITION + panel.add(logCheckBox, c); + c.gridx = 9; + //END ADDITION panel.add(snapButton, c); - + // Bottom row c.gridy = 2; c.gridx = 0; @@ -730,7 +760,7 @@ private void createAndShowGUI() frame.pack(); frame.addWindowListener(new FrameExitListener()); frame.setVisible(true); - + int timerDelay = 2000; // 2 seconds timer = new javax.swing.Timer(timerDelay, new ActionListener() { @@ -742,7 +772,7 @@ public void actionPerformed(ActionEvent event) NumberFormat form = DecimalFormat.getInstance(); ((DecimalFormat)form).applyPattern("0.0"); fpsText.setText("" + form.format(fps)); - if (isPluginRunning && isStarted && numImageUpdates > 0) + if (isPluginRunning && isStarted && numImageUpdates > 0) logMessage(String.format("Received %d images in %.2f sec", numImageUpdates, elapsedTime), true, false); prevTime = time; numImageUpdates = 0; @@ -775,6 +805,39 @@ public void actionPerformed(ActionEvent event) } }); + //ADDITION + logCheckBox.addItemListener(new ItemListener() + { + public void itemStateChanged(ItemEvent e) + { + if(e.getStateChange() == ItemEvent.SELECTED) + { + isLogOn = true; + logMessage("Log display on", true, true); + if(!stopButton.isEnabled()){ + img.getProcessor().snapshot(); + snapBackup = img.getProcessor().getSnapshotPixels(); + img.getProcessor().resetMinAndMax(); + img.getProcessor().log(); + img.updateAndDraw(); + } + startedLog = true; + } + else + { + logMessage("Log display off", true, true); + isLogOn = false; + if(!stopButton.isEnabled()){ + WindowManager.setCurrentWindow(img.getWindow()); + img.getProcessor().setSnapshotPixels(snapBackup); + Undo.undo(); + } + startedLog = false; + } + } + }); + //END ADDITION + snapButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) @@ -865,4 +928,4 @@ private void writeProperties() logMessage("writeProperties:exception: " + ex.getMessage(), true, true); } } - } +} From 4fbfb699a7358135b6e854624309e25f3d6ccba1 Mon Sep 17 00:00:00 2001 From: abrewe Date: Thu, 30 Jun 2022 11:25:28 -0500 Subject: [PATCH 2/8] format, 32bit behavior --- .../EPICS_areaDetector/EPICS_AD_Viewer.java | 103 ++-- .../EPICS_NTNDA_Viewer.java | 505 ++++++++---------- 2 files changed, 276 insertions(+), 332 deletions(-) diff --git a/ImageJ/EPICS_areaDetector/EPICS_AD_Viewer.java b/ImageJ/EPICS_areaDetector/EPICS_AD_Viewer.java index 8472986..a000a21 100755 --- a/ImageJ/EPICS_areaDetector/EPICS_AD_Viewer.java +++ b/ImageJ/EPICS_areaDetector/EPICS_AD_Viewer.java @@ -24,9 +24,8 @@ public class EPICS_AD_Viewer implements PlugIn { ImagePlus img; ImageStack imageStack; - //ADDITION Object snapBackup = null; - //END ADDITION + Object altSnapBackup = null; int imageSizeX = 0; int imageSizeY = 0; int imageSizeZ = 0; @@ -80,10 +79,8 @@ public class EPICS_AD_Viewer implements PlugIn boolean isSaveToStack; boolean isNewStack; boolean isConnected; - //ADDITION boolean isLogOn; - boolean startedLog; - //END ADDITION + // boolean startedLog; volatile boolean isNewImageAvailable; javax.swing.Timer timer; @@ -100,10 +97,7 @@ public void run(String arg) isNewImageAvailable = false; isSaveToStack = false; isNewStack = false; - //ADDITION isLogOn = false; - startedLog = false; - //END ADDITION Date date = new Date(); prevTime = date.getTime(); numImageUpdates = 0; @@ -449,9 +443,6 @@ else if (ADDataType == 3) img.show(); if (oldWindowLocation != null) img.getWindow().setLocation(oldWindowLocation); madeNewWindow = true; - //ADDITION - startedLog = false; - //END ADDITION } if (isNewStack) @@ -552,17 +543,11 @@ else if (i>dispMax) } img.getProcessor().setPixels(pixels); } - - //ADDITION - if (isLogOn && startedLog) + if (isLogOn) { - img.getProcessor().snapshot(); - snapBackup = img.getProcessor().getSnapshotPixels(); - img.getProcessor().resetMinAndMax(); - img.getProcessor().log(); + snapBackup = takeLog(img); + if(dataType!= ScalarType.pvUShort && dataType!=ScalarType.pvUByte) resetContrast(img); } - //ADDITION - if (isSaveToStack) { img.getStack().addSlice(PVPrefix + ArrayCounter, img.getProcessor().duplicate()); @@ -576,9 +561,6 @@ else if (i>dispMax) img.mouseMoved(loc.x,loc.y); img.updateStatusbarValue(); numImageUpdates++; - //ADDITION - if (isLogOn) startedLog = true; - //END ADDITION // Automatically set brightness and contrast if we made a new window if (madeNewWindow) new ContrastEnhancer().stretchHistogram(img, 0.5); } @@ -666,9 +648,7 @@ public void createAndShowGUI() stopButton.setEnabled(false); snapButton = new JButton("Snap"); JCheckBox captureCheckBox = new JCheckBox(""); - //ADDITION JCheckBox logCheckBox = new JCheckBox(""); - //END ADDITION frame = new JFrame("Image J EPICS_AD_Viewer Plugin"); JPanel panel = new JPanel(new BorderLayout()); @@ -695,10 +675,8 @@ public void createAndShowGUI() panel.add(new JLabel("Frames/s"), c); c.gridx = 5; panel.add(new JLabel("Capture to Stack"), c); - //ADDITION c.gridx = 9; panel.add(new JLabel("Log"), c); - //END ADDITION // Middle row // These widgets should be centered @@ -722,10 +700,8 @@ public void createAndShowGUI() panel.add(startButton, c); c.gridx = 8; panel.add(stopButton, c); - //ADDITION c.gridx = 9; panel.add(logCheckBox, c); - //END ADDITION // Bottom row c.gridy = 2; @@ -793,36 +769,57 @@ public void actionPerformed(ActionEvent event) logMessage("Image display stopped", true, true); } }); - //ADDITION logCheckBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { - if(e.getStateChange() == ItemEvent.SELECTED) - { + if (e.getStateChange() == ItemEvent.SELECTED) { isLogOn = true; logMessage("Log display on", true, true); - if(!stopButton.isEnabled()){ - img.getProcessor().snapshot(); - snapBackup = img.getProcessor().getSnapshotPixels(); - img.getProcessor().resetMinAndMax(); - img.getProcessor().log(); - img.updateAndDraw(); + if (!stopButton.isEnabled()) { + if (WindowManager.getCurrentWindow().equals(img.getWindow())) { + snapBackup = takeLog(img); + img.updateAndDraw(); + resetContrast(img); + } + else{ + ImagePlus imgC = WindowManager.getCurrentImage(); + altSnapBackup = takeLog(imgC); + imgC.updateAndDraw(); + resetContrast(imgC); + } } - startedLog = true; - } - else - { + else if (!WindowManager.getCurrentWindow().equals(img.getWindow())){ + ImagePlus imgC = WindowManager.getCurrentImage(); + altSnapBackup = takeLog(imgC); + imgC.updateAndDraw(); + resetContrast(imgC); + isLogOn = false; + } + } else { logMessage("Log display off", true, true); isLogOn = false; - if(!stopButton.isEnabled()){ - WindowManager.setCurrentWindow(img.getWindow()); - img.getProcessor().setSnapshotPixels(snapBackup); + if (!stopButton.isEnabled()) { + if (WindowManager.getCurrentWindow().equals(img.getWindow())) { + WindowManager.setCurrentWindow(img.getWindow()); + img.getProcessor().setSnapshotPixels(snapBackup); + Undo.undo(); + resetContrast(img); + } + else{ + ImagePlus imgC = WindowManager.getCurrentImage(); + imgC.getProcessor().setSnapshotPixels(altSnapBackup); + Undo.undo(); + resetContrast(imgC); + } + } + else if (!WindowManager.getCurrentWindow().equals(img.getWindow())){ + ImagePlus imgC = WindowManager.getCurrentImage(); + imgC.getProcessor().setSnapshotPixels(altSnapBackup); Undo.undo(); + resetContrast(imgC); } - startedLog = false; } } }); - //END ADDITION snapButton.addActionListener(new ActionListener() { @@ -853,7 +850,17 @@ public void itemStateChanged(ItemEvent e) ); } - + private Object takeLog(ImagePlus image){ + image.getProcessor().snapshot(); + image.getProcessor().resetMinAndMax(); + image.getProcessor().log(); + return image.getProcessor().getSnapshotPixels(); + } + private void resetContrast(ImagePlus image){ + image.getProcessor().resetMinAndMax(); + if (image.getProcessor().getMin()<0) image.getProcessor().setMinAndMax(0, image.getProcessor().getMax()); + new ContrastEnhancer().stretchHistogram(img, 0.5); + } public class FrameExitListener extends WindowAdapter { public void windowClosing(WindowEvent event) diff --git a/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java b/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java index 75dc2b3..9956a48 100755 --- a/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java +++ b/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java @@ -11,6 +11,7 @@ import java.awt.event.ItemListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; +import java.awt.geom.Arc2D; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.PrintStream; @@ -28,6 +29,8 @@ import javax.swing.JTextField; import javax.swing.border.EmptyBorder; +import ij.gui.PlotWindow; +import ij.plugin.frame.ContrastAdjuster; import org.epics.nt.NTNDArray; import org.epics.pvaClient.PvaClient; import org.epics.pvaClient.PvaClientChannel; @@ -66,8 +69,7 @@ * */ public class EPICS_NTNDA_Viewer - implements PvaClientChannelStateChangeRequester,PlugIn -{ + implements PvaClientChannelStateChangeRequester,PlugIn { // may want to change these private String channelName = "13SIM1:Pva1:Image"; private boolean isDebugMessages = false; @@ -76,15 +78,14 @@ public class EPICS_NTNDA_Viewer private static final int QUEUE_SIZE = 1; private static final int MS_WAIT = 100; - private static PvaClient pva=PvaClient.get("pva"); + private static PvaClient pva = PvaClient.get("pva"); private static Convert convert = ConvertFactory.getConvert(); private PvaClientChannel pvaClientChannel = null; private PvaClientMonitor pvaClientMonitor = null; private ImagePlus img = null; - //ADDITION private Object snapBackup = null; - //END ADDITION + private Object altSnapBackup = null; private ImageStack imageStack = null; private int imageSizeX = 0; private int imageSizeY = 0; @@ -104,11 +105,7 @@ public class EPICS_NTNDA_Viewer private volatile boolean isPluginRunning = false; private volatile boolean isSaveToStack = false; private volatile boolean isNewStack = false; - //ADDITION private volatile boolean isLogOn = false; - private volatile boolean startedLog = false; - //END ADDITION - // These are used for the frames/second calculation private long prevTime = 0; private volatile int numImageUpdates = 0; @@ -127,12 +124,12 @@ public class EPICS_NTNDA_Viewer private JButton snapButton = null; private javax.swing.Timer timer = null; + /** * Constructor */ - public EPICS_NTNDA_Viewer() - { - String temp=null; + public EPICS_NTNDA_Viewer() { + String temp = null; readProperties(); temp = System.getenv("EPICS_NTNDA_VIEWER_CHANNELNAME"); if (temp != null) { @@ -140,84 +137,74 @@ public EPICS_NTNDA_Viewer() } createAndShowGUI(); } + /* (non-Javadoc) * @see org.epics.pvaClient.PvaClientChannelStateChangeRequester#channelStateChange(org.epics.pvaClient.PvaClientChannel, boolean) */ public void channelStateChange(PvaClientChannel channel, boolean connected) { isChannelConnected = connected; - if(isChannelConnected) { + if (isChannelConnected) { channelNameText.setBackground(Color.green); logMessage("State changed to connected for " + channelName, true, true); - if(pvaClientMonitor==null) { - pvaClientMonitor=pvaClientChannel.createMonitor("record[queueSize="+QUEUE_SIZE+"]field()"); + if (pvaClientMonitor == null) { + pvaClientMonitor = pvaClientChannel.createMonitor("record[queueSize=" + QUEUE_SIZE + "]field()"); pvaClientMonitor.issueConnect(); } if (startIsTrue) startMonitor(); - } else if(pvaClientMonitor!=null) { + } else if (pvaClientMonitor != null) { channelNameText.setBackground(Color.red); logMessage("State changed to disconnected for " + channelName, true, true); numImageUpdates = 0; } } - private void connectPV() - { - try - { - if(pvaClientChannel!=null) { + private void connectPV() { + try { + if (pvaClientChannel != null) { throw new RuntimeException("Channel already connected"); } channelName = channelNameText.getText(); logMessage("Trying to connect to : " + channelName, true, true); - pvaClientChannel = pva.createChannel(channelName,"pva"); + pvaClientChannel = pva.createChannel(channelName, "pva"); pvaClientChannel.setStateChangeRequester(this); isChannelConnected = false; channelNameText.setBackground(Color.red); pvaClientChannel.issueConnect(); - } - catch (Exception ex) - { + } catch (Exception ex) { logMessage("Could not connect to : " + channelName + " " + ex.getMessage(), true, false); } } - private void disconnectPV() - { - try - { - if(pvaClientChannel==null) { + private void disconnectPV() { + try { + if (pvaClientChannel == null) { throw new RuntimeException("Channel already disconnected"); } isChannelConnected = false; - if(isStarted) stopMonitor(); + if (isStarted) stopMonitor(); pvaClientChannel.destroy(); - if(pvaClientMonitor!=null) pvaClientMonitor.destroy(); + if (pvaClientMonitor != null) pvaClientMonitor.destroy(); pvaClientChannel = null; pvaClientMonitor = null; logMessage("Disconnected from EPICS PV:" + channelName, true, true); - } - catch (Exception ex) - { + } catch (Exception ex) { logMessage("Cannot disconnect from EPICS PV:" + channelName + ex.getMessage(), true, true); } } - private void startMonitor() - { + private void startMonitor() { isStarted = true; pvaClientMonitor.start(); } - private void stopMonitor() - { - synchronized(this) { + private void stopMonitor() { + synchronized (this) { pvaClientMonitor.stop(); isStarted = false; } } - private void startDisplay() - { + private void startDisplay() { startButton.setEnabled(false); stopButton.setEnabled(true); snapButton.setEnabled(true); @@ -226,8 +213,7 @@ private void startDisplay() logMessage("Display started", true, false); } - private void stopDisplay() - { + private void stopDisplay() { startButton.setEnabled(true); stopButton.setEnabled(false); snapButton.setEnabled(false); @@ -236,27 +222,26 @@ private void stopDisplay() logMessage("Display stopped", true, false); } - private void handleEvents() - { + private void handleEvents() { boolean gotEvent = pvaClientMonitor.poll(); - if(!gotEvent) { + if (!gotEvent) { try { Thread.sleep(1); - } catch(Exception ex) { - logMessage("Thread.sleep exception " + ex,true,true); + } catch (Exception ex) { + logMessage("Thread.sleep exception " + ex, true, true); } return; } - while(gotEvent) { + while (gotEvent) { if (isDebugMessages) logMessage("calling updateImage", true, true); try { boolean result = updateImage(pvaClientMonitor.getData()); - if(!result) { - logMessage("updateImage failed",true,true); + if (!result) { + logMessage("updateImage failed", true, true); Thread.sleep(MS_WAIT); } - } catch(Exception ex) { - logMessage("handleEvents caught exception " + ex,true,true); + } catch (Exception ex) { + logMessage("handleEvents caught exception " + ex, true, true); } pvaClientMonitor.releaseEvent(); // Break out of the loop if the display is stopped @@ -264,31 +249,28 @@ private void handleEvents() gotEvent = pvaClientMonitor.poll(); } } + /* (non-Javadoc) * @see ij.plugin.PlugIn#run(java.lang.String) */ - public void run(String arg) - { + public void run(String arg) { IJ.showStatus("epics running"); - try - { + try { isPluginRunning = true; Date date = new Date(); prevTime = date.getTime(); - if (isDebugFile) - { + if (isDebugFile) { debugFile = new FileOutputStream(System.getProperty("user.home") + System.getProperty("file.separator") + "IJEPICS_debug.txt"); debugPrintStream = new PrintStream(debugFile); } stopDisplay(); connectPV(); - while (isPluginRunning) - { + while (isPluginRunning) { // A very short wait here lets stopMonitor run quickly when needed Thread.sleep(1); - synchronized(this) { - if (isStarted && pvaClientMonitor!=null) { + synchronized (this) { + if (isStarted && pvaClientMonitor != null) { handleEvents(); } else { Thread.sleep(MS_WAIT); @@ -297,8 +279,7 @@ public void run(String arg) } // isPluginRunning if (isDebugMessages) logMessage("run: Plugin stopping", true, true); - if (isDebugFile) - { + if (isDebugFile) { debugPrintStream.close(); debugFile.close(); logMessage("Closed debug file", true, true); @@ -306,31 +287,26 @@ public void run(String arg) disconnectPV(); timer.stop(); writeProperties(); - if(img!=null) img.close(); + if (img != null) img.close(); frame.setVisible(false); IJ.showStatus("Exiting Server"); - } - catch (Exception e) - { + } catch (Exception e) { logMessage("run: Got exception: " + e.getMessage(), true, true); e.printStackTrace(); logMessage("Close EPICS_NTNDA_Viewer window, and reopen, try again", true, true); IJ.showStatus(e.toString()); - try - { - if (isDebugFile) - { + try { + if (isDebugFile) { debugPrintStream.close(); debugFile.close(); } + } catch (Exception ee) { } - catch (Exception ee) { } } } - private void makeImageCopy() - { + private void makeImageCopy() { ImageProcessor ip = img.getProcessor(); if (ip == null) return; ImagePlus imgcopy = new ImagePlus(channelName + ":" + numImageUpdates, ip.duplicate()); @@ -338,29 +314,28 @@ private void makeImageCopy() } - private boolean updateImage(PvaClientMonitorData monitorData) - { - Point oldWindowLocation =null; + private boolean updateImage(PvaClientMonitorData monitorData) { + Point oldWindowLocation = null; boolean madeNewWindow = false; PVStructure pvs = monitorData.getPVStructure(); - PVStructureArray dimArray = pvs.getSubField(PVStructureArray.class,"dimension"); - if(dimArray==null) { - logMessage("dimension not found",true,true); + PVStructureArray dimArray = pvs.getSubField(PVStructureArray.class, "dimension"); + if (dimArray == null) { + logMessage("dimension not found", true, true); return false; } int ndim = dimArray.getLength(); - if(ndim<1) { - logMessage("dimension is empty",true,true); + if (ndim < 1) { + logMessage("dimension is empty", true, true); return false; } int dimsint[] = new int[ndim]; - StructureArrayData dimdata=new StructureArrayData(); - dimArray.get(0,ndim,dimdata); - for(int i=0; i=3) + if (ndim >= 3) nz = dimsint[2]; int cm = 0; - PVStructureArray attrArray = pvs.getSubField(PVStructureArray.class,"attribute"); - if(attrArray!=null) { + PVStructureArray attrArray = pvs.getSubField(PVStructureArray.class, "attribute"); + if (attrArray != null) { int nattr = attrArray.getLength(); - StructureArrayData attrdata=new StructureArrayData(); - attrArray.get(0,nattr,attrdata); - for (int i = 0; i= 2 && colorMode <= 4) - { - int[] pixels = (int[])img.getProcessor().getPixels(); + } else if (colorMode >= 2 && colorMode <= 4) { + int[] pixels = (int[]) img.getProcessor().getPixels(); - byte inpixels[]=new byte[numElements]; + byte inpixels[] = new byte[numElements]; convert.toByteArray(imagedata, 0, numElements, inpixels, 0); double dispMin = img.getDisplayRangeMin(); @@ -570,54 +524,46 @@ else if (colorMode >= 2 && colorMode <= 4) // Recompute LUT prevDispMin = dispMin; prevDispMax = dispMax; - double slope = 255/(dispMax - dispMin); - for (i=0; i<256; i++) { - if (idispMax) - colorLUT[i] = (byte)255; + else if (i > dispMax) + colorLUT[i] = (byte) 255; else - colorLUT[i] = (byte)((i-dispMin)*slope + 0.5); + colorLUT[i] = (byte) ((i - dispMin) * slope + 0.5); } } - for (i=0; idispMax) img.getProcessor().setPixels(pixels); } - //ADDITION - if (isLogOn && startedLog) - { - img.getProcessor().snapshot(); - snapBackup = img.getProcessor().getSnapshotPixels(); - img.getProcessor().resetMinAndMax(); - img.getProcessor().log(); + /*Takes log of image, stores snapshot for Undo if plugin is stopped. + */ + if (isLogOn) { + snapBackup = takeLog(img); + if(dataType!= ScalarType.pvUShort && dataType!=ScalarType.pvUByte) resetContrast(img); } - //END ADDITION - if (isSaveToStack) - { + if (isSaveToStack) { img.getStack().addSlice(channelName + numImageUpdates, img.getProcessor().duplicate()); } img.setSlice(img.getNSlices()); img.show(); img.updateAndDraw(); ImageCanvas ic = img.getCanvas(); - Point loc = ic!=null ? ic.getCursorLoc() : null; - if (loc!=null) - img.mouseMoved(loc.x,loc.y); + Point loc = ic != null ? ic.getCursorLoc() : null; + if (loc != null) + img.mouseMoved(loc.x, loc.y); img.updateStatusbarValue(); numImageUpdates++; - //ADDITION - if (isLogOn) startedLog = true; - //END ADDITION // Automatically set brightness and contrast if we made a new window if (madeNewWindow) new ContrastEnhancer().stretchHistogram(img, 0.5); return true; @@ -662,8 +601,7 @@ else if (i>dispMax) * this method should be invoked from the * event-dispatching thread. */ - private void createAndShowGUI() - { + private void createAndShowGUI() { //Create and set up the window. nxText = new JTextField(6); nxText.setEditable(false); @@ -685,9 +623,7 @@ private void createAndShowGUI() stopButton = new JButton("Stop"); snapButton = new JButton("Snap"); JCheckBox captureCheckBox = new JCheckBox(""); - //ADDITION JCheckBox logCheckBox = new JCheckBox(""); - //END ADDITION frame = new JFrame("Image J EPICS_NTNDA_Viewer Plugin"); JPanel panel = new JPanel(new BorderLayout()); @@ -714,10 +650,8 @@ private void createAndShowGUI() panel.add(new JLabel("Frames/s"), c); c.gridx = 7; panel.add(new JLabel("Capture to Stack"), c); - //ADDITION c.gridx = 8; panel.add(new JLabel("Log"), c); - //END ADDITION // Middle row // These widgets should be centered @@ -740,10 +674,8 @@ private void createAndShowGUI() c.gridx = 7; panel.add(captureCheckBox, c); c.gridx = 8; - //ADDITION panel.add(logCheckBox, c); c.gridx = 9; - //END ADDITION panel.add(snapButton, c); // Bottom row @@ -762,15 +694,13 @@ private void createAndShowGUI() frame.setVisible(true); int timerDelay = 2000; // 2 seconds - timer = new javax.swing.Timer(timerDelay, new ActionListener() - { - public void actionPerformed(ActionEvent event) - { + timer = new javax.swing.Timer(timerDelay, new ActionListener() { + public void actionPerformed(ActionEvent event) { long time = new Date().getTime(); - double elapsedTime = (double)(time - prevTime)/1000.; + double elapsedTime = (double) (time - prevTime) / 1000.; double fps = numImageUpdates / elapsedTime; NumberFormat form = DecimalFormat.getInstance(); - ((DecimalFormat)form).applyPattern("0.0"); + ((DecimalFormat) form).applyPattern("0.0"); fpsText.setText("" + form.format(fps)); if (isPluginRunning && isStarted && numImageUpdates > 0) logMessage(String.format("Received %d images in %.2f sec", numImageUpdates, elapsedTime), true, false); @@ -780,84 +710,92 @@ public void actionPerformed(ActionEvent event) }); timer.start(); - channelNameText.addActionListener(new ActionListener() - { - public void actionPerformed(ActionEvent event) - { + channelNameText.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent event) { disconnectPV(); connectPV(); } }); - startButton.addActionListener(new ActionListener() - { - public void actionPerformed(ActionEvent event) - { + startButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent event) { startDisplay(); } }); - stopButton.addActionListener(new ActionListener() - { - public void actionPerformed(ActionEvent event) - { + stopButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent event) { stopDisplay(); } }); - //ADDITION - logCheckBox.addItemListener(new ItemListener() - { - public void itemStateChanged(ItemEvent e) - { - if(e.getStateChange() == ItemEvent.SELECTED) - { + //Turns log on and off. If plugin is stopped, log checkbox takes log or undoes log on static image or other image depending on user selection + logCheckBox.addItemListener(new ItemListener() { + public void itemStateChanged(ItemEvent e) { + if (e.getStateChange() == ItemEvent.SELECTED) { isLogOn = true; logMessage("Log display on", true, true); - if(!stopButton.isEnabled()){ - img.getProcessor().snapshot(); - snapBackup = img.getProcessor().getSnapshotPixels(); - img.getProcessor().resetMinAndMax(); - img.getProcessor().log(); - img.updateAndDraw(); + if (!stopButton.isEnabled()) { + if (WindowManager.getCurrentWindow().equals(img.getWindow())) { + snapBackup = takeLog(img); + img.updateAndDraw(); + resetContrast(img); + } + else{ + ImagePlus imgC = WindowManager.getCurrentImage(); + altSnapBackup = takeLog(imgC); + imgC.updateAndDraw(); + resetContrast(imgC); + } } - startedLog = true; - } - else - { + else if (!WindowManager.getCurrentWindow().equals(img.getWindow())){ + ImagePlus imgC = WindowManager.getCurrentImage(); + altSnapBackup = takeLog(imgC); + imgC.updateAndDraw(); + resetContrast(imgC); + isLogOn = false; + } + } else { logMessage("Log display off", true, true); isLogOn = false; - if(!stopButton.isEnabled()){ - WindowManager.setCurrentWindow(img.getWindow()); - img.getProcessor().setSnapshotPixels(snapBackup); + if (!stopButton.isEnabled()) { + if (WindowManager.getCurrentWindow().equals(img.getWindow())) { + WindowManager.setCurrentWindow(img.getWindow()); + img.getProcessor().setSnapshotPixels(snapBackup); + Undo.undo(); + resetContrast(img); + } + else{ + ImagePlus imgC = WindowManager.getCurrentImage(); + imgC.getProcessor().setSnapshotPixels(altSnapBackup); + Undo.undo(); + resetContrast(imgC); + } + } + else if (!WindowManager.getCurrentWindow().equals(img.getWindow())){ + ImagePlus imgC = WindowManager.getCurrentImage(); + imgC.getProcessor().setSnapshotPixels(altSnapBackup); Undo.undo(); + resetContrast(imgC); } - startedLog = false; } + } }); - //END ADDITION - snapButton.addActionListener(new ActionListener() - { - public void actionPerformed(ActionEvent event) - { + snapButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent event) { makeImageCopy(); } }); - captureCheckBox.addItemListener(new ItemListener() - { - public void itemStateChanged(ItemEvent e) - { - if (e.getStateChange() == ItemEvent.SELECTED) - { + captureCheckBox.addItemListener(new ItemListener() { + public void itemStateChanged(ItemEvent e) { + if (e.getStateChange() == ItemEvent.SELECTED) { isSaveToStack = true; isNewStack = true; logMessage("Capture on", true, true); - } - else - { + } else { isSaveToStack = false; logMessage("Capture off", true, true); } @@ -866,21 +804,28 @@ public void itemStateChanged(ItemEvent e) }); } - private class FrameExitListener extends WindowAdapter - { - public void windowClosing(WindowEvent event) - { + private Object takeLog(ImagePlus image){ + image.getProcessor().snapshot(); + image.getProcessor().resetMinAndMax(); + image.getProcessor().log(); + return image.getProcessor().getSnapshotPixels(); + } + private void resetContrast(ImagePlus image){ + image.getProcessor().resetMinAndMax(); + if (image.getProcessor().getMin()<0) image.getProcessor().setMinAndMax(0, image.getProcessor().getMax()); + new ContrastEnhancer().stretchHistogram(img, 0.5); + } + private class FrameExitListener extends WindowAdapter { + public void windowClosing(WindowEvent event) { isPluginRunning = false; // We need to wake up the main thread so it shuts down cleanly - synchronized (this) - { + synchronized (this) { notify(); } } } - private void logMessage(String message, boolean logDisplay, boolean logFile) - { + private void logMessage(String message, boolean logDisplay, boolean logFile) { Date date = new Date(); SimpleDateFormat simpleDate = new SimpleDateFormat("dd-MMM-yyyy kk:mm:ss.SSS"); String completeMessage; @@ -890,11 +835,9 @@ private void logMessage(String message, boolean logDisplay, boolean logFile) if (logFile) IJ.log(completeMessage); } - private void readProperties() - { + private void readProperties() { String temp, path = null; - try - { + try { String fileSep = System.getProperty("file.separator"); path = System.getProperty("user.home") + fileSep + propertyFile; FileInputStream file = new FileInputStream(path); @@ -903,18 +846,14 @@ private void readProperties() temp = properties.getProperty("channelName"); if (temp != null) channelName = temp; IJ.log("Read properties file: " + path + " channelName= " + channelName); - } - catch (Exception ex) - { + } catch (Exception ex) { IJ.log("readProperties:exception: " + ex.getMessage()); } } - private void writeProperties() - { + private void writeProperties() { String path; - try - { + try { String fileSep = System.getProperty("file.separator"); path = System.getProperty("user.home") + fileSep + propertyFile; properties.setProperty("channelName", channelName); @@ -922,9 +861,7 @@ private void writeProperties() properties.store(file, "EPICS_NTNDA_Viewer Properties"); file.close(); logMessage("Wrote properties file: " + path, true, true); - } - catch (Exception ex) - { + } catch (Exception ex) { logMessage("writeProperties:exception: " + ex.getMessage(), true, true); } } From 10177282163a6e95342ce2658ed09835b9e62365 Mon Sep 17 00:00:00 2001 From: abrewe Date: Thu, 30 Jun 2022 12:00:31 -0500 Subject: [PATCH 3/8] formatting --- .../EPICS_NTNDA_Viewer.java | 209 +++++++++++------- 1 file changed, 128 insertions(+), 81 deletions(-) diff --git a/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java b/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java index 9956a48..588c4d5 100755 --- a/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java +++ b/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java @@ -11,7 +11,6 @@ import java.awt.event.ItemListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; -import java.awt.geom.Arc2D; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.PrintStream; @@ -29,8 +28,6 @@ import javax.swing.JTextField; import javax.swing.border.EmptyBorder; -import ij.gui.PlotWindow; -import ij.plugin.frame.ContrastAdjuster; import org.epics.nt.NTNDArray; import org.epics.pvaClient.PvaClient; import org.epics.pvaClient.PvaClientChannel; @@ -177,13 +174,13 @@ private void connectPV() { private void disconnectPV() { try { - if (pvaClientChannel == null) { + if(pvaClientChannel == null) { throw new RuntimeException("Channel already disconnected"); } isChannelConnected = false; - if (isStarted) stopMonitor(); + if(isStarted) stopMonitor(); pvaClientChannel.destroy(); - if (pvaClientMonitor != null) pvaClientMonitor.destroy(); + if(pvaClientMonitor!=null) pvaClientMonitor.destroy(); pvaClientChannel = null; pvaClientMonitor = null; logMessage("Disconnected from EPICS PV:" + channelName, true, true); @@ -224,7 +221,7 @@ private void stopDisplay() { private void handleEvents() { boolean gotEvent = pvaClientMonitor.poll(); - if (!gotEvent) { + if(!gotEvent) { try { Thread.sleep(1); } catch (Exception ex) { @@ -232,11 +229,11 @@ private void handleEvents() { } return; } - while (gotEvent) { + while(gotEvent) { if (isDebugMessages) logMessage("calling updateImage", true, true); try { boolean result = updateImage(pvaClientMonitor.getData()); - if (!result) { + if(!result) { logMessage("updateImage failed", true, true); Thread.sleep(MS_WAIT); } @@ -269,8 +266,8 @@ public void run(String arg) { while (isPluginRunning) { // A very short wait here lets stopMonitor run quickly when needed Thread.sleep(1); - synchronized (this) { - if (isStarted && pvaClientMonitor != null) { + synchronized(this) { + if (isStarted && pvaClientMonitor!=null) { handleEvents(); } else { Thread.sleep(MS_WAIT); @@ -287,7 +284,7 @@ public void run(String arg) { disconnectPV(); timer.stop(); writeProperties(); - if (img != null) img.close(); + if (img!=null) img.close(); frame.setVisible(false); IJ.showStatus("Exiting Server"); @@ -368,25 +365,25 @@ private boolean updateImage(PvaClientMonitorData monitorData) { break; } } - PVUnion pvUnionValue = pvs.getSubField(PVUnion.class, "value"); - if (pvUnionValue == null) { - logMessage("value not found", true, true); + PVUnion pvUnionValue = pvs.getSubField(PVUnion.class,"value"); + if (pvUnionValue==null) { + logMessage("value not found",true,true); return false; } PVScalarArray imagedata = pvUnionValue.get(PVScalarArray.class); - if (imagedata == null) { - logMessage("value is not a scalar array", true, true); + if (imagedata==null) { + logMessage("value is not a scalar array",true,true); return false; } - PVStructure pvCodecStruct = pvs.getSubField(PVStructure.class, "codec"); + PVStructure pvCodecStruct = pvs.getSubField(PVStructure.class,"codec"); PVString pvCodec = pvCodecStruct.getSubField(PVString.class, "name"); String codec = pvCodec.get(); if (!codec.isEmpty()) { - if (ntndCodec == null) { + if (ntndCodec==null) { ntndCodec = new NTNDCodec(); } NTNDArray ntndArray = NTNDArray.wrapUnsafe(pvs); - if (ntndArray == null) { + if (ntndArray==null) { logMessage("value is not a valid NTNDArray", true, true); return false; } @@ -402,7 +399,7 @@ private boolean updateImage(PvaClientMonitorData monitorData) { int numElements = nx * ny * nz; if (numElements == 0) { - logMessage("array size = 0", true, true); + logMessage("array size = 0",true,true); return false; } @@ -410,12 +407,13 @@ private boolean updateImage(PvaClientMonitorData monitorData) { logMessage("UpdateImage dt,dataType" + scalarType, true, true); if (isDebugMessages) - logMessage("UpdateImage cm,colorMode" + cm + " " + colorMode, true, true); + logMessage("UpdateImage cm,colorMode" + cm+ " "+colorMode, true, true); // if image size changes we must close window and make a new one. boolean makeNewWindow = false; - if (nx != imageSizeX || ny != imageSizeY || nz != imageSizeZ || cm != colorMode || scalarType != dataType) { + if (nx != imageSizeX || ny != imageSizeY || nz != imageSizeZ || cm != colorMode || scalarType != dataType) + { makeNewWindow = true; imageSizeX = nx; imageSizeY = ny; @@ -429,35 +427,47 @@ private boolean updateImage(PvaClientMonitorData monitorData) { // If we are making a new stack close the window if (isNewStack) makeNewWindow = true; - if (img == null) makeNewWindow = true; + if(img==null) makeNewWindow = true; // If we need to make a new window then close the current one if it exists - if (img != null && makeNewWindow) { - try { - if (img.getWindow() == null || !img.getWindow().isClosed()) { + if (img != null && makeNewWindow) + { + try + { + if (img.getWindow() == null || !img.getWindow().isClosed()) + { ImageWindow win = img.getWindow(); if (win != null) { oldWindowLocation = win.getLocationOnScreen(); } img.close(); } - } catch (Exception ex) { + } + catch (Exception ex) + { logMessage("Exception closing window " + ex.getMessage(), true, true); } makeNewWindow = false; } // If the window does not exist or is closed make a new one if (img == null || img.getWindow() == null || img.getWindow().isClosed()) { - switch (colorMode) { + switch (colorMode) + { case 0: case 1: - if (dataType == ScalarType.pvUByte) { + if (dataType == ScalarType.pvUByte) + { img = new ImagePlus(channelName, new ByteProcessor(imageSizeX, imageSizeY)); - } else if (dataType == ScalarType.pvUShort) { + } + else if (dataType == ScalarType.pvUShort) + { img = new ImagePlus(channelName, new ShortProcessor(imageSizeX, imageSizeY)); - } else if (dataType.isNumeric()) { + } + else if (dataType.isNumeric()) { img = new ImagePlus(channelName, new FloatProcessor(imageSizeX, imageSizeY)); - } else { + } + else + { throw new RuntimeException("illegal array type " + dataType); } break; @@ -476,7 +486,8 @@ private boolean updateImage(PvaClientMonitorData monitorData) { madeNewWindow = true; } - if (isNewStack) { + if (isNewStack) + { imageStack = new ImageStack(img.getWidth(), img.getHeight()); imageStack.addSlice(channelName + numImageUpdates, img.getProcessor()); // Note: we need to add this first slice twice in order to get the slider bar @@ -485,35 +496,43 @@ private boolean updateImage(PvaClientMonitorData monitorData) { img.close(); img = new ImagePlus(channelName, imageStack); img.show(); - logMessage("img.show() run", true, true); isNewStack = false; } imagedata = pvUnionValue.get(PVScalarArray.class); - if (imagedata == null) { + if(imagedata==null) { logMessage("value is not a scalar array", true, true); return false; } - if (colorMode == 0 || colorMode == 1) { - if (dataType == ScalarType.pvUByte) { + if (colorMode == 0 || colorMode == 1) + { + if (dataType == ScalarType.pvUByte) + { byte[] pixels = new byte[numElements]; convert.toByteArray(imagedata, 0, numElements, pixels, 0); img.getProcessor().setPixels(pixels); - } else if (dataType == ScalarType.pvUShort) { + } + else if (dataType == ScalarType.pvUShort) + { short[] pixels = new short[numElements]; convert.toShortArray(imagedata, 0, numElements, pixels, 0); img.getProcessor().setPixels(pixels); - } else if (dataType.isNumeric()) { + } + else if (dataType.isNumeric()) { float[] pixels = new float[numElements]; convert.toFloatArray(imagedata, 0, numElements, pixels, 0); img.getProcessor().setPixels(pixels); - } else { + } + else + { throw new RuntimeException("illegal array type " + dataType); } - } else if (colorMode >= 2 && colorMode <= 4) { + } + else if (colorMode >= 2 && colorMode <= 4) + { int[] pixels = (int[]) img.getProcessor().getPixels(); - byte inpixels[] = new byte[numElements]; + byte inpixels[]=new byte[numElements]; convert.toByteArray(imagedata, 0, numElements, inpixels, 0); double dispMin = img.getDisplayRangeMin(); @@ -524,30 +543,33 @@ private boolean updateImage(PvaClientMonitorData monitorData) { // Recompute LUT prevDispMin = dispMin; prevDispMax = dispMax; - double slope = 255 / (dispMax - dispMin); - for (i = 0; i < 256; i++) { - if (i < dispMin) + double slope = 255/(dispMax - dispMin); + for (i=0; i<256; i++) { + if (i dispMax) + else if (i>dispMax) colorLUT[i] = (byte) 255; else - colorLUT[i] = (byte) ((i - dispMin) * slope + 0.5); + colorLUT[i] = (byte)((i-dispMin)*slope + 0.5); } } - for (i = 0; i < numElements; i++) { + for (i=0; i dispMax) } } break; - case 4: { + case 4: + { int imageSize = imageSizeX * imageSizeY; int redIn = 0, greenIn = imageSize, blueIn = 2 * imageSize, out = 0; while (redIn < imageSize) { @@ -579,16 +602,17 @@ else if (i > dispMax) if(dataType!= ScalarType.pvUShort && dataType!=ScalarType.pvUByte) resetContrast(img); } - if (isSaveToStack) { + if (isSaveToStack) + { img.getStack().addSlice(channelName + numImageUpdates, img.getProcessor().duplicate()); } img.setSlice(img.getNSlices()); img.show(); img.updateAndDraw(); ImageCanvas ic = img.getCanvas(); - Point loc = ic != null ? ic.getCursorLoc() : null; - if (loc != null) - img.mouseMoved(loc.x, loc.y); + Point loc = ic!=null ? ic.getCursorLoc() : null; + if (loc!=null) + img.mouseMoved(loc.x,loc.y); img.updateStatusbarValue(); numImageUpdates++; // Automatically set brightness and contrast if we made a new window @@ -601,7 +625,8 @@ else if (i > dispMax) * this method should be invoked from the * event-dispatching thread. */ - private void createAndShowGUI() { + private void createAndShowGUI() + { //Create and set up the window. nxText = new JTextField(6); nxText.setEditable(false); @@ -694,13 +719,15 @@ private void createAndShowGUI() { frame.setVisible(true); int timerDelay = 2000; // 2 seconds - timer = new javax.swing.Timer(timerDelay, new ActionListener() { - public void actionPerformed(ActionEvent event) { + timer = new javax.swing.Timer(timerDelay, new ActionListener() + { + public void actionPerformed(ActionEvent event) + { long time = new Date().getTime(); - double elapsedTime = (double) (time - prevTime) / 1000.; + double elapsedTime = (double)(time - prevTime)/1000.; double fps = numImageUpdates / elapsedTime; NumberFormat form = DecimalFormat.getInstance(); - ((DecimalFormat) form).applyPattern("0.0"); + ((DecimalFormat)form).applyPattern("0.0"); fpsText.setText("" + form.format(fps)); if (isPluginRunning && isStarted && numImageUpdates > 0) logMessage(String.format("Received %d images in %.2f sec", numImageUpdates, elapsedTime), true, false); @@ -710,21 +737,27 @@ public void actionPerformed(ActionEvent event) { }); timer.start(); - channelNameText.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent event) { + channelNameText.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent event) + { disconnectPV(); connectPV(); } }); - startButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent event) { + startButton.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent event) + { startDisplay(); } }); - stopButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent event) { + stopButton.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent event) + { stopDisplay(); } }); @@ -783,19 +816,26 @@ else if (!WindowManager.getCurrentWindow().equals(img.getWindow())){ } }); - snapButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent event) { + snapButton.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent event) + { makeImageCopy(); } }); - captureCheckBox.addItemListener(new ItemListener() { - public void itemStateChanged(ItemEvent e) { - if (e.getStateChange() == ItemEvent.SELECTED) { + captureCheckBox.addItemListener(new ItemListener() + { + public void itemStateChanged(ItemEvent e) + { + if (e.getStateChange() == ItemEvent.SELECTED) + { isSaveToStack = true; isNewStack = true; logMessage("Capture on", true, true); - } else { + } + else + { isSaveToStack = false; logMessage("Capture off", true, true); } @@ -825,7 +865,8 @@ public void windowClosing(WindowEvent event) { } } - private void logMessage(String message, boolean logDisplay, boolean logFile) { + private void logMessage(String message, boolean logDisplay, boolean logFile) + { Date date = new Date(); SimpleDateFormat simpleDate = new SimpleDateFormat("dd-MMM-yyyy kk:mm:ss.SSS"); String completeMessage; @@ -835,9 +876,11 @@ private void logMessage(String message, boolean logDisplay, boolean logFile) { if (logFile) IJ.log(completeMessage); } - private void readProperties() { + private void readProperties() + { String temp, path = null; - try { + try + { String fileSep = System.getProperty("file.separator"); path = System.getProperty("user.home") + fileSep + propertyFile; FileInputStream file = new FileInputStream(path); @@ -846,14 +889,17 @@ private void readProperties() { temp = properties.getProperty("channelName"); if (temp != null) channelName = temp; IJ.log("Read properties file: " + path + " channelName= " + channelName); - } catch (Exception ex) { + } catch (Exception ex) + { IJ.log("readProperties:exception: " + ex.getMessage()); } } - private void writeProperties() { + private void writeProperties() + { String path; - try { + try + { String fileSep = System.getProperty("file.separator"); path = System.getProperty("user.home") + fileSep + propertyFile; properties.setProperty("channelName", channelName); @@ -861,7 +907,8 @@ private void writeProperties() { properties.store(file, "EPICS_NTNDA_Viewer Properties"); file.close(); logMessage("Wrote properties file: " + path, true, true); - } catch (Exception ex) { + } catch (Exception ex) + { logMessage("writeProperties:exception: " + ex.getMessage(), true, true); } } From 3fd6088ac21b4efd57976e8c17293df660abe7b2 Mon Sep 17 00:00:00 2001 From: abrewe Date: Thu, 30 Jun 2022 12:28:51 -0500 Subject: [PATCH 4/8] more formatting --- .../EPICS_NTNDA_Viewer.java | 174 ++++++++++-------- 1 file changed, 98 insertions(+), 76 deletions(-) diff --git a/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java b/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java index 588c4d5..5608aea 100755 --- a/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java +++ b/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java @@ -75,7 +75,7 @@ public class EPICS_NTNDA_Viewer private static final int QUEUE_SIZE = 1; private static final int MS_WAIT = 100; - private static PvaClient pva = PvaClient.get("pva"); + private static PvaClient pva=PvaClient.get("pva"); private static Convert convert = ConvertFactory.getConvert(); private PvaClientChannel pvaClientChannel = null; private PvaClientMonitor pvaClientMonitor = null; @@ -125,7 +125,8 @@ public class EPICS_NTNDA_Viewer /** * Constructor */ - public EPICS_NTNDA_Viewer() { + public EPICS_NTNDA_Viewer() + { String temp = null; readProperties(); temp = System.getenv("EPICS_NTNDA_VIEWER_CHANNELNAME"); @@ -140,47 +141,53 @@ public EPICS_NTNDA_Viewer() { */ public void channelStateChange(PvaClientChannel channel, boolean connected) { isChannelConnected = connected; - if (isChannelConnected) { + if(isChannelConnected) { channelNameText.setBackground(Color.green); logMessage("State changed to connected for " + channelName, true, true); - if (pvaClientMonitor == null) { - pvaClientMonitor = pvaClientChannel.createMonitor("record[queueSize=" + QUEUE_SIZE + "]field()"); + if (pvaClientMonitor==null) { + pvaClientMonitor=pvaClientChannel.createMonitor("record[queueSize=" + QUEUE_SIZE + "]field()"); pvaClientMonitor.issueConnect(); } if (startIsTrue) startMonitor(); - } else if (pvaClientMonitor != null) { + } else if(pvaClientMonitor!=null) { channelNameText.setBackground(Color.red); logMessage("State changed to disconnected for " + channelName, true, true); numImageUpdates = 0; } } - private void connectPV() { - try { + private void connectPV() + { + try + { if (pvaClientChannel != null) { throw new RuntimeException("Channel already connected"); } channelName = channelNameText.getText(); logMessage("Trying to connect to : " + channelName, true, true); - pvaClientChannel = pva.createChannel(channelName, "pva"); + pvaClientChannel = pva.createChannel(channelName,"pva"); pvaClientChannel.setStateChangeRequester(this); isChannelConnected = false; channelNameText.setBackground(Color.red); pvaClientChannel.issueConnect(); - } catch (Exception ex) { + } + catch (Exception ex) + { logMessage("Could not connect to : " + channelName + " " + ex.getMessage(), true, false); } } - private void disconnectPV() { - try { + private void disconnectPV() + { + try + { if(pvaClientChannel == null) { throw new RuntimeException("Channel already disconnected"); } isChannelConnected = false; if(isStarted) stopMonitor(); pvaClientChannel.destroy(); - if(pvaClientMonitor!=null) pvaClientMonitor.destroy(); + if(pvaClientMonitor!=null) pvaClientMonitor.destroy(); pvaClientChannel = null; pvaClientMonitor = null; logMessage("Disconnected from EPICS PV:" + channelName, true, true); @@ -189,19 +196,22 @@ private void disconnectPV() { } } - private void startMonitor() { + private void startMonitor() + { isStarted = true; pvaClientMonitor.start(); } - private void stopMonitor() { + private void stopMonitor() + { synchronized (this) { pvaClientMonitor.stop(); isStarted = false; } } - private void startDisplay() { + private void startDisplay() + { startButton.setEnabled(false); stopButton.setEnabled(true); snapButton.setEnabled(true); @@ -210,7 +220,8 @@ private void startDisplay() { logMessage("Display started", true, false); } - private void stopDisplay() { + private void stopDisplay() + { startButton.setEnabled(true); stopButton.setEnabled(false); snapButton.setEnabled(false); @@ -219,13 +230,14 @@ private void stopDisplay() { logMessage("Display stopped", true, false); } - private void handleEvents() { + private void handleEvents() + { boolean gotEvent = pvaClientMonitor.poll(); if(!gotEvent) { try { Thread.sleep(1); - } catch (Exception ex) { - logMessage("Thread.sleep exception " + ex, true, true); + } catch(Exception ex) { + logMessage("Thread.sleep exception " + ex,true,true); } return; } @@ -234,11 +246,11 @@ private void handleEvents() { try { boolean result = updateImage(pvaClientMonitor.getData()); if(!result) { - logMessage("updateImage failed", true, true); + logMessage("updateImage failed",true,true); Thread.sleep(MS_WAIT); } - } catch (Exception ex) { - logMessage("handleEvents caught exception " + ex, true, true); + } catch(Exception ex) { + logMessage("handleEvents caught exception " + ex,true,true); } pvaClientMonitor.releaseEvent(); // Break out of the loop if the display is stopped @@ -250,20 +262,24 @@ private void handleEvents() { /* (non-Javadoc) * @see ij.plugin.PlugIn#run(java.lang.String) */ - public void run(String arg) { + public void run(String arg) + { IJ.showStatus("epics running"); - try { + try + { isPluginRunning = true; Date date = new Date(); prevTime = date.getTime(); - if (isDebugFile) { + if (isDebugFile) + { debugFile = new FileOutputStream(System.getProperty("user.home") + System.getProperty("file.separator") + "IJEPICS_debug.txt"); debugPrintStream = new PrintStream(debugFile); } stopDisplay(); connectPV(); - while (isPluginRunning) { + while (isPluginRunning) + { // A very short wait here lets stopMonitor run quickly when needed Thread.sleep(1); synchronized(this) { @@ -276,7 +292,8 @@ public void run(String arg) { } // isPluginRunning if (isDebugMessages) logMessage("run: Plugin stopping", true, true); - if (isDebugFile) { + if (isDebugFile) + { debugPrintStream.close(); debugFile.close(); logMessage("Closed debug file", true, true); @@ -284,26 +301,31 @@ public void run(String arg) { disconnectPV(); timer.stop(); writeProperties(); - if (img!=null) img.close(); + if(img!=null) img.close(); frame.setVisible(false); IJ.showStatus("Exiting Server"); - } catch (Exception e) { + } + catch (Exception e) + { logMessage("run: Got exception: " + e.getMessage(), true, true); e.printStackTrace(); logMessage("Close EPICS_NTNDA_Viewer window, and reopen, try again", true, true); IJ.showStatus(e.toString()); - try { - if (isDebugFile) { + try + { + if (isDebugFile) + { debugPrintStream.close(); debugFile.close(); } - } catch (Exception ee) { } + catch (Exception ee) { } } } - private void makeImageCopy() { + private void makeImageCopy() + { ImageProcessor ip = img.getProcessor(); if (ip == null) return; ImagePlus imgcopy = new ImagePlus(channelName + ":" + numImageUpdates, ip.duplicate()); @@ -311,28 +333,29 @@ private void makeImageCopy() { } - private boolean updateImage(PvaClientMonitorData monitorData) { + private boolean updateImage(PvaClientMonitorData monitorData) + { Point oldWindowLocation = null; boolean madeNewWindow = false; PVStructure pvs = monitorData.getPVStructure(); PVStructureArray dimArray = pvs.getSubField(PVStructureArray.class, "dimension"); - if (dimArray == null) { + if(dimArray==null) { logMessage("dimension not found", true, true); return false; } int ndim = dimArray.getLength(); - if (ndim < 1) { + if(ndim<1) { logMessage("dimension is empty", true, true); return false; } int dimsint[] = new int[ndim]; - StructureArrayData dimdata = new StructureArrayData(); - dimArray.get(0, ndim, dimdata); - for (int i = 0; i < ndim; ++i) { + StructureArrayData dimdata=new StructureArrayData(); + dimArray.get(0,ndim,dimdata); + for(int i=0; i= 3) + if (ndim>=3) nz = dimsint[2]; int cm = 0; PVStructureArray attrArray = pvs.getSubField(PVStructureArray.class, "attribute"); - if (attrArray != null) { + if(attrArray!=null) { int nattr = attrArray.getLength(); StructureArrayData attrdata = new StructureArrayData(); attrArray.get(0, nattr, attrdata); - for (int i = 0; i < nattr; i++) { + for (int i = 0; i < nattr; i++) + { PVStructure pvAttr = attrdata.data[i]; - PVString pvName = pvAttr.getSubField(PVString.class, "name"); - if (pvName == null) continue; + PVString pvName = pvAttr.getSubField(PVString.class,"name"); + if(pvName==null) continue; String name = pvName.get(); - if (!name.equals("ColorMode")) continue; + if(!name.equals("ColorMode")) continue; PVUnion pvUnion = pvAttr.getSubField(PVUnion.class, "value"); - if (pvUnion == null) continue; + if(pvUnion==null) continue; PVScalar pvcm = pvUnion.get(PVScalar.class); - if (pvcm == null) { + if(pvcm==null) { logMessage("color mode is not a PVScalar", true, true); continue; } @@ -366,12 +390,12 @@ private boolean updateImage(PvaClientMonitorData monitorData) { } } PVUnion pvUnionValue = pvs.getSubField(PVUnion.class,"value"); - if (pvUnionValue==null) { + if(pvUnionValue==null) { logMessage("value not found",true,true); return false; } PVScalarArray imagedata = pvUnionValue.get(PVScalarArray.class); - if (imagedata==null) { + if(imagedata==null) { logMessage("value is not a scalar array",true,true); return false; } @@ -379,12 +403,12 @@ private boolean updateImage(PvaClientMonitorData monitorData) { PVString pvCodec = pvCodecStruct.getSubField(PVString.class, "name"); String codec = pvCodec.get(); if (!codec.isEmpty()) { - if (ntndCodec==null) { + if(ntndCodec==null) { ntndCodec = new NTNDCodec(); } NTNDArray ntndArray = NTNDArray.wrapUnsafe(pvs); - if (ntndArray==null) { - logMessage("value is not a valid NTNDArray", true, true); + if(ntndArray==null) { + logMessage("value is not a valid NTNDArray",true,true); return false; } ntndCodec.decompress(ntndArray); @@ -395,7 +419,7 @@ private boolean updateImage(PvaClientMonitorData monitorData) { if (ny == 0) ny = 1; // 1-D images which are OK, useful with dynamic profiler if (isDebugMessages) - logMessage("UpdateImage: got image, sizes: " + nx + " " + ny + " " + nz, true, true); + logMessage("UpdateImage: got image, sizes: " + nx + " " + ny + " " + nz,true,true); int numElements = nx * ny * nz; if (numElements == 0) { @@ -412,7 +436,7 @@ private boolean updateImage(PvaClientMonitorData monitorData) { // if image size changes we must close window and make a new one. boolean makeNewWindow = false; - if (nx != imageSizeX || ny != imageSizeY || nz != imageSizeZ || cm != colorMode || scalarType != dataType) + if (nx != imageSizeX || ny != imageSizeY || nz != imageSizeZ || cm != colorMode || scalarType !=dataType) { makeNewWindow = true; imageSizeX = nx; @@ -430,7 +454,7 @@ private boolean updateImage(PvaClientMonitorData monitorData) { if(img==null) makeNewWindow = true; // If we need to make a new window then close the current one if it exists - if (img != null && makeNewWindow) + if (img!=null && makeNewWindow) { try { @@ -443,14 +467,14 @@ private boolean updateImage(PvaClientMonitorData monitorData) { img.close(); } } - catch (Exception ex) - { + catch (Exception ex) { logMessage("Exception closing window " + ex.getMessage(), true, true); } makeNewWindow = false; } // If the window does not exist or is closed make a new one - if (img == null || img.getWindow() == null || img.getWindow().isClosed()) { + if (img == null || img.getWindow() == null || img.getWindow().isClosed()) + { switch (colorMode) { case 0: @@ -500,37 +524,33 @@ else if (dataType.isNumeric()) { } imagedata = pvUnionValue.get(PVScalarArray.class); if(imagedata==null) { - logMessage("value is not a scalar array", true, true); + logMessage("value is not a scalar array",true,true); return false; } if (colorMode == 0 || colorMode == 1) { - if (dataType == ScalarType.pvUByte) - { + if (dataType == ScalarType.pvUByte) { byte[] pixels = new byte[numElements]; convert.toByteArray(imagedata, 0, numElements, pixels, 0); img.getProcessor().setPixels(pixels); } - else if (dataType == ScalarType.pvUShort) - { + else if (dataType == ScalarType.pvUShort) { short[] pixels = new short[numElements]; convert.toShortArray(imagedata, 0, numElements, pixels, 0); img.getProcessor().setPixels(pixels); } else if (dataType.isNumeric()) { - float[] pixels = new float[numElements]; + float[] pixels =new float[numElements]; convert.toFloatArray(imagedata, 0, numElements, pixels, 0); img.getProcessor().setPixels(pixels); - } - else - { + } else { throw new RuntimeException("illegal array type " + dataType); } } else if (colorMode >= 2 && colorMode <= 4) { - int[] pixels = (int[]) img.getProcessor().getPixels(); + int[] pixels = (int[])img.getProcessor().getPixels(); byte inpixels[]=new byte[numElements]; @@ -548,7 +568,7 @@ else if (colorMode >= 2 && colorMode <= 4) if (idispMax) - colorLUT[i] = (byte) 255; + colorLUT[i] = (byte)255; else colorLUT[i] = (byte)((i-dispMin)*slope + 0.5); } @@ -859,7 +879,8 @@ private class FrameExitListener extends WindowAdapter { public void windowClosing(WindowEvent event) { isPluginRunning = false; // We need to wake up the main thread so it shuts down cleanly - synchronized (this) { + synchronized (this) + { notify(); } } @@ -889,7 +910,8 @@ private void readProperties() temp = properties.getProperty("channelName"); if (temp != null) channelName = temp; IJ.log("Read properties file: " + path + " channelName= " + channelName); - } catch (Exception ex) + } + catch (Exception ex) { IJ.log("readProperties:exception: " + ex.getMessage()); } @@ -912,4 +934,4 @@ private void writeProperties() logMessage("writeProperties:exception: " + ex.getMessage(), true, true); } } -} + } From 4314d037dd63a4f71dfd6ae552961daafb4b9e05 Mon Sep 17 00:00:00 2001 From: abrewe Date: Thu, 30 Jun 2022 12:40:23 -0500 Subject: [PATCH 5/8] final formats --- .../EPICS_areaDetector/EPICS_AD_Viewer.java | 46 ++++++++---------- .../EPICS_NTNDA_Viewer.java | 48 ++++++++++--------- 2 files changed, 45 insertions(+), 49 deletions(-) diff --git a/ImageJ/EPICS_areaDetector/EPICS_AD_Viewer.java b/ImageJ/EPICS_areaDetector/EPICS_AD_Viewer.java index a000a21..08c69bf 100755 --- a/ImageJ/EPICS_areaDetector/EPICS_AD_Viewer.java +++ b/ImageJ/EPICS_areaDetector/EPICS_AD_Viewer.java @@ -80,7 +80,6 @@ public class EPICS_AD_Viewer implements PlugIn boolean isNewStack; boolean isConnected; boolean isLogOn; - // boolean startedLog; volatile boolean isNewImageAvailable; javax.swing.Timer timer; @@ -106,8 +105,7 @@ public void run(String arg) if (isDebugFile) { - debugFile = new FileOutputStream(System.getProperty("user.home") + - System.getProperty("file.separator") + "IJEPICS_debug.txt"); + debugFile = new FileOutputStream(System.getProperty("user.home") + System.getProperty("file.separator") + "IJEPICS_debug.txt"); debugPrintStream = new PrintStream(debugFile); } javax.swing.SwingUtilities.invokeLater( @@ -317,12 +315,12 @@ public void checkConnections() try { connected = (ch_nx != null && ch_nx.getConnectionState() == Channel.ConnectionState.CONNECTED && - ch_ny != null && ch_ny.getConnectionState() == Channel.ConnectionState.CONNECTED && - ch_nz != null && ch_nz.getConnectionState() == Channel.ConnectionState.CONNECTED && - ch_colorMode != null && ch_colorMode.getConnectionState() == Channel.ConnectionState.CONNECTED && - ch_dataType != null && ch_dataType.getConnectionState() == Channel.ConnectionState.CONNECTED && - ch_image != null && ch_image.getConnectionState() == Channel.ConnectionState.CONNECTED && - ch_image_id != null && ch_image_id.getConnectionState() == Channel.ConnectionState.CONNECTED); + ch_ny != null && ch_ny.getConnectionState() == Channel.ConnectionState.CONNECTED && + ch_nz != null && ch_nz.getConnectionState() == Channel.ConnectionState.CONNECTED && + ch_colorMode != null && ch_colorMode.getConnectionState() == Channel.ConnectionState.CONNECTED && + ch_dataType != null && ch_dataType.getConnectionState() == Channel.ConnectionState.CONNECTED && + ch_image != null && ch_image.getConnectionState() == Channel.ConnectionState.CONNECTED && + ch_image_id != null && ch_image_id.getConnectionState() == Channel.ConnectionState.CONNECTED); if (connected && !isConnected) { isConnected = true; @@ -830,23 +828,19 @@ public void actionPerformed(ActionEvent event) }); captureCheckBox.addItemListener(new ItemListener() - { - public void itemStateChanged(ItemEvent e) - { - if (e.getStateChange() == ItemEvent.SELECTED) - { - isSaveToStack = true; - isNewStack = true; - IJ.log("record on"); - } - else - { - isSaveToStack = false; - IJ.log("record off"); - } - - } - } + { + public void itemStateChanged(ItemEvent e) { + if (e.getStateChange() == ItemEvent.SELECTED) { + isSaveToStack = true; + isNewStack = true; + IJ.log("record on"); + } else { + isSaveToStack = false; + IJ.log("record off"); + } + + } + } ); } diff --git a/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java b/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java index 5608aea..e10b852 100755 --- a/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java +++ b/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java @@ -127,7 +127,7 @@ public class EPICS_NTNDA_Viewer */ public EPICS_NTNDA_Viewer() { - String temp = null; + String temp=null; readProperties(); temp = System.getenv("EPICS_NTNDA_VIEWER_CHANNELNAME"); if (temp != null) { @@ -144,7 +144,7 @@ public void channelStateChange(PvaClientChannel channel, boolean connected) { if(isChannelConnected) { channelNameText.setBackground(Color.green); logMessage("State changed to connected for " + channelName, true, true); - if (pvaClientMonitor==null) { + if(pvaClientMonitor==null) { pvaClientMonitor=pvaClientChannel.createMonitor("record[queueSize=" + QUEUE_SIZE + "]field()"); pvaClientMonitor.issueConnect(); } @@ -181,7 +181,7 @@ private void disconnectPV() { try { - if(pvaClientChannel == null) { + if(pvaClientChannel==null) { throw new RuntimeException("Channel already disconnected"); } isChannelConnected = false; @@ -191,7 +191,8 @@ private void disconnectPV() pvaClientChannel = null; pvaClientMonitor = null; logMessage("Disconnected from EPICS PV:" + channelName, true, true); - } catch (Exception ex) { + } + catch (Exception ex) { logMessage("Cannot disconnect from EPICS PV:" + channelName + ex.getMessage(), true, true); } } @@ -204,7 +205,7 @@ private void startMonitor() private void stopMonitor() { - synchronized (this) { + synchronized(this) { pvaClientMonitor.stop(); isStarted = false; } @@ -335,26 +336,26 @@ private void makeImageCopy() private boolean updateImage(PvaClientMonitorData monitorData) { - Point oldWindowLocation = null; + Point oldWindowLocation =null; boolean madeNewWindow = false; PVStructure pvs = monitorData.getPVStructure(); - PVStructureArray dimArray = pvs.getSubField(PVStructureArray.class, "dimension"); + PVStructureArray dimArray = pvs.getSubField(PVStructureArray.class,"dimension"); if(dimArray==null) { - logMessage("dimension not found", true, true); + logMessage("dimension not found",true,true); return false; } int ndim = dimArray.getLength(); if(ndim<1) { - logMessage("dimension is empty", true, true); + logMessage("dimension is empty",true,true); return false; } int dimsint[] = new int[ndim]; StructureArrayData dimdata=new StructureArrayData(); dimArray.get(0,ndim,dimdata); for(int i=0; i=3) nz = dimsint[2]; int cm = 0; - PVStructureArray attrArray = pvs.getSubField(PVStructureArray.class, "attribute"); + PVStructureArray attrArray = pvs.getSubField(PVStructureArray.class,"attribute"); if(attrArray!=null) { int nattr = attrArray.getLength(); - StructureArrayData attrdata = new StructureArrayData(); - attrArray.get(0, nattr, attrdata); - for (int i = 0; i < nattr; i++) + StructureArrayData attrdata=new StructureArrayData(); + attrArray.get(0,nattr,attrdata); + for (int i = 0; i Date: Mon, 11 Jul 2022 01:37:18 -0500 Subject: [PATCH 6/8] increase efficiency --- .../EPICS_areaDetector/EPICS_AD_Viewer.java | 77 +++++++++++---- .../EPICS_NTNDA_Viewer.java | 95 ++++++++++++------- 2 files changed, 118 insertions(+), 54 deletions(-) diff --git a/ImageJ/EPICS_areaDetector/EPICS_AD_Viewer.java b/ImageJ/EPICS_areaDetector/EPICS_AD_Viewer.java index 08c69bf..575ad38 100755 --- a/ImageJ/EPICS_areaDetector/EPICS_AD_Viewer.java +++ b/ImageJ/EPICS_areaDetector/EPICS_AD_Viewer.java @@ -80,6 +80,7 @@ public class EPICS_AD_Viewer implements PlugIn boolean isNewStack; boolean isConnected; boolean isLogOn; + boolean firstLog; volatile boolean isNewImageAvailable; javax.swing.Timer timer; @@ -97,6 +98,7 @@ public void run(String arg) isSaveToStack = false; isNewStack = false; isLogOn = false; + firstLog=false; Date date = new Date(); prevTime = date.getTime(); numImageUpdates = 0; @@ -180,8 +182,13 @@ public void makeImageCopy() { ImageProcessor ip = img.getProcessor(); if (ip == null) return; - ImagePlus imgcopy = new ImagePlus(PVPrefix + ":" + ArrayCounter, ip.duplicate()); - imgcopy.show(); + if(isLogOn) { + logMessage("turn off log to use Snap function", true, true); + } + else { + ImagePlus imgcopy = new ImagePlus(PVPrefix + ":" + ArrayCounter, ip.duplicate()); + imgcopy.show(); + } } public void connectPVs() @@ -543,8 +550,17 @@ else if (i>dispMax) } if (isLogOn) { - snapBackup = takeLog(img); - if(dataType!= ScalarType.pvUShort && dataType!=ScalarType.pvUByte) resetContrast(img); + img.getProcessor().snapshot(); + snapBackup=img.getProcessor().getSnapshotPixels(); + if(ADDataType!=3 && ADDataType!=1) + log(img); + else { + img.getProcessor().log(); + } + if (firstLog){ + resetContrast(img); + firstLog = false; + } } if (isSaveToStack) { @@ -770,28 +786,32 @@ public void actionPerformed(ActionEvent event) logCheckBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { - isLogOn = true; - logMessage("Log display on", true, true); - if (!stopButton.isEnabled()) { - if (WindowManager.getCurrentWindow().equals(img.getWindow())) { - snapBackup = takeLog(img); - img.updateAndDraw(); - resetContrast(img); - } - else{ + if (colorMode >= 2 && colorMode <= 4) { + logMessage("Log not intended for color images", true, true); + logCheckBox.setSelected(false); + } else { + isLogOn = true; + logMessage("Log display on", true, true); + firstLog = true; + if (!stopButton.isEnabled()) { + if (WindowManager.getCurrentWindow().equals(img.getWindow())) { + snapBackup = takeLog(img); + img.updateAndDraw(); + resetContrast(img); + } else { + ImagePlus imgC = WindowManager.getCurrentImage(); + altSnapBackup = takeLog(imgC); + imgC.updateAndDraw(); + resetContrast(imgC); + } + } else if (!WindowManager.getCurrentWindow().equals(img.getWindow())) { ImagePlus imgC = WindowManager.getCurrentImage(); altSnapBackup = takeLog(imgC); imgC.updateAndDraw(); resetContrast(imgC); + isLogOn = false; } } - else if (!WindowManager.getCurrentWindow().equals(img.getWindow())){ - ImagePlus imgC = WindowManager.getCurrentImage(); - altSnapBackup = takeLog(imgC); - imgC.updateAndDraw(); - resetContrast(imgC); - isLogOn = false; - } } else { logMessage("Log display off", true, true); isLogOn = false; @@ -815,6 +835,9 @@ else if (!WindowManager.getCurrentWindow().equals(img.getWindow())){ Undo.undo(); resetContrast(imgC); } + else{ + resetContrast(img); + } } } }); @@ -850,9 +873,21 @@ private Object takeLog(ImagePlus image){ image.getProcessor().log(); return image.getProcessor().getSnapshotPixels(); } + private void log(ImagePlus image){ + float[] pixelArr = (float[]) image.getProcessor().getPixels(); + int i = 0; + for(float x : pixelArr){ + if(Math.abs(x - 0) < 1e-9) + pixelArr[i++]=Float.NEGATIVE_INFINITY; + else if(x<0) + pixelArr[i++]=Float.NaN; + else + pixelArr[i++]=(float)Math.log(x); + } + image.getProcessor().setPixels(pixelArr); + } private void resetContrast(ImagePlus image){ image.getProcessor().resetMinAndMax(); - if (image.getProcessor().getMin()<0) image.getProcessor().setMinAndMax(0, image.getProcessor().getMax()); new ContrastEnhancer().stretchHistogram(img, 0.5); } public class FrameExitListener extends WindowAdapter diff --git a/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java b/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java index e10b852..b781971 100755 --- a/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java +++ b/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java @@ -28,6 +28,9 @@ import javax.swing.JTextField; import javax.swing.border.EmptyBorder; +import com.sun.corba.se.pept.transport.ByteBufferPool; +import ij.*; +import ij.process.*; import org.epics.nt.NTNDArray; import org.epics.pvaClient.PvaClient; import org.epics.pvaClient.PvaClientChannel; @@ -46,20 +49,12 @@ import org.epics.pvdata.pv.ScalarType; import org.epics.pvdata.pv.StructureArrayData; -import ij.IJ; -import ij.ImagePlus; -import ij.ImageStack; import ij.gui.ImageWindow; import ij.gui.ImageCanvas; import ij.plugin.ContrastEnhancer; import ij.plugin.PlugIn; -import ij.process.ByteProcessor; -import ij.process.ColorProcessor; -import ij.process.FloatProcessor; -import ij.process.ImageProcessor; -import ij.process.ShortProcessor; -import ij.Undo; -import ij.WindowManager; + +import static ij.measure.Measurements.MIN_MAX; /** * ImageJ viewer for NTNDArray data. @@ -83,6 +78,7 @@ public class EPICS_NTNDA_Viewer private ImagePlus img = null; private Object snapBackup = null; private Object altSnapBackup = null; + private ImageStatistics stats = null; private ImageStack imageStack = null; private int imageSizeX = 0; private int imageSizeY = 0; @@ -103,6 +99,7 @@ public class EPICS_NTNDA_Viewer private volatile boolean isSaveToStack = false; private volatile boolean isNewStack = false; private volatile boolean isLogOn = false; + private volatile boolean firstLog = false; // These are used for the frames/second calculation private long prevTime = 0; private volatile int numImageUpdates = 0; @@ -329,8 +326,13 @@ private void makeImageCopy() { ImageProcessor ip = img.getProcessor(); if (ip == null) return; - ImagePlus imgcopy = new ImagePlus(channelName + ":" + numImageUpdates, ip.duplicate()); - imgcopy.show(); + if(isLogOn) { + logMessage("turn off log to use Snap function", true, true); + } + else { + ImagePlus imgcopy = new ImagePlus(channelName + ":" + numImageUpdates, ip.duplicate()); + imgcopy.show(); + } } @@ -619,8 +621,16 @@ else if (i>dispMax) /*Takes log of image, stores snapshot for Undo if plugin is stopped. */ if (isLogOn) { - snapBackup = takeLog(img); - if(dataType!= ScalarType.pvUShort && dataType!=ScalarType.pvUByte) resetContrast(img); + img.getProcessor().snapshot(); + snapBackup=img.getProcessor().getSnapshotPixels(); + if(dataType!=ScalarType.pvUShort && dataType!= ScalarType.pvUByte && dataType.isNumeric()) + log(img); + else + img.getProcessor().log(); + if (firstLog){ + resetContrast(img); + firstLog = false; + } } if (isSaveToStack) @@ -783,32 +793,36 @@ public void actionPerformed(ActionEvent event) } }); - //Turns log on and off. If plugin is stopped, log checkbox takes log or undoes log on static image or other image depending on user selection + // Turns log on and off. If plugin is stopped, log checkbox takes log or undoes log on static image or other image depending on user selection logCheckBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { - isLogOn = true; - logMessage("Log display on", true, true); - if (!stopButton.isEnabled()) { - if (WindowManager.getCurrentWindow().equals(img.getWindow())) { - snapBackup = takeLog(img); - img.updateAndDraw(); - resetContrast(img); - } - else{ + if (colorMode >= 2 && colorMode <= 4) { + logMessage("Log not intended for color images", true, true); + logCheckBox.setSelected(false); + } else { + isLogOn = true; + logMessage("Log display on", true, true); + firstLog = true; + if (!stopButton.isEnabled()) { + if (WindowManager.getCurrentWindow().equals(img.getWindow())) { + snapBackup = takeLog(img); + img.updateAndDraw(); + resetContrast(img); + } else { + ImagePlus imgC = WindowManager.getCurrentImage(); + altSnapBackup = takeLog(imgC); + imgC.updateAndDraw(); + resetContrast(imgC); + } + } else if (!WindowManager.getCurrentWindow().equals(img.getWindow())) { ImagePlus imgC = WindowManager.getCurrentImage(); altSnapBackup = takeLog(imgC); imgC.updateAndDraw(); resetContrast(imgC); + isLogOn = false; } } - else if (!WindowManager.getCurrentWindow().equals(img.getWindow())){ - ImagePlus imgC = WindowManager.getCurrentImage(); - altSnapBackup = takeLog(imgC); - imgC.updateAndDraw(); - resetContrast(imgC); - isLogOn = false; - } } else { logMessage("Log display off", true, true); isLogOn = false; @@ -832,6 +846,9 @@ else if (!WindowManager.getCurrentWindow().equals(img.getWindow())){ Undo.undo(); resetContrast(imgC); } + else{ + resetContrast(img); + } } } @@ -871,10 +888,22 @@ private Object takeLog(ImagePlus image){ image.getProcessor().log(); return image.getProcessor().getSnapshotPixels(); } + private void log(ImagePlus image){ + float[] pixelArr = (float[]) image.getProcessor().getPixels(); + int i = 0; + for(float x : pixelArr){ + if(Math.abs(x - 0) < 1e-9) + pixelArr[i++]=Float.NEGATIVE_INFINITY; + else if(x<0) + pixelArr[i++]=Float.NaN; + else + pixelArr[i++]=(float)Math.log(x); + } + image.getProcessor().setPixels(pixelArr); + } private void resetContrast(ImagePlus image){ image.getProcessor().resetMinAndMax(); - if (image.getProcessor().getMin()<0) image.getProcessor().setMinAndMax(0, image.getProcessor().getMax()); - new ContrastEnhancer().stretchHistogram(img, 0.5); + new ContrastEnhancer().stretchHistogram(image, 0.5); } private class FrameExitListener extends WindowAdapter { public void windowClosing(WindowEvent event) { From 630005d9faea0463defcec45aefca6137a1ba844 Mon Sep 17 00:00:00 2001 From: abrewe Date: Wed, 13 Jul 2022 11:09:55 -0500 Subject: [PATCH 7/8] fix compile error (hopefully) --- ImageJ/EPICS_areaDetector/EPICS_AD_Viewer.java | 3 ++- ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/ImageJ/EPICS_areaDetector/EPICS_AD_Viewer.java b/ImageJ/EPICS_areaDetector/EPICS_AD_Viewer.java index 575ad38..6b4acbc 100755 --- a/ImageJ/EPICS_areaDetector/EPICS_AD_Viewer.java +++ b/ImageJ/EPICS_areaDetector/EPICS_AD_Viewer.java @@ -71,6 +71,7 @@ public class EPICS_AD_Viewer implements PlugIn JButton startButton; JButton stopButton; JButton snapButton; + JCheckBox logCheckBox; boolean isDebugMessages; boolean isDebugFile; @@ -662,7 +663,7 @@ public void createAndShowGUI() stopButton.setEnabled(false); snapButton = new JButton("Snap"); JCheckBox captureCheckBox = new JCheckBox(""); - JCheckBox logCheckBox = new JCheckBox(""); + logCheckBox = new JCheckBox(""); frame = new JFrame("Image J EPICS_AD_Viewer Plugin"); JPanel panel = new JPanel(new BorderLayout()); diff --git a/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java b/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java index b781971..4ed88ca 100755 --- a/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java +++ b/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java @@ -28,9 +28,6 @@ import javax.swing.JTextField; import javax.swing.border.EmptyBorder; -import com.sun.corba.se.pept.transport.ByteBufferPool; -import ij.*; -import ij.process.*; import org.epics.nt.NTNDArray; import org.epics.pvaClient.PvaClient; import org.epics.pvaClient.PvaClientChannel; @@ -49,6 +46,8 @@ import org.epics.pvdata.pv.ScalarType; import org.epics.pvdata.pv.StructureArrayData; +import ij.*; +import ij.process.*; import ij.gui.ImageWindow; import ij.gui.ImageCanvas; import ij.plugin.ContrastEnhancer; @@ -116,6 +115,7 @@ public class EPICS_NTNDA_Viewer private JButton startButton = null; private JButton stopButton = null; private JButton snapButton = null; + private JCheckBox logCheckBox = null; private javax.swing.Timer timer = null; @@ -679,7 +679,7 @@ private void createAndShowGUI() stopButton = new JButton("Stop"); snapButton = new JButton("Snap"); JCheckBox captureCheckBox = new JCheckBox(""); - JCheckBox logCheckBox = new JCheckBox(""); + logCheckBox = new JCheckBox(""); frame = new JFrame("Image J EPICS_NTNDA_Viewer Plugin"); JPanel panel = new JPanel(new BorderLayout()); From 87d7be6d0eced8458f633d2ba0b769d0160cfe94 Mon Sep 17 00:00:00 2001 From: abrewe Date: Fri, 15 Jul 2022 09:13:28 -0500 Subject: [PATCH 8/8] Update log-snap behavior --- .../EPICS_areaDetector/EPICS_AD_Viewer.java | 50 ++++++++++--------- .../EPICS_NTNDA_Viewer.java | 10 ++-- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/ImageJ/EPICS_areaDetector/EPICS_AD_Viewer.java b/ImageJ/EPICS_areaDetector/EPICS_AD_Viewer.java index 6b4acbc..d267d46 100755 --- a/ImageJ/EPICS_areaDetector/EPICS_AD_Viewer.java +++ b/ImageJ/EPICS_areaDetector/EPICS_AD_Viewer.java @@ -184,7 +184,11 @@ public void makeImageCopy() ImageProcessor ip = img.getProcessor(); if (ip == null) return; if(isLogOn) { - logMessage("turn off log to use Snap function", true, true); + ImageProcessor ipcopy = ip.duplicate(); + ipcopy.setPixels(snapBackup); + ImagePlus imgcopy = new ImagePlus(PVPrefix + ":" + ArrayCounter, ipcopy); + resetContrast(imgcopy); + imgcopy.show(); } else { ImagePlus imgcopy = new ImagePlus(PVPrefix + ":" + ArrayCounter, ip.duplicate()); @@ -323,12 +327,12 @@ public void checkConnections() try { connected = (ch_nx != null && ch_nx.getConnectionState() == Channel.ConnectionState.CONNECTED && - ch_ny != null && ch_ny.getConnectionState() == Channel.ConnectionState.CONNECTED && - ch_nz != null && ch_nz.getConnectionState() == Channel.ConnectionState.CONNECTED && - ch_colorMode != null && ch_colorMode.getConnectionState() == Channel.ConnectionState.CONNECTED && - ch_dataType != null && ch_dataType.getConnectionState() == Channel.ConnectionState.CONNECTED && - ch_image != null && ch_image.getConnectionState() == Channel.ConnectionState.CONNECTED && - ch_image_id != null && ch_image_id.getConnectionState() == Channel.ConnectionState.CONNECTED); + ch_ny != null && ch_ny.getConnectionState() == Channel.ConnectionState.CONNECTED && + ch_nz != null && ch_nz.getConnectionState() == Channel.ConnectionState.CONNECTED && + ch_colorMode != null && ch_colorMode.getConnectionState() == Channel.ConnectionState.CONNECTED && + ch_dataType != null && ch_dataType.getConnectionState() == Channel.ConnectionState.CONNECTED && + ch_image != null && ch_image.getConnectionState() == Channel.ConnectionState.CONNECTED && + ch_image_id != null && ch_image_id.getConnectionState() == Channel.ConnectionState.CONNECTED); if (connected && !isConnected) { isConnected = true; @@ -455,7 +459,7 @@ else if (ADDataType == 3) { imageStack = new ImageStack(img.getWidth(), img.getHeight()); imageStack.addSlice(PVPrefix + ArrayCounter, img.getProcessor()); - // Note: we need to add this first slice twice in order to get the slider bar + // Note: we need to add this first slice twice in order to get the slider bar // on the window - ImageJ won't put it there if there is only 1 slice. imageStack.addSlice(PVPrefix + ArrayCounter, img.getProcessor()); img.close(); @@ -734,7 +738,7 @@ public void createAndShowGUI() frame.setVisible(true); - int timerDelay = 2000; // 2 seconds + int timerDelay = 2000; // 2 seconds timer = new javax.swing.Timer(timerDelay, new ActionListener() { public void actionPerformed(ActionEvent event) @@ -852,19 +856,19 @@ public void actionPerformed(ActionEvent event) }); captureCheckBox.addItemListener(new ItemListener() - { - public void itemStateChanged(ItemEvent e) { - if (e.getStateChange() == ItemEvent.SELECTED) { - isSaveToStack = true; - isNewStack = true; - IJ.log("record on"); - } else { - isSaveToStack = false; - IJ.log("record off"); - } - - } - } + { + public void itemStateChanged(ItemEvent e) { + if (e.getStateChange() == ItemEvent.SELECTED) { + isSaveToStack = true; + isNewStack = true; + IJ.log("record on"); + } else { + isSaveToStack = false; + IJ.log("record off"); + } + + } + } ); } @@ -889,7 +893,7 @@ else if(x<0) } private void resetContrast(ImagePlus image){ image.getProcessor().resetMinAndMax(); - new ContrastEnhancer().stretchHistogram(img, 0.5); + new ContrastEnhancer().stretchHistogram(image, 0.51); } public class FrameExitListener extends WindowAdapter { diff --git a/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java b/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java index 4ed88ca..e4c2a8a 100755 --- a/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java +++ b/ImageJ/EPICS_areaDetector/EPICS_NTNDA_Viewer.java @@ -327,7 +327,11 @@ private void makeImageCopy() ImageProcessor ip = img.getProcessor(); if (ip == null) return; if(isLogOn) { - logMessage("turn off log to use Snap function", true, true); + ImageProcessor ipcopy = ip.duplicate(); + ipcopy.setPixels(snapBackup); + ImagePlus imgcopy = new ImagePlus(channelName + ":" + numImageUpdates, ipcopy); + resetContrast(imgcopy); + imgcopy.show(); } else { ImagePlus imgcopy = new ImagePlus(channelName + ":" + numImageUpdates, ip.duplicate()); @@ -619,7 +623,7 @@ else if (i>dispMax) } /*Takes log of image, stores snapshot for Undo if plugin is stopped. - */ + */ if (isLogOn) { img.getProcessor().snapshot(); snapBackup=img.getProcessor().getSnapshotPixels(); @@ -965,4 +969,4 @@ private void writeProperties() logMessage("writeProperties:exception: " + ex.getMessage(), true, true); } } - } +}