Scripts/Zoom View

From J Wiki
Jump to navigation Jump to search

Wm yes check.png

Zoom View is a prototype of fast image zooming using indexed nearest neighbor interpolation. The speed is such that real-time mouse resize of the window produces continuous seamless view update. [{{#file: "zoomview.ijs"}} Download script: zoomview.ijs ]

NB. zoomview - very fast proportional image zoom
NB. 05/30/07 Oleg Kobchenko - initial version
NB. 05/31/07 Oleg Kobchenko - glpixelsx pass by pointer
NB. 06/07/07 Oleg Kobchenko - single dual-index From
NB. 02/06/08 Oleg Kobchenko - literate

The image is loaded from a file using Addons/media/platimg. [{{#file: "zoomview.ijs"}} Download script: zoomview.ijs ]

require 'gl2 media/platimg'

coclass 'pzoomview'
coinsert 'jgl2'

F=: 0 : 0
pc f; pn "Zoom View";
xywh 0 0 300 200;cc g isigraph rightmove bottommove;
pas 0 0;pcenter;
rem form end;

create=: 3 : 0
  wd F
  read y
  wd 'pshow;'

destroy=: 3 : 0

f_close=: destroy

f_g_paint=: 3 : 0
  (|.$PIC) calcZoom glqwh''

An additional performance boost comes from extended glpixelsx command, which accepts pointer to data values as opposed to actual values. Subset of indices along both axes can be taken with one { From. [{{#file: "zoomview.ijs"}} Download script: zoomview.ijs ]

  p=. (<Iy;Ix) { PIC
  glpixelsx Ox,Oy,Zx,Zy,symdat symget <'p'

calcZoom=: 4 : 0
  'Px Py'=: x
  'Vx Vy'=: y

Zoomed picture dimensions Zx,Zy and origin offset Ox,Oy are calculated based on view dimensions Vx,Vy and original picture dimensions Px,Py and proportion.

[{{#file: "zoomview.ijs"}} Download script: zoomview.ijs ]

  if. (Vx%Vy)>(Px%Py) do.
    Zx=: <.0.5+Vy*Px%Py
    Zy=: Vy
    Ox=: <.0.5+0.5*Vx-Zx
    Oy=: 0
    Zx=: Vx
    Zy=: <.0.5+Vx*Py%Px
    Ox=: 0
    Oy=: <.0.5+0.5*Vy-Zy

The second step is to produce nearest neighbor interpolation using scaled indices. [{{#file: "zoomview.ijs"}} Download script: zoomview.ijs ]

  Ix=: <.0.5+ (Px-1) * (Zx-1) %~ i.Zx
  Iy=: <.0.5+ (Py-1) * (Zy-1) %~ i.Zy

[{{#file: "zoomview.ijs"}} Download script: zoomview.ijs ]

read=: 3 : 0
  PIC=: readimg y

require 'dll'
symdat_z_=: 3 : 0   NB. symdat symget <'name'
  had=. {.memr y,(IF64{4 8),1,JPTR
  had+{.memr had,0,1,JPTR

A public verb in z locale is defined to open an arbitrary picture. [{{#file: "zoomview.ijs"}} Download script: zoomview.ijs ]

zoomview_z_=: conew & 'pzoomview'

Note 'Test'
  zoomview jpath'~system/examples/data/toucan.bmp'
  zoomview wd 'mbopen'

See Also

Contributed by Oleg Kobchenko