/// Evolver Copyright 2000, Morgan McGuire
/// matrix@alum.mit.edu   http://graphics3d.com/matrix
public class Dots extends OneArgumentOperator implements ELOperator 
{
    // Pseudo random
    static private long r = 12345678;

    public void apply(ELMatrix argument[], ELMatrix out, ELEnvironment environment)
    {
	checkArguments(argument);
        
	final int width = out.getWidth();
	final int height = out.getHeight();

	final float or[] = out.channel[0];
	final float og[] = out.channel[1];
	final float ob[] = out.channel[2];

	ELMatrix arg = argument[0];
	final float ir[] = arg.channel[0];
	final float ig[] = arg.channel[1];
	final float ib[] = arg.channel[2];

	int num = width * height;
	for(int offset = num - 1; offset >= 0; offset--) {
	    r = (r + (r >> 8) + 13) ^ ((r + 73221) >> 3) + (r << 17);

	    if ((r & 63) > 60) {
		int i = (int)r;
		int iffset = (int)((i < 0) ? -i : i) % num;
		//System.out.println("" + iffset + "  " + num);
		or[offset] = ir[iffset];
		og[offset] = ig[iffset];
		ob[offset] = ib[iffset];
	    } else { 
		or[offset] = 0f;
		og[offset] = 0f;
		ob[offset] = 0f;
	    }
	}
    }

    public String name()
    {
	return "Dots";
    }

    public String description()
    {
	return "Draws sparse, random dots using the palette of the input image.";
    }  

    protected String iconString()
    {
	return "Dot";
    }

    public boolean hasExternalInfluences() {
	return true;
    }
}
