Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 package com.generalrobotix.ui.view.tdview;
00016
00017 import java.awt.*;
00018 import java.awt.image.DirectColorModel;
00019 import java.awt.image.MemoryImageSource;
00020 import java.util.Vector;
00021 import javax.media.*;
00022 import javax.media.format.*;
00023
00024 public class MyBufferToImage
00025 {
00026
00027 public MyBufferToImage(VideoFormat format)
00028 {
00029 converterNotRequired = false;
00030 if((format instanceof YUVFormat) || (format instanceof RGBFormat))
00031 {
00032 this.format = format;
00033 size = format.getSize();
00034 prefFormat = new RGBFormat(size, size.width * size.height, Format.intArray, format.getFrameRate(), 32, -1, -1, -1, 1, -1, 0, -1);
00035 if(format.matches(prefFormat))
00036 {
00037 converterNotRequired = true;
00038 return;
00039 }
00040 Codec codec = findCodec(format, prefFormat);
00041 if(codec != null)
00042 converter = codec;
00043 outputBuffer = new Buffer();
00044 }
00045 }
00046
00047 private Codec findCodec(VideoFormat input, VideoFormat output)
00048 {
00049 Vector codecList = PlugInManager.getPlugInList(input, output, 2);
00050 if(codecList == null || codecList.size() == 0)
00051 return null;
00052 for(int i = 0; i < codecList.size(); i++)
00053 {
00054 String codecName = (String)codecList.elementAt(i);
00055 Class<?> codecClass = null;
00056 Codec codec = null;
00057 try
00058 {
00059 codecClass = Class.forName(codecName);
00060 if(codecClass != null)
00061 codec = (Codec)codecClass.newInstance();
00062 }
00063 catch(ClassNotFoundException _ex) { }
00064 catch(IllegalAccessException _ex) { }
00065 catch(InstantiationException _ex) { }
00066 catch(ClassCastException _ex) { }
00067 if(codec != null && codec.setInputFormat(input) != null)
00068 {
00069 Format outputs[] = codec.getSupportedOutputFormats(input);
00070 if(outputs != null && outputs.length != 0)
00071 {
00072 for(int j = 0; j < outputs.length; j++)
00073 if(outputs[j].matches(output))
00074 {
00075 Format out = codec.setOutputFormat(outputs[j]);
00076 if(out != null && out.matches(output))
00077 try
00078 {
00079 codec.open();
00080 return codec;
00081 }
00082 catch(ResourceUnavailableException _ex) { }
00083 }
00084
00085 }
00086 }
00087 }
00088
00089 return null;
00090 }
00091
00092 public Image createImage(Buffer buffer)
00093 {
00094 if(buffer == null || converter == null && !converterNotRequired || prefFormat == null || buffer.getFormat() == null || !buffer.getFormat().matches(format) || buffer.getData() == null || buffer.isEOM() || buffer.isDiscard())
00095 return null;
00096 int outputData[];
00097 RGBFormat vf;
00098 try
00099 {
00100 if(converterNotRequired)
00101 {
00102 outputData = (int[])buffer.getData();
00103 vf = (RGBFormat)buffer.getFormat();
00104 } else
00105 {
00106 int retVal = converter.process(buffer, outputBuffer);
00107 if(retVal != 0)
00108 return null;
00109 outputData = (int[])outputBuffer.getData();
00110 vf = (RGBFormat)outputBuffer.getFormat();
00111 }
00112 }
00113 catch(Exception ex)
00114 {
00115 System.err.println("Exception " + ex);
00116 return null;
00117 }
00118 Image outputImage = null;
00119
00120 int redMask = vf.getRedMask();
00121 int greenMask = vf.getGreenMask();
00122 int blueMask = vf.getBlueMask();
00123 DirectColorModel dcm = new DirectColorModel(32, redMask, greenMask, blueMask);
00124 MemoryImageSource sourceImage = new MemoryImageSource(size.width, size.height, dcm, outputData, 0, size.width);
00125 outputImage = Toolkit.getDefaultToolkit().createImage(sourceImage);
00126 return outputImage;
00127 }
00128
00129 private VideoFormat format;
00130 private Codec converter;
00131 private RGBFormat prefFormat;
00132 private Buffer outputBuffer;
00133 private Dimension size;
00134 private boolean converterNotRequired;
00135 }