Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 package com.generalrobotix.ui.view.vsensor;
00012 
00013 import java.awt.GraphicsConfiguration;
00014 import java.awt.image.BufferedImage;
00015 
00016 import javax.media.j3d.DepthComponentFloat;
00017 import javax.media.j3d.GraphicsContext3D;
00018 import javax.media.j3d.Raster;
00019 
00025 @SuppressWarnings("serial")
00026 public class Canvas3DI extends javax.media.j3d.Canvas3D {
00027         
00028         protected Raster raster_;
00029         protected int width_;
00030         protected int height_;
00031         protected int rasterType_;
00032         protected GraphicsContext3D     gc_;
00033 
00034         
00035         int[]   colorBuffer_;
00036         float[] depthBuffer_;
00037         boolean finished_ = false;
00038 
00039         
00040         protected BufferedImage                 color_;
00041         protected DepthComponentFloat   depth_;
00042 
00047         public Canvas3DI(GraphicsConfiguration gc) {
00048                 super(gc);
00049         }
00050 
00056         public Canvas3DI(GraphicsConfiguration gc, boolean offscr,
00057                                          Raster raster, int width, int height, int rasterType) {
00058                 super(gc, offscr);
00059                 gc_ = getGraphicsContext3D();
00060                 raster_ = raster;
00061                 width_ = width;
00062                 height_ = height;
00063                 rasterType_ = rasterType;
00064 
00065                 if (rasterType_ == Raster.RASTER_DEPTH
00066                         || rasterType_ == Raster.RASTER_COLOR_DEPTH) {
00067                         depthBuffer_ = new float[width_ * height_];
00068                 }
00069                 if (rasterType_ == Raster.RASTER_COLOR
00070                         || rasterType_ == Raster.RASTER_COLOR_DEPTH) {
00071                     colorBuffer_ = new int[width_ * height_]; 
00072         }
00073                 setSize(width, height);
00074         }
00075 
00080         public int[] getColorBuffer() {
00081                 return colorBuffer_;
00082         }
00083 
00084         public byte[] getMonoBuffer() {
00085                 byte[] monoBuffer = new byte[width_*height_];
00086                 for (int i=0; i<width_; i++) {
00087                         for (int j=0; j<height_; j++) {
00088                                 monoBuffer[i+j*width_] = 
00089                                         (byte)(0.587*(0xff&(colorBuffer_[i+j*width_]>>8))
00090                                         +0.114*(0xff&(colorBuffer_[i+j*width_]))
00091                                         +0.299*(0xff&(colorBuffer_[i+j*width_]>>16)));
00092                         }
00093                 }
00094                 return monoBuffer;
00095         }
00096 
00101         public float[] getDepthBuffer() {
00102                 return depthBuffer_;
00103         }
00107         public void postSwap() {
00108                 super.postSwap();
00109                 _readRaster();
00110         }
00111 
00115         private void _readRaster() {
00116                 if (finished_)
00117                         return;
00118 
00119                 gc_.readRaster(raster_);
00120                 
00121                 
00122                 if (rasterType_ == Raster.RASTER_COLOR ||
00123                                 rasterType_ == Raster.RASTER_COLOR_DEPTH){
00124                         color_ = raster_.getImage().getImage();
00125                         color_.getRGB(0, 0, width_, height_, colorBuffer_, 0, width_);
00126             if (colorBuffer_.length != width_*height_){
00127                 System.out.println("invalid length of color buffer = "+colorBuffer_.length);
00128             }
00129                 }
00130 
00131                 
00132                 if (rasterType_ == Raster.RASTER_DEPTH ||
00133                                 rasterType_ == Raster.RASTER_COLOR_DEPTH){
00134                         depth_ = (DepthComponentFloat)raster_.getDepthComponent();
00135                         depth_.getDepthData(depthBuffer_);
00136             if (depthBuffer_.length != width_*height_){
00137                 System.out.println("invalid length of depth buffer = "+depthBuffer_.length);
00138             }
00139                 }
00140                 finished_ = true;
00141         }
00142 
00143         public void renderOnce(){
00144         finished_ = false;
00145     }
00146 }