import java.awt.*;
import java.awt.image.*;
import java.applet.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.image.TextureLoader;

public class pixelWiseTrans extends Applet

{
	public pixelWiseTrans()

	{
	    
	    
	    setLayout(new BorderLayout());
	    GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
	    
	    Canvas3D canvas3D = new Canvas3D(config);
	    add("Center",canvas3D);
	    BranchGroup scene = createSceneGraph();
	    scene.compile();
	    
	    SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
	    
		
	    // Default Value is 1 and it is perspective projection
	    
	    int policy = 0;
	    
	    View myview = new View();
	    myview.setProjectionPolicy(policy);
	    

	    
	    int mypolicy = myview.getProjectionPolicy();
	    System.out.println("Policy "+mypolicy);
	    
	    
	    simpleU.getViewingPlatform().setNominalViewingTransform();
	    simpleU.addBranchGraph(scene);
	}
    
    public BranchGroup createSceneGraph()
    {
	
	// Create the root of the BranchGraph
	
	BranchGroup objRoot = new BranchGroup();
	
	
	//Create a simple shape leaf node, add it to the scene graph
	float t = 0.0f;
	int components[] = new int[4];
	
	for(int k=9;k<=50;k+=1)
	    {
		String outFileName = new String("outnew/giffiles/out_");
		if(k<10)	outFileName+="00";
		else if(k<100) outFileName+="0";
		
		outFileName+=Integer.toString(k);
		outFileName+="new";
		outFileName+=".gif";
		
		TextureLoader loader = new TextureLoader(outFileName,this);

		
		ImageComponent2D image1 = loader.getImage();
		
		BufferedImage bufferedImage = image1.getImage();
		
		int height = bufferedImage.getHeight();
		int width = bufferedImage.getWidth();


		ColorModel colorModel = bufferedImage.getColorModel();
		
		boolean testAlpha = colorModel.hasAlpha();


		for(int x=0; x<width; x++)
		    {
			for(int y=0; y<height; y++)
			    {
				int pixel = bufferedImage.getRGB(x,y);


				int blue = colorModel.getBlue(pixel);
				int green = colorModel.getGreen(pixel);
				int red = colorModel.getRed(pixel);
				int alpha = colorModel.getAlpha(pixel);
				

				// For my images R=G=B


				if( blue < 50 ) 
				    {
					alpha = 255;
					red = 100;
					green = 200;
					blue = 300;
				    }
				else  alpha = 0;
		
				components[0] = red;
				components[1] = green;
				components[2] = blue;
				components[3] = alpha;
				
				int dataElement = colorModel.getDataElement(components,0);
				
				bufferedImage.setRGB(x,y,dataElement);

			    }   // End of inner for loop
		    }  // End of outer for loop
		
		TextureLoader loader2 = new TextureLoader(bufferedImage);
		
		ImageComponent2D image2 = loader2.getImage();

		int width1 = image2.getWidth();
		int height1 = image2.getHeight();

		Texture2D texture = new Texture2D(Texture2D.BASE_LEVEL,Texture2D.RGBA,width1,height1);
		texture.setEnable(true);

		
		try
		    {
			texture.setImage(0,image2);
		    }
		
		catch(Exception ex)
		    {
			System.out.println("Error");
		    } 

		Appearance appear = new Appearance();
		appear.setTexture(texture);
		
		
		QuadArray plane = new QuadArray(4, QuadArray.COORDINATES | QuadArray.TEXTURE_COORDINATE_2);
		
		Point3f p = new Point3f();
		
		p.set(-1.0f, 1.0f, t);
		plane.setCoordinate(0,p);
		
		p.set(-1.0f, -1.0f, t);
		plane.setCoordinate(1,p);
		
		p.set(1.0f, -1.0f, t);
		plane.setCoordinate(2,p);
		
		p.set(1.0f, 1.0f, t);
		plane.setCoordinate(3,p);
		
		t = t - 0.0001f;
		
		Point2f q = new Point2f();
		
		q.set(0.0f, 1.0f);
		plane.setTextureCoordinate(0,q);
		
		q.set(0.0f,0.0f);
		plane.setTextureCoordinate(1,q);
		
		q.set(1.0f,0.0f);
		plane.setTextureCoordinate(2,q);
		
		q.set(1.0f,1.0f);
		plane.setTextureCoordinate(3,q);
		
		
		Shape3D s3d = new Shape3D(plane,appear);

		Appearance app = s3d.getAppearance();

		TextureAttributes texAttr = new TextureAttributes();
		texAttr.setTextureMode(TextureAttributes.MODULATE);		
		app.setTextureAttributes(texAttr);
		
		
		RenderingAttributes renAttr = new RenderingAttributes();
		renAttr.setAlphaTestValue(255);
		renAttr.setAlphaTestFunction(RenderingAttributes.NOT_EQUAL);
		app.setRenderingAttributes(renAttr);
		
		s3d.setAppearance(app);

		objRoot.addChild(s3d);

		System.out.println("End...........");
				
	    } // End of for loop
	
	return objRoot;
	
	
	
    } // End of the method 
    
    public static void main(String args[])
    {
	Frame frame = new MainFrame(new pixelWiseTrans(), 600, 600);
    }
    
}
