Here's the (rather long) e-mail thread about problems using "viewmat" and saving the resulting display within a loop or otherwise within a program. It's over 700 lines and the solution appears only some point after line 600 (thanks to Dave Mitchell). However, I've included the full thread so we have a record of how people approached the problem and what was tried unsuccessfully before a solution was reached.

The much shorter summary solution can be found here.

Only the first full header is included. All replies are indicated by a brief mention of the replier's name "to Programming" like this:

___________________________________

Devon McCormick to Programming
=============== 

This thread ran from "Sun, Dec 14, 2008 at 12:54 PM" until the final summary message on "Tue, Dec 16, 2008 at 9:42 AM".

from    Roelof K Brouwer <rkbrouwer@gmail.com>
to      programming@jsoftware.com
date    Sun, Dec 14, 2008 at 12:54 PM
subject [Jprogramming] savemat_jviewmat_
        
I have problems with viewmat save in vista 64bit. In particular the
following two statements

( 1 4 14 {". COLOR16) viewmat i.4 4

savemat_jviewmat_ 'C:\Temp\garbage.bmp'

 this will give a blank window when used in a verb and also in ijx when debug is on.

Sometimes by adjusting the viewmat window I will get the proper display.
i.e. click the margin of the window for exdpansion. However sometimes on
expansion the window does not have the correct

displayI

if I remove the save command things will work

when in ijx I can first run viewmat command and then the save command and
things will work

I also tried puttting a delay between the two viewmat commands

here is my complete file with verb

drivedata=: 'C:\Users\RKBrouwer\Documents\ACA\Research\ResData\'
drive_op=:'C:\Users\RKBrouwer\Documents\Temp\Set\'
drive=:'C:\Users\RKBrouwer\Documents\ACA\'

require 'viewmat color16 plot'
load 'C:\Users\RKBrouwer\Documents\J_Code_Roelof\J_utilities_roelof.ijs'
NB. load
'C:\Users\RKBrouwer\Documents\J_utilities_roelof\visual_clustering.ijs'

require 'jfiles'

test_viewmat=:3 : 0
               ( 1 4 14 {". COLOR16) viewmat i.4 4
               savemat_jviewmat_ 'C:\Temp\garbage.bmp'
)

cheers

Roelof

"Have a cool mind and a warm heart" - Ajahn Sona

 "Like ......(hamsters on a wheel), (ambitious people) still climb and
climb, with great labor, and incessant anxiety, but never reach the top."
Robert Browning

"We shall not cease from exploration. And at the end of all our exploring
Will be to arrive were we started and know the place for the first time" -
T. S. Eliot

Dr. Roelof K. Brouwer PH.D.  P.Eng.
Professor Emeritus
Dept. of Computing Science
School of Advanced Technologies and Mathematics
Thompson Rivers University
900 McGill Road
Kamloops, BC
Canada, V2C 5N3
email :  <mailto:rkbrouwer@ieee.org> rkbrouwer@ieee.org

 <http://www.tru.ca/advtech/compsci/faculty/rbrouwer>
www.tru.ca/advtech/compsci/faculty/rbrouwer
___________________________________

Devon McCormick to Programming
=============== 
Dr. Brouwer - I think this is not so much a Vista 64-bit problem but a general problem with the event-loop/message-pump handling.

I've encountered this problem before: I have a sequence of lines of code that work fine in immediate execution but fail when they're in a function.
I believe the problem is that viewmat needs to get to a state of waiting for user input before it will finish displaying its picture.  This doesn't happen within multiple lines in a function, so only a blank picture is saved.

I've tried lots of different ways to get around this and have settled on using the "image3" add-on to write my images instead of relying on viewmat.  This is too bad because viewmat does offer a nice display of complex numbers that I haven't replicated with image3 code.

This problem is also mentioned by Dan Bron at http://www.jsoftware.com/jwiki/DanBron/Snippets/PrettyPictures and even Oleg doesn't have a solution.

Regards,

Devon
-- 
Devon McCormick, CFA
^me^ at acm.
org is my
preferred e-mail
___________________________________

Oleg Kobchenko to Programming
=============== 
        
You are right about giving a chance for viewmat to update the screen.
For sequence of images I suggested animate addon in the link below.

Also you may try wd'msgs'.


Oleg
___________________________________

Devon McCormick to Programming
=============== 
        
I've tried a number of variations on using "wd 'msgs'" with no success.  I've also noticed that playing around with this will eventually start messing up my J session: response to a line of input will defer until the following "Enter" and spurious form feeds (12{a.) will start popping up in my Jconsole session (under emacs).
___________________________________

Roelof K Brouwer to Programming
=============== 
        
You are right in that most of the time when I start to expand the window I
will get the right image. However sometimes even with that action  I get the
wrong image ( vague , hazy lines as opposed to squares. Also in ijx with
debug on which I used to stop my verb before the savemat action I get the
same result in the ijx window.

My images are simply like density plots and I will look at image3 addon. Is
it a matter of replacing 2 lines with 2 lines in my verb or something much
more complex ( nothing to do with complex numbers) than that.

cheers

Roelof
...
___________________________________

Devon McCormick to Programming
=============== 
        
Roelof - here's an example

   load 'images'
   arbimage=. <.20*+/~2 o. i:3j999   NB. Arbitrary image: 1000x1000
   (<./,>./),arbimage                NB. Integers from _40 to 39
_40 39

   arbimage{~,&.>~495+i.5 5          NB. High values in center
39 39 39 39 39
39 39 39 39 39
39 39 39 39 39
39 39 39 39 39
39 39 39 39 39
   5 5{.arbimage                     NB. Low values in corners
_40 _40 _40 _40 _40
_40 _40 _40 _40 _40
_40 _40 _40 _40 _40
_40 _40 _40 _40 _40
_40 _40 _40 _40 _40

   makeGrayPal=: [: |: 3 # [: ,: [: <. 0.5 + i. * 255 % <:
   NB.* makeGrayPal: make gray-scale palette with y entries.
   $pal100=. makeGrayPal 100
100 3
   (3&{.,_3&{.) pal100       NB. palette ranges from black to white
  0   0   0
  3   3   3
  5   5   5
250 250 250
252 252 252
255 255 255

NB. Write an image file using the palette.  
   (pal100{~arbimage-<./,arbimage) write_image 'C:\temp\pseudoDensity.png'
73624

NB. ".png" is efficient format for generated images; figures type from suffix:
   (pal100{~arbimage-<./,arbimage) write_image 'C:\temp\pseudoDensity.jpg'
191578

NB. ".jpg" takes 191K, ".png" only 74K.

NB. File will look like this:
   (79{.pal100) viewmat arbimage
NB. where we use "79" because
   (>./-<./),arbimage
79
___________________________________

Dan Bron to Programming
=============== 

Devon wrote:
>  a general problem with the event-loop/message-pump handling.
>  I have a sequence of lines of code that work fine in
>  immediate execution but fail when they're in a function.

As a last-ditch effort, you might try using a (latent) immex phrase.  This
is already packaged up nicely by  runimmx1_jijs_  .  Try  runimmx1_jijs_ '
''Hello, World!'' '  .

-Dan

PS:  See also the documentation for 9!:26, 9!:27, 9!:28, 9!:29 :
    http://www.jsoftware.com/help/dictionary/dx009.htm#26
___________________________________

Roelof K Brouwer to Programming
=============== 
        
Thanks
That will save me the time of struggling with another potential solution.

cheers

Roelof
...
___________________________________

Roelof K Brouwer to Programming
=============== 
        
Thanks
I am going to try this.
Ps the goal here is to produce an image that can easily be pasted into a
word document where it can be resized also if necc.

cheers

Roelof
...
___________________________________

Roelof K Brouwer to Programming
=============== 
                
Roelof K Brouwer        
I assume you mean that I should stick that before the saveviewmat command. ( ...
___________________________________

Roelof K Brouwer to Programming
=============== 

load 'images' gives me a file error
is it an addon or something

cheers
Roelof
...
___________________________________

Devon McCormick to Programming
=============== 
        
Yes - this is the "image3" addon.
___________________________________

Devon McCormick to Programming
=============== 
        
Dan - this doesn't solve the problem.  What he's trying to do is something I brought up at an NYCJUG meeting in May - i.e. an expression like this:

   savemat_jviewmat_ 'C:\Temp\immex0.bmp'[wait 1[viewmat i. 16 16

displays and saves a blank viewmat image.  On the other hand, entering

   viewmat i. 16 16
   savemat_jviewmat_ 'C:\Temp\immex0.bmp'

in immediate execution works fine.

This is frustrating for me because I tend to develop in immediate execution and rely on the behavior remaining the same when I gather together the working lines from the session and put them into a function definition.  In this case, I use the "image3" functions for a workaround.
- Show quoted text -


On Mon, Dec 15, 2008 at 4:40 PM, Dan Bron <j@bron.us> wrote:

    I wrote:
    >  you might try using a (latent) immex phrase.

    Roelof responded:
    >  I assume you mean that I should stick that before the saveviewmat command.

    Sorry for being unclear.

    I didn't read the thread in detail, but I got the impression that you have
    some asynchronous GUI effects that need to be run from immediate execution
    to work properly.  This problem has arisen before.

    A common (to use the word generously) solution is to use the "immex phrase"
    (latent expression) foreigns.  These foreigns permit you to schedule a
    sentence to execute the next time J becomes free.  That is, when J is done
    with all its other work, just before it returns to immediate execution
    (the idle state where it awaits commands), it will execute your sentence.

    The foreigns are already packaged up neatly by the standard verb
    runimmx1_jijs_  .  So what I'm suggesting is that you replace
    savemat_jviewmat_ blah  with  runimmx1_jijs_ 'savemat_jviewmat_ blah'  in
    your verb.

    If you do so, you must keep in mind that the  savemat_jviewmat_  will be
    executed *after your verb completes*, so the remainder of your verb (or
    script, or anything else) cannot depend upon its results or side effects.
    Also remember you can only have one immex phrase (but of course it can do
    more than one thing).

    Does that help?

    -Dan
    ----------------------------------------------------------------------
    For information about J forums see http://www.jsoftware.com/forums.htm

___________________________________

Dan Bron  to Programming
=============== 
        
Roelof wrote:
>  I tried the above and I have the same problem as before

Yes, I too get a blank image & file.  I don't get the freeze though.  Sorry
for not testing my suggestion before I made it.

I wonder why  9!:27'theSentence'  is different from executing  theSentence
directly in the IJX window.  My suspicion is that control doesn't pass
back from the J engine (j.dll) to the JFE (j.exe) until after the immex
phrase has completed.

That is, in the situation where you enter a phrase in the IJX window, press
enter, then enter another phrase, then enter again, from the first enter
to the second, control has passed from j.dll to j.exe and back to j.dll.
I speculate that in the case of  9!:27  j.dll checks the immex flag before
it returns control to j.exe (executing a phrase if appropriate), and so
this round trip is avoided.  However, in Roelof's case, the transfer of
control is required, as j.exe has the responsiblity of painting graphics.

Which makes me think his aims might be acheived via a different indirection:

       vm =: verb define
               viewmat i. 16 16
               wd 'timer 500'
               sys_timer_z_ =:  'timer 0' wd@:[ savemat_jviewmat_ bind 'C:\immex0.bmp'
       )

       vm''

This works, which seems like corroboration of my hypothesis (j.exe owns wd,
and hence control must pass back to it before timer events can fire, so we
get the requisite round trip).

___________________________________

Devon McCormick to Programming
=============== 
        
Dan - what's "bind" do?  I don't have this defined.

Regards,

Devon
___________________________________

Devon McCormick to Programming
=============== 
        
Is it this one?

bind=: 2 : 'x@(y"_)'

from stlib - guess I did have it defined but the function gets a syntax error anyway on the assignment of sys_timer_.
___________________________________

Roelof K Brouwer to Programming
=============== 
        
btw when I do the following I have the same problem

ex=: 3 : 0
viewmat i. 16 16
stop=.
savemat_jviewmat_ 'C:\Temp\immex0.bmp'
)

run ex
run debug
execute ex 0
then execute
savemat_jviewmat_ 'C:\Temp\immex0.bmp'
in the ijx window while in debug mode

cheers

Roelof
...
___________________________________

Henry Rich  to Programming
=============== 
        
That's an old-style conjunction.  Isn't it the same as

bind =: @:

?

Henry Rich
___________________________________

Roelof K Brouwer to Programming
=============== 
        
what other files have to be loaded for this to work? I did what you
suggested and get a syntax error followed by a crash. Even with debug on
later to see where the problem is

cheers

Roelof
...
___________________________________

Roelof K Brouwer to Programming
=============== 
        
Hello Dan
For me it causes J to stop working after going into a loop. I copied your
verb over verbatim. I do get the display however.
BTW I use vista 64

cheers

Roelof
...
___________________________________

Dan Bron to Programming
=============== 
        
Devon wrote:
>  Is it this one?
>  bind=: 2 : 'x@(y"_)'
>  from stlib

Yep, that's the one.

>  the function gets a syntax error anyway on
>  the assignment of sys_timer_.

I can't reproduce this.  What version are you running?  Also, the verb I posted had only 3 lines; the last line started with
sys_timer_z_  and ended with  'C:\immex0.bmp'  so watch for wrap.

Henry wrote:
>  That's an old-style conjunction. Isn't it the same as
>  bind =: @:

Nope.  In this case,  y  is a noun, not a verb.  Today, we would probably define   bind =: 2 : 'u@(n"_)'  .

Concretely,  bind  fixes an argument to a monadic verb.  For example, given  S =: smoutput bind 'hello'  the invocation  S y  will
always   smoutput 'hello'  regardless of the value of  y  .

Roelof wrote:
>  what other files have to be loaded for this to work?

Only viewmat, which I assumed you already loaded.

>  I did what you suggested and get a syntax error
>  followed by a crash.

I cannot reproduce this error.  What version are you running?  Did you watch for line-wrap too?

Also, if you ever get a crash in J, please report it.  (That is, post a complete, minimal script which consistently reproduces the
crash.)
- Show quoted text -

-Dan
___________________________________

Dan Bron to Programming
=============== 
        
Thanks to Tracy Harms, I realize the problem is probably that my verb  vm  is trying to return a verb (sys_timer).    I don't know
why this didn't cause a problem for me.  Anyway, a better definition would be:

       require 'viewmat'

       execute_later  =: 500&$: : (dyad define)
               wd 'timer ',":x
               sys_timer_z_ =: 3 : ('". y';'wd '' timer 0 '' ') @: (y"_)
               empty ''
       )


       vm =: verb define
               viewmat i. 16 16
               execute_later ' savemat_jviewmat_ jpath ''~temp\immex0.bmp'' '
       )


       vm ''

Does that work for everyone?

-Dan
___________________________________

Devon McCormick to Programming
=============== 

This still doesn't do it.  It appears to because you're running on a single example.  However, if you try this:

execute_later=: 500&$: : (dyad define)
   wd 'timer ',":x
   sys_timer_z_ =: 3 : ('". y';'wd '' timer 0 '' ') @: (y"_)
   empty ''
)

vm=: 3 : 0
   'flnm mat'=. y.
   viewmat mat
   execute_later ' savemat_jviewmat_ jpath ''',flnm,''''
)

showsome=: 3 : 0
   td=. 'C:\Temp\'
   vm (td,'fl0.bmp');i. 16 16
   vm (td,'fl1.bmp');2 o. 16%~i. 16 16
   vm (td,'fl2.bmp');%:+/\i. 16 16
)
                 
   showsome ''

you only get the file saved for "fl2.bmp" because that's what's on top when you return to immediate execution and the timer pops.

I've tried a lot of variations on the timer idea already and have not gotten it to work.  The image3 libraries work well - too bad you're forced to get them through the addons which doesn't always work when you're behind a company firewall.
___________________________________

Roelof K Brouwer  to Programming
=============== 
        
Hello Dan
Exactly the same problem as before i.e. good display followed by being stuck
in a loop and then J dies

cheers

Roelof
...
___________________________________

Devon McCormick to Programming
=============== 
        
As I mentioned in a private e-mail to Roelof, I've really spent a lot of time on this - I already looked into the timer route, among other things.  The image3 addons are quite easy to use and obviate this problem.

One caveat about the image3 addons: they use a global variable "jpeg_quality" which is set to 75 for 75% .jpg compression.  IMO, this is a very bad default as it silently degrades the quality of your image.  I usually set this to 90 or 99, e.g. "jpeg_quality_ima3_=: 90" within any script file that uses these functions.
___________________________________

bill lam  to programming
=============== 
        
On Sun, 14 Dec 2008, Roelof K Brouwer wrote:
> ( 1 4 14 {". COLOR16) viewmat i.4 4
>
> savemat_jviewmat_ 'C:\Temp\garbage.bmp'

The root of problem is that viewmat is implemented by J gl2 verbs and
gl2 can only render itself when J is idle. A possible solution is to
re-implement viewmat with other libraries. For example, in the recent
plot, Chris wrote cover verbs using gtk to emulate some J gl2 and the
plot itself does not need major modification to incorporate gtk
backend.  Parallel to this, cover verbs using gdiplus can be added to
viewmat to make it work without gl2 under m$ window.  The gdiplus
addon is a good starting point.  For linux jconsole, I suggest using
gtk instead.

--
regards,
====================================================
GPG key 1024D/4434BAB3 2008-08-24
gpg --keyserver subkeys.pgp.net --recv-keys 4434BAB3
??129 ???  ????????
   ?????  ?????  ?????  ?????
   ?????  ?????  ?????  ?????
___________________________________

Oleg Kobchenko  to Programming
=============== 
        
While you were talking about emacs, immex and gtk I went and generated
all the bitmaps from Pretty Pictures using the media/animate addon
and viewmat.

It is fairly straightforward. Get the addon with Package Manager.
  load 'media/animate/demo'
Pick a closest example as a template.
Copy/paste definitions. Run to verify.
Export to a folder/file. All bitmaps are saved automatically.

Note: it works with any graphics--gl2, OpenGL--that you can
place on isigraph.


On Dec 15, 2008, at 3:59, Oleg Kobchenko <olegykj@yahoo.com> wrote:

You are right about giving a chance for viewmat to update the screen.
For sequence of images I suggested animate addon in the link below.

Also you may try wd'msgs'.


Oleg
___________________________________

Roelof K Brouwer  to Programming
=============== 
        
Sorry I don't have much of an idea of what you are talking about. That is because I use J mainly as a calculation server for expressing algorithms that I develop and execute. Graphic output has been outside my domain so far and I was looking for a simple working way to produce black/white/grey graphs of matrices and then saving them in files, without my intervention, at several locations in my verb. The displays saved should be easily portable into a word document where I can resize if necessarily. I can do this with displays produced with plot ( density graphs ) but viewmat seemed so much nicer. If I used clipboard I would be restricted to one save unless I intervened.

cheers

Roelof
...
___________________________________

David Mitchell to Programming
=============== 
        
Perhaps this is too simple or has side effects I haven't encountered, but it
seems to work for me

   open'viewmat'

change

NB. =========================================================
vmrun=: 4 : 0
...
wd 'pmovex ',": 0 >. fx,fy,dif + fw,fh
wd 'pshow'

to
NB. =========================================================
vmrun=: 4 : 0
...
wd 'pmovex ',": 0 >. fx,fy,dif + fw,fh
jvm_show '' NB. DM 2008 12 16
wd 'pshow'

Save and load viewmat.ijs .

Run test_viewmat'' .  On Vista 32, for me, Viewmat now displays correctly and
saves a good BMP.

--
Regards,
David Mitchell
___________________________________

Eric Iverson to Programming
=============== 
        
I just started paying attention to this thread. I experimented enough to
verify that the problem is a gl2 bug. This should work as desired and it is
a bug that it doesn't. The bug is unlikely to be fixed until a future
release.

I haven't tried it yet, but David's suggested minor code modification to
viewmat is a plausible workaround for the bug.
___________________________________

Devon McCormick  to Programming
=============== 
        
David - this appears to work!  I'll leave it changed on my system to see if I encounter any side-effects.

Thanks,

Devon
___________________________________

Devon McCormick  to Programming
=============== 
        
To summarize the new version of vmrun explicitly, over-write the existing version thusly:

vmrun_jviewmat_=: 4 : 0
SHOW=: 0
ifRGB=: x -: 'rgb'
'DAT MAT ANG TITLE'=: x getvm y
wd 'pc jvm;pn *',TITLE
NB. if. IFWINCE do.
NB.   a=. 0 ". wd 'qm'
NB.   s=. -: ({.a),-/15 13 { a
NB.   wd 'xywh 0 0 ',":s
NB.   wd 'cc g isigraph;pas 0 0;pcenter'
NB. else.
NB.   wd 'xywh 0 0 180 180;cc g isigraph rightmove bottommove;pas 0 0;pcenter'
NB. end.
wd 'xywh 0 0 180 180;cc g isigraph rightmove bottommove;pas 0 0;pcenter'
HWNDP=: wd 'qhwndp'
hcascade''
hadd''
mat=. finite MAT
'rws cls'=. $mat
gwh=. glqwh''
mwh=. cls,rws
if. -. ifRGB do.
  mwh=. MINWH >. <. mwh * <./ gwh % cls,rws
end.
dif=. mwh - gwh
'fx fy fw fh'=. 0 ". wd 'qformx'
NB. wd 'pmovex ',": 0 >. fx,fy,dif + fw,fh
NB. wd 'pshow'

wd 'pmovex ',": 0 >. fx,fy,dif + fw,fh
jvm_show '' NB. DM 2008 12 16
wd 'pshow'
)

DevonMcCormick/ViewmatFixMsgs (last edited 2009-12-21 04:46:57 by DevonMcCormick)