From 2e1c36b7375e335fbcfe4a40225927b29e69d5fb Mon Sep 17 00:00:00 2001
From: Tejas Guruswamy <tguruswamy@anl.gov>
Date: Wed, 5 Apr 2023 19:05:24 -0500
Subject: [PATCH] Also build Fiji HDF5 plugins. New startup script

Download and install Fiji HDF5 plugins and JHDF5.
Improve plugin building overall
Include example startup script which sets LD_LIBRARY_PATH to ADSupport
Pull in Andrea Brewe's patches for Live Fitting and Log-scale GUI
---
 .gitignore                 | 12 +++--
 build_imagej+ad_plugins.sh | 98 ++++++++++++++++++++++++++++++++------
 start_imageJ               |  7 +++
 3 files changed, 100 insertions(+), 17 deletions(-)
 create mode 100755 start_imageJ

diff --git a/.gitignore b/.gitignore
index 0b0eeb8..d216d25 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,11 @@
+*~
+*.swp
+
 ImageJ/
 ADViewers-*/
-ij153-linux64-java8.zip
-ADViewers-R1-6-1.tar.gz
-ADViewers-R1-6-1/
+HDF5_Vibez-*/
+sis-jhdf5/
+*.zip
+*.tar.gz
+*.h5
+*.patch
diff --git a/build_imagej+ad_plugins.sh b/build_imagej+ad_plugins.sh
index a99a2da..7be6099 100644
--- a/build_imagej+ad_plugins.sh
+++ b/build_imagej+ad_plugins.sh
@@ -2,15 +2,26 @@
 # T. Guruswamy <tguruswamy@anl.gov>, 2020
 # Download and build ImageJ and ADViewers EPICS plugins
 
-IMAGEJ_VER=153
+# Select from http://wsr.imagej.net/distros/linux/
+IMAGEJ_VER="153"
 # Select from https://github.com/areaDetector/ADViewers/tags
 ADVIEWERS_VER="R1-7"
+# Select from https://github.com/fiji/HDF5_Vibez/tags
+HDF5PLUGIN_VER="HDF5_Vibez-1.1.1"
+# Select from https://unlimited.ethz.ch/display/JHDF/Download+Page
+JHDF5_VER="19.04.1"
 
-# Fetch ImageJ1
-# TODO: update if Java version changes
+# Add Andrea Brewe's patches to ADViewers
+ABREWE_PATCH=Y
+
+# Fetch ImageJ
 if [ ! -d ImageJ ]; then
     wget -c "http://wsr.imagej.net/distros/linux/ij${IMAGEJ_VER}-linux64-java8.zip"
     unzip -q "ij${IMAGEJ_VER}-linux64-java8.zip"
+    # Don't use git version, it's not built
+    #wget -c "https://github.com/imagej/ImageJ/archive/${IMAGEJ_VER}/ImageJ-v1.54d.tar.gz"
+    #tar xf "ImageJ-${IMAGEJ_VER}.tar.gz"
+    #mv "ImageJ-${IMAGEJ_VER#v}" ImageJ
 fi
 
 if [ ! -d ImageJ ]; then
@@ -19,10 +30,10 @@ if [ ! -d ImageJ ]; then
 fi
 
 # Fetch ADViewers, including ImageJ plugins
-# not necessary to use git, can just download tar.gz from github releases
 if [ ! -d ADViewers-${ADVIEWERS_VER} ]; then
     wget -c "https://github.com/areaDetector/ADViewers/archive/${ADVIEWERS_VER}/ADViewers-${ADVIEWERS_VER}.tar.gz"
     tar xf "ADViewers-${ADVIEWERS_VER}.tar.gz"
+    # Or you can use git version
     #git clone -q -b master https://github.com/areaDetector/ADViewers.git
     #git checkout "$ADVIEWERS_VER"
 fi
@@ -32,34 +43,93 @@ if [ ! -d ADViewers-${ADVIEWERS_VER} ]; then
     exit 64
 fi
 
+if [ -n $ABREWE_PATCH ]; then
+    if [ ! -f ADViewers-${ADVIEWERS_VER}/ImageJ/EPICS_areaDetector/LiveFitter_EPICSUserCalc.java ]; then
+    wget -O ADViewers-ABrewe-log_gui.patch -c "https://patch-diff.githubusercontent.com/raw/areaDetector/ADViewers/pull/22.patch"
+    wget -O ADViewers-ABrewe-live_fitting.patch -c "https://patch-diff.githubusercontent.com/raw/areaDetector/ADViewers/pull/23.patch"
+    pushd ADViewers-${ADVIEWERS_VER}
+        patch -f -r - -p1 < ../ADViewers-ABrewe-log_gui.patch
+        patch -f -r - -p1 < ../ADViewers-ABrewe-live_fitting.patch
+    popd
+    fi
+fi
+
 # Copy ADViewer plugins into ImageJ folder
 cp -a "ADViewers-${ADVIEWERS_VER}/ImageJ/EPICS_areaDetector" ImageJ/plugins/
 
