import javax.swing.*;
import com.graphics3d.book.image.*;
import com.graphics3d.book.math.*;
import java.awt.*;


public class TestMatcher {
    static String filename = "../TestImages/test4-256.jpg";
    static String code = "Collage(HueShift(HueShift(Min(RockImage(), Collage(Rotate(0.22693111, {0.4339271, -0.060890462, -0.14983156}), {0.9689341, -0.31166452, 1.0}, EnvironmentMap(Interpolate(Derivative({0.5260445, -0.9943271, -0.83629435}), FishImage(), 0.22693111), Collage(VSplit(VGradient(), Interpolate(Min(RockImage(), Collage(0.22693111, {0.90638816, -0.3161332, 1.0}, EnvironmentMap({0.3538252, -0.11179591, 0.76402247}, {0.3538252, -0.11179591, 0.76402247}))), -0.75621074, Derivative(HueShift(LowColorNoise(), {-0.60136193, -0.9961748, 0.956824})))), {-0.6025814, -0.5151359, -0.2444776}, MiniBlur(Blur({0.48381335, 0.37744927, 0.18049468})))))), LeafImage()), LeafImage()), Rotate(Rotate(Interpolate(Max(Rotate(LeafImage(), Cosine({0.13357106, -0.48899084, 0.46273336})), LeafImage()), {0.26036343, -0.2474052, 0.3318561}, Add(Rotate(Interpolate(Max(LowNoise(), LeafImage()), {0.26036343, -0.2474052, 0.3318561}, Add(Rotate(Interpolate({0.26036343, -0.2474052, 0.3318561}, {0.26036343, -0.2474052, 0.3318561}, Max(Rotate(Blur(Noise()), FrequencyStars()), BitAnd(VSplit(LeafImage(), {-0.3782095, 0.06973941, 0.7708523}), Collage(Blur({0.19214037, 0.7060751, 0.9632803}), {-0.94875985, 0.9535051, 0.9628181}, {-0.94875985, 0.9535051, 0.9628181})))), FrequencyStars()), Zoom({0.84155905, 0.44450688, -0.6368634}, Interpolate(0.97325927, {0.43938103, 0.8003519, -0.8865588}, FrequencyStars())))), FrequencyStars()), Zoom({1.0, 0.35874906, -0.42753658}, {1.0, 0.35874906, -0.42753658}))), FrequencyStars()), {0.30287892, -0.7879979, 0.756324}), Distort(Distort(HueShift(Distort(Distort(Distort(HueShift(Distort(Rotate(Distort(Distort(HueShift(Distort(Rotate(Distort(Distort(Blur(Distort(Distort(RockImage(), ArcTangent(ExpandRange(ColorNoise()))), ArcTangent(ExpandRange(ColorNoise())))), ArcTangent(ExpandRange(ColorNoise()))), ArcTangent(ColorNoise())), RockImage()), ArcTangent(ColorNoise())), RockImage()), ColorNoise()), Blur(Add({0.214469, -0.05106278, -0.8334819}, Blur(Blur({-0.44914088, 0.86714524, -0.038012877}))))), Distort(Distort(Blur(Distort(Distort(-0.32682085, SunriseImage()), ArcTangent(0.84263784))), LeafImage()), ArcTangent(ColorNoise()))), ArcTangent(ColorNoise())), ExpandRange(RockImage())), 0.315652), ColorNoise()), Blur(ExpandRange(Add({0.44753784, 0.15750253, -0.9017423}, {0.214469, -0.05106278, -0.8334819})))), ExpandRange(RockImage())), ColorNoise()), Blur(Rotate({0.10588625, 0.2359776, -0.20337643}, {0.2809281, -0.97692156, -0.49766022}))))";

    public static void main(String arg[]) {
        new TestMatcher().run();
    }

    public void run() {
        JImage input = loadImage(filename);        
        GeneticParameters gp = new GeneticParameters("./");
        System.out.println("segmenting");
        Segmentation segmentation = new Segmentation(input, gp);
        System.out.println("done segmenting");
        segmentation.scaleBy(3);
        Collage.setSegmentation(segmentation);

        ELExpression expression = gp.parseELExpression(code);

        Behavior behavior = new Behavior(expression, 768, 768, false);
        
        JFrame frame = new JFrame();
        frame.setTitle("Frequency test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.getContentPane().add(behavior.toJImage());
        frame.pack();
        frame.show();

        System.out.println("done");
    }

    static JImage loadImage(String filename) {
        JImage image = null;

        try {
            image = new JImage(filename);
        } catch (java.io.IOException e) {
            System.err.println("'" + filename + "' not found.");
            System.exit(1);
        } catch (com.graphics3d.book.image.ImageLoadingException e) {
            System.err.println("'" + filename + "' corrupted.");
            System.exit(1);
        }

        return image;
    }

}
