NB. J script for Keys bicubic interpolation
NB. Cliff Reiter, January 2011

load '~addons/media/image3/view_m.ijs'

conext0=:{.,{.,],{:
conext3=:conext0"_1@:conext0
clamp=:0>.255<.<.
ts=:6!:2,7!:2@]

acon=: -:".;._2]0 : 0
0 2 0 0
_1 0 1 0
2 _5 4 _1
_1 3 _3 1
)

mp=: +/ . *

bicuev=:4 : 0
'X Y'=.y^"0 1 i.4
Y mp"2 acon mp X mp"3 acon mp"3 x
)

bicubic_resize_image=:4 : 0
szi=.2{.$y
szo=.<.szi*<./(|.x)%szi
get_patch=.(( [:<((i.4)+{.);(i.4)+{:)) { (conext3 y)"_
'indj indk'=:(>:(<:{.szi)*(i.%[){.szo);>:(<:{:szi)*(i.%[){:szo
z=.(szo,2}.$y)$0
for_j. i.#indj do.
  inds=.(j{indj),.indk
  as=.get_patch"1 <.inds
  t=.clamp as bicuev"_1 1 ]1&| inds
  z=.t j}z
  end.
z
)

resize_image=:4 : 0
szi=.2{.$y
szo=.<.szi*<./(|.x)%szi
ind=.(<"0 szi%szo) <.@*&.> <@i."0 szo
(<ind){y
)

some_of_paper=:0 : 0
acon
acon mp 2 3 5 7
0 0.5 1 p.~ acon mp 2 3 5 7
(0 0.5 1^/i.4) mp acon mp 2 3 5 7
bicuev
]p=:p: i.4 4
p bicuev 0 0
p bicuev 1 0
p bicuev 1 1
p bicuev 0.1 0.9

6 6 resize_image p
x=.6 6[y=.p
6 6 bicubic_resize_image p
conext3 p


NB. Figure 1
$b=:(?.5 8$4){?.4 3$255
view_image a=:720 720 resize_image b
view_image c=:720 720 bicubic_resize_image b

NB. Figure 2
$B=:read_image jpath,'~addons/media/image3/atkiln.jpg'
view_image B
ken=:100 100{.120 210}.B
view_image a=:720 720 resize_image ken
view_image c=:720 720 bicubic_resize_image ken
)