package transformer;

import timedistortion.Point;
import timedistortion.ITransform;
import timedistortion.PixelData;

public class Curtain extends AbstractTransformer implements ITransform {

	private static final float ceiling_speed = 3.0f;
	private static final float floor_speed = 4.0f;
	private static final float horizon = 0.4f;

	@Override
	public void t(Point p) {
		int horizon_height = (int) (horizon * ictx.height);
		float diff = horizon_height - p.dy;
		float where;
		if(diff < 0) {
			where = p.ti + Math.abs(diff) / floor_speed;
		}
		else {
			where = p.ti + diff / ceiling_speed;
		}
		int ti0 = (int)where;
		int ti1 = ti0 + 1;

		int in2d_index = 3 * (horizon_height  * ictx.width + p.dx);
		PixelData pd0 = ictx.at(ti0);
		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));
		PixelData pd1 = ictx.at(ti1);
		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 = where % 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;
	}

}
