package ac.essex.ecj.functions;

import ec.gp.GPNode;
import ec.gp.GPData;
import ec.gp.ADFStack;
import ec.gp.GPIndividual;
import ec.EvolutionState;
import ec.Problem;
import ac.essex.ecj.data.DoubleData;
import ac.essex.ecj.problems.PixelProblem;

/**
 *
 * Gets the value of a given pixel, or depending on the offset values, that of a pixel around it.
 *
 * @author Olly Oechsle, University of Essex, Date: 17-Aug-2006
 * @version 1.0
 */
public class GetPixel extends GPNode {

    public void eval(final EvolutionState state, final int thread, final GPData input, final ADFStack stack, final GPIndividual individual, final Problem problem) {

        DoubleData result = ((DoubleData) (input));

        // evaluate the first child, and save the result
        children[0].eval(state, thread, input, stack, individual, problem);
        int offsetX = (int) result.value;

        // do the same with the second child.
        children[1].eval(state, thread, input, stack, individual, problem);
        int offsetY = (int) result.value;

        // get a reference to the problem object, through which we can access the image.
        PixelProblem pixelProblem = (PixelProblem) problem;

        // set the value of the result
        result.value = pixelProblem.image.getBooleanPixel(pixelProblem.x + offsetX, pixelProblem.y + offsetY);
    }

    public String toString() {
        return "getPixel";
    }

}
