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