Re: Copy To System and asynchronicity

Posted by Fred Damen on
URL: http://imagej.273.s1.nabble.com/Copy-To-System-and-asynchronicity-tp5023888p5023901.html

Greetings Michael,

Thanks for the reply...

I tried your suggestions of using IJ.selectWindow(imageID) with
NonBlockingGenericDialog and with GenericDialog, and the results are the
same as before.  At the moment, all concoctions, about 20% of the time the
snap(shot) of the window contains the part of the GenericDialog that was
overlaying the window when I clicked OK.  The visual screen, i.e., what I
see with my eyeballs, appears to be updated correctly before the results
of the screen shot are displayed, i.e., an ImagePlus with the results.
Increasing the IJ.wait time just extends the time between visual screen
update and image being produced, i.e., does not change the frequency of
this anomaly's occurrence.  I suspect that double buffering of the
screen's contents is going on, and the completion thereof is being blocked
stochastically; I would have suspected that new Robot().waitForIdle();
would have rectified this, but no joy.

The main impetus of writing this snap shot functionality is to capture
nonimage windows, namely GenericDialogs for putting in documentation
webpages; OS adornments included.  The impetus for the plugin itself is
that it is possible to adjust a window's width to zero - preventing it
from being seen, and, having a plethora of windows open such that the main
window Window menu extends past the bottom of the screen, rendering
selecting of the window impossible.  I have included the plugin in case
someone can spot if I am doing something wrong...

Thanks again,

Fred

import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import java.awt.datatransfer.*;
import java.io.*;
import java.util.*;
import ij.plugin.*;
import ij.plugin.frame.*;

public class Find_Window implements PlugIn {

   public void run(String arg) {
      Vector<String> itv = new Vector<String>();
      itv.add("<choose>");
      int[] ids = WindowManager.getIDList();
      if (ids != null)
         for(int id : ids)
            itv.add(WindowManager.getImage(id).getTitle());

      Vector<String> wtv = new Vector<String>();
      wtv.add("<choose>");
      Window[] ws = WindowManager.getAllNonImageWindows();
      if (ws != null)
         for(Window w : ws)
            wtv.add(w instanceof Frame ? ((Frame)w).getTitle() :
((Dialog)w).getTitle());

      GenericDialog gd = new NonBlockingGenericDialog("Find Windows");
      gd.addChoice("Find Image:", itv.toArray(new String[0]), "");
      gd.addMessage("Applies only to ImageWindow(s)");
      gd.addCheckbox("Unlock",false);
      gd.addMessage("  ");
      gd.addChoice("Find Frame:", wtv.toArray(new String[0]), "");
      gd.addMessage("Applies to all Window(s)\n ");
      gd.addCheckbox("Here",true);
      gd.addCheckbox("Raise",true);
      gd.addCheckbox("100%",true);
      gd.addCheckbox("Snap",false);
      gd.showDialog();
      if (gd.wasCanceled()) return;
      //gd.setVisible(false);

      int iind = gd.getNextChoiceIndex();
      boolean unlock = gd.getNextBoolean();
      boolean here   = gd.getNextBoolean();
      boolean raise  = gd.getNextBoolean();
      boolean zoom   = gd.getNextBoolean();
      boolean snap   = gd.getNextBoolean();
      if (iind > 0) {
         ImagePlus imp = WindowManager.getImage(ids[iind-1]);
         if (unlock) imp.unlock();
         ImageWindow win = imp.getWindow();
         if (here)   win.setLocation(gd.getLocation());
         if (zoom)   win.getCanvas().zoom100Percent();
         if (raise)  IJ.selectWindow(imp.getID()); //win.toFront();
         ImageWindow.setNextLocation(gd.getLocation());
         if (snap)   { IJ.wait(1000); snapAndDisplay(win); }
         }

      int wind = gd.getNextChoiceIndex();
      if (wind > 0) {
         Window win = ws[wind-1];
         if (here)   win.setLocation(gd.getLocation());
         if (zoom)
            if (win instanceof GenericDialog) ((GenericDialog)win).pack();
                                         else win.setSize(500,500);
         ImageWindow.setNextLocation(gd.getLocation());
         if (raise)  win.toFront();
         if (snap)   { IJ.wait(1000); snapAndDisplay(win); }
         }
      }

