package transformer;

import timedistortion.Ictx;
import timedistortion.PixelData;
import timedistortion.Point;

public class AbstractTransformer {

	protected Ictx ictx;

	public void setContext(Ictx ictx) {
		this.ictx = ictx;
	}

	protected void interpolate_linear(Point p, float pos) {	
		PixelData pd0 = ictx.at(pos);
		PixelData pd1 = ictx.at(pos + 1);
		
		int in2d_index = 3 * (p.dy * ictx.width + p.dx);
		float r0 = PixelData.unsigned(pd0.at(in2d_index));
		float g0 = PixelData.unsigned(pd0.at(in2d_index + 1));
		float b0 = PixelData.unsigned(pd0.at(in2d_index + 2));
		float r1 = PixelData.unsigned(pd1.at(in2d_index));
		float g1 = PixelData.unsigned(pd1.at(in2d_index + 1));
		float b1 = PixelData.unsigned(pd1.at(in2d_index + 2));

		float fraction = pos % 1.0f;
		p.r = fraction * r1 + (1.0f - fraction) * r0;
		p.g = fraction * g1 + (1.0f - fraction) * g0;
		p.b = fraction * b1 + (1.0f - fraction) * b0;
	}

}