Launching ImageJ from external program

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Launching ImageJ from external program

Jon Harman
Hi,

I use Irfanview to view a directory as thumbnails on my W2000 machine.  
I'd like to select an image then launch ImageJ.  Irfanview has the
ability to run an external image editor.  This feature works with
"native" windows programs, but fails with ImageJ, giving an error that
complains about missing ImageJ.cfg and launcher.cfg files.  Does anyone
know what is going on and if there is a fix?

Jon
Reply | Threaded
Open this post in threaded view
|

Re: Launching ImageJ from external program

Wayne Rasband
> I use Irfanview to view a directory as thumbnails on my W2000  
> machine.  I'd like to select an image then launch ImageJ.  
> Irfanview has the ability to run an external image editor.  This  
> feature works with "native" windows programs, but fails with  
> ImageJ, giving an error that complains about missing ImageJ.cfg and  
> launcher.cfg files.  Does anyone know what is going on and if there  
> is a fix?

The ImageJ Windows launcher (ImageJ.exe) expects its configuration  
file (ImageJ.cfg) to be in the current directory. Someone familiar  
with C++ could modify the launcher to look for the configuration file  
in the c:\Program Files\ImageJ directory if it can't find it in the  
current directory. The C++ source code for the launcher is available at

   http://www.rolemaker.dk/nonRoleMaker/javalauncher/ 
marner_java_launcher.htm

This fix would also allow extension associations (i.e.,  
assigning .tif to Image) to work.

-wayne
Reply | Threaded
Open this post in threaded view
|

Re: Launching ImageJ from external program

Gabriel Landini
In reply to this post by Jon Harman
On Sunday 15 January 2006 16:14, Jon Harman wrote:
> I use Irfanview to view a directory as thumbnails on my W2000 machine.
> I'd like to select an image then launch ImageJ.  Irfanview has the
> ability to run an external image editor.  This feature works with
> "native" windows programs, but fails with ImageJ, giving an error that
> complains about missing ImageJ.cfg and launcher.cfg files.  Does anyone
> know what is going on and if there is a fix?

This will not help :-/ but I have a tip that may come handy to any linux/KDE
users of IJ.
I made a script:

cd ~/ImageJ
./jdk1.5.0_05/bin/java -mx900m -cp
ij.jar:jimi.jar:/home/gabriel/ImageJ/jdk1.5.0_05/lib/tools.jar ij.ImageJ $1

Note the $1 at the end. This allows to use the script to send an image browsed
with konqueror (the KDE file browser) to IJ when using the "Image view"
browsing mode. (There are 3 modes: Icon, Tree and Image view in konqueror,
accessible from 3 icons in the toolbar).
(Just make sure that you change "gabriel" for your own home directory! :-)  )

To make konqueror launch IJ, one must run in the Control Centre:
KDE Components>File Associations and choose the types of images one wishes to
open with IJ.
Let's say you chose tiff, then:
In the "Application Preference Order" list, click "Add..." and go the the
Application tab. Set a name (I chose run_IJ); then in the Command box, use
the Browse button to navigate to the ImageJ folder and choose the script file
above. (then  Apply and do the same for any other image types).

Now each time one right-clicks on an image type set this way (handy in Image
view mode) there will be an entry "Open with..." and the IJ script should be
one of the options.

I hope it is useful.
Cheers,

Gabriel
Reply | Threaded
Open this post in threaded view
|

Re: Launching ImageJ from external program

Jeff Brandenburg
In reply to this post by Jon Harman
On Jan 15, 2006, at 11:14 AM, Jon Harman wrote:

> Hi,
>
> I use Irfanview to view a directory as thumbnails on my W2000 machine.  
>  I'd like to select an image then launch ImageJ.  Irfanview has the  
> ability to run an external image editor.  This feature works with  
> "native" windows programs, but fails with ImageJ, giving an error that  
> complains about missing ImageJ.cfg and launcher.cfg files.  Does  
> anyone know what is going on and if there is a fix?
>
> Jon
>

