

import javax.media.*;
import javax.media.renderer.VideoRenderer;

import javax.media.Format;
import javax.media.format.VideoFormat;
import javax.media.format.RGBFormat;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.util.Vector;
import java.util.Random;
import java.awt.geom.*;
// java3d packages
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.geometry.Cylinder;

import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.behaviors.mouse.*;

/*
 * This is a JMF renderer plugin. It texure maps the incoming
 * video frame to a 3D surface, making use of Java 3D 
 *
 */

class JMFTexture2D extends Texture2D implements VideoRenderer {
      
    protected RGBFormat inputFormat; 
    protected RGBFormat supportedRGB;
    protected Format [] supportedFormats;
    BufferedImage bi;
    ImageComponent2D ic;
    
   private static final String name = "JMFTexture2D";
    protected int       inWidth = 0;
    protected int       inHeight = 0;
    protected Component component = null;
    protected Rectangle reqBounds = null;
    protected Rectangle bounds = new Rectangle();
    protected boolean started = false;
    protected Object lastData = null;
    Canvas3D c;
    boolean firstFrame = true;
    
    public JMFTexture2D(Canvas3D c) {
           this.c = c;
                
          //super(Texture2D.BASE_LEVEL,Texture2D.RGB,256,256);
           this.setCapability(Texture2D.ALLOW_IMAGE_WRITE);
           this.setCapability(Texture2D.ALLOW_IMAGE_READ);
           
                 supportedRGB =  new RGBFormat(null,
                                      Format.NOT_SPECIFIED,
                                      Format.byteArray,
                                      Format.NOT_SPECIFIED,
                                      24,
                                      3, 2, 1,
                                      3, Format.NOT_SPECIFIED,
                                      Format.TRUE,
                                      Format.NOT_SPECIFIED);
        
          supportedFormats = new VideoFormat[] {supportedRGB };
          firstFrame = true;
                   
          String os = System.getProperty("os.name");
          System.out.println("running on " + os);

          bi = new BufferedImage(256,256, BufferedImage.TYPE_3BYTE_BGR);
          
        /*
      if ( os.startsWith("W") || os.startsWith("w")) {
            bi = new BufferedImage(256,
                                   256, 
                                   BufferedImage.TYPE_3BYTE_BGR); 
            
            super(ImageComponent.FORMAT_RGB,
                                      bi,
                                      true,
                                      true);
     //bi = new BufferedIma
        }else if (os.startsWith("S") || os.startsWith("s")){
            bi = new BufferedImage(256,
                                   256, 
                                   BufferedImage.TYPE_4BYTE_ABGR);  

            super(ImageComponent.FORMAT_RGBA,
                                      bi,
                                      true,
                                      true);            
        } else {
            bi = new BufferedImage(256,
                                   256, 
                                   BufferedImage.TYPE_3BYTE_BGR);   
         
            super(ImageComponent.FORMAT_RGB,
                                      bi,
                                      false,
                                      true);
        }
 */
        
        ImageComponent2D ic = new ImageComponent2D (ImageComponent.FORMAT_RGB,
                                                     bi,
                                                     false,
                                                     true);
        
        this.setImage(0, ic);
  
      //set bi as ImageComponenet object
       //.set(bi);
      
      //set ic as the image component for the texture, this.
      //this.setImage(0, ic);
        
           System.out.println("JMFTexture2D constructor");
        // Prepare supported input formats and preferred format
             
    }
    
    
  //process is a method that is called continually   

  public int process(Buffer buffer) {
        
        byte[] rawData =(byte[])(buffer.getData());
        //BufferedImage bimg = new BufferedImage(SS,SS, btype);
        byte[] byteData = ((DataBufferByte)bi.getRaster().getDataBuffer()).getData();

        byte alpha_1 = (byte)0xff;
        // System.out.println("in doProcess");
        return BUFFER_PROCESSED_OK;
    }
  
  //the following methods must be implemented
    
      public Object[] getControls() {
        // No controls
        return (Object[]) new Control[0];
    }

    /**
     * Return the control based on a control type for the PlugIn.
     */
    public Object getControl(String controlType) {
       try {
          Class  cls = Class.forName(controlType);
          Object cs[] = getControls();
          for (int i = 0; i < cs.length; i++) {
             if (cls.isInstance(cs[i]))
                return cs[i];
          }
          return null;
       } catch (Exception e) {   // no such controlType or such control
         return null;
       }
    }

    /*************************************************************************
     * PlugIn implementation
     *************************************************************************/

    public String getName() {
        return name;
    }
    
       public java.awt.Rectangle getBounds() {
        return reqBounds;
    }
    
     public Format [] getSupportedInputFormats() {
        return supportedFormats;
    }
    
 
   public boolean setComponent(java.awt.Component comp) {
        component = comp;
        return true;
    }
  
  public void open() throws javax.media.ResourceUnavailableException {
  }
  
  public Format setInputFormat(Format format) {
        if ( format != null && format instanceof RGBFormat &&
             format.matches(supportedRGB)) {
            
            inputFormat = (RGBFormat) format;
            Dimension size = inputFormat.getSize();
            inWidth = size.width;
            inHeight = size.height;
            // System.out.println("in setInputFormat = " + format);
            return format;
        } else
            return null;
    }

  public void setBounds(java.awt.Rectangle rectangle) {
  }
  public synchronized void close() {
  }
    
 public java.awt.Component getComponent() {
        return c;
    }   
 public void reset() {
        // Nothing to do
    }
  public void start() {
  }
  
  public void stop() {
  }
  

  
}