Visual Servoing Platform  version 3.5.1 under development (2022-07-06)
vpImageStorageWorker.h
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Image storage helper.
33  *
34  *****************************************************************************/
35 
36 #ifndef vpImageStorageWorker_h
37 #define vpImageStorageWorker_h
38 
39 #include <visp3/core/vpConfig.h>
40 
41 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
42 
43 #include <visp3/io/vpImageIo.h>
44 #include <visp3/io/vpImageQueue.h>
45 
54 template <class Type> class vpImageStorageWorker
55 {
56 public:
62  : m_queue(queue), m_dataname(""), m_cpt(1), m_ofs_data(), m_data_file_created(false)
63  {
64  m_seqname = queue.getSeqName();
65  m_record_mode = queue.getRecordingMode();
66  }
67 
71  void run()
72  {
73  try {
74  vpImage<Type> I;
75  std::string data;
76  char filename[FILENAME_MAX];
77 
78  for (;;) {
79  m_queue.pop(I, data);
80 
81  // Save image
82  sprintf(filename, m_seqname.c_str(), m_cpt);
83 
84  if (m_record_mode > 0) { // Single image
85  std::cout << "Save image: " << filename << std::endl;
86  } else if (m_cpt == 1) {
87  std::cout << "Started sequence saving: " << m_seqname << std::endl;
88  }
89  vpImageIo::write(I, filename);
90 
91  if (!data.empty()) {
92  if (!m_data_file_created) {
93  std::string parent = vpIoTools::getParent(m_seqname);
94  if (!parent.empty()) {
95  m_dataname = vpIoTools::getParent(m_seqname) + "/";
96  }
97  m_dataname += vpIoTools::getNameWE(m_seqname);
98  m_dataname += ".txt";
99 
100  std::cout << "Create data file: " << m_dataname << std::endl;
101  m_ofs_data.open(m_dataname);
102 
103  m_data_file_created = true;
104  }
105  m_ofs_data << vpIoTools::getName(filename) << " " << data << std::endl;
106  }
107 
108  m_cpt++;
109  }
110  } catch (const vpImageQueue<vpRGBa>::cancelled &) {
111  std::cout << "Receive cancel during color image saving." << std::endl;
112  if (m_data_file_created) {
113  std::cout << "Close data file: " << m_dataname << std::endl;
114  m_ofs_data.close();
115  }
116  } catch (const vpImageQueue<unsigned char>::cancelled &) {
117  std::cout << "Receive cancel during gray image saving." << std::endl;
118  if (m_data_file_created) {
119  std::cout << "Close data file: " << m_dataname << std::endl;
120  m_ofs_data.close();
121  }
122  }
123  }
124 
125 private:
126  vpImageQueue<Type> &m_queue;
127  std::string m_seqname;
128  std::string m_dataname;
129  int m_record_mode;
130  unsigned int m_cpt;
131  std::ofstream m_ofs_data;
132  bool m_data_file_created;
133 };
134 
135 #endif
136 #endif
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:292
int getRecordingMode() const
Definition: vpImageQueue.h:104
std::string getSeqName() const
Definition: vpImageQueue.h:114
vpImageStorageWorker(vpImageQueue< Type > &queue)
Definition of the vpImage class member functions.
Definition: vpImage.h:73
static std::string getNameWE(const std::string &pathname)
Definition: vpIoTools.cpp:1536
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1620
static std::string getName(const std::string &pathname)
Definition: vpIoTools.cpp:1514