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

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

remove opencv dep

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