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

Last change on this file since 334 was 330, checked in by Sanahuja Guillaume, 5 years ago

use less bandwidth in vprnlite

File size: 5.5 KB
Line 
1// %flair:license{
2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
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>
26#include <VisionFilter.h>
27#include <highgui.h>
28#include <fstream>
29
30using std::string;
31using namespace flair::core;
32using namespace flair::gui;
33
34namespace flair {
35namespace sensor {
36
37Camera::Camera(string name, uint16_t width,
38 uint16_t height, cvimage::Type::Format format)
39 : IODevice(getFrameworkManager(), name) {
40 plot_tab = NULL;
41 logFormat=LogFormat::NONE;
42
43 // do not allocate imagedata, allocation is done by the camera
44 output = new cvimage((IODevice *)this, width, height, format, "out", false);
45
46 // station sol
47 main_tab = new Tab(getFrameworkManager()->GetTabWidget(), name);
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");
52
53}
54
55//This contructor must only be called for a simulated device.
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;
63
64 output = NULL;
65 logFormat=LogFormat::NONE;
66}
67
68Camera::~Camera() {
69 if (main_tab != NULL)
70 delete main_tab;
71}
72
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:
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");
83 break;
84 default:
85 Warn("LogFormat unknown\n");
86 }
87}
88
89DataType const &Camera::GetOutputDataType() const {
90 return output->GetDataType();
91}
92
93GroupBox *Camera::GetGroupBox(void) const { return setup_groupbox; }
94
95GridLayout *Camera::GetLayout(void) const { return setup_layout; }
96
97void Camera::UseDefaultPlot(const core::cvimage *image) {
98 if (tab == NULL) {
99 Err("not applicable for simulation part.\n");
100 return;
101 }
102
103 plot_tab = new Tab(tab, "Picture");
104 Picture *plot = new Picture(plot_tab->NewRow(), ObjectName(), image);
105}
106
107Tab *Camera::GetPlotTab(void) const { return plot_tab; }
108
109uint16_t Camera::Width(void) const { return output->GetDataType().GetWidth(); }
110
111uint16_t Camera::Height(void) const {
112 return output->GetDataType().GetHeight();
113}
114
115core::cvimage *Camera::Output(void) { return output; }
116
117void Camera::ProcessUpdate(core::io_data* data) {
118 if(getFrameworkManager()->IsLogging() && getFrameworkManager()->IsDeviceLogged(this)) {
119 Printf("todo logging and jpeg without opencv\n");
120 /*
121 if(logFormat==LogFormat::JPG) {
122 data->GetMutex();
123 //IplImage *img=((cvimage*)data)->img;
124 const cvimage* input = dynamic_cast<const cvimage*>(data);
125 if (!input) {
126 Warn("casting %s to cvimage failed\n",data->ObjectName().c_str());
127 return;
128 }
129 IplImage *img=input->img;
130
131 string filename=getFrameworkManager()->GetLogPath()+"/"+ObjectName()+"_"+std::to_string(data->DataTime())+".jpg";
132 switch(((cvimage*)data)->GetDataType().GetFormat()) {
133 case cvimage::Type::Format::Gray:
134 saveToJpeg(img,filename,PictureFormat_t::Gray,PictureFormat_t::Gray);
135 break;
136 case cvimage::Type::Format::BGR:
137 saveToJpeg(img,filename,PictureFormat_t::RGB,PictureFormat_t::RGB);
138 break;
139 case cvimage::Type::Format::UYVY:
140 saveToJpeg(img,filename,PictureFormat_t::YUV_422ile,PictureFormat_t::YUV_422p);
141 break;
142 default:
143 Warn("cannot log to this format\n");
144 break;
145 }
146 data->ReleaseMutex();
147 }*/
148 }
149 IODevice::ProcessUpdate(data);
150}
151
152void Camera::SavePictureToFile(string filename) const {
153 if(filename=="") filename="./"+ObjectName()+"_"+std::to_string(GetTime())+".jpg";
154 string::size_type idx = filename.rfind('.');
155Printf("todo SavePictureToFile without opencv\n");/*
156 if(idx != string::npos) {
157 Printf("saving %s\n", filename.c_str());
158 string extension = filename.substr(idx+1);
159
160 output->GetMutex();
161 if(extension=="jpg") {
162 if(output->GetDataType().GetFormat()==cvimage::Type::Format::Gray) saveToJpeg(output->img,filename,PictureFormat_t::Gray,PictureFormat_t::Gray);
163 if(output->GetDataType().GetFormat()==cvimage::Type::Format::BGR) saveToJpeg(output->img,filename,PictureFormat_t::RGB,PictureFormat_t::RGB);
164 if(output->GetDataType().GetFormat()==cvimage::Type::Format::UYVY) saveToJpeg(output->img,filename,PictureFormat_t::YUV_422ile,PictureFormat_t::YUV_422p);
165 } else {
166 cvSaveImage(filename.c_str(),output->img);
167 }
168 output->ReleaseMutex();
169 Printf("saving %s ok\n", filename.c_str());
170
171 } else {
172 Warn("saving %s no file extension!\n", filename.c_str());
173 }*/
174}
175
176void Camera::SaveRawPictureToFile(string filename) const {
177 Printf("saving %s, size %i\n", filename.c_str(), output->GetDataType().GetSize());
178 std::ofstream pFile;
179 pFile.open(filename);
180 output->GetMutex();
181 pFile.write(output->buffer, output->GetDataType().GetSize());
182 output->ReleaseMutex();
183
184 pFile.close();
185}
186
187} // end namespace sensor
188} // end namespace flair
Note: See TracBrowser for help on using the repository browser.