zooming in on a jpg

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

zooming in on a jpg

audrey karperien-2
Hello.  I need to make animations of zooming in on still images, so, thinking it would be quite simple, I spent some time tonight and wrote the code at the bottom of this message. It works for my purposes, but is very slow especially with images that are not small. Have I re-invented a poorer wheel?  Is there some code that does this but is not so sloooooooooow and clunky?

Thanks,
Audrey

import ij.*;
import ij.gui.Roi;
import ij.gui.StackWindow;
import ij.io.OpenDialog;
import ij.plugin.*;
public class zoomer_ implements PlugIn {
public int height = 100;
public int width = 100;
public int roiheight = 100;
public int roiwidth = 100;
String NAME;
 public void run(String arg)
        {
            ImagePlus img=IJ.getImage();
            height=img.getHeight();
            width=img.getWidth();
            Roi roi = img.getRoi();
            if (roi==null)
            {
                IJ.showMessage("Please select an roi then try again");
                return;
            }
                     
            String NAME=img.getTitle();
            double scale=80;
            scale=IJ.getNumber("Rate of Scaling (%)?", (double)scale);
            roiwidth=(int)roi.getBounds().getWidth();
            roiheight=(int)roi.getBounds().getHeight();
            int w =(int)( width*(scale/100f));
            int h = (int)( height*(scale/100f));
           
            int cx = (int)roi.getBounds().getCenterX();
            int cy = (int)roi.getBounds().getCenterY();
           //scale it each time by the same amount
            //the user determines the amount by
            //inputting a number for the rate of scaling
            //e.g., if the rate of scaling is 50,
            //then the roi is always 50% of the current size
                       
            ImageStack s= new ImageStack(width, height);  
           
           for (int i = w, j = h;
                (i > roiwidth)&&(j>roiheight);
                i=(int)( i*(scale/100f)), j=(int)( j*(scale/100f)))
                {
                    IJ.makeRectangle(cx-i/2,cy-j/2, i, j);                      
                    IJ.run("Size...",
                            "width="+width+
                            " height="+height+
                            " constrain interpolate");
                    String name=NAME+i;
                    if(WindowManager.getCurrentImage().getHeight()!=height||
                            WindowManager.getCurrentImage().getWidth()!=width)
                    IJ.run("Canvas Size...", "width="+width+
                           " height="+height+" position=Center");
                    s.addSlice(name,
                                WindowManager.getCurrentImage().getProcessor());
                    IJ.run("Revert");              
                }
            IJ.run("Revert");
            img.setRoi(roi);
            StackWindow sw=new StackWindow(new ImagePlus(NAME+"Zoom", s));
            sw.setVisible(true);
        }        
       
}