package resampler;

import javax.vecmath.Point2f;

import resampler.ui.MainFrame;

/**
 * the super class of all Resampler algorithms
 * @author likan
 *
 */
public abstract class Resampler
{
    /**
     * clipping mode of the resampling.  
     * true -
     *      only draw pixels in the selected
     *      quadrilateral in the destination image.
     * false -
     *      draw the whole destination image,
     *      with the source image tiles up
     *      and wrapping around.
     */
    protected boolean clipToDestination;
    /**
     * input image from which your code reads data
     */
    protected Image inputImage;
    /**
     * output image to which your code writes data
     */
    protected Image outputImage;
    /**
     * the four selected coordinates on the input image
     */
    protected Point2f []inputPoints;
    /**
     * the four selected coordinates on the output image
     */
    protected Point2f []outputPoints;
    
    /**
     * 
     * @param inputImage
     * @param outputImage
     * @param clippingMode
     */
    public Resampler(Image inputImage, Image outputImage, boolean clipToDestination)
    {
        this.inputImage = inputImage;
        this.outputImage = outputImage;
        this.clipToDestination = clipToDestination;
    }
    
    /**
     * do resampling, given the four points on the input image
     * and four points on the output image
     * @param ptsInput
     * @param ptsOutput
     */
    public void resample(Point2f[] ptsInput,
            Point2f[] ptsOutput)
    {
        inputPoints = ptsInput;
        outputPoints = ptsOutput;
        // first fill the image with white background
        outputImage.clear(1.0f);
        // do actual resampling here
        resample();
    }

    /**
     * override this method to do actual computation here
     * @param m
     */
    abstract protected void resample();

    
    /**
     * Mitchell-Netravali filter
     * @param x
     * @return
     */
    protected float filter(float x)
    {
        // TODO implement the filter here
        x = x < 0 ? -x : x;
        float retval;
        if(x <= 1)
        {
            x = 1 - x;
            retval = (((-21 * x + 27) * x + 9) * x + 1) / 18;
        }
        else if(x < 2)
        {
            x = 2 - x;
            retval = (7 * x - 6) * x * x / 18;
        }
        else
        {
            retval = 0;
        }
        return retval;
    }
}
