// %flair:license{ // This file is part of the Flair framework distributed under the // CECILL-C License, Version 1.0. // %flair:license} // created: 2014/03/06 // filename: Camera.cpp // // author: Guillaume Sanahuja // Copyright Heudiasyc UMR UTC/CNRS 7253 // // version: $Id: $ // // purpose: Virtual class for Camera // // /*********************************************************************/ #include "Camera.h" #include #include #include #include #include #include #include #include #include #include using std::string; using namespace flair::core; using namespace flair::gui; namespace flair { namespace sensor { Camera::Camera(string name, uint16_t width, uint16_t height, cvimage::Type::Format format) : IODevice(getFrameworkManager(), name) { plot_tab = NULL; logFormat=LogFormat::NONE; // do not allocate imagedata, allocation is done by the camera output = new cvimage((IODevice *)this, width, height, format, "out", false); // station sol main_tab = new Tab(getFrameworkManager()->GetTabWidget(), name); tab = new TabWidget(main_tab->NewRow(), name); sensor_tab = new Tab(tab, "Setup"); setup_groupbox = new GroupBox(sensor_tab->NewRow(), name); setup_layout = new GridLayout(sensor_tab->NewRow(), "setup"); } //This contructor must only be called for a simulated device. Camera::Camera(const IODevice *parent, std::string name) : IODevice(parent, name) { plot_tab = NULL; main_tab = NULL; tab = NULL; sensor_tab = NULL; setup_groupbox = NULL; output = NULL; logFormat=LogFormat::NONE; } Camera::~Camera() { if (main_tab != NULL) delete main_tab; } void Camera::SetLogFormat(LogFormat logFormat) { this->logFormat=logFormat; switch(logFormat) { case LogFormat::RAW: AddDataToLog(output); Warn("raw log of cvimage is not yet implemented\n"); break; case LogFormat::JPG: Warn("logging cvimage to jpeg\n"); Warn("jpeg are not included in classical dbt file, as dbt does not handle variable length\n"); break; default: Warn("LogFormat unknown\n"); } } DataType const &Camera::GetOutputDataType() const { return output->GetDataType(); } GroupBox *Camera::GetGroupBox(void) const { return setup_groupbox; } GridLayout *Camera::GetLayout(void) const { return setup_layout; } void Camera::UseDefaultPlot(const core::cvimage *image) { if (tab == NULL) { Err("not applicable for simulation part.\n"); return; } plot_tab = new Tab(tab, "Picture"); Picture *plot = new Picture(plot_tab->NewRow(), ObjectName(), image); } Tab *Camera::GetPlotTab(void) const { return plot_tab; } uint16_t Camera::Width(void) const { return output->GetDataType().GetWidth(); } uint16_t Camera::Height(void) const { return output->GetDataType().GetHeight(); } core::cvimage *Camera::Output(void) { return output; } void Camera::ProcessUpdate(core::io_data* data) { if(getFrameworkManager()->IsLogging() && getFrameworkManager()->IsDeviceLogged(this)) { if(logFormat==LogFormat::JPG) { data->GetMutex(); IplImage *img=((cvimage*)data)->img; string filename=getFrameworkManager()->GetLogPath()+"/"+ObjectName()+"_"+std::to_string(data->DataTime())+".jpg"; switch(((cvimage*)data)->GetDataType().GetFormat()) { case cvimage::Type::Format::Gray: saveToJpeg(img,filename,PictureFormat_t::Gray,PictureFormat_t::Gray); break; case cvimage::Type::Format::BGR: saveToJpeg(img,filename,PictureFormat_t::RGB,PictureFormat_t::RGB); break; case cvimage::Type::Format::UYVY: saveToJpeg(img,filename,PictureFormat_t::YUV_422ile,PictureFormat_t::YUV_422p); break; default: Warn("cannot log to this format\n"); break; } data->ReleaseMutex(); } } IODevice::ProcessUpdate(data); } void Camera::SavePictureToFile(string filename) const { if(filename=="") filename="./"+ObjectName()+"_"+std::to_string(GetTime())+".jpg"; string::size_type idx = filename.rfind('.'); if(idx != string::npos) { Printf("saving %s\n", filename.c_str()); string extension = filename.substr(idx+1); output->GetMutex(); if(extension=="jpg") { if(output->GetDataType().GetFormat()==cvimage::Type::Format::Gray) saveToJpeg(output->img,filename,PictureFormat_t::Gray,PictureFormat_t::Gray); if(output->GetDataType().GetFormat()==cvimage::Type::Format::BGR) saveToJpeg(output->img,filename,PictureFormat_t::RGB,PictureFormat_t::RGB); if(output->GetDataType().GetFormat()==cvimage::Type::Format::UYVY) saveToJpeg(output->img,filename,PictureFormat_t::YUV_422ile,PictureFormat_t::YUV_422p); } else { cvSaveImage(filename.c_str(),output->img); } output->ReleaseMutex(); } else { Warn("saving %s no file extension!\n", filename.c_str()); } } void Camera::SaveRawPictureToFile(string filename) const { Printf("saving %s, size %i\n", filename.c_str(), output->img->imageSize); std::ofstream pFile; pFile.open(filename); output->GetMutex(); pFile.write(output->img->imageData, output->img->imageSize); output->ReleaseMutex(); pFile.close(); } } // end namespace sensor } // end namespace flair