This question ("how do I get ImageJ to launch as a helper application  
in Windows?") is starting to come up more frequently on the list.  
Wayne has pointed out what needs to be done:

> The ImageJ Windows launcher (ImageJ.exe) expects its configuration  
> file (ImageJ.cfg) to be in the current directory. Someone familiar  
> with C++ could modify the launcher to look for the configuration file  
> in the c:\Program Files\ImageJ directory if it can't find it in the  
> current directory. The C++ source code for the launcher is available  
> at
>
>    
> http://www.rolemaker.dk/nonRoleMaker/javalauncher/ 
> marner_java_launcher.htm
>
> This fix would also allow extension associations (i.e., assigning .tif  
> to Image) to work.

The source code for the Marner Java launcher is pretty short, and seems  
simple and straightforward enough.

I wish someone with a Windows box, Developer Studio, and some Windows  
expertise would take a swing at this.  I've got a system here that  
launches ImageJ to do remote volume browsing, and it works like a charm  
on OS X, but it's embarrassing to tell Windows users that I can't make  
it work with their machines. :-/
--
        -jeffB (Jeff Brandenburg, Duke Center for In-Vivo Microscopy)
Reply | Threaded
Open this post in threaded view
|

Re: Launching ImageJ from external program

Hugo Gratama van Andel
Hello Jef,
I have looked in the code of JavaLauncher and it is easy to add some
lines to look in a specific directory like c:\Program Files\ImageJ.
But ... that is nog what you (I?) want. For instance I have ImageJ
installed in "C:\Program Files (x86)\ImageJ".
ImageJ could be installed everywhere, and you would like to let it work
in al ocassions. With an extra 'few more lines' it is also possible to
to find the ImageJ.cfg in the directory where the ImageJ.exe file (that
is executed) is located and to use that as the new working directory.
I have attached the source code. Would you like to receive a compiled
version?

The executable now works fine, but I notice a strange delay when opening
an image with extension associations (or in the "open with"-dialog). It
takes up to 12 seconds to launch ImageJ with the image. The cause of the
delay is not the executable but it is the javaw.exe (as can be seen in
the task manager, the executable ends directly, while the javaw.exe is
inactive for a time and then suddenly launches imageJ with the image)
When running the executable without an image it does launch directly.
Is ImageJ not used to be called with a specified image??

I hope that someone has an idea to solve the delay.
with regards,

Hugo Gratama van Andel

Jeff Brandenburg wrote:

>
> This question ("how do I get ImageJ to launch as a helper application  
> in Windows?") is starting to come up more frequently on the list.  
> Wayne has pointed out what needs to be done:
>
>> The ImageJ Windows launcher (ImageJ.exe) expects its configuration  
>> file (ImageJ.cfg) to be in the current directory. Someone familiar  
>> with C++ could modify the launcher to look for the configuration file  
>> in the c:\Program Files\ImageJ directory if it can't find it in the  
>> current directory. The C++ source code for the launcher is available  at
>>
>>    http://www.rolemaker.dk/nonRoleMaker/javalauncher/ 
>> marner_java_launcher.htm
>>
>> This fix would also allow extension associations (i.e., assigning
>> .tif  to Image) to work.
>
>
> The source code for the Marner Java launcher is pretty short, and seems  
> simple and straightforward enough.
>
> I wish someone with a Windows box, Developer Studio, and some Windows  
> expertise would take a swing at this.  I've got a system here that  
> launches ImageJ to do remote volume browsing, and it works like a charm  
> on OS X, but it's embarrassing to tell Windows users that I can't make  
> it work with their machines. :-/
> --
>     -jeffB (Jeff Brandenburg, Duke Center for In-Vivo Microscopy)
>
--
Hugo Gratama van Andel - MSc
Academic Medical Centre - University of Amsterdam
Dept. Medical Physics      L0-152
Meibergdreef 9
1105 AZ Amsterdam
The Netherlands
Phone  :  +31 20 566 5206
FAX    :  +31 20 691 7233
e-mail :  [hidden email]

/*
This is an application that can be used to launch Java
applications by just clicking on a exe file.

You can rename launcher.exe to anything you want.
*/

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <direct.h>
#include <stdlib.h>
#include <stdio.h>
#include <shellapi.h>

void deletenewlines(char* buffer)
{
  while(*buffer != '\0')
  {
    if (*buffer == '\n')
    {
      *buffer = '\0';
      return;
    }
    ++buffer;
  }
}

void getExecutable(char *result, char* lpCmdLine)
{ //returns the name of the executable appended with .cfg
  const char* constCommandLine = ::GetCommandLine();
  char* commandLine = new char[strlen(constCommandLine)+1];
  strcpy(commandLine,constCommandLine);
 
  // Remove lpCmdLine from back
  int lastpos = strlen(commandLine)-strlen(lpCmdLine);
  commandLine[lastpos] = '\0';
  lastpos--;
  while(commandLine[lastpos]=='\"' || commandLine[lastpos]==' ')
  {
    commandLine[lastpos]='\0';
    lastpos--;
  }  
  int firstpos = lastpos;
  while(commandLine[firstpos] > 0 && commandLine[firstpos]!='\\'
    && commandLine[firstpos]!=':' && commandLine[firstpos]!='/')
    firstpos--;

  strcpy(&result[0], &commandLine[firstpos+1]);
  int resultlength = strlen(result);
 
  bool containsdot = false;
  for (int i=0; i<resultlength; i++)
    if (result[i]=='.')
      containsdot = true;

  if (containsdot)
    while(result[resultlength]!='.')
      resultlength--;

  strcpy(&result[resultlength],".cfg");

  resultlength = strlen(result);  
  result[resultlength+1] = '\0';
}

void getExecutablePath(char *result, char* lpCmdLine)
{ // returns the path to the executable
  const char* constCommandLine = ::GetCommandLine();
  char* commandLine = new char[strlen(constCommandLine)+1];
  strcpy(commandLine,constCommandLine);

  // Remove lpCmdLine from back
  int lastpos = strlen(commandLine)-strlen(lpCmdLine);
  commandLine[lastpos] = '\0';
  lastpos--;
  while(commandLine[lastpos]!='\\')
  {
    commandLine[lastpos]='\0';
    lastpos--;
  }  
  int firstpos = 0;
  while(commandLine[firstpos]=='\"' || commandLine[firstpos]==' ')
    firstpos++;

  strcpy(&result[0], &commandLine[firstpos]);
  int resultlength = strlen(result);
  resultlength = strlen(result);  
  result[resultlength+1] = '\0';
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
  FILE* file = NULL;

  // Look for config file with same name as executable but with cfg suffix.
  char executable[1000];
  getExecutable(executable,lpCmdLine);
  char executablePath[1000];
  getExecutablePath(executablePath,lpCmdLine);
  char executablePathWithName[1000];
 
  file = fopen(executable,"rt"); // Check for the executable.cfg -file in the current working directory
  char cfgFileName[1000];
  if (file)
    strcpy(cfgFileName,executable);

  if (!file)
  { // Check for the cfg executable.file in the directory of the application
    sprintf(executablePathWithName,"%s%s",executablePath,executable);
        file = fopen(executablePathWithName,"rt");
    strcpy(cfgFileName, executablePath);
        _chdir(executablePath);
  }

  // Open default config file
  if (!file)
  {
    file = fopen("ImageJ.cfg","rt");
    strcpy(cfgFileName, "ImageJ.cfg");
  }

  if (file==NULL)
  {
    char temp[1000];
    sprintf(temp,"Could not find file %s or ImageJ.cfg. Please\n"
      "reinstall the application.", executable);
    MessageBox(NULL, temp,"Error loading configuration file", MB_OK);
    return 1;
  }

  char buffer[10000];

  // Read new directory from config file. We will change to this dir
  // before starting execution.
  fgets(buffer,10000,file);
  deletenewlines(buffer);
  int result = _chdir(buffer);

  if (result!=0)
  {
    char temp[1000];
    sprintf(temp, "Could not find the directory given in %s\n" \
      "Please reinstall the application.", cfgFileName);
    MessageBox(NULL, temp, "Error changing directory",MB_OK);
    return 2;
  }

  // Read the name of the executable. Is usually java.exe or javaw.exe
  char exe[1000];
  fgets(exe,1000,file);
  deletenewlines(exe);

  // Read any extra parameters you want to give to the program.
  // such as -jar parameters.
  char parameter[1000];
  fgets(parameter,1000,file);
  deletenewlines(parameter);

  // Add parameters given to the exe
  strcat(parameter," ");
  strcat(parameter, lpCmdLine);

  char thecwd[1000];
  _getcwd(thecwd,1000);
  if (ShellExecute(NULL,NULL,exe,parameter,thecwd,SW_SHOWNORMAL)<=(HINSTANCE)32)
  {
    MessageBox(NULL,
      "Could not start the application",
      "Please reinstall the application",MB_OK);
    return 2;
  }

  return 0;
}