Resources.cpp
Go to the documentation of this file.
00001 #include <ros/console.h>
00002 #include <blort/Tracker/Resources.h>
00003 
00004 using namespace Tracking;
00005 
00006 // *** PRIVATE ***
00007 
00008 int Resources::SearchName(NameList* list, const char* filename){
00009         // parse through name list
00010         int i = 0;
00011         NameList::iterator it_name = list->begin();
00012         while(it_name != list->end()){
00013                 if(!strcmp((*it_name),filename))
00014                         return i;
00015                 ++it_name;
00016                 ++i;
00017         }
00018         
00019         // not found
00020         return -1;
00021 }
00022 
00023 // *** PUBLIC ***
00024 
00025 Resources::Resources(){
00026         m_capture = 0;
00027         m_image = 0;
00028         m_ip = 0;
00029         m_showlog = false;
00030 }
00031 
00032 Resources::~Resources(){
00033         ReleaseCapture();
00034         //ReleaseScreen();
00035         ReleaseImageProcessor();
00036         
00037         ReleaseShader();
00038         
00039         if(m_showlog) ROS_DEBUG("Resources released");
00040 }
00041 
00042 
00043 // *** Initialisation ***
00044 IplImage* Resources::InitCapture(const char* file){
00045         m_capture = cvCaptureFromFile(file);
00046         if(!m_capture) {
00047                 char errmsg[128];
00048                 sprintf(errmsg, "[Resources::InitCapture] Error could not read '%s'\n", file );
00049                 throw std::runtime_error(errmsg);
00050         }
00051         
00052         double w = cvGetCaptureProperty( m_capture, CV_CAP_PROP_FRAME_WIDTH );
00053         double h = cvGetCaptureProperty( m_capture, CV_CAP_PROP_FRAME_HEIGHT );
00054         
00055         if(m_showlog) ROS_DEBUG("Camera settings: %.1f x %.1f", w, h);
00056         
00057         m_image = cvQueryFrame(m_capture);
00058         cvConvertImage(m_image, m_image, CV_CVTIMG_FLIP | CV_CVTIMG_SWAP_RB);
00059         //cvFlip(m_image, m_image, 1);
00060         return m_image;
00061 }
00062 
00063 IplImage* Resources::InitCapture(float width, float height, int camID){
00064         m_capture = cvCreateCameraCapture(camID);
00065         if(!m_capture) {
00066                 char errmsg[128];
00067                 sprintf(errmsg, "[Resources::InitCapture] Error could not initialise camera\n" );
00068                 throw std::runtime_error(errmsg);
00069         }
00070         
00071         cvSetCaptureProperty(m_capture, CV_CAP_PROP_FRAME_WIDTH, width );
00072         cvSetCaptureProperty(m_capture, CV_CAP_PROP_FRAME_HEIGHT, height );
00073         
00074         double w = cvGetCaptureProperty( m_capture, CV_CAP_PROP_FRAME_WIDTH );
00075         double h = cvGetCaptureProperty( m_capture, CV_CAP_PROP_FRAME_HEIGHT );
00076         
00077         if(m_showlog) ROS_DEBUG("Camera settings: %.1f x %.1f", w, h);
00078         
00079         m_image = cvQueryFrame(m_capture);
00080         cvConvertImage(m_image, m_image, CV_CVTIMG_FLIP | CV_CVTIMG_SWAP_RB);
00081         //cvFlip(m_image, m_image, 1);
00082         return m_image;
00083 }
00084 
00085 ImageProcessor* Resources::InitImageProcessor(int width, int height){
00086         if(width==0||height==0){
00087                 ROS_DEBUG("[Resources::GetImageProcessor] Error ImageProcessor needs width and height for initialisation");
00088                 return 0;
00089         }
00090         
00091         if(!m_ip)
00092                 m_ip = new(ImageProcessor);
00093                 
00094         if( !(m_ip->init(width, height)) ){
00095                 ROS_DEBUG("[Resources::GetImageProcessor] Error could not initialise ImageProcessor");
00096                 delete(m_ip);
00097                 m_ip = 0;
00098                 return 0;
00099         }
00100 
00101         return m_ip;
00102 }
00103 
00104 // *** Release-functions ***
00105 void Resources::ReleaseCapture(){
00106         if(m_capture)
00107                 cvReleaseCapture(&m_capture);
00108         m_capture = 0;
00109 }
00110 
00111 void Resources::ReleaseImageProcessor(){
00112         if(m_ip)
00113                 delete(m_ip);
00114         m_ip = 0;
00115 }
00116 
00117 
00118 // *** Get-functions ***
00119 IplImage* Resources::GetNewImage(){
00120         
00121         if(!m_capture){
00122                 ROS_DEBUG("[Resources::GetNewImage] Error camera not initialised" );
00123                 return 0;
00124         }
00125         IplImage* img = 0;
00126         
00127         try{
00128                 img = cvQueryFrame(m_capture);
00129         }
00130         catch(char const* e){
00131                 ROS_DEBUG("[Resources::GetNewImage()] Warning: %s", e);
00132         }
00133         
00134         if(img != NULL){
00135                 m_image = img;
00136 //              cvConvertImage(m_image, m_image, CV_CVTIMG_SWAP_RB);
00137         }
00138         
00139         //cvFlip(m_image, m_image, 1);
00140         return m_image;
00141 }
00142 
00143 IplImage* Resources::GetImage(){
00144         if(!m_image){
00145                 return GetNewImage();
00146         }
00147         return m_image;
00148 }
00149 
00150 ImageProcessor* Resources::GetImageProcessor(){
00151         if(!m_ip){
00152                 ROS_DEBUG("[Resources::GetImageProcessor] Error ImageProcessor not initialised" );
00153                 return 0;
00154     }
00155         return m_ip;    
00156 }
00157 
00158 Shader* Resources::GetShader(int id){
00159         return m_shaderList[id];
00160 }
00161 
00162 // *** Add
00163 int     Resources::AddShader(   const char* shadername,
00164                                                         const char* vertex_file,
00165                                                         const char* fragment_file,
00166                                                         const char* header)
00167 {
00168         int shaderID=-1;
00169         /*
00170         // check if texture allready loaded before by comparing filename
00171         int shaderID = SearchShaderName(shadername);
00172         if(shaderID != -1)
00173                 return shaderID;        // return existing texture ID
00174         */
00175         
00176         // texture doesn't exist and needs to be loaded
00177         char vertex_fullname[FN_LEN];
00178         char fragment_fullname[FN_LEN];
00179         char header_fullname[FN_LEN];
00180         sprintf(vertex_fullname, "%s%s", m_shaderPath, vertex_file);
00181         sprintf(fragment_fullname, "%s%s", m_shaderPath, fragment_file);
00182         sprintf(header_fullname, "%s%s", m_shaderPath, header);
00183                 
00184         if(vertex_file)
00185                 vertex_file = &vertex_fullname[0];
00186         if(fragment_file)
00187                 fragment_file = &fragment_fullname[0];
00188         if(header)
00189                 header = &header_fullname[0];
00190 
00191         Shader* shader = new Shader(vertex_file, fragment_file, header);
00192 
00193         if(!shader->getStatus()){
00194                 ROS_DEBUG("[Resources::AddShader] Error failed to load shader %s", shadername);
00195                 ROS_DEBUG("[Resources::AddShader]   Vertex shader: '%s'", vertex_fullname);
00196                 ROS_DEBUG("[Resources::AddShader]   Fragment shader: '%s'", fragment_fullname);
00197                 ROS_DEBUG("[Resources::AddShader]   Header shader: '%s'", header_fullname);
00198                 delete(shader);
00199                 return -1;
00200         }
00201         
00202         char* name = new char[FN_LEN];
00203         strcpy(name, shadername);
00204         
00205         // put model into texture list
00206         m_shaderNameList.push_back(name);
00207         m_shaderList.push_back(shader);
00208         
00209         shaderID = m_shaderList.size()-1;
00210         
00211         if(m_showlog) ROS_DEBUG("Shader %i loaded: %s", shaderID, name);
00212         
00213         return shaderID;
00214 }
00215 
00216 // *** Release
00217 void Resources::ReleaseShader(){
00218         // release Shader
00219         for(unsigned int i = 0; i<m_shaderList.size(); ++i)
00220             delete(m_shaderList[i]);
00221         m_shaderList.clear();
00222         // release Shadernames
00223         for(unsigned int i = 0; i<m_shaderNameList.size(); ++i)
00224             delete(m_shaderNameList[i]);
00225         m_shaderNameList.clear();
00226 }
00227 
00228 void Resources::ReleaseShader(int id){
00229         delete(m_shaderList[id]);
00230         m_shaderList[id] = 0;
00231         if(m_showlog) ROS_DEBUG("Shader %i released", id);
00232 }
00233 
00234 
00235 // *** Search-functions ***
00236 int     Resources::SearchShaderName(const char* filename){
00237         return SearchName(&m_shaderNameList, filename);
00238 }


blort
Author(s): Michael Zillich, Thomas Mörwald, Johann Prankl, Andreas Richtsfeld, Bence Magyar (ROS version)
autogenerated on Thu Jan 2 2014 11:38:25