+# Fetch Fiji HDF5 plugin
+if [ ! -d HDF5_Vibez-${HDF5PLUGIN_VER} ]; then
+    wget -c "https://github.com/fiji/HDF5_Vibez/archive/refs/tags/${HDF5PLUGIN_VER}.tar.gz"
+    tar xf "${HDF5PLUGIN_VER}.tar.gz"
+fi
+
+if [ ! -d HDF5_Vibez-${HDF5PLUGIN_VER} ]; then
+    echo "NO HDF5 Plugin found."
+    exit 64
+fi
+
+# Copy HDF5 plugins into ImageJ folder
+cp -a "HDF5_Vibez-${HDF5PLUGIN_VER}/src/main/java/sc/fiji/hdf5/" ImageJ/plugins/
+sed -i 's/package sc\.fiji\.hdf5/package hdf5/g' ImageJ/plugins/hdf5/*.java
+
+# If you want a test HDF5 file
+#wget -c "https://lmb.informatik.uni-freiburg.de/resources/opensource/imagej_plugins/samples/pollen.h5"
+#cp -a pollen.h5 ImageJ/plugins/hdf5/
+
+# Fetch JHDF5 library
+if [ ! -d sis-jhdf5 ]; then
+    wget --content-disposition -c "https://unlimited.ethz.ch/download/attachments/92867433/sis-jhdf5-${JHDF5_VER}.zip?version=1&api=v2"
+    unzip -q "sis-jhdf5-${JHDF5_VER}.zip"
+fi
+
+if [ ! -d sis-jhdf5 ]; then
+    echo "NO JHDF5 library found."
+    exit 64
+fi
+
+# Copy JHDF5 library into ImageJ folder
+cp -a sis-jhdf5/lib/*.jar ImageJ/plugins/hdf5/
+
 # Compile all plugins
 pushd ImageJ
 
-base="$(pwd)"
+# Not all files are plugins. Look for 'implements PlugIn' in source
 plugins=(
-    "EPICS_AD_Viewer.java" 
-    "EPICS_AD_Controller.java" 
-    "EPICS_NTNDA_Viewer.java" 
-    "Gaussian_Profiler.java" 
-    "Dynamic_Profiler.java"
+    "EPICS_areaDetector/EPICS_AD_Viewer.java" 
+    "EPICS_areaDetector/EPICS_AD_Controller.java" 
+    "EPICS_areaDetector/EPICS_NTNDA_Viewer.java" 
+    "EPICS_areaDetector/Gaussian_Profiler.java" 
+    "EPICS_areaDetector/Dynamic_Profiler.java"
+    "hdf5/HDF5_Reader_Vibez.java"
+    "hdf5/HDF5_Simple_Custom_Reader.java"
+    "hdf5/HDF5_Simple_Reader.java"
+    "hdf5/HDF5_Simple_Writer.java"
+    "hdf5/HDF5_Writer_Vibez.java"
+    "hdf5/Vibez_Validate.java"
 )
 
+if [ -n $ABREWE_PATCH ]; then
+plugins+=( "EPICS_areaDetector/LiveFitter_EPICSUserCalc.java" )
+fi
+
 for plugin in "${plugins[@]}"; do
     echo "Compiling ${plugin}"
+    plugin_basedir=$(dirname ./plugins/${plugin})
+    # The classpath is constructed from every *.jar file in the plugin folder. Do not include .jars from *other* plugins!
+    PLUGIN_CLASSPATH="$(find ./plugins/jars ${plugin_basedir} -name "*.jar" -print | tr '\n' ':')"
+    PLUGIN_CLASSPATH="./ij.jar:${plugin_basedir}/:./plugins/:${PLUGIN_CLASSPATH%:}"
+    #echo "PLUGIN_CLASSPATH= ${PLUGIN_CLASSPATH}"
     # This command was extracted from the ImageJ logs after manually running "Plugins -> Compile and Run..."
-    # The classpath is constructed from every *.jar file in the plugins/jars and plugins/EPICS_areaDetector folders
-    javac -source 1.8 -target 1.8 -Xlint:unchecked -deprecation \
-        -classpath "${base}/ij.jar:${base}/plugins/EPICS_areaDetector:${base}/plugins/jars/Auto_Threshold.jar:${base}/plugins/jars/BeanShell.jar:${base}/plugins/EPICS_areaDetector/epics-ntypes-0.3.5.jar:${base}/plugins/EPICS_areaDetector/epics-pvaccess-5.1.5.jar:${base}/plugins/EPICS_areaDetector/epics-pvaclient-4.3.2.jar:${base}/plugins/EPICS_areaDetector/epics-pvdata-6.1.5.jar:${base}/plugins/EPICS_areaDetector/epics-util-1.0.3.jar:${base}/plugins/EPICS_areaDetector/jca-2.4.5.jar:${base}/plugins/EPICS_areaDetector/jna-5.1.0.jar" \
-        "${base}/plugins/EPICS_areaDetector/${plugin}"
+    # If you are having compilation issues, add -verbose
+    javac -source 1.8 -target 1.8 -Xlint:unchecked -deprecation -classpath "${PLUGIN_CLASSPATH}" "plugins/${plugin}"
 done
 
 popd
 
+# Detector Pool pre-configured ImageJ profiles for specific detectors
 if [ -d "ImageJ_Profiles" ]; then
     cp -a "ImageJ_Profiles" "ImageJ"
 fi
 
+# Make permissions consistent
 chmod -R ug+rwX,o+rX ImageJ
diff --git a/start_imageJ b/start_imageJ
new file mode 100755
index 0000000..72c2179
--- /dev/null
+++ b/start_imageJ
@@ -0,0 +1,7 @@
+#!/bin/bash
+cd ${HOME}/ImageJ
+export EPICS_CA_MAX_ARRAY_BYTES=30000000
+export EPICS_CA_AUTO_ADDR_LIST=NO
+export EPICS_CA_ADDR_LIST=127.0.0.1
+export LD_LIBRARY_PATH=${HOME}/epics/synApps-6.2.1/support/areaDetector-R3-12-1/ADSupport/lib/linux-x86_64/
+./ImageJ -run "EPICS NTNDA Viewer" &
-- 
GitLab