source: flair-src/branches/sanscv/lib/FlairSensorActuator/src/Camera.cpp@ 328

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

add imagesize to iplimage

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