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