Author Topic: Japi Experiments  (Read 16943 times)

0 Members and 2 Guests are viewing this topic.

Arnold

  • Guest
Japi Experiments
« on: November 21, 2016, 08:48:48 AM »
Hello,

while working a little bit with the Japi library last week I found this Julia demo in the OxygenBasic forum:

http://www.oxygenbasic.org/forum/index.php?topic=1144.msg9846#msg9846

The demo is coded in Freebasic and I ported it to OxygenBasic and added some options.

The program should run anywhere on harddisk or usb-stick if Java is installed. It expects Japi in /OxygenBasic/projectsC. If using a previous distribution of OxygenBasic then projectsC must be changed to ProjectsB.

I am not really interested in the Julia algorithm but maybe I could get some help about some of the parameters of the algorithm used in the program and what they are used for?

Roland

Edit: there was a small bug in the save options routine. In the meantime I have modified the correspondent lines.

Code: OxygenBasic
  1. 'Using the Japi library
  2. 'www.japi.de
  3.  
  4. $ filename "julia.exe"
  5. 'include "$/inc/RTL32.inc"
  6. 'include "$/inc/console.inc"
  7.  
  8. extern lib "$/projectsC/Japi/japi.dll" cdecl
  9. include once "$/projectsC/Japi/japi.h"
  10. end extern
  11.  
  12. j_setdebug(0)
  13. if( j_start() = J_FALSE ) then  
  14.    print "can't connect to JAPI server"    
  15.   end  
  16. endif  
  17.  
  18. indexbase 0
  19.  
  20. Dim int r(400*400), g(400*400), b(400*400)
  21. int gc
  22.  
  23. function julia(double x, y, r, s, int it) as double  
  24.   double dup
  25.   for k=0 to it
  26.      gc+=1
  27.      dup=x
  28.      x=x*x-y*y+r
  29.      y=2*dup*y+s
  30.      if abs(x*y)>4 then exit for
  31.   next
  32.   return k*abs(x*y)
  33. end function  
  34.  
  35. int m_p,it_p
  36. double xo_p, yo_P
  37. string color
  38. int col_val=2
  39.  
  40. sub initState()    
  41.     m_p=200 : it_p=12 : xo_p=-1.5 : yo_p=-1.5 : color="Green"  : col_val=2
  42. end sub
  43.  
  44. sub main()
  45.     double x , y , dz , rc , ic , xo , yo , orb
  46.  
  47.     sys frame, canvas
  48.      
  49.     frame=j_frame("Graphic Win - Julia Sets")
  50.     j_setflowlayout(frame,j_vertical)
  51.     j_setinsets(frame,50,20,20,20)
  52.     j_setnamedcolorbg(frame,j_light_gray)
  53.  
  54.     initState()    
  55.     toplbl=j_label(frame,"Julia params: m=" m_p ", it=" it_p ", xo=" xo_p ", yo=" yo_p ", color += " color)    
  56.     j_setnamedcolor(toplbl,j_blue)
  57.  
  58.     'Menu
  59.    menubar = j_menubar(frame)
  60.     file    = j_menu(menubar,"File")
  61.     save    = j_menuitem(file, "Save as BMP")
  62.     jprint   = j_menuitem(file,"Print")
  63.     quit    = j_menuitem(file,"Quit")
  64.  
  65.     calc    = j_menu(menubar,"Calc")
  66.     start   = j_menuitem(calc,"Start")
  67.     options = j_menuitem(calc, "Options")
  68.     reset   = j_menuitem(calc,"Reset")
  69.  
  70.     'Options Dialog
  71.    optdialog  = j_dialog(frame,"Options for Julia Sets")
  72.     j_setsize(optdialog,300,200)
  73.  
  74.     clr_lbl = j_label(optdialog, " Color:")
  75.     j_setpos(clr_lbl, 10,30)
  76.     radio =j_radiogroup(optdialog)
  77.     Red   = j_radiobutton(radio,"Red")
  78.     j_setpos(Red,20,60)
  79.     Green = j_radiobutton(radio,"Green")
  80.     j_setpos(Green,20,80)
  81.     Blue   = j_radiobutton(radio,"Blue")
  82.     j_setpos(Blue,20,100)
  83.    
  84.     p_lbl = j_label(optdialog,"Parameters:")
  85.     j_setpos(p_lbl, 150,30)
  86.     m_lbl = j_label(optdialog,"m:")
  87.     j_setpos(m_lbl, 150,60)
  88.     it_lbl = j_label(optdialog,"it:")
  89.     j_setpos(it_lbl, 150,80)
  90.     xo_lbl = j_label(optdialog,"xo:")
  91.     j_setpos(xo_lbl, 150,110)
  92.     yo_lbl = j_label(optdialog,"yo:")
  93.     j_setpos(yo_lbl, 150,130)
  94.     m_field = j_textfield(optdialog,5)
  95.     j_setpos(m_field, 180,60)    
  96.     it_field = j_textfield(optdialog,5)
  97.     j_setpos(it_field, 180,80)    
  98.     xo_field = j_textfield(optdialog,6)
  99.     j_setpos(xo_field, 180,110)    
  100.     yo_field = j_textfield(optdialog,6)
  101.     j_setpos(yo_field, 180,130)    
  102.  
  103.     close   = j_button(optdialog,"Ok")
  104.     j_setpos(close, 100,160)
  105.  
  106.     canvas=j_canvas(frame,400,400)
  107.     j_setnamedcolorbg(canvas,j_dark_gray)
  108.     lbl=j_label(frame,"INFO")
  109.     j_setsize(lbl,260,32)
  110.     j_setnamedcolor(lbl,j_blue)
  111.  
  112.     j_pack(frame)
  113.     j_setpos(frame, 200,100)
  114.     j_show(frame)
  115.  
  116.     obj=0
  117.     while obj != quit and obj != frame
  118.  
  119.        obj=j_getaction()
  120.                              
  121.        if obj=start then
  122.          j_settext(lbl,"Calculating - Please wait")
  123.  
  124.          gc=0
  125.          for m=m_p to 0 step -1
  126.             xo=xo_p
  127.             yo=yo_p
  128.             rc=0+m/100
  129.             ic=-1+m/100
  130.             dz=3/400
  131.             it=it_p
  132.             for i=0 to 399
  133.                x=xo+i*dz
  134.                for j=0 to 399
  135.                   y=yo+j*dz
  136.                   orb=julia(x,y,rc,ic,it)
  137.                   if color="Red" then   r(i+400*j)=int (5*orb)
  138.                   if color="Green" then g(i+400*j)=int (5*orb)
  139.                   if color="Blue" then  b(i+400*j)=int (5*orb)
  140.                next j
  141.             next i
  142.             j_drawimagesource(canvas,0,0,400,400,r,g,b)            
  143.          next m
  144.          j_settext(lbl,"MAIN ROUTINE CALLED "+str(gc) +" times" )
  145.          
  146.        end if
  147.  
  148.        if obj=reset then
  149.          initState()
  150.          j_settext(toplbl, "Julia params: m=" m_p ", it=" it_p ", xo=" xo_p ", yo=" yo_p ", color += " color)  
  151.          j_settext(lbl, "INFO")
  152.  
  153.          j_getimagesource(canvas,0,0,400,400,r,g,b)
  154.          for i=0 to <(400*400)
  155.             r[i]=0 : g[i]=0 : b[i]=0
  156.          next i            
  157.          j_drawimagesource(canvas,0,0,400,400,r,g,b)
  158.          j_setnamedcolorbg(canvas,j_dark_gray)        
  159.        end if
  160.  
  161.        if obj=jprint then j_print(canvas)
  162.  
  163.        if obj=options then
  164.          'place centered in frame        
  165.         x = j_getxpos(frame)+j_getwidth(frame)/2 - j_getwidth(optdialog)/2
  166.          y = j_getypos(frame)+j_getheight(frame)/2 -  j_getheight(optdialog)/2
  167.          j_setpos(optdialog,x,y)
  168.          'values
  169.         if col_val=1 then j_setstate(Red, j_true)
  170.          if col_val=2 then j_setstate(Green, j_true)
  171.          if col_val=3 then j_setstate(Blue, j_true)
  172.          j_settext(m_field, m_p)
  173.          j_settext(it_field, it_p)
  174.          j_settext(xo_field, xo_p)
  175.          j_settext(yo_field, yo_p)
  176.  
  177.          j_show(optdialog)
  178.         end if
  179.  
  180.         if obj=close or obj = optdialog then
  181.           j_hide(optdialog)
  182.           'update values
  183.          if j_getstate(Red)   = j_true then col_val=1 : color="Red"
  184.           if j_getstate(Green) = j_true then col_val=2 : color="Green"
  185.           if j_getstate(Blue)  = j_true then col_val=3 : color="Blue"
  186.           string tmp
  187.           tmp = j_gettext(m_field, tmp) : m_p=val(rtrim (ltrim (tmp)))
  188.           tmp = j_gettext(it_field, tmp) : it_p=val(rtrim (ltrim (tmp)))
  189.           tmp = j_gettext(xo_field, tmp) : xo_p=val(rtrim (ltrim (tmp)))
  190.           tmp = j_gettext(yo_field, tmp) : yo_p=val(rtrim (ltrim (tmp)))
  191.          
  192.           j_settext(toplbl, "Julia params: m=" m_p ", it=" it_p ", xo=" xo_p ", yo=" yo_p ", color += " color)
  193.         end if
  194.  
  195.         if obj=save then
  196.           image = j_image(400,400)
  197.           j_drawimagesource(image,0,0,400,400,r,g,b)          
  198.           if j_saveimage(image,"Julia.bmp",J_BMP) != 1 then
  199.             mbox "Error saving Bitmap file"
  200.           else
  201.             mbox "Saved as Julia.bmp"
  202.           end if  
  203.         end if
  204.  
  205.     wend
  206.     j_quit()
  207.  
  208. end sub
  209.  
  210. main()
  211.  


.
« Last Edit: November 22, 2016, 02:21:24 AM by Arnold »

JRS

  • Guest
Re: Japi Experiments
« Reply #1 on: November 21, 2016, 10:19:13 AM »
If JAPI wasn't a socket interface and supported the current Java UI API, it might be worth investing time in. IMHO

This also reminds me that I miss Rob's posts to the forum. I hope he is okay.
« Last Edit: November 21, 2016, 03:23:56 PM by John »

Mike Lobanovsky

  • Guest
Re: Japi Experiments
« Reply #2 on: November 21, 2016, 05:32:28 PM »
John, Charles, gentlemen,

I'm afraid I'll have to bring in some sad news.

I did some quick search on the net with what little I knew about Rob outside this forum: he spoke Flemish, he was my coeval (born 1956), and apart from being a good programmer and mathematician, he was very proficient in photography and had a good command of related optical gear.

So what I found is this.

Rest in peace, friend. I value every moment we spent together on this forum. I'll be missing you for years to come.


 :'(

JRS

  • Guest
Re: Japi Experiments
« Reply #3 on: November 21, 2016, 08:53:24 PM »
I will miss you Rob!




Charles Pegge

  • Guest
Re: Japi Experiments
« Reply #4 on: November 22, 2016, 01:33:52 AM »
Very sorry to hear of Rob's early departure from us. Off into the grand fractal universe.

Here is some of Rob's valuable material from my desktop (mostly thinBasic)

.

Arnold

  • Guest
Re: Japi Experiments
« Reply #5 on: November 23, 2016, 01:29:09 AM »
Yes, these are really sad news. As I started so late with Oxygen I often explore the forum for older messages and I found some lisp examples coded by him. Maybe they can be written in leanlisp or oxyscheme in some way too.

The julia example which I found with his message looks really nice and I wonder how the graphics could be influenced. I found some documentation about Julia fractals in internet but until now this seems to be a very complex topic.

Charles Pegge

  • Guest
Re: Japi Experiments
« Reply #6 on: November 23, 2016, 12:41:34 PM »
Hi Roland,

Here it is without the JAPI dependency: I've tightened up the algorithm and run it in the ConsoleG framework, as a continuous animation.

Code: [Select]
  #compact
  % Title "Animated Julia Fractal Demo"
  % Animated
  % ScaleUp
 '% PlaceCentral
 '% AnchorCentral
 '% NoEscape

  includepath "$\inc\"
  '% filename "t.exe"
  'include "RTL64.inc"
  include "ConsoleG.inc"


  indexbase 0
  pixel4 juliapix[512*512]
  int gc
     
  function JuliaCalc(double x, y, r, s, int it) as double
  =======================================================
  double dup
  int k
  for k=0 to it
     gc+=1
     dup=x
     x=x*x-y*y+r
     y=2*dup*y+s
     if abs(x*y)>4 then exit for
  next
  return k*abs(x*y)
  end function 
     
  int m_p,it_p
  double xo_p, yo_P
  string colorstr
  int col_val=2
     

  sub JuliaInitState()
  ====================
  m_p=200 : it_p=12 : xo_p=-1.5 : yo_p=-1.5 : colorstr="Green"  : col_val=2
  end sub

  function JuliaRender(float mf,rf,gf,bf)
  =======================================
  'mf 2 .. 0 'factor
  'rf 0 .. 1 'red
  'gf 0 .. 1 'green
  'bf 0 .. 1 'blue
  sys i , j
  double x , y , dz , rc , ic , xo , yo , orb, amp
  gc=0
  pixel4 jp at @JuliaPix
  xo=xo_p
  yo=yo_p
  rc=mf
  ic=mf-1.0
  dz=3/512
  amp=5.0
  it=it_p
  for i=0 to <512
    y=yo+i*dz
    for j=0 to <512
      x=xo+j*dz
      orb=JuliaCalc(x,y,rc,ic,it)*amp
      jp.r=orb*rf
      jp.g=orb*gf
      jp.b=orb*bf
      jp.a=255
      @jp+=4 'next pixel
    next j
  next i
  end function


  function main()
  ===============
  '
  indexbase 1
  static int init
  static float jf,jfi
  if not init then
    JuliaInitState()
    CreateSynthTexture JuliaTex,JuliaPix,512*512*4
    jf=0
    jfi=0.002
    init=1
  end if
  JuliaRender jf,1,1,0
  jf+=jfi
  if jf<0.0 then
    jf=0.0 : jfi=-jfi
  elseif jf>2.0 then
    jf=2.0 : jfi=-jfi
  end if
  pushstate
    move 15,-15.0
    'UserMovement m2,200
    flat : color 1,1,1,1
    texture JuliaTex
    MakeTexture @JuliaPix, 512, 512, texn[JuliaTex] 'dynamic texture
    quadnorm 15.0,15.0 'apply image texture to quad
    texture 0
  popstate
  '
  end function



  EndScript

The next step would be to include some key-based controls, to be able to zoom in and pan around. :)

Arnold

  • Guest
Re: Japi Experiments
« Reply #7 on: November 24, 2016, 12:15:03 AM »
Hi Charles,

your Julia demo works excellent. As it does contiuous animation there are always changing patterns. And the UserMovement macro is incredibly powerful. I can use arrow-keys (also with shift or control), page down, page up (also with shift or control), left/right/middle mouse button and move / turn  the object in all possible directions. This is not and will not be possible with the Japi library.

I have not yet figured out how the Julia algorithm works. But meanwhile I found that by changing the amp value it is already possible to create an infinite number of different patterns. I assume that by changing some other variables and manipulating the color values some more interesting effects are possible. This Julia algorithm is indeed amazing.

Roland

Mike Lobanovsky

  • Guest
Re: Japi Experiments
« Reply #8 on: November 24, 2016, 05:23:19 AM »
Hi Charles,

This is what I'm getting trying to run your Julia script with the latest Oxygen pack dated November 10, 2016 that I've just DL'ed from the Wizard.

I'm perfectly sure there's nothing of the kind located on my C:\ disk:

.

Arnold

  • Guest
Re: Japi Experiments
« Reply #9 on: November 24, 2016, 07:21:07 AM »
Hi Mike,

I commented out in /inc/Sysutil.inc line 12: librarypath "$\" (only for this special purpose) and executed \tools\BuildCo2.bat. The resulting co2.exe will not include the path for oxygen.dll. You could also use co2.exe of the previous distribution.

Roland

Mike Lobanovsky

  • Guest
Re: Japi Experiments
« Reply #10 on: November 24, 2016, 08:03:31 PM »
Oh yes Roland,

That did the trick for me. Thanks! :)

Charles Pegge

  • Guest
Re: Japi Experiments
« Reply #11 on: November 24, 2016, 11:23:28 PM »

Sorry about the 'baked' LibraryPath, Mike. Something to avoid in pre-compiled code!

Here is a slightly more efficient, FPUified version of the JuliaCalc function

Code: [Select]
  function JuliaCalc(double x, y, r, s, int it) as double
  =======================================================
  double xy,mx=4.0
  int k=it
  'for k=0 to it
  mov esi,it
  (
    dec esi
    jl exit
     'gc+=1
       inc dword gc
     'xy=x*y
       fld qword x
       fmul qword y
       fstp qword xy
     'x=x*x-y*y+r
       fld qword y
       fmul st0
       fld qword x
       fmul st0
       fsubp st1
       fadd qword r
       fstp qword x
     'y=2*xy+s
       fld qword xy
       fadd st0
       fadd qword s
       fstp qword y
     'if abs(xy)>mx then exit for
       fld qword mx
       fld qword xy
       fabs
       fcomip
       fstp st0
       ja exit
  'next
    repeat
  )
  'return k*abs(xy)
    fld qword xy
    fabs
    sub k,esi
    fimul dword k
    return
  end function 

Mike Lobanovsky

  • Guest
Re: Japi Experiments
« Reply #12 on: November 25, 2016, 01:55:55 AM »
Don't mention it, Charles! :)


Here is a slightly more efficient, FPUified version of the JuliaCalc function

Is that for the sake of avoiding the overhead inherent in HLL variable access? If yes than what's the estimated speed gain?

Charles Pegge

  • Guest
Re: Japi Experiments
« Reply #13 on: November 25, 2016, 09:19:40 AM »
Just looking at the code, I would say there is a 12..15% saving due to reduced memory access. But cache memory is very fast.

You might be able to squeeze it further by using SIMD registers, and do most of the work on 3 pixels simultaneously :)

Arnold

  • Guest
Re: Japi Experiments
« Reply #14 on: November 25, 2016, 10:12:40 AM »
Somehow this Julia is a bit seductive. I am still trying to improve the Japi demo and find out which params are important, but I already like the resulting images.

.