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