   public static void snapAndDisplay(Window win) {
      ImagePlus imp = snap(win);
      if (imp == null) return;
      imp.show();
      clipboard(imp);
      }
   public static ImagePlus snap(Window win) {
      try {
         Rectangle r = new Rectangle(win.getLocation(),win.getSize());
         //new Robot().mouseMove(win.getX()+10,win.getY()+10);
         new Robot().waitForIdle();
         //win.setVisible(true);
         //win.requestFocus();
         //boolean flag = win.isAlwaysOnTop();
         //win.setAlwaysOnTop(true);
         ImagePlus imp = new ImagePlus("Snapshot",new
Robot().createScreenCapture(r));
         //win.setAlwaysOnTop(flag);
         return imp;
         }
      catch(Exception e) {
         IJ.showStatus("Could not grab snapshot of window");
         }
      return null;
      }
   public static void clipboard(final ImagePlus imp) {
      Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
         new Transferable() {
            @Override public DataFlavor[] getTransferDataFlavors() {
                      return new DataFlavor[] { DataFlavor.imageFlavor };
                      }
            @Override public boolean isDataFlavorSupported(DataFlavor
flavor) {
                      return DataFlavor.imageFlavor.equals(flavor);
                      }
            @Override public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException {
                      if (!isDataFlavorSupported(flavor))
                         throw new UnsupportedFlavorException(flavor);
                      return imp.getImage();
                      }
            }, null);
      }
}


On Mon, August 31, 2020 4:53 am, Michael Schmid wrote:

> Hi Fred,
>
> concerning:
>  > I have noticed that the processing of the screen rendering of the
>  > GenericDialog, specifically the removal thereof from the screen,
>  > and the return of the showDialog method are asynchronous of each
>  > other.  Is there a way to make sure the GenericDialog window has
>  > been removed from the screen?
>
> It is a property of Java that changes to the GUI on the screen are
> asynchronous; it is rather difficult to avoid this in ImageJ. There is
> such code for images brought into the foreground, but it has been the
> root for many problems (that are solved by now, as far as I can say). If
> possible at all, it would be a pain to implement something like this for
> the GenericDialog.
>
> For your application of capturing the screen, the easiest would be a
> short delay (which is not a very clean way of doing it).
> If you have a NonblockingGenericDialog (which you can get for most
> filters, enable it in the Misc options), you could try to explicitly put
> the image window into the foreground with IJ.selectWindow(imageID). This
> should wait until the image is really there. I did not try it however,
> and I fear that it would not work for 'normal' (non-blocking)
> GenericDialogs.
> Instead of the screenshot, you might also try Plugins>Utilities>Capture
> Image.
>
> -----
>
> Concerning the 'copy to system' race condition, I can confirm it.
> Javascript code:
>
>    impB = IJ.openImage("https://imagej.net/images/blobs.gif");
>    impB.show();
>    Thread.sleep(1000);
>    impP = IJ.openImage("https://imagej.net/images/particles.gif");
>    impP.show();
>    Thread.sleep(1000);
>    IJ.run("Copy to System", "");   //should copy the particles
>    IJ.selectWindow(impB.getID());
>    Thread.sleep(1000);
>    IJ.run("System Clipboard", ""); //shows the blobs, not the particles
>
> Michael
> ________________________________________________________________
> On 30.08.20 05:38, Fred Damen wrote:
>> Greeting Wayne,
>>
>> An annoyance/bug and a question.
>>
>> With a ImageWindow current/active and the Edit>Copy to System is invoked
>> and then no other ImageJ window becomes active the contents of the
>> aforementioned ImageWindow gets placed into the system clipboard. If
>> another ImageJ window gets activated, i.e., made current, a short time
>> later (as dictated by current system conditions and possible OS
>> peculiarities) the newly current window is what is put on the system
>> clipboard.  As these are asynchronous method calls, the current
>> ImageWindow can, and in my case does, change in between
>> ij.plugin.Clipboard.scopy calling system Clipboard.setContents and the
>> system Clipboard calling ij.plugin.Clipboard.getTransferData, n.b.
>> getTransfterData refetches the current window. Caching the image before
>> the call to setContents and use thereof in getTransferData should
>> rectify
>> this issue.
>>
>> I have noticed that the processing of the screen rendering of the
>> GenericDialog, specifically the removal thereof from the screen, and the
>> return of the showDialog method are asynchronous of each other.  Is
>> there
>> a way to make sure the GenericDialog window has been removed from the
>> screen?  I have written a method that resizes and raises an ImageJ
>> image/nonimage window and then using Robot to take a snapshot of this
>> window, OS adornments included; although about half the time the
>> GenericDialog has not yet been removed from the screen and is covering
>> window, thus corrupting the snapshot.  I have tried putting waits in,
>> but
>> to no avail.  I suspect that the thread that was displaying the
>> GenericDialog is being blocked by the thread that had called the
>> showDialog, n.b., OS is Fedora 31.
>>
>> Thanks in advance,
>>
>> Fred
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html