/*

Java Graphics Demos: 

by Orion Lawlor, 12/1998

olawlor@acm.org



This code is totally free, and

should be considered in the public domain.

Use and modify it at will.

*/

import java.awt.*;

import java.awt.image.*;

//import ColorMap;

//import ByteTable;



class Demo 

{

	int cenOffset=600;

	int stdDev=41;

	int len;

	int w,h;

	ByteTable table=new ByteTable();

	byte store_pix[];

	public void init(ColorMap cm,byte pix[],int Nw,int Nh)

	{

		int x,y;

		w=Nw;h=Nh;

		store_pix=pix;

		int dex[]  ={0,90,110,140,200,255},

			red[]  ={0, 0,200,255,255,255},

			green[]={0, 0, 50,200,255,255},

			blue[] ={0, 0,  0, 50,255,255};

		cm.addBreaks(dex,red,green,blue);

		len=8*w;

		table.init(cenOffset,len,stdDev);

		for (y=0;y<h;y++)

		{

			int l=y*w;

			for (x=0;x<w;x++)

				pix[l+x]=0;

		}

	}

	public boolean handleAction(Event ev,Object obj) {return false;}

	public void fillBuffer(ColorMap cm,byte pix[])

	{

		int x,y,l;

		//Fill bottom two rows with noise

		for (y=h-2;y<h;y++)

		{

			l=y*w;

			int randDex=(int)((len-w)*Math.random());

			for (x=0;x<w;x++)

				pix[l+x]=table.rand[randDex+x];

		}

		

		//Blur image slightly, and move up

		for (y=1;y<h-2;y++)

		{

			l=y*w;

			for (x=1;x<w-1;x++)

			{

				int x1=l+x;

				int x2=l+x+w;

				int x3=l+x+2*w;

				int sum=//Note: this sum weights the center by 8, which makes a weak blur.

					(0xff&(int)pix[x1-1])+  (0xff&(int)pix[x1])+(0xff&(int)pix[x1+1])+

					(0xff&(int)pix[x2-1])+8*(0xff&(int)pix[x2])+(0xff&(int)pix[x2+1])+

					(0xff&(int)pix[x3-1])+  (0xff&(int)pix[x3])+(0xff&(int)pix[x3+1]);

				pix[l+x]=(byte)((sum)>>>4);//Note: a>>>4 == a/16

			}

		}

	}

}

