source: flair-src/trunk/lib/FlairSensorActuator/src/Camera.cpp@ 169

Last change on this file since 169 was 169, checked in by Sanahuja Guillaume, 7 years ago

cosmetic

File size: 5.2 KB
RevLine 
[3]1// %flair:license{
[15]2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
[3]4// %flair:license}
5// created: 2014/03/06
6// filename: Camera.cpp
7//
8// author: Guillaume Sanahuja
9// Copyright Heudiasyc UMR UTC/CNRS 7253
10//
11// version: $Id: $
12//
13// purpose: Virtual class for Camera
14//
15//
16/*********************************************************************/
17
18#include "Camera.h"
19#include <FrameworkManager.h>
20#include <Tab.h>
21#include <TabWidget.h>
22#include <GroupBox.h>
23#include <GridLayout.h>
24#include <DataPlot1D.h>
25#include <Picture.h>
[123]26#include <VisionFilter.h>
[3]27#include <highgui.h>
28#include <fstream>
29
30using std::string;
31using namespace flair::core;
32using namespace flair::gui;
33
[15]34namespace flair {
35namespace sensor {
[3]36
[137]37Camera::Camera(string name, uint16_t width,
[15]38 uint16_t height, cvimage::Type::Format format)
[137]39 : IODevice(getFrameworkManager(), name) {
[15]40 plot_tab = NULL;
[121]41 logFormat=LogFormat::NONE;
[3]42
[15]43 // do not allocate imagedata, allocation is done by the camera
44 output = new cvimage((IODevice *)this, width, height, format, "out", false);
[121]45
[15]46 // station sol
[137]47 main_tab = new Tab(getFrameworkManager()->GetTabWidget(), name);
[15]48 tab = new TabWidget(main_tab->NewRow(), name);
49 sensor_tab = new Tab(tab, "Setup");
50 setup_groupbox = new GroupBox(sensor_tab->NewRow(), name);
51 setup_layout = new GridLayout(sensor_tab->NewRow(), "setup");
[157]52
[3]53}
54
[121]55//This contructor must only be called for a simulated device.
[15]56Camera::Camera(const IODevice *parent, std::string name)
57 : IODevice(parent, name) {
58 plot_tab = NULL;
59 main_tab = NULL;
60 tab = NULL;
61 sensor_tab = NULL;
62 setup_groupbox = NULL;
[3]63
[15]64 output = NULL;
[121]65 logFormat=LogFormat::NONE;
[3]66}
67
68Camera::~Camera() {
[15]69 if (main_tab != NULL)
70 delete main_tab;
[3]71}
72
[121]73void Camera::SetLogFormat(LogFormat logFormat) {
74 this->logFormat=logFormat;
75 switch(logFormat) {
76 case LogFormat::RAW:
77 AddDataToLog(output);
78 Warn("raw log of cvimage is not yet implemented\n");
79 break;
80 case LogFormat::JPG:
[122]81 Warn("logging cvimage to jpeg\n");
82 Warn("jpeg are not included in classical dbt file, as dbt does not handle variable length\n");
[121]83 break;
[122]84 default:
85 Warn("LogFormat unknown\n");
[121]86 }
87}
88
[3]89DataType const &Camera::GetOutputDataType() const {
[15]90 return output->GetDataType();
[3]91}
92
[15]93GroupBox *Camera::GetGroupBox(void) const { return setup_groupbox; }
[3]94
[15]95GridLayout *Camera::GetLayout(void) const { return setup_layout; }
[3]96
97void Camera::UseDefaultPlot(const core::cvimage *image) {
[15]98 if (tab == NULL) {
99 Err("not applicable for simulation part.\n");
100 return;
101 }
[3]102
[15]103 plot_tab = new Tab(tab, "Picture");
104 Picture *plot = new Picture(plot_tab->NewRow(), ObjectName(), image);
[3]105}
106
[15]107Tab *Camera::GetPlotTab(void) const { return plot_tab; }
[3]108
[15]109uint16_t Camera::Width(void) const { return output->GetDataType().GetWidth(); }
[3]110
111uint16_t Camera::Height(void) const {
[15]112 return output->GetDataType().GetHeight();
[3]113}
114
[15]115core::cvimage *Camera::Output(void) { return output; }
[3]116
[121]117void Camera::ProcessUpdate(core::io_data* data) {
[122]118 if(getFrameworkManager()->IsLogging() && getFrameworkManager()->IsDeviceLogged(this)) {
[124]119 if(logFormat==LogFormat::JPG) {
120 data->GetMutex();
121 IplImage *img=((cvimage*)data)->img;
122 string filename=getFrameworkManager()->GetLogPath()+"/"+ObjectName()+"_"+std::to_string(data->DataTime())+".jpg";
123 switch(((cvimage*)data)->GetDataType().GetFormat()) {
124 case cvimage::Type::Format::Gray:
125 saveToJpeg(img,filename,PictureFormat_t::Gray,PictureFormat_t::Gray);
126 break;
127 case cvimage::Type::Format::BGR:
128 saveToJpeg(img,filename,PictureFormat_t::RGB,PictureFormat_t::RGB);
129 break;
130 case cvimage::Type::Format::UYVY:
[125]131 saveToJpeg(img,filename,PictureFormat_t::YUV_422ile,PictureFormat_t::YUV_422p);
[124]132 break;
133 default:
134 Warn("cannot log to this format\n");
135 break;
136 }
137 data->ReleaseMutex();
[122]138 }
[121]139 }
140 IODevice::ProcessUpdate(data);
141}
142
[128]143void Camera::SavePictureToFile(string filename) const {
144 if(filename=="") filename="./"+ObjectName()+"_"+std::to_string(GetTime())+".jpg";
[169]145 string::size_type idx = filename.rfind('.');
[124]146
[128]147 if(idx != string::npos) {
[124]148 Printf("saving %s\n", filename.c_str());
[128]149 string extension = filename.substr(idx+1);
[124]150
151 output->GetMutex();
152 if(extension=="jpg") {
153 if(output->GetDataType().GetFormat()==cvimage::Type::Format::Gray) saveToJpeg(output->img,filename,PictureFormat_t::Gray,PictureFormat_t::Gray);
154 if(output->GetDataType().GetFormat()==cvimage::Type::Format::BGR) saveToJpeg(output->img,filename,PictureFormat_t::RGB,PictureFormat_t::RGB);
[125]155 if(output->GetDataType().GetFormat()==cvimage::Type::Format::UYVY) saveToJpeg(output->img,filename,PictureFormat_t::YUV_422ile,PictureFormat_t::YUV_422p);
[124]156 } else {
157 cvSaveImage(filename.c_str(),output->img);
158 }
159 output->ReleaseMutex();
[169]160 Printf("saving %s ok\n", filename.c_str());
[124]161
162 } else {
163 Warn("saving %s no file extension!\n", filename.c_str());
164 }
165}
166
167void Camera::SaveRawPictureToFile(string filename) const {
[15]168 Printf("saving %s, size %i\n", filename.c_str(), output->img->imageSize);
169 std::ofstream pFile;
170 pFile.open(filename);
171 output->GetMutex();
172 pFile.write(output->img->imageData, output->img->imageSize);
173 output->ReleaseMutex();
[3]174
[15]175 pFile.close();
[3]176}
177
178} // end namespace sensor
179} // end namespace flair
Note: See TracBrowser for help on using the repository browser.