So, after quite a bit of effort, here's a reasonably clean image resizing
verb. It uses nearest-neighbor when upsampling, and for downsampling it uses
an antialiasing algorithm: Each pixel is a weighted average of the pixels it
contains, based on the area of intersection.
The algorithm is separated in terms of dimensions, using the one-dimensional
resize1 on each dimension. Since the algorithms are much faster at full
rank, it uses |: rather than rank to apply on both axes.
Performance is still pretty slow--in the seconds for downsampling of
~1000x1000 images.
This could be a good addition to the image3 addon.
NB. resizedn takes weighted averages of items of y to produce x items.
NB. Each average uses the elements given by indices in getcells and weights
given by getmults.
NB. getcells produces a result of shape x,(x<.@%~#y). Its elements are
indices of items of y,
NB. and give the pixels which intersect with each pixel of the final result.
getcells =: ((%~i.)@[ <.@* #@]) { (>:@>.@%~ #) ]\ (,{:)@(i.@#)@]
NB. getweights produces the weights to multiply by cells. Its result has
shape x,(x<.@%~#y).
NB. It first rescales (i.>:x) to refer to indices in y, then takes each pair
of two and produces the
NB. weights, which start with -.1|start , end with 1|end , and have an
appropriate number of 1s in between.
NB. All weights are normalized at once by multiplying by x(%#)y.
getmults =: (%#) * 2 (((-.@{.),{:)@:(1&|) (0 _1}) 1$~ >:@:-~/"1@:<.)\
(%~i.@>:)@[ * #@]
NB. Multiply the weights by the cells, selecting from y while doing so
NB. (this lowers memory usage by using indices instead of rows).
resizedn =: 4 :'x (getweights (+/@:* {&y)"_1 getcells) y'
NB. Simple upsampling by copying pixels
resizeup =: ] #~ (%#) (2&(-~/\))@:<.@:(* i.) >:@#@]
resize1 =: resizeup`resizedn @. (< #)
resize =: 4 :0
SCALE =: x%(#x){.$y
for_z. x do. y=.0 |: z resize1 y end.
y=. 0&|:^:(($y) -&# x) y
)
Marshall
On Tue, Oct 4, 2011 at 8:05 AM, Cliff Reiter <[email protected]> wrote:
> Marshall,
> It is not anti-aliased, but resize_image from image3.ijs in the image3
> addon subsamples.
> Best, Cliff
>
> On 10/3/2011 10:52 PM, Marshall Lochbaum wrote:
> > Does anyone know of a good J implementation of anti-aliased rescaling for
> > bitmaps? My first attempt has been sloppy and really slow. The more
> > important part of the algorithm is downsampling--to increase the size
> I'll
> > probably just end up using nearest-neighbor.
> >
> > Marshall
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> >
>
> --
> Clifford A. Reiter
> Lafayette College, Easton, PA 18042
> http://webbox.lafayette.edu/~reiterc/
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm