[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 |
|
---|
| 30 | using std::string;
|
---|
| 31 | using namespace flair::core;
|
---|
| 32 | using namespace flair::gui;
|
---|
| 33 |
|
---|
[15] | 34 | namespace flair {
|
---|
| 35 | namespace sensor {
|
---|
[3] | 36 |
|
---|
[137] | 37 | Camera::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");
|
---|
[3] | 52 | }
|
---|
| 53 |
|
---|
[121] | 54 | //This contructor must only be called for a simulated device.
|
---|
[15] | 55 | Camera::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;
|
---|
[3] | 62 |
|
---|
[15] | 63 | output = NULL;
|
---|
[121] | 64 | logFormat=LogFormat::NONE;
|
---|
[3] | 65 | }
|
---|
| 66 |
|
---|
| 67 | Camera::~Camera() {
|
---|
[15] | 68 | if (main_tab != NULL)
|
---|
| 69 | delete main_tab;
|
---|
[3] | 70 | }
|
---|
| 71 |
|
---|
[121] | 72 | void Camera::SetLogFormat(LogFormat logFormat) {
|
---|
| 73 | this->logFormat=logFormat;
|
---|
| 74 | switch(logFormat) {
|
---|
| 75 | case LogFormat::RAW:
|
---|
| 76 | AddDataToLog(output);
|
---|
| 77 | Warn("raw log of cvimage is not yet implemented\n");
|
---|
| 78 | break;
|
---|
| 79 | case LogFormat::JPG:
|
---|
[122] | 80 | Warn("logging cvimage to jpeg\n");
|
---|
| 81 | Warn("jpeg are not included in classical dbt file, as dbt does not handle variable length\n");
|
---|
[121] | 82 | break;
|
---|
[122] | 83 | default:
|
---|
| 84 | Warn("LogFormat unknown\n");
|
---|
[121] | 85 | }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[3] | 88 | DataType const &Camera::GetOutputDataType() const {
|
---|
[15] | 89 | return output->GetDataType();
|
---|
[3] | 90 | }
|
---|
| 91 |
|
---|
[15] | 92 | GroupBox *Camera::GetGroupBox(void) const { return setup_groupbox; }
|
---|
[3] | 93 |
|
---|
[15] | 94 | GridLayout *Camera::GetLayout(void) const { return setup_layout; }
|
---|
[3] | 95 |
|
---|
| 96 | void Camera::UseDefaultPlot(const core::cvimage *image) {
|
---|
[15] | 97 | if (tab == NULL) {
|
---|
| 98 | Err("not applicable for simulation part.\n");
|
---|
| 99 | return;
|
---|
| 100 | }
|
---|
[3] | 101 |
|
---|
[15] | 102 | plot_tab = new Tab(tab, "Picture");
|
---|
| 103 | Picture *plot = new Picture(plot_tab->NewRow(), ObjectName(), image);
|
---|
[3] | 104 | }
|
---|
| 105 |
|
---|
[15] | 106 | Tab *Camera::GetPlotTab(void) const { return plot_tab; }
|
---|
[3] | 107 |
|
---|
[15] | 108 | uint16_t Camera::Width(void) const { return output->GetDataType().GetWidth(); }
|
---|
[3] | 109 |
|
---|
| 110 | uint16_t Camera::Height(void) const {
|
---|
[15] | 111 | return output->GetDataType().GetHeight();
|
---|
[3] | 112 | }
|
---|
| 113 |
|
---|
[15] | 114 | core::cvimage *Camera::Output(void) { return output; }
|
---|
[3] | 115 |
|
---|
[121] | 116 | void Camera::ProcessUpdate(core::io_data* data) {
|
---|
[122] | 117 | if(getFrameworkManager()->IsLogging() && getFrameworkManager()->IsDeviceLogged(this)) {
|
---|
[124] | 118 | if(logFormat==LogFormat::JPG) {
|
---|
| 119 | data->GetMutex();
|
---|
| 120 | IplImage *img=((cvimage*)data)->img;
|
---|
| 121 | string filename=getFrameworkManager()->GetLogPath()+"/"+ObjectName()+"_"+std::to_string(data->DataTime())+".jpg";
|
---|
| 122 | switch(((cvimage*)data)->GetDataType().GetFormat()) {
|
---|
| 123 | case cvimage::Type::Format::Gray:
|
---|
| 124 | saveToJpeg(img,filename,PictureFormat_t::Gray,PictureFormat_t::Gray);
|
---|
| 125 | break;
|
---|
| 126 | case cvimage::Type::Format::BGR:
|
---|
| 127 | saveToJpeg(img,filename,PictureFormat_t::RGB,PictureFormat_t::RGB);
|
---|
| 128 | break;
|
---|
| 129 | case cvimage::Type::Format::UYVY:
|
---|
[125] | 130 | saveToJpeg(img,filename,PictureFormat_t::YUV_422ile,PictureFormat_t::YUV_422p);
|
---|
[124] | 131 | break;
|
---|
| 132 | default:
|
---|
| 133 | Warn("cannot log to this format\n");
|
---|
| 134 | break;
|
---|
| 135 | }
|
---|
| 136 | data->ReleaseMutex();
|
---|
[122] | 137 | }
|
---|
[121] | 138 | }
|
---|
| 139 | IODevice::ProcessUpdate(data);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[128] | 142 | void Camera::SavePictureToFile(string filename) const {
|
---|
| 143 | if(filename=="") filename="./"+ObjectName()+"_"+std::to_string(GetTime())+".jpg";
|
---|
| 144 | string::size_type idx = filename.rfind('.');
|
---|
[124] | 145 |
|
---|
[128] | 146 | if(idx != string::npos) {
|
---|
[124] | 147 | Printf("saving %s\n", filename.c_str());
|
---|
[128] | 148 | string extension = filename.substr(idx+1);
|
---|
[124] | 149 |
|
---|
| 150 | output->GetMutex();
|
---|
| 151 | if(extension=="jpg") {
|
---|
| 152 | if(output->GetDataType().GetFormat()==cvimage::Type::Format::Gray) saveToJpeg(output->img,filename,PictureFormat_t::Gray,PictureFormat_t::Gray);
|
---|
| 153 | if(output->GetDataType().GetFormat()==cvimage::Type::Format::BGR) saveToJpeg(output->img,filename,PictureFormat_t::RGB,PictureFormat_t::RGB);
|
---|
[125] | 154 | if(output->GetDataType().GetFormat()==cvimage::Type::Format::UYVY) saveToJpeg(output->img,filename,PictureFormat_t::YUV_422ile,PictureFormat_t::YUV_422p);
|
---|
[124] | 155 | } else {
|
---|
| 156 | cvSaveImage(filename.c_str(),output->img);
|
---|
| 157 | }
|
---|
| 158 | output->ReleaseMutex();
|
---|
| 159 |
|
---|
| 160 | } else {
|
---|
| 161 | Warn("saving %s no file extension!\n", filename.c_str());
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | void Camera::SaveRawPictureToFile(string filename) const {
|
---|
[15] | 166 | Printf("saving %s, size %i\n", filename.c_str(), output->img->imageSize);
|
---|
| 167 | std::ofstream pFile;
|
---|
| 168 | pFile.open(filename);
|
---|
| 169 | output->GetMutex();
|
---|
| 170 | pFile.write(output->img->imageData, output->img->imageSize);
|
---|
| 171 | output->ReleaseMutex();
|
---|
[3] | 172 |
|
---|
[15] | 173 | pFile.close();
|
---|
[3] | 174 | }
|
---|
| 175 |
|
---|
| 176 | } // end namespace sensor
|
---|
| 177 | } // end namespace flair
|